diff --git a/jpeg/jpeg-6b-2022/jchuff.c b/jpeg/jpeg-6b-2022/jchuff.c index 9bda899..2ab5f3b 100644 --- a/jpeg/jpeg-6b-2022/jchuff.c +++ b/jpeg/jpeg-6b-2022/jchuff.c @@ -971,9 +971,9 @@ jpeg_gen_optimal_table(j_compress_ptr cinfo, JHUFF_TBL* htbl, long freq[]) j--; } - bits[i] = (UINT8) (bits[i] - 2U); /* remove two symbols */ + bits[i] = (UINT8)(bits[i] - 2U); /* remove two symbols */ bits[i - 1]++; /* one goes in this length */ - bits[j + 1] = (UINT8) (bits[j + 1] + 2U); /* two new symbols in this length */ + bits[j + 1] = (UINT8)(bits[j + 1] + 2U); /* two new symbols in this length */ bits[j]--; /* symbol of this length is now a prefix */ } } diff --git a/jpeg/jpeg-6b-2022/jcmaster.c b/jpeg/jpeg-6b-2022/jcmaster.c index e1460e4..a9bc3f8 100644 --- a/jpeg/jpeg-6b-2022/jcmaster.c +++ b/jpeg/jpeg-6b-2022/jcmaster.c @@ -433,7 +433,7 @@ per_scan_setup(j_compress_ptr cinfo) /* For noninterleaved scans, it is convenient to define last_row_height * as the number of block rows present in the last iMCU row. */ - tmp = (int) (compptr->height_in_blocks % (JDIMENSION) compptr->v_samp_factor); + tmp = (int)(compptr->height_in_blocks % (JDIMENSION) compptr->v_samp_factor); if(tmp == 0) { diff --git a/jpeg/jpeg-6b-2022/jcparam.c b/jpeg/jpeg-6b-2022/jcparam.c index 405ad02..f52f0b8 100644 --- a/jpeg/jpeg-6b-2022/jcparam.c +++ b/jpeg/jpeg-6b-2022/jcparam.c @@ -211,7 +211,7 @@ add_huff_table(j_compress_ptr cinfo, ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); } - MEMCOPY((*htblptr)->huffval, val, (size_t) ((size_t) nsymbols * SIZEOF(UINT8))); + MEMCOPY((*htblptr)->huffval, val, (size_t)((size_t) nsymbols * SIZEOF(UINT8))); /* Initialize sent_table FALSE so table will be written to JPEG file. */ (*htblptr)->sent_table = FALSE; @@ -657,7 +657,7 @@ jpeg_simple_progression(j_compress_ptr cinfo) cinfo->script_space_size = MAX(nscans, 10); cinfo->script_space = (jpeg_scan_info*) (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_PERMANENT, - (size_t) ((size_t) cinfo->script_space_size * SIZEOF(jpeg_scan_info))); + (size_t)((size_t) cinfo->script_space_size * SIZEOF(jpeg_scan_info))); } scanptr = cinfo->script_space; diff --git a/jpeg/jpeg-6b-2022/jcphuff.c b/jpeg/jpeg-6b-2022/jcphuff.c index 078e1d7..4a7523d 100644 --- a/jpeg/jpeg-6b-2022/jcphuff.c +++ b/jpeg/jpeg-6b-2022/jcphuff.c @@ -370,7 +370,7 @@ emit_eobrun(phuff_entropy_ptr entropy) unsigned int temp = entropy->EOBRUN; int nbits = 0; - while((unsigned int) (temp >>= 1U) != 0U) + while((unsigned int)(temp >>= 1U) != 0U) { nbits++; } diff --git a/jpeg/jpeg-6b-2022/jcprepct.c b/jpeg/jpeg-6b-2022/jcprepct.c index a097761..f0efd04 100644 --- a/jpeg/jpeg-6b-2022/jcprepct.c +++ b/jpeg/jpeg-6b-2022/jcprepct.c @@ -317,7 +317,7 @@ create_context_buffer(j_compress_ptr cinfo) */ fake_buffer = (JSAMPARRAY) (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_IMAGE, - (size_t) (cinfo->num_components * 5 * rgroup_height) * + (size_t)(cinfo->num_components * 5 * rgroup_height) * SIZEOF(JSAMPROW)); for(ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; @@ -334,7 +334,7 @@ create_context_buffer(j_compress_ptr cinfo) (JDIMENSION)(3 * rgroup_height)); /* Copy true buffer row pointers into the middle of the fake row array */ MEMCOPY(fake_buffer + rgroup_height, true_buffer, - (size_t) (3 * rgroup_height) * SIZEOF(JSAMPROW)); + (size_t)(3 * rgroup_height) * SIZEOF(JSAMPROW)); /* Fill in the above and below wraparound pointers */ for(i = 0; i < rgroup_height; i++) diff --git a/jpeg/jpeg-6b-2022/jdapistd.c b/jpeg/jpeg-6b-2022/jdapistd.c index 4abc8c7..06eea37 100644 --- a/jpeg/jpeg-6b-2022/jdapistd.c +++ b/jpeg/jpeg-6b-2022/jdapistd.c @@ -35,49 +35,77 @@ LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo)); */ GLOBAL(boolean) -jpeg_start_decompress (j_decompress_ptr cinfo) +jpeg_start_decompress(j_decompress_ptr cinfo) { - if (cinfo->global_state == DSTATE_READY) { + if(cinfo->global_state == DSTATE_READY) + { /* First call: initialize master control, select active modules */ jinit_master_decompress(cinfo); - if (cinfo->buffered_image) { + + if(cinfo->buffered_image) + { /* No more work here; expecting jpeg_start_output next */ cinfo->global_state = DSTATE_BUFIMAGE; return TRUE; } + cinfo->global_state = DSTATE_PRELOAD; } - if (cinfo->global_state == DSTATE_PRELOAD) { + + if(cinfo->global_state == DSTATE_PRELOAD) + { /* If file has multiple scans, absorb them all into the coef buffer */ - if (cinfo->inputctl->has_multiple_scans) { + if(cinfo->inputctl->has_multiple_scans) + { #ifdef D_MULTISCAN_FILES_SUPPORTED - for (;;) { - int retcode; - /* Call progress monitor hook if present */ - if (cinfo->progress != NULL) - (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); - /* Absorb some more input */ - retcode = (*cinfo->inputctl->consume_input) (cinfo); - if (retcode == JPEG_SUSPENDED) - return FALSE; - if (retcode == JPEG_REACHED_EOI) - break; - /* Advance progress counter if appropriate */ - if (cinfo->progress != NULL && - (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { - if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { - /* jdmaster underestimated number of scans; ratchet up one scan */ - cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; - } - } + + for(;;) + { + int retcode; + + /* Call progress monitor hook if present */ + if(cinfo->progress != NULL) + { + (*cinfo->progress->progress_monitor)((j_common_ptr) cinfo); + } + + /* Absorb some more input */ + retcode = (*cinfo->inputctl->consume_input)(cinfo); + + if(retcode == JPEG_SUSPENDED) + { + return FALSE; + } + + if(retcode == JPEG_REACHED_EOI) + { + break; + } + + /* Advance progress counter if appropriate */ + if(cinfo->progress != NULL && + (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) + { + if(++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) + { + /* jdmaster underestimated number of scans; ratchet up one scan */ + cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; + } + } } + #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif /* D_MULTISCAN_FILES_SUPPORTED */ } + cinfo->output_scan_number = cinfo->input_scan_number; - } else if (cinfo->global_state != DSTATE_PRESCAN) + } + else if(cinfo->global_state != DSTATE_PRESCAN) + { ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + } + /* Perform any dummy output passes, and set up for the final pass */ return output_pass_setup(cinfo); } @@ -92,41 +120,54 @@ jpeg_start_decompress (j_decompress_ptr cinfo) */ LOCAL(boolean) -output_pass_setup (j_decompress_ptr cinfo) +output_pass_setup(j_decompress_ptr cinfo) { - if (cinfo->global_state != DSTATE_PRESCAN) { + if(cinfo->global_state != DSTATE_PRESCAN) + { /* First call: do pass setup */ - (*cinfo->master->prepare_for_output_pass) (cinfo); + (*cinfo->master->prepare_for_output_pass)(cinfo); cinfo->output_scanline = 0; cinfo->global_state = DSTATE_PRESCAN; } + /* Loop over any required dummy passes */ - while (cinfo->master->is_dummy_pass) { + while(cinfo->master->is_dummy_pass) + { #ifdef QUANT_2PASS_SUPPORTED + /* Crank through the dummy pass */ - while (cinfo->output_scanline < cinfo->output_height) { + while(cinfo->output_scanline < cinfo->output_height) + { JDIMENSION last_scanline; + /* Call progress monitor hook if present */ - if (cinfo->progress != NULL) { - cinfo->progress->pass_counter = (long) cinfo->output_scanline; - cinfo->progress->pass_limit = (long) cinfo->output_height; - (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + if(cinfo->progress != NULL) + { + cinfo->progress->pass_counter = (long) cinfo->output_scanline; + cinfo->progress->pass_limit = (long) cinfo->output_height; + (*cinfo->progress->progress_monitor)((j_common_ptr) cinfo); } + /* Process some data */ last_scanline = cinfo->output_scanline; - (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL, - &cinfo->output_scanline, (JDIMENSION) 0); - if (cinfo->output_scanline == last_scanline) - return FALSE; /* No progress made, must suspend */ + (*cinfo->main->process_data)(cinfo, (JSAMPARRAY) NULL, + &cinfo->output_scanline, (JDIMENSION) 0); + + if(cinfo->output_scanline == last_scanline) + { + return FALSE; /* No progress made, must suspend */ + } } + /* Finish up dummy pass, and set up for another one */ - (*cinfo->master->finish_output_pass) (cinfo); - (*cinfo->master->prepare_for_output_pass) (cinfo); + (*cinfo->master->finish_output_pass)(cinfo); + (*cinfo->master->prepare_for_output_pass)(cinfo); cinfo->output_scanline = 0; #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif /* QUANT_2PASS_SUPPORTED */ } + /* Ready for application to drive output pass through * jpeg_read_scanlines or jpeg_read_raw_data. */ @@ -149,28 +190,33 @@ output_pass_setup (j_decompress_ptr cinfo) */ GLOBAL(JDIMENSION) -jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines, - JDIMENSION max_lines) +jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines, + JDIMENSION max_lines) { JDIMENSION row_ctr; - if (cinfo->global_state != DSTATE_SCANNING) + if(cinfo->global_state != DSTATE_SCANNING) + { ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); - if (cinfo->output_scanline >= cinfo->output_height) { + } + + if(cinfo->output_scanline >= cinfo->output_height) + { WARNMS(cinfo, JWRN_TOO_MUCH_DATA); return 0; } /* Call progress monitor hook if present */ - if (cinfo->progress != NULL) { + if(cinfo->progress != NULL) + { cinfo->progress->pass_counter = (long) cinfo->output_scanline; cinfo->progress->pass_limit = (long) cinfo->output_height; - (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + (*cinfo->progress->progress_monitor)((j_common_ptr) cinfo); } /* Process some data */ row_ctr = 0; - (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines); + (*cinfo->main->process_data)(cinfo, scanlines, &row_ctr, max_lines); cinfo->output_scanline += row_ctr; return row_ctr; } @@ -182,33 +228,43 @@ jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines, */ GLOBAL(JDIMENSION) -jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data, - JDIMENSION max_lines) +jpeg_read_raw_data(j_decompress_ptr cinfo, JSAMPIMAGE data, + JDIMENSION max_lines) { JDIMENSION lines_per_iMCU_row; - if (cinfo->global_state != DSTATE_RAW_OK) + if(cinfo->global_state != DSTATE_RAW_OK) + { ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); - if (cinfo->output_scanline >= cinfo->output_height) { + } + + if(cinfo->output_scanline >= cinfo->output_height) + { WARNMS(cinfo, JWRN_TOO_MUCH_DATA); return 0; } /* Call progress monitor hook if present */ - if (cinfo->progress != NULL) { + if(cinfo->progress != NULL) + { cinfo->progress->pass_counter = (long) cinfo->output_scanline; cinfo->progress->pass_limit = (long) cinfo->output_height; - (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); + (*cinfo->progress->progress_monitor)((j_common_ptr) cinfo); } /* Verify that at least one iMCU row can be returned. */ - lines_per_iMCU_row = (JDIMENSION) (cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size); - if (max_lines < lines_per_iMCU_row) + lines_per_iMCU_row = (JDIMENSION)(cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size); + + if(max_lines < lines_per_iMCU_row) + { ERREXIT(cinfo, JERR_BUFFER_SIZE); + } /* Decompress directly into user's buffer. */ - if (! (*cinfo->coef->decompress_data) (cinfo, data)) - return 0; /* suspension forced, can do nothing more */ + if(!(*cinfo->coef->decompress_data)(cinfo, data)) + { + return 0; /* suspension forced, can do nothing more */ + } /* OK, we processed one iMCU row. */ cinfo->output_scanline += lines_per_iMCU_row; @@ -225,17 +281,26 @@ jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data, */ GLOBAL(boolean) -jpeg_start_output (j_decompress_ptr cinfo, int scan_number) +jpeg_start_output(j_decompress_ptr cinfo, int scan_number) { - if (cinfo->global_state != DSTATE_BUFIMAGE && + if(cinfo->global_state != DSTATE_BUFIMAGE && cinfo->global_state != DSTATE_PRESCAN) + { ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + } + /* Limit scan number to valid range */ - if (scan_number <= 0) + if(scan_number <= 0) + { scan_number = 1; - if (cinfo->inputctl->eoi_reached && + } + + if(cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number) + { scan_number = cinfo->input_scan_number; + } + cinfo->output_scan_number = scan_number; /* Perform any dummy output passes, and set up for the real pass */ return output_pass_setup(cinfo); @@ -250,24 +315,32 @@ jpeg_start_output (j_decompress_ptr cinfo, int scan_number) */ GLOBAL(boolean) -jpeg_finish_output (j_decompress_ptr cinfo) +jpeg_finish_output(j_decompress_ptr cinfo) { - if ((cinfo->global_state == DSTATE_SCANNING || - cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) { + if((cinfo->global_state == DSTATE_SCANNING || + cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) + { /* Terminate this pass. */ /* We do not require the whole pass to have been completed. */ - (*cinfo->master->finish_output_pass) (cinfo); + (*cinfo->master->finish_output_pass)(cinfo); cinfo->global_state = DSTATE_BUFPOST; - } else if (cinfo->global_state != DSTATE_BUFPOST) { + } + else if(cinfo->global_state != DSTATE_BUFPOST) + { /* BUFPOST = repeat call after a suspension, anything else is error */ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); } + /* Read markers looking for SOS or EOI */ - while (cinfo->input_scan_number <= cinfo->output_scan_number && - ! cinfo->inputctl->eoi_reached) { - if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED) - return FALSE; /* Suspend, come back later */ + while(cinfo->input_scan_number <= cinfo->output_scan_number && + ! cinfo->inputctl->eoi_reached) + { + if((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) + { + return FALSE; /* Suspend, come back later */ + } } + cinfo->global_state = DSTATE_BUFIMAGE; return TRUE; } diff --git a/jpeg/jpeg-6b-2022/jdcoefct.c b/jpeg/jpeg-6b-2022/jdcoefct.c index 956eb76..1d94fc4 100644 --- a/jpeg/jpeg-6b-2022/jdcoefct.c +++ b/jpeg/jpeg-6b-2022/jdcoefct.c @@ -211,7 +211,7 @@ decompress_onepass(j_decompress_ptr cinfo, JSAMPIMAGE output_buf) useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width : compptr->last_col_width; output_ptr = output_buf[compptr->component_index] + - (JDIMENSION) ((JDIMENSION) yoffset * (JDIMENSION) compptr->DCT_scaled_size); + (JDIMENSION)((JDIMENSION) yoffset * (JDIMENSION) compptr->DCT_scaled_size); start_col = MCU_col_num * (JDIMENSION) compptr->MCU_sample_width; for(yindex = 0; yindex < compptr->MCU_height; yindex++) @@ -635,7 +635,7 @@ decompress_smooth_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf) access_rows += compptr->v_samp_factor; /* prior iMCU row too */ buffer = (*cinfo->mem->access_virt_barray) ((j_common_ptr) cinfo, coef->whole_image[ci], - (JDIMENSION) (cinfo->output_iMCU_row - 1) * (JDIMENSION) compptr->v_samp_factor, + (JDIMENSION)(cinfo->output_iMCU_row - 1) * (JDIMENSION) compptr->v_samp_factor, (JDIMENSION) access_rows, FALSE); buffer += compptr->v_samp_factor; /* point to current iMCU row */ first_row = FALSE; diff --git a/jpeg/jpeg-6b-2022/jdmainct.c b/jpeg/jpeg-6b-2022/jdmainct.c index b8ea1d3..90e24ac 100644 --- a/jpeg/jpeg-6b-2022/jdmainct.c +++ b/jpeg/jpeg-6b-2022/jdmainct.c @@ -171,7 +171,7 @@ alloc_funny_pointers(j_decompress_ptr cinfo) */ main->xbuffer[0] = (JSAMPIMAGE) (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_IMAGE, - (size_t) (cinfo->num_components * 2) * SIZEOF(JSAMPARRAY)); + (size_t)(cinfo->num_components * 2) * SIZEOF(JSAMPARRAY)); main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components; for(ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; @@ -184,7 +184,7 @@ alloc_funny_pointers(j_decompress_ptr cinfo) */ xbuf = (JSAMPARRAY) (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_IMAGE, - (size_t) (2 * (rgroup * (M + 4))) * SIZEOF(JSAMPROW)); + (size_t)(2 * (rgroup * (M + 4))) * SIZEOF(JSAMPROW)); xbuf += rgroup; /* want one row group at negative offsets */ main->xbuffer[0][ci] = xbuf; xbuf += rgroup * (M + 4); diff --git a/jpeg/jpeg-6b-2022/jdmarker.c b/jpeg/jpeg-6b-2022/jdmarker.c index 2ac8d92..a5ad723 100644 --- a/jpeg/jpeg-6b-2022/jdmarker.c +++ b/jpeg/jpeg-6b-2022/jdmarker.c @@ -672,8 +672,8 @@ examine_app0(j_decompress_ptr cinfo, JOCTET* data, cinfo->JFIF_major_version = GETJOCTET(data[5]); cinfo->JFIF_minor_version = GETJOCTET(data[6]); cinfo->density_unit = GETJOCTET(data[7]); - cinfo->X_density = (UINT16) ((GETJOCTET(data[8]) << 8) + GETJOCTET(data[9])); - cinfo->Y_density = (UINT16) ((GETJOCTET(data[10]) << 8) + GETJOCTET(data[11])); + cinfo->X_density = (UINT16)((GETJOCTET(data[8]) << 8) + GETJOCTET(data[9])); + cinfo->Y_density = (UINT16)((GETJOCTET(data[10]) << 8) + GETJOCTET(data[11])); /* Check version. * Major version must be 1, anything else signals an incompatible change. @@ -760,9 +760,9 @@ examine_app14(j_decompress_ptr cinfo, JOCTET* data, GETJOCTET(data[4]) == 0x65) { /* Found Adobe APP14 marker */ - version = (unsigned int) ((GETJOCTET(data[5]) << 8) + GETJOCTET(data[6])); - flags0 = (unsigned int) ((GETJOCTET(data[7]) << 8) + GETJOCTET(data[8])); - flags1 = (unsigned int) ((GETJOCTET(data[9]) << 8) + GETJOCTET(data[10])); + version = (unsigned int)((GETJOCTET(data[5]) << 8) + GETJOCTET(data[6])); + flags0 = (unsigned int)((GETJOCTET(data[7]) << 8) + GETJOCTET(data[8])); + flags1 = (unsigned int)((GETJOCTET(data[9]) << 8) + GETJOCTET(data[10])); transform = GETJOCTET(data[11]); TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform); cinfo->saw_Adobe_marker = TRUE; @@ -944,7 +944,7 @@ save_marker(j_decompress_ptr cinfo) /* Reset pointer & calc remaining data length */ data = cur_marker->data; - length = (INT32_JPEG) (cur_marker->original_length - data_length); + length = (INT32_JPEG)(cur_marker->original_length - data_length); } /* Reset to initial state for next marker */ diff --git a/jpeg/jpeg-6b-2022/jdmaster.c b/jpeg/jpeg-6b-2022/jdmaster.c index 2802c5b..8161817 100644 --- a/jpeg/jpeg-6b-2022/jdmaster.c +++ b/jpeg/jpeg-6b-2022/jdmaster.c @@ -18,7 +18,8 @@ /* Private state */ -typedef struct { +typedef struct +{ struct jpeg_decomp_master pub; /* public fields */ int pass_number; /* # of passes completed */ @@ -28,11 +29,11 @@ typedef struct { /* Saved references to initialized quantizer modules, * in case we need to switch modes. */ - struct jpeg_color_quantizer * quantizer_1pass; - struct jpeg_color_quantizer * quantizer_2pass; + struct jpeg_color_quantizer* quantizer_1pass; + struct jpeg_color_quantizer* quantizer_2pass; } my_decomp_master; -typedef my_decomp_master * my_master_ptr; +typedef my_decomp_master* my_master_ptr; /* @@ -41,30 +42,43 @@ typedef my_decomp_master * my_master_ptr; */ LOCAL(boolean) -use_merged_upsample (j_decompress_ptr cinfo) +use_merged_upsample(j_decompress_ptr cinfo) { #ifdef UPSAMPLE_MERGING_SUPPORTED + /* Merging is the equivalent of plain box-filter upsampling */ - if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling) + if(cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling) + { return FALSE; + } + /* jdmerge.c only supports YCC=>RGB color conversion */ - if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 || + if(cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 || cinfo->out_color_space != JCS_RGB || cinfo->out_color_components != RGB_PIXELSIZE) + { return FALSE; + } + /* and it only handles 2h1v or 2h2v sampling ratios */ - if (cinfo->comp_info[0].h_samp_factor != 2 || + if(cinfo->comp_info[0].h_samp_factor != 2 || cinfo->comp_info[1].h_samp_factor != 1 || cinfo->comp_info[2].h_samp_factor != 1 || cinfo->comp_info[0].v_samp_factor > 2 || cinfo->comp_info[1].v_samp_factor != 1 || cinfo->comp_info[2].v_samp_factor != 1) + { return FALSE; + } + /* furthermore, it doesn't work if we've scaled the IDCTs differently */ - if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size || + if(cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size || cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size || cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size) + { return FALSE; + } + /* ??? also need to test for upsample-time rescaling, when & if supported */ return TRUE; /* by golly, it'll work... */ #else @@ -81,80 +95,95 @@ use_merged_upsample (j_decompress_ptr cinfo) */ GLOBAL(void) -jpeg_calc_output_dimensions (j_decompress_ptr cinfo) +jpeg_calc_output_dimensions(j_decompress_ptr cinfo) /* Do computations that are needed before master selection phase */ { #ifdef IDCT_SCALING_SUPPORTED int ci; - jpeg_component_info *compptr; + jpeg_component_info* compptr; #endif /* Prevent application from calling me at wrong times */ - if (cinfo->global_state != DSTATE_READY) + if(cinfo->global_state != DSTATE_READY) + { ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + } #ifdef IDCT_SCALING_SUPPORTED /* Compute actual output image dimensions and DCT scaling choices. */ - if (cinfo->scale_num * 8 <= cinfo->scale_denom) { + if(cinfo->scale_num * 8 <= cinfo->scale_denom) + { /* Provide 1/8 scaling */ cinfo->output_width = (JDIMENSION) - jdiv_round_up((long) cinfo->image_width, 8L); + jdiv_round_up((long) cinfo->image_width, 8L); cinfo->output_height = (JDIMENSION) - jdiv_round_up((long) cinfo->image_height, 8L); + jdiv_round_up((long) cinfo->image_height, 8L); cinfo->min_DCT_scaled_size = 1; - } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) { + } + else if(cinfo->scale_num * 4 <= cinfo->scale_denom) + { /* Provide 1/4 scaling */ cinfo->output_width = (JDIMENSION) - jdiv_round_up((long) cinfo->image_width, 4L); + jdiv_round_up((long) cinfo->image_width, 4L); cinfo->output_height = (JDIMENSION) - jdiv_round_up((long) cinfo->image_height, 4L); + jdiv_round_up((long) cinfo->image_height, 4L); cinfo->min_DCT_scaled_size = 2; - } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) { + } + else if(cinfo->scale_num * 2 <= cinfo->scale_denom) + { /* Provide 1/2 scaling */ cinfo->output_width = (JDIMENSION) - jdiv_round_up((long) cinfo->image_width, 2L); + jdiv_round_up((long) cinfo->image_width, 2L); cinfo->output_height = (JDIMENSION) - jdiv_round_up((long) cinfo->image_height, 2L); + jdiv_round_up((long) cinfo->image_height, 2L); cinfo->min_DCT_scaled_size = 4; - } else { + } + else + { /* Provide 1/1 scaling */ cinfo->output_width = cinfo->image_width; cinfo->output_height = cinfo->image_height; cinfo->min_DCT_scaled_size = DCTSIZE; } + /* In selecting the actual DCT scaling for each component, we try to * scale up the chroma components via IDCT scaling rather than upsampling. * This saves time if the upsampler gets to use 1:1 scaling. * Note this code assumes that the supported DCT scalings are powers of 2. */ - for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; - ci++, compptr++) { + for(ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) + { int ssize = cinfo->min_DCT_scaled_size; - while (ssize < DCTSIZE && - (compptr->h_samp_factor * ssize * 2 <= - cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) && - (compptr->v_samp_factor * ssize * 2 <= - cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) { + + while(ssize < DCTSIZE && + (compptr->h_samp_factor* ssize * 2 <= + cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) && + (compptr->v_samp_factor* ssize * 2 <= + cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) + { ssize = ssize * 2; } + compptr->DCT_scaled_size = ssize; } /* Recompute downsampled dimensions of components; * application needs to know these if using raw downsampled data. */ - for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; - ci++, compptr++) { + for(ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; + ci++, compptr++) + { /* Size in samples, after IDCT scaling */ compptr->downsampled_width = (JDIMENSION) - jdiv_round_up((long) cinfo->image_width * - (long) (compptr->h_samp_factor * compptr->DCT_scaled_size), - (long) (cinfo->max_h_samp_factor * DCTSIZE)); + jdiv_round_up((long) cinfo->image_width * + (long)(compptr->h_samp_factor * compptr->DCT_scaled_size), + (long)(cinfo->max_h_samp_factor * DCTSIZE)); compptr->downsampled_height = (JDIMENSION) - jdiv_round_up((long) cinfo->image_height * - (long) (compptr->v_samp_factor * compptr->DCT_scaled_size), - (long) (cinfo->max_v_samp_factor * DCTSIZE)); + jdiv_round_up((long) cinfo->image_height * + (long)(compptr->v_samp_factor * compptr->DCT_scaled_size), + (long)(cinfo->max_v_samp_factor * DCTSIZE)); } #else /* !IDCT_SCALING_SUPPORTED */ @@ -170,34 +199,44 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo) /* Report number of components in selected colorspace. */ /* Probably this should be in the color conversion module... */ - switch (cinfo->out_color_space) { - case JCS_GRAYSCALE: - cinfo->out_color_components = 1; - break; - case JCS_RGB: + switch(cinfo->out_color_space) + { + case JCS_GRAYSCALE: + cinfo->out_color_components = 1; + break; + + case JCS_RGB: #if RGB_PIXELSIZE != 3 - cinfo->out_color_components = RGB_PIXELSIZE; - break; + cinfo->out_color_components = RGB_PIXELSIZE; + break; #endif /* else share code with YCbCr */ - case JCS_YCbCr: - cinfo->out_color_components = 3; - break; - case JCS_CMYK: - case JCS_YCCK: - cinfo->out_color_components = 4; - break; - default: /* else must be same colorspace as in file */ - cinfo->out_color_components = cinfo->num_components; - break; + + case JCS_YCbCr: + cinfo->out_color_components = 3; + break; + + case JCS_CMYK: + case JCS_YCCK: + cinfo->out_color_components = 4; + break; + + default: /* else must be same colorspace as in file */ + cinfo->out_color_components = cinfo->num_components; + break; } + cinfo->output_components = (cinfo->quantize_colors ? 1 : - cinfo->out_color_components); + cinfo->out_color_components); /* See if upsampler will want to emit more than one row at a time */ - if (use_merged_upsample(cinfo)) + if(use_merged_upsample(cinfo)) + { cinfo->rec_outbuf_height = cinfo->max_v_samp_factor; + } else + { cinfo->rec_outbuf_height = 1; + } } @@ -217,7 +256,7 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo) * For most steps we can mathematically guarantee that the initial value * of x is within MAXJSAMPLE+1 of the legal range, so a table running from * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial - * limiting step (just after the IDCT), a wildly out-of-range value is + * limiting step (just after the IDCT), a wildly out-of-range value is * possible if the input data is corrupt. To avoid any chance of indexing * off the end of memory and getting a bad-pointer trap, we perform the * post-IDCT limiting thus: @@ -245,31 +284,39 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo) */ LOCAL(void) -prepare_range_limit_table (j_decompress_ptr cinfo) +prepare_range_limit_table(j_decompress_ptr cinfo) /* Allocate and fill in the sample_range_limit table */ { - JSAMPLE * table; + JSAMPLE* table; int i; - table = (JSAMPLE *) - (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, - (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE)); - table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */ + table = (JSAMPLE*) + (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_IMAGE, + (5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE)); + table += (MAXJSAMPLE + 1); /* allow negative subscripts of simple table */ cinfo->sample_range_limit = table; /* First segment of "simple" table: limit[x] = 0 for x < 0 */ - MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE)); + MEMZERO(table - (MAXJSAMPLE + 1), (MAXJSAMPLE + 1) * SIZEOF(JSAMPLE)); + /* Main part of "simple" table: limit[x] = x */ - for (i = 0; i <= MAXJSAMPLE; i++) + for(i = 0; i <= MAXJSAMPLE; i++) + { table[i] = (JSAMPLE) i; + } + table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */ + /* End of simple table, rest of first half of post-IDCT table */ - for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++) + for(i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++) + { table[i] = MAXJSAMPLE; + } + /* Second half of post-IDCT table */ - MEMZERO(table + (2 * (MAXJSAMPLE+1)), - (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE)); - MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE), - cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE)); + MEMZERO(table + (2 * (MAXJSAMPLE + 1)), + (2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE)); + MEMCOPY(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE), + cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE)); } @@ -285,7 +332,7 @@ prepare_range_limit_table (j_decompress_ptr cinfo) */ LOCAL(void) -master_selection (j_decompress_ptr cinfo) +master_selection(j_decompress_ptr cinfo) { my_master_ptr master = (my_master_ptr) cinfo->master; boolean use_c_buffer; @@ -299,8 +346,11 @@ master_selection (j_decompress_ptr cinfo) /* Width of an output scanline must be representable as JDIMENSION. */ samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components; jd_samplesperrow = (JDIMENSION) samplesperrow; - if ((long) jd_samplesperrow != samplesperrow) + + if((long) jd_samplesperrow != samplesperrow) + { ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); + } /* Initialize my private state */ master->pass_number = 0; @@ -309,30 +359,45 @@ master_selection (j_decompress_ptr cinfo) /* Color quantizer selection */ master->quantizer_1pass = NULL; master->quantizer_2pass = NULL; + /* No mode changes if not using buffered-image mode. */ - if (! cinfo->quantize_colors || ! cinfo->buffered_image) { + if(! cinfo->quantize_colors || ! cinfo->buffered_image) + { cinfo->enable_1pass_quant = FALSE; cinfo->enable_external_quant = FALSE; cinfo->enable_2pass_quant = FALSE; } - if (cinfo->quantize_colors) { - if (cinfo->raw_data_out) + + if(cinfo->quantize_colors) + { + if(cinfo->raw_data_out) + { ERREXIT(cinfo, JERR_NOTIMPL); + } + /* 2-pass quantizer only works in 3-component color space. */ - if (cinfo->out_color_components != 3) { + if(cinfo->out_color_components != 3) + { cinfo->enable_1pass_quant = TRUE; cinfo->enable_external_quant = FALSE; cinfo->enable_2pass_quant = FALSE; cinfo->colormap = NULL; - } else if (cinfo->colormap != NULL) { + } + else if(cinfo->colormap != NULL) + { cinfo->enable_external_quant = TRUE; - } else if (cinfo->two_pass_quantize) { + } + else if(cinfo->two_pass_quantize) + { cinfo->enable_2pass_quant = TRUE; - } else { + } + else + { cinfo->enable_1pass_quant = TRUE; } - if (cinfo->enable_1pass_quant) { + if(cinfo->enable_1pass_quant) + { #ifdef QUANT_1PASS_SUPPORTED jinit_1pass_quantizer(cinfo); master->quantizer_1pass = cinfo->cquantize; @@ -342,7 +407,8 @@ master_selection (j_decompress_ptr cinfo) } /* We use the 2-pass code to map to external colormaps. */ - if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) { + if(cinfo->enable_2pass_quant || cinfo->enable_external_quant) + { #ifdef QUANT_2PASS_SUPPORTED jinit_2pass_quantizer(cinfo); master->quantizer_2pass = cinfo->cquantize; @@ -350,70 +416,94 @@ master_selection (j_decompress_ptr cinfo) ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } + /* If both quantizers are initialized, the 2-pass one is left active; * this is necessary for starting with quantization to an external map. */ } /* Post-processing: in particular, color conversion first */ - if (! cinfo->raw_data_out) { - if (master->using_merged_upsample) { + if(! cinfo->raw_data_out) + { + if(master->using_merged_upsample) + { #ifdef UPSAMPLE_MERGING_SUPPORTED jinit_merged_upsampler(cinfo); /* does color conversion too */ #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif - } else { + } + else + { jinit_color_deconverter(cinfo); jinit_upsampler(cinfo); } + jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant); } + /* Inverse DCT */ jinit_inverse_dct(cinfo); + /* Entropy decoding: either Huffman or arithmetic coding. */ - if (cinfo->arith_code) { + if(cinfo->arith_code) + { ERREXIT(cinfo, JERR_ARITH_NOTIMPL); - } else { - if (cinfo->progressive_mode) { + } + else + { + if(cinfo->progressive_mode) + { #ifdef D_PROGRESSIVE_SUPPORTED jinit_phuff_decoder(cinfo); #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif - } else + } + else + { jinit_huff_decoder(cinfo); + } } /* Initialize principal buffer controllers. */ use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image; jinit_d_coef_controller(cinfo, use_c_buffer); - if (! cinfo->raw_data_out) + if(! cinfo->raw_data_out) + { jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */); + } /* We can now tell the memory manager to allocate virtual arrays. */ - (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); + (*cinfo->mem->realize_virt_arrays)((j_common_ptr) cinfo); /* Initialize input side of decompressor to consume first scan. */ - (*cinfo->inputctl->start_input_pass) (cinfo); + (*cinfo->inputctl->start_input_pass)(cinfo); #ifdef D_MULTISCAN_FILES_SUPPORTED + /* If jpeg_start_decompress will read the whole file, initialize * progress monitoring appropriately. The input step is counted * as one pass. */ - if (cinfo->progress != NULL && ! cinfo->buffered_image && - cinfo->inputctl->has_multiple_scans) { + if(cinfo->progress != NULL && ! cinfo->buffered_image && + cinfo->inputctl->has_multiple_scans) + { int nscans; + /* Estimate number of scans to set pass_limit. */ - if (cinfo->progressive_mode) { + if(cinfo->progressive_mode) + { /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ nscans = 2 + 3 * cinfo->num_components; - } else { + } + else + { /* For a nonprogressive multiscan file, estimate 1 scan per component. */ nscans = cinfo->num_components; } + cinfo->progress->pass_counter = 0L; cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; cinfo->progress->completed_passes = 0; @@ -421,6 +511,7 @@ master_selection (j_decompress_ptr cinfo) /* Count the input pass as done */ master->pass_number++; } + #endif /* D_MULTISCAN_FILES_SUPPORTED */ } @@ -435,55 +526,77 @@ master_selection (j_decompress_ptr cinfo) */ METHODDEF(void) -prepare_for_output_pass (j_decompress_ptr cinfo) +prepare_for_output_pass(j_decompress_ptr cinfo) { my_master_ptr master = (my_master_ptr) cinfo->master; - if (master->pub.is_dummy_pass) { + if(master->pub.is_dummy_pass) + { #ifdef QUANT_2PASS_SUPPORTED /* Final pass of 2-pass quantization */ master->pub.is_dummy_pass = FALSE; - (*cinfo->cquantize->start_pass) (cinfo, FALSE); - (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST); - (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST); + (*cinfo->cquantize->start_pass)(cinfo, FALSE); + (*cinfo->post->start_pass)(cinfo, JBUF_CRANK_DEST); + (*cinfo->main->start_pass)(cinfo, JBUF_CRANK_DEST); #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif /* QUANT_2PASS_SUPPORTED */ - } else { - if (cinfo->quantize_colors && cinfo->colormap == NULL) { + } + else + { + if(cinfo->quantize_colors && cinfo->colormap == NULL) + { /* Select new quantization method */ - if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) { - cinfo->cquantize = master->quantizer_2pass; - master->pub.is_dummy_pass = TRUE; - } else if (cinfo->enable_1pass_quant) { - cinfo->cquantize = master->quantizer_1pass; - } else { - ERREXIT(cinfo, JERR_MODE_CHANGE); + if(cinfo->two_pass_quantize && cinfo->enable_2pass_quant) + { + cinfo->cquantize = master->quantizer_2pass; + master->pub.is_dummy_pass = TRUE; + } + else if(cinfo->enable_1pass_quant) + { + cinfo->cquantize = master->quantizer_1pass; + } + else + { + ERREXIT(cinfo, JERR_MODE_CHANGE); } } - (*cinfo->idct->start_pass) (cinfo); - (*cinfo->coef->start_output_pass) (cinfo); - if (! cinfo->raw_data_out) { - if (! master->using_merged_upsample) - (*cinfo->cconvert->start_pass) (cinfo); - (*cinfo->upsample->start_pass) (cinfo); - if (cinfo->quantize_colors) - (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass); - (*cinfo->post->start_pass) (cinfo, - (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); - (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); + + (*cinfo->idct->start_pass)(cinfo); + (*cinfo->coef->start_output_pass)(cinfo); + + if(! cinfo->raw_data_out) + { + if(! master->using_merged_upsample) + { + (*cinfo->cconvert->start_pass)(cinfo); + } + + (*cinfo->upsample->start_pass)(cinfo); + + if(cinfo->quantize_colors) + { + (*cinfo->cquantize->start_pass)(cinfo, master->pub.is_dummy_pass); + } + + (*cinfo->post->start_pass)(cinfo, + (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); + (*cinfo->main->start_pass)(cinfo, JBUF_PASS_THRU); } } /* Set up progress monitor's pass info if present */ - if (cinfo->progress != NULL) { + if(cinfo->progress != NULL) + { cinfo->progress->completed_passes = master->pass_number; cinfo->progress->total_passes = master->pass_number + - (master->pub.is_dummy_pass ? 2 : 1); + (master->pub.is_dummy_pass ? 2 : 1); + /* In buffered-image mode, we assume one more output pass if EOI not * yet reached, but no more passes if EOI has been reached. */ - if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) { + if(cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) + { cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1); } } @@ -495,12 +608,15 @@ prepare_for_output_pass (j_decompress_ptr cinfo) */ METHODDEF(void) -finish_output_pass (j_decompress_ptr cinfo) +finish_output_pass(j_decompress_ptr cinfo) { my_master_ptr master = (my_master_ptr) cinfo->master; - if (cinfo->quantize_colors) - (*cinfo->cquantize->finish_pass) (cinfo); + if(cinfo->quantize_colors) + { + (*cinfo->cquantize->finish_pass)(cinfo); + } + master->pass_number++; } @@ -512,23 +628,29 @@ finish_output_pass (j_decompress_ptr cinfo) */ GLOBAL(void) -jpeg_new_colormap (j_decompress_ptr cinfo) +jpeg_new_colormap(j_decompress_ptr cinfo) { my_master_ptr master = (my_master_ptr) cinfo->master; /* Prevent application from calling me at wrong times */ - if (cinfo->global_state != DSTATE_BUFIMAGE) + if(cinfo->global_state != DSTATE_BUFIMAGE) + { ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); + } - if (cinfo->quantize_colors && cinfo->enable_external_quant && - cinfo->colormap != NULL) { + if(cinfo->quantize_colors && cinfo->enable_external_quant && + cinfo->colormap != NULL) + { /* Select 2-pass quantizer for external colormap use */ cinfo->cquantize = master->quantizer_2pass; /* Notify quantizer of colormap change */ - (*cinfo->cquantize->new_color_map) (cinfo); + (*cinfo->cquantize->new_color_map)(cinfo); master->pub.is_dummy_pass = FALSE; /* just in case */ - } else + } + else + { ERREXIT(cinfo, JERR_MODE_CHANGE); + } } #endif /* D_MULTISCAN_FILES_SUPPORTED */ @@ -540,14 +662,14 @@ jpeg_new_colormap (j_decompress_ptr cinfo) */ GLOBAL(void) -jinit_master_decompress (j_decompress_ptr cinfo) +jinit_master_decompress(j_decompress_ptr cinfo) { my_master_ptr master; master = (my_master_ptr) - (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, - SIZEOF(my_decomp_master)); - cinfo->master = (struct jpeg_decomp_master *) master; + (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(my_decomp_master)); + cinfo->master = (struct jpeg_decomp_master*) master; master->pub.prepare_for_output_pass = prepare_for_output_pass; master->pub.finish_output_pass = finish_output_pass; diff --git a/jpeg/jpeg-6b-2022/jdphuff.c b/jpeg/jpeg-6b-2022/jdphuff.c index 15d8ee0..9b42710 100644 --- a/jpeg/jpeg-6b-2022/jdphuff.c +++ b/jpeg/jpeg-6b-2022/jdphuff.c @@ -40,7 +40,8 @@ * but must not be updated permanently until we complete the MCU. */ -typedef struct { +typedef struct +{ unsigned int EOBRUN; /* remaining EOBs in EOBRUN */ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ } savable_state; @@ -64,7 +65,8 @@ typedef struct { #endif -typedef struct { +typedef struct +{ struct jpeg_entropy_decoder pub; /* public fields */ /* These fields are loaded into local variables at start of each MCU. @@ -77,22 +79,22 @@ typedef struct { unsigned int restarts_to_go; /* MCUs left in this restart interval */ /* Pointers to derived tables (these workspaces have image lifespan) */ - d_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; + d_derived_tbl* derived_tbls[NUM_HUFF_TBLS]; - d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */ + d_derived_tbl* ac_derived_tbl; /* active table during an AC scan */ } phuff_entropy_decoder; -typedef phuff_entropy_decoder * phuff_entropy_ptr; +typedef phuff_entropy_decoder* phuff_entropy_ptr; /* Forward declarations */ METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo, - JBLOCKROW *MCU_data)); + JBLOCKROW* MCU_data)); METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo, - JBLOCKROW *MCU_data)); + JBLOCKROW* MCU_data)); METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo, - JBLOCKROW *MCU_data)); + JBLOCKROW* MCU_data)); METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo, - JBLOCKROW *MCU_data)); + JBLOCKROW* MCU_data)); /* @@ -100,93 +102,141 @@ METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo, */ METHODDEF(void) -start_pass_phuff_decoder (j_decompress_ptr cinfo) +start_pass_phuff_decoder(j_decompress_ptr cinfo) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; boolean is_DC_band, bad; int ci, coefi, tbl; - int *coef_bit_ptr; - jpeg_component_info * compptr; + int* coef_bit_ptr; + jpeg_component_info* compptr; is_DC_band = (cinfo->Ss == 0); /* Validate scan parameters */ bad = FALSE; - if (is_DC_band) { - if (cinfo->Se != 0) + + if(is_DC_band) + { + if(cinfo->Se != 0) + { bad = TRUE; - } else { + } + } + else + { /* need not check Ss/Se < 0 since they came from unsigned bytes */ - if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2) + if(cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2) + { bad = TRUE; + } + /* AC scans may have only one component */ - if (cinfo->comps_in_scan != 1) + if(cinfo->comps_in_scan != 1) + { bad = TRUE; + } } - if (cinfo->Ah != 0) { + + if(cinfo->Ah != 0) + { /* Successive approximation refinement scan: must have Al = Ah-1. */ - if (cinfo->Al != cinfo->Ah-1) + if(cinfo->Al != cinfo->Ah - 1) + { bad = TRUE; + } } - if (cinfo->Al > 13) /* need not check for < 0 */ + + if(cinfo->Al > 13) /* need not check for < 0 */ + { bad = TRUE; + } + /* Arguably the maximum Al value should be less than 13 for 8-bit precision, * but the spec doesn't say so, and we try to be liberal about what we * accept. Note: large Al values could result in out-of-range DC * coefficients during early scans, leading to bizarre displays due to * overflows in the IDCT math. But we won't crash. */ - if (bad) + if(bad) ERREXIT4(cinfo, JERR_BAD_PROGRESSION, - cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); + cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); + /* Update progression status, and verify that scan order is legal. * Note that inter-scan inconsistencies are treated as warnings * not fatal errors ... not clear if this is right way to behave. */ - for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + for(ci = 0; ci < cinfo->comps_in_scan; ci++) + { int cindex = cinfo->cur_comp_info[ci]->component_index; coef_bit_ptr = & cinfo->coef_bits[cindex][0]; - if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ + + if(!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ + { WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); - for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { + } + + for(coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) + { int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; - if (cinfo->Ah != expected) - WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); + + if(cinfo->Ah != expected) + { + WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); + } + coef_bit_ptr[coefi] = cinfo->Al; } } /* Select MCU decoding routine */ - if (cinfo->Ah == 0) { - if (is_DC_band) + if(cinfo->Ah == 0) + { + if(is_DC_band) + { entropy->pub.decode_mcu = decode_mcu_DC_first; + } else + { entropy->pub.decode_mcu = decode_mcu_AC_first; - } else { - if (is_DC_band) + } + } + else + { + if(is_DC_band) + { entropy->pub.decode_mcu = decode_mcu_DC_refine; + } else + { entropy->pub.decode_mcu = decode_mcu_AC_refine; + } } - for (ci = 0; ci < cinfo->comps_in_scan; ci++) { + for(ci = 0; ci < cinfo->comps_in_scan; ci++) + { compptr = cinfo->cur_comp_info[ci]; + /* Make sure requested tables are present, and compute derived tables. * We may build same derived table more than once, but it's not expensive. */ - if (is_DC_band) { - if (cinfo->Ah == 0) { /* DC refinement needs no table */ - tbl = compptr->dc_tbl_no; - jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, - & entropy->derived_tbls[tbl]); + if(is_DC_band) + { + if(cinfo->Ah == 0) /* DC refinement needs no table */ + { + tbl = compptr->dc_tbl_no; + jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, + & entropy->derived_tbls[tbl]); } - } else { + } + else + { tbl = compptr->ac_tbl_no; jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, - & entropy->derived_tbls[tbl]); + & entropy->derived_tbls[tbl]); /* remember the single active table */ entropy->ac_derived_tbl = entropy->derived_tbls[tbl]; } + /* Initialize DC predictions to 0 */ entropy->saved.last_dc_val[ci] = 0; } @@ -218,14 +268,18 @@ start_pass_phuff_decoder (j_decompress_ptr cinfo) #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) static const int extend_test[16] = /* entry n is 2**(n-1) */ - { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, - 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; +{ + 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, + 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 +}; static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ - { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, - ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, - ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, - ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 }; +{ + 0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1, + ((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1, + ((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1, + ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1 +}; #endif /* AVOID_TABLES */ @@ -236,7 +290,7 @@ static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ */ LOCAL(boolean) -process_restart (j_decompress_ptr cinfo) +process_restart(j_decompress_ptr cinfo) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; int ci; @@ -247,12 +301,17 @@ process_restart (j_decompress_ptr cinfo) entropy->bitstate.bits_left = 0; /* Advance past the RSTn marker */ - if (! (*cinfo->marker->read_restart_marker) (cinfo)) + if(!(*cinfo->marker->read_restart_marker)(cinfo)) + { return FALSE; + } /* Re-initialize DC predictions to 0 */ - for (ci = 0; ci < cinfo->comps_in_scan; ci++) + for(ci = 0; ci < cinfo->comps_in_scan; ci++) + { entropy->saved.last_dc_val[ci] = 0; + } + /* Re-init EOB run count, too */ entropy->saved.EOBRUN = 0; @@ -264,8 +323,10 @@ process_restart (j_decompress_ptr cinfo) * segment as empty, and we can avoid producing bogus output pixels by * leaving the flag set. */ - if (cinfo->unread_marker == 0) + if(cinfo->unread_marker == 0) + { entropy->pub.insufficient_data = FALSE; + } return TRUE; } @@ -274,7 +335,7 @@ process_restart (j_decompress_ptr cinfo) /* * Huffman MCU decoding. * Each of these routines decodes and returns one MCU's worth of - * Huffman-compressed coefficients. + * Huffman-compressed coefficients. * The coefficients are reordered from zigzag order into natural array order, * but are not dequantized. * @@ -294,8 +355,8 @@ process_restart (j_decompress_ptr cinfo) */ METHODDEF(boolean) -decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) -{ +decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW* MCU_data) +{ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; int Al = cinfo->Al; register int s, r; @@ -303,28 +364,33 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) JBLOCKROW block; BITREAD_STATE_VARS; savable_state state; - d_derived_tbl * tbl; - jpeg_component_info * compptr; + d_derived_tbl* tbl; + jpeg_component_info* compptr; /* Process restart marker if needed; may have to suspend */ - if (cinfo->restart_interval) { - if (entropy->restarts_to_go == 0) - if (! process_restart(cinfo)) - return FALSE; + if(cinfo->restart_interval) + { + if(entropy->restarts_to_go == 0) + if(! process_restart(cinfo)) + { + return FALSE; + } } /* If we've run out of data, just leave the MCU set to zeroes. * This way, we return uniform gray for the remainder of the segment. */ - if (! entropy->pub.insufficient_data) { + if(! entropy->pub.insufficient_data) + { /* Load up working state */ - BITREAD_LOAD_STATE(cinfo,entropy->bitstate); + BITREAD_LOAD_STATE(cinfo, entropy->bitstate); ASSIGN_STATE(state, entropy->saved); /* Outer loop handles each block in the MCU */ - for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + for(blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) + { block = MCU_data[blkn]; ci = cinfo->MCU_membership[blkn]; compptr = cinfo->cur_comp_info[ci]; @@ -334,21 +400,23 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) /* Section F.2.2.1: decode the DC coefficient difference */ HUFF_DECODE(s, br_state, tbl, return FALSE, label1); - if (s) { - CHECK_BIT_BUFFER(br_state, s, return FALSE); - r = GET_BITS(s); - s = HUFF_EXTEND(r, s); + + if(s) + { + CHECK_BIT_BUFFER(br_state, s, return FALSE); + r = GET_BITS(s); + s = HUFF_EXTEND(r, s); } /* Convert DC difference to actual value, update last_dc_val */ s += state.last_dc_val[ci]; state.last_dc_val[ci] = s; /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */ - (*block)[0] = (JCOEF) (s << Al); + (*block)[0] = (JCOEF)(s << Al); } /* Completed MCU, so update state */ - BITREAD_SAVE_STATE(cinfo,entropy->bitstate); + BITREAD_SAVE_STATE(cinfo, entropy->bitstate); ASSIGN_STATE(entropy->saved, state); } @@ -365,8 +433,8 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) */ METHODDEF(boolean) -decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) -{ +decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW* MCU_data) +{ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; int Se = cinfo->Se; int Al = cinfo->Al; @@ -374,19 +442,23 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) unsigned int EOBRUN; JBLOCKROW block; BITREAD_STATE_VARS; - d_derived_tbl * tbl; + d_derived_tbl* tbl; /* Process restart marker if needed; may have to suspend */ - if (cinfo->restart_interval) { - if (entropy->restarts_to_go == 0) - if (! process_restart(cinfo)) - return FALSE; + if(cinfo->restart_interval) + { + if(entropy->restarts_to_go == 0) + if(! process_restart(cinfo)) + { + return FALSE; + } } /* If we've run out of data, just leave the MCU set to zeroes. * This way, we return uniform gray for the remainder of the segment. */ - if (! entropy->pub.insufficient_data) { + if(! entropy->pub.insufficient_data) + { /* Load up working state. * We can avoid loading/saving bitread state if in an EOB run. @@ -395,41 +467,55 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) /* There is always only one block per MCU */ - if (EOBRUN > 0) /* if it's a band of zeroes... */ - EOBRUN--; /* ...process it now (we do nothing) */ - else { - BITREAD_LOAD_STATE(cinfo,entropy->bitstate); + if(EOBRUN > 0) /* if it's a band of zeroes... */ + { + EOBRUN--; /* ...process it now (we do nothing) */ + } + else + { + BITREAD_LOAD_STATE(cinfo, entropy->bitstate); block = MCU_data[0]; tbl = entropy->ac_derived_tbl; - for (k = cinfo->Ss; k <= Se; k++) { - HUFF_DECODE(s, br_state, tbl, return FALSE, label2); - r = s >> 4; - s &= 15; - if (s) { - k += r; - CHECK_BIT_BUFFER(br_state, s, return FALSE); - r = GET_BITS(s); - s = HUFF_EXTEND(r, s); - /* Scale and output coefficient in natural (dezigzagged) order */ - (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al); - } else { - if (r == 15) { /* ZRL */ - k += 15; /* skip 15 zeroes in band */ - } else { /* EOBr, run length is 2^r + appended bits */ - EOBRUN = 1 << r; - if (r) { /* EOBr, r > 0 */ - CHECK_BIT_BUFFER(br_state, r, return FALSE); - r = GET_BITS(r); - EOBRUN += r; - } - EOBRUN--; /* this band is processed at this moment */ - break; /* force end-of-band */ - } - } + for(k = cinfo->Ss; k <= Se; k++) + { + HUFF_DECODE(s, br_state, tbl, return FALSE, label2); + r = s >> 4; + s &= 15; + + if(s) + { + k += r; + CHECK_BIT_BUFFER(br_state, s, return FALSE); + r = GET_BITS(s); + s = HUFF_EXTEND(r, s); + /* Scale and output coefficient in natural (dezigzagged) order */ + (*block)[jpeg_natural_order[k]] = (JCOEF)(s << Al); + } + else + { + if(r == 15) /* ZRL */ + { + k += 15; /* skip 15 zeroes in band */ + } + else /* EOBr, run length is 2^r + appended bits */ + { + EOBRUN = 1 << r; + + if(r) /* EOBr, r > 0 */ + { + CHECK_BIT_BUFFER(br_state, r, return FALSE); + r = GET_BITS(r); + EOBRUN += r; + } + + EOBRUN--; /* this band is processed at this moment */ + break; /* force end-of-band */ + } + } } - BITREAD_SAVE_STATE(cinfo,entropy->bitstate); + BITREAD_SAVE_STATE(cinfo, entropy->bitstate); } /* Completed MCU, so update state */ @@ -450,8 +536,8 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) */ METHODDEF(boolean) -decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) -{ +decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW* MCU_data) +{ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ int blkn; @@ -459,10 +545,13 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) BITREAD_STATE_VARS; /* Process restart marker if needed; may have to suspend */ - if (cinfo->restart_interval) { - if (entropy->restarts_to_go == 0) - if (! process_restart(cinfo)) - return FALSE; + if(cinfo->restart_interval) + { + if(entropy->restarts_to_go == 0) + if(! process_restart(cinfo)) + { + return FALSE; + } } /* Not worth the cycles to check insufficient_data here, @@ -470,22 +559,27 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) */ /* Load up working state */ - BITREAD_LOAD_STATE(cinfo,entropy->bitstate); + BITREAD_LOAD_STATE(cinfo, entropy->bitstate); /* Outer loop handles each block in the MCU */ - for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { + for(blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) + { block = MCU_data[blkn]; /* Encoded data is simply the next bit of the two's-complement DC value */ CHECK_BIT_BUFFER(br_state, 1, return FALSE); - if (GET_BITS(1)) + + if(GET_BITS(1)) + { (*block)[0] |= p1; + } + /* Note: since we use |=, repeating the assignment later is safe */ } /* Completed MCU, so update state */ - BITREAD_SAVE_STATE(cinfo,entropy->bitstate); + BITREAD_SAVE_STATE(cinfo, entropy->bitstate); /* Account for restart interval (no-op if not using restarts) */ entropy->restarts_to_go--; @@ -499,8 +593,8 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) */ METHODDEF(boolean) -decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) -{ +decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW* MCU_data) +{ phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; int Se = cinfo->Se; int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ @@ -510,23 +604,27 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) JBLOCKROW block; JCOEFPTR thiscoef; BITREAD_STATE_VARS; - d_derived_tbl * tbl; + d_derived_tbl* tbl; int num_newnz; int newnz_pos[DCTSIZE2]; /* Process restart marker if needed; may have to suspend */ - if (cinfo->restart_interval) { - if (entropy->restarts_to_go == 0) - if (! process_restart(cinfo)) - return FALSE; + if(cinfo->restart_interval) + { + if(entropy->restarts_to_go == 0) + if(! process_restart(cinfo)) + { + return FALSE; + } } /* If we've run out of data, don't modify the MCU. */ - if (! entropy->pub.insufficient_data) { + if(! entropy->pub.insufficient_data) + { /* Load up working state */ - BITREAD_LOAD_STATE(cinfo,entropy->bitstate); + BITREAD_LOAD_STATE(cinfo, entropy->bitstate); EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ /* There is always only one block per MCU */ @@ -544,89 +642,139 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) /* initialize coefficient loop counter to start of band */ k = cinfo->Ss; - if (EOBRUN == 0) { - for (; k <= Se; k++) { - HUFF_DECODE(s, br_state, tbl, goto undoit, label3); - r = s >> 4; - s &= 15; - if (s) { - if (s != 1) /* size of new coef should always be 1 */ - WARNMS(cinfo, JWRN_HUFF_BAD_CODE); - CHECK_BIT_BUFFER(br_state, 1, goto undoit); - if (GET_BITS(1)) - s = p1; /* newly nonzero coef is positive */ - else - s = m1; /* newly nonzero coef is negative */ - } else { - if (r != 15) { - EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */ - if (r) { - CHECK_BIT_BUFFER(br_state, r, goto undoit); - r = GET_BITS(r); - EOBRUN += r; - } - break; /* rest of block is handled by EOB logic */ - } - /* note s = 0 for processing ZRL */ - } - /* Advance over already-nonzero coefs and r still-zero coefs, - * appending correction bits to the nonzeroes. A correction bit is 1 - * if the absolute value of the coefficient must be increased. - */ - do { - thiscoef = *block + jpeg_natural_order[k]; - if (*thiscoef != 0) { - CHECK_BIT_BUFFER(br_state, 1, goto undoit); - if (GET_BITS(1)) { - if ((*thiscoef & p1) == 0) { /* do nothing if already set it */ - if (*thiscoef >= 0) - *thiscoef += p1; - else - *thiscoef += m1; - } - } - } else { - if (--r < 0) - break; /* reached target zero coefficient */ - } - k++; - } while (k <= Se); - if (s) { - int pos = jpeg_natural_order[k]; - /* Output newly nonzero coefficient */ - (*block)[pos] = (JCOEF) s; - /* Remember its position in case we have to suspend */ - newnz_pos[num_newnz++] = pos; - } + if(EOBRUN == 0) + { + for(; k <= Se; k++) + { + HUFF_DECODE(s, br_state, tbl, goto undoit, label3); + r = s >> 4; + s &= 15; + + if(s) + { + if(s != 1) /* size of new coef should always be 1 */ + { + WARNMS(cinfo, JWRN_HUFF_BAD_CODE); + } + + CHECK_BIT_BUFFER(br_state, 1, goto undoit); + + if(GET_BITS(1)) + { + s = p1; /* newly nonzero coef is positive */ + } + else + { + s = m1; /* newly nonzero coef is negative */ + } + } + else + { + if(r != 15) + { + EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */ + + if(r) + { + CHECK_BIT_BUFFER(br_state, r, goto undoit); + r = GET_BITS(r); + EOBRUN += r; + } + + break; /* rest of block is handled by EOB logic */ + } + + /* note s = 0 for processing ZRL */ + } + + /* Advance over already-nonzero coefs and r still-zero coefs, + * appending correction bits to the nonzeroes. A correction bit is 1 + * if the absolute value of the coefficient must be increased. + */ + do + { + thiscoef = *block + jpeg_natural_order[k]; + + if(*thiscoef != 0) + { + CHECK_BIT_BUFFER(br_state, 1, goto undoit); + + if(GET_BITS(1)) + { + if((*thiscoef & p1) == 0) /* do nothing if already set it */ + { + if(*thiscoef >= 0) + { + *thiscoef += p1; + } + else + { + *thiscoef += m1; + } + } + } + } + else + { + if(--r < 0) + { + break; /* reached target zero coefficient */ + } + } + + k++; + } + while(k <= Se); + + if(s) + { + int pos = jpeg_natural_order[k]; + /* Output newly nonzero coefficient */ + (*block)[pos] = (JCOEF) s; + /* Remember its position in case we have to suspend */ + newnz_pos[num_newnz++] = pos; + } } } - if (EOBRUN > 0) { + if(EOBRUN > 0) + { /* Scan any remaining coefficient positions after the end-of-band * (the last newly nonzero coefficient, if any). Append a correction * bit to each already-nonzero coefficient. A correction bit is 1 * if the absolute value of the coefficient must be increased. */ - for (; k <= Se; k++) { - thiscoef = *block + jpeg_natural_order[k]; - if (*thiscoef != 0) { - CHECK_BIT_BUFFER(br_state, 1, goto undoit); - if (GET_BITS(1)) { - if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */ - if (*thiscoef >= 0) - *thiscoef += p1; - else - *thiscoef += m1; - } - } - } + for(; k <= Se; k++) + { + thiscoef = *block + jpeg_natural_order[k]; + + if(*thiscoef != 0) + { + CHECK_BIT_BUFFER(br_state, 1, goto undoit); + + if(GET_BITS(1)) + { + if((*thiscoef & p1) == 0) /* do nothing if already changed it */ + { + if(*thiscoef >= 0) + { + *thiscoef += p1; + } + else + { + *thiscoef += m1; + } + } + } + } } + /* Count one block completed in EOB run */ EOBRUN--; } /* Completed MCU, so update state */ - BITREAD_SAVE_STATE(cinfo,entropy->bitstate); + BITREAD_SAVE_STATE(cinfo, entropy->bitstate); entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ } @@ -636,9 +784,12 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) return TRUE; undoit: + /* Re-zero any output coefficients that we made newly nonzero */ - while (num_newnz > 0) + while(num_newnz > 0) + { (*block)[newnz_pos[--num_newnz]] = 0; + } return FALSE; } @@ -649,31 +800,35 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) */ GLOBAL(void) -jinit_phuff_decoder (j_decompress_ptr cinfo) +jinit_phuff_decoder(j_decompress_ptr cinfo) { phuff_entropy_ptr entropy; - int *coef_bit_ptr; + int* coef_bit_ptr; int ci, i; entropy = (phuff_entropy_ptr) - (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, - SIZEOF(phuff_entropy_decoder)); - cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; + (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_IMAGE, + SIZEOF(phuff_entropy_decoder)); + cinfo->entropy = (struct jpeg_entropy_decoder*) entropy; entropy->pub.start_pass = start_pass_phuff_decoder; /* Mark derived tables unallocated */ - for (i = 0; i < NUM_HUFF_TBLS; i++) { + for(i = 0; i < NUM_HUFF_TBLS; i++) + { entropy->derived_tbls[i] = NULL; } /* Create progression status table */ cinfo->coef_bits = (int (*)[DCTSIZE2]) - (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, - cinfo->num_components*DCTSIZE2*SIZEOF(int)); + (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_IMAGE, + cinfo->num_components * DCTSIZE2 * SIZEOF(int)); coef_bit_ptr = & cinfo->coef_bits[0][0]; - for (ci = 0; ci < cinfo->num_components; ci++) - for (i = 0; i < DCTSIZE2; i++) + + for(ci = 0; ci < cinfo->num_components; ci++) + for(i = 0; i < DCTSIZE2; i++) + { *coef_bit_ptr++ = -1; + } } #endif /* D_PROGRESSIVE_SUPPORTED */ diff --git a/jpeg/jpeg-6b-2022/jdsample.c b/jpeg/jpeg-6b-2022/jdsample.c index 32fff35..82de2e6 100644 --- a/jpeg/jpeg-6b-2022/jdsample.c +++ b/jpeg/jpeg-6b-2022/jdsample.c @@ -145,8 +145,8 @@ sep_upsample(j_decompress_ptr cinfo, /* Adjust counts */ *out_row_ctr += num_rows; - upsample->rows_to_go = ((JDIMENSION) upsample->rows_to_go - (JDIMENSION) num_rows); - upsample->next_row_out = (int) ((JDIMENSION) upsample->next_row_out + (JDIMENSION) num_rows); + upsample->rows_to_go = ((JDIMENSION) upsample->rows_to_go - (JDIMENSION) num_rows); + upsample->next_row_out = (int)((JDIMENSION) upsample->next_row_out + (JDIMENSION) num_rows); /* When the buffer is emptied, declare this input row group consumed */ if(upsample->next_row_out >= cinfo->max_v_samp_factor) diff --git a/jpeg/jpeg-6b-2022/jmemmgr.c b/jpeg/jpeg-6b-2022/jmemmgr.c index 840292e..33d77a5 100644 --- a/jpeg/jpeg-6b-2022/jmemmgr.c +++ b/jpeg/jpeg-6b-2022/jmemmgr.c @@ -460,8 +460,8 @@ alloc_sarray(j_common_ptr cinfo, int pool_id, long ltemp; /* Calculate max # of rows allowed in one allocation chunk */ - ltemp = (long) (((long) MAX_ALLOC_CHUNK - (long) SIZEOF(large_pool_hdr)) / - ((long) samplesperrow * (long) SIZEOF(JSAMPLE))); + ltemp = (long)(((long) MAX_ALLOC_CHUNK - (long) SIZEOF(large_pool_hdr)) / + ((long) samplesperrow * (long) SIZEOF(JSAMPLE))); if(ltemp <= 0) { @@ -521,8 +521,8 @@ alloc_barray(j_common_ptr cinfo, int pool_id, long ltemp; /* Calculate max # of rows allowed in one allocation chunk */ - ltemp = (long) (((long) MAX_ALLOC_CHUNK - (long) SIZEOF(large_pool_hdr)) / - ((long) blocksperrow * (long) SIZEOF(JBLOCK))); + ltemp = (long)(((long) MAX_ALLOC_CHUNK - (long) SIZEOF(large_pool_hdr)) / + ((long) blocksperrow * (long) SIZEOF(JBLOCK))); if(ltemp <= 0) { @@ -807,7 +807,7 @@ do_sarray_io(j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing) { long bytesperrow, file_offset, byte_count, rows, thisrow, i; - bytesperrow = (long) ((long) ptr->samplesperrow * (long) SIZEOF(JSAMPLE)); + bytesperrow = (long)((long) ptr->samplesperrow * (long) SIZEOF(JSAMPLE)); file_offset = (long) ptr->cur_start_row * bytesperrow; /* Loop to read or write each allocation chunk in mem_buffer */ @@ -848,7 +848,7 @@ do_barray_io(j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing) { long bytesperrow, file_offset, byte_count, rows, thisrow, i; - bytesperrow = (long) ((long) ptr->blocksperrow * (long) SIZEOF(JBLOCK)); + bytesperrow = (long)((long) ptr->blocksperrow * (long) SIZEOF(JBLOCK)); file_offset = (long) ptr->cur_start_row * bytesperrow; /* Loop to read or write each allocation chunk in mem_buffer */ @@ -933,7 +933,7 @@ access_virt_sarray(j_common_ptr cinfo, jvirt_sarray_ptr ptr, /* use long arithmetic here to avoid overflow & unsigned problems */ long ltemp; - ltemp = (long) ((long) end_row - (long) ptr->rows_in_mem); + ltemp = (long)((long) end_row - (long) ptr->rows_in_mem); if(ltemp < 0) { diff --git a/jpeg/jpeg-6b-2022/jquant1.c b/jpeg/jpeg-6b-2022/jquant1.c index 9fcddae..a5fea08 100644 --- a/jpeg/jpeg-6b-2022/jquant1.c +++ b/jpeg/jpeg-6b-2022/jquant1.c @@ -625,7 +625,7 @@ quantize_ord_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf, * inputs. The maximum dither is +- MAXJSAMPLE; this sets the * required amount of padding. */ - *output_ptr = (JSAMPLE) (*output_ptr + (JDIMENSION) colorindex_ci[GETJSAMPLE(*input_ptr) + dither[col_index]]); + *output_ptr = (JSAMPLE)(*output_ptr + (JDIMENSION) colorindex_ci[GETJSAMPLE(*input_ptr) + dither[col_index]]); input_ptr += nc; output_ptr++; col_index = (col_index + 1) & ODITHER_MASK; @@ -728,8 +728,8 @@ quantize_fs_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf, if(cquantize->on_odd_row) { /* work right to left in this row */ - input_ptr += (JSAMPLE) ((width - 1) * (JDIMENSION) nc); /* so point to rightmost pixel */ - output_ptr += (JSAMPLE) (width - 1); + input_ptr += (JSAMPLE)((width - 1) * (JDIMENSION) nc); /* so point to rightmost pixel */ + output_ptr += (JSAMPLE)(width - 1); dir = -1; dirnc = -nc; errorptr = cquantize->fserrors[ci] + (width + 1); /* => entry after last column */ @@ -768,7 +768,7 @@ quantize_fs_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf, cur = GETJSAMPLE(range_limit[cur]); /* Select output value, accumulate into output code for this pixel */ pixcode = GETJSAMPLE(colorindex_ci[cur]); - *output_ptr = (JSAMPLE) (*output_ptr + (JSAMPLE) pixcode); + *output_ptr = (JSAMPLE)(*output_ptr + (JSAMPLE) pixcode); /* Compute actual representation error at this pixel */ /* Note: we can do this even though we don't have the final */ /* pixel code, because the colormap is orthogonal. */