AboutNarendra Expertise I can answer questions in C related to programming, data structures, pointers and file manipulation.
I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.
Experience 6.5
Organizations belong to Sun Microsystems
Awards and Honors Brain Bench Certified Expert C programmer.
Advanced System Software Certified
I found this C code on the internet. They claim that it works (reads bmp files) but when I run it, the compiler says that the first declaration contains syntax error. I'd very much appreciate if you could suggest what might be wrong with the first declaration and why the program won't compile. Also, I don't know where to put the filename that the program is supposed to work on.
Thanks a lot!
Eric
if ( header -> colorspace == INDEX_8_BIT )
;/* need to read color table */
/* move to start of image data */
rewind(infile);
fseek(infile, (long)bh.pixeloffset, SEEK_SET);
/* allocate space for new image */
image_ptr = init_image(*header, 0);
/* Windows BMP files are backwards so move write pointer to end of image */
writep = image_ptr + ( header -> rowbytes * header -> length );
/* only read 24 bit images for now */
if ( header -> colorspace == RGB_24_BIT )
{
/* loop through the image data */
for ( row = 0; row < header -> length; row++ )
{
/* backup one row */
writep -= header -> rowbytes;
for ( col = 0; col < header -> width; col++ )
{
/* read blue byte */
fread((writep+2), sizeof(unsigned char), 1, infile);
/* read green byte */
fread((writep+1), sizeof(unsigned char), 1, infile);
/* read red byte */
fread((writep+0), sizeof(unsigned char), 1, infile);
/* advance three bytes */
writep += 3;
}
/* backup another row */
writep -= header -> rowbytes;
}
}
else
fprintf(stderr, "%s is an indexed image.\n", infilename);
/* close file */
fclose(infile);
return image_ptr;
}
Knock yourself out (not all structure include in the code below):
if ( header -> colorspace == INDEX_8_BIT )
;/* need to read color table */
/* move to start of image data */
rewind(infile);
fseek(infile, (long)bh.pixeloffset, SEEK_SET);
/* allocate space for new image */
image_ptr = init_image(*header, 0);
/* Windows BMP files are backwards so move write pointer to end of image */
writep = image_ptr + ( header -> rowbytes * header -> length );
/* only read 24 bit images for now */
if ( header -> colorspace == RGB_24_BIT )
{
/* loop through the image data */
for ( row = 0; row < header -> length; row++ )
{
/* backup one row */
writep -= header -> rowbytes;
for ( col = 0; col < header -> width; col++ )
{
/* read blue byte */
fread((writep+2), sizeof(unsigned char), 1, infile);
/* read green byte */
fread((writep+1), sizeof(unsigned char), 1, infile);
/* read red byte */
fread((writep+0), sizeof(unsigned char), 1, infile);
/* advance three bytes */
writep += 3;
}
/* backup another row */
writep -= header -> rowbytes;
}
}
else
fprintf(stderr, "%s is an indexed image.\n", infilename);
/* close file */
fclose(infile);
return image_ptr;
}
Answer It will be easier to analyze, if you post the exact errors that you are seeing.
But, I don't see any error with the first statement of this code.
The only reason it could be throwing error is, it is missing main() function.
Try to add a main() function and then try to compile.