yuv_to_jpeg

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 pointer
array to conform to this structure.  Pointers to JSAMPLE rows are of type JSAMPROW, 
and the pointer to the pointer array is of type JSAMPARRAY.
*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值