You are here:

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 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
Hi Eric

What compiler are you using to test your code ... If you are using Turbo C/C++ Compiler then main is a mandatory function which is missing if you can tell me from where you have found this code the name of the site then I might help you to explain the logic fully

regards
Joydeep

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joydeep Bhattacharya

Expertise

Ability to solve C and Data Structure problems and puzzles with simple and easy to understand logic.

Experience

C, C++, Java, Data Structure, PHP, Web Designing

Organizations
http://www.scodz.com Designation: webmaster

Publications
http://www.scodz.com

Education/Credentials
Master of Computer Applications

Past/Present Clients
http://analysingc.50webs.com http://www.funforu.com

©2012 About.com, a part of The New York Times Company. All rights reserved.