Hi Vart, here is a snap of my ppm file.
Code:
P3
# LEAD Tech.
628 658
255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255....
Blue - States, what type of file
DarkOrange - Information which is been commented out
Magenta - Row Column
Lime - max color
The interesting part here is, when I delcare the array straight away like
Code:
GLubyte IndiaMapPPM[628][658][3];
It just works fine. But when I allocate memory dynamically it gives me problem. If there was some problem with memory allocation or not allocated enough memory, i should have got sef fault when reading value into array. I font even get that. When I draw the pixel values, it just drawa call black, which i pretty wired. But this donst happen when I array is statically declared.
Here is some more bit of code of my PPMread function.
Code:
void ReadPPMFile( void )
{
FILE *ppmFile;
unsigned char Buffer[BUFSIZ];
unsigned int Row, Column, MaxColor;
int RowIndex, ColumnIndex, Flag = 0;
if( ( ppmFile = fopen( PPMFILENAME, "r") ) == NULL )
{
printf("Error: PPM file cannot be opened\n");
getchar();
exit( 1 );
}
if( fgets( Buffer, sizeof(Buffer), ppmFile ) != NULL )
{
if( strncmp( Buffer, "P3", 3 ) == 0)
{
printf("Error: File is not an ppm format\n");
getchar();
exit( 1 );
}
}
else
{
printf("Error: File is empty\n");
getchar();
exit ( 1 );
}
while( fgets( Buffer, sizeof( Buffer ), ppmFile) != NULL )
{
if( Buffer[0] == '#' )
continue;
if( sscanf( Buffer, "%d %d %d", &Row, &Column, &MaxColor ) != 3 )
{
if( sscanf( Buffer, "%d %d", &Row, &Column ) != 2 )
{
printf("Error: Reading row and column failed\n");
getchar();
exit( 1 );
}
else
{
if( fgets( Buffer, sizeof( Buffer ), ppmFile) != NULL )
{
if( sscanf( Buffer, "%d", &MaxColor) != 1)
{
printf("Error: reading max color failed\n");
getchar();
exit( 1 );
}
break;
}
}
}
else
break;
}
Width = Row;
Height = Column;
/* I have just commented this part since it wasn't working. */
/* if( AllocateMemory() )
exit(1); */
for( RowIndex = 0; RowIndex < Row; RowIndex++ )
for( ColumnIndex = 0; ColumnIndex < Column; ColumnIndex++ )
fscanf( ppmFile, "%d%d%d", &IndiaMapPPM[RowIndex][ColumnIndex][0],
&IndiaMapPPM[RowIndex][ColumnIndex][1],
&IndiaMapPPM[RowIndex][ColumnIndex][2] );
fclose( ppmFile );
}
Thank you
ssharish