主函数的部分代码
/*************************************************************************************************************************
*函数 : static PIC_ERROR OpenPictureFile(const char *FileName,u8 *buff,u32 FileMaxSize)
*功能 : 打开一张,并将数据读取到内存
*参数 : FileName:文件名,路径指针;buff:读取缓冲区;FileMaxSize:文件最大限制
*返回 : PIC_ERROR
*依赖 : FATFS文件系统支持
*作者 : cp1300@139.com
*时间 : 20121207
*最后修改时间 : 20121209
*说明 : 调用FATFS打开图片文件
*************************************************************************************************************************/
int OpenPictureFile(const char *FileName,u8 *buff,u32 FileMaxSize)
{
FIL file;
UINT cnt;
int status;
int error = 0;
status = f_open(&file,FileName,FA_READ); //只读方式打开文件
if(status != FR_OK) //打开文件错误
{
uart_printf("open \"%s\" error!\r\n",FileName);
error = 1;
}
else
{
//获取文件大小
uart_printf("file size : %dB\r\n",file.fsize); //输出文件大小
if(file.fsize > FileMaxSize)
{
uart_printf("file size > %d\r\n",FileMaxSize);
error = 2;
}
else
{
status = f_read(&file,buff,file.fsize,&cnt); //读取文件
uart_printf("Read File Num=%d\r\n",cnt);
if(cnt == file.fsize) //判断文件是否读取完毕
{
uart_printf("read file end!\r\n");
error = 0;
}
else
{
uart_printf("read file error!\r\n");
error = 3;
}
}
}
f_close(&file);
return error;
}
void YUV422ToRGB565(u8 *YUVBuff,u16 width,u16 height)
{
u16 x,y;
u32 RGBdata;
u32 cnt = 0;
u8 Y,U = 0,V = 0;
int R,G,B;
for(y = 0;y < height;y ++)
{
for(x = 0;x < width;x ++ )
{
Y = YUVBuff[cnt << 1];
if((cnt % 2) == 0)
{
U = YUVBuff[(cnt << 1) + 1];
V = YUVBuff[(cnt << 1) + 3];
}
R = Y + (1.370705 * (V - 128));
G = Y - (0.698001 * (V - 128)) - (0.337633 * (U-128));
B = Y + (1.732446 * (U - 128));
R = (R > 255) ? 255 : ((R < 0) ? 0 : R);
G = (G > 255) ? 255 : ((G < 0) ? 0 : G);
B = (B > 255) ? 255 : ((B < 0) ? 0 : B);
//RGBdata = (u32)(R << 16) | (G << 8) | B;
R >>= 3;
G >>= 2;
B >>= 3;
RGBdata = (R << 11) + (G << 5) + B;
LCD_DrawPoint(x,y,(u16)RGBdata);
//LCD_DrawPoint(x,y,RGB888toRGB565(RGBdata));
cnt ++;
}
}
}
u8 buff[10*1024*1024];
u8 image[10*1024*1024];
//主函数
int main(void)
{
OS_CPU_SR cpu_sr;
int result;
JPEG_INFO JpegInfo;
u32 p;
u32 i;
UART0_Init(DISABLE,115200); //初始化串口,失能中断接收,波特率115200
LCD_Init(); //初始化LCD
LED_Init(); //初始化LED
RunTimeInit();
JPEG_Init();
result = Explorer_Init(); //初始化资源管理器
if(result == 0)
{
lcd_printf("Disk Init OK!\n");
}
else
{
lcd_printf("Disk Init Error(%d)!\n",result);
}
p = (u32)buff;
if((u32)buff % 16)
{
p = (p / 16 + 1) * 16;
}
OpenPictureFile("0:/system/ui/ico/设置.jpg",(u8 *)p,10*1024*1024);
result = JPEG_DecodeOneFrame(p,(u32)image,31654, &JpegInfo);
uart_printf("decode jpeg file %d\r\n",result);
if(result == 0)
{
YUV422ToRGB565(image,JpegInfo.Width,JpegInfo.Height);
}