|
All bitmaps have a standard header which contains
all of the specs of the Bitmap. The size of course isn't important.
Anyways, you need to load in these file headers first using any method
necessary. I start it by making a
few variables:
BITMAPFILEHEADER bitmapfileheader;
BITMAPINFOHEADER bitmapinfoheader;
UCHAR
*buffer;
/*Or unsigned char*/
UCHAR *temp_buffer; /*Used for
storing the 24bit bitmap*/
Then the next thing to do is to load in the file
header and the info header like so:
ifstream in("bitmapfile.bmp");
in.read(bitmapfileheader,
sizeof(BITMAPFILEHEADER);
in.read(bitmapinfoheader,
sizeof(BITMAPINFOHEADER);
Then you allocate the buffers.
buffer = new UCHAR
[2*bitmapinfoheader.biWidth*bitmapinfoheader.biHeight];
temp_buffer = new
UCHAR[bitmapinfoheader.biSizeImage];
Notice above that how I allocated the space for the
final 16bit buffer. Since 16bit color is 2 bytes per pixel, and there are
width*height pixels, that means that the size of the final buffer will be
2*width*he,
ight. And the temp buffer for the 24bit image
is just based on the size of the image data in bits. Now finally we read
in the information:
in. seekp(-bitmapinfoheader.biSizeImage,
ios::bottom);
in.read(temp_buffer,
bitmapinfoheader.biSizeImage);
This reads in the image data in 24bits per
pixel. To convert this, use binary shifting for each RGB value. This
is the tricky part. Most 16bit graphics modes use 5 bits per channel (5
bits for red, 5 bits for green, 5 bits for blue). But some, especially
newer cards use 5 bits for red, 6 bits for green, and 5 bits for
blue. You'll need to find some way to do this. In Direct X it is
easy. Just use lpdds->GetPixelFormat(&ddpixelformat), and then
check ddpixelformat.IGBitMask for it's value. But here is basically how
you would convert it for a 5, 5, 5 mode:
for(i=0; i<bitmapinfoheader.biSizeImage;
i++)
{
UCHAR blue=
temp_buffer[index*3+0]>>3,
green=temp_buffer[index*3+1]>>3,
red=
temp_buffer[index*3+2]>>3;
USHORT color =
(blue+(green<<5)+(red<<10));
((USHORT)buffer)[index]=color;
}
This will load the whole bitmap into the buffer
where the final image is stored, and it will be in 16bpp. That's all I've
got. And this code was written using windows data types, so this won't
work in DOS the way I have written it. And I'm sure that I have errors
galore in there, but it's the same BASIC IDEA :) . Just follow the
idea.
----- Original Message -----
Sent: Sunday, October 08, 2000 7:29
PM
Subject: BMP/PCX file
specifications
I am trying to developing a
CRPG running under Protected mode DOS using DJGPP. I have searched the
internet for reference materials and have already written a set of
routines to handle SVGA graphics. I am now trying to get a bitmap/pcx
loader, however, I have not found much good info. Can anyone
send/refer any file specifications, tutorials, examples, etc. I
have found many bad tutorials but nothing that has answered all of my
questions. I have tried to implement 256 color PCX with limited
success. When reading the byte value and looking up the RGB triplet from
the end of the file, there was some error that I could not trace. I
would ideally like to read 24-bit BMPs, however I can always convert
files.
I am using 640x480 16-bit color, but would
appreciate any resources that deal with the subject.
Thanks,
Greg H
|