jpeg_init_read_tile_scanline(j_decompress_ptr cinfo, huffman_index *index, int *start_x, int *start_y, int *width, int *height) { int lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE; int lines_per_iMCU_col = cinfo->max_h_samp_factor * DCTSIZE; int row_offset = *start_y / lines_per_iMCU_row; int col_left_boundary = ((*start_x / lines_per_iMCU_col) / index->MCU_sample_size) * index->MCU_sample_size; int col_right_boundary = jdiv_round_up(*start_x + *width, lines_per_iMCU_col); cinfo->coef->MCU_columns_to_skip = *start_x / lines_per_iMCU_col - col_left_boundary; *height = (*start_y - row_offset * lines_per_iMCU_row) + *height; *start_x = col_left_boundary * lines_per_iMCU_col; *start_y = row_offset * lines_per_iMCU_row; cinfo->image_width = jmin(cinfo->original_image_width, col_right_boundary * lines_per_iMCU_col) - col_left_boundary * lines_per_iMCU_col; cinfo->input_iMCU_row = row_offset; cinfo->output_iMCU_row = row_offset; jinit_color_deconverter(cinfo); jpeg_calc_output_dimensions(cinfo); jinit_upsampler(cinfo); (*cinfo->master->prepare_for_output_pass) (cinfo); if (cinfo->progressive_mode) (*cinfo->entropy->start_pass) (cinfo); else jpeg_decompress_per_scan_setup(cinfo); int sample_size = DCTSIZE / cinfo->min_DCT_scaled_size; *height = jdiv_round_up(*height, sample_size); *width = cinfo->output_width; cinfo->output_scanline = lines_per_iMCU_row * row_offset / sample_size; cinfo->inputctl->consume_input = cinfo->coef->consume_data; cinfo->inputctl->consume_input_build_huffman_index = cinfo->coef->consume_data_build_huffman_index; cinfo->entropy->index = index; cinfo->input_iMCU_row = row_offset; cinfo->output_iMCU_row = row_offset; cinfo->coef->MCU_column_left_boundary = col_left_boundary; cinfo->coef->MCU_column_right_boundary = col_right_boundary; cinfo->coef->column_left_boundary = col_left_boundary / index->MCU_sample_size; cinfo->coef->column_right_boundary = jdiv_round_up(col_right_boundary, index->MCU_sample_size); }
consume_data_build_huffman_index_baseline(j_decompress_ptr cinfo, huffman_index *index, int current_scan) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; JDIMENSION MCU_col_num; /* index of current MCU within row */ int ci, xindex, yindex, yoffset; JDIMENSION start_col; JBLOCKROW buffer_ptr; huffman_scan_header *scan_header = index->scan + current_scan; scan_header->MCU_rows_per_iMCU_row = coef->MCU_rows_per_iMCU_row; size_t allocate_size = coef->MCU_rows_per_iMCU_row * jdiv_round_up(cinfo->MCUs_per_row, index->MCU_sample_size) * sizeof(huffman_offset_data); scan_header->offset[cinfo->input_iMCU_row] = (huffman_offset_data *) malloc(allocate_size); index->mem_used += allocate_size; huffman_offset_data *offset_data = scan_header->offset[cinfo->input_iMCU_row]; /* Loop to process one whole iMCU row */ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; yoffset++) { for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; MCU_col_num++) { // Record huffman bit offset if (MCU_col_num % index->MCU_sample_size == 0) { (*cinfo->entropy->get_huffman_decoder_configuration) (cinfo, offset_data); ++offset_data; } /* Try to fetch the MCU. */ if (!(*cinfo->entropy->decode_mcu_discard_coef)(cinfo)) { /* Suspension forced; update state counters and exit */ coef->MCU_vert_offset = yoffset; coef->MCU_ctr = MCU_col_num; return JPEG_SUSPENDED; } } /* Completed an MCU row, but perhaps not an iMCU row */ coef->MCU_ctr = 0; } /* Completed the iMCU row, advance counters for next one */ if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { start_iMCU_row(cinfo); return JPEG_ROW_COMPLETED; } /* Completed the scan */ (*cinfo->inputctl->finish_input_pass)(cinfo); return JPEG_SCAN_COMPLETED; }
initial_setup (j_compress_ptr cinfo, boolean transcode_only) /* Do computations that are needed before master selection phase */ { int ci; jpeg_component_info *compptr; long samplesperrow; JDIMENSION jd_samplesperrow; #if JPEG_LIB_VERSION >= 70 #if JPEG_LIB_VERSION >= 80 if (!transcode_only) #endif jpeg_calc_jpeg_dimensions(cinfo); #endif /* Sanity check on image dimensions */ if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 || cinfo->num_components <= 0 || cinfo->input_components <= 0) ERREXIT(cinfo, JERR_EMPTY_IMAGE); /* Make sure image isn't bigger than I can handle */ if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION || (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION) ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); /* Width of an input scanline must be representable as JDIMENSION. */ samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; jd_samplesperrow = (JDIMENSION) samplesperrow; if ((long) jd_samplesperrow != samplesperrow) ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); /* 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); } /* Compute dimensions of components */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Fill in the correct component_index value; don't rely on application */ compptr->component_index = ci; /* For compression, we never do DCT scaling. */ #if JPEG_LIB_VERSION >= 70 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE; #else compptr->DCT_scaled_size = DCTSIZE; #endif /* Size in DCT blocks */ compptr->width_in_blocks = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor, (long) (cinfo->max_h_samp_factor * DCTSIZE)); compptr->height_in_blocks = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor, (long) (cinfo->max_v_samp_factor * DCTSIZE)); /* Size in samples */ compptr->downsampled_width = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor, (long) cinfo->max_h_samp_factor); compptr->downsampled_height = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor, (long) cinfo->max_v_samp_factor); /* Mark component needed (this flag isn't actually used for compression) */ compptr->component_needed = TRUE; } /* Compute number of fully interleaved MCU rows (number of times that * main controller will call coefficient controller). */ cinfo->total_iMCU_rows = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_height, (long) (cinfo->max_v_samp_factor*DCTSIZE)); }
per_scan_setup (j_compress_ptr cinfo) /* Do computations that are needed before processing a JPEG scan */ /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ { int ci, mcublks, tmp; jpeg_component_info *compptr; if (cinfo->comps_in_scan == 1) { /* Noninterleaved (single-component) scan */ compptr = cinfo->cur_comp_info[0]; /* Overall image size in MCUs */ cinfo->MCUs_per_row = compptr->width_in_blocks; cinfo->MCU_rows_in_scan = compptr->height_in_blocks; /* For noninterleaved scan, always one block per MCU */ compptr->MCU_width = 1; compptr->MCU_height = 1; compptr->MCU_blocks = 1; compptr->MCU_sample_width = DCTSIZE; compptr->last_col_width = 1; /* 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 % compptr->v_samp_factor); if (tmp == 0) tmp = compptr->v_samp_factor; compptr->last_row_height = tmp; /* Prepare array describing MCU composition */ cinfo->blocks_in_MCU = 1; cinfo->MCU_membership[0] = 0; } else { /* Interleaved (multi-component) scan */ if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, MAX_COMPS_IN_SCAN); /* Overall image size in MCUs */ cinfo->MCUs_per_row = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_width, (long) (cinfo->max_h_samp_factor*DCTSIZE)); cinfo->MCU_rows_in_scan = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_height, (long) (cinfo->max_v_samp_factor*DCTSIZE)); cinfo->blocks_in_MCU = 0; for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; /* Sampling factors give # of blocks of component in each MCU */ compptr->MCU_width = compptr->h_samp_factor; compptr->MCU_height = compptr->v_samp_factor; compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE; /* Figure number of non-dummy blocks in last MCU column & row */ tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); if (tmp == 0) tmp = compptr->MCU_width; compptr->last_col_width = tmp; tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); if (tmp == 0) tmp = compptr->MCU_height; compptr->last_row_height = tmp; /* Prepare array describing MCU composition */ mcublks = compptr->MCU_blocks; if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) ERREXIT(cinfo, JERR_BAD_MCU_SIZE); while (mcublks-- > 0) { cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; } } } /* Convert restart specified in rows to actual MCU count. */ /* Note that count must fit in 16 bits, so we provide limiting. */ if (cinfo->restart_in_rows > 0) { long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row; cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L); } }
initial_setup (j_decompress_ptr cinfo) /* Called once, when first SOS marker is reached */ { int ci; jpeg_component_info *compptr; 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); } /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE. * In the full decompressor, this will be overridden by jdmaster.c; * but in the transcoder, jdmaster.c is not used, so we must do it here. */ cinfo->min_DCT_scaled_size = DCTSIZE; /* Compute dimensions of components */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { compptr->DCT_scaled_size = DCTSIZE; /* 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 * DCTSIZE)); compptr->height_in_blocks = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, (long) (cinfo->max_v_samp_factor * DCTSIZE)); /* 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*DCTSIZE)); /* 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; }
per_scan_setup (j_decompress_ptr cinfo) /* Do computations that are needed before processing a JPEG scan */ /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */ { int ci, mcublks, tmp; jpeg_component_info *compptr; if (cinfo->comps_in_scan == 1) { /* Noninterleaved (single-component) scan */ compptr = cinfo->cur_comp_info[0]; /* Overall image size in MCUs */ cinfo->MCUs_per_row = compptr->width_in_blocks; cinfo->MCU_rows_in_scan = compptr->height_in_blocks; /* For noninterleaved scan, always one block per MCU */ compptr->MCU_width = 1; compptr->MCU_height = 1; compptr->MCU_blocks = 1; compptr->MCU_sample_width = compptr->DCT_scaled_size; compptr->last_col_width = 1; /* 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 % compptr->v_samp_factor); if (tmp == 0) tmp = compptr->v_samp_factor; compptr->last_row_height = tmp; /* Prepare array describing MCU composition */ cinfo->blocks_in_MCU = 1; cinfo->MCU_membership[0] = 0; } else { /* Interleaved (multi-component) scan */ if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, MAX_COMPS_IN_SCAN); /* Overall image size in MCUs */ cinfo->MCUs_per_row = (JDIMENSION) jdiv_round_up((long) cinfo->image_width, (long) (cinfo->max_h_samp_factor*DCTSIZE)); cinfo->MCU_rows_in_scan = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, (long) (cinfo->max_v_samp_factor*DCTSIZE)); cinfo->blocks_in_MCU = 0; for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; /* Sampling factors give # of blocks of component in each MCU */ compptr->MCU_width = compptr->h_samp_factor; compptr->MCU_height = compptr->v_samp_factor; compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size; /* Figure number of non-dummy blocks in last MCU column & row */ tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); if (tmp == 0) tmp = compptr->MCU_width; compptr->last_col_width = tmp; tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); if (tmp == 0) tmp = compptr->MCU_height; compptr->last_row_height = tmp; /* Prepare array describing MCU composition */ mcublks = compptr->MCU_blocks; if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU) ERREXIT(cinfo, JERR_BAD_MCU_SIZE); while (mcublks-- > 0) { cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; } } } }
GLOBAL void jpeg_calc_output_dimensions (j_decompress_ptr cinfo) /* Do computations that are needed before master selection phase */ { #if 0 // JDC: commented out to remove warning 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); #ifdef IDCT_SCALING_SUPPORTED /* Compute actual output image dimensions and DCT scaling choices. */ if (cinfo->scale_num * 8 <= cinfo->scale_denom) { /* Provide 1/8 scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width, 8L); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 8L); cinfo->min_DCT_scaled_size = 1; } 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); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 4L); cinfo->min_DCT_scaled_size = 2; } 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); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 2L); cinfo->min_DCT_scaled_size = 4; } 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++) { 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)) { 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++) { /* 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: #if RGB_PIXELSIZE != 3 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; } 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; }
jpeg_core_output_dimensions (j_decompress_ptr cinfo) /* Do computations that are needed before master selection phase. * This function is used for transcoding and full decompression. */ { #ifdef IDCT_SCALING_SUPPORTED int ci; jpeg_component_info *compptr; /* Compute actual output image dimensions and DCT scaling choices. */ if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) { /* Provide 1/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 1; cinfo->min_DCT_v_scaled_size = 1; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) { /* Provide 2/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 2; cinfo->min_DCT_v_scaled_size = 2; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 3) { /* Provide 3/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 3L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 3L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 3; cinfo->min_DCT_v_scaled_size = 3; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) { /* Provide 4/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 4; cinfo->min_DCT_v_scaled_size = 4; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 5) { /* Provide 5/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 5L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 5L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 5; cinfo->min_DCT_v_scaled_size = 5; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 6) { /* Provide 6/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 6L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 6L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 6; cinfo->min_DCT_v_scaled_size = 6; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 7) { /* Provide 7/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 7L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 7L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 7; cinfo->min_DCT_v_scaled_size = 7; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) { /* Provide 8/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 8; cinfo->min_DCT_v_scaled_size = 8; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 9) { /* Provide 9/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 9L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 9L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 9; cinfo->min_DCT_v_scaled_size = 9; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 10) { /* Provide 10/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 10L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 10L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 10; cinfo->min_DCT_v_scaled_size = 10; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 11) { /* Provide 11/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 11L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 11L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 11; cinfo->min_DCT_v_scaled_size = 11; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 12) { /* Provide 12/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 12L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 12L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 12; cinfo->min_DCT_v_scaled_size = 12; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 13) { /* Provide 13/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 13L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 13L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 13; cinfo->min_DCT_v_scaled_size = 13; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 14) { /* Provide 14/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 14L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 14L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 14; cinfo->min_DCT_v_scaled_size = 14; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 15) { /* Provide 15/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 15L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 15L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 15; cinfo->min_DCT_v_scaled_size = 15; } else { /* Provide 16/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 16L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 16L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 16; cinfo->min_DCT_v_scaled_size = 16; } /* Recompute dimensions of components */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size; compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size; } #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, * and has computed unscaled downsampled_width and downsampled_height. */ #endif /* IDCT_SCALING_SUPPORTED */ }
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; }
jpeg_crop_scanline (j_decompress_ptr cinfo, JDIMENSION *xoffset, JDIMENSION *width) { int ci, align, orig_downsampled_width; JDIMENSION input_xoffset; boolean reinit_upsampler = FALSE; jpeg_component_info *compptr; if (cinfo->global_state != DSTATE_SCANNING || cinfo->output_scanline != 0) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (!xoffset || !width) ERREXIT(cinfo, JERR_BAD_CROP_SPEC); /* xoffset and width must fall within the output image dimensions. */ if (*width == 0 || *xoffset + *width > cinfo->output_width) ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); /* No need to do anything if the caller wants the entire width. */ if (*width == cinfo->output_width) return; /* Ensuring the proper alignment of xoffset is tricky. At minimum, it * must align with an MCU boundary, because: * * (1) The IDCT is performed in blocks, and it is not feasible to modify * the algorithm so that it can transform partial blocks. * (2) Because of the SIMD extensions, any input buffer passed to the * upsampling and color conversion routines must be aligned to the * SIMD word size (for instance, 128-bit in the case of SSE2.) The * easiest way to accomplish this without copying data is to ensure * that upsampling and color conversion begin at the start of the * first MCU column that will be inverse transformed. * * In practice, we actually impose a stricter alignment requirement. We * require that xoffset be a multiple of the maximum MCU column width of all * of the components (the "iMCU column width.") This is to simplify the * single-pass decompression case, allowing us to use the same MCU column * width for all of the components. */ align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor; /* Adjust xoffset to the nearest iMCU boundary <= the requested value */ input_xoffset = *xoffset; *xoffset = (input_xoffset / align) * align; /* Adjust the width so that the right edge of the output image is as * requested (only the left edge is altered.) It is important that calling * programs check this value after this function returns, so that they can * allocate an output buffer with the appropriate size. */ *width = *width + input_xoffset - *xoffset; cinfo->output_width = *width; /* Set the first and last iMCU columns that we must decompress. These values * will be used in single-scan decompressions. */ cinfo->master->first_iMCU_col = (JDIMENSION) (long) (*xoffset) / (long) align; cinfo->master->last_iMCU_col = (JDIMENSION) jdiv_round_up((long) (*xoffset + cinfo->output_width), (long) align) - 1; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Set downsampled_width to the new output width. */ orig_downsampled_width = compptr->downsampled_width; compptr->downsampled_width = (JDIMENSION) jdiv_round_up((long) (cinfo->output_width * compptr->h_samp_factor), (long) cinfo->max_h_samp_factor); if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2) reinit_upsampler = TRUE; /* Set the first and last iMCU columns that we must decompress. These * values will be used in multi-scan decompressions. */ cinfo->master->first_MCU_col[ci] = (JDIMENSION) (long) (*xoffset * compptr->h_samp_factor) / (long) align; cinfo->master->last_MCU_col[ci] = (JDIMENSION) jdiv_round_up((long) ((*xoffset + cinfo->output_width) * compptr->h_samp_factor), (long) align) - 1; } if (reinit_upsampler) { cinfo->master->jinit_upsampler_no_alloc = TRUE; jinit_upsampler(cinfo); cinfo->master->jinit_upsampler_no_alloc = FALSE; } }
consume_data_build_huffman_index_progressive(j_decompress_ptr cinfo, huffman_index *index, int current_scan) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; JDIMENSION MCU_col_num; /* index of current MCU within row */ int blkn, ci, xindex, yindex, yoffset; JDIMENSION start_col; JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; JBLOCKROW buffer_ptr; jpeg_component_info *compptr; int factor = 4; // maximum factor is 4. for (ci = 0; ci < cinfo->comps_in_scan; ci++) factor = jmin(factor, cinfo->cur_comp_info[ci]->h_samp_factor); int sample_size = index->MCU_sample_size * factor; huffman_scan_header *scan_header = index->scan + current_scan; scan_header->MCU_rows_per_iMCU_row = coef->MCU_rows_per_iMCU_row; scan_header->MCUs_per_row = jdiv_round_up(cinfo->MCUs_per_row, sample_size); scan_header->comps_in_scan = cinfo->comps_in_scan; size_t allocate_size = coef->MCU_rows_per_iMCU_row * scan_header->MCUs_per_row * sizeof(huffman_offset_data); scan_header->offset[cinfo->input_iMCU_row] = (huffman_offset_data *) malloc(allocate_size); index->mem_used += allocate_size; huffman_offset_data *offset_data = scan_header->offset[cinfo->input_iMCU_row]; /* Align the virtual buffers for the components used in this scan. */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; buffer[ci] = (*cinfo->mem->access_virt_barray) ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], 0, // Only need one row buffer (JDIMENSION) compptr->v_samp_factor, TRUE); } /* Loop to process one whole iMCU row */ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; yoffset++) { for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; MCU_col_num++) { /* For each MCU, we loop through different color components. * Then, for each color component we will get a list of pointers to DCT * blocks in the virtual buffer. */ blkn = 0; /* index of current DCT block within MCU */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; start_col = MCU_col_num * compptr->MCU_width; /* Get the list of pointers to DCT blocks in * the virtual buffer in a color component of the MCU. */ for (yindex = 0; yindex < compptr->MCU_height; yindex++) { buffer_ptr = buffer[ci][yindex + yoffset] + start_col; for (xindex = 0; xindex < compptr->MCU_width; xindex++) { coef->MCU_buffer[blkn++] = buffer_ptr++; if (cinfo->input_scan_number == 0) { // need to do pre-zero by ourself. jzero_far((void FAR *) coef->MCU_buffer[blkn - 1], (size_t)(SIZEOF(JBLOCK))); } } } } // Record huffman bit offset if (MCU_col_num % sample_size == 0) { (*cinfo->entropy->get_huffman_decoder_configuration) (cinfo, offset_data); ++offset_data; } /* Try to fetch the MCU. */ if (!(*cinfo->entropy->decode_mcu)(cinfo, coef->MCU_buffer)) { /* Suspension forced; update state counters and exit */ coef->MCU_vert_offset = yoffset; coef->MCU_ctr = MCU_col_num; return JPEG_SUSPENDED; } } /* Completed an MCU row, but perhaps not an iMCU row */ coef->MCU_ctr = 0; } (*cinfo->entropy->get_huffman_decoder_configuration) (cinfo, &scan_header->prev_MCU_offset); /* Completed the iMCU row, advance counters for next one */ if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { start_iMCU_row(cinfo); return JPEG_ROW_COMPLETED; } /* Completed the scan */ (*cinfo->inputctl->finish_input_pass)(cinfo); return JPEG_SCAN_COMPLETED; }
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; }
jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo) /* Do computations that are needed before master selection phase */ { #ifdef DCT_SCALING_SUPPORTED /* Compute actual JPEG image dimensions and DCT scaling choices. */ if (cinfo->scale_num >= cinfo->scale_denom * 8) { /* Provide 8/1 scaling */ cinfo->jpeg_width = cinfo->image_width << 3; cinfo->jpeg_height = cinfo->image_height << 3; cinfo->min_DCT_h_scaled_size = 1; cinfo->min_DCT_v_scaled_size = 1; } else if (cinfo->scale_num >= cinfo->scale_denom * 4) { /* Provide 4/1 scaling */ cinfo->jpeg_width = cinfo->image_width << 2; cinfo->jpeg_height = cinfo->image_height << 2; cinfo->min_DCT_h_scaled_size = 2; cinfo->min_DCT_v_scaled_size = 2; } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 8) { /* Provide 8/3 scaling */ cinfo->jpeg_width = (cinfo->image_width << 1) + (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 2, 3L); cinfo->jpeg_height = (cinfo->image_height << 1) + (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 2, 3L); cinfo->min_DCT_h_scaled_size = 3; cinfo->min_DCT_v_scaled_size = 3; } else if (cinfo->scale_num >= cinfo->scale_denom * 2) { /* Provide 2/1 scaling */ cinfo->jpeg_width = cinfo->image_width << 1; cinfo->jpeg_height = cinfo->image_height << 1; cinfo->min_DCT_h_scaled_size = 4; cinfo->min_DCT_v_scaled_size = 4; } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 8) { /* Provide 8/5 scaling */ cinfo->jpeg_width = cinfo->image_width + (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 3, 5L); cinfo->jpeg_height = cinfo->image_height + (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 3, 5L); cinfo->min_DCT_h_scaled_size = 5; cinfo->min_DCT_v_scaled_size = 5; } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 4) { /* Provide 4/3 scaling */ cinfo->jpeg_width = cinfo->image_width + (JDIMENSION) jdiv_round_up((long) cinfo->image_width, 3L); cinfo->jpeg_height = cinfo->image_height + (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 3L); cinfo->min_DCT_h_scaled_size = 6; cinfo->min_DCT_v_scaled_size = 6; } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 8) { /* Provide 8/7 scaling */ cinfo->jpeg_width = cinfo->image_width + (JDIMENSION) jdiv_round_up((long) cinfo->image_width, 7L); cinfo->jpeg_height = cinfo->image_height + (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 7L); cinfo->min_DCT_h_scaled_size = 7; cinfo->min_DCT_v_scaled_size = 7; } else if (cinfo->scale_num >= cinfo->scale_denom) { /* Provide 1/1 scaling */ cinfo->jpeg_width = cinfo->image_width; cinfo->jpeg_height = cinfo->image_height; cinfo->min_DCT_h_scaled_size = 8; cinfo->min_DCT_v_scaled_size = 8; } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * 8) { /* Provide 8/9 scaling */ cinfo->jpeg_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 8, 9L); cinfo->jpeg_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 8, 9L); cinfo->min_DCT_h_scaled_size = 9; cinfo->min_DCT_v_scaled_size = 9; } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 4) { /* Provide 4/5 scaling */ cinfo->jpeg_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 4, 5L); cinfo->jpeg_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 4, 5L); cinfo->min_DCT_h_scaled_size = 10; cinfo->min_DCT_v_scaled_size = 10; } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * 8) { /* Provide 8/11 scaling */ cinfo->jpeg_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 8, 11L); cinfo->jpeg_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 8, 11L); cinfo->min_DCT_h_scaled_size = 11; cinfo->min_DCT_v_scaled_size = 11; } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 2) { /* Provide 2/3 scaling */ cinfo->jpeg_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 2, 3L); cinfo->jpeg_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 2, 3L); cinfo->min_DCT_h_scaled_size = 12; cinfo->min_DCT_v_scaled_size = 12; } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * 8) { /* Provide 8/13 scaling */ cinfo->jpeg_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 8, 13L); cinfo->jpeg_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 8, 13L); cinfo->min_DCT_h_scaled_size = 13; cinfo->min_DCT_v_scaled_size = 13; } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 4) { /* Provide 4/7 scaling */ cinfo->jpeg_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 4, 7L); cinfo->jpeg_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 4, 7L); cinfo->min_DCT_h_scaled_size = 14; cinfo->min_DCT_v_scaled_size = 14; } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * 8) { /* Provide 8/15 scaling */ cinfo->jpeg_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 8, 15L); cinfo->jpeg_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 8, 15L); cinfo->min_DCT_h_scaled_size = 15; cinfo->min_DCT_v_scaled_size = 15; } else { /* Provide 1/2 scaling */ cinfo->jpeg_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width, 2L); cinfo->jpeg_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 2L); cinfo->min_DCT_h_scaled_size = 16; cinfo->min_DCT_v_scaled_size = 16; } #else /* !DCT_SCALING_SUPPORTED */ /* Hardwire it to "no scaling" */ cinfo->jpeg_width = cinfo->image_width; cinfo->jpeg_height = cinfo->image_height; cinfo->min_DCT_h_scaled_size = DCTSIZE; cinfo->min_DCT_v_scaled_size = DCTSIZE; #endif /* DCT_SCALING_SUPPORTED */ }
initial_setup (j_compress_ptr cinfo, boolean transcode_only) /* Do computations that are needed before master selection phase */ { int ci, ssize; jpeg_component_info *compptr; long samplesperrow; JDIMENSION jd_samplesperrow; if (transcode_only) jpeg_calc_trans_dimensions(cinfo); else jpeg_calc_jpeg_dimensions(cinfo); /* Sanity check on image dimensions */ if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 || cinfo->num_components <= 0 || cinfo->input_components <= 0) ERREXIT(cinfo, JERR_EMPTY_IMAGE); /* Make sure image isn't bigger than I can handle */ if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION || (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION) ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); /* Width of an input scanline must be representable as JDIMENSION. */ samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; jd_samplesperrow = (JDIMENSION) samplesperrow; if ((long) jd_samplesperrow != samplesperrow) ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); /* 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); } /* Compute dimensions of components */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Fill in the correct component_index value; don't rely on application */ compptr->component_index = ci; /* In selecting the actual DCT scaling for each component, we try to * scale down the chroma components via DCT scaling rather than downsampling. * This saves time if the downsampler gets to use 1:1 scaling. * Note this code adapts subsampling ratios which are powers of 2. */ ssize = 1; #ifdef DCT_SCALING_SUPPORTED while (cinfo->min_DCT_h_scaled_size * ssize <= (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) { ssize = ssize * 2; } #endif compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; ssize = 1; #ifdef DCT_SCALING_SUPPORTED while (cinfo->min_DCT_v_scaled_size * ssize <= (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { ssize = ssize * 2; } #endif compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; /* We don't support DCT 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; /* Size in DCT blocks */ compptr->width_in_blocks = (JDIMENSION) jdiv_round_up((long) cinfo->jpeg_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->jpeg_height * (long) compptr->v_samp_factor, (long) (cinfo->max_v_samp_factor * cinfo->block_size)); /* Size in samples */ compptr->downsampled_width = (JDIMENSION) jdiv_round_up((long) cinfo->jpeg_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->jpeg_height * (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size), (long) (cinfo->max_v_samp_factor * cinfo->block_size)); /* Mark component needed (this flag isn't actually used for compression) */ compptr->component_needed = TRUE; } /* Compute number of fully interleaved MCU rows (number of times that * main controller will call coefficient controller). */ cinfo->total_iMCU_rows = (JDIMENSION) jdiv_round_up((long) cinfo->jpeg_height, (long) (cinfo->max_v_samp_factor * cinfo->block_size)); }
calc_output_dimensions (j_decompress_ptr cinfo) { #ifdef IDCT_SCALING_SUPPORTED int ci; jpeg_component_info *compptr; /* Compute actual output image dimensions and DCT scaling choices. */ if (cinfo->scale_num * 8 <= cinfo->scale_denom) { /* Provide 1/8 scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width, 8L); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 8L); cinfo->min_codec_data_unit = 1; } 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); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 4L); cinfo->min_codec_data_unit = 2; } 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); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 2L); cinfo->min_codec_data_unit = 4; } else { /* Provide 1/1 scaling */ cinfo->output_width = cinfo->image_width; cinfo->output_height = cinfo->image_height; cinfo->min_codec_data_unit = 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++) { int ssize = cinfo->min_codec_data_unit; while (ssize < DCTSIZE && (compptr->h_samp_factor * ssize * 2 <= cinfo->max_h_samp_factor * cinfo->min_codec_data_unit) && (compptr->v_samp_factor * ssize * 2 <= cinfo->max_v_samp_factor * cinfo->min_codec_data_unit)) { ssize = ssize * 2; } compptr->codec_data_unit = 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++) { /* Size in samples, after IDCT scaling */ compptr->downsampled_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * (long) (compptr->h_samp_factor * compptr->codec_data_unit), (long) (cinfo->max_h_samp_factor * DCTSIZE)); compptr->downsampled_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * (long) (compptr->v_samp_factor * compptr->codec_data_unit), (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 codec_data_unit to DCTSIZE, * and has computed unscaled downsampled_width and downsampled_height. */ #endif /* IDCT_SCALING_SUPPORTED */ }
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; }