For all the existing image file formats, we will introduce the BMP Image File Format here, and the image data in the file is not compressed, because the digital processing of images mainly processes each pixel in the image, the pixel value in the uncompressed BMP image exactly corresponds to the actual digital image to be processed, this file format is most suitable for us to digitize it. Remember that compressed images cannot be directly digitalized, such as JPEG and GIF files. At this time, extract the image files first, this involves some complicated compression algorithms. In the subsequent sections, we will discuss how to convert a special file format to a BMP file. After conversion, we can use the uncompressed BMP file format for subsequent processing. For JPEG, GIF, and other formats, the compression algorithm is involved, which requires the reader to have certain information theory knowledge. If it is expanded, you can write a book. Due to space limitations, we will only give a general explanation. If you are interested, you can refer to relevant books and materials.
1. BMP file structure
1. BMP file Composition
A bmp file consists of four parts: File Header, bitmap information header, color information, and graphic data. The file header contains information such as the file size, file type, and image data deviation from the file header length; the bitmap information header contains the size information of the image. The image uses several bits to represent a pixel, whether the image is compressed, and the number of colors used by the image. Color information includes the color table used by the image. You need to use this color table to generate a color palette when displaying the image. However, if the image is true color, each pixel of the image is represented in 24 bits, this information does not exist in the file, so you do not need to operate the palette. The data block in the file indicates the corresponding pixel value of the image. Note that the order of storing the pixel value in the file is from left to right, from bottom to top, that is, in the BMP file, the last line of pixels of the image is first stored, and the first line of pixels of the image is stored, it is stored in the order on the left and right. The other detail that requires attention from readers is: when each line of the image is stored in a file, if the number of bytes occupied by the row's pixel value is a multiple of 4, the row is stored normally. Otherwise, you must add 0 at the backend to make up a multiple of 4.
2. BMP File Header
The data structure of the BMP file header contains information such as the type, size, and start position of the BMP file. Its structure is defined as follows: typedef struct tagbitmapfileheader
{
Word bftype; // The type of the bitmap file, which must be "BM"
DWORD bfsize; // the size of the bitmap file, in bytes.
Word bfreserved1; // reserved word of the bitmap file, which must be 0
Word bfreserved2; // reserved word of the bitmap file, which must be 0
DWORD bfoffbits; // the starting position of the bitmap data, expressed in bytes as the offset relative to the bitmap file header.
} Bitmapfileheader; the structure occupies 14 bytes.
3. Bitmap header
The BMP Bitmap header is used to describe the size and other information of the bitmap. Its structure is as follows:
Typedef struct tagbitmapinfoheader {
DWORD bisize; // number of bytes occupied by the structure
Long biwidth; // The bitmap width, in pixels.
Long biheight; // The height of the bitmap, in pixels
Word biplanes; // The number of planes of the target device is unknown and must be 1.
Word bibitcount // The number of digits required for each pixel. It must be one of 1 (two-color), 4 (16 colors), 8 (256 colors), or 24 (true color ).
DWORD bicompression; // bitmap compression type, which must be 0 (not compressed), 1 (bi_rle8 compression type), or 2 (bi_rle4 compression type)
DWORD bisizeimage; // bitmap size, in bytes
Long bixpelspermeter; // horizontal bitmap resolution, number of workers per meter
Long biypelspermeter; // bitmap vertical resolution, number of bytes per meter
DWORD biclrused; // number of colors in the color table used by the bitmap
DWORD biclrimportant; // number of important colors in the bitmap display process
} Bitmapinfoheader; the structure occupies 40 bytes.
Note: For BMP file formats, when processing monochrome and true color images, no matter how large the image data is, the image data is not compressed. Generally, if the bitmap adopts the compression format, the 16-color image adopts the rle4 compression algorithm and the 256-color image adopts the rle8 compression algorithm.
4. Color Table
A color table is used to describe the color in a bitmap. It has several table items. Each table item is an rgbquad-type structure and defines a color. The rgbquad structure is defined as follows:
Typedef struct tagrgbquad {
Bytergbblue; // blue brightness (value range: 0-255)
Bytergbgreen; // The brightness of the green color (value range: 0-255)
Bytergbred; // The Red brightness (value range: 0-255)
Bytergbreserved; // reserved, must be 0
} Rgbquad;
The number of rgbquad structure data in the color table is determined by the bibitcount item in bitmapinfoheader. When bibitcount is 1, 4, and 8, there are 2, 16, and 256 color table items respectively. When bibitcount is 24, the image is true color. The color of each pixel is represented in three bytes, corresponding to the R, G, and B values respectively. The image file does not have a color table. The bitmap information header and the color table form bitmap information. The bitmapinfo structure is defined as follows:
Typedef struct tagbitmapinfo {
Bitmapinfoheader bmiheader; // Bitmap header
Rgbquad bmicolors [1]; // color table
} Bitmapinfo;
Note: In the rgbquad data structure, a reserved field rgbreserved is added, which does not represent any color and must be set to "0, in the color values defined in the rgbquad structure, the red, green, and blue colors are arranged in the same order as the color data in general true color image files: if the color of a pixel in a bitmap is described as ", FF, 00", it indicates that the pixel is red rather than blue.
5. bitmap data
The bitmap data records each pixel value of the bitmap or the index value of the corresponding pixel color table. The image record sequence is from left to right in the scan row, and the scanning rows are from bottom to top. This format is also called a bottom_up bitmap. Of course, there is a up_down bitmap, which records the order from top to bottom, and there is no compression form for this form of Bitmap. The number of bytes occupied by a pixel value of a bitmap: When bibitcount = 1, 8 pixels constitute 1 byte; When bibitcount = 4, 2 pixels constitute 1 byte; when bibitcount = 8, 1 pixel occupies 1 byte; When bibitcount = 24, 1 pixel occupies 3 bytes, and the image is a true color image. When the image is not true color, the image file contains a color table. The bitmap data indicates the index value of the corresponding pixel in the color table. When the image is true color, each pixel uses three bytes to represent the color values of the corresponding pixels of the image. Each byte corresponds to the values of R, G, and B, respectively. At this time, there is no color table in the image file. As I have mentioned above, Windows requires that the number of bytes occupied by a scanned row in an image file must be a multiple of 4 (in words), and insufficient bytes must be filled with 0, calculation of the number of bytes occupied by a scanned row in an image file:
Datasizeperline = (biwidth * bibitcount + 31)/8; // number of bytes occupied by a scanned row
The bitmap data size is calculated as follows (without compression ):
Datasize = datasizeperline * biheight.
The above is a description of the BMP file format. After understanding the above structure, you can operate the image file correctly and read or write it.
GIF Image File Format
The GIF image format is fully called Graphics Interchange Format. From this name, we can see that this image format is designed to transmit images over the network. GIF files do not support 24-bit true-color images. They can only store up to 256-color images or grayscale images. GIF files cannot store image data of cmy and his models. In addition, generally, there is no fixed Data Length and storage sequence in various data regions of GIF image files. Therefore, the first byte in the Data zone is used as a flag to facilitate the search for data areas; at last, you need to note that the image data stored in GIF files is arranged in two order: sequential or cross-arranged. Cross-arrangement is suitable for network transmission. This allows users to obtain the profile data of the current image before they fully grasp the image data.
The GIF file format is divided into two versions: 87 and 89. For the 87 version, the file consists of five parts, which appear in sequence: the file header block, the logical screen description block, the selectable color section, the image data block, and the final block that marks the end of the file. The block is always set to 3bh. The first and second parts use the GIF Image File Header structure description:
Gifheader :{
DB signature; // This field occupies six bytes. to indicate that the image is in GIF format, the first three characters must be "GIF" and the last three characters must be used to specify the version, 87 or 89.
DW screenwidth ;//
DW screendepth; // occupies two bytes, indicating the width and height of the image in pixels
DB globalflagbyte; // the bit of this byte is used for the color description.
DB backgroundcolor; // The index that indicates the background color of the image.
DB aspectratio; image Aspect Ratio
}
The color palette in GIF format is divided into a general color palette and a partial color palette, because the GIF format allows a file to store multiple images, there are two color palette, where the general color palette is suitable for all images in the file, the partial color palette only applies to an image. The data area in the format is generally divided into four parts: the image data recognition area, the local color palette data, the image data area obtained using the compression algorithm and the ending Mark area.
In gif89, it contains seven parts: File Header, General color palette data, image data area, and four supplementary data areas. They are mainly used to prompt the program to process images.
The following describes various RGB formats.
Rgb1, rgb4, and rgb8 are color palette RGB formats. when describing the format details of these media types, the bitmapinfoheader data structure is followed by a color palette (defining a series of colors ). Their image data is not a true color value, but an index of the current pixel color value in the palette. Take rgb1 (2-color Bitmap) as an example. For example, if the two color values defined in the palette are 0x000000 (black) and 0 xffffff (white), then the image data is 001101010111... (Each pixel is represented by one digit.) The color of each pixel is black, black, white, black, white, black, black, and white ....
22 rgb565 uses 16 bits to represent a pixel. 5 bits in the 16 bits are used for R, 6 bits are used for G, and 5 bits are used for B. In a program, one word (word, one word equals two bytes) is usually used to operate on one pixel. After a pixel is read, the meaning of each bit of the word is as follows:
High byte and low byte
R g B
The value of each RGB component can be obtained by combining the shielded word and the shift operation:
# Define rgb565_mask_red 0xf800
# Define rgb565_mask_green 0x07e0
# Define rgb565_mask_blue 0x001f
R = (wpixel & rgb565_mask_red)> 11; // value range: 0-31
G = (wpixel & rgb565_mask_green)> 5; // value range: 0-63
B = wpixel & rgb565_mask_blue; // value range: 0-31
Rgb555 is another 16-bit RGB format. The RGB component is represented by 5 bits (the remaining 1 bits are not used ). After reading a pixel with a word, the meaning of each bit of the word is as follows:
High byte and low byte
X r r g B (X indicates no, can be ignored)
The value of each RGB component can be obtained by combining the shielded word and the shift operation:
# Define rgb555_mask_red 0x7c00
# Define rgb555_mask_green 0x03e0
# Define rgb555_mask_blue 0x001f
R = (wpixel & rgb555_mask_red)> 10; // value range: 0-31
G = (wpixel & rgb555_mask_green)> 5; // value range: 0-31
B = wpixel & rgb555_mask_blue; // value range: 0-31
Rgb24 uses 24 bits to represent a pixel, and RGB is represented by 8 bits. The value range is 0-255. Note that the order of RGB components in the memory is BGR .... Generally, you can use the rgbtriple data structure to operate a pixel, which is defined:
Typedef struct tagrgbtriple {
Byte rgbtblue; // blue weight
Byte rgbtgreen; // green component
Byte rgbtred; // red weight
} Rgbtriple;
Rgb32 uses 32 bits to represent a pixel. Each RGB component uses 8 bits. The remaining 8 bits are used as the alpha channel or are not used. (Argb32 is rgb32 with alpha channel .) Note that the order of RGB components in the memory is bgra .... Generally, you can use the rgbquad data structure to operate a pixel, which is defined:
Typedef struct tagrgbquad {
Byte rgbblue; // blue weight
Byte rgbgreen; // green component
Byte rgbred; // red weight
Byte rgbreserved; // reserved bytes (used as alpha channel or ignored)
} Rgbquad;