15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * jctrans.c
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 1995-1998, Thomas G. Lane.
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Modified 2000-2009 by Guido Vollbeding.
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * This file is part of the Independent JPEG Group's software.
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * For conditions of distribution and use, see the accompanying README file.
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * This file contains library routines for transcoding compression,
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * that is, writing raw DCT coefficient arrays to an output JPEG file.
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * The routines in jcapimin.c will also be needed by a transcoder.
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define JPEG_INTERNALS
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "jinclude.h"
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "jpeglib.h"
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* Forward declarations */
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)LOCAL(void) transencode_master_selection
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)	JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)LOCAL(void) transencode_coef_controller
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)	JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Compression initialization for writing raw-coefficient data.
28a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Before calling this, all parameters and a data destination must be set up.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Call jpeg_finish_compress() to actually write the data.
301e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) *
31a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * The number of passed virtual arrays must match cinfo->num_components.
32a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Note that the virtual arrays need not be filled or even realized at
331e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * the time write_coefficients is called; indeed, if the virtual arrays
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * were requested from this compression object's memory manager, they
35c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) * typically will be realized during this routine and filled afterwards.
365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
37e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)GLOBAL(void)
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
40e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles){
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  if (cinfo->global_state != CSTATE_START)
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  /* Mark all tables to be written */
44e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)  jpeg_suppress_tables(cinfo, FALSE);
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  /* (Re)initialize error mgr and destination modules */
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  (*cinfo->dest->init_destination) (cinfo);
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  /* Perform master selection of active modules */
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  transencode_master_selection(cinfo, coef_arrays);
505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  /* Wait for jpeg_finish_compress() call */
515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  cinfo->next_scanline = 0;	/* so jpeg_write_marker works */
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  cinfo->global_state = CSTATE_WRCOEFS;
535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
54e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Initialize the compression object with default parameters,
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * then copy from the source object all parameters needed for lossless
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * transcoding.  Parameters that can be varied without loss (such as
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * scan script and Huffman optimization) are left in their default states.
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)GLOBAL(void)
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)			       j_compress_ptr dstinfo)
665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  JQUANT_TBL ** qtblptr;
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  jpeg_component_info *incomp, *outcomp;
695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  JQUANT_TBL *c_quant, *slot_quant;
705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  int tblno, ci, coefi;
715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
72e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)  /* Safety check to ensure start_compress not called yet. */
735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  if (dstinfo->global_state != CSTATE_START)
745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  /* Copy fundamental image dimensions */
765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  dstinfo->image_width = srcinfo->image_width;
77926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)  dstinfo->image_height = srcinfo->image_height;
78926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)  dstinfo->input_components = srcinfo->num_components;
79926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)  dstinfo->in_color_space = srcinfo->jpeg_color_space;
805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#if JPEG_LIB_VERSION >= 70
815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  dstinfo->jpeg_width = srcinfo->output_width;
825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  dstinfo->jpeg_height = srcinfo->output_height;
83926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)  dstinfo->min_DCT_h_scaled_size = srcinfo->min_DCT_h_scaled_size;
845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  dstinfo->min_DCT_v_scaled_size = srcinfo->min_DCT_v_scaled_size;
855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif
865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  /* Initialize all parameters to default values */
875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  jpeg_set_defaults(dstinfo);
88e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)  /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   * Fix it to get the right header markers for the image colorspace.
90e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)   */
915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  dstinfo->data_precision = srcinfo->data_precision;
935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  /* Copy the source's quantization tables. */
95e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)  for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      if (*qtblptr == NULL)
995c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)	*qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
1005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      MEMCOPY((*qtblptr)->quantval,
101e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)	      srcinfo->quant_tbl_ptrs[tblno]->quantval,
1025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)	      SIZEOF((*qtblptr)->quantval));
1035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      (*qtblptr)->sent_table = FALSE;
1045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
1055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  }
1065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  /* Copy the source's per-component info.
1075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
1085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   */
1095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  dstinfo->num_components = srcinfo->num_components;
1105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
1115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
1125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)	     MAX_COMPONENTS);
1135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
1145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)       ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
1155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    outcomp->component_id = incomp->component_id;
1165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    outcomp->h_samp_factor = incomp->h_samp_factor;
1175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    outcomp->v_samp_factor = incomp->v_samp_factor;
1185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    outcomp->quant_tbl_no = incomp->quant_tbl_no;
1195267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    /* Make sure saved quantization table for component matches the qtable
1205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)     * slot.  If not, the input file re-used this qtable slot.
1215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)     * IJG encoder currently cannot duplicate this.
1225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)     */
1235267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    tblno = outcomp->quant_tbl_no;
1245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
1255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)	srcinfo->quant_tbl_ptrs[tblno] == NULL)
1265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
1275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    slot_quant = srcinfo->quant_tbl_ptrs[tblno];
1285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    c_quant = incomp->quant_table;
129c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (c_quant != NULL) {
130      for (coefi = 0; coefi < DCTSIZE2; coefi++) {
131	if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
132	  ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
133      }
134    }
135    /* Note: we do not copy the source's Huffman table assignments;
136     * instead we rely on jpeg_set_colorspace to have made a suitable choice.
137     */
138  }
139  /* Also copy JFIF version and resolution information, if available.
140   * Strictly speaking this isn't "critical" info, but it's nearly
141   * always appropriate to copy it if available.  In particular,
142   * if the application chooses to copy JFIF 1.02 extension markers from
143   * the source file, we need to copy the version to make sure we don't
144   * emit a file that has 1.02 extensions but a claimed version of 1.01.
145   * We will *not*, however, copy version info from mislabeled "2.01" files.
146   */
147  if (srcinfo->saw_JFIF_marker) {
148    if (srcinfo->JFIF_major_version == 1) {
149      dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
150      dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
151    }
152    dstinfo->density_unit = srcinfo->density_unit;
153    dstinfo->X_density = srcinfo->X_density;
154    dstinfo->Y_density = srcinfo->Y_density;
155  }
156}
157
158
159/*
160 * Master selection of compression modules for transcoding.
161 * This substitutes for jcinit.c's initialization of the full compressor.
162 */
163
164LOCAL(void)
165transencode_master_selection (j_compress_ptr cinfo,
166			      jvirt_barray_ptr * coef_arrays)
167{
168  /* Although we don't actually use input_components for transcoding,
169   * jcmaster.c's initial_setup will complain if input_components is 0.
170   */
171  cinfo->input_components = 1;
172  /* Initialize master control (includes parameter checking/processing) */
173  jinit_c_master_control(cinfo, TRUE /* transcode only */);
174
175  /* Entropy encoding: either Huffman or arithmetic coding. */
176  if (cinfo->arith_code) {
177#ifdef C_ARITH_CODING_SUPPORTED
178    jinit_arith_encoder(cinfo);
179#else
180    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
181#endif
182  } else {
183    if (cinfo->progressive_mode) {
184#ifdef C_PROGRESSIVE_SUPPORTED
185      jinit_phuff_encoder(cinfo);
186#else
187      ERREXIT(cinfo, JERR_NOT_COMPILED);
188#endif
189    } else
190      jinit_huff_encoder(cinfo);
191  }
192
193  /* We need a special coefficient buffer controller. */
194  transencode_coef_controller(cinfo, coef_arrays);
195
196  jinit_marker_writer(cinfo);
197
198  /* We can now tell the memory manager to allocate virtual arrays. */
199  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
200
201  /* Write the datastream header (SOI, JFIF) immediately.
202   * Frame and scan headers are postponed till later.
203   * This lets application insert special markers after the SOI.
204   */
205  (*cinfo->marker->write_file_header) (cinfo);
206}
207
208
209/*
210 * The rest of this file is a special implementation of the coefficient
211 * buffer controller.  This is similar to jccoefct.c, but it handles only
212 * output from presupplied virtual arrays.  Furthermore, we generate any
213 * dummy padding blocks on-the-fly rather than expecting them to be present
214 * in the arrays.
215 */
216
217/* Private buffer controller object */
218
219typedef struct {
220  struct jpeg_c_coef_controller pub; /* public fields */
221
222  JDIMENSION iMCU_row_num;	/* iMCU row # within image */
223  JDIMENSION mcu_ctr;		/* counts MCUs processed in current row */
224  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
225  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
226
227  /* Virtual block array for each component. */
228  jvirt_barray_ptr * whole_image;
229
230  /* Workspace for constructing dummy blocks at right/bottom edges. */
231  JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
232} my_coef_controller;
233
234typedef my_coef_controller * my_coef_ptr;
235
236
237LOCAL(void)
238start_iMCU_row (j_compress_ptr cinfo)
239/* Reset within-iMCU-row counters for a new row */
240{
241  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
242
243  /* In an interleaved scan, an MCU row is the same as an iMCU row.
244   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
245   * But at the bottom of the image, process only what's left.
246   */
247  if (cinfo->comps_in_scan > 1) {
248    coef->MCU_rows_per_iMCU_row = 1;
249  } else {
250    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
251      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
252    else
253      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
254  }
255
256  coef->mcu_ctr = 0;
257  coef->MCU_vert_offset = 0;
258}
259
260
261/*
262 * Initialize for a processing pass.
263 */
264
265METHODDEF(void)
266start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
267{
268  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
269
270  if (pass_mode != JBUF_CRANK_DEST)
271    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
272
273  coef->iMCU_row_num = 0;
274  start_iMCU_row(cinfo);
275}
276
277
278/*
279 * Process some data.
280 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
281 * per call, ie, v_samp_factor block rows for each component in the scan.
282 * The data is obtained from the virtual arrays and fed to the entropy coder.
283 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
284 *
285 * NB: input_buf is ignored; it is likely to be a NULL pointer.
286 */
287
288METHODDEF(boolean)
289compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
290{
291  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
292  JDIMENSION MCU_col_num;	/* index of current MCU within row */
293  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
294  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
295  int blkn, ci, xindex, yindex, yoffset, blockcnt;
296  JDIMENSION start_col;
297  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
298  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
299  JBLOCKROW buffer_ptr;
300  jpeg_component_info *compptr;
301
302  /* Align the virtual buffers for the components used in this scan. */
303  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
304    compptr = cinfo->cur_comp_info[ci];
305    buffer[ci] = (*cinfo->mem->access_virt_barray)
306      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
307       coef->iMCU_row_num * compptr->v_samp_factor,
308       (JDIMENSION) compptr->v_samp_factor, FALSE);
309  }
310
311  /* Loop to process one whole iMCU row */
312  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
313       yoffset++) {
314    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
315	 MCU_col_num++) {
316      /* Construct list of pointers to DCT blocks belonging to this MCU */
317      blkn = 0;			/* index of current DCT block within MCU */
318      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
319	compptr = cinfo->cur_comp_info[ci];
320	start_col = MCU_col_num * compptr->MCU_width;
321	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
322						: compptr->last_col_width;
323	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
324	  if (coef->iMCU_row_num < last_iMCU_row ||
325	      yindex+yoffset < compptr->last_row_height) {
326	    /* Fill in pointers to real blocks in this row */
327	    buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
328	    for (xindex = 0; xindex < blockcnt; xindex++)
329	      MCU_buffer[blkn++] = buffer_ptr++;
330	  } else {
331	    /* At bottom of image, need a whole row of dummy blocks */
332	    xindex = 0;
333	  }
334	  /* Fill in any dummy blocks needed in this row.
335	   * Dummy blocks are filled in the same way as in jccoefct.c:
336	   * all zeroes in the AC entries, DC entries equal to previous
337	   * block's DC value.  The init routine has already zeroed the
338	   * AC entries, so we need only set the DC entries correctly.
339	   */
340	  for (; xindex < compptr->MCU_width; xindex++) {
341	    MCU_buffer[blkn] = coef->dummy_buffer[blkn];
342	    MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
343	    blkn++;
344	  }
345	}
346      }
347      /* Try to write the MCU. */
348      if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
349	/* Suspension forced; update state counters and exit */
350	coef->MCU_vert_offset = yoffset;
351	coef->mcu_ctr = MCU_col_num;
352	return FALSE;
353      }
354    }
355    /* Completed an MCU row, but perhaps not an iMCU row */
356    coef->mcu_ctr = 0;
357  }
358  /* Completed the iMCU row, advance counters for next one */
359  coef->iMCU_row_num++;
360  start_iMCU_row(cinfo);
361  return TRUE;
362}
363
364
365/*
366 * Initialize coefficient buffer controller.
367 *
368 * Each passed coefficient array must be the right size for that
369 * coefficient: width_in_blocks wide and height_in_blocks high,
370 * with unitheight at least v_samp_factor.
371 */
372
373LOCAL(void)
374transencode_coef_controller (j_compress_ptr cinfo,
375			     jvirt_barray_ptr * coef_arrays)
376{
377  my_coef_ptr coef;
378  JBLOCKROW buffer;
379  int i;
380
381  coef = (my_coef_ptr)
382    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
383				SIZEOF(my_coef_controller));
384  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
385  coef->pub.start_pass = start_pass_coef;
386  coef->pub.compress_data = compress_output;
387
388  /* Save pointer to virtual arrays */
389  coef->whole_image = coef_arrays;
390
391  /* Allocate and pre-zero space for dummy DCT blocks. */
392  buffer = (JBLOCKROW)
393    (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
394				C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
395  jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
396  for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
397    coef->dummy_buffer[i] = buffer + i;
398  }
399}
400