jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl, const unsigned int *basic_table, int scale_factor, jboolean force_baseline) /* Define a quantization table equal to the basic_table times * a scale factor (given as a percentage). * If force_baseline is TRUE, the computed quantization table entries * are limited to 1..255 for JPEG baseline compatibility. */ { JQUANT_TBL ** qtblptr = & cinfo->quant_tbl_ptrs[which_tbl]; int i; long temp; /* Safety check to ensure start_compress not called yet. */ if (cinfo->global_state != CSTATE_START) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (*qtblptr == NULL) *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo); for (i = 0; i < DCTSIZE2; i++) { temp = ((long) basic_table[i] * scale_factor + 50L) / 100L; /* limit the values to the valid range */ if (temp <= 0L) temp = 1L; if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */ if (force_baseline && temp > 255L) temp = 255L; /* limit to baseline range if requested */ (*qtblptr)->quantval[i] = (UINT16) temp; } /* Initialize sent_table FALSE so table will be written to JPEG file. */ (*qtblptr)->sent_table = FALSE; }
/** * Sets DQT tables */ static void setupDQTs(JNIEnv *env, j_compress_ptr cinfo, jobjectArray dqts) { JQUANT_TBL *quant_ptr; jint len; int i, j; jobject slot; jint *arrPtr; if (!dqts) { //-- TODO: write warning return; } len = (*env)->GetArrayLength(env, dqts); for (i = 0; i < len; i++) { slot = (*env)->GetObjectArrayElement(env, dqts, i); arrPtr = (*env)->GetPrimitiveArrayCritical(env, slot, NULL); if (cinfo->quant_tbl_ptrs[i] == NULL) { cinfo->quant_tbl_ptrs[i] = jpeg_alloc_quant_table((j_common_ptr)cinfo); } quant_ptr = cinfo->quant_tbl_ptrs[i]; for (j = 0; j < 64; j++) { quant_ptr->quantval[j] = arrPtr[j]; } quant_ptr->sent_table = FALSE; (*env)->ReleasePrimitiveArrayCritical(env, slot, arrPtr, 0); (*env)->DeleteLocalRef(env, slot); } }
jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl, const unsigned int *basic_table, int scale_factor, boolean force_baseline) { JQUANT_TBL ** qtblptr; int i; long temp; if (cinfo->global_state != CSTATE_START) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS) ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl); qtblptr = & cinfo->quant_tbl_ptrs[which_tbl]; if (*qtblptr == NULL) *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo); for (i = 0; i < DCTSIZE2; i++) { temp = ((long) basic_table[i] * scale_factor + 50L) / 100L; if (temp <= 0L) temp = 1L; if (temp > 32767L) temp = 32767L; if (force_baseline && temp > 255L) temp = 255L; (*qtblptr)->quantval[i] = (UINT16) temp; } /* Initialize sent_table FALSE so table will be written to JPEG file. */ (*qtblptr)->sent_table = FALSE; }
JQUANT_TBL * gs_jpeg_alloc_quant_table(stream_DCT_state * st) { if (setjmp(find_jmp_buf(st->data.common->exit_jmpbuf))) { gs_jpeg_log_error(st); return NULL; } return jpeg_alloc_quant_table((j_common_ptr) & st->data.compress->cinfo); }
jpeg_copy_critical_parameters (j_decompress_ptr srcinfo, j_compress_ptr dstinfo) { JQUANT_TBL ** qtblptr; jpeg_component_info *incomp, *outcomp; JQUANT_TBL *c_quant, *slot_quant; int tblno, ci, coefi; /* Safety check to ensure start_compress not called yet. */ if (dstinfo->global_state != CSTATE_START) ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state); /* Copy fundamental image dimensions */ dstinfo->image_width = srcinfo->image_width; dstinfo->image_height = srcinfo->image_height; dstinfo->input_components = srcinfo->num_components; dstinfo->in_color_space = srcinfo->jpeg_color_space; /* Initialize all parameters to default values */ jpeg_set_defaults(dstinfo); /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB. * Fix it to get the right header markers for the image colorspace. */ jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space); dstinfo->data_precision = srcinfo->data_precision; dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling; /* Copy the source's quantization tables. */ for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { if (srcinfo->quant_tbl_ptrs[tblno] != NULL) { qtblptr = & dstinfo->quant_tbl_ptrs[tblno]; if (*qtblptr == NULL) *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo); MEMCOPY((*qtblptr)->quantval, srcinfo->quant_tbl_ptrs[tblno]->quantval, SIZEOF((*qtblptr)->quantval)); (*qtblptr)->sent_table = FALSE; } } /* Copy the source's per-component info. * Note we assume jpeg_set_defaults has allocated the dest comp_info array. */ dstinfo->num_components = srcinfo->num_components; if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS) ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components, MAX_COMPONENTS); for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info; ci < dstinfo->num_components; ci++, incomp++, outcomp++) { outcomp->component_id = incomp->component_id; outcomp->h_samp_factor = incomp->h_samp_factor; outcomp->v_samp_factor = incomp->v_samp_factor; outcomp->quant_tbl_no = incomp->quant_tbl_no; /* Make sure saved quantization table for component matches the qtable * slot. If not, the input file re-used this qtable slot. * IJG encoder currently cannot duplicate this. */ tblno = outcomp->quant_tbl_no; if (tblno < 0 || tblno >= NUM_QUANT_TBLS || srcinfo->quant_tbl_ptrs[tblno] == NULL) ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno); slot_quant = srcinfo->quant_tbl_ptrs[tblno]; c_quant = incomp->quant_table; if (c_quant != NULL) { for (coefi = 0; coefi < DCTSIZE2; coefi++) { if (c_quant->quantval[coefi] != slot_quant->quantval[coefi]) ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno); } } /* Note: we do not copy the source's Huffman table assignments; * instead we rely on jpeg_set_colorspace to have made a suitable choice. */ } /* Also copy JFIF version and resolution information, if available. * Strictly speaking this isn't "critical" info, but it's nearly * always appropriate to copy it if available. In particular, * if the application chooses to copy JFIF 1.02 extension markers from * the source file, we need to copy the version to make sure we don't * emit a file that has 1.02 extensions but a claimed version of 1.01. * We will *not*, however, copy version info from mislabeled "2.01" files. */ if (srcinfo->saw_JFIF_marker) { if (srcinfo->JFIF_major_version == 1) { dstinfo->JFIF_major_version = srcinfo->JFIF_major_version; dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version; } dstinfo->density_unit = srcinfo->density_unit; dstinfo->X_density = srcinfo->X_density; dstinfo->Y_density = srcinfo->Y_density; } }
jpeg_copy_critical_parameters (j_decompress_ptr srcinfo, j_compress_ptr dstinfo) { JQUANT_TBL ** qtblptr; jpeg_component_info *incomp, *outcomp; JQUANT_TBL *c_quant, *slot_quant; int tblno, ci, coefi; /* Safety check to ensure start_compress not called yet. */ if (dstinfo->global_state != CSTATE_START) ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state); /* Copy fundamental image dimensions */ dstinfo->image_width = srcinfo->image_width; dstinfo->image_height = srcinfo->image_height; dstinfo->input_components = srcinfo->num_components; dstinfo->in_color_space = srcinfo->jpeg_color_space; /* Initialize all parameters to default values */ jpeg_set_defaults(dstinfo); /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB. * Fix it to get the right header markers for the image colorspace. */ jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space); dstinfo->data_precision = srcinfo->data_precision; dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling; /* Copy the source's quantization tables. */ for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { if (srcinfo->quant_tbl_ptrs[tblno] != NULL) { qtblptr = & dstinfo->quant_tbl_ptrs[tblno]; if (*qtblptr == NULL) *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo); MEMCOPY((*qtblptr)->quantval, srcinfo->quant_tbl_ptrs[tblno]->quantval, SIZEOF((*qtblptr)->quantval)); (*qtblptr)->sent_table = FALSE; } } /* Copy the source's per-component info. * Note we assume jpeg_set_defaults has allocated the dest comp_info array. */ dstinfo->num_components = srcinfo->num_components; if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS) ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components, MAX_COMPONENTS); for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info; ci < dstinfo->num_components; ci++, incomp++, outcomp++) { outcomp->component_id = incomp->component_id; outcomp->h_samp_factor = incomp->h_samp_factor; outcomp->v_samp_factor = incomp->v_samp_factor; outcomp->quant_tbl_no = incomp->quant_tbl_no; /* Make sure saved quantization table for component matches the qtable * slot. If not, the input file re-used this qtable slot. * IJG encoder currently cannot duplicate this. */ tblno = outcomp->quant_tbl_no; if (tblno < 0 || tblno >= NUM_QUANT_TBLS || srcinfo->quant_tbl_ptrs[tblno] == NULL) ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno); slot_quant = srcinfo->quant_tbl_ptrs[tblno]; c_quant = incomp->quant_table; if (c_quant != NULL) { for (coefi = 0; coefi < DCTSIZE2; coefi++) { if (c_quant->quantval[coefi] != slot_quant->quantval[coefi]) ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno); } } /* Note: we do not copy the source's Huffman table assignments; * instead we rely on jpeg_set_colorspace to have made a suitable choice. */ } }
/* 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_add_quant_table (j_compress_ptr cinfo, int which_tbl, const unsigned int *basic_table, int scale_factor, boolean force_baseline) /* Define a quantization table equal to the basic_table times * a scale factor (given as a percentage). * If force_baseline is TRUE, the computed quantization table entries * are limited to 1..255 for JPEG baseline compatibility. */ { JQUANT_TBL ** qtblptr = & cinfo->quant_tbl_ptrs[_RV_insert_check(0,4,32,29,"jpeg_add_quant_table",which_tbl)]; __boundcheck_metadata_store((void *)(&qtblptr),(void *)((size_t)(&qtblptr)+sizeof(qtblptr)*8-1)); int i; __boundcheck_metadata_store((void *)(&i),(void *)((size_t)(&i)+sizeof(i)*8-1)); long temp; __boundcheck_metadata_store((void *)(&temp),(void *)((size_t)(&temp)+sizeof(temp)*8-1)); /* Safety check to ensure start_compress not called yet. */ if (cinfo->global_state != CSTATE_START) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (*(JQUANT_TBL **)(__boundcheck_ptr_reference(40,8,"jpeg_add_quant_table",(void *)(qtblptr),(void *)(qtblptr))) == NULL) *(JQUANT_TBL **)(__boundcheck_ptr_reference(41,6,"jpeg_add_quant_table",(void *)(qtblptr),(void *)(qtblptr))) = jpeg_alloc_quant_table((j_common_ptr) cinfo); for (i = 0; i < DCTSIZE2; i++) { temp = ((long) (*(const unsigned int *)(__boundcheck_ptr_reference(44,20,"jpeg_add_quant_table",(void *)(&basic_table[0]),(void *)(&basic_table[i])))) * scale_factor + 50L) / 100L; /* limit the values to the valid range */ if (temp <= 0L) temp = 1L; if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */ if (force_baseline && temp > 255L) temp = 255L; /* limit to baseline range if requested */ (*(JQUANT_TBL **)(__boundcheck_ptr_reference(50,7,"jpeg_add_quant_table",(void *)(qtblptr),(void *)(qtblptr))))->quantval[_RV_insert_check(0,64,50,5,"jpeg_add_quant_table",i)] = (UINT16) temp; } /* Initialize sent_table FALSE so table will be written to JPEG file. */ (*(JQUANT_TBL **)(__boundcheck_ptr_reference(54,5,"jpeg_add_quant_table",(void *)(qtblptr),(void *)(qtblptr))))->sent_table = FALSE; }