add_huff_table (j_compress_ptr cinfo, JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val) /* Define a Huffman table */ { int nsymbols, len; if (*htblptr == NULL) *htblptr = jm_jpeg_alloc_huff_table((j_common_ptr) cinfo); /* Copy the number-of-symbols-of-each-code-length counts */ MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits)); /* Validate the counts. We do this here mainly so we can copy the right * number of symbols from the val[] array, without risking marching off * the end of memory. jchuff.c will do a more thorough test later. */ nsymbols = 0; for (len = 1; len <= 16; len++) nsymbols += bits[len]; if (nsymbols < 1 || nsymbols > 256) ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8)); /* Initialize sent_table FALSE so table will be written to JPEG file. */ (*htblptr)->sent_table = FALSE; }
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; }
/* * Code to preset the huffman table given the H_Factor */ void jm_calculate_huffman_table (j_decompress_ptr cinfo, const unsigned char *jm_huffmanTable) { INT32 length = 0; UINT8 bits[17]; UINT8 huffval[256]; int i, index, count; JHUFF_TBL **htblptr; /* Read in the length bytes */ jm_huffmanTable += 2; // Skip over the marker first // length = *(short *)jm_huffmanTable; length = 0x1a2; // Hard coded from the table length -= 2; jm_huffmanTable += 2; /* * @JC Read in all components, note that all components start with * an identifyer that indicates the table class and the */ while (length > 0) { index = *(jm_huffmanTable++); bits[0] = 0; count = 0; /* @JC read in the 16 bit length entries, the value at index 'i' * represents how many 'i' bitlength codewords follow. Zero * entrues indicate that there are no codes for that bitlength * * Count keeps a track of how many code values should follow the * bits array. This should be <= 256 */ for (i = 1; i <= 16; i++) { bits[i] = *(jm_huffmanTable++); count += bits[i]; } /* Update length to acount for index & bitlengths */ length -= 1 + 16; /* if (count > 256 || ((INT32) count) > length) ERREXIT(cinfo, JERR_DHT_COUNTS); */ for (i = 0; i < count; i++) huffval[i] = *(jm_huffmanTable++); /* Update length as code values read in */ length -= count; /* AC table definition */ if (index & 0x10) { index -= 0x10; htblptr = &cinfo->ac_huff_tbl_ptrs[index]; } else { /* DC table definition */ htblptr = &cinfo->dc_huff_tbl_ptrs[index]; } /* if (index < 0 || index >= NUM_HUFF_TBLS) ERREXIT1(cinfo, JERR_DHT_INDEX, index); */ /* @JC Must ensure that this if is never true in Modified Nielsen Codec * This is because the huffman table will be read in beforehand * in ISO format */ if (*htblptr == NULL) *htblptr = jm_jpeg_alloc_huff_table((j_common_ptr) cinfo); memcpy((*htblptr)->bits, bits, sizeof((*htblptr)->bits)); memcpy((*htblptr)->huffval, huffval, sizeof((*htblptr)->huffval)); } }