0

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);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
kanedaws
  • 9
  • 1
  • 1
    Removed the tag <[processing](https://stackoverflow.com/tags/processing/info)>. [Processing](https://processing.org/) is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. – Rabbid76 Nov 18 '19 at 09:42
  • 3
    Welcome to stackoverflow. You need to ask a specific question. "Debug my code" is not a specific question. Even if someone were to debug the code for you that would not be helpful to you in the long run. You need to learn to do that yourself. Have you stepped through the code in a debugger? Or adding debug prints? Or any other debugging steps? – kaylum Nov 18 '19 at 09:43
  • 1
    When your input image is a p5 or p6 image, you force the output to be a p6 image. But in generator, when image is p6, you explicitly draw some kind of rainbow.... What is your problem exactly? – Mathieu Nov 18 '19 at 10:52
  • Do you have a strong desire or a requirement to write your own code for reading and writing these files? Because [NetPBM](http://netpbm.sourceforge.net/) already comes with nice, easy-to-use library functions for performing those tasks. – Steve Summit Nov 18 '19 at 12:54

1 Answers1

0

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

  • `sizeof(char)` is defined to be 1. See [N1570](http://chimera.roma1.infn.it/SP/COMMON/iso-iec-9899-1990.pdf) 6.5.3.4: The `sizeof` and `_Alignof` operators, paragraph 4 – fcdt Sep 01 '20 at 19:28