Question

I am struggling with some issue in my ansi c code. I want to read PPM picture (P6) and then write it to the color (int) arrays. Everything should work great, but unfortunatelly it isn't.

The problem is, that the reading freezes at some point - at this same point each time.

This is a code for reading:

int **red,**blue,**green;
    //... Some code, allocations etc.
    static unsigned char a[3];
    for(i = 0; i < x; i++)
    {
           for(j = 0; j < y; j++)
           {
                fread(a, 1, 3, pic);
                red[i][j] = a[0];
                //green[i][j] = a[1];
                //blue[i][j] = a[2];
           }
    }

When it reads value 24 it is getting crazy - before everything is ok. This value is asigned to the red and it is 24 till the end of loops.

I have noo idea, what is going on there, so this is why I am asking you guys, for some advice - what can be wrong here and how it can be fixed?

EDIT

This is my allocation:

red = (int **)malloc(sizeof(int*) * x);
green = (int **)malloc(sizeof(int*) * x);
blue = (int **)malloc(sizeof(int*) * x);

for (i = 0; i < x; i++)
{
       red[i] = (int*)malloc(sizeof(int) * y);
       green[i] = (int*)malloc(sizeof(int) * y);
       blue[i] = (int*)malloc(sizeof(int) * y);
}

EDIT2:

X is 473 an Y is 600

EDIT3:

I did as told and uploaded picture (different this time) with my source code on dropbox: code. Despite different picture still it freez at some point. This time it is 70.

Was it helpful?

Solution

In the code you linked to, you're not opening the file in binary mode : "rb".

See : what's the differences between r and rb in fopen

Note that fixing this might mean your fscanfs need tweaking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top