1

I am new to programming C and i am experimenting with file manipulation, i am trying to output a PPM file with its comments and rgb data values like so:

P3
# The same image with width 3 and height 2,
# using 0 or 1 per color (red, green, blue)
3 2 1
1 0 0   0 1 0   0 0 1
1 1 0   1 1 1   0 0 0

In this program i have been able to check that it has the correct format and read in to a struct but where i am stuck is how to collect the rgb data and then printing it out. Here is what i have so far. The method to display this information is the showPPM struct which i have started but do not know how to read the image struct and collect its rgb values and display it, any help would be great.

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

typedef struct {
 unsigned char red,green,blue;
} PPMPixel;

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


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

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

//check the image format
if (buff[0] != 'P' || buff[1] != '3') {
     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", 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 showPPM(struct * image){
int rgb_array[600][400];
int i;
int j;

for(i = 0; i<600; i++)
{
    for(j = 0; j<400; j++)
    {
        printf("%d", rgb_array[i][j]);
    }
}
}


int main(){
PPMImage *image;
image = readPPM("aab.ppm");
showPPM(image);
}
Sean Turnbull
  • 39
  • 1
  • 8
  • 1
    Your question is not specific enough. "Don't know where to go" is not a question. What specifically do you not understand? For starters, why don't you at least try to access the `image` that was passed into the `showPPM` function? That `image` contains a struct which has an array of `data`. Just iterate over that array and print out what you need. – kaylum Jan 19 '17 at 19:49
  • Thanks, changed my question to be more specific, the trouble i am having is i do not know how to print out the image struct as i am not used to c. – Sean Turnbull Jan 19 '17 at 19:53
  • What do you mean don't know how? Call `printf`. If you can't ask a specific question then it will be difficult to help. For example, do you mean you don't know how to write a loop to iterate over the data? Or what specifically? Also, I think your `readPPM` function is wrong. `fread(img->data, 3 * img->x, img->y, fp)` that will not work. There are spaces in the input so `3` bytes for each pixel is not enough. – kaylum Jan 19 '17 at 19:55
  • I'm just trying to get you to ask a specific question so that we can answer that. Your question initially said "don't know how to print". But seems you may mean "don't know how to iterate over an array"? – kaylum Jan 19 '17 at 20:01
  • Thats what i was trying to ask couldn't explain it sorry, how do i loop to iterate over the data that is in the image struct? Thanks – Sean Turnbull Jan 19 '17 at 20:01

1 Answers1

0

Your code looks suspiciously like:

read PPM file and store it in an array; coded with C

There were some really good comments and help on that as well.

So, I leave the other stuff below as further help:


Starting off somewhat vague here, to let you have chance to work through it yourself...

First, you need to read in the entire ppm file into a buffer. fread() is probably the function of choice for this. You can use ftell() to get the size of the file. Next, you can typecast your PPMImage structure on top of the buffer used by fread(), and from there, you should be able to access the data via the data field, maybe even using array notation if you wished. (I think this is the step you are missing right now...)

Once you can access the data, you should be able to iterate over the data and printf() or whatever you need to the console based on the input data.

Community
  • 1
  • 1
Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61