C++/read BMP

Advertisement


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 (line 8) contains a "declaration 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. Maybe I'm missing some #include statements? 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
You need to include the declaration of ImageHeader. This should be done by including some missing header file. I think you should spend some time on learning introductory C++ before you have another question. One good resource is Bruce Eckel's online and print versions of Thinking in C++. All the best!

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Amit Kumar

Expertise

I can answer Cplusplus language and library questions, including STL, ACE, Boost. I have a good background in Mathematics.

Experience

Programming in CPP for about 8 years. Industry experience of 4 years.

Education/Credentials
Bachelors and Masters in Computer Science from Indian Institute of Technology Delhi.

©2016 About.com. All rights reserved.