组成
典型的BMP图像文件由四部分组成:
1:位图头文件数据结构,它包含BMP图像文件的类型、显示内容等信息;
2:位图信息数据结构,它包含有BMP图像的宽、高、压缩方法,以及定义颜色等信息;
3:调色板,这个部分是可选的,有些位图需要调色板,有些位图,比如真彩色图(24位的BMP)就不需要调色板;
4:位图数据,这部分的内容根据BMP位图使用的位数不同而不同,在24位图中直接使用RGB,而其他的小于24位的使用调色板中颜色索引值。
简单说BMP文件由文件头、位图信息头、颜色信息和图像数据四部分组成。
在linux中有开源BMP文件解析源码:bmp_layout.h
/* (C) Copyright 2002 * Detlev Zundel, 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 as * published by the Free Software Foundation; either version 2 of * 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 of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received 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;// 蓝色的亮度(值范围为0-255) __u8 green;// 绿色的亮度(值范围为0-255) __u8 red;// 红色的亮度(值范围为0-255) __u8 reserved;// 保留,必须为0 } __attribute__ ((packed)) bmp_color_table_entry_t;//调色板 //调色板结构数据的个数有bit_count来确定: 当bit_count=1,4,8时,分别有2,16,256个表项; 当bit_count=24时,没有颜色表项。 /* 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 */文件头14Byte char signature[2];//必须BM __u32 file_size;// 位图文件的大小 __u32 reserved;// 位图文件保留字,必须为0 __u32 data_offset;// 位图数据的起始位置,以相对于位图,文件头的偏移量表示,以字节为单位 /* InfoHeader */位图信息头40Byte __u32 size;// 本结构所占用字节数 __u32 width;// 位图的宽度,以像素为单位 __u32 height;// 位图的高度,以像素为单位 __u16 planes;// 目标设备的级别,必须为1 __u16 bit_count;// 每个像素所需的位数,必须是1(双色),4(16色),8(256色)或24(真彩色)之一 __u32 compression;// 位图压缩类型,必须是 0(不压缩),1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一 __u32 image_size;// 位图的大小,以字节为单位 __u32 x_pixels_per_m;// 位图水平分辨率,每米像素数 __u32 y_pixels_per_m;// 位图垂直分辨率,每米像素数 __u32 colors_used;// 位图实际使用的颜色表中的颜色数 __u32 colors_important;// 位图显示过程中重要的颜色数 /* ColorTable */调色板 } __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_ */ |
位图数据记录了位图的每一个像素值,记录顺序是在扫描行内是从左到右,扫描行之间是从下到上。位图的一个像素值所占的字节数:
当bit_count=1时,8个像素占1个字节;
当bit_count=4时,2个像素占1个字节;
当bit_count=8时,1个像素占1个字节;
当bit_count=24时,1个像素占3个字节; 规定一个扫描行所占的字节数必须是4的倍数(即以long为单位),不足的以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 < height; ++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 |