AllExperts > C 
Search      
C
Volunteer
Answers to thousands of questions
 Home · More C Questions · Answer Library  · Encyclopedia ·
More C Answers
Question Library

Ask a question about C
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Narendra
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

 
   

You are here:  Experts > Computing/Technology > C/C++ > C > read BMP

C - read BMP


Expert: Narendra - 4/17/2007

Question
Hi,

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

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <mem.h>
#include <errno.h>


unsigned char* read_windows_bmp(const char* infilename, ImageHeader* header)
{
FILE*infile = NULL;/* input file handle */
unsigned char*image_ptr = NULL;/* pointer to output image in memory */
unsigned char*writep = NULL;/* temp pointer writing to image */
BMPHeaderbh;/* BMP header */
charmode[] = "rb";/* mode to write to file */
unsigned longrow;/* row iterator */
unsigned longcol;/* column iterator */


/* Open the BMP File */
if ( (infile = fopen(infilename, mode)) == NULL )
{
fprintf(stderr, "can't open %s\n", infilename);
return image_ptr;
}

/* read first two bytes */
fread(&bh.magic1, sizeof(unsigned char), 1, infile);
fread(&bh.magic2, sizeof(unsigned char), 1, infile);

/* test to see if this is really a BMP file */
if (bh.magic1 != 'B' && bh.magic2 != 'M')
return image_ptr;

/* read header information */
pm_read_little_long(infile, (long*)&bh.filesize);
pm_read_little_short(infile, (short*)&bh.res1);
pm_read_little_short(infile, (short*)&bh.res2);
pm_read_little_long(infile, (long*)&bh.pixeloffset);
pm_read_little_long(infile, (long*)&bh.bmisize);
pm_read_little_long(infile, (long*)&bh.cols);
pm_read_little_long(infile, (long*)&bh.rows);
pm_read_little_short(infile, (short*)&bh.planes);
pm_read_little_short(infile, (short*)&bh.bitsperpixel);
pm_read_little_long(infile, (long*)&bh.compression);
pm_read_little_long(infile, (long*)&bh.cmpsize);
pm_read_little_long(infile, (long*)&bh.xscale);
pm_read_little_long(infile, (long*)&bh.yscale);
pm_read_little_long(infile, (long*)&bh.colors);
pm_read_little_long(infile, (long*)&bh.impcolors);

/* If compressed, bail */
if ( bh.compression )
{
fprintf(stderr, "%s is compressed.\n", infilename);
return image_ptr;
}

/* set out header information */
header -> width = bh.cols;
header -> length = bh.rows;
header -> spp = (bh.bitsperpixel == 24) ? 3 : 1;
header -> rowbytes = header -> width * header -> spp;
header -> colorspace = (bh.bitsperpixel == 24) ? RGB_24_BIT : INDEX_8_BIT;

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):

unsigned char* read_windows_bmp(const char* infilename, ImageHeader* header)
{
FILE*infile = NULL;/* input file handle */
unsigned char*image_ptr = NULL;/* pointer to output image in memory */
unsigned char*writep = NULL;/* temp pointer writing to image */
BMPHeaderbh;/* BMP header */
charmode[] = "rb";/* mode to write to file */
unsigned longrow;/* row iterator */
unsigned longcol;/* column iterator */

/* Open the BMP File */
if ( (infile = fopen(infilename, mode)) == NULL )
{
fprintf(stderr, "can't open %s\n", infilename);
return image_ptr;
}

/* read first two bytes */
fread(&bh.magic1, sizeof(unsigned char), 1, infile);
fread(&bh.magic2, sizeof(unsigned char), 1, infile);

/* test to see if this is really a BMP file */
if (bh.magic1 != 'B' && bh.magic2 != 'M')
return image_ptr;

/* read header information */
pm_read_little_long(infile, (long*)&bh.filesize);
pm_read_little_short(infile, (short*)&bh.res1);
pm_read_little_short(infile, (short*)&bh.res2);
pm_read_little_long(infile, (long*)&bh.pixeloffset);
pm_read_little_long(infile, (long*)&bh.bmisize);
pm_read_little_long(infile, (long*)&bh.cols);
pm_read_little_long(infile, (long*)&bh.rows);
pm_read_little_short(infile, (short*)&bh.planes);
pm_read_little_short(infile, (short*)&bh.bitsperpixel);
pm_read_little_long(infile, (long*)&bh.compression);
pm_read_little_long(infile, (long*)&bh.cmpsize);
pm_read_little_long(infile, (long*)&bh.xscale);
pm_read_little_long(infile, (long*)&bh.yscale);
pm_read_little_long(infile, (long*)&bh.colors);
pm_read_little_long(infile, (long*)&bh.impcolors);

/* If compressed, bail */
if ( bh.compression )
{
fprintf(stderr, "%s is compressed.\n", infilename);
return image_ptr;
}

/* set out header information */
header -> width = bh.cols;
header -> length = bh.rows;
header -> spp = (bh.bitsperpixel == 24) ? 3 : 1;
header -> rowbytes = header -> width * header -> spp;
header -> colorspace = (bh.bitsperpixel == 24) ? RGB_24_BIT : INDEX_8_BIT;

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.

View Follow-Ups    Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.