1. 程式人生 > >嵌入式Linux截圖工具gsnap移植與分析【轉】

嵌入式Linux截圖工具gsnap移植與分析【轉】

ram creat 嵌入式linux mbed 生活 rip 改變 因此 sig

轉自:http://blog.csdn.net/lu_embedded/article/details/53934184

  由於 Linux 系統的 FrameBuffer 機制,把屏幕上的每個點映射成一段線性內存空間,這樣,程序就可以通過改變這段內存的值來改變屏幕上某一點的顏色。如果我們想把當前的顯示內容保存起來,可能會想到如下命令:

# cat /dev/fb0 > fb_data.raw
  • 1

  反過來,可以將這些數據回顯到 framebuffer 中:

# cat fb_data.raw > /dev/fb0
  • 1

  使用 clear 命令清除,可以恢復正常。
  但是,用這用方法保存起來的數據是原始數據,只有專用軟件才能打開,並且大小固定(如:8MB)。基於這些原因,我們找到一個不錯的工具——gsnap,這個工具可以將 framebuffer 的數據保存為圖片(png或jpeg格式)。下面我們介紹一下移植過程。
  這裏的移植很簡單,因為源文件只有 gsnap.c,因此我們只需用相應平臺的編譯工具鏈進行編譯鏈接即可。命令如下:

# $(CC) gsnap.c -ljpeg -lpng -o gsnap
  • 1

  顯然,gsnap 需要用到 libjpeg 和 libpng 兩個庫。那麽編譯成功與否就跟這兩個庫有關了。如果你的目標平臺還沒有這些依賴庫,那麽就有必要下載相關源碼進行編譯安裝,步驟遵循 configure、make、make install 三部曲。
  由於我的目標平臺已經包含 libjpeg 和 libpng,於是我嘗試用上述命令進行編譯,提示缺少頭文件,所以編譯不成功。然後我將 libjpeg 和 libpng 源碼包中的頭文件抽取出來,添加到 /usr/include。發現仍然缺少頭文件,如下:

  • libpng 方面——找不到 pnglibconf.h,經檢查發現將 scripts/pnglibconf.h.prebuilt 另存為
    pnglibconf.h,並添加到 /usr/include 即可。
  • libjpeg 方面——找不到 jconfig.h,經檢查發現將 jconfig.txt 另存為 jconfig.h,並添加到
    /usr/include 即可。

不用擔心,如果你遵循三部曲來安裝這些庫,上面的 pnglibconf.h 和 jconfig.h 都會在編譯的過程中生成。
  一旦編譯成功,我們就可以運行 gsnap 來截取屏幕畫面了。gsnap 的使用也很簡單,格式為:

    gsnap <jpeg|png file> <framebuffer dev>
  • 1

  例如:

# ./gsnap test.png /dev/fb0
  • 1

  我這裏用的是 i.mx6q 的 yocto 1.5.3 系統,截圖 test.png 如下:

技術分享圖片

  以下是 gsnap.c 的源代碼:

