yuv_to_jpeg

来源:互联网 发布:恶棍天使知乎 编辑:程序博客网 时间:2024/06/06 05:51
int compress_yuyv_to_jpeg(struct vdIn *vd, unsigned char *buffer, int size, int quality){    //1. Allocate and initialize a JPEG compression object.*/        /*define a JPEG compression object*/    struct jpeg_compress_struct cinfo; /*the object is a "struct jpeg_compress_strucre*/    struct jpeg_error_mgr jerr;        /* a structure representing a JPEG error handler.*/    /*initialize the error handler structure, store a pointer to it into the JPEG object's "err" field, and then call jpeg_create_compress() to initialize the rest of the JPEG object.*/    cinfo.err = jpeg_std_error(&jerr);    jpeg_create_compress(&cinfo);        /*jpeg_create_compress allocates a small amount of memory,     so it could fail if you are out of memory.    In that case it will exit via the error handler;     that's why the error handler must be initialized first.*/        //2. Specify the destination for the compressed data    /*jpeg_stdio_dest (&cinfo, file);*/    dest_buffer(&cinfo, buffer, size, &written);//3. Set parameters for compression, including image size & colorspace.    cinfo.image_width = vd->width;  /* image width and height, in pixels */    cinfo.image_height = vd->height;    cinfo.input_components = 3;    /* # of color components per pixel */    cinfo.in_color_space = JCS_RGB;/* colorspace of input image */    jpeg_set_defaults(&cinfo);/* Make optional parameter settings here */    jpeg_set_quality(&cinfo, quality, TRUE);    //4. begin a compression cycle.     jpeg_start_compress(&cinfo, TRUE);    /* This will initialize internal state, allocate working storage,     and emit the first few bytes of the JPEG datastream header.*/        //5. write all the required image data     /*by calling jpeg_write_scanlines() one or more times. */    JSAMPROW row_pointer[1];/* pointer to a single row */    unsigned char *line_buffer, *yuyv;     int z;    static int written;    line_buffer = calloc(vd->width * 3, 1);/*width*3:physical row width in buffer */    yuyv = vd->framebuffer;        z = 0;    while(cinfo.next_scanline < vd->height) {/*while (scan lines remain to be written)*/        int x;        unsigned char *ptr = line_buffer;/*color space convert begin (from YUV to RGB)*/        for(x = 0; x < vd->width; x++) {            int r, g, b;            int y, u, v;            if(!z)                y = yuyv[0] << 8;            else                y = yuyv[2] << 8;            u = yuyv[1] - 128;            v = yuyv[3] - 128;            r = (y + (359 * v)) >> 8;            g = (y - (88 * u) - (183 * v)) >> 8;            b = (y + (454 * u)) >> 8;            *(ptr++) = (r > 255) ? 255 : ((r < 0) ? 0 : r);            *(ptr++) = (g > 255) ? 255 : ((g < 0) ? 0 : g);            *(ptr++) = (b > 255) ? 255 : ((b < 0) ? 0 : b);            if(z++) {                z = 0;                yuyv += 4;            }        }/*color space convert end*/        row_pointer[0] = line_buffer;        jpeg_write_scanlines(&cinfo, row_pointer, 1);    }// 6. complete the compression cycle    jpeg_finish_compress(&cinfo);    /*This step is ESSENTIAL to ensure that the last bufferload of data is     written to the data destination. jpeg_finish_compress() also releases    working memory associated with the JPEG object.*/        // 7. Release the JPEG compression object.    jpeg_destroy_compress(&cinfo);    free(line_buffer);    return (written);}// about data formats/*Pixels are stored by scanlines, with each scanline running from left to right.  The component values for each pixel are adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit RGB color.  Each scanline is an array of data type JSAMPLE --- which is typically "unsigned char", unless you've changed jmorecfg.h.  (You can also change the RGB pixel layout, say to B,G,R order,by modifying jmorecfg.h.  But see the restrictions listed in that file before doing so.)A 2-D array of pixels is formed by making a list of pointers to the starts of scanlines;so the scanlines need not be physically adjacent in memory.  Even if you process just one scanline at a time, you must make a one-element pointerarray to conform to this structure.  Pointers to JSAMPLE rows are of type JSAMPROW, and the pointer to the pointer array is of type JSAMPARRAY.*/

原创粉丝点击