您的位置:首页 > 编程语言 > C语言/C++

C语言实现的在framebuff上面显示图片

2013-08-05 18:11 309 查看
/**************************************************

//初始化framebuffer,和在framebuffer上面显示图片数据

*************************************************/

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <linux/fb.h>

#include <errno.h>

#include <sys/mman.h>

#include <stdlib.h>

#include <string.h>

#include <sys/ioctl.h>

#include <linux/input.h>   

#include <time.h>

#include <stdlib.h>

//framebuffer

char *fb_addr;

int screen_fbd = 0;

struct fb_fix_screeninfo fb_fix;//这两个用于获取framebuffer相关信息的

struct fb_var_screeninfo fb_var;

unsigned fb_size; //buffer的长度

char *env = NULL;//fb的路径

//初始化framebuffer函数

int initFramebuffer(void)

{

        if(!(env = getenv("FRAMEBUFFER"))) //getenv()用来取得参数envvar环境变量的内容,如果没有这样的变量,默认framebuffer路径是下面的

        {

        env = "/dev/fb0";

        }

        

        screen_fbd = open(env, O_RDWR);       //以可读可写的方式打fb0       

        if(screen_fbd < 0) 

        {

                return 0;

        }

         //ioctl:控制I/O设备 ,提供了一种获得设备信息和向设备发送控制参数的手段       

        if(ioctl(screen_fbd, FBIOGET_FSCREENINFO, &fb_fix) == -1) 

        {//返回与Framebuffer有关的固定的信息,比如图形硬件上实际的帧缓存空间的大小、

               close(screen_fbd);

               return 0;

        }        

        if(ioctl(screen_fbd, FBIOGET_VSCREENINFO, &fb_var) == -1) 

        {//而后者返回的是与Framebuffer有关的可变信息,可变的信息就是指Framebuffer的长度、宽度以及颜色深度等信息

              close(screen_fbd);

              return 0;

        } //应该是位深度吧,用几位表示一个像素点

        fb_size = fb_var.yres * fb_fix.line_length;        

        fb_addr = (char *)mmap(NULL, fb_size, PROT_READ|PROT_WRITE,MAP_SHARED, screen_fbd, 0);

       return 1;     

}

/*

写数据文件到LCD的freamebuffer中

参数1:解码后的图片数据的指针

参数2:要在LCD上显示的位置的X坐标

参数3:要在LCD上显示的位置的Y坐标

参数4:图片宽度

参数5:图片高度

*/

void write_buf_image(unsigned char *gimage,int x,int y,int width,int height)
{ short *buff = (short*)fb_addr;
short *buffer  = buff;
int i = 0,j = 0,a = 0;
for(j = 0; (j < height)&&(y+j)<272; j++ )
{
buff = (buffer+x)+480*(j+y);

for(i = 0; ((i < width)&&((x+i)<480)); i++)    
{
*buff++ = *(gimage+a+1)<<8|*(gimage+a);

a++;
a++;   
}
a=width*2*j;
}
}

/*

int main(void)

{
int i;
initFramebuffer();

unsigned char *image=(char*)malloc(480*272*2);
for(i=0; i<480*272*2; i++)
image[i] = 0xff;

write_buf_image(image,0,0,480,272);

return 0;

}

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: