2015-04-15 68 views
-1

我已經發現堆棧溢出代碼來做到這一點,但我有一些使用它的錯誤。我不確定我做錯了什麼,因爲我沒有太多的ppm文件經驗。謝謝大家的幫助。閱讀更改和打印PPM圖像

typedef struct { 
    int x, y; 
    PPMPixel *data; 
} PPMImage; 

#define CREATOR "RPFELGUEIRAS" 
#define RGB_COMPONENT_COLOR 255 

static PPMImage *readPPM(const char *filename = "ukraine.ppm") 
{ 
     char buff[16]; 
     PPMImage *img; 
     FILE *fp; 
     int c, rgb_comp_color; 
     //open PPM file for reading 
     fp = fopen("ukraine.ppm", "rb"); 
     if (!fp) { 
       fprintf(stderr, "Unable to open file '%s'\n", filename); 
       exit(1); 
     } 

     //read image format 
     if (!fgets(buff, sizeof(buff), fp)) { 
       perror(ukraine.ppm); 
       exit(1); 
     } 

    //check the image format 
    if (buff[0] != 'P' || buff[1] != '6') { 
     fprintf(stderr, "Invalid image format (must be 'P6')\n"); 
     exit(1); 
    } 

    //alloc memory form image 
    img = (PPMImage *)malloc(sizeof(PPMImage)); 
    if (!img) { 
     fprintf(stderr, "Unable to allocate memory\n"); 
     exit(1); 
    } 

    //check for comments 
    c = getc(fp); 
    while (c == '#') { 
    while (getc(fp) != '\n') ; 
     c = getc(fp); 
    } 

    ungetc(c, fp); 
    //read image size information 
    if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) { 
     fprintf(stderr, "Invalid image size (error loading '%s')\n",  ukraine.ppm); 
     exit(1); 
    } 

    //read rgb component 
    if (fscanf(fp, "%d", &rgb_comp_color) != 1) { 
     fprintf(stderr, "Invalid rgb component (error loading '%s')\n",  filename); 
     exit(1); 
    } 

    //check rgb component depth 
    if (rgb_comp_color!= RGB_COMPONENT_COLOR) { 
     fprintf(stderr, "'%s' does not have 8-bits components\n",  filename); 
     exit(1); 
    } 

    while (fgetc(fp) != '\n') ; 
    //memory allocation for pixel data 
    img->data = (PPMPixel*)malloc(img->x * img->y * sizeof(PPMPixel)); 

    if (!img) { 
     fprintf(stderr, "Unable to allocate memory\n"); 
     exit(1); 
    } 

    //read pixel data from file 
    if (fread(img->data, 3 * img->x, img->y, fp) != img->y) { 
     fprintf(stderr, "Error loading image '%s'\n", filename); 
     exit(1); 
    } 

    fclose(fp); 
    return img; 
} 
void writePPM(const char *filename = "ukraine.ppm", PPMImage *img) 
{ 
    FILE *fp; 
    //open file for output 
    fp = fopen(ukraine.ppm, "wb"); 
    if (!fp) { 
     fprintf(stderr, "Unable to open file '%s'\n",filename); 
     exit(1); 
    } 

    //write the header file 
    //image format 
    fprintf(fp, "P6\n"); 

    //comments 
    fprintf(fp, "# Created by %s\n",CREATOR); 

    //image size 
    fprintf(fp, "%d %d\n",img->x,img->y); 

    // rgb component depth 
    fprintf(fp, "%d\n",RGB_COMPONENT_COLOR); 

    // pixel data 
    fwrite(img->data, 3 * img->x, img->y, fp); 
    fclose(fp); 
} 

void changeColorPPM(PPMImage *img) 
{ 
    int i; 
    if(img){ 

     for(i=0;i<img->x*img->y;i++){ 
       img->data[i].red=RGB_COMPONENT_COLOR-img->data[i].red; 
       img->data[i].green=RGB_COMPONENT_COLOR-img->data[i].green; 
       img->data[i].blue=RGB_COMPONENT_COLOR-img->data[i].blue; 
     } 
    } 
} 

int main(){ 
    PPMImage *image; 
    image = readPPM("can_bottom.ppm"); 
    changeColorPPM(image); 
    writePPM("can_bottom2.ppm",image); 
    printf("Press any key..."); 
    getchar(); 

    return(0); 
} 

錯誤消息

