Embedded Linux snipping Tool gsnap porting and analysis "Go"

Source: Internet
Author: User
Tags goto unpack

Transferred from: http://blog.csdn.net/lu_embedded/article/details/53934184

Copyright statement: Happy from sharing, happiness from life-sharing technology, to convey happiness. Reproduced article please indicate the source, thank you! http://blog.csdn.net/lu_embedded/article/details/53934184

Because of the FrameBuffer mechanism of the Linux system, each point on the screen is mapped into a linear memory space, so that the program can change the value of the memory to alter the color of a point on the screen. If we want to save the current display, we might think of the following command:

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

In turn, you can echo this data back into framebuffer:

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

With clear command cleanup, you can return to normal.
However, the data saved with this method is the original data, only the specialized software can open, and the size is fixed (for example: 8MB). For these reasons, we found a good tool--gsnap, which can save framebuffer data as a picture (PNG or JPEG format). Let's look at the porting process.
The porting here is simple, because the source file is only gsnap.c, so we just need to compile the link with the corresponding platform's compilation tool chain. The command is as follows:

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

Obviously, Gsnap needs to use two libraries of libjpeg and libpng. Then the success of the compilation is related to the two libraries. If your target platform does not yet have these dependent libraries, then it is necessary to download the relevant source code to compile and install, the steps follow the configure, make, make install trilogy.
Since my target platform already contains libjpeg and libpng, I tried to compile with the above command, prompting for missing header files, so the compilation was unsuccessful. Then I extracted the header files from the Libjpeg and libpng source packages and added them to the/usr/include. The discovery still lacks a header file, as follows:

    • Libpng Aspect--Cannot find pnglibconf.h, after inspection found scripts/pnglibconf.h.prebuilt Save As
      Pnglibconf.h, and add to/usr/include.
    • Libjpeg Aspect--jconfig.h not found, after inspection found Jconfig.txt Save as jconfig.h, and added to
      /usr/include can be.

Don't worry, if you follow the trilogy to install these libraries, the above pnglibconf.h and Jconfig.h will be generated during the compilation process.
Once the compilation is successful, we can run Gsnap to intercept the screen. Gsnap is also very simple to use, in the form of:

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

For example:

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

I use the i.mx6q yocto 1.5.3 system here, test.png as follows:

The following is the source code for GSNAP.C:

/* * FILE:GSNAP.C * author:li xianjing <[email protected]> * Brief:snap the Linux mobile device screen . * Copyright (c) Li xianjing <[email protected]> * * Licensed under the academic free License version 2. 1 * * This program was free software; Can redistribute it and/or modify * it under the terms of the GNU general public License as published by * the Ftware Foundation; Either version 2 of the License, or * (at your option) any later version. * * This program was distributed in the hope that it'll 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 has received a copy of the GNU general public License * along with this program; If not, write to the free software * Foundation, Inc., Temple Place, Suite, 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 ' 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;typedefstruct _fbinfo fbinfo;typedefInt (*unpackpixel) (fbinfo* FB,Unsignedchar* Pixel,Unsignedchar* R,Unsignedchar* G,Unsignedchar* b);struct _fbinfo{int FD; Unpackpixel unpack;UnsignedChar *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))Staticint fb_unpack_rgb565 (fbinfo* fb,Unsignedchar* Pixel,Unsignedchar* R,Unsignedchar* G,UnsignedChar* b) {UnsignedShort color = * (Unsignedshort*) pixel; *r = ((Color >>) &0xFF) <<3; *g = ((Color >>5) &0xFF) <<2; *b = (Color &0xFF) <<3;Return0;}Staticint Fb_unpack_rgb24 (fbinfo* fb,Unsignedchar* Pixel,Unsignedchar* R,Unsignedchar* G,Unsignedchar* b) {*r = pixel[fb->vi.red.offset>>3]; *g = pixel[fb->vi.green.offset>>3]; *b = pixel[fb->vi.blue.offset>>3];Return0;}Staticint Fb_unpack_argb32 (fbinfo* fb,Unsignedchar* Pixel,Unsignedchar* R,Unsignedchar* G,Unsignedchar* b) {*r = pixel[fb->vi.red.offset>>3]; *g = pixel[fb->vi.green.offset>>3]; *b = pixel[fb->vi.blue.offset>>3];Return0;}Staticint Fb_unpack_none (fbinfo* fb,Unsignedchar* Pixel,Unsignedchar* R,Unsignedchar* G,Unsignedchar* b) {*r = *g = *b =0;Return0;}Staticvoid Set_pixel_unpacker (fbinfo* fb) {if (FB_BPP (FB) = =2) {fb->unpack = fb_unpack_rgb565;}Elseif (FB_BPP (FB) = =3) {fb->unpack = fb_unpack_rgb24;}Elseif (FB_BPP (FB) = =4) {fb->unpack = Fb_unpack_argb32;}else {fb->unpack = Fb_unpack_none;printf"%s:not supported format.\n", __func__); }return;}Staticint Fb_open (fbinfo* fb,Constchar* fbfilename) {fb->fd = open (Fbfilename, O_RDWR);if (FB-&GT;FD <0) {fprintf (stderr,"Can ' t open%s\n", fbfilename);Return-1; }if (IOCTL (FB-&GT;FD, Fbioget_fscreeninfo, &fb->fi) <0)Goto fail;if (IOCTL (FB-&GT;FD, Fbioget_vscreeninfo, &fb->vi) <0)Goto fail; Fb->bits = Mmap (0, Fb_size (FB), Prot_read | Prot_write, map_shared, FB-&GT;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 (%2d,%2d) \ n", Fbfilename, Fb_width (FB), Fb_h Eight (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);Return0;fail:printf"%s is not a framebuffer.\n", fbfilename); Close (FB-&GT;FD);Return-1;}Staticvoid Fb_close (fbinfo* fb) {Munmap (fb->bits, Fb_size (FB)); close (FB-&GT;FD);return;}Staticint Snap2jpg (Constchar * 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,0x00sizeof (JERR));memset (&cinfo,0x00sizeof (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 =malloc3 * fb_width (FB));while (Cinfo.next_scanline < cinfo.image_height) {int i =0;int offset =0;Unsignedchar* 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);Return0;}ref:http://blog.chinaunix.net/space.php?uid=15059847&do=blog&cuid=2040565static int snap2png (const char * filename, int quality, fbinfo* fb) {FILE *outfile; (outfile = fopen (filename, "wb+") = = NULL) {fprintf (stderr, "CA N ' 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 is called in every function, 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) {p rintf ("\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 "); return 0; } filename = argv[1]; Fbfilename = argv[2]; memset (&AMP;FB, 0x00, sizeof (FB)); if (Fb_open (&AMP;FB, fbfilename) = = 0) {if (strstr (filename, ". png") = NULL) {snap2png (filename,, &AMP;FB);} else { Snap2jpg (filename, &AMP;FB); } fb_close (&AMP;FB); } return 0;}

Embedded Linux Tools Gsnap porting and parsing "go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.