initial_setup (j_decompress_ptr cinfo) /* Called once, when first SOS marker is reached */ { int ci; jpeg_component_info *compptr; /* Make sure image isn't bigger than I can handle */ if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION || (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION) ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); /* For now, precision must match compiled-in value... */ if (cinfo->data_precision != BITS_IN_JSAMPLE) ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); /* Check that number of components won't exceed internal array sizes */ if (cinfo->num_components > MAX_COMPONENTS) ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, MAX_COMPONENTS); /* Compute maximum sampling factors; check factor validity */ cinfo->max_h_samp_factor = 1; cinfo->max_v_samp_factor = 1; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) ERREXIT(cinfo, JERR_BAD_SAMPLING); cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, compptr->h_samp_factor); cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, compptr->v_samp_factor); } /* Derive block_size, natural_order, and lim_Se */ if (cinfo->is_baseline || (cinfo->progressive_mode && cinfo->comps_in_scan)) { /* no pseudo SOS marker */ cinfo->block_size = DCTSIZE; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; } else switch (cinfo->Se) { case (1*1-1): cinfo->block_size = 1; cinfo->natural_order = jpeg_natural_order; /* not needed */ cinfo->lim_Se = cinfo->Se; break; case (2*2-1): cinfo->block_size = 2; cinfo->natural_order = jpeg_natural_order2; cinfo->lim_Se = cinfo->Se; break; case (3*3-1): cinfo->block_size = 3; cinfo->natural_order = jpeg_natural_order3; cinfo->lim_Se = cinfo->Se; break; case (4*4-1): cinfo->block_size = 4; cinfo->natural_order = jpeg_natural_order4; cinfo->lim_Se = cinfo->Se; break; case (5*5-1): cinfo->block_size = 5; cinfo->natural_order = jpeg_natural_order5; cinfo->lim_Se = cinfo->Se; break; case (6*6-1): cinfo->block_size = 6; cinfo->natural_order = jpeg_natural_order6; cinfo->lim_Se = cinfo->Se; break; case (7*7-1): cinfo->block_size = 7; cinfo->natural_order = jpeg_natural_order7; cinfo->lim_Se = cinfo->Se; break; case (8*8-1): cinfo->block_size = 8; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; break; case (9*9-1): cinfo->block_size = 9; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; break; case (10*10-1): cinfo->block_size = 10; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; break; case (11*11-1): cinfo->block_size = 11; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; break; case (12*12-1): cinfo->block_size = 12; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; break; case (13*13-1): cinfo->block_size = 13; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; break; case (14*14-1): cinfo->block_size = 14; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; break; case (15*15-1): cinfo->block_size = 15; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; break; case (16*16-1): cinfo->block_size = 16; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; break; default: ERREXIT4(cinfo, JERR_BAD_PROGRESSION, cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); break; } /* We initialize DCT_scaled_size and min_DCT_scaled_size to block_size. * In the full decompressor, * this will be overridden by jpeg_calc_output_dimensions in jdmaster.c; * but in the transcoder, * jpeg_calc_output_dimensions is not used, so we must do it here. */ cinfo->min_DCT_h_scaled_size = cinfo->block_size; cinfo->min_DCT_v_scaled_size = cinfo->block_size; /* Compute dimensions of components */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { compptr->DCT_h_scaled_size = cinfo->block_size; compptr->DCT_v_scaled_size = cinfo->block_size; /* Size in DCT blocks */ compptr->width_in_blocks = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, (long) (cinfo->max_h_samp_factor * cinfo->block_size)); compptr->height_in_blocks = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, (long) (cinfo->max_v_samp_factor * cinfo->block_size)); /* downsampled_width and downsampled_height will also be overridden by * jdmaster.c if we are doing full decompression. The transcoder library * doesn't use these values, but the calling application might. */ /* Size in samples */ compptr->downsampled_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, (long) cinfo->max_h_samp_factor); compptr->downsampled_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, (long) cinfo->max_v_samp_factor); /* Mark component needed, until color conversion says otherwise */ compptr->component_needed = TRUE; /* Mark no quantization table yet saved for component */ compptr->quant_table = NULL; } /* Compute number of fully interleaved MCU rows. */ cinfo->total_iMCU_rows = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, (long) (cinfo->max_v_samp_factor * cinfo->block_size)); /* Decide whether file contains multiple scans */ if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode) cinfo->inputctl->has_multiple_scans = TRUE; else cinfo->inputctl->has_multiple_scans = FALSE; }
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; is_DC_band = (cinfo->Ss == 0); /* Validate scan parameters */ bad = FALSE; if (is_DC_band) { if (cinfo->Se != 0) bad = TRUE; } else { /* need not check Ss/Se < 0 since they came from unsigned bytes */ if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2) bad = TRUE; /* AC scans may have only one component */ if (cinfo->comps_in_scan != 1) bad = TRUE; } if (cinfo->Ah != 0) { /* Successive approximation refinement scan: must have Al = Ah-1. */ if (cinfo->Al != cinfo->Ah-1) bad = TRUE; } 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) ERREXIT4(cinfo, JERR_BAD_PROGRESSION, 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++) { 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 */ WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); 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); coef_bit_ptr[coefi] = cinfo->Al; } } /* Select MCU decoding routine */ 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) 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++) { 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]); } } else { tbl = compptr->ac_tbl_no; jpeg_make_d_derived_tbl(cinfo, FALSE, 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; } /* Initialize bitread state variables */ entropy->bitstate.bits_left = 0; entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ entropy->pub.insufficient_data = FALSE; /* Initialize private state variables */ entropy->saved.EOBRUN = 0; /* Initialize restart counter */ entropy->restarts_to_go = cinfo->restart_interval; }
METHODDEF void 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; is_DC_band = (cinfo->Ss == 0); /* Validate scan parameters */ bad = FALSE; if (is_DC_band) { if (cinfo->Se != 0) bad = TRUE; } else { /* need not check Ss/Se < 0 since they came from unsigned bytes */ if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2) bad = TRUE; /* AC scans may have only one component */ if (cinfo->comps_in_scan != 1) bad = TRUE; } if (cinfo->Ah != 0) { /* Successive approximation refinement scan: must have Al = Ah-1. */ if (cinfo->Al != cinfo->Ah-1) bad = TRUE; } if (cinfo->Al > 13) /* need not check for < 0 */ bad = TRUE; if (bad) ERREXIT4(cinfo, JERR_BAD_PROGRESSION, 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++) { 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 */ WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); 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); coef_bit_ptr[coefi] = cinfo->Al; } } /* Select MCU decoding routine */ 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) 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++) { 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; if (tbl < 0 || tbl >= NUM_HUFF_TBLS || cinfo->dc_huff_tbl_ptrs[tbl] == NULL) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); jpeg_make_d_derived_tbl(cinfo, cinfo->dc_huff_tbl_ptrs[tbl], & entropy->derived_tbls[tbl]); } } else { tbl = compptr->ac_tbl_no; if (tbl < 0 || tbl >= NUM_HUFF_TBLS || cinfo->ac_huff_tbl_ptrs[tbl] == NULL) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); jpeg_make_d_derived_tbl(cinfo, cinfo->ac_huff_tbl_ptrs[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; } /* Initialize bitread state variables */ entropy->bitstate.bits_left = 0; entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ entropy->bitstate.printed_eod = FALSE; /* Initialize private state variables */ entropy->saved.EOBRUN = 0; /* Initialize restart counter */ entropy->restarts_to_go = cinfo->restart_interval; }