A BMP file read and written program

xiaoxiao2021-03-06  57

BMPTest.h: Introducing the format and structure of the BMP file definition BMPTEST.CP: 24bitBMP color data to 256 color bitmap color data conversion function implementation, specific algorithm can refer to the previous post BMPTransfer.cpp: read a 24bitbmp file, conversion The program obtained after a 256-color BMP file is completed, such as BMPTransfer.exe executing BMPTRANSFER FILE1 file2file1 is a 24bit's BMP bitmill source file name, file2 is a newly generated 256 color bitmap file name to view with Windows As a result, it seems to be better than the conversion effect of 24bitBMP to 256 color BMP files than the direct paper program program. / ************* BMPTEST.H ************************* / # ifndef __bmptest_h_ # deflude typedef unsigned char Byte; Typedef unsigned short word; // BMP image Note According to the following / *********** The first partial graph file header This structure is fixed, 14 bytes, the sequence of each domain is as follows : 2Byte: File type, must be 0x4d42, ie string "BM". 4Byte: The whole file size 4Byte: Reserved word, 0 4Byte: From the file head to the actual number of bitmap image data. ************* / TYPEDEF STRUCT {Long ImageSize; long blank; long startposition; void show (void) {Printf ("bmp head: / n"); printf ("Image size:% D / N ", ImageSize; Printf (" Image Data Start Position:% D / N ", StartPosition);}} bmphead; / ********************* * The length of the second partial graph information is also fixed, 40 bytes, the sequence of each domain, as follows: 4Byte: The length of the structure is 40 4byte: The width of the image is how much pixels. 4BYTE: How much pixels are heavily imaged. 2Byte: must be 1. 2Byte: Represents the number of bits used in color, common value is 1 (black and white two color map), 4 (16 color map), 8 (256 color map), 24 (true color map). 4Byte: Individualization map is compressed, valid value is Bi_RGB, Bi_RLE8, BI_RLE4, BI_BITFIELDS. The Windows bitmap can use the compressed format of RLE4 and RLE8, and Bi_RGB represents no compression. 4BYTE: Specifies the number of bytes occupied by the actual bitmap image data, and the following formula is calculated: image data = width '* height * indicates the number of BYTEs occupied by each pixel color (ie color bits / 8,24bit) It should be noted that the biwidth 'in the above formula must be 4 integral times (not BiWidth, but is greater than or equal to the minimum of 4 of BiWidth).

If bicompression is bi_rgb, the item may be 0. 4BYTE: The horizontal resolution of the target device. 4Byte: Vertical resolution of target equipment. 4BYTE: The number of colors actually used in this image, if the value is 0, the number of colors used is 2 (number of color bits), such as 8, 2 ^ 8 = 256, ie 256 colors Bitchart 4Byte: Specifies the number of important colors in this image, if the value is 0, it is considered to be important. *********************************** / TYPEDEF STRUCT {Long Length; Long Width; Long Height; Word ColorPlane ; WORD bitColor; long zipFormat; long realSize; long xPels; long yPels; long colorUse; long colorImportant; void show (void) {printf ( "infoHead Length:% d / n", Length); printf ( "width & height:% d *% d / n ", width, height); Printf (" ColorPlane:% D / N ", ColorPlane); Printf (" BitColor:% D / N ", BitColor; Printf (" Compression Format:% D / N ", zipformat); Printf (" Image Real size:% D / N ", RealSize; Printf (" PELS (X, Y): (% D,% D) / N ", Xpels, Ypels); Printf (" ColorUse:% D / N ", ColorUse); Printf (" Important Color:% D / N ", ColorImportant);}}} infohead; / **************************** ******* The third part of the palette structure for 256 color BMP bitmap, the number of color bits is 8, required 2 ^ 8 = 256 color panel; for the 24bitBMP bitmap, each pixel RGB value is directly Save in the image data area, does not require a palette, and there is no color palette RGBBLUE: the blue component of the color. RGBGreen: The green component of the color. RGBRED: The red component of the color. RGBRESERVED: Reserved value.

********************************* / TYPEDEF STRUCT {byte rgbblue; Byte Rgbred; Byte RgBreserved; Void Show (Void) {Printf ("MIX Plate B, G, R:% D% D% D / N ", RGBBLUE, RGBGREEN, RGBRED);}} RGBMXPLATE; / ************************ ******* Part 4 Image Data Zone For bitmaps used to palette, the image data is the index value of the pixel color in the palette; for true color maps, image data is actual R , G, b value. 2 color map, use 1 bits to represent the color of the pixel, so 1 byte can represent 8 pixels. 16 color diagram, with 4 bits can represent the color of a pixel, so 1 byte can represent 2 pixels. 256 color map, 1 byte is just 1 pixel. True color map, 3 bytes can represent 1 pixels.

******************************************************************** Int Transfer (Word * Color24bit, INT LEN, BYTE * INDEX, RGBMIXPLATE * MainColor); # Endif / *************************** ******* / # include "bmptest.h" #include #include // calculate the square difference function int PFC (int color1, int color2) {int x, y , z; x = (color1 & 0xF) - (color2 & 0xf); y = ((Color1 >> 4) & 0xF) - ((Color2 >> 4) & 0xF); z = ((Color1 >> 8) & 0xF) - ((color2 >> 8) & 0xf); return (x * x y * y z * z);}; // Direct insertion INT SORT1 (INT * SRC, INT * ATTACH, INT N ) {INT CUR, CUR1; INT I, J, K = 0; for (i = 1; i = 0; J - {IF (Cur> SRC [J]) {src [j 1] = src [j]; attach [j 1] = attach [j];} else break; SRC [J 1] = Cur; Attach [J 1] = CUR1;} Return 0;} // Quick Sort INT SORT2 (INT * SRC, INT * ATTACH, INT N) {IF (n <= 12) Return Sort1 (src, attach, n); int low = 1, high = n - 1; int Tmp; while (low <= high) {while (src [low]> = src [0]) {if ( Low> n - 1) Break;} while (src [high] high) Break; {TMP = src [low]; SRC [low] = src [high]; src [high] = TMP; TMP =

Attach [low] = attach [high]; attach [high] = TMP;}}} {TMP = SRC [low - 1]; src [low - 1] = src [0 ]; SRC [0] = TMP; TMP = attach [low - 1]; attach [low - 1] = attach [0]; attach [0] = TMP;} if (low> 1) sort2 (src, attach, Low - 1); if (Low 0) NumberOfColors ;} // Sort UsedTimes, MIN during sorting Color arrays (saved color values) also exchange Sort2 (UseTimes, MiniColor, 4096) similar to the use times // array; // /dtimes array is the frequency used in each color, from high to low alignment, obviously the NUMBEROFCOLOR The corresponding color data / / predetermined color data // is saved in the 0 // minicolor array in the palette of 256 color data to 256 color bitmap, for (i = 0; i <256; i ) {maincolor [i]. RGBBLUE = (Byte) ((Miniicolor [I] >> 8) << 4); maincolor [i] .RGBGreen = (Byte) ((MINICOLOR [I] >> 4) & 0xF) << 4); maincolor [i] .rgbred = (byte) ((Miniicolor [i] & 0xf) << 4); maincolor [i] .rgbreserved = 0;} int * colorindex = usedtimes;

// Save the index value MEMSET (INT) * 4096) with the original UseTimes array; IF (NumberOfColors <= 256) {for (i = 0; I TMP1) {TMP = Tmp1; Index = J;}} colorindex [minicolor [i]] = index;}} // record each The index value of point color data, the color data for 256 color bitmap FOR (i = 0; I int __cdecl main (int Argc, char * argv [] ) {IF (ARGC <3) {Printf ("USAGE: / N"); Printf ("% s filename1 filename2 / n", argv [0]); Printf ("FileName1: Source 24bit BMP filename Like: xxx.bmp) / N "); Printf (" FileName2: New 256 Color Bmp FileName / N "); Return -1;} Bmphead Headbmp; InfoHead Infohead; File * P; Char * FileName = Argv [1]; P = Fopen (FileName, "rb"); if (p == null) {printf ("

!!! File% s open failed./n ", filename);} Printf (" File% s open success); / **********} ********* read bmp head *** **************** / FSEEK (p, 2, seek_cur); Fread (& Headbmp, 1, 12, p); headbmp.show (); FREAD (& Infohead, 1, 40, p); infoHead.show (); if (infoHead.bitcolor! = 24) {fclose (p); Printf ("this is not a 24bit bmp file./n"); return -1;} / ** ************************************** / long ndata = infohead.realSize; byte * pcolordata = new byte [nData]; FREAD (PcolorData, 1, NDATA, P); Printf ("Last 4 Byte of Color Data:% X% X% X / N / N", / PColORDATA [NDATA-4], PCOLORDATA [NDATA-3], / PCOLORDATA [NDATA-2], PCOLORDATA [NDATA-1]); / *********** READ file over *************** / INT LEFTDATA = 0; char ch = 0; while (! FeOf (p)) {Fread (& Ch, 1, 1, P); LeftData ;} if (leftdata) Printf ("% D Bytes Not Read in File./N", LeftData); Printf ("Read File Over "); if (! fclose (p)) { Printf ("File Close./N");} // 24-bit BMP file information reads, you can view print information / ************ 24bit to 256 colors color data conversion * **************** / BYTE * INDEX = New byte [NDATA / 3]; RGBMixplate Maincolor [256]; MEMSET (Maincolor, 0, Sizeof (Maincolor); Word * Shortcolor = New Word [NDATA / 3]; INT IRED, IGREEN, IBLUE; For (INT i = 0; I > 4; Igreen = pcolordata [i * 3 1] >> 4; IBLUE = Pcolordata [i * 3 2] >> 4; ShortColor [i] = (Ired << 8) (Igreen << 4) iblue;} delete [] pcolordata;

转载请注明原文地址:https://www.9cbs.com/read-83765.html

New Post(0)