把从framebuffer抓出来的数据转换成jpg图像

来源:互联网 发布:淘宝刷流量 猎流 编辑:程序博客网 时间:2024/04/30 04:59
把从framebuffer抓出来的数据转换成为jpg图象
2008-07-10 14:15
//gcc -o grab grab.c -ljpeg

//The source code of grab.c
//cat /dev/fb/0 >tmp
//./grab -i tmp -o grab.jpg
#include <setjmp.h>
#include <stdio.h>
#include <getopt.h>


#include <jpeglib.h>
#define W 800
#define H 480
/*The name of this program.*/
const char *program_name;

/*
   Prints usage information for this program to STREAM,and exit the program with EXIT_CODE.
*/

void print_usage(FILE* stream,int exit_code)
{
        fprintf(stream,"Usage: %s options [inputfile outputfile...]\n",program_name);

        fprintf(stream,
                        " -h --help Display this usage information\n"
                        " -i --input filename Read from file\n"
                        " -o --output filename Write output to file\n"
                   );
        exit(exit_code);
}


void convert_line( char *src, char *dst, int line,int weight,int height );

int main(int argc,char **argv)
{

        int next_option;
        unsigned int image_height = 0;
        unsigned int image_width = 0;
        const char* const short_opti;
        const struct option long_options[] = {
                {"help", 0, NULL, 'h'},
                {"input",1, NULL, 'i'},
                {"output",1, NULL, 'o'},
                {NULL, 0, NULL,0      }
        };

        const char *output_filename = NULL;
        char *input_filename = NULL;
        program_name = argv[0];
        do{
                next_option = getopt_long(argc,argv,short_options,long_options,NULL);
                switch(next_option)
                {
                        case 'h':
                                print_usage(stdout,0);
                        case 'o':
                                output_filename = optarg;
                                break;
                        case 'i':
                                input_filename = optarg;
                                break;
                        case '?':
                                print_usage(stdout,1);
                        case -1:
                                break;
                        default:
                                abort();
                }
        }
        while(next_option != -1);

        if(!output_filename){
                output_filename = "grab.jpg";
        }

        if(!input_filename)
        {
                input_filename = "tmp";
        }


        printf("outputfile = %s,inputfile = %s\n",output_filename,input_filename);
        struct jpeg_compress_struct cinfo;
        struct jpeg_error_mgr jerr;

        JSAMPLE *buffer;
        JSAMPLE *image_buffer;
        buffer = malloc(W*H*3);
        image_buffer = malloc(W*3);


        FILE *in;
        int ret = 0;
        in = fopen(input_filename,"r");
        if(!in){
                printf("please provide the rgb source\n");
                exit(0);
        }

        ret = fread(buffer, 1,W*H*2, in );

        if( ret == 0 ){
                printf("read image buffer error\n");
                exit(0);
        }

        int height = H;
        int width = W;

        FILE *outfile;
        JSAMPROW row_pointer[1];//points to JSAMPLE row[s]

        //setp 1:allocate and initialize JPEG compression object
        cinfo.err = jpeg_std_error(&jerr);
        jpeg_create_compress(&cinfo);

        //step 2:specify data destination
        //char *filename="grab1.jpg";
        if((outfile = fopen(output_filename,"wb")) == NULL){
                printf("cannot open file to write\n");
                exit(0);
        }
        jpeg_stdio_dest( &cinfo, outfile );

        //step 3:set parameters for compression
        cinfo.image_width = width;
        cinfo.image_height = height;
        cinfo.input_components = 3;
        cinfo.in_color_space = JCS_RGB;

        jpeg_set_defaults(&cinfo);

        jpeg_set_quality(&cinfo, 100, TRUE );

        //step 4:start compresor
        jpeg_start_compress(&cinfo, TRUE);

        //step 5:
        while(cinfo.next_scanline < cinfo.image_height ){
                convert_line( buffer,image_buffer,cinfo.next_scanline,height,width);
                //printf("%d======end",cinfo.next_scanline);
                row_pointer[0] = image_buffer;
                (void)jpeg_write_scanlines( &cinfo, row_pointer, 1);
        }
        jpeg_finish_compress(&cinfo);
        fclose( outfile );
        jpeg_destroy_compress(&cinfo);
        free(buffer);
        free(image_buffer);
}

void convert_line(char* src,char* dst,int line,int width,int height){
        int NUM1 = W*2;
        int NUM2 = W*3;
        char *p = src + line*NUM1;
        int i = 0;
        while( i < NUM2 ){
                dst[i] = (p[1]&0xf8 );
                dst[i+1] = ((p[1]&0x7)<<5) | ((p[0] & 0xe0)>>3);
                dst[i+2] = (p[0]&0x1f)<<3;
                i += 3;
                p += 2;
        }
}

原创粉丝点击