struct jpeg_compress_struct * RGB_To_JPEG_init(int width, int height, int quality, int decimation) { struct jmf_error_mgr *jerr; struct jpeg_compress_struct *cinfo; jmf_destination_mgr *jmf_dest; jmf_dest_data *clientData; clientData = (jmf_dest_data*) malloc(sizeof(jmf_dest_data)); /* Alloc 1 */ clientData->tmp_data = (char *) malloc(JMF_OUTPUT_BUF_SIZE); /* Alloc 2 */ //clientData->data = (char *) outData; //clientData->length = 0; /* Step 1: allocate and initialize JPEG compression object */ cinfo = (struct jpeg_compress_struct *) malloc(sizeof(struct jpeg_compress_struct));/* Alloc 3 */ /* Initialize error parameters */ jerr = (struct jmf_error_mgr *) malloc(sizeof(struct jmf_error_mgr)); /* Alloc 4 */ clientData->jerr = (void *) jerr; cinfo->err = jm_jpeg_std_error(&(jerr->pub)); (jerr->pub).error_exit = jmf_error_exit; /* Establish the setjmp return context for jmf_error_exit to use. */ if (setjmp(jerr->setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. * We need to clean up the JPEG object, close the input file, and return. */ jm_jpeg_destroy_compress(cinfo); free(jerr); free(clientData->tmp_data); free(clientData); free(cinfo); printf("JPEG encoding error!\n"); return 0; } /* Now we can initialize the JPEG compression object. */ jpeg_create_compress(cinfo); /* Set up my own destination manager. */ jmf_dest = (jmf_destination_mgr *) malloc(sizeof(jmf_destination_mgr));/* Alloc 5 */ jmf_dest->pub.init_destination = jmf_init_destination; jmf_dest->pub.empty_output_buffer = jmf_empty_output_buffer; jmf_dest->pub.term_destination = jmf_term_destination; cinfo->dest = (struct jpeg_destination_mgr *) jmf_dest; /* Set up client data */ cinfo->client_data = clientData; /* Step 3: set parameters for compression */ /* First we supply a description of the input image. * Four fields of the cinfo struct must be filled in: */ cinfo->image_width = width; /* image width and height, in pixels */ cinfo->image_height = height; cinfo->input_components = 3; /* # of color components per pixel */ cinfo->in_color_space = JCS_RGB; /* colorspace of input image */ /* Tell the library to set default parameters */ jm_jpeg_set_defaults(cinfo); /* Default decimation is YUV 4:2:0. If we need 422 or 444, we modify the h_samp_factor and v_samp_factor for U and V components. */ if (decimation >= 1) { int hs, vs; switch (decimation) { case 1: hs = 2; vs = 2; break; case 2: hs = 2; vs = 1; break; case 4: hs = 1; vs = 1; break; } (cinfo->comp_info[0]).v_samp_factor = vs; (cinfo->comp_info[0]).h_samp_factor = hs; (cinfo->comp_info[1]).v_samp_factor = 1; (cinfo->comp_info[2]).v_samp_factor = 1; (cinfo->comp_info[1]).h_samp_factor = 1; (cinfo->comp_info[2]).h_samp_factor = 1; } /* Now you can set any non-default parameters you wish to. * Here we just illustrate the use of quality (quantization table) scaling: */ jm_jpeg_set_quality(cinfo, quality, TRUE /* limit to baseline-JPEG values */ ); return cinfo; }
void * JPEG_To_RGB_init() { struct jmf_error_mgr2 *jerr; struct jpeg_decompress_struct *cinfo; jmf_source_mgr *jmf_src; jmf_src_data *clientData; clientData = (jmf_src_data*) pcsl_mem_malloc(sizeof(jmf_src_data)); /* Alloc 1 */ clientData->data = NULL; /* Step 1: allocate and initialize JPEG decompression object */ cinfo = (struct jpeg_decompress_struct *) pcsl_mem_malloc(sizeof(struct jpeg_decompress_struct));/* Alloc 3 */ /* Initialize error parameters */ jerr = (struct jmf_error_mgr2 *) pcsl_mem_malloc(sizeof(struct jmf_error_mgr2)); /* Alloc 4 */ clientData->jerr = (void *) jerr; cinfo->err = jm_jpeg_std_error(&(jerr->pub)); (jerr->pub).error_exit = jmf_error_exit2; /* Establish the setjmp return context for jmf_error_exit to use. */ if (setjmp(jerr->setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. * We need to clean up the JPEG object, close the input file, and return. */ jm_jpeg_destroy_decompress(cinfo); pcsl_mem_free(jerr); pcsl_mem_free(clientData); pcsl_mem_free(cinfo); return 0; } /* Now we can initialize the JPEG decompression object. */ jpeg_create_decompress(cinfo); /* Set up my own destination manager. */ jmf_src = (jmf_source_mgr *) pcsl_mem_malloc(sizeof(jmf_source_mgr));/* Alloc 5 */ jmf_src->pub.init_source = jmf_init_source; jmf_src->pub.skip_input_data = jmf_skip_input_data; jmf_src->pub.resync_to_restart = jm_jpeg_resync_to_restart; jmf_src->pub.fill_input_buffer = jmf_fill_input_buffer; jmf_src->pub.term_source = jmf_term_source; cinfo->src = (struct jpeg_source_mgr *) jmf_src; /* Set up client data */ cinfo->client_data = clientData; { // John Coffey's code /* * @JC This is where the precalculated ISO huffmann tables * are calculated and put into instance data, in Nielsen's * case this data will not ne overwritten as no DHT markers * will be present in the stream */ int i; for (i = 0; i < NUM_HUFF_TBLS - 1; i++) /* Note the -1 @JC */ { /* @JC cinfo->dc_huff_tbl_ptrs[i] = NULL; */ /* @JC cinfo->ac_huff_tbl_ptrs[i] = NULL; */ /* Note probably should not allocate for 4th unused component */ if (cinfo->dc_huff_tbl_ptrs[i]) { cinfo->dc_huff_tbl_ptrs[i] = jm_jpeg_alloc_huff_table ((j_common_ptr) cinfo); } if (cinfo->ac_huff_tbl_ptrs[i]) { cinfo->ac_huff_tbl_ptrs[i] = jm_jpeg_alloc_huff_table ((j_common_ptr) cinfo); } } /* @JC Ensure that the 4th table is as per normal, only allocate if needed */ cinfo->dc_huff_tbl_ptrs[i] = NULL; cinfo->ac_huff_tbl_ptrs[i] = NULL; /* @JC This huffman table works for all 3 components, this * is why it is defined outside the loop above */ jm_calculate_huffman_table (cinfo, jm_huffmanTable); } return (void*) cinfo; }