Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4239935
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-07-05 21:24:13

工程代码:     7SD读取bmp文件显示.rar  


参考资料:http://blog.ednchina.com/nthq2004/1175628/message.aspx
          rgb24 位 转为16位 http://blog.163.com/wanghengzhi@126/blog/static/168853652201158114745198/

描述:

    从SD中读取 bmp文件,将bmp文件显示在 LCD上
   
    没有时间 继续修改代码, 先这么着吧
  1. #ifndef __BMP_H
  2. #define __BMP_H

  3. #include "stm32f10x.h"
  4. #include "..\FATS\ff.h"            /* FatFs configurations and declarations */
  5. #include "..\FATS\diskio.h"        /* Declarations of low level disk I/O functions */
  6. #include "lcd.h"

  7. void sd_show_bmp(void);//从SD卡中读取bmp文件,显示在LCD上

  8. #endif

  1. #include "bmp.h"

  2. void sd_show_bmp(void)
  3. {
  4.     FATFS fs; //fat文件系统
  5.     FRESULT res;//返回的错误值
  6.     FIL file; //文件
  7.     UINT br; //读文件,具体读写的个数
  8.     u16 bitcount,width,height; //bitcount=图像位数取值 24位
  9.                                 //width height 宽度*高度 128*160
  10.     u16 PixelColor;    // RGB565 颜色值
  11.     u8 picH,picL; //颜色高低 8位
  12.     u8 bmp_data[54]; //bmp头信息54
  13.     u32    BytePerRow;//每行像素点 占用点数
  14.     u32 row,col,pixeloffset; //pixeloffset 数据位像素偏移
  15.     
  16.     res = f_mount(0, &fs);//打开文件,不存在则创建,可读 可写
  17.     res = f_open(&file, "test.bmp", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
  18.     res = f_read(&file, bmp_data, 0x36, &br); //读取文件头信息
  19.         
  20.     if(bmp_data[0]==0x42 && bmp_data[1]==0x4D)//文件格式标记为BM
  21.     {
  22.         width = bmp_data[18] (bmp_data[19]<<8) (bmp_data[20]<<16) (bmp_data[21]<<24);//图像宽度128
  23.         height = bmp_data[22] (bmp_data[23]<<8) (bmp_data[24]<<16) (bmp_data[25]<<24);//图像高度160
  24.         
  25.         ////每行像素占用字节数,因为4字节对齐,需要专门计算
  26.         BytePerRow = (u32)((u32)((width * 3 3 ) / 4) * 4) ;//每行像素点占用字节数
  27.         
  28.         if(width ==128 && height == 160) //假如是 128*160 像素点图片
  29.         {
  30.             bitcount = bmp_data[28] (bmp_data[29]<<8); //图像位数取值 24位
  31.             if(bitcount ==24) //是24位真彩色
  32.             {
  33.                  for(row = 159; row > 0; row--) //160 一行一行倒序扫描
  34.                 {
  35.                     for(col = 0; col < 128; col ) // 一行128个像素扫描
  36.                     {                 //24位真彩色没有彩色表
  37.                                       //54 bfoffbits 就是文件开始到图数据开始之间的偏移
  38.                         pixeloffset = 54 (height-row)*BytePerRow 3*col;
  39.                         res = f_lseek(&file,pixeloffset);
  40.                         res = f_read(&file, bmp_data, 3, &br); //在测试命令中,固定读取24位真彩色图,因此一次读取3个字节
  41.                         PixelColor = (bmp_data[2] & 0xF8) << 8 | (bmp_data[1] & 0xFC) << 3 | (bmp_data[0] & 0xF8) >> 3;
  42.                          //24位RGB格式转换为16位的 R:G:B = 5:6:5格式
  43.                          // r=userbuf[2] g = userbuf[1] b= userbuf[0]
  44.                         picL = (u8)PixelColor;     //颜色 低8位
  45.                         picH = (u8)(PixelColor >> 8); //颜色 高8位
  46.                         DrawPoint_xy(col,row,picH,picL);//打印 像素点
  47.                     }
  48.                 }         
  49.             }
  50.         }
  51.     }
  52.                      
  53.     f_close(&file);
  54.     f_mount(0, 0);    
  55. }





      






阅读(2354) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~