doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
jctrans.c
Go to the documentation of this file.
1 /*
2  * jctrans.c
3  *
4  * Copyright (C) 1995, 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 */
20  JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
22  JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
23 
24 
25 /*
26  * Compression initialization for writing raw-coefficient data.
27  * Before calling this, all parameters and a data destination must be set up.
28  * Call jpeg_finish_compress() to actually write the data.
29  *
30  * The number of passed virtual arrays must match cinfo->num_components.
31  * Note that the virtual arrays need not be filled or even realized at
32  * the time write_coefficients is called; indeed, if the virtual arrays
33  * were requested from this compression object's memory manager, they
34  * typically will be realized during this routine and filled afterwards.
35  */
36 
37 GLOBAL void
39 {
40  if (cinfo->global_state != CSTATE_START)
41  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
42  /* Mark all tables to be written */
44  /* (Re)initialize error mgr and destination modules */
45  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
46  (*cinfo->dest->init_destination) (cinfo);
47  /* Perform master selection of active modules */
48  transencode_master_selection(cinfo, coef_arrays);
49  /* Wait for jpeg_finish_compress() call */
50  cinfo->next_scanline = 0; /* so jpeg_write_marker works */
51  cinfo->global_state = CSTATE_WRCOEFS;
52 }
53 
54 
55 /*
56  * Initialize the compression object with default parameters,
57  * then copy from the source object all parameters needed for lossless
58  * transcoding. Parameters that can be varied without loss (such as
59  * scan script and Huffman optimization) are left in their default states.
60  */
61 
62 GLOBAL void
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,
106  for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
107  ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
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 }
133 
134 
135 /*
136  * Master selection of compression modules for transcoding.
137  * This substitutes for jcinit.c's initialization of the full compressor.
138  */
139 
140 LOCAL void
142  jvirt_barray_ptr * coef_arrays)
143 {
144  /* Although we don't actually use input_components for transcoding,
145  * jcmaster.c's initial_setup will complain if input_components is 0.
146  */
147  cinfo->input_components = 1;
148  /* Initialize master control (includes parameter checking/processing) */
149  jinit_c_master_control(cinfo, TRUE /* transcode only */);
150 
151  /* Entropy encoding: either Huffman or arithmetic coding. */
152  if (cinfo->arith_code) {
153  ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
154  } else {
155  if (cinfo->progressive_mode) {
156 #ifdef C_PROGRESSIVE_SUPPORTED
157  jinit_phuff_encoder(cinfo);
158 #else
159  ERREXIT(cinfo, JERR_NOT_COMPILED);
160 #endif
161  } else
162  jinit_huff_encoder(cinfo);
163  }
164 
165  /* We need a special coefficient buffer controller. */
166  transencode_coef_controller(cinfo, coef_arrays);
167 
168  jinit_marker_writer(cinfo);
169 
170  /* We can now tell the memory manager to allocate virtual arrays. */
171  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
172 
173  /* Write the datastream header (SOI) immediately.
174  * Frame and scan headers are postponed till later.
175  * This lets application insert special markers after the SOI.
176  */
177  (*cinfo->marker->write_file_header) (cinfo);
178 }
179 
180 
181 /*
182  * The rest of this file is a special implementation of the coefficient
183  * buffer controller. This is similar to jccoefct.c, but it handles only
184  * output from presupplied virtual arrays. Furthermore, we generate any
185  * dummy padding blocks on-the-fly rather than expecting them to be present
186  * in the arrays.
187  */
188 
189 /* Private buffer controller object */
190 
191 typedef struct {
192  struct jpeg_c_coef_controller pub; /* public fields */
193 
194  JDIMENSION iMCU_row_num; /* iMCU row # within image */
195  JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
196  int MCU_vert_offset; /* counts MCU rows within iMCU row */
197  int MCU_rows_per_iMCU_row; /* number of such rows needed */
198 
199  /* Virtual block array for each component. */
201 
202  /* Workspace for constructing dummy blocks at right/bottom edges. */
205 
207 
208 
209 LOCAL void
211 /* Reset within-iMCU-row counters for a new row */
212 {
213  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
214 
215  /* In an interleaved scan, an MCU row is the same as an iMCU row.
216  * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
217  * But at the bottom of the image, process only what's left.
218  */
219  if (cinfo->comps_in_scan > 1) {
220  coef->MCU_rows_per_iMCU_row = 1;
221  } else {
222  if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
224  else
226  }
227 
228  coef->mcu_ctr = 0;
229  coef->MCU_vert_offset = 0;
230 }
231 
232 
233 /*
234  * Initialize for a processing pass.
235  */
236 
237 METHODDEF void
239 {
240  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
241 
242  if (pass_mode != JBUF_CRANK_DEST)
243  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
244 
245  coef->iMCU_row_num = 0;
246  start_iMCU_row(cinfo);
247 }
248 
249 
250 /*
251  * Process some data.
252  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
253  * per call, ie, v_samp_factor block rows for each component in the scan.
254  * The data is obtained from the virtual arrays and fed to the entropy coder.
255  * Returns TRUE if the iMCU row is completed, FALSE if suspended.
256  *
257  * NB: input_buf is ignored; it is likely to be a NULL pointer.
258  */
259 
260 METHODDEF boolean
262 {
263  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
264  JDIMENSION MCU_col_num; /* index of current MCU within row */
265  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
266  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267  int blkn, ci, xindex, yindex, yoffset, blockcnt;
268  JDIMENSION start_col;
270  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
271  JBLOCKROW buffer_ptr;
272  jpeg_component_info *compptr;
273 
274  /* Align the virtual buffers for the components used in this scan. */
275  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
276  compptr = cinfo->cur_comp_info[ci];
277  buffer[ci] = (*cinfo->mem->access_virt_barray)
278  ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
279  coef->iMCU_row_num * compptr->v_samp_factor,
280  (JDIMENSION) compptr->v_samp_factor, FALSE);
281  }
282 
283  /* Loop to process one whole iMCU row */
284  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
285  yoffset++) {
286  for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
287  MCU_col_num++) {
288  /* Construct list of pointers to DCT blocks belonging to this MCU */
289  blkn = 0; /* index of current DCT block within MCU */
290  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
291  compptr = cinfo->cur_comp_info[ci];
292  start_col = MCU_col_num * compptr->MCU_width;
293  blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
294  : compptr->last_col_width;
295  for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
296  if (coef->iMCU_row_num < last_iMCU_row ||
297  yindex+yoffset < compptr->last_row_height) {
298  /* Fill in pointers to real blocks in this row */
299  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
300  for (xindex = 0; xindex < blockcnt; xindex++)
301  MCU_buffer[blkn++] = buffer_ptr++;
302  } else {
303  /* At bottom of image, need a whole row of dummy blocks */
304  xindex = 0;
305  }
306  /* Fill in any dummy blocks needed in this row.
307  * Dummy blocks are filled in the same way as in jccoefct.c:
308  * all zeroes in the AC entries, DC entries equal to previous
309  * block's DC value. The init routine has already zeroed the
310  * AC entries, so we need only set the DC entries correctly.
311  */
312  for (; xindex < compptr->MCU_width; xindex++) {
313  MCU_buffer[blkn] = coef->dummy_buffer[blkn];
314  MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
315  blkn++;
316  }
317  }
318  }
319  /* Try to write the MCU. */
320  if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
321  /* Suspension forced; update state counters and exit */
322  coef->MCU_vert_offset = yoffset;
323  coef->mcu_ctr = MCU_col_num;
324  return FALSE;
325  }
326  }
327  /* Completed an MCU row, but perhaps not an iMCU row */
328  coef->mcu_ctr = 0;
329  }
330  /* Completed the iMCU row, advance counters for next one */
331  coef->iMCU_row_num++;
332  start_iMCU_row(cinfo);
333  return TRUE;
334 }
335 
336 
337 /*
338  * Initialize coefficient buffer controller.
339  *
340  * Each passed coefficient array must be the right size for that
341  * coefficient: width_in_blocks wide and height_in_blocks high,
342  * with unitheight at least v_samp_factor.
343  */
344 
345 LOCAL void
347  jvirt_barray_ptr * coef_arrays)
348 {
349  my_coef_ptr coef;
351  int i;
352 
353  coef = (my_coef_ptr)
354  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
356  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
357  coef->pub.start_pass = start_pass_coef;
358  coef->pub.compress_data = compress_output;
359 
360  /* Save pointer to virtual arrays */
361  coef->whole_image = coef_arrays;
362 
363  /* Allocate and pre-zero space for dummy DCT blocks. */
364  buffer = (JBLOCKROW)
365  (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
367  jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
368  for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
369  coef->dummy_buffer[i] = buffer + i;
370  }
371 }
LOCAL void start_iMCU_row(j_compress_ptr cinfo)
Definition: jctrans.c:210
UINT16 quantval[DCTSIZE2]
Definition: jpeglib.h:100
#define CSTATE_START
Definition: jpegint.h:25
my_coef_controller * my_coef_ptr
Definition: jctrans.c:206
#define NUM_QUANT_TBLS
Definition: jpeglib.h:44
jpeg_component_info * comp_info
Definition: jpeglib.h:530
struct jpeg_common_struct * j_common_ptr
Definition: jpeglib.h:260
JCOEF JBLOCK[DCTSIZE2]
Definition: jpeglib.h:83
JDIMENSION image_height
Definition: jpeglib.h:279
JBLOCKROW * JBLOCKARRAY
Definition: jpeglib.h:85
#define MAX_COMPONENTS
Definition: jmorecfg.h:35
#define LOCAL
Definition: jmorecfg.h:189
JDIMENSION mcu_ctr
Definition: jccoefct.c:38
#define ERREXIT(cinfo, code)
Definition: jerror.h:193
METHODDEF void start_pass_coef(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
Definition: jctrans.c:238
JDIMENSION image_width
Definition: jpeglib.h:415
GLOBAL void jpeg_suppress_tables(j_compress_ptr cinfo, boolean suppress)
Definition: jcapimin.c:104
#define SIZEOF(object)
Definition: jinclude.h:80
JDIMENSION image_height
Definition: jpeglib.h:416
struct jpeg_entropy_encoder * entropy
Definition: jpeglib.h:400
int i
Definition: process.py:33
J_COLOR_SPACE in_color_space
Definition: jpeglib.h:281
JDIMENSION next_scanline
Definition: jpeglib.h:351
#define JPOOL_IMAGE
Definition: jpeglib.h:736
METHODDEF boolean compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
Definition: jctrans.c:261
struct jpeg_c_coef_controller pub
Definition: jccoefct.c:35
LOCAL void transencode_coef_controller(j_compress_ptr cinfo, jvirt_barray_ptr *coef_arrays)
Definition: jctrans.c:346
#define MEMCOPY(dest, src, size)
Definition: jinclude.h:68
jvirt_barray_ptr * whole_image
Definition: jctrans.c:200
struct jpeg_c_coef_controller * coef
Definition: jpeglib.h:395
#define DCTSIZE2
Definition: jpeglib.h:43
JDIMENSION total_iMCU_rows
Definition: jpeglib.h:364
#define NULL
Definition: Lib.h:88
JDIMENSION iMCU_row_num
Definition: jccoefct.c:37
GLuint buffer
Definition: glext.h:3108
LOCAL void transencode_master_selection(j_compress_ptr cinfo, jvirt_barray_ptr *coef_arrays)
Definition: jctrans.c:141
#define C_MAX_BLOCKS_IN_MCU
Definition: jpeglib.h:56
struct jpeg_destination_mgr * dest
Definition: jpeglib.h:271
struct jpeg_marker_writer * marker
Definition: jpeglib.h:396
JBLOCK FAR * JBLOCKROW
Definition: jpeglib.h:84
#define FAR
Definition: jmorecfg.h:205
GLOBAL void jpeg_set_colorspace(j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
Definition: jcparam.c:365
GLOBAL void jinit_huff_encoder(j_compress_ptr cinfo)
Definition: jchuff.c:828
GLOBAL void jinit_phuff_encoder(j_compress_ptr cinfo)
Definition: jcphuff.c:810
GLint GLint GLint yoffset
Definition: glext.h:2879
JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU]
Definition: jctrans.c:203
#define GLOBAL
Definition: jmorecfg.h:190
JQUANT_TBL * quant_table
Definition: jpeglib.h:187
JSAMPARRAY * JSAMPIMAGE
Definition: jpeglib.h:81
JDIMENSION MCUs_per_row
Definition: jpeglib.h:379
#define METHODDEF
Definition: jmorecfg.h:188
boolean CCIR601_sampling
Definition: jpeglib.h:553
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:196
boolean arith_code
Definition: jpeglib.h:320
LOCAL void transencode_master_selection JPP((j_compress_ptr cinfo, jvirt_barray_ptr *coef_arrays))
GLOBAL void jzero_far(void FAR *target, size_t bytestozero)
Definition: jutils.c:161
GLOBAL void jinit_marker_writer(j_compress_ptr cinfo)
Definition: jcmarker.c:626
boolean progressive_mode
Definition: jpeglib.h:360
JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS]
Definition: jpeglib.h:301
boolean CCIR601_sampling
Definition: jpeglib.h:322
jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN]
Definition: jpeglib.h:376
J_BUF_MODE
Definition: jpegint.h:16
#define FALSE
Definition: mprintf.c:70
GLOBAL JQUANT_TBL * jpeg_alloc_quant_table(j_common_ptr cinfo)
Definition: jcomapi.c:74
jvirt_barray_ptr whole_image[MAX_COMPONENTS]
Definition: jccoefct.c:54
unsigned int JDIMENSION
Definition: jmorecfg.h:177
#define TRUE
Definition: mprintf.c:69
GLOBAL void jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only)
Definition: jcmaster.c:531
GLOBAL void jpeg_write_coefficients(j_compress_ptr cinfo, jvirt_barray_ptr *coef_arrays)
Definition: jctrans.c:38
GLOBAL void jpeg_set_defaults(j_compress_ptr cinfo)
Definition: jcparam.c:250
if(!ValidDisplayID(prefInfo.prefDisplayID)) prefInfo.prefDisplayID
GLOBAL void jpeg_copy_critical_parameters(j_decompress_ptr srcinfo, j_compress_ptr dstinfo)
Definition: jctrans.c:63
#define CSTATE_WRCOEFS
Definition: jpegint.h:28
#define ERREXIT2(cinfo, code, p1, p2)
Definition: jerror.h:200
JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS]
Definition: jpeglib.h:517
#define MAX_COMPS_IN_SCAN
Definition: jpeglib.h:47
jpeg_component_info * comp_info
Definition: jpeglib.h:298
J_COLOR_SPACE jpeg_color_space
Definition: jpeglib.h:418
int MCU_rows_per_iMCU_row
Definition: jccoefct.c:40
JDIMENSION image_width
Definition: jpeglib.h:278