BMP Image Parsing

Source: Internet
Author: User
Tags bmp image
Composition

A typical BMP image file consists of four parts:

1: The data structure of the Bitmap header file, which contains information such as the type and content of the BMP image file;

2: The bitmap information data structure, which contains the width, height, compression method, and color of the BMP image;

3: color palette. This part is optional. Some bitmaps need a color palette, and some bitmaps, such as true color graphs (24-bit BMP) do not need a color palette;

4: bitmap data. The content varies depending on the number of digits used by the BMP bitmap. In the 24-Bit Bitmap, RGB is used directly, the other less than 24 digits use the color index value in the color palette.

To put it simply, the BMP file consistsFile Header, bitmap information header, color informationAndImage DataIt consists of four parts.

In Linux, there are open source BMP File Parsing source code:BMP _layout.h

/* (C) Copyright 2002
* Detlevzundel, denx software engineering, dzu@denx.de.
*
* See File credits for list of people who contributed to this
* Project.
*
* This program is free software; you can redistribute it and/or
* Modify it under the terms of the GNU General Public License
* Published by the Free Software Foundation; either version 2
* The license, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* But without any warranty; without even the implied warranty
* Merchantability or fitness for a special purpose. See
* GNU General Public License for more details.
*
* You shoshould have written ed a copy of the GNU General Public License
* Along with this program; if not, write to the Free Software
* Foundation, inc., 59 temple place, Suite 330, Boston,
* Ma 02111-1307 USA
*/

/*************************************** *********************************/
/*** Layout of a BMP file */
/*************************************** *********************************/

# Ifndef _ BMP _h _
# DEFINE _ BMP _h _

Typedef struct BMP _color_table_entry {
_ U8 blue; // The brightness of blue (value range: 0-255)
_ U8 green; // The brightness of green (value range: 0-255)
_ U8 red; // brightness of the red color (value range: 0-255)
_ U8 reserved; // reserved, must be 0
} _ Attribute _ (packed) BMP _color_table_entry_t; // color palette
// The number of color palette structure data is determined by bit_count: When bit_count = 256, 8, there are 2, 16, table items, and when bit_count = 24, there is no color table item.
/* When accessing these fields, remember that they are stored in little
Endian format, so use Linux macros, e.g. le32_to_cpu (width )*/

Typedef struct BMP _header {
/* Header */File Header 14 bytes
Char signature [2]; // required BM
_ U32 file_size; // the size of the bitmap file
_ U32 reserved; // reserved word of the bitmap file, which must be 0
_ U32 data_offset; // the starting position of the bitmap data, expressed in bytes as the offset of the file header relative to the bitmap.
/* Infoheader */Bitmap header 40 bytes
_ U32 size; // number of bytes occupied by the structure
_ U32 width; // The bitmap width, in pixels
_ U32 height; // The height of the bitmap, in pixels
_ Planes; // the target device level, which must be 1
_ 256 bit_count; // The number of digits required for each pixel. The value must be 1 (two-color), 4 (16 colors), 8 (colors), or 24 (true color ).
_ U32 compression; // bitmap compression type, which must be 0 (not compressed), 1 (bi_rle8 compression type), or 2 (bi_rle4 compression type)
_ U32 image_size; // bitmap size, in bytes
_ U32 x_pixels_per_m; // horizontal bitmap resolution, number of workers per meter
_ U32 y_pixels_per_m; // bitmap vertical resolution, number of bytes per meter
_ U32 colors_used; // number of colors in the color table used by the bitmap
_ U32 colors_important; // number of important colors in the bitmap display process
/* Colortable */color palette

} _ Attribute _ (packed) BMP _header_t;

Typedef struct BMP _image {
BMP _header_t header;
/* We use a zero sized array just as a placeholder for Variable
Sized array */
BMP _color_table_entry_t color_table [0];
} BMP _image_t;

/* Data in the BMP _image is aligned to this length */
# Define BMP _data_align 4

/* Constants for the compression field */
# Define BMP _bi_rgb 0
# Define BMP _bi_rle8 1
# Define BMP _bi_rle4 2

# Endif/* _ BMP _h _*/

Bitmap data

The bitmap data records each pixel value of the bitmap. The record sequence is from left to right in the scan row, and the rows are from bottom to top. The number of bytes occupied by a pixel value of a bitmap:

When bit_count = 1, 8 pixels constitute 1 byte;

When bit_count = 4, 2 pixels constitute 1 byte;

When bit_count = 8, 1 pixel occupies 1 byte;

When bit_count = 24, one pixel occupies three bytes. The number of bytes of A scanned row must be a multiple of four (in the unit of long ), insufficient values are filled with 0,

# Ifdef config_second_logo
Static void prepare_secondlogo (void)
{

Unsigned long width, height;
BMP _image_t * BMP;
Uchar * bmap;
Ushort padded_line;
Int X, Y, I, j;

BMP = (BMP _image_t *) (LCD _base + calc_fbsize ());
Bmap = (uchar *) BMP + le32_to_cpu (BMP-> header. data_offset );
Width = BMP-> header. width;
Height = BMP-> header. height;
Printf ("\ n ----------- width = % lD, Height = % LD ---------------- \ n", width, height );
X = max (0, (panel_info.vl_width-width)/2 );
Y = max (0, (panel_info.vl_height-height)/2 );
Padded_line = (width & 0x3 )? (Width &~ 0x3) + 4): (width );
Memset (char *) LCD _base-2 * calc_fbsize (), 0xff, 2 * calc_fbsize ());
Uchar * Fb = (uchar *) (LCD _base-2 * calc_fbsize () +
(Y + height-1) * LCD _line_length + x * (nbits (panel_info.vl_bpix)/8 ));

For (I = 0; I For (j = 0; j <width; j ++ ){
* (FB ++) = * (bmap ++ );
* (FB ++) = * (bmap ++ );
* (FB ++) = * (bmap ++ );
FB ++;
}
Bmap + = (padded_line-width) * (nbits (panel_info.vl_bpix)/8 );
FB-= (width * (nbits (panel_info.vl_bpix)/8 + LCD _line_length );
}
}
Void show_secondlogo (void)
{

Read_boot_logo (char *) (LCD _base + calc_fbsize (), "boot1.logo ");
Prepare_secondlogo ();
Panel_info.vl_fbuf = (ulong) (uchar *) LCD _base-2 * calc_fbsize ());
Fb_enable_controller (& panel_info );
V8de_rd_channel3_en ();
Memcpy (LCD _base, LCD _base-2 * calc_fbsize (), calc_fbsize ());
Printf ("\ n --------- now show the second logo ----------- \ n ");
}
# Endif

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.