Mail Archives: djgpp/2001/10/09/09:50:04
Check www.wotsit.org for more info. Below is a simple BMP reader. It reads
256-color images with screen resolutions of 320x200.
typedef struct STRUCT_BMP{
UINT8BIT TYPE[2]; //Should be 'BM'
UINT8BIT PALLETTE_LOCATION,//Location of pallette in file
PALLETTE_SIZE; //Pallette size
UINT16BIT RESERVED[3]; //Reserved
INT32BIT BITMAP_LOCATION, //Offset to start of bitmap data
STRUCT_SIZE, //Size of this structure
WIDTH, //Width of image
HEIGHT; //Height of image
UINT16BIT NUM_OF_PLANES, //Number of planes
BITS_PER_IMAGE; //Number of BPI
UINT32BIT COMPRESSION, //Type of compression
IMAGE_SIZE, //Size of image in bytes
X_PIXELS, //Number of horizontal pixels per meter
Y_PIXELS, //Number of vertical pixels per meter
COLORS, //Number of colors
IMPORTANT_COLORS;//Number of important colors
} STRUCT_BMP;
/*_____________________________________________________________________
¦ ¦
¦ Summary: Load a BMP file into '_bmp_destination' ¦
¦ ¦
¦ Algorithm Notes: ¦
¦ ¦
¦ +A BMP file consists of a 54 bytes header,the pallette data,and ¦
¦ image data. ¦
¦ +---------------+ ¦
¦ ¦ HEADER ¦ ¦
¦ ¦ 54 bytes ¦ ¦
¦ +---------------¦ ¦
¦ ¦ PALLETTE ¦ ¦
¦ ¦ 768 bytes ¦ ¦
¦ +---------------¦ ¦
¦ ¦ IMAGE DATA ¦ ¦
¦ ¦ 64000 bytes ¦ ¦
¦ +---------------+ ¦
¦ ¦
¦ +The image data is encoded upside down and needs to be read from ¦
¦ the bottom up. ¦
¦ ¦
¦ +The pallette is also upside down in the file in BGRX format. ¦
¦ Care should be taken to skip the 'X' byte in the above pallette ¦
¦ arrangement. ¦
¦ ¦
¦ +Also, the pallette byte needs shifting right to get 8 to 6 bits. ¦
¦ 10101000--shifting right by 2 gives--00101010 for a maximum of ¦
¦ 64 different shades for given byte color. ¦
¦ ¦
¦ ¦
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯*/
INT8BIT bmp_load(UINT8BIT *_bmp_filename,
UINT8BIT *_bmp_destination,
STRUCT_BMP *_bmp_head,
STRUCT_PALLETTE *_bmp_pallette,
UINT8BIT _bmp_enable_pallette){
FILE *pointer_file; //--file pointer
STRUCT_PIXEL *struct_pixel;
UINT8BIT bmp_pixel_color,
key;
UINT32BIT bmp_width,
bmp_height,
temporary;
INT32BIT counter1,
counter2,
counter3;
pointer_file=fopen(_bmp_filename,"rb");
if(!pointer_file)
return -1;
fread(_bmp_head,sizeof(STRUCT_BMP),1,pointer_file);
if(_bmp_head->TYPE[0]!='B'
||_bmp_head->TYPE[1]!='M'
||_bmp_head->BITS_PER_IMAGE!=8)
{
fclose(pointer_file);
return -1;
}
fseek(pointer_file,_bmp_head->PALLETTE_LOCATION,SEEK_SET);
for(counter1=0;counter1<256;counter1++)//colors are BGR, not RGB
{
key=fgetc(pointer_file);
_bmp_pallette[counter1].BLUE=key>>2;
key=fgetc(pointer_file);
_bmp_pallette[counter1].GREEN=key>>2;
key=fgetc(pointer_file);
_bmp_pallette[counter1].RED=key>>2;
key=fgetc(pointer_file);
}
if(_bmp_enable_pallette)
setallpallette(_bmp_pallette);
fseek(pointer_file,_bmp_head->BITMAP_LOCATION,SEEK_SET);
bmp_height=_bmp_head->HEIGHT;
bmp_width=_bmp_head->WIDTH;
for (struct_pixel->Y_POSITION=bmp_height;
struct_pixel->Y_POSITION>=1;
struct_pixel->Y_POSITION--)//image is flipped,unflip
{
for (struct_pixel->X_POSITION=1;
struct_pixel->X_POSITION<=bmp_width;
struct_pixel->X_POSITION++)
{
struct_pixel->PIXEL_COLOR=fgetc(pointer_file);
dblbfr_put_pixel(struct_pixel,_bmp_destination);
}
}
fclose(pointer_file);
return 0;
}
-----Original Message-----
From: pouzzler AT aol DOT com [mailto:pouzzler AT aol DOT com]
Sent: Friday, October 05, 2001 3:13 PM
To: djgpp AT delorie DOT com
Subject: Pointer/array understanding?
I'm trying to load/draw a bitmap from a file (PLEASE don't answer
"Allegro")
I searched for info, and the file is as follow:
BITMAPFILEHEADER //struct heading the file
BITMAPINFOHEADER //struct with info on the bitmap
RGBQUAD[] //array of RGB triplets
bitmapbits[] //the actual bitmap
I tried reading a bitmapbit and drawing it, but it was awfully slow.
How can I read ALL the bits, then draw them. It seems I must have a
BITMAP struct looking like
typedef struct tagBITMAP {
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
RGBQUAD //and it's here I don't know how
bitmapbits // same
} BITMAP;
How can I make those arrays, not knowing their size in the first
place?
Is RGBQUAD *rgb; correct?
- Raw text -