Friday, July 31, 2009

How can I read a .bmp file in my C pragramme?

I want a method to read a bitmap(.bmp) file in my c source code.


besides the method I will be very greatfull and consider the best answer of those who will provide me the gud source code by e-mail plz help me.

How can I read a .bmp file in my C pragramme?
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(%26amp;bh.magic1, sizeof(unsigned char), 1, infile);


fread(%26amp;bh.magic2, sizeof(unsigned char), 1, infile);





/* test to see if this is really a BMP file */


if (bh.magic1 != 'B' %26amp;%26amp; bh.magic2 != 'M')


return image_ptr;





/* read header information */


pm_read_little_long(infile, (long*)%26amp;bh.filesize);


pm_read_little_short(infile, (short*)%26amp;bh.res1);


pm_read_little_short(infile, (short*)%26amp;bh.res2);


pm_read_little_long(infile, (long*)%26amp;bh.pixeloffset);


pm_read_little_long(infile, (long*)%26amp;bh.bmisize);


pm_read_little_long(infile, (long*)%26amp;bh.cols);


pm_read_little_long(infile, (long*)%26amp;bh.rows);


pm_read_little_short(infile, (short*)%26amp;bh.planes);


pm_read_little_short(infile, (short*)%26amp;bh.bitsperpixel);


pm_read_little_long(infile, (long*)%26amp;bh.compression);


pm_read_little_long(infile, (long*)%26amp;bh.cmpsize);


pm_read_little_long(infile, (long*)%26amp;bh.xscale);


pm_read_little_long(infile, (long*)%26amp;bh.yscale);


pm_read_little_long(infile, (long*)%26amp;bh.colors);


pm_read_little_long(infile, (long*)%26amp;bh.impcolors);





/* If compressed, bail */


if ( bh.compression )


{


fprintf(stderr, "%s is compressed.\n", infilename);


return image_ptr;


}





/* set out header information */


header -%26gt; width = bh.cols;


header -%26gt; length = bh.rows;


header -%26gt; spp = (bh.bitsperpixel == 24) ? 3 : 1;


header -%26gt; rowbytes = header -%26gt; width * header -%26gt; spp;


header -%26gt; colorspace = (bh.bitsperpixel == 24) ? RGB_24_BIT : INDEX_8_BIT;





if ( header -%26gt; 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 -%26gt; rowbytes * header -%26gt; length );





/* only read 24 bit images for now */


if ( header -%26gt; colorspace == RGB_24_BIT )


{


/* loop through the image data */


for ( row = 0; row %26lt; header -%26gt; length; row++ )


{


/* backup one row */


writep -= header -%26gt; rowbytes;





for ( col = 0; col %26lt; header -%26gt; 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 -%26gt; rowbytes;


}


}


else


fprintf(stderr, "%s is an indexed image.\n", infilename);





/* close file */


fclose(infile);





return image_ptr;


}
Reply:You can read a BMP using pure C code, but it's not easy... I found here an example written entirely in C which can read a 8-bit BMP file..


There is another way though... The C language recognizes assembler instructions. Each file has a header which gives all the info required to make the OS "understand" it. This part isn't located exactly at the begining at the file.. It depends!


Of course knowing these values and where to find them it would be easy just to configure them! You'll need assembler language for this.


I found the BMP header file at the second link (not much info though...).


Also, a way to work with it (and an example of code that understands 256-bits BMP and not only) is at the last link.. Although it's in pascla the code it's adaptable very easy to C


Hope this helps!
Reply:http://www.google.co.uk/search?q=bitmap+...


Searching the net is so easy it hurts!





Rawlyn.





p.s. It's spelt "program" in computer terminology. A programme is something you buy at the theater.


No comments:

Post a Comment