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:
I think that the problem lies in this portion of the above code
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
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));
}
}
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));
remync
Comment