00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #define JPEG_INTERNALS
00015 #include "jinclude.h"
00016 #include "jpeglib.h"
00017
00018
00019
00020 LOCAL(void) transencode_master_selection
00021 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
00022 LOCAL(void) transencode_coef_controller
00023 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 GLOBAL(void)
00039 jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
00040 {
00041 if (cinfo->global_state != CSTATE_START)
00042 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00043
00044 jpeg_suppress_tables(cinfo, FALSE);
00045
00046 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
00047 (*cinfo->dest->init_destination) (cinfo);
00048
00049 transencode_master_selection(cinfo, coef_arrays);
00050
00051 cinfo->next_scanline = 0;
00052 cinfo->global_state = CSTATE_WRCOEFS;
00053 }
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 GLOBAL(void)
00064 jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
00065 j_compress_ptr dstinfo)
00066 {
00067 JQUANT_TBL ** qtblptr;
00068 jpeg_component_info *incomp, *outcomp;
00069 JQUANT_TBL *c_quant, *slot_quant;
00070 int tblno, ci, coefi;
00071
00072
00073 if (dstinfo->global_state != CSTATE_START)
00074 ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
00075
00076 dstinfo->image_width = srcinfo->image_width;
00077 dstinfo->image_height = srcinfo->image_height;
00078 dstinfo->input_components = srcinfo->num_components;
00079 dstinfo->in_color_space = srcinfo->jpeg_color_space;
00080 dstinfo->jpeg_width = srcinfo->output_width;
00081 dstinfo->jpeg_height = srcinfo->output_height;
00082 dstinfo->min_DCT_h_scaled_size = srcinfo->min_DCT_h_scaled_size;
00083 dstinfo->min_DCT_v_scaled_size = srcinfo->min_DCT_v_scaled_size;
00084
00085 jpeg_set_defaults(dstinfo);
00086
00087
00088
00089 jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
00090 dstinfo->data_precision = srcinfo->data_precision;
00091 dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
00092
00093 for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
00094 if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
00095 qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
00096 if (*qtblptr == NULL)
00097 *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
00098 MEMCOPY((*qtblptr)->quantval,
00099 srcinfo->quant_tbl_ptrs[tblno]->quantval,
00100 SIZEOF((*qtblptr)->quantval));
00101 (*qtblptr)->sent_table = FALSE;
00102 }
00103 }
00104
00105
00106
00107 dstinfo->num_components = srcinfo->num_components;
00108 if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
00109 ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
00110 MAX_COMPONENTS);
00111 for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
00112 ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
00113 outcomp->component_id = incomp->component_id;
00114 outcomp->h_samp_factor = incomp->h_samp_factor;
00115 outcomp->v_samp_factor = incomp->v_samp_factor;
00116 outcomp->quant_tbl_no = incomp->quant_tbl_no;
00117
00118
00119
00120
00121 tblno = outcomp->quant_tbl_no;
00122 if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
00123 srcinfo->quant_tbl_ptrs[tblno] == NULL)
00124 ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
00125 slot_quant = srcinfo->quant_tbl_ptrs[tblno];
00126 c_quant = incomp->quant_table;
00127 if (c_quant != NULL) {
00128 for (coefi = 0; coefi < DCTSIZE2; coefi++) {
00129 if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
00130 ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
00131 }
00132 }
00133
00134
00135
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145 if (srcinfo->saw_JFIF_marker) {
00146 if (srcinfo->JFIF_major_version == 1) {
00147 dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
00148 dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
00149 }
00150 dstinfo->density_unit = srcinfo->density_unit;
00151 dstinfo->X_density = srcinfo->X_density;
00152 dstinfo->Y_density = srcinfo->Y_density;
00153 }
00154 }
00155
00156
00157
00158
00159
00160
00161
00162 LOCAL(void)
00163 transencode_master_selection (j_compress_ptr cinfo,
00164 jvirt_barray_ptr * coef_arrays)
00165 {
00166
00167 jinit_c_master_control(cinfo, TRUE );
00168
00169
00170 if (cinfo->arith_code)
00171 jinit_arith_encoder(cinfo);
00172 else {
00173 jinit_huff_encoder(cinfo);
00174 }
00175
00176
00177 transencode_coef_controller(cinfo, coef_arrays);
00178
00179 jinit_marker_writer(cinfo);
00180
00181
00182 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
00183
00184
00185
00186
00187
00188 (*cinfo->marker->write_file_header) (cinfo);
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 typedef struct {
00203 struct jpeg_c_coef_controller pub;
00204
00205 JDIMENSION iMCU_row_num;
00206 JDIMENSION mcu_ctr;
00207 int MCU_vert_offset;
00208 int MCU_rows_per_iMCU_row;
00209
00210
00211 jvirt_barray_ptr * whole_image;
00212
00213
00214 JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
00215 } my_coef_controller;
00216
00217 typedef my_coef_controller * my_coef_ptr;
00218
00219
00220 LOCAL(void)
00221 start_iMCU_row (j_compress_ptr cinfo)
00222
00223 {
00224 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00225
00226
00227
00228
00229
00230 if (cinfo->comps_in_scan > 1) {
00231 coef->MCU_rows_per_iMCU_row = 1;
00232 } else {
00233 if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
00234 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
00235 else
00236 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
00237 }
00238
00239 coef->mcu_ctr = 0;
00240 coef->MCU_vert_offset = 0;
00241 }
00242
00243
00244
00245
00246
00247
00248 METHODDEF(void)
00249 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
00250 {
00251 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00252
00253 if (pass_mode != JBUF_CRANK_DEST)
00254 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00255
00256 coef->iMCU_row_num = 0;
00257 start_iMCU_row(cinfo);
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 METHODDEF(boolean)
00272 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
00273 {
00274 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00275 JDIMENSION MCU_col_num;
00276 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
00277 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
00278 int blkn, ci, xindex, yindex, yoffset, blockcnt;
00279 JDIMENSION start_col;
00280 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
00281 JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
00282 JBLOCKROW buffer_ptr;
00283 jpeg_component_info *compptr;
00284
00285
00286 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00287 compptr = cinfo->cur_comp_info[ci];
00288 buffer[ci] = (*cinfo->mem->access_virt_barray)
00289 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
00290 coef->iMCU_row_num * compptr->v_samp_factor,
00291 (JDIMENSION) compptr->v_samp_factor, FALSE);
00292 }
00293
00294
00295 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
00296 yoffset++) {
00297 for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
00298 MCU_col_num++) {
00299
00300 blkn = 0;
00301 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00302 compptr = cinfo->cur_comp_info[ci];
00303 start_col = MCU_col_num * compptr->MCU_width;
00304 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
00305 : compptr->last_col_width;
00306 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
00307 if (coef->iMCU_row_num < last_iMCU_row ||
00308 yindex+yoffset < compptr->last_row_height) {
00309
00310 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
00311 for (xindex = 0; xindex < blockcnt; xindex++)
00312 MCU_buffer[blkn++] = buffer_ptr++;
00313 } else {
00314
00315 xindex = 0;
00316 }
00317
00318
00319
00320
00321
00322
00323 for (; xindex < compptr->MCU_width; xindex++) {
00324 MCU_buffer[blkn] = coef->dummy_buffer[blkn];
00325 MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
00326 blkn++;
00327 }
00328 }
00329 }
00330
00331 if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
00332
00333 coef->MCU_vert_offset = yoffset;
00334 coef->mcu_ctr = MCU_col_num;
00335 return FALSE;
00336 }
00337 }
00338
00339 coef->mcu_ctr = 0;
00340 }
00341
00342 coef->iMCU_row_num++;
00343 start_iMCU_row(cinfo);
00344 return TRUE;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 LOCAL(void)
00357 transencode_coef_controller (j_compress_ptr cinfo,
00358 jvirt_barray_ptr * coef_arrays)
00359 {
00360 my_coef_ptr coef;
00361 JBLOCKROW buffer;
00362 int i;
00363
00364 coef = (my_coef_ptr)
00365 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00366 SIZEOF(my_coef_controller));
00367 cinfo->coef = (struct jpeg_c_coef_controller *) coef;
00368 coef->pub.start_pass = start_pass_coef;
00369 coef->pub.compress_data = compress_output;
00370
00371
00372 coef->whole_image = coef_arrays;
00373
00374
00375 buffer = (JBLOCKROW)
00376 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00377 C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
00378 jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
00379 for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
00380 coef->dummy_buffer[i] = buffer + i;
00381 }
00382 }