[21:47:34] [email protected]:~/101/lab12 [8] gcc imageChange.c imageChange.c:18:47: error: expected â;â, â,â or â)â before â=â token 
imageChange.c:94:36: error: expected â;â, â,â or â)â before â=â token 
imageChange.c: In function âmainâ: imageChange.c:137:11: warning: assignment makes pointer from integer without a cast [enabled by default] 
[21:47:34] [email protected]:~/101/lab12 [9] Last login: Tue Apr 14 21:28:19 2015 from 48.253.21.198.tigernet.wifi.dyn.clemson.edu 
[23:42:52] [email protected]:~ [1]` 
+0

你錯過了幾個字符串的引號。看起來好像你要傳遞一個文件名參數,你應該使用它。您也不能有一個默認參數,後跟一個非默認參數。這些問題都不是PPM問題,它們只是不正確的C. –

+0

以避免混淆,請在每個開口大括號'{「之後縮進(比如說4個)空格,並且在每個大括號之前取消縮進}}使用此一致縮進甚至如果大括號不包括'if','do','while','else'聲明 – user3629249

+0

這是第18行?這是一般的行137? – user3629249

回答

-1

下面的代碼完全編譯。

它是硬編碼的文件ukraine.ppm

您可以輕鬆地修改該文件使用一個文件中全局字符數組

,對於輸入文件主要集

您可以輕鬆地修改文件使用文件全局字符數組

該主組爲輸出文件

#include <stdio.h> 
#include <stdlib.h> 

struct PPMPixel 
{ 
    char red; 
    char blue; 
    char green; 
}; 

struct PPMImage 
{ 
    unsigned x, y; 
    struct PPMPixel *data; 
}; 

#define CREATOR "RPFELGUEIRAS" 
#define RGB_COMPONENT_COLOR (255) 

static struct PPMImage *readPPM() 
{ 
    char buff[16]; 
    struct PPMImage *img; 

    FILE *fp; 

    int c; 
    int rgb_comp_color; 

    //open PPM file for reading 
    fp = fopen("ukraine.ppm", "rb"); 
    if (!fp) 
    { 
     perror("fopen for ukraine.ppm failed"); 
     // fprintf(stderr, "Unable to open file '%s'\n", filename); 
      exit(1); 
    } 

    // implied els,e fopen successful 

    //read image format // get first 15 bytes of image file 
    if (!fgets(buff, sizeof(buff), fp)) 
    { 
      perror("ukraine.ppm"); 
      exit(1); 
    } 

    // implied else, fgets successful 

    //check the image format 
    if (buff[0] != 'P' || buff[1] != '6') { 
     fprintf(stderr, "Invalid image format (must be 'P6')\n"); 
     exit(1); 
    } 

    // implied else, image file file has correct format indicators 

    //alloc memory for image 
    img = malloc(sizeof(struct PPMImage)); 
    if (!img) 
    { 
     perror("malloc for PPMImage failed"); 
     //fprintf(stderr, "Unable to allocate memory\n"); 
     exit(1); 
    } 

    // implied else, malloc successful 

    //check for comments 
    c = getc(fp); 
    while (c == '#') 
    { 
     while((c = getc(fp)) != '\n') ; 
    } 

    //read image size information 
    if (fscanf(fp, "%u %u", &img->x, &img->y) != 2) 
    { 
     perror("fscanf for ukraine.ppm image size parameters failed"); 
     exit(1); 
    } 

    // implied else, fscanf successful 

    //read rgb component 
    if (fscanf(fp, "%d", &rgb_comp_color) != 1) 
    { 
     perror("fscanf for rgb_comp_color in ukraine.ppm failed"); 
     //fprintf(stderr, "Invalid rgb component (error loading '%s')\n",  filename); 
     exit(1); 
    } 

    // implied else, fscanf successful 

    //check rgb component depth 
    if (rgb_comp_color!= RGB_COMPONENT_COLOR) 
    { 
     fprintf(stderr, "'ukraine.ppm' does not have 8-bits components\n"); 
     exit(1); 
    } 

    // implied else, rgb component depth correct 

    // consume rest of line 
    while (fgetc(fp) != '\n') ; 

    //memory allocation for pixel data 
    img->data = malloc(img->x * img->y * sizeof(struct PPMPixel)); 

    if (!img->data) //< corrected element being checked 
    { 
     perror("malloc for pixel data failed"); 
     //fprintf(stderr, "Unable to allocate memory\n"); 
     exit(1); 
    } 

    // implied else, malloc successful 

    //read pixel data from file 
    if (fread(img->data, 3 * img->x, img->y, fp) != img->y) 
    { 
     perror("fread for pixel data failed"); 
     // fprintf(stderr, "Error loading image '%s'\n", filename); 
     exit(1); 
    } 

    // implied else, fread successful 

    fclose(fp); 
    return img; 
} // end function: readPPM 


void writePPM(struct PPMImage *img) 
{ 
    FILE *fp; 
    //open file for output 
    fp = fopen("ukraine.ppm", "wb"); 
    if (!fp) 
    { 
     perror("fopen for ukraine.ppm for write failed"); 
     // fprintf(stderr, "Unable to open file '%s'\n",filename); 
     exit(1); 
    } 

    // implied else, fopen successful 

    //write the header file 
    //image format 
    fprintf(fp, "P6\n"); 

    //comments 
    fprintf(fp, "# Created by %s\n",CREATOR); 

    //image size 
    fprintf(fp, "%d %d\n",img->x,img->y); 

    // rgb component depth 
    fprintf(fp, "%d\n",RGB_COMPONENT_COLOR); 

    // pixel data 
    fwrite(img->data, 3 * img->x, img->y, fp); 
    fclose(fp); 
} // end function: writePPM 


void changeColorPPM(struct PPMImage *img) 
{ 
    unsigned i; 
    if(img) 
    { 

     for(i=0;i<img->x*img->y;i++) 
     { 
       img->data[i].red=RGB_COMPONENT_COLOR-img->data[i].red; 
       img->data[i].green=RGB_COMPONENT_COLOR-img->data[i].green; 
       img->data[i].blue=RGB_COMPONENT_COLOR-img->data[i].blue; 
     } 
    } 
} // end function: changeColorPPM 


int main() 
{ 
    struct PPMImage *image; 
    image = readPPM(); 
    changeColorPPM(image); 
    writePPM(image); 
    printf("Press any key..."); 
    getchar(); 

    return(0); 
} // end function: main 
+0

謝謝,當我編譯它說這個像素數據fread失敗:成功我假設這意味着它的工作。我沒有看到它創建操縱圖像的位置? @ user3629249 – user4766244