#ifndef _BITMAP_H_
#define _BITMAP_H_
#include <stdio.h>
#define GET_B(bmp,i,j) ( *( bmp->ptr + bmp->line_width * i + j ) )
#define GET_G(bmp,i,j) ( *( bmp->ptr + bmp->line_width * i + j + 1 ) )
#define GET_R(bmp,i,j) ( *( bmp->ptr + bmp->line_width * i + j + 2 ) )
#define SET_B(bmp,i,j,v) ( *( bmp->ptr + bmp->line_width * i + j ) = v )
#define SET_G(bmp,i,j,v) ( *( bmp->ptr + bmp->line_width * i + j + 1 ) = v )
#define SET_R(bmp,i,j,v) ( *( bmp->ptr + bmp->line_width * i + j + 2 ) = v )
#define IS8BITS(bmp) ( ( bmp->ptr != NULL && bmp->bit_count == 8 )? 1 : 0 )
#define IS24BITS(bmp) ( ( bmp->ptr != NULL && bmp->bit_count == 24 ) ? 1 : 0 )
#define ISEMPTY(bmp) ( ( bmp->ptr == NULL ) ? 1 : 0 )
#define LIM255(v) ( ( v > 255 ) ? 255 : v )
#define LIMTO0(v) ( ( v < 0 ) ? 0 : v )
typedef unsigned char
BYTE;
typedef unsigned short WORD;
typedef unsigned long
DWORD;
typedef struct tagBitmap
{
BYTE* ptr;
BYTE* palette;
DWORD width;
DWORD height;
WORD
bit_count;
DWORD line_width;
WORD pal_length;
}Bitmap;
/*下面的就是调色板的每一项,包括红、绿、蓝的值,和一个保留位。为了保险起见,你可以建立一个256个颜色的调色板,每个调色板的颜色的r、g、b都是一致的,比如:
for(i=0;i<256;i++)
{
pal[i].rgbBlue = (BYTE)i;
pal[i].rgbGreen = (BYTE)i;
pal[i].rgbRed = (BYTE)i;
pal[i].rgbReserved = (BYTE)0;
}
*/
/*
typedef struct tagRGBQUAD
{
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
}RGBQUAD;
*/
//load a bitmap to the structure Bitmap
void load_bitmap( const char* filename, Bitmap *bmp );
//save the structure Bitmap to a file
void save_bitmap( const char* filename, Bitmap *bmp );
//release the memory which is taked by the structure Bitmap
void free_bitmap( Bitmap *bmp );
#endif
--------------------------------------------------------------------------------------------
#include "Bitmap.h"
#include <stdio.h>
#include <stdlib.h>
void load_bitmap( const char *filename, Bitmap *bmp )
{
FILE *fp = 0;
DWORD off_bits = 0, width = 0, height = 0, line_width = 0;
WORD bit_count = 0, pal_length = 0;
//open the file to read
if( ( fp = fopen( filename, "rb" ) ) == NULL )
{
printf("can not open the bitmap : %s\n", filename );
return;
}
//read the bfOffBits in the file header of the bitmap
fseek( fp, 10, 0 );
fread( &off_bits, 4, 1, fp );
//read the biWidth and biHeight in the info header of the bitmap
fseek( fp, 18, 0 );
fread( &width, 4, 1, fp );
fread( &height, 4, 1, fp );
//read the biBitCount in the info header of the bitmap
fseek( fp, 28, 0 );
fread( &bit_count, 2, 1, fp );
//read the color palette if the bitmap have a color palette
bmp->pal_length = (unsigned short)(off_bits - 54);
if( pal_length == 0 )
{
bmp->palette = 0;
}
else
{
bmp->palette = (BYTE*)malloc( pal_length * sizeof(BYTE) );
if( !bmp->palette ) return;
fseek( fp, 54, 0 );
fread( bmp->palette, pal_length * sizeof(BYTE), 1, fp );
}
//set the memeber of the structure Bitmap
line_width = ( width * bit_count + 31 ) / 32 * 4;
bmp->line_width = line_width;
bmp->width = width;
bmp->height = height;
bmp->bit_count = bit_count;
//allocate memory for the pixel of the bitmap
bmp->ptr = (BYTE*)malloc( line_width * height * sizeof(BYTE) );
if( !bmp->ptr )
{
printf("can not allocate memory for the bitmap.\n");
bmp->width = 0;
bmp->height = 0;
bmp->bit_count = 0;
return;
}
//read the pixel matrix from the bitmap
fseek( fp, off_bits, 0 );
fread( bmp->ptr, line_width * height, 1, fp );
//close the file
fclose( fp );
}
void save_bitmap( const char *filename, Bitmap *bmp )
{
FILE *fp = 0;
DWORD dw = 0;
WORD
w = 0;
//open the file for writing
if( ( fp = fopen( filename, "wb" ) ) == NULL )
{
printf("can not create the bitmap : %s\n", filename );
return;
}
//write the bfType
w = 19778;
fwrite( &w, 2, 1, fp );
//write the bfSize
dw = 14;
fwrite( &dw, 4, 1, fp );
//write the bfReserved1 and bfReserved2
w = 0;
fwrite( &w, 2, 1, fp );
fwrite( &w, 2, 1, fp );
//write the bfOffBits
if( bmp->bit_count == 8 )
dw = 1078;
else
dw = 54;
fwrite( &dw, 4, 1, fp );
//write the biSize
dw = 40;
fwrite( &dw, 4, 1, fp );
//write the biWidth
dw = bmp->width;
fwrite( &dw, 4, 1, fp );
//write the biHeight
dw = bmp->height;
fwrite( &dw, 4, 1, fp );
//write the biPlanes
w = 0;
fwrite( &w, 2, 1, fp );
//write the biBitCount
w = bmp->bit_count;
fwrite( &w, 2, 1, fp );
//write the biCompression
dw = 0;
fwrite( &dw, 4, 1, fp );
//write the biSizeImage
dw = bmp->height * bmp->line_width;
fwrite( &dw, 4, 1, fp );
//write the biXPelsPerMeter and the biYPelsPerMeter
dw = 0;
fwrite( &dw, 4, 1, fp );
fwrite( &dw, 4, 1, fp );
//write the biClrUsed
fwrite( &dw, 4, 1, fp );
//write the biClrImportant
fwrite( &dw, 4, 1, fp );
//write the color palette if the bitmap have
if( bmp->bit_count == 8 )
{
fwrite( bmp->palette, bmp->pal_length, 1, fp );
}
//write the pixel of the bitmap
dw = bmp->height * bmp->line_width;
fwrite( bmp->ptr, dw, 1, fp );
//close the file
fclose(fp);
}
void free_bitmap( Bitmap *bmp )
{
free( bmp->ptr );
bmp->ptr = 0;
free( bmp->palette );
bmp->palette = 0;
}