资源简介

linux从framebuffer获取image源码

资源截图

代码片段和文件信息

/*
 * File:    gsnap.c
 * Author:  Li XianJing 
 * Brief:   snap the linux mobile device screen.
 *
 * Copyright (c) 2009-2011  Li XianJing 
 *
 */

/*
 * History:
 * ================================================================
 * 2009-08-20 Li XianJing  created
 * 2011-02-28 Li XianJing  suppport RGB888 framebuffer.
 * 2011-04-09 Li XianJing  merge figofuture‘s png output.
 *  ref: http://blog.chinaunix.net/space.php?uid=15059847&do=blog&cuid=2040565
 *
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

struct _FBInfo;
typedef struct _FBInfo FBInfo;
typedef int (*UnpackPixel)(FBInfo* fb unsigned char* pixel 
unsigned char* r unsigned char* g unsigned char* b);

struct _FBInfo
{
int fd;
UnpackPixel unpack;
unsigned char *bits;
struct fb_fix_screeninfo fi;
struct fb_var_screeninfo vi;
};

#define fb_width(fb)  ((fb)->vi.xres)
#define fb_height(fb) ((fb)->vi.yres)
#define fb_bpp(fb)    ((fb)->vi.bits_per_pixel>>3)
#define fb_size(fb)   ((fb)->vi.xres * (fb)->vi.yres * fb_bpp(fb))
#define fb_line_length(fb) ((fb)->fi.line_length)

static int fb_unpack_rgb565(FBInfo* fb unsigned char* pixel 
unsigned char* r unsigned char* g unsigned char* b)
{
unsigned short color = *(unsigned short*)pixel;

*r = ((color >> 11) & 0xff) << 3;
*g = ((color >> 5) & 0xff)  << 2;
*b = (color & 0xff )<< 3;

return 0;
}

static int fb_unpack_rgb24(FBInfo* fb unsigned char* pixel 
unsigned char* r unsigned char* g unsigned char* b)
{
*r = pixel[fb->vi.red.offset>>3];
*g = pixel[fb->vi.green.offset>>3];
*b = pixel[fb->vi.blue.offset>>3];

return 0;
}

static int fb_unpack_argb32(FBInfo* fb unsigned char* pixel 
unsigned char* r unsigned char* g unsigned char* b)
{
*r = pixel[fb->vi.red.offset>>3];
*g = pixel[fb->vi.green.offset>>3];
*b = pixel[fb->vi.blue.offset>>3];

return 0;
}

static int fb_unpack_none(FBInfo* fb unsigned char* pixel 
unsigned char* r unsigned char* g unsigned char* b)
{
*r = *g = *b = 0;

return 0;
}

static void set_pixel_unpacker(FBInfo* fb)
{
if(fb_bpp(fb) == 2)
{
fb->unpack = fb_unpack_rgb565;
}
else if(fb_bpp(fb) == 3)
{
fb->unpack = fb_unpack_rgb24;
}
else if(fb_bpp(fb) == 4)
{
fb->unpack = fb_unpack_argb32;
}
else
{
fb->unpack = fb_unpack_none;
printf(“%s: not supported format.\n“ __func__);
}

return;
}

static int fb_open(FBInfo* fb const char* fbfilename)
{
fb->fd = open(fbfilename O_RDWR);

if (fb->fd < 0)
{
fprintf(stderr “can‘t open %s\n“ fbfilename);

return -1;
}

if (ioctl(fb->fd FBIOGET_FSCREENINFO &fb->fi) < 0)
goto fail;

if (ioctl(fb->fd FBIOGET_VSCREENINFO &fb->vi) < 0)
goto fail;

fb->bits = mmap(0 fb_size(fb) PROT_READ | PR

评论

共有 条评论