1 /*
2 * jctrans.c
3 *
4 * Copyright (C) 1995-1998, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains library routines for transcoding compression,
9 * that is, writing raw DCT coefficient arrays to an output JPEG file.
10 * The routines in jcapimin.c will also be needed by a transcoder.
11 */
12
13 #define JPEG_INTERNALS
14 #include "jinclude.h"
15 #include "jpeglib.h"
16
17
18 /* Forward declarations */
19 LOCAL(void) transencode_master_selection
20 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
21 LOCAL(void) transencode_coef_controller
22 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
23
24
59 * scan script and Huffman optimization) are left in their default states.
60 */
61
62 GLOBAL(void)
63 jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
64 j_compress_ptr dstinfo)
65 {
66 JQUANT_TBL ** qtblptr;
67 jpeg_component_info *incomp, *outcomp;
68 JQUANT_TBL *c_quant, *slot_quant;
69 int tblno, ci, coefi;
70
71 /* Safety check to ensure start_compress not called yet. */
72 if (dstinfo->global_state != CSTATE_START)
73 ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
74 /* Copy fundamental image dimensions */
75 dstinfo->image_width = srcinfo->image_width;
76 dstinfo->image_height = srcinfo->image_height;
77 dstinfo->input_components = srcinfo->num_components;
78 dstinfo->in_color_space = srcinfo->jpeg_color_space;
79 /* Initialize all parameters to default values */
80 jpeg_set_defaults(dstinfo);
81 /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
82 * Fix it to get the right header markers for the image colorspace.
83 */
84 jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
85 dstinfo->data_precision = srcinfo->data_precision;
86 dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
87 /* Copy the source's quantization tables. */
88 for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
89 if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
90 qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
91 if (*qtblptr == NULL)
92 *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
93 MEMCOPY((*qtblptr)->quantval,
94 srcinfo->quant_tbl_ptrs[tblno]->quantval,
95 SIZEOF((*qtblptr)->quantval));
96 (*qtblptr)->sent_table = FALSE;
97 }
98 }
99 /* Copy the source's per-component info.
100 * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
101 */
102 dstinfo->num_components = srcinfo->num_components;
103 if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
104 ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
105 MAX_COMPONENTS);
108 outcomp->component_id = incomp->component_id;
109 outcomp->h_samp_factor = incomp->h_samp_factor;
110 outcomp->v_samp_factor = incomp->v_samp_factor;
111 outcomp->quant_tbl_no = incomp->quant_tbl_no;
112 /* Make sure saved quantization table for component matches the qtable
113 * slot. If not, the input file re-used this qtable slot.
114 * IJG encoder currently cannot duplicate this.
115 */
116 tblno = outcomp->quant_tbl_no;
117 if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
118 srcinfo->quant_tbl_ptrs[tblno] == NULL)
119 ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
120 slot_quant = srcinfo->quant_tbl_ptrs[tblno];
121 c_quant = incomp->quant_table;
122 if (c_quant != NULL) {
123 for (coefi = 0; coefi < DCTSIZE2; coefi++) {
124 if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
125 ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
126 }
127 }
128 /* Note: we do not copy the source's Huffman table assignments;
129 * instead we rely on jpeg_set_colorspace to have made a suitable choice.
130 */
131 }
132 /* Also copy JFIF version and resolution information, if available.
133 * Strictly speaking this isn't "critical" info, but it's nearly
134 * always appropriate to copy it if available. In particular,
135 * if the application chooses to copy JFIF 1.02 extension markers from
136 * the source file, we need to copy the version to make sure we don't
137 * emit a file that has 1.02 extensions but a claimed version of 1.01.
138 * We will *not*, however, copy version info from mislabeled "2.01" files.
139 */
140 if (srcinfo->saw_JFIF_marker) {
141 if (srcinfo->JFIF_major_version == 1) {
142 dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
143 dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
144 }
145 dstinfo->density_unit = srcinfo->density_unit;
146 dstinfo->X_density = srcinfo->X_density;
147 dstinfo->Y_density = srcinfo->Y_density;
148 }
149 }
150
151
152 /*
153 * Master selection of compression modules for transcoding.
154 * This substitutes for jcinit.c's initialization of the full compressor.
155 */
156
157 LOCAL(void)
158 transencode_master_selection (j_compress_ptr cinfo,
159 jvirt_barray_ptr * coef_arrays)
160 {
161 /* Although we don't actually use input_components for transcoding,
162 * jcmaster.c's initial_setup will complain if input_components is 0.
163 */
164 cinfo->input_components = 1;
165 /* Initialize master control (includes parameter checking/processing) */
166 jinit_c_master_control(cinfo, TRUE /* transcode only */);
167
168 /* Entropy encoding: either Huffman or arithmetic coding. */
169 if (cinfo->arith_code) {
170 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
171 } else {
172 jinit_huff_encoder(cinfo);
173 }
174
175 /* We need a special coefficient buffer controller. */
176 transencode_coef_controller(cinfo, coef_arrays);
177
178 jinit_marker_writer(cinfo);
179
180 /* We can now tell the memory manager to allocate virtual arrays. */
181 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
182
183 /* Write the datastream header (SOI, JFIF) immediately.
184 * Frame and scan headers are postponed till later.
346
347 /*
348 * Initialize coefficient buffer controller.
349 *
350 * Each passed coefficient array must be the right size for that
351 * coefficient: width_in_blocks wide and height_in_blocks high,
352 * with unitheight at least v_samp_factor.
353 */
354
355 LOCAL(void)
356 transencode_coef_controller (j_compress_ptr cinfo,
357 jvirt_barray_ptr * coef_arrays)
358 {
359 my_coef_ptr coef;
360 JBLOCKROW buffer;
361 int i;
362
363 coef = (my_coef_ptr)
364 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
365 SIZEOF(my_coef_controller));
366 cinfo->coef = (struct jpeg_c_coef_controller *) coef;
367 coef->pub.start_pass = start_pass_coef;
368 coef->pub.compress_data = compress_output;
369
370 /* Save pointer to virtual arrays */
371 coef->whole_image = coef_arrays;
372
373 /* Allocate and pre-zero space for dummy DCT blocks. */
374 buffer = (JBLOCKROW)
375 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
376 C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
377 jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
378 for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
379 coef->dummy_buffer[i] = buffer + i;
380 }
381 }
|
1 /*
2 * jctrans.c
3 *
4 * Copyright (C) 1995-1998, Thomas G. Lane.
5 * Modified 2000-2017 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains library routines for transcoding compression,
10 * that is, writing raw DCT coefficient arrays to an output JPEG file.
11 * The routines in jcapimin.c will also be needed by a transcoder.
12 */
13
14 #define JPEG_INTERNALS
15 #include "jinclude.h"
16 #include "jpeglib.h"
17
18
19 /* Forward declarations */
20 LOCAL(void) transencode_master_selection
21 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
22 LOCAL(void) transencode_coef_controller
23 JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
24
25
60 * scan script and Huffman optimization) are left in their default states.
61 */
62
63 GLOBAL(void)
64 jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
65 j_compress_ptr dstinfo)
66 {
67 JQUANT_TBL ** qtblptr;
68 jpeg_component_info *incomp, *outcomp;
69 JQUANT_TBL *c_quant, *slot_quant;
70 int tblno, ci, coefi;
71
72 /* Safety check to ensure start_compress not called yet. */
73 if (dstinfo->global_state != CSTATE_START)
74 ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
75 /* Copy fundamental image dimensions */
76 dstinfo->image_width = srcinfo->image_width;
77 dstinfo->image_height = srcinfo->image_height;
78 dstinfo->input_components = srcinfo->num_components;
79 dstinfo->in_color_space = srcinfo->jpeg_color_space;
80 dstinfo->jpeg_width = srcinfo->output_width;
81 dstinfo->jpeg_height = srcinfo->output_height;
82 dstinfo->min_DCT_h_scaled_size = srcinfo->min_DCT_h_scaled_size;
83 dstinfo->min_DCT_v_scaled_size = srcinfo->min_DCT_v_scaled_size;
84 /* Initialize all parameters to default values */
85 jpeg_set_defaults(dstinfo);
86 /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
87 * Fix it to get the right header markers for the image colorspace.
88 * Note: Entropy table assignment in jpeg_set_colorspace
89 * depends on color_transform.
90 * Adaption is also required for setting the appropriate
91 * entropy coding mode dependent on image data precision.
92 */
93 dstinfo->color_transform = srcinfo->color_transform;
94 jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
95 dstinfo->data_precision = srcinfo->data_precision;
96 dstinfo->arith_code = srcinfo->data_precision > 8 ? TRUE : FALSE;
97 dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
98 /* Copy the source's quantization tables. */
99 for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
100 if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
101 qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
102 if (*qtblptr == NULL)
103 *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
104 MEMCOPY((*qtblptr)->quantval,
105 srcinfo->quant_tbl_ptrs[tblno]->quantval,
106 SIZEOF((*qtblptr)->quantval));
107 (*qtblptr)->sent_table = FALSE;
108 }
109 }
110 /* Copy the source's per-component info.
111 * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
112 */
113 dstinfo->num_components = srcinfo->num_components;
114 if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
115 ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
116 MAX_COMPONENTS);
119 outcomp->component_id = incomp->component_id;
120 outcomp->h_samp_factor = incomp->h_samp_factor;
121 outcomp->v_samp_factor = incomp->v_samp_factor;
122 outcomp->quant_tbl_no = incomp->quant_tbl_no;
123 /* Make sure saved quantization table for component matches the qtable
124 * slot. If not, the input file re-used this qtable slot.
125 * IJG encoder currently cannot duplicate this.
126 */
127 tblno = outcomp->quant_tbl_no;
128 if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
129 srcinfo->quant_tbl_ptrs[tblno] == NULL)
130 ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
131 slot_quant = srcinfo->quant_tbl_ptrs[tblno];
132 c_quant = incomp->quant_table;
133 if (c_quant != NULL) {
134 for (coefi = 0; coefi < DCTSIZE2; coefi++) {
135 if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
136 ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
137 }
138 }
139 /* Note: we do not copy the source's entropy table assignments;
140 * instead we rely on jpeg_set_colorspace to have made a suitable choice.
141 */
142 }
143 /* Also copy JFIF version and resolution information, if available.
144 * Strictly speaking this isn't "critical" info, but it's nearly
145 * always appropriate to copy it if available. In particular,
146 * if the application chooses to copy JFIF 1.02 extension markers from
147 * the source file, we need to copy the version to make sure we don't
148 * emit a file that has 1.02 extensions but a claimed version of 1.01.
149 */
150 if (srcinfo->saw_JFIF_marker) {
151 if (srcinfo->JFIF_major_version == 1 ||
152 srcinfo->JFIF_major_version == 2) {
153 dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
154 dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
155 }
156 dstinfo->density_unit = srcinfo->density_unit;
157 dstinfo->X_density = srcinfo->X_density;
158 dstinfo->Y_density = srcinfo->Y_density;
159 }
160 }
161
162
163 LOCAL(void)
164 jpeg_calc_trans_dimensions (j_compress_ptr cinfo)
165 /* Do computations that are needed before master selection phase */
166 {
167 if (cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size)
168 ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
169 cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size);
170
171 cinfo->block_size = cinfo->min_DCT_h_scaled_size;
172 }
173
174
175 /*
176 * Master selection of compression modules for transcoding.
177 * This substitutes for jcinit.c's initialization of the full compressor.
178 */
179
180 LOCAL(void)
181 transencode_master_selection (j_compress_ptr cinfo,
182 jvirt_barray_ptr * coef_arrays)
183 {
184 /* Do computations that are needed before master selection phase */
185 jpeg_calc_trans_dimensions(cinfo);
186
187 /* Initialize master control (includes parameter checking/processing) */
188 jinit_c_master_control(cinfo, TRUE /* transcode only */);
189
190 /* Entropy encoding: either Huffman or arithmetic coding. */
191 if (cinfo->arith_code) {
192 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
193 } else {
194 jinit_huff_encoder(cinfo);
195 }
196
197 /* We need a special coefficient buffer controller. */
198 transencode_coef_controller(cinfo, coef_arrays);
199
200 jinit_marker_writer(cinfo);
201
202 /* We can now tell the memory manager to allocate virtual arrays. */
203 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
204
205 /* Write the datastream header (SOI, JFIF) immediately.
206 * Frame and scan headers are postponed till later.
368
369 /*
370 * Initialize coefficient buffer controller.
371 *
372 * Each passed coefficient array must be the right size for that
373 * coefficient: width_in_blocks wide and height_in_blocks high,
374 * with unitheight at least v_samp_factor.
375 */
376
377 LOCAL(void)
378 transencode_coef_controller (j_compress_ptr cinfo,
379 jvirt_barray_ptr * coef_arrays)
380 {
381 my_coef_ptr coef;
382 JBLOCKROW buffer;
383 int i;
384
385 coef = (my_coef_ptr)
386 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
387 SIZEOF(my_coef_controller));
388 cinfo->coef = &coef->pub;
389 coef->pub.start_pass = start_pass_coef;
390 coef->pub.compress_data = compress_output;
391
392 /* Save pointer to virtual arrays */
393 coef->whole_image = coef_arrays;
394
395 /* Allocate and pre-zero space for dummy DCT blocks. */
396 buffer = (JBLOCKROW)
397 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
398 C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
399 FMEMZERO((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
400 for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
401 coef->dummy_buffer[i] = buffer + i;
402 }
403 }
|