transdecode_master_selection (j_decompress_ptr cinfo) { /* This is effectively a buffered-image operation. */ cinfo->buffered_image = TRUE; #if JPEG_LIB_VERSION >= 80 /* Compute output image dimensions and related values. */ jpeg_core_output_dimensions(cinfo); #endif /* Entropy decoding: either Huffman or arithmetic coding. */ if (cinfo->arith_code) { #ifdef D_ARITH_CODING_SUPPORTED jinit_arith_decoder(cinfo); #else ERREXIT(cinfo, JERR_ARITH_NOTIMPL); #endif } else { if (cinfo->progressive_mode) { #ifdef D_PROGRESSIVE_SUPPORTED jinit_phuff_decoder(cinfo); #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else jinit_huff_decoder(cinfo); } /* Always get a full-image coefficient buffer. */ jinit_d_coef_controller(cinfo, TRUE); /* We can now tell the memory manager to allocate virtual arrays. */ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); /* Initialize input side of decompressor to consume first scan. */ (*cinfo->inputctl->start_input_pass) (cinfo); /* Initialize progress monitoring. */ if (cinfo->progress != NULL) { int nscans; /* Estimate number of scans to set pass_limit. */ if (cinfo->progressive_mode) { /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ nscans = 2 + 3 * cinfo->num_components; } else if (cinfo->inputctl->has_multiple_scans) { /* For a nonprogressive multiscan file, estimate 1 scan per component. */ nscans = cinfo->num_components; } else { nscans = 1; } cinfo->progress->pass_counter = 0L; cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; cinfo->progress->completed_passes = 0; cinfo->progress->total_passes = 1; } }
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; #endif /* Prevent application from calling me at wrong times */ if (cinfo->global_state != DSTATE_READY) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); /* Compute core output image dimensions and DCT scaling choices. */ jpeg_core_output_dimensions(cinfo); #ifdef IDCT_SCALING_SUPPORTED /* 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 adapts subsampling ratios which are powers of 2. */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { int ssize = cinfo->_min_DCT_scaled_size; while (ssize < DCTSIZE && ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) % (compptr->h_samp_factor * ssize * 2) == 0) && ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) % (compptr->v_samp_factor * ssize * 2) == 0)) { ssize = ssize * 2; } #if JPEG_LIB_VERSION >= 70 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize; #else compptr->DCT_scaled_size = ssize; #endif } /* 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++) { /* 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)); 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)); } #else /* !IDCT_SCALING_SUPPORTED */ /* Hardwire it to "no scaling" */ cinfo->output_width = cinfo->image_width; cinfo->output_height = cinfo->image_height; /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE, * and has computed unscaled downsampled_width and downsampled_height. */ #endif /* IDCT_SCALING_SUPPORTED */ /* 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: case JCS_EXT_RGB: case JCS_EXT_RGBX: case JCS_EXT_BGR: case JCS_EXT_BGRX: case JCS_EXT_XBGR: case JCS_EXT_XRGB: case JCS_EXT_RGBA: case JCS_EXT_BGRA: case JCS_EXT_ABGR: case JCS_EXT_ARGB: cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space]; break; case JCS_YCbCr: case JCS_RGB565: 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); /* See if upsampler will want to emit more than one row at a time */ if (use_merged_upsample(cinfo)) cinfo->rec_outbuf_height = cinfo->max_v_samp_factor; else cinfo->rec_outbuf_height = 1; }
/* TODO: the whole purpose of fastforward_jpeg_stream_state(), (as well as add_huff_table()/add_huff_tables() above), is to wind the state of the jpeg stream forward, since the incoming data is headerless. On hide-sight, it might be cleaner and more future-proof against changes in libjpeg to construct a made-up header, read it then switch data stream. */ static void fastforward_jpeg_stream_state(jpeg_decompress_data * jddp, stream_DCT_state * ss, px_state_t * pxs) { px_vendor_state_t *v_state = pxs->vendor_state; j_decompress_ptr cinfo = &jddp->dinfo; int i; int j, qcount = 0; jpeg_component_info *compptr; int factor = ((v_state->color_space == eGraySub) ? 8 : 1); extern const int jpeg_natural_order[]; if (cinfo->comp_info == NULL) cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, MAX_COMPONENTS * sizeof(jpeg_component_info)); /* default_decompress_parms() starts */ if (v_state->color_space == eGraySub) { cinfo->jpeg_color_space = JCS_GRAYSCALE; cinfo->out_color_space = JCS_GRAYSCALE; } else { cinfo->jpeg_color_space = JCS_YCbCr; cinfo->out_color_space = JCS_YCbCr; } cinfo->scale_num = 8; cinfo->scale_denom = 8; cinfo->output_gamma = 1.0; cinfo->buffered_image = FALSE; cinfo->raw_data_out = FALSE; cinfo->dct_method = JDCT_DEFAULT; cinfo->do_fancy_upsampling = TRUE; cinfo->do_block_smoothing = TRUE; cinfo->quantize_colors = FALSE; /* */ cinfo->dither_mode = JDITHER_FS; /* */ cinfo->two_pass_quantize = 1; /* */ cinfo->desired_number_of_colors = 256; /* */ cinfo->enable_1pass_quant = 0; cinfo->enable_external_quant = 0; cinfo->enable_2pass_quant = FALSE; /* default_decompress_parms() ends */ std_huff_tables(cinfo); /* get_soi begins */ for (i = 0; i < NUM_ARITH_TBLS; i++) { cinfo->arith_dc_L[i] = 0; cinfo->arith_dc_U[i] = 1; cinfo->arith_ac_K[i] = 5; } cinfo->restart_interval = 0; cinfo->CCIR601_sampling = FALSE; /* */ cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */ cinfo->JFIF_minor_version = 1; cinfo->density_unit = 0; /* Pixel size is unknown by default */ cinfo->X_density = 1; /* Pixel aspect ratio is square by default */ cinfo->Y_density = 1; /* get_soi ends */ /* get_sof */ cinfo->is_baseline = 1; cinfo->progressive_mode = FALSE; cinfo->arith_code = FALSE; cinfo->data_precision = BITS_IN_JSAMPLE; if (v_state->color_space == eGraySub) cinfo->num_components = 1; else cinfo->num_components = 3; cinfo->image_width = v_state->SourceWidth; cinfo->image_height = v_state->BlockHeight; for (i = 0; i < cinfo->num_components; i++) { if (cinfo->quant_tbl_ptrs[i] == NULL) cinfo->quant_tbl_ptrs[i] = jpeg_alloc_quant_table((j_common_ptr) cinfo); for (j = 0; j < 64; j++) cinfo->quant_tbl_ptrs[i]->quantval[j] = v_state->qvalues[qcount++]; } #define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \ (compptr = &cinfo->comp_info[index], \ compptr->component_id = (id), \ compptr->h_samp_factor = (hsamp), \ compptr->v_samp_factor = (vsamp), \ compptr->quant_tbl_no = (quant), \ compptr->dc_tbl_no = (dctbl), \ compptr->ac_tbl_no = (actbl), \ compptr->DCT_h_scaled_size = 8, \ compptr->DCT_v_scaled_size = 8, \ compptr->width_in_blocks = cinfo->image_width/factor, \ compptr->height_in_blocks = cinfo->image_height/factor, \ compptr->quant_table = cinfo->quant_tbl_ptrs[index], \ compptr->component_index = index, \ compptr->component_needed = TRUE, \ compptr->downsampled_width = cinfo->image_width, \ compptr->downsampled_height = cinfo->image_height) SET_COMP(0, 1, 1, 1, 0, 0, 0); /* not default! */ if (v_state->color_space != eGraySub) { SET_COMP(1, 2, 1, 1, 1, 1, 1); SET_COMP(2, 3, 1, 1, 1, 1, 1); } /* end_sof */ cinfo->rec_outbuf_height = 1; if (v_state->color_space == eGraySub) { cinfo->out_color_components = 1; cinfo->output_components = 1; } else { cinfo->out_color_components = 3; cinfo->output_components = 3; } cinfo->global_state = DSTATE_READY; /* no SOS markers, so we just do this by hand - mostly from get_sos() */ cinfo->cur_comp_info[0] = &cinfo->comp_info[0]; cinfo->comp_info[0].dc_tbl_no = 0; cinfo->comp_info[0].ac_tbl_no = 0; if (v_state->color_space != eGraySub) { cinfo->cur_comp_info[1] = &cinfo->comp_info[1]; cinfo->comp_info[1].dc_tbl_no = 1; cinfo->comp_info[1].ac_tbl_no = 1; cinfo->cur_comp_info[2] = &cinfo->comp_info[2]; cinfo->comp_info[2].dc_tbl_no = 1; cinfo->comp_info[2].ac_tbl_no = 1; } cinfo->Ss = 0; cinfo->Se = 63; cinfo->Ah = 0; cinfo->Al = 0; /* initial_setup() */ cinfo->input_scan_number = 1; cinfo->block_size = DCTSIZE; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2 - 1; cinfo->min_DCT_h_scaled_size = 8; cinfo->min_DCT_v_scaled_size = 8; cinfo->max_h_samp_factor = 1; cinfo->max_v_samp_factor = 1; if (v_state->color_space == eGraySub) cinfo->comps_in_scan = 1; else cinfo->comps_in_scan = 3; /* */ jpeg_core_output_dimensions(cinfo); cinfo->total_iMCU_rows = 16; ss->phase = 2; /* fast forward to phase 2, on to start_decompress */ }
jpeg_calc_output_dimensions (j_decompress_ptr cinfo) /* Do computations that are needed before master selection phase. * This function is used for full decompression. */ { #ifdef IDCT_SCALING_SUPPORTED int ci; jpeg_component_info *compptr; #endif /* Prevent application from calling me at wrong times */ if (cinfo->global_state != DSTATE_READY) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); /* Compute core output image dimensions and DCT scaling choices. */ jpeg_core_output_dimensions(cinfo); #ifdef IDCT_SCALING_SUPPORTED /* 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 adapts subsampling ratios which are powers of 2. */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { int ssize = 1; while (cinfo->min_DCT_h_scaled_size * ssize <= (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) && (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) { ssize = ssize * 2; } compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; ssize = 1; while (cinfo->min_DCT_v_scaled_size * ssize <= (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) && (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { ssize = ssize * 2; } compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; /* We don't support IDCT ratios larger than 2. */ if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2) compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2; else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2) compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2; } /* 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++) { /* Size in samples, after IDCT scaling */ compptr->downsampled_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size), (long) (cinfo->max_h_samp_factor * cinfo->block_size)); compptr->downsampled_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size), (long) (cinfo->max_v_samp_factor * cinfo->block_size)); } #endif /* IDCT_SCALING_SUPPORTED */ /* 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: cinfo->out_color_components = RGB_PIXELSIZE; 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); /* See if upsampler will want to emit more than one row at a time */ if (use_merged_upsample(cinfo)) cinfo->rec_outbuf_height = cinfo->max_v_samp_factor; else cinfo->rec_outbuf_height = 1; }