解码带压缩形式的Bitmap BMP图像并使用Python可视化解码后实际图像
参考文章
一、数据定义
#define TFT_LCD_PHY_XWIDTH (320)
#define TFT_LCD_PHY_YHEIGHT (240)
#pragma pack(1)
typedef struct
{
u16 FileType;
u32 FileSize;
u16 Reserve1;
u16 Reserve2;
u32 ImgDataOffset;
}BitMapFileHeader_TypeDef, * pBitMapFileHeader_TypeDef;
typedef struct
{
u32 InfoHeaderSize;
u32 ImgWidth;
u32 ImgHeight;
u16 ColorPlanes;
u16 PixelsBits;
u32 CompresType;
u32 ImgDataSize;
u32 HorResolut;
u32 VerResolut;
u32 ColorPalette;
u32 ColorImportant;
}BitMapInfoHeader_TypeDef, * pBitMapInfoHeader_TypeDef;
typedef struct
{
u8 B;
u8 G;
u8 R;
u8 A;
}BitmapARGB_TypeDef;
typedef struct
{
BitMapFileHeader_TypeDef FileHeader;
BitMapInfoHeader_TypeDef InfoHeader;
}BitMapInfo_TypeDef, * pBitMapInfo_TypeDef, * BitMapInfo_TypeDef_t;
typedef union
{
u8 ByteDat;
struct
{
u8 Bit0:1;
u8 Bit1:1;
u8 Bit2:1;
u8 Bit3:1;
u8 Bit4:1;
u8 Bit5:1;
u8 Bit6:1;
u8 Bit7:1;
}Bits;
}Type8Bits_TypeDef;
#pragma pack()
typedef struct
{
u32 PaletteData[256];
u32 DisplayMemory[TFT_LCD_PHY_XWIDTH * TFT_LCD_PHY_YHEIGHT];
u8 ImageData[1024 * 5];
}BitmapDecode_TypeDef;
BitmapDecode_TypeDef AppBitmapDecode = {
0};
二、获取调色板数据
void vGet_Palette_Data(const u8 * pSrcDat, u32 * pColorDat, u16 count)
{
u16 i = 0;
u32 * pARGB = (u32 *)(pSrcDat + sizeof(BitMapInfo_TypeDef));
for (i = 0; i < count; ++i)
{
pColorDat[i] = *pARGB++;
}
}
三、Bitmap图片解码
static u8 ubDecode_Bitmap(const u8 * pSrcDat)
{
const u8 * pDat = NULL;
Type8Bits_TypeDef bitDat = {
0};
BitMapInfo_TypeDef bitmapInfo = {
0};
u32 color = 0, index = 0, temp = 0;
u32 widthCount = 0, heightCount = 0;
u16 i = 0, num = 0, count = 0;
u16 width = 0, height = 0;
u16 line = 0, colum = 0;
memcpy((void *)(&bitmapInfo), (const void *)pSrcDat, sizeof(BitMapInfo_TypeDef));
vGet_Palette_Data(pSrcDat, AppBitmapDecode.PaletteData, bitmapInfo.InfoHeader.ColorPalette);
vShow_ColorPalette_Table(AppBitmapDecode.PaletteData, bitmapInfo.InfoHeader.ColorPalette);
pDat = pSrcDat + bitmapInfo.FileHeader.ImgDataOffset;
while(1)
{
if (*pDat == 0)
{
pDat++;
switch (*pDat)
{
case 0x00:
{
pDat++;
if (bitmapInfo.InfoHeader.ImgWidth >= widthCount)
{
temp = bitmapInfo.InfoHeader.ImgWidth - widthCount;
if (!bitDat.Bits.Bit0)
{
bitDat.Bits.Bit0 = 1;
printf("Tips2......%u\r\n", index);
}
}
else
{
temp = bitmapInfo.InfoHeader.ImgWidth - (widthCount % bitmapInfo.InfoHeader.ImgWidth);
heightCount += (widthCount / bitmapInfo.InfoHeader.ImgWidth);
if (!bitDat.Bits.Bit1)