View Single Post
  #6 (permalink)  
Old 03-30-2008, 06:47 AM
nutario's Avatar   
nutario nutario is offline
Newbie
 
Join Date: Mar 2008
Location: Germany
Posts: 23
Rep Power: 3
nutario is on a distinguished road
Send a message via ICQ to nutario Send a message via Skype™ to nutario
Default Re: How to write image data to binary PGM file format(P5)?

Code:
for(i=Y_min;i<=Y_max;i++){
for(j=XL;j<=XR;j++){
temp[(i*width)+j]= (unsigned char)output[i][j];
}
}
With this code you probably don't get a linear arry of unsigned char, with no memory holes.
e.g:
width = 10; height = 5;
Situation: ouy are at the end of the first line:
i = 0 and j = 5 => temp[(0*10)+5] = temp[5]
The next char you'll put in the temp array will be at position:
i = 1 j = 0 => temp[(1*10)+0] = temp[10]

You should write it like this to get a well formed array:
Code:
int count = 0;
for( i = >_min; i <= Y_MAX; i++)
{
    for(j = XL; j <= XR; j++)
    {
       temp[count] = (unsigned char)output[i][j];  //or ... = output[i][j]
       count++;
     }
}
Reply With Quote