Bool savebmp (char * BMP name, unsigned char * imgbuf, int width, int height, int bibitcount, rgbquad * pcolortable) {// If the bitmap data pointer is 0, no data is transmitted, the function returns if (! Imgbuf) return 0; // color table size, in bytes. The gray image color table // is 1024 bytes. The color image color table size is 0 int colortablesize = 0; if (bibitcount = 1) colortablesize = 8; // int linebyte = (width * bibitcount/8 + 3)/4*4; // open the file * fp = fopen (BMP name, "WB") in binary mode; If (FP = 0) return 0; // apply for bitmap file header Structure Variables, fill in the file header information bitmapfileheader filehead; filehead. bftype = 0x4d42; // BMP Type // bfsize is the sum of the four components of the image file filehead. bfsize = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + colortablesize + linebyte * height; filehead. bfreserved1 = 0; filehead. bfreserved2 = 0; // bfoffbits is the sum of the space required for the first three parts of the image file filehead. bfoffbits = 54 + colortablesize; // write the file header into the file fwrite (& filehead, sizeof (bitmapfileheader), 1, FP); // apply for bitmap information header Structure Variables, fill in the Information header information bitmapinfoheader head; head. bibitcount = bibitcount; head. biclrimportant = 0; head. biclrused = 0; head. bicompression = 0; head. biheight = height; head. biplanes = 1; head. bisize = 40; head. bisizeimage = linebyte * height; head. biwidth = width; head. bixpelspermeter = 0; head. biypelspermeter = 0; // write the bitmap information header into the memory fwrite (& head, sizeof (bitmapinfoheader), 1, FP); // If the grayscale image has a color table, write File if (bibitcount = 1) fwrite (pcolortable, sizeof (rgbquad), 2, FP); // write bitmap data into file fwrite (imgbuf, height * linebyte, 1, FP); // close the file fclose (FP); return 1 ;}