// BMP. h
# Pragma pack (1)
Typedef struct tagbitmapfileheader
{
Unsigned char bftype [2];
Int bfsize;
Unsigned short bfreserved1;
Unsigned short bfreserved2;
Int bfoffbits;
} Bitmapfileheader;
Typedef struct tagbitmapinfoheader
{
Int bisize;
Int biwidth;
Int biheight;
Unsigned short biplanes;
Unsigned short bibitcount;
Int bicompression;
Int bisizeimage;
Int bixpelspermeter;
Int biypelspermeter;
Int biclrused;
Int biclrimportant;
} Bitmapinfoheader;
Typedef struct tagrgbquad
{
Unsigned char rgbblue;
Unsigned char rgbgreen;
Unsigned char rgbred;
Unsigned char rgbreserved;
} Rgbquad;
// Global variable definition
Extern unsigned char * pbmpbuf; // pointer for reading image data
Extern int BMP width; // Image Width
Extern int bmpheight; // Image Height
Extern rgbquad * pcolortable; // color table pointer
Extern unsigned short bibitcount; // image type, number of digits per pixel
Extern int linebyte; // number of bytes per line
// Read. c
# Include "stdio. H"
# Include "BMP. H"
Bool BMP read (char * BMP name)
{
File * fp = fopen (BMP name, "rb"); // open the BMP name file in binary read mode.
File * Ft = fopen ("BMP .txt", "WB ");
If (FP = 0) return 0; // defines null as 0 in stdio. h.
Else // open successfully
{
// Read the file header and determine the file type
Bitmapfileheader fhead;
Fread (& fhead, sizeof (bitmapfileheader), 1, FP );
If (fhead. bftype [0] = 'B' & fhead. bftype [1] = 'M') // The file is in BMP format.
{
Bitmapinfoheader head; // defines the bitmap information header structure variable,
Fread (& head, sizeof (bitmapinfoheader), 1, FP); // read the information header into the memory and store it in the head. sizeof (bitmapinfoheader = 40,
// Obtain the width, height, and the number of digits occupied by each pixel. You can determine the type, size, and actual number of colors used by the bitmap.
BMP width = head. biwidth;
Bmpheight = head. biheight;
Bibitcount = head. bibitcount;
// Calculate the number of bytes in each row of pixels (must be a multiple of 4)
Linebyte = (BMP width * bibitcount)/8 + 3)/4*4;
// Apply the color table memory for different types of images and read the color table
Switch (bibitcount)
{
Case 1: pcolortable = new rgbquad [2]; fread (pcolortable, sizeof (rgbquad), 2, FP); break;
Case 4: pcolortable = new rgbquad [16]; fread (pcolortable, sizeof (rgbquad), 16, FP); break;
Case 8: pcolortable = new rgbquad [256]; fread (pcolortable, sizeof (rgbquad), 256, FP); break;
Default: break;
}
// Apply for space required for bitmap data and read bitmap data into memory
Pbmpbuf = new unsigned char [linebyte * bmpheight];
Fread (pbmpbuf, 1, linebyte * bmpheight, FP );
// Output bitmap data and some important parameters
Printf ("bftype = % C, bibitcount = % d/N", fhead. bftype [0], fhead. bftype [1], head. bibitcount );
Printf ("width = % d, Height = % d, size = % d/N", head. biwidth, head. biheight, fhead. bfsize );
Printf ("linebyte * bmpheight = % d/N", linebyte * bmpheight );
// Outputs bitmap data. Only image data with small content can be output.
If (fhead. bfsize <5000000)
{
Printf ("imagedate:/N ");
Int I, J;
Unsigned char * P;
P = pbmpbuf;
For (j = 0; j <bmpheight; j ++)
{
For (I = 0; I <linebyte; I ++)
{
Printf ("% d", * P); P ++;
If (* P <15 ){
Fprintf (FT, "0 x 0000000% x/R/N", * P );
} Else {
Fprintf (FT, "0 x 000000% x/R/N", * P );
}
P ++;
}
Printf ("/N ");
}
}
// Close the file
Fclose (FP );
Fclose (FT );
Return 1;
}
Else return 0;
}
}
// Write. c
# Include "stdio. H"
# Include "BMP. H"
Bool BMP write (char * BMP name, unsigned char * imgbuf, int width, int height, int bibitcount, rgbquad * pcolortable)
{
// If the bitmap data pointer is 0, return
If (! Imgbuf) return 0;
Else
{
// Open the file in binary mode
File * fp = fopen (BMP name, "WB ");
If (FP = 0) return 0;
Else
{
Int linebyte = (width * bibitcount/8 + 3)/4*4;
Int colortablesize = 0;
Switch (bibitcount)
{
Case 1: colortablesize = 8; break;
Case 4: colortablesize = 64; break;
Case 8: colortablesize = 1024; break;
Default: break;
}
// Apply for a file header structure variable and fill in the file header information
Bitmapfileheader filehead;
Filehead. bftype [0] = 'B ';
Filehead. bftype [1] = 'M ';
Filehead. bfsize = 54 + colortablesize + linebyte * height; // filehead. bfsize = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + colortablesize + linebyte * height;
Filehead. bfreserved1 = 0;
Filehead. bfreserved2 = 0;
Filehead. bfoffbits = 54 + colortablesize;
// Write the file header
Fwrite (& filehead, sizeof (bitmapfileheader), 1, FP );
// Apply for bitmap information header Structure Variables and fill in 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 bitmap information header into memory
Fwrite (& head, sizeof (bitmapinfoheader), 1, FP );
// Write the color table
Switch (bibitcount)
{
Case 1: fwrite (pcolortable, sizeof (rgbquad), 2, FP); break;
Case 4: fwrite (pcolortable, sizeof (rgbquad), 16, FP); break;
Case 8: fwrite (pcolortable, sizeof (rgbquad), 256, FP); break;
Default: break;
}
// Write bitmap data into the file
Fwrite (imgbuf, height * linebyte, 1, FP );
// Close the file
Fclose (FP );
Return 1;
}
}
}
// Main. c
# Include "stdio. H"
# Include "BMP. H"
Unsigned char * pbmpbuf; // pointer for reading image data
Int BMP width; // The image width.
Int bmpheight; // Image Height
Rgbquad * pcolortable; // color table pointer
Unsigned short bibitcount; // image type, number of digits per pixel
Int linebyte; // number of bytes per line
Void main ()
{
// Declare the call Function
Bool BMP read (char * BMP name );
Bool BMP write (char * BMP name, unsigned char * imgbuf, int width, int height, int bibitcount, rgbquad * pcolortable );
// Read the specified BMP file path
Char readpath [] = "red.bmp ";
Char writepath [] = "bwcpy.txt ";
Int A = BMP read (readpath );
If (! A) printf ("cann' t read the file! ");
Else
{
// Save image data to disk
Int B = BMP write (writepath, pbmpbuf, BMP width, bmpheight, bibitcount, pcolortable );
If (! B) printf ("cann' t write to the file! ");
// Clear the buffer. pbmpbuf and pcolortable are global variables. Space requested during File Read
Delete [] pbmpbuf;
If (bibitcount! = 24) Delete [] pcolortable;
}
}