图片解码首先是最简单的bmp图片解码,关于bmp的结构可自行查阅,代码如下- #ifndef __BMPDECODE_H_
- #define __BMPDECODE_H_
- #include "ff.h"
- #include "lcd.h"
- #include "stdlib.h"
- #include "usb_type.h"
- //重定义区
- typedef char CHAR; //数据类型重定义,便于移植
- typedef short SHORT;
- //typedef int LONG;
- //typedef unsigned int DWORD;
- typedef int BOOL;
- typedef u8 BYTE;
- typedef unsigned short WORD;
- //#define FALSE 0
- //#define TRUE 1
- //BMP图象数据压缩的类型
- #define BI_RGB 0L //无压缩
- #define BI_RLE8 1L //每个像素四个bit
- #define BI_RLE4 2L //每个像素8个bit
- #define BI_BITFIELDS 3L //每个像素的bit由指定的掩码决定
- #define bufferToLong(buffer,t) (LONG)((((u32)bmpbuffer[t])) + (((u32)bmpbuffer[t+1])<<8) +(((u32)bmpbuffer[t+2])<<16) + (((u32)bmpbuffer[t+3])<<24))
- #define bufferToWord(buffer,t) (WORD)(((u32)(buffer[t])) + (((u32)(buffer[t+1]))<<8))
- #define bufferToDword(buffer,t) (DWORD)((((u32)bmpbuffer[t])) + (((u32)bmpbuffer[t+1])<<8) +(((u32)bmpbuffer[t+2])<<16) + (((u32)bmpbuffer[t+3])<<24))
- #define RGB888buffertoRGB565(buffer,t) ((((u16)buffer[t])>>3) + ((((u16)buffer[t+1])>>2)<<5) + ((((u16)buffer[t+2])>>3)<<11))
- //BMP文件头 14个字节
- typedef __packed struct
- {
- WORD bfType ; //文件标志.只对'BM',用来识别BMP位图类型 2
- DWORD bfSize ; //文件大小,占四个字节 4
- WORD bfReserved1 ;//保留 2
- WORD bfReserved2 ;//保留 2
- DWORD bfOffBits ; //从文件开始到位图数据(bitmap data)开始之间的的偏移量,这一段中存放的就是文件信息 4
- }BITMAPFILEHEADER ; //位图文件头
- //位图信息头
- typedef __packed struct
- {
- DWORD biSize ; //说明BITMAPINFOHEADER结构所需要的字数。
- LONG biWidth ; //说明图象的宽度,以象素为单位
- LONG biHeight ; //说明图象的高度,以象素为单位
- WORD biPlanes ; //为目标设备说明位面数,其值将总是被设为1
- WORD biBitCount ; //说明比特数/象素,其值为1、4、8、16、24、或32
- DWORD biCompression ; //说明图象数据压缩的类型。其值可以是下述值之一:
- /*BI_RGB:没有压缩;
- *BI_RLE8:每个象素8比特的RLE压缩编码,压缩格式由2字节组成(重复象素计数和颜色索引);
- *BI_RLE4:每个象素4比特的RLE压缩编码,压缩格式由2字节组成
- *BI_BITFIELDS:每个象素的比特由指定的掩码决定。*/
- DWORD biSizeImage ;//说明图象的大小,以字节为单位。当用BI_RGB格式时,可设置为0
- LONG biXPelsPerMeter ;//说明水平分辨率,用象素/米表示
- LONG biYPelsPerMeter ;//说明垂直分辨率,用象素/米表示
- DWORD biClrUsed ; //说明位图实际使用的彩色表中的颜色索引数
- DWORD biClrImportant ; //说明对图象显示有重要影响的颜色索引的数目,如果是0,表示都重要。
- }BITMAPINFOHEADER;
- //颜色索引表,每个颜色索引占4个字节,16位以及以下才会有这个数据 也就是说 最多65535个数据
- //当然,最好不要用16位色的,浪费空间
- typedef __packed struct
- {
- BYTE rgbBlue ; //指定蓝色强度
- BYTE rgbGreen ; //指定绿色强度
- BYTE rgbRed ; //指定红色强度
- BYTE rgbReserved ;//保留,设置为0
- }RGBQUAD ;
- BOOL BmpDecode(u8 *filename,u16 sx,u16 sy,u16 ex,u16 ey);
- #endif
复制代码- u8 bmpbuffer[1024] = {0}; //存储bmp文件的数组
- //解码这个BMP文件
- //设定显示起始位置以及终止位置
- BOOL BmpDecode(u8 *filename,u16 sx,u16 sy,u16 ex,u16 ey)
- {
- FIL f_bmp; //文件系统变量,用于读取bmp文件
- u16 filereadnum = 0; //用于记录文件字节读取数量
- FRESULT res; //文件读取返回信息
- BITMAPFILEHEADER bmpfileHead; //位图文件头
- BITMAPINFOHEADER bmpinfoHead; //位图信息头
- u8 colorByte = 0; //用于标记颜色位
- u16 index = 0; //读取文件信息时候用来定位
- u16 uitemp = 0; //记录实际数据一行有多少个点
- u16 xtemp = 0;
- u16 ytemp = 0; //显示时辅助计数
- u16 colortemp = 0; //颜色缓冲
-
- res=f_open(&f_bmp,(const TCHAR*)filename,FA_READ); //打开文件
- if(res!=FR_OK) return FALSE;
- res = f_read(&f_bmp,bmpbuffer,1024,(UINT*)&filereadnum); //读取文件
- if(res!=FR_OK) return FALSE;
-
- //获取位图文件头
- bmpfileHead.bfType = (WORD)((((u16)bmpbuffer[0])<<8) + bmpbuffer[1+1]);
- bmpfileHead.bfSize = bufferToDword(bmpbuffer,2);
- bmpfileHead.bfReserved1 = bufferToWord(bmpbuffer,6);
- bmpfileHead.bfReserved2 = bufferToWord(bmpbuffer,8);
- bmpfileHead.bfOffBits = bufferToDword(bmpbuffer,10); //数据段开始地址
- //获取位图信息头
- bmpinfoHead.biSize = bufferToDword(bmpbuffer,14);
- bmpinfoHead.biWidth = bufferToLong(bmpbuffer,18);
- bmpinfoHead.biHeight = bufferToLong(bmpbuffer,22);
- bmpinfoHead.biPlanes = bufferToWord(bmpbuffer,26);
- bmpinfoHead.biBitCount = bufferToWord(bmpbuffer,28); //颜色位
- bmpinfoHead.biCompression = bufferToDword(bmpbuffer,30);
- bmpinfoHead.biSizeImage = bufferToDword(bmpbuffer,34);
- bmpinfoHead.biXPelsPerMeter = bufferToLong(bmpbuffer,38);
- bmpinfoHead.biYPelsPerMeter = bufferToLong(bmpbuffer,42);
- bmpinfoHead.biClrUsed = bufferToDword(bmpbuffer,46);
- bmpinfoHead.biClrImportant = bufferToDword(bmpbuffer,50);
- index = bmpfileHead.bfOffBits;
- //所有信息得到,这里应该进行测试了
-
- colorByte = bmpinfoHead.biBitCount/8; //颜色位数 16 / 24 / 32 得到2/3/4
- if((bmpinfoHead.biWidth%4) != 0) //不是4的倍数
- {
- uitemp = (((bmpinfoHead.biWidth/4) + 1) * 4);
- }
- else //是4的倍数
- {
- uitemp = bmpinfoHead.biWidth ;
- }
- if(colorByte == 3) //24位颜色
- {
- while(1)
- {
- if(index <= 1021)
- {
- colortemp = RGB888buffertoRGB565(bmpbuffer,index);
- index = index +3;
- if(sx+xtemp <ex && sy+ytemp < ey)
- LCD_Draw_Point_Buffer(sx+xtemp,sy+ytemp,colortemp);
- xtemp++;
- if(xtemp >= uitemp)
- {
- xtemp = 0;
- ytemp++;
- }
- }
- else
- {
- if(index == 1022)
- {
- if(sx+xtemp <ex && sy+ytemp < ey)
- LCD_Draw_Point_Buffer(sx+xtemp,sy+ytemp,colortemp);
- xtemp++;
- if(xtemp >= uitemp)
- {
- xtemp = 0;
- ytemp++;
- }
- index = 1;
- }
- else if(index == 1023)
- {
- if(sx+xtemp <ex && sy+ytemp < ey)
- LCD_Draw_Point_Buffer(sx+xtemp,sy+ytemp,colortemp);
- xtemp++;
- if(xtemp >= uitemp)
- {
- xtemp = 0;
- ytemp++;
- }
- index = 2;
- }
- else index = 0;
- res = f_read(&f_bmp,bmpbuffer,1024,(UINT*)&filereadnum); //读取文件
- if(res||filereadnum==0)break; //读取出错
- }
- }
- }
- f_close(&f_bmp); /* 关闭打开的文件 */
-
- LCD_Flush();
- return TRUE;
- }
复制代码为了防止图片刷新速度过慢可以在内存中建立一个屏幕缓存,解码完成后一次性刷入屏幕 JPEG图片解码使用网络上一个tjpgdec的库,该库的移植如下 - #ifndef __JPGDEC_CALLBACK_H_
- #define __JPGDEC_CALLBACK_H_
- #include "lcd.h"
- #include "ff.h"
- #include "tjpgd.h"
- #include "stdlib.h"
- void load_jpg (FIL *fp,void *work,UINT sz_work);
- void load_file (const char *fn);
- #endif
复制代码- // 以下3句宏定义在tjpgd.h中修改
- static int MaskL = 0;
- static int MaskR = LCD_X_SIZE - 1;
- static int MaskT = 0;
- static int MaskB = LCD_Y_SIZE - 1;
- #define SIZE_OF_FILE_BUFFER 4096
- BYTE* jpegbuffer; //图像文件数据缓冲区
- /*********************************************************************************************
- 函数名称: STM32_Display
- 函数功能: 在TFTLCD屏幕上显示图片
- 入口参数: 见函数头
- 出口参数: 无
- 全局变量: 无
- 备注说明: 无
- *********************************************************************************************/
- void Lcd_Display_Rect(
- int left, /*图片左方起始点,即一行的起始点 */
- int right, /*图片右方的结束点,即一行的结束点*/
- int top, /* 图片上方的起始点,即一列的起始点 */
- int bottom, /*图像下方的结束点,即一列的结束点 */
- const uint16_t * RGB_Data_Pointer /* 待显示的图像数据,RGB格式*/
- )
- {
- int yc, xc, xl, xs;
- unsigned short pd;
- if (left > right || top > bottom)
- {
- return; // Check varidity
- }
-
- if (left > MaskR || right < MaskL || top > MaskB || bottom < MaskT)
- {
- return; // Check if in active area
- }
- yc = bottom - top + 1 ; /* Vertical size */
- xc = right - left + 1; xs = 0; /* Horizontal size and skip */
-
- // 已经计算出了要绘制的x方向pixels和y方向pixels
- // 上下左右的值已经校正为,为屏幕上的绝对位置,由此可以算出屏幕缓冲区的起始位置
- do { /* Send image data */
- xl = xc;
- do {
- pd = *RGB_Data_Pointer++;
- LCD_Draw_Point_Buffer(right - xl,bottom - yc,pd); //显示像素
- } while (--xl);
- RGB_Data_Pointer += xs;
- } while (--yc);
- }
- BYTE Buff[1024] __attribute__ ((aligned(4))); //定义全局数组变量,作为输入的缓冲区,强制4字节对齐
- /*********************************************************************************************
- 函数名称: Jpeg_Data_in_func
- 函数功能: 用户自定义的用于输入文件数据的功能函数
- 入口参数: 见函数头
- 出口参数: 读取或者删除的数据量
- 全局变量: 无
- 备注说明: 本函数在解码准备工作中用于读取文件头信息
- *********************************************************************************************/
- UINT Jpeg_Data_in_func (
- JDEC* jd, /*储存待解码的对象信息的结构体 */
- BYTE* buff, /* 输入数据缓冲区 (NULL:删除数据) */
- UINT nd /*需要从输入数据流读出/删除的数据量*/
- )
- {
- UINT rb;
- FIL * dev = (FIL *)jd->device; /* 待解码的文件的信息,使用FATFS中的FIL结构类型进行定义 */
- if (buff) /*读取数据有效,开始读取数据 */
- {
- f_read(dev, buff, nd, &rb); //调用FATFS的f_read函数,用于把jpeg文件的数据读取出来
- return rb; /* 返回读取到的字节数目*/
- }
- else
- {
- return (f_lseek(dev, f_tell(dev) + nd) == FR_OK) ? nd : 0;/* 重新定位数据点,相当于删除之前的n字节数据 */
- }
- }
- /*********************************************************************************************
- 函数名称: STM32_out_func
- 函数功能: 用户自定义的用于输出RGB位图数据的功能函数
- 入口参数: 见函数头
- 出口参数: 1:令解码函数继续执行
- 全局变量: 无
- 备注说明: 无
- *********************************************************************************************/
- UINT Jpeg_Data_out_func (
- JDEC* jd, /*储存待解码的对象信息的结构体*/
- void* bitmap, /* 指向等待输出的RGB位图数据 的指针*/
- JRECT* rect /* 等待输出的矩形图像的参数 */
- )
- {
- jd = jd; /* 说明:输出函数中JDEC结构体没有用到 */
- //显示一块区域到LCD上
- Lcd_Display_Rect(rect->left, rect->right, rect->top, rect->bottom, (uint16_t*)bitmap);
-
- return 1; /*返回1,使解码工作继续执行 */
- }
- //加载并显示jpg文件
- void load_jpg (
- FIL *fp, /* 指向打开的文件执政 */
- void *work, /*指向四字节对其的工作区缓存 */
- UINT sz_work /*工作区的大小 */
- )
- {
- JDEC jd; /* Decoder object (124 bytes) */
- JRESULT rc;
- BYTE scale;
- /* Prepare to decompress the file */
- rc = jd_prepare(&jd, Jpeg_Data_in_func, work, sz_work, fp);
- if (rc == JDR_OK)
- {
- /* 根据图片大小选选择一个刚刚好能够缩放的图片比例 */
- for (scale = 0; scale < 3; scale++)
- {
- if ((jd.width >> scale) <= LCD_X_SIZE && (jd.height >> scale) <= LCD_Y_SIZE) break;
- }
- rc = jd_decomp(&jd, Jpeg_Data_out_func, scale); /* Start to decompress */
- } else
- {
- //显示错误,将错误信息打印在屏幕上
- printf("jpg error %d\r\n",rc);
- }
- }
- /*参数:指向文件名 */
- void load_file (const char *fn)
- {
- FIL fil; /* Pointer to a file object */
- if (f_open(&fil, fn, FA_READ) == FR_OK)
- {
- jpegbuffer = NULL;
- jpegbuffer = malloc(SIZE_OF_FILE_BUFFER);
- if(jpegbuffer != NULL)
- {
- LCD_Fill_Buffer(LCD_BLACK);
- load_jpg(&fil, (void *)jpegbuffer, SIZE_OF_FILE_BUFFER ); //打开jpg文件并解码显示
- }
- f_close(&fil);
- if(jpegbuffer != NULL)free(jpegbuffer);
- LCD_Flush();
- }
- else
- {
- printf("open file failed\r\n");
- }
- }
复制代码该库的解码性能还不错,使用了图片流的概念来进行解码操作,剩下的是一个GIF图片解码,如下 - #ifndef __GIFDECODE_H_
- #define __GIFDECODE_H_
- #include "ff.h"
- #include "lcd.h"
- #include "stdlib.h"
- #define MAX_NUM_LWZ_BITS 12
- #define LCD_MAX_LOG_COLORS 256
- #define GIF_INTRO_TERMINATOR ';' //0X3B GIF文件结束符
- #define GIF_INTRO_EXTENSION '!' //0X21
- #define GIF_INTRO_IMAGE ',' //0X2C
- #define GIF_COMMENT 0xFE
- #define GIF_APPLICATION 0xFF
- #define GIF_PLAINTEXT 0x01
- #define GIF_GRAPHICCTL 0xF9
- #define PIC_FORMAT_ERR 0x27 //格式错误
- #define PIC_SIZE_ERR 0x28 //图片尺寸错误
- #define PIC_WINDOW_ERR 0x29 //窗口设定错误
- #define PIC_MEM_ERR 0x11 //内存错误
- typedef struct
- {
- u8 aBuffer[258]; // Input buffer for data block
- short aCode [(1 << MAX_NUM_LWZ_BITS)]; // This array stores the LZW codes for the compressed strings
- u8 aPrefix[(1 << MAX_NUM_LWZ_BITS)]; // Prefix character of the LZW code.
- u8 aDecompBuffer[3000]; // Decompression buffer. The higher the compression, the more bytes are needed in the buffer.
- u8 * sp; // Pointer into the decompression buffer
- int CurBit;
- int LastBit;
- int GetDone;
- int LastByte;
- int ReturnClear;
- int CodeSize;
- int SetCodeSize;
- int MaxCode;
- int MaxCodeSize;
- int ClearCode;
- int EndCode;
- int FirstCode;
- int OldCode;
- }LZW_INFO;
- //逻辑屏幕描述块
- __packed typedef struct
- {
- u16 width; //GIF宽度
- u16 height; //GIF高度
- u8 flag; //标识符 1:3:1:3=全局颜色表标志(1):颜色深度(3):分类标志(1):全局颜色表大小(3)
- u8 bkcindex; //背景色在全局颜色表中的索引(仅当存在全局颜色表时有效)
- u8 pixratio; //像素宽高比
- }LogicalScreenDescriptor;
- //图像描述块
- __packed typedef struct
- {
- u16 xoff; //x方向偏移
- u16 yoff; //y方向偏移
- u16 width; //宽度
- u16 height; //高度
- u8 flag; //标识符 1:1:1:2:3=局部颜色表标志(1):交织标志(1):保留(2):局部颜色表大小(3)
- }ImageScreenDescriptor;
- //图像描述
- __packed typedef struct
- {
- LogicalScreenDescriptor gifLSD; //逻辑屏幕描述块
- ImageScreenDescriptor gifISD; //图像描述快
- u16 colortbl[256]; //当前使用颜色表
- u16 bkpcolortbl[256]; //备份颜色表.当存在局部颜色表时使用
- u16 numcolors; //颜色表大小
- u16 delay; //延迟时间
- LZW_INFO *lzw; //LZW信息
- }gif89a;
- extern u8 gifdecoding; //GIF正在解码标记.
- u8 gif_check_head(FIL *file); //检测GIF头
- u16 gif_getrgb565(u8 *ctb); //将RGB888转为RGB565
- u8 gif_readcolortbl(FIL *file,gif89a * gif,u16 num); //读取颜色表
- u8 gif_getinfo(FIL *file,gif89a * gif); //得到逻辑屏幕描述,图像尺寸等
- void gif_savegctbl(gif89a* gif); //保存全局颜色表
- void gif_recovergctbl(gif89a* gif); //恢复全局颜色表
- void gif_initlzw(gif89a* gif,u8 codesize); //初始化LZW相关参数
- u16 gif_getdatablock(FIL *gfile,u8 *buf,u16 maxnum); //读取一个数据块
- u8 gif_readextension(FIL *gfile,gif89a* gif, int *pTransIndex,u8 *pDisposal); //读取扩展部分
- int gif_getnextcode(FIL *gfile,gif89a* gif); //从LZW缓存中得到下一个LZW码,每个码包含12位
- int gif_getnextbyte(FIL *gfile,gif89a* gif); //得到LZW的下一个码
- u8 gif_dispimage(FIL *gfile,gif89a* gif,u16 x0,u16 y0,int Transparency, u8 Disposal); //显示图片
- void gif_clear2bkcolor(u16 x,u16 y,gif89a* gif,ImageScreenDescriptor pimge); //恢复成背景色
- u8 gif_drawimage(FIL *gfile,gif89a* gif,u16 x0,u16 y0); //画GIF图像的一帧
- u8 gif_decode(const u8 *filename,u16 x,u16 y,u16 width,u16 height);//在指定区域解码一个GIF文件.
- void gif_quit(void); //退出当前解码.
- #endif
复制代码这是UCGUI的库,性能差一点的单片机解码起来有点慢,看起来有点卡,哦对了还是用了fatfs文件系统,移植接口如下 - /*-----------------------------------------------------------------------*/
- /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */
- /*-----------------------------------------------------------------------*/
- /* If a working storage control module is available, it should be */
- /* attached to the FatFs via a glue function rather than modifying it. */
- /* This is an example of glue functions to attach various exsisting */
- /* storage control modules to the FatFs module with a defined API. */
- /*-----------------------------------------------------------------------*/
- #include "diskio.h" /* FatFs lower layer API */
- #include "sdcard.h" /* Example: MMC/SDC contorl */
- #define MMC 0 /* Example: Map MMC/SD card to drive number 1 */
- /*-----------------------------------------------------------------------*/
- /* Get Drive Status */
- /*-----------------------------------------------------------------------*/
- DSTATUS disk_status (
- BYTE pdrv /* Physical drive nmuber to identify the drive */
- )
- {
- DSTATUS stat;
- int result;
- switch (pdrv)
- {
- case MMC :
- result = SDCARD_Get_Sector_Count();
- if(result == 0)stat = STA_NOINIT;
- else stat = 0;//OK
- return stat;
- }
- return STA_NOINIT;
- }
- /*-----------------------------------------------------------------------*/
- /* Inidialize a Drive */
- /*-----------------------------------------------------------------------*/
- DSTATUS disk_initialize (
- BYTE pdrv /* Physical drive nmuber to identify the drive */
- )
- {
- DSTATUS stat;
- int result;
- switch (pdrv)
- {
- case MMC :
- result = SDCARD_Init();
- if(result == 0)stat = 0;
- else //STM32 SPI的bug,在sd卡操作失败的时候如果不执行下面的语句,可能导致SPI读写异常
- {
- SPI2_Set_Speed(SPI_SPEED_256);
- SPI2_Write_Read_Byte(0xff);//提供额外的8个时钟
- SPI2_Set_Speed(SPI_SPEED_4);
- stat = STA_NOINIT;//error
- }
- return stat;
- }
- return STA_NOINIT;
- }
- /*-----------------------------------------------------------------------*/
- /* Read Sector(s) */
- /*-----------------------------------------------------------------------*/
- DRESULT disk_read (
- BYTE pdrv, /* Physical drive nmuber to identify the drive */
- BYTE *buff, /* Data buffer to store read data */
- DWORD sector, /* Sector address in LBA */
- UINT count /* Number of sectors to read */
- )
- {
- DRESULT res;
- int result;
- switch (pdrv)
- {
- case MMC :
- result = SDCARD_Read_Sector(buff, sector, count);
- if(result == 0)res = RES_OK;
- else res = RES_ERROR;
- return res;
- }
- return RES_PARERR;
- }
- /*-----------------------------------------------------------------------*/
- /* Write Sector(s) */
- /*-----------------------------------------------------------------------*/
- #if _USE_WRITE
- DRESULT disk_write (
- BYTE pdrv, /* Physical drive nmuber to identify the drive */
- const BYTE *buff, /* Data to be written */
- DWORD sector, /* Sector address in LBA */
- UINT count /* Number of sectors to write */
- )
- {
- DRESULT res;
- int result;
- switch (pdrv)
- {
- case MMC :
- result = SDCARD_Write_Sector((u8*)buff, sector, count);
- if(result == 0)res = RES_OK;
- else res = RES_ERROR;
- return res;
- }
- return RES_PARERR;
- }
- #endif
- /*-----------------------------------------------------------------------*/
- /* Miscellaneous Functions */
- /*-----------------------------------------------------------------------*/
- #if _USE_IOCTL
- DRESULT disk_ioctl (
- BYTE pdrv, /* Physical drive nmuber (0..) */
- BYTE cmd, /* Control code */
- void *buff /* Buffer to send/receive control data */
- )
- {
- DRESULT res;
- switch (pdrv)
- {
- case MMC :
- switch(cmd)
- {
- case CTRL_SYNC:
- SD_CS=0;
- if(SDCARD_Wait_Ready()==0)res = RES_OK;
- else res = RES_ERROR;
- SD_CS=1;
- break;
- case GET_SECTOR_SIZE:
- *(WORD*)buff = 512;
- res = RES_OK;
- break;
- case GET_BLOCK_SIZE:
- *(WORD*)buff = 8;
- res = RES_OK;
- break;
- case GET_SECTOR_COUNT:
- *(DWORD*)buff = SDCARD_Get_Sector_Count();
- res = RES_OK;
- break;
- default:
- res = RES_PARERR;
- break;
- }
- return res;
- }
- return RES_PARERR;
- }
- #endif
复制代码
|