BMP picture is a bitmap (bitmap), generally uncompressed, to read BMP file As long as you know its file structure can be, the specific format can be Baidu or Google, do not say, a few important points in the code there are comments.
/** C language reads bitmap information **/#include <stdio.h> #include <malloc.h> #define BM 19778//Bitmap logo #define PATH "d:\\test.
BMP "//Open File path//judgment is bitmap, in 0-1-byte int isbitmap (file *fp) {unsigned short S;
Fread (&S,1,2,FP);
if (S==BM) return 1;
else return 0;
///Get the width of the picture, in 18-21-byte long getwidth (FILE *fp) {long;
Fseek (Fp,18,seek_set);
Fread (&WIDTH,1,4,FP);
return width;
///Get the height of the picture, in 22-25-byte long getheight (FILE *fp) {long;
Fseek (Fp,22,seek_set);
Fread (&HEIGHT,1,4,FP);
return height;
//Get the number of bits per pixel in the 28-29-byte unsigned short getbit (FILE *fp) {unsigned short bit;
Fseek (Fp,28,seek_set);
Fread (&BIT,1,2,FP);
return bit;
///Get the starting position of the data unsigned int getoffset (FILE *fp) {unsigned int OffSet;
Fseek (Fp,10l,seek_set);
Fread (&OFFSET,1,4,FP);
return OffSet;
}//Obtain the image data void GetData (file* fp,unsigned char *r,unsigned char *g,unsigned char *b) {file* FPR;
File* FPG;
File* FPB;
int I, j=0;
int stride;
unsigned char* pix=null; LOng Height,width;
Height=getheight (FP);
Width=getwidth (FP); Fseek (FP, GetOffset (FP), seek_set); Locate the data area of the bitmap stride= (24*width+31)/8; Align to calculate how many 8-bit stride=stride/4*4 a width is;
Take a multiple of four R,g,b,alph//write array pix= (unsigned char *) malloc (stride);
for (j=0;j