Use Libjpeg to compress yuv420 to JPG (memory mode)

Source: Internet
Author: User
#include <Windows.h> #include <stdio.h> extern "C" {#include <jpeglib.h>} #define WIDTH 352 #d Efine HEIGHT #define QUALITY #define Buffer_szie (width*height*2)/* The following declarations and 5 functions are JPEG related * functions used by put_jpeg_grey_memory and put_jpeg_yuv420p_memory * * typedef struct {struct JP
    Eg_destination_mgr Pub;
    Joctet *buf;
    size_t bufsize;
size_t Jpegsize;
  
} mem_destination_mgr;
  
  
typedef mem_destination_mgr *MEM_DEST_PTR;
    METHODDEF (void) init_destination (j_compress_ptr cinfo) {mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
    Dest->pub.next_output_byte = dest->buf;
    Dest->pub.free_in_buffer = dest->bufsize;
dest->jpegsize = 0; 
    } METHODDEF (Boolean) Empty_output_buffer (J_compress_ptr cinfo) {mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
    Dest->pub.next_output_byte = dest->buf;
  
    Dest->pub.free_in_buffer = dest->bufsize; RetuRN FALSE;
    } METHODDEF (void) term_destination (j_compress_ptr cinfo) {mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
Dest->jpegsize = dest->bufsize-dest->pub.free_in_buffer;
  
    Static GLOBAL (void) jpeg_mem_dest (j_compress_ptr cinfo, joctet* buf, size_t bufsize) {mem_dest_ptr dest; if (cinfo->dest = = NULL) {cinfo->dest = (struct jpeg_destination_mgr *) (*cinfo->mem->al
    Loc_small) ((j_common_ptr) cinfo, jpool_permanent, sizeof (mem_destination_mgr));
  
    } dest = (mem_dest_ptr) cinfo->dest;
    Dest->pub.init_destination = init_destination;
    Dest->pub.empty_output_buffer = Empty_output_buffer;
  
    Dest->pub.term_destination = term_destination;
    Dest->buf = BUF;
    Dest->bufsize = bufsize;
dest->jpegsize = 0;
    static GLOBAL (int) jpeg_mem_size (j_compress_ptr cinfo) {mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
Return dest->jpegsize;}/* Put_jpeg_yuv420p_memory converts an input image in the yuv420p format to a JPEG image and puts * it in a
 Memory buffer.
 * Inputs: *-Input_image is the image in yuv420p format. *-width and height are the dimensions of the image * Output: *-Dest_image is a pointer to the JPEG image buffer * R
                                   Eturns buffer size of JPEG image */static int put_jpeg_yuv420p_memory (unsigned char *dest_image,
  
    unsigned char *input_image, int width, int height) {int I, j, Jpeg_image_size; Jsamprow y[16],cb[16],cr[16]; Y[2][5] = Color sample of row 2 and pixel column 5;
    (One plane) Jsamparray Data[3];
    T[0][2][5] = Color Sample 0 of row 2 and column 5 struct jpeg_compress_struct cinfo;
  
    struct Jpeg_error_mgr jerr;
    Data[0] = y;
    DATA[1] = CB;
  
    DATA[2] = CR;  Cinfo.err = Jpeg_std_error (&jerr);
    Errors get written to stderr jpeg_create_compress (&cinfo); Cinfo.image_width = width;
    Cinfo.image_height = height;
    Cinfo.input_components = 3;
  
    Jpeg_set_defaults (&cinfo);
  
    Jpeg_set_colorspace (&cinfo, JCS_YCBCR);                  cinfo.raw_data_in = TRUE;       Supply downsampled Data cinfo.do_fancy_downsampling = FALSE;
    Fix segfaulst with v7 cinfo.comp_info[0].h_samp_factor = 2;
    Cinfo.comp_info[0].v_samp_factor = 2;
    Cinfo.comp_info[1].h_samp_factor = 1;
    Cinfo.comp_info[1].v_samp_factor = 1;
    Cinfo.comp_info[2].h_samp_factor = 1;
  
    Cinfo.comp_info[2].v_samp_factor = 1;
    Jpeg_set_quality (&cinfo, quality, TRUE);
  
    Cinfo.dct_method = Jdct_fastest;    Jpeg_mem_dest (&cinfo, Dest_image, Buffer_szie);
  
    Data written to Mem jpeg_start_compress (&cinfo, TRUE); for (j = 0; j < height; j = +) {for (i = 0; i < i++) {Y[i] = input_image + Width * (i +
            j); if (i%2 = = 0) {CB[I/2] = input_image + width * HeiGht + WIDTH/2 * ((i + j)/2);
            CR[I/2] = input_image + Width * height + width * HEIGHT/4 + width/2 * ((i + j)/2);
    } jpeg_write_raw_data (&cinfo, data, 16);
    } jpeg_finish_compress (&cinfo);
    Jpeg_image_size = Jpeg_mem_size (&cinfo);
      
    Jpeg_destroy_compress (&cinfo);
return jpeg_image_size;
    int main (int argc, TCHAR * argv[], TCHAR * envp[]) {HANDLE fyuv,fyuvjpg;
    BYTE *psrc, *PDST;
    LONG lsize = 0;
    DWORD readsize;
  
    DWORD writesize;
    PSRC = new Byte[buffer_szie];
    FYUV = CreateFile (L "Cif.yuv", Generic_all, 0, NULL, open_existing, 0, NULL);
  
    ReadFile (FYUV, PSRC, Buffer_szie, &readsize,null);
    PDST = new Byte[buffer_szie];
    lsize = Put_jpeg_yuv420p_memory (pdst,psrc, WIDTH, HEIGHT);
    Fyuvjpg = CreateFile (L "cif_yuv.jpg", generic_write, 0, NULL, create_always, 0, NULL);
  
    WriteFile (Fyuvjpg, PDST, Lsize, &writesize, NULL); ClosehaNdle (FYUV);
    CloseHandle (fyuvjpg);
return 0; }

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.