/*
 * File:    gsnap.c
 * Author:  Li XianJing <[email protected]>
 * Brief:   snap the linux mobile device screen.
 *
 * Copyright (c) 2009  Li XianJing <[email protected]>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

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

#include <png.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <jpeglib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/fb.h>
#include <linux/kd.h>

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))

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 | PROT_WRITE, MAP_SHARED, fb->fd, 0);

    if (fb->bits == MAP_FAILED)
        goto fail;

    printf("---------------framebuffer---------------\n");
    printf("%s: \n  width : %8d\n  height: %8d\n  bpp   : %8d\n  r(%2d, %2d)\n  g(%2d, %2d)\n  b(%2d, %2d)\n",
        fbfilename, fb_width(fb), fb_height(fb), fb_bpp(fb), 
        fb->vi.red.offset, fb->vi.red.length,
        fb->vi.green.offset, fb->vi.green.length,
        fb->vi.blue.offset, fb->vi.blue.length);
    printf("-----------------------------------------\n");

    set_pixel_unpacker(fb);

    return 0;

fail:
    printf("%s is not a framebuffer.\n", fbfilename);
    close(fb->fd);

    return -1;
}

static void fb_close(FBInfo* fb)
{
    munmap(fb->bits, fb_size(fb));
    close(fb->fd);

    return;
}

static int snap2jpg(const char * filename, int quality, FBInfo* fb)
{
    int row_stride = 0; 
    FILE * outfile = NULL;
    JSAMPROW row_pointer[1] = {0};
    struct jpeg_error_mgr jerr;
    struct jpeg_compress_struct cinfo;

    memset(&jerr, 0x00, sizeof(jerr));
    memset(&cinfo, 0x00, sizeof(cinfo));

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);

    if ((outfile = fopen(filename, "wb+")) == NULL) 
    {
        fprintf(stderr, "can‘t open %s\n", filename);

        return -1;
    }

    jpeg_stdio_dest(&cinfo, outfile);
    cinfo.image_width = fb_width(fb);
    cinfo.image_height = fb_height(fb);
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, quality, TRUE);
    jpeg_start_compress(&cinfo, TRUE);

    row_stride = fb_width(fb) * 2;
    JSAMPLE* image_buffer = malloc(3 * fb_width(fb));

    while (cinfo.next_scanline < cinfo.image_height) 
    {
        int i = 0;
        int offset = 0;
        unsigned char* line = fb->bits + cinfo.next_scanline * fb_width(fb) * fb_bpp(fb);

        for(i = 0; i < fb_width(fb); i++, offset += 3, line += fb_bpp(fb))
        {
            fb->unpack(fb, line, image_buffer+offset, image_buffer + offset + 1, image_buffer + offset + 2);
        }

        row_pointer[0] = image_buffer;
        (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }

    jpeg_finish_compress(&cinfo);
    fclose(outfile);

    jpeg_destroy_compress(&cinfo);

    return 0;
}

//Ref: http://blog.chinaunix.net/space.php?uid=15059847&do=blog&cuid=2040565
static int snap2png(const char * filename, int quality, FBInfo* fb)
{
    FILE *outfile;
    if ((outfile = fopen(filename, "wb+")) == NULL)
    {
        fprintf(stderr, "can‘t open %s\n", filename);
        return -1;
    }

    /* prepare the standard PNG structures */
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0);

    png_infop info_ptr = png_create_info_struct(png_ptr);

    /* setjmp() must be called in every function that calls a PNG-reading libpng function */
    if (setjmp(png_jmpbuf(png_ptr)))
    {
        png_destroy_write_struct(&png_ptr, &info_ptr);
        fclose(outfile);
        return -1;
    }

    /* initialize the png structure */
    png_init_io(png_ptr, outfile);

    //
    int width = 0;
    int height = 0;
    int bit_depth = 8;
    int color_type = PNG_COLOR_TYPE_RGB;
    int interlace = 0;
    width = fb_width(fb);
    height = fb_height(fb);

    png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
                    (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
                    PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

    /* write the file header information */
    png_write_info(png_ptr, info_ptr);

    png_bytep row_pointers[height];
    png_byte* image_buffer = malloc(3 * width);

    int i = 0;
    int j = 0;
    unsigned char* line = NULL;
    for( ; i < height; i++ )
    {
        line = (char*)fb->bits + i * width * fb_bpp(fb);
        for(j = 0; j < width; j++, line += fb_bpp(fb))
        {
            int offset = j * 3;
            fb->unpack(fb, line, image_buffer+offset, image_buffer+offset+1, image_buffer+offset+2);
        }
        row_pointers[i] = image_buffer;
        png_write_rows(png_ptr, &row_pointers[i], 1);
    }

    png_destroy_write_struct(&png_ptr, &info_ptr);

    fclose(outfile);

    return 0;

}

int main(int argc, char* argv[])
{
    FBInfo fb;
    const char* filename   = NULL;
    const char* fbfilename = NULL;

    if(argc != 3)
    {
        printf("\nUsage: %s [jpeg|png file] [framebuffer dev]\n", argv[0]);
        printf("Example: %s fb.jpg /dev/fb0\n", argv[0]);
        printf("-----------------------------------------\n");
        printf("Powered by broncho(www.broncho.cn)\n\n");

        return 0;
    }

    filename   = argv[1];
    fbfilename = argv[2];

    memset(&fb, 0x00, sizeof(fb));
    if (fb_open(&fb, fbfilename) == 0)
    {
        if(strstr(filename, ".png") != NULL)
        {
            snap2png(filename, 100, &fb);
        }
        else
        {
            snap2jpg(filename, 100, &fb);
        }
        fb_close(&fb);
    }

    return 0;
}

嵌入式Linux截圖工具gsnap移植與分析【轉】