// Printbmp pixel. C // the C language reads the source code of the BMP Format File // It can simultaneously process binary images, index images, and real-color images without compressing the BMP format. # Include <stdio. h> # include <windows. h> // bitmap file header typedef struct tagbmp fileheader {unsigned short bftype; // 2 bytesunsigned long bfsize; // 4 bytesword bytes; // 2 bytesword bfreserved2; // 2 bytesunsigned long enough; // 4 bytes} bmpheader; // bitmap information header typedef struct tagbmp infoheader {DWORD bisize; long biwidth; long biheight; Word biplanes; Word bibitcount; DWORD bicompression; DWORD B Isizeimage; long bixpelspermeter; long biypelspermeter; DWORD biclrused; DWORD biclrimportant;} BMP infoheader; // color palette (optional) typedef struct detail {byte rgbblue; // typedef unsigned char byte rgbgreen; byte rgbred; byte rgbreserved;} bytes; BMP rgbquad * BMP rgbquad; // bitmap data typedef struct tagimagedata {byte red; byte green; byblue ;} imagedata; imagedata * imagedata; // obtain the current BM The size of the P file int getbmp size (File * FP) {long curpos; // attention! This is a long type.int Len; curpos = ftell (FP); fseek (FP, 0, seek_end); Len = ftell (FP); fseek (FP, curpos, seek_set ); return Len;} int main (INT argc, char ** argv) {file * FP; unsigned char * CP1, * CP2, * sp; int btrueclr = 1; int I, j; int width; unsigned char * paletteindex; // scanf ("% s", BMP file); If (argc <= 1) {printf ("Please input the filename: \ n "); System (" pause "); exit (0);} If (FP = fopen (argv [1]," rb ") = Null) // must be "rb" to indicate the position of the file while the pointer is moving! {Printf ("can not open file \ n"); System ("pause"); exit (0);} fread (& bmpheader, sizeof (unsigned short), 1, FP); SP = (unsigned char *) & bmpheader; SP = SP + 4; fread (SP, sizeof (struct tagbmp fileheader)-4, 1, FP ); fread (& BMP infoheader, sizeof (struct tagbmp infoheader), 1, FP); If (BMP infoheader. bibitcount = 1 | BMP infoheader. bibitcount = 4 | BMP infoheader. bibitcount = 8) {BMP rgbquad = (BMP rgbquad *) malloc (Sizeof (struct tagbmp rgbquad) * (1 <BMP infoheader. bibitcount); fread (BMP rgbquad, sizeof (BMP rgbquad) * (1 <BMP infoheader. bibitcount), 1, FP); // The power of 2. Use the right shift to realize btrueclr = 0;} If (bmpheader. bftype! = 0x0000d) {printf ("the type of the image is not supported \ n");} If (BMP infoheader. bicompression! = 0) {printf ("the compression type are not supported. \ n");} If (bmpheader. bfsize! = Getbmp size (FP) {printf ("the size of the file is not matched \ n");} // print the BMP fileheader value. CP1 = (unsigned char *) & (bmpheader. bftype); CP2 = CP1 + 1; printf ("% C", * CP1); printf ("% C \ n", * CP2); printf ("file size: % lu \ n ", bmpheader. bfsize); printf ("offset from the BOF to the virtual image data: % lu \ n", bmpheader. bfoffbits); // print the BMP infoheader value. printf ("the size of the infoheader is: % lu \ N ", BMP infoheader. bisize); printf ("width: % LD \ n", BMP infoheader. biwidth); printf ("height: % LD \ n", BMP infoheader. biheight); printf ("Planes: % d \ n", BMP infoheader. biplanes); printf ("bit depth: % d \ n", BMP infoheader. bibitcount); printf ("compression type: % lu \ n", BMP infoheader. bicompression); printf ("image data size: % lu \ n", BMP infoheader. bisizeimage); printf ("xpermeter: % LD \ n", BMP infoheader. bixpelspermeter); Printf ("ypermeter: % LD \ n", BMP infoheader. biypelspermeter); printf ("color used: % lu \ n", BMP infoheader. biclrused); printf ("important color: % lu \ n", BMP infoheader. biclrimportant); // The number of bytes in each row must be four integer times width = (BMP infoheader. biwidth * BMP infoheader. bibitcount + 31)/8/4*4; // print the pixel of the imagedataimagedata = (imagedata *) malloc (sizeof (struct tagimagedata) * width * BMP infoheader. biheight); paletteindex = (Unsigned char *) malloc (sizeof (unsigned char) * width * BMP infoheader. biheight); If (! Btrueclr) {fread (paletteindex, sizeof (unsigned char) * width * BMP infoheader. biheight, 1, FP); for (I = 0; I <BMP infoheader. biheight; I ++) {for (j = 0; j <width; j ++) {(* (imagedata + I * BMP infoheader. biheight + j )). blue = BMP rgbquad [(byte) (* (paletteindex + I * BMP infoheader. biheight + J)]. rgbblue; (* (imagedata + I * BMP infoheader. biheight + j )). green = BMP rgbquad [(byte) (* (paletteindex + I * BMP infoheader. biheight + J)]. rgbgreen; (* (imagedata + I * BMP infoheader. biheight + j )). red = BMP rgbquad [(byte) (* (paletteindex + I * BMP infoheader. biheight + J)]. rgbred ;}} else {fread (imagedata, sizeof (struct tagimagedata) * width * BMP infoheader. biheight, 1, FP) ;}for (I = 0; I <1; I ++) // print the first line pixel. {for (j = 0; j <BMP infoheader. biwidth; j ++) {If (* (imagedata + I * BMP infoheader. biheight + j )). blue = (* (imagedata + I * BMP infoheader. biheight + j )). green & (* (imagedata + I * BMP infoheader. biheight + j )). blue = (* (imagedata + I * BMP infoheader. biheight + j )). red) {printf ("% d", (* (imagedata + I * BMP infoheader. biheight + j )). red);} else {printf ("% d", (* (imagedata + I * BMP infoheader. biheight + j )). blue); printf ("% d", (* (imagedata + I * BMP infoheader. biheight + j )). green); printf ("% d", (* (imagedata + I * BMP infoheader. biheight + j )). red);} If (0 = J % 3) {printf ("\ n") ;}} printf ("\ n") ;}fclose (FP ); system ("pause"); free (imagedata); free (paletteindex); Return 0 ;}