256-color non-compressed BMP file format

Source: Internet
Author: User
Tags bmp image fread

A 256-color BMP file consists of four parts: BMP file header, BMP Information header, color table, and bitmap information matrix.

BMP file header structure;

Struct bitmapfileheader _
{
Short Type; // --------- file type, which must be 'bm'
Int bfsize; // --------- file size, in bytes
Short RE1, re2; // ------ Reserved Bit
Int offbits; // -------- Bitmap matrix offset, which is the offset relative to the beginning of the file, in bytes
};

Next is the BMP Information header.

Struct bitmapinfo _
{
Long SIZE; // --------------- bitmap size, not necessarily valid.
Long width, height; // ------- bitmap width and height, pixel unit
Short planes, bitcount; // --- number of planes, which must be 1; color depth, which can be 256, 8, or 16, indicating monochrome, 16, and 16 colors respectively.
Long comp, sizeimg; // ------- compression mode. 0 indicates no compression, 1 indicates RLE compression, and 2 indicates 4-bit RLE compression for each pixel.
Long xpels, ypels; // -------- horizontal and vertical resolutions, pixel/meter Representation
Long used, important; // ----- the number of colors in the actually used color table, which is not necessarily valid. The number of important colors is not necessarily valid.
};

The structure of the color table item is
Struct color _
{
Unsigned char blue; // -------- blue brightness
Unsigned char green; // ------- green brightness
Unsigned char red; // --------- red brightness
Unsigned char re; // ---------- Reserved
}

Rle is (run length encoded travel Length Encoding) Compression

Here, only BMP files with 256 colors and no compression are processed.

The following code reads the BMP file in BCB and displays it in the canvas.

# Include <VCL. h>
# Pragma hdrstop
# Include <stdio. h>
# Include "unit1.h"
# Include "file1.h"

# Pragma pack (1)
Struct bitmapfileheader _
{
Short type;
Int bfsize;
Short RE1, re2;
Int offbits;
};

Struct bitmapinfo _
{
Long size;
Long width, height;
Short planes, bitcount;
Long comp, sizeimg;
Long xpels, ypels;
Long used, important;
};

// ------------- Correct the data in the BMP color table to the BCB tcolor data.
Void switchcolor (Long & C)
{
Long Blue = C & 0x000000ff;
Long green = C & 0x0000ff00;
Long red = C & 0x00ff0000;
C = (blue <16) | green | (Red> 16 );
}

Void XXX ()
{
File * f = fopen ("F: // fx3.bmp", "rb ");
If (F = NULL)/* determines whether the file is successfully opened */
{
Showmessage ("file open error ");
Return;
}

Fseek (F, 0, 0); // move to the beginning

// ---------- Read the BMP File Header
Bitmapfileheader _ * bmp h = new bitmapfileheader _();
If (fread (char *) bmp h, sizeof (bitmapfileheader _), 1, F) = NULL)
{
Showmessage ("file read error ");
Return;
}

// ----------- Read the BMP Header
Bitmapinfo _ * BMP I = new bitmapinfo _();
If (fread (char *) bmp I, sizeof (bitmapinfo _), 1, F) = NULL)
{
Showmessage ("File Read error2 ");
Return;
}

// -------------- Read the color table
Long * c = new long [bmp h-> offbits-sizeof (bitmapfileheader _)-sizeof (bitmapinfo _)];
Fread (char *) C, bmp h-> offbits-sizeof (bitmapfileheader _)-sizeof (bitmapinfo _), 1, F );

// ------------ Display image
Unsigned char * P = new unsigned char [4];
Int I = 0, j = 0, K = 0, WC = 0;
Tcolor * TC;
If (bmp I-> width % 4 = 0) // ----------- the BMP image is 4-byte aligned.
WC = bmp I-> width/4;
Else
WC = bmp I-> width/4 + 1;

For (I = 0; I <bmp I-> height; I ++)
{
For (j = 0; j <WC; j ++)
{
Fread (p, 4, 1, F );
For (k = 0; k <4; k ++)
{
Long x = C [p [k];
Switchcolor (x); // ---------- because the tcolor of BCB and the color table of BMP are reversed.
Form1-> canvas-> pixels [200 + J * 4 + k] [300-i] = x; // ------ 200 and 300 are located in the middle of the canvas.
}
}
}
Fclose (f );
};

BMP files are stored from bottom to bottom, left to right, and the bottom line of the first row of the Bitmap matrix.

The row pixels of BMP are 4-byte alignment. If there is an image with a width of 63 pixels in each row, the BMP File Stores 64 bytes in each row, the last byte is supplemented with 0. The width in the header of the BMP Information is the actual row-like prime number. For example, 63 is displayed. when reading the 63rd bytes of each line, you must read the supplemented byte before you can wrap the line.

The tcolor used for writing the vertex function in BCB differs from the byte sequence of the color table in the BMP file.
The tcolor structure is 0x00bbggrr, RR is the 8-bit red brightness, GG is the 8-Bit green brightness, BB is the 8-bit red brightness, and the maximum 8-bit is kept as 0;
The structure of the items in the BMP color table is 0x00rrggbb. The red, blue, and tcolor are the opposite. Therefore, to correct the items in BCB, use the switchcolor function to implement this function. However, I did not use the color _ structure and directly used the long data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.