简体   繁体   中英

PGM File Processing in C

New to C, I'm trying to read an input PGM file that's either P2, P5, or P6 along with an output file from the command prompt, then read the data to a buffer, and finally write it to the output file. Everything is compiling but when I view the output file only P2 looks like the original file. P5 and P6 look like a rainbow mess. I'm including my code.

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

unsigned char* getPGMfile(char filename[], int *lwidth, int *lheight, int *lchannel);
int save(char filename[], unsigned char*data, int lwidth, int lheight, int lchannel);

int main(int argc, char **argv){
    int lwidth, lheight, lchannel;
    getPGMfile(argv[1], &lwidth, &lheight, &lchannel);
    unsigned char* data=(getPGMfile(argv[1], &lwidth, &lheight, &lchannel));
    printf("width, height, channel: %d %d %d\n",lwidth,lheight,lchannel);
    save(argv[2], data, lwidth, lheight, lchannel);
    return 0;
}

unsigned char* getPGMfile (char filename[], int *lwidth, int *lheight, int *lchannel){
        FILE *in_file;
        char ch;
        int row, col, type;
        int ch_int;
        in_file = fopen(filename, "r");
        if (in_file == NULL){
                fprintf(stderr, "Unable to open file %s\n\n", filename);
                exit(8);
        }
        printf("\nReading image file: %s\n", filename);
        ch = getc(in_file);
        if(ch != 'P'){
                printf("Not valid pgm/ppm file type\n");
                exit(1);
        }
        ch = getc(in_file);
        type = ch - 48;
        if((type != 2) && (type != 5) && (type != 6)){
                printf("Not valid pgm/ppm file type\n");
                exit(1);
        }
        while(getc(in_file) != '\n');
    while (getc(in_file) == '#'){
                while (getc(in_file) != '\n');
        }
        fseek(in_file, -1, SEEK_CUR);
    int width, height;
        fscanf(in_file,"%d%d", &width, &height);
    printf("width and heigth: %d %d\n", width, height);
    int intensity;
    fscanf(in_file,"%d", &intensity);
    printf("intensity: %d\n", intensity);

        unsigned char *data; 
        if(type == 2){
        *lchannel=5;
        int k=0;
            data = (unsigned char *) malloc((width*height)*sizeof(unsigned char));
                for (row=height-1; row >=0; row--)
                for (col=0; col<width; col++){
                        fscanf(in_file,"%d", &ch_int);
            data[k]=ch_int;k++;
 }
        }
        else if(type == 5){
        *lchannel=6;
                data=(unsigned char *) malloc(width*height*sizeof(unsigned char));
        fread(data, 1, (width*height), in_file);
        }
    else if(type == 6){
        *lchannel=6;
                data=(unsigned char *) malloc(3*(width*height)*sizeof(unsigned char));
        fread(data, 1, (3*(width*height)), in_file);
        }
        fclose(in_file);
        printf("\nDone reading file\n");
    *lwidth=width;
    *lheight=height;
    return data;
}

int save(char filename[],unsigned char *data, int lwidth, int lheight, int lchannel){
        FILE *fp;
    fp = fopen(filename, "wb");
    const int dimx = 800, dimy = 800;
    int i,j;
        if(lchannel == 5){
                fprintf(fp,"P5\n%u %u\n255\n", lwidth, lheight);
        fwrite(data,1,(lwidth*lheight),fp);
        printf("File %s saved.\n",filename);
        }
        else if(lchannel == 6){
        fprintf(fp,"P6\n%u %u\n255\n", lwidth, lheight);
        for(j=0;j<dimy;++j){
            for(i=0;i<dimx;++i){
                static unsigned char color[3];
                color[0]=i%256;
                color[1]=j%256;
                color[2]=(i*j)%256;
                fwrite(color,1,3,fp);
            }
        }
        printf("File %s saved.\n",filename);
        }
        fclose(fp);
    return(0);
    free(data);
}

The fread size should be sizeof(unsigned char) rather than 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM