Read .ppm into One Dimensional Arrays For Red Green and Blue Values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Remync
    New Member
    • Sep 2008
    • 1

    #1

    Read .ppm into One Dimensional Arrays For Red Green and Blue Values

    I am writing a program to store a ppm into one dimensional arrays in order to manipulate the data. The image that is being read is not correct. The code is as follows:
    Code:
    if (strcmp (str, "P6") == 0)
     {
     fscanf(fp, "%d %d ", &(I->NCols), &(I->NRows));
      printf("\n\n******In the input image:******\n");
     printf("\nColumns=%d and Rows=%d\n", (I->NCols),(I->NRows));
     pixels =(I->NCols)*(I->NRows);
     printf("There are %d pixels\n", pixels);
     fscanf(fp, "%d ", &temp) ;
     printf("%d\n", temp);
     I->R = (int *) malloc(pixels*sizeof(int));
     I->G = (int *) malloc(pixels*sizeof(int));
     I->B = (int *) malloc(pixels*sizeof(int));
     for (row=0; row < I->NRows; row++)
     {
         for (col=0; col < I->NCols; col++)
         {
             (I->R[row*I->NCols+col]) = (unsigned char) (getc(fp));
             (I->G[row*I->NCols+col]) = (unsigned char) (getc(fp));
             (I->B[row*I->NCols+col]) = (unsigned char) (getc(fp));
    
         }
     }
    I think that the problem lies in this portion of the above code
    Code:
    (I->R[row*I->NCols+col]) = (unsigned char) (getc(fp));
             (I->G[row*I->NCols+col]) = (unsigned char) (getc(fp));
             (I->B[row*I->NCols+col]) = (unsigned char) (getc(fp));
    I'm not sure if I am using getc correctly. Now, I know that there are easier ways to do this. This method of one dimensional arrays is required by the assignment however. Essentially, there is a struct with items red, green and blue. Each of these is a pointer to a one dimensional array which contains only the red, green or blue values in the image. Anyway, let me know if I need to clarify and I thank anyone who can help in advance.

    remync
  • Flugeldorph
    New Member
    • Oct 2008
    • 11

    #2
    remync ,

    Give more details on the structure of 'I' I can probably help.

    Larry

    Comment

    • manontheedge
      New Member
      • Oct 2006
      • 175

      #3
      In your loop you have to adjust the COLUMN value, the value from the header, that you have in your variable is the number of PIXELS ... there are 3 values per pixel, so you have to multiply the column by 3 to properly get the data.

      Comment

      Working...