S3C6410 FrameBuffer编程(六) --- 利用FrameBuffer显示图片,并实现左右、上下、180度翻转

[html]  view plain copy
  1. /*********wzk_fb.h*************/  
  2. /********************************  
  3. /*  定义了屏幕信息结构体 */  
  4.   
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7. #include <unistd.h>  
  8. #include <linux/fb.h>  
  9. #include <fcntl.h>  
  10. #include <sys/mman.h>  
  11.   
  12. int fb;  
  13.   
  14. struct screen_info{  
  15.   
  16.         unsigned int smem_size;  
  17.         unsigned int line_size;  
  18.         unsigned int xres;  
  19.         unsigned int yres;  
  20.         unsigned int bits_per_pixel;  
  21.       
  22. };  
  23.   
  24. struct screen_info h43_info;  
[html]  view plain copy
  1. <pre name="code" class="html">/*************** fb.h ******************/  
  2. /***************************************/  
  3. /***************************************/  
  4. /**** 操作FrameBuffer的相关函数 ****/  
  5.   
  6.   
  7. #include <math.h>  
  8.   
  9. #include "wzk_fb.h"  
  10. //#include "wzk_jpeg.h"  
  11.   
  12. int open_fb(void)  
  13. {  
  14.     fb = open ("/dev/fb0",O_RDWR);    
  15.     if (fb < 0)   
  16.     {     
  17.         printf("Error : Can not open framebuffer device\n");     
  18.         return -1;    
  19.     }  
  20.   
  21.     printf("open framebuffer device sccuess!\n");  
  22.   
  23.     return 0;       
  24. }  
  25.   
  26. void close_fb(void)  
  27. {  
  28.     close(fb);  
  29.       
  30.     return;  
  31. }  
  32.   
  33. int get_screen_info(void)  
  34. {  
  35.     struct fb_var_screeninfo vinfo;  
  36.     struct fb_fix_screeninfo finfo;  
  37.   
  38.     if (ioctl(fb,FBIOGET_FSCREENINFO,&finfo))   
  39.         {     
  40.             printf("Error reading fixed information\n");     
  41.             return -1;  
  42.         }  
  43.      
  44.         if (ioctl(fb,FBIOGET_VSCREENINFO,&vinfo))   
  45.         {     
  46.             printf("Error reading variable information\n");     
  47.             return -1;  
  48.         }  
  49.   
  50.     h43_info.smem_size = finfo.smem_len;  
  51.     h43_info.line_size = finfo.line_length;  
  52.     h43_info.xres = vinfo.xres;  
  53.     h43_info.yres = vinfo.yres;  
  54.     h43_info.bits_per_pixel = vinfo.bits_per_pixel;  
  55.     
  56.         printf("The mem is :%d\n",h43_info.smem_size);    
  57.         printf("The line_length is :%d\n",h43_info.line_size);    
  58.         printf("The xres is :%d\n",h43_info.xres);    
  59.         printf("The yres is :%d\n",h43_info.yres);    
  60.         printf("bits_per_pixel is :%d\n",h43_info.bits_per_pixel);    
  61.   
  62.     return 0;  
  63. }  
  64.   
  65.   
  66. int clear_screen(void)  
  67. {  
  68.     char *fpb;  
  69.     size_t screen_size;  
  70.     long int location;  
  71.     int x,y;  
  72.     int i;  
  73.   
  74.     x = 0;  
  75.     y = 0;  
  76.       
  77.       
  78.     screen_size = h43_info.smem_size/2;  
  79.     location = h43_info.line_size * y + h43_info.bits_per_pixel / 8 * x;  
  80.   
  81.     fpb = (char *)mmap(NULL,screen_size,PROT_READ | PROT_WRITE,MAP_SHARED,fb,0);  
  82.     if((int)fpb < 0)  
  83.     {  
  84.         printf("mmap fail!\n");  
  85.         return -1;  
  86.     }  
  87.   
  88.     for(i = 0; i < h43_info.xres * h43_info.yres; i++)  
  89.     {  
  90.         *(fpb + location) = 0x00;  
  91.         *(fpb + location + 1) = 0x00;  
  92.         location += 2;  
  93.     }         
  94.     munmap(fpb,screen_size);      
  95.       
  96. }  
  97.   
  98.   
  99. int draw_point(int x,int y,unsigned char rg,unsigned gb)  
  100. {  
  101.     char *fpb;  
  102.     size_t screen_size;  
  103.     long int location;  
  104.       
  105.     screen_size = h43_info.smem_size/2;  
  106.     location = h43_info.line_size * y + h43_info.bits_per_pixel / 8 * x;  
  107.   
  108.     fpb = (char *)mmap(NULL,screen_size,PROT_READ | PROT_WRITE,MAP_SHARED,fb,0);  
  109.     if((int)fpb < 0)  
  110.     {  
  111.         printf("mmap fail!\n");  
  112.         return -1;  
  113.     }  
  114.   
  115.     *(fpb + location) = gb;  
  116.     *(fpb + location + 1) = rg;  
  117.   
  118.     munmap(fpb,screen_size);  
  119.   
  120.     return 0;     
  121. }  
  122.   
  123. int draw_image(unsigned short *buf)  
  124. {  
  125.     char *fpb;  
  126.     size_t screen_size;  
  127.     long int location;  
  128.     int i;  
  129.       
  130.     location = 0;     
  131.   
  132.     screen_size = h43_info.smem_size/2;  
  133. //  location = h43_info.line_size * y + h43_info.bits_per_pixel / 8 * x;  
  134.   
  135.     fpb = (char *)mmap(NULL,screen_size,PROT_READ | PROT_WRITE,MAP_SHARED,fb,0);  
  136.     if((int)fpb < 0)  
  137.     {  
  138.         printf("mmap fail!\n");  
  139.         return -1;  
  140.     }  
  141.   
  142.     for(i = 0; i < 480 * 272; i++)  
  143.     {  
  144.         *(fpb + location) = (unsigned char)(buf[i] & 0x00ff);  
  145.         *(fpb + location + 1) = (unsigned char)((buf[i] >> 8) & 0x00ff);  
  146.         location += 2;  
  147.     }  
  148.   
  149.     munmap(fpb,screen_size);  
  150.   
  151.     return 0;     
  152. }  
  153. </pre><br>  
  154. <br>  
  155. <pre></pre>  
  156. <p><br>  
  157. </p>  
  158. <p><br>  
  159. </p>  
  160. <p></p>  
  161. <pre name="code" class="html">/**************************************************/  
  162. /**************** bmp.h **************************/  
  163. /*** 定义了bmp格式图片的相关结构体********/  
  164. typedef struct bmp_file{  
  165.   
  166.     unsigned short map_id; //type  
  167.     unsigned int file_size; //size  
  168.     unsigned int reserved;   //reserved,set 0  
  169.     unsigned int offset; //offset  
  170.   
  171. }__attribute__((packed)) BMPFILEHEADER;  
  172.   
  173. typedef struct bmp_info{  
  174.   
  175.     unsigned int cur_size;  
  176.     unsigned int width;  
  177.     unsigned int hight;  
  178.     unsigned short reserved;  
  179.     unsigned short bpp;  
  180.     unsigned int compression;  
  181.     unsigned int map_size;  
  182.       
  183.     unsigned int x_ppm;  
  184.     unsigned int y_ppm;  
  185.     unsigned int palette;  
  186.     unsigned int bitmapdata;  
  187. /*  
  188.     unsigned int R;  
  189.     unsigned int G;  
  190.     unsigned int B;  
  191.     unsigned int A;  
  192. */  
  193.   
  194. }__attribute__((packed))BMPINFOHEADER;  
  195.   
  196. typedef struct pixel{  
  197.   
  198.     unsigned char blue;  
  199.     unsigned char green;  
  200.     unsigned char red;  
  201. }__attribute__((packed))PIXEL;  
  202.   
  203. BMPFILEHEADER FileHead;  
  204. BMPINFOHEADER InfoHead;  
  205. //PIXEL pix;</pre><br>  
  206. <br>  
  207. <p></p>  
  208. <p></p><pre name="code" class="html">/******************** convert.h*********************/  
  209. /****************************************************/  
  210. /******* 实现屏幕的上下、左右、180度翻转 *******/  
  211.   
  212. #define BUF_SIZE 480*272  
  213. #define BUF_WIDTH 480  
  214. #define BUF_HEIGHT 272  
  215.   
  216. unsigned short  *rotate_180(unsigned short *buf)  
  217. {  
  218.     unsigned short tmp[BUF_SIZE];  
  219.     int i,j;  
  220.   
  221.     j = 0;  
  222.   
  223.     for(i = BUF_SIZE - 1; i >= 0; i--)  
  224.     {  
  225.         tmp[j] = buf[i];  
  226.         j++;  
  227.     }  
  228.   
  229.     for(i = 0;i < BUF_SIZE; i++)  
  230.     {  
  231.         buf[i] = tmp[i];  
  232.     }  
  233.   
  234.     return buf;  
  235. }  
  236.   
  237. unsigned short  *left_to_right(unsigned short *buf)  
  238. {  
  239.     unsigned short tmp[BUF_SIZE];  
  240.     int i,j,m;  
  241.     unsigned long int count;  
  242.   
  243.     count = 0;  
  244.     m = 0;  
  245.   
  246.     for(j = 1;j <= BUF_HEIGHT;j++)  
  247.     {  
  248.         count = j * BUF_WIDTH - 1;  
  249.   
  250.         for(i = 0; i < BUF_WIDTH; i++)  
  251.         {  
  252.             tmp[m] = buf[count];  
  253.             count --;  
  254.             m++;          
  255.         }  
  256.     }     
  257.   
  258.     for(i = 0; i < BUF_SIZE; i++)  
  259.     {  
  260.         buf[i] = tmp[i];          
  261.     }  
  262.   
  263.     return buf;  
  264. }  
  265.   
  266. unsigned short  *up_to_down(unsigned short *buf)  
  267. {  
  268.     unsigned short tmp[BUF_SIZE];  
  269.     int i,j,m;  
  270.     unsigned long int count;  
  271.   
  272.     m = 0;  
  273. //  count = BUF_SIZE - BUF_WIDTH;  
  274.   
  275.     for(j = 1;j <= BUF_HEIGHT;j++)  
  276.     {  
  277.         count = BUF_SIZE - j * BUF_WIDTH;  
  278.   
  279.         for(i = 0; i < BUF_WIDTH; i++)  
  280.         {  
  281.             tmp[count] = buf[m];  
  282.             count ++;  
  283.             m++;          
  284.         }  
  285.     }     
  286.   
  287.     for(i = 0; i < BUF_SIZE; i++)  
  288.     {  
  289.         buf[i] = tmp[i];          
  290.     }  
  291.   
  292.     return buf;  
  293. }</pre><br>  
  294. <br>  
  295. <p></p>  
  296. <p><br>  
  297. </p>  
  298. <p><br>  
  299. </p>  
  300. <p></p>  
  301. <pre name="code" class="html">/****** *****************bmp.c ******************************/  
  302. /********************************************/  
  303. /********* 实现图片的显示******************/  
  304. #include <stdio.h>  
  305. #include <stdlib.h>  
  306. #include <unistd.h>  
  307.   
  308. #include "bmp.h"  
  309. #include "fb.h"  
  310. #include "convert.h"  
  311.   
  312. unsigned short frame_buffer[480*272];  
  313.   
  314. unsigned short rgb24_2_rgb565(unsigned int r, unsigned int g, unsigned int b)  
  315. {  
  316.     unsigned short ret;  
  317.       
  318.     ret = (r << 8)&0xf800 | (g << 3)&0x07e0 | (b >> 3);  
  319.   
  320.     return ret;  
  321. }  
  322.   
  323. int init_fb(void)  
  324. {  
  325.         int ret;  
  326.         ret = open_fb();  
  327.         if(ret < 0)  
  328.         {  
  329.                 return -1;  
  330.         }   
  331.   
  332.         ret = get_screen_info();  
  333.         if(ret < 0)  
  334.         {  
  335.                 return -1;  
  336.         }  
  337.   
  338.         clear_screen();  
  339.   
  340.     return 0;  
  341.         //draw_point(100,100,0x3f,0x7e);          
  342.         //close_fb();  
  343. }  
  344.   
  345.   
  346. int main(int argc,char *argv[])  
  347. {  
  348.   
  349.     FILE *fp;  
  350.     int rc;  
  351.     int Count;  
  352.     long int BytesPerLine;    
  353. //  int x,y;  
  354.     int re;  
  355.   
  356.     re = init_fb();   
  357.     if(re < 0)  
  358.     {  
  359.         printf("init_fb error!\n");  
  360.         return 0;  
  361.     }  
  362.     Count = 0;    
  363. //  x = 0;  
  364. //  y = 0;  
  365.       
  366.     fp = fopen("./wzk.bmp","rb");  
  367.     if(fp == NULL)  
  368.     {  
  369.         goto CLOSE;  
  370.         return -1;  
  371.     }  
  372.   
  373.     rc = fread(&FileHead,sizeof(BMPFILEHEADER),1,fp);  
  374.     if(rc != 1)  
  375.     {  
  376.         printf("read file header error!\n");  
  377. //      fclose(fp);  
  378.         goto CLOSE;  
  379.           
  380.         return -2;  
  381.     }  
  382.   
  383.     printf("type = %4x\n",FileHead.map_id);  
  384. //  printf("file size:%d,reserved:%d\n",FileHead.file_size,FileHead.reserved);  
  385. /*  
  386.     if(memcmp(FileHead.map_id,"BM",2) != 0)  
  387.     {  
  388.         printf("it is not a bmp file \n");  
  389.         fclose(fp);  
  390.         return -3;  
  391.     }     
  392.   
  393. */  
  394.     rc = fread(&InfoHead,sizeof(BMPINFOHEADER),1,fp);  
  395.     if(rc != 1)  
  396.     {  
  397.         printf("read info header error!\n");  
  398.     //  fclose(fp);  
  399.         goto CLOSE;  
  400.         return -4;  
  401.     }  
  402.   
  403.     fseek(fp,FileHead.offset,SEEK_SET);  
  404.       
  405.     BytesPerLine = InfoHead.width * InfoHead.bpp/8;  
  406.   
  407.     printf("width:%d,bpp:%d\n",InfoHead.width,InfoHead.bpp);  
  408.   
  409.     while(!feof(fp))  
  410.     {  
  411.         PIXEL pix;  
  412.         unsigned short ret;  
  413.   
  414.         rc = fread(&pix,sizeof(PIXEL),1,fp);  
  415.         if(rc != 1)  
  416.         {  
  417.             printf("read bmp file error!\n");  
  418. //          fclose(fp);  
  419.             goto CLOSE;  
  420.             return -1;  
  421.         }  
  422.   
  423.         ret = rgb24_2_rgb565(pix.red,pix.green,pix.blue);  
  424. /*      draw_point(x,y,(unsigned char)((ret >> 8) & 0x00ff),(unsigned char)(ret & 0x00ff));  
  425.         x++;  
  426.         if(x == 480)  
  427.         {  
  428.             x = 0;  
  429.             y++;  
  430.             if(y == 272)  
  431.             {  
  432.                 goto CLOSE;  
  433.             }  
  434.         }  
  435. */                    
  436. //      printf("blue:%d,green:%d,red:%d\n",pix.blue,pix.green,pix.red);  
  437.   
  438.         frame_buffer[Count] = ret;  
  439.         Count++;  
  440.         if(Count == 480*272)  
  441.         {     
  442.             Count = 0;  
  443.             break;  
  444.         }             
  445.     }  
  446.   
  447.     draw_image(frame_buffer);             
  448.     while(1)  
  449.     {  
  450.         int tmp;  
  451.   
  452.         printf("1:rolate 180\n");  
  453.         printf("2:left to right\n");  
  454.         printf("3:up to down\n");  
  455.         printf("other:exit\n");  
  456.   
  457.         scanf("%d",&tmp);  
  458.               
  459.         if(tmp == 1)  
  460.         {  
  461.             draw_image(rotate_180(frame_buffer));  
  462.         }   
  463.         else if(tmp == 2)  
  464.         {  
  465.             draw_image(left_to_right(frame_buffer));      
  466.         }  
  467.         else if(tmp == 3)  
  468.         {  
  469.             draw_image(up_to_down(frame_buffer));  
  470.         }  
  471.         else  
  472.         {  
  473.             break;  
  474.         }  
  475.           
  476.     }     
  477.   
  478. CLOSE:  
  479.     fclose(fp);  
  480.     close(fb);  
  481. }</pre><br>  
  482. <img src="https://img-my.csdn.net/uploads/201210/17/1350459317_4282.png" alt=""><br>  
  483. <p></p>  
  484. <p><br>  
  485. </p>  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值