4 * Copyright (C) 1994-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.
8 * This file contains the difference buffer controller for compression.
9 * This controller is the top level of the lossless JPEG compressor proper.
10 * The difference buffer lies between prediction/differencing and entropy
14 #define JPEG_INTERNALS
17 #include "jlossls.h" /* Private declarations for lossless codec */
20 #ifdef C_LOSSLESS_SUPPORTED
22 /* We use a full-image sample buffer when doing Huffman optimization,
23 * and also for writing multiple-scan JPEG files. In all cases, the
24 * full-image buffer is filled during the first pass, and the scaling,
25 * prediction and differencing steps are run during subsequent passes.
27 #ifdef ENTROPY_OPT_SUPPORTED
28 #define FULL_SAMP_BUFFER_SUPPORTED
30 #ifdef C_MULTISCAN_FILES_SUPPORTED
31 #define FULL_SAMP_BUFFER_SUPPORTED
36 /* Private buffer controller object */
39 JDIMENSION iMCU_row_num; /* iMCU row # within image */
40 JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
41 int MCU_vert_offset; /* counts MCU rows within iMCU row */
42 int MCU_rows_per_iMCU_row; /* number of such rows needed */
44 JSAMPROW cur_row[MAX_COMPONENTS]; /* row of point transformed samples */
45 JSAMPROW prev_row[MAX_COMPONENTS]; /* previous row of Pt'd samples */
46 JDIFFARRAY diff_buf[MAX_COMPONENTS]; /* iMCU row of differences */
48 /* In multi-pass modes, we need a virtual sample array for each component. */
49 jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
52 typedef c_diff_controller * c_diff_ptr;
55 /* Forward declarations */
56 METHODDEF(boolean) compress_data
57 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
58 #ifdef FULL_SAMP_BUFFER_SUPPORTED
59 METHODDEF(boolean) compress_first_pass
60 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
61 METHODDEF(boolean) compress_output
62 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
67 start_iMCU_row (j_compress_ptr cinfo)
68 /* Reset within-iMCU-row counters for a new row */
70 j_lossless_c_ptr losslsc = (j_lossless_c_ptr) cinfo->codec;
71 c_diff_ptr diff = (c_diff_ptr) losslsc->diff_private;
73 /* In an interleaved scan, an MCU row is the same as an iMCU row.
74 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
75 * But at the bottom of the image, process only what's left.
77 if (cinfo->comps_in_scan > 1) {
78 diff->MCU_rows_per_iMCU_row = 1;
80 if (diff->iMCU_row_num < (cinfo->total_iMCU_rows-1))
81 diff->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
83 diff->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
87 diff->MCU_vert_offset = 0;
92 * Initialize for a processing pass.
96 start_pass_diff (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
98 j_lossless_c_ptr losslsc = (j_lossless_c_ptr) cinfo->codec;
99 c_diff_ptr diff = (c_diff_ptr) losslsc->diff_private;
101 diff->iMCU_row_num = 0;
102 start_iMCU_row(cinfo);
106 if (diff->whole_image[0] != NULL)
107 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
108 losslsc->pub.compress_data = compress_data;
110 #ifdef FULL_SAMP_BUFFER_SUPPORTED
111 case JBUF_SAVE_AND_PASS:
112 if (diff->whole_image[0] == NULL)
113 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114 losslsc->pub.compress_data = compress_first_pass;
116 case JBUF_CRANK_DEST:
117 if (diff->whole_image[0] == NULL)
118 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
119 losslsc->pub.compress_data = compress_output;
123 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
129 #define SWAP_ROWS(rowa,rowb) {JSAMPROW temp; temp=rowa; rowa=rowb; rowb=temp;}
132 * Process some data in the single-pass case.
133 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
134 * per call, ie, v_samp_factor rows for each component in the image.
135 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
137 * NB: input_buf contains a plane for each component in image,
138 * which we index according to the component's SOF position.
142 compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
144 j_lossless_c_ptr losslsc = (j_lossless_c_ptr) cinfo->codec;
145 c_diff_ptr diff = (c_diff_ptr) losslsc->diff_private;
146 JDIMENSION MCU_col_num; /* index of current MCU within row */
147 JDIMENSION MCU_count; /* number of MCUs encoded */
148 /* JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; */
149 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
150 int comp, ci, yoffset, samp_row, samp_rows, samps_across;
151 jpeg_component_info *compptr;
153 /* Loop to write as much as one whole iMCU row */
154 for (yoffset = diff->MCU_vert_offset; yoffset < diff->MCU_rows_per_iMCU_row;
157 MCU_col_num = diff->mcu_ctr;
159 /* Scale and predict each scanline of the MCU-row separately.
161 * Note: We only do this if we are at the start of a MCU-row, ie,
162 * we don't want to reprocess a row suspended by the output.
164 if (MCU_col_num == 0) {
165 for (comp = 0; comp < cinfo->comps_in_scan; comp++) {
166 compptr = cinfo->cur_comp_info[comp];
167 ci = compptr->component_index;
168 if (diff->iMCU_row_num < last_iMCU_row)
169 samp_rows = compptr->v_samp_factor;
171 /* NB: can't use last_row_height here, since may not be set! */
172 samp_rows = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
173 if (samp_rows == 0) samp_rows = compptr->v_samp_factor;
175 /* Fill dummy difference rows at the bottom edge with zeros, which
176 * will encode to the smallest amount of data.
178 for (samp_row = samp_rows; samp_row < compptr->v_samp_factor;
180 MEMZERO(diff->diff_buf[ci][samp_row],
181 jround_up((long) compptr->width_in_data_units,
182 (long) compptr->h_samp_factor) * SIZEOF(JDIFF));
185 samps_across = compptr->width_in_data_units;
187 for (samp_row = 0; samp_row < samp_rows; samp_row++) {
188 (*losslsc->scaler_scale) (cinfo,
189 input_buf[ci][samp_row],
190 diff->cur_row[ci], samps_across);
191 (*losslsc->predict_difference[ci]) (cinfo, ci,
194 diff->diff_buf[ci][samp_row],
196 SWAP_ROWS(diff->cur_row[ci], diff->prev_row[ci]);
201 /* Try to write the MCU-row (or remaining portion of suspended MCU-row). */
203 (*losslsc->entropy_encode_mcus) (cinfo,
204 diff->diff_buf, yoffset, MCU_col_num,
205 cinfo->MCUs_per_row - MCU_col_num);
206 if (MCU_count != cinfo->MCUs_per_row - MCU_col_num) {
207 /* Suspension forced; update state counters and exit */
208 diff->MCU_vert_offset = yoffset;
209 diff->mcu_ctr += MCU_col_num;
213 /* Completed an MCU row, but perhaps not an iMCU row */
217 /* Completed the iMCU row, advance counters for next one */
218 diff->iMCU_row_num++;
219 start_iMCU_row(cinfo);
224 #ifdef FULL_SAMP_BUFFER_SUPPORTED
227 * Process some data in the first pass of a multi-pass case.
228 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
229 * per call, ie, v_samp_factor rows for each component in the image.
230 * This amount of data is read from the source buffer and saved into the
233 * We must also emit the data to the compressor. This is conveniently
234 * done by calling compress_output() after we've loaded the current strip
235 * of the virtual arrays.
237 * NB: input_buf contains a plane for each component in image. All components
238 * are loaded into the virtual arrays in this pass. However, it may be that
239 * only a subset of the components are emitted to the compressor during
240 * this first pass; be careful about looking at the scan-dependent variables
241 * (MCU dimensions, etc).
245 compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
247 j_lossless_c_ptr losslsc = (j_lossless_c_ptr) cinfo->codec;
248 c_diff_ptr diff = (c_diff_ptr) losslsc->diff_private;
249 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
250 JDIMENSION samps_across;
251 int ci, samp_row, samp_rows;
252 JSAMPARRAY buffer[MAX_COMPONENTS];
253 jpeg_component_info *compptr;
255 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
257 /* Align the virtual buffers for this component. */
258 buffer[ci] = (*cinfo->mem->access_virt_sarray)
259 ((j_common_ptr) cinfo, diff->whole_image[ci],
260 diff->iMCU_row_num * compptr->v_samp_factor,
261 (JDIMENSION) compptr->v_samp_factor, TRUE);
263 /* Count non-dummy sample rows in this iMCU row. */
264 if (diff->iMCU_row_num < last_iMCU_row)
265 samp_rows = compptr->v_samp_factor;
267 /* NB: can't use last_row_height here, since may not be set! */
268 samp_rows = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
269 if (samp_rows == 0) samp_rows = compptr->v_samp_factor;
271 samps_across = compptr->width_in_data_units;
273 /* Perform point transform scaling and prediction/differencing for all
274 * non-dummy rows in this iMCU row. Each call on these functions
275 * process a complete row of samples.
277 for (samp_row = 0; samp_row < samp_rows; samp_row++) {
278 MEMCOPY(buffer[ci][samp_row], input_buf[ci][samp_row],
279 samps_across * SIZEOF(JSAMPLE));
283 /* NB: compress_output will increment iMCU_row_num if successful.
284 * A suspension return will result in redoing all the work above next time.
287 /* Emit data to the compressor, sharing code with subsequent passes */
288 return compress_output(cinfo, input_buf);
293 * Process some data in subsequent passes of a multi-pass case.
294 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
295 * per call, ie, v_samp_factor rows for each component in the scan.
296 * The data is obtained from the virtual arrays and fed to the compressor.
297 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
299 * NB: input_buf is ignored; it is likely to be a NULL pointer.
303 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
305 j_lossless_c_ptr losslsc = (j_lossless_c_ptr) cinfo->codec;
306 c_diff_ptr diff = (c_diff_ptr) losslsc->diff_private;
307 /* JDIMENSION MCU_col_num; */ /* index of current MCU within row */
308 /* JDIMENSION MCU_count; */ /* number of MCUs encoded */
309 int comp, ci /* , yoffset */ ;
310 JSAMPARRAY buffer[MAX_COMPONENTS];
311 jpeg_component_info *compptr;
314 /* Align the virtual buffers for the components used in this scan.
315 * NB: during first pass, this is safe only because the buffers will
316 * already be aligned properly, so jmemmgr.c won't need to do any I/O.
318 for (comp = 0; comp < cinfo->comps_in_scan; comp++) {
319 compptr = cinfo->cur_comp_info[comp];
320 ci = compptr->component_index;
321 buffer[ci] = (*cinfo->mem->access_virt_sarray)
322 ((j_common_ptr) cinfo, diff->whole_image[ci],
323 diff->iMCU_row_num * compptr->v_samp_factor,
324 (JDIMENSION) compptr->v_samp_factor, FALSE);
327 return compress_data(cinfo, buffer);
330 #endif /* FULL_SAMP_BUFFER_SUPPORTED */
334 * Initialize difference buffer controller.
338 jinit_c_diff_controller (j_compress_ptr cinfo, boolean need_full_buffer)
340 j_lossless_c_ptr losslsc = (j_lossless_c_ptr) cinfo->codec;
343 jpeg_component_info *compptr;
346 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
347 SIZEOF(c_diff_controller));
348 losslsc->diff_private = (void *) diff;
349 losslsc->diff_start_pass = start_pass_diff;
351 /* Create the prediction row buffers. */
352 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
354 diff->cur_row[ci] = *(*cinfo->mem->alloc_sarray)
355 ((j_common_ptr) cinfo, JPOOL_IMAGE,
356 (JDIMENSION) jround_up((long) compptr->width_in_data_units,
357 (long) compptr->h_samp_factor),
359 diff->prev_row[ci] = *(*cinfo->mem->alloc_sarray)
360 ((j_common_ptr) cinfo, JPOOL_IMAGE,
361 (JDIMENSION) jround_up((long) compptr->width_in_data_units,
362 (long) compptr->h_samp_factor),
366 /* Create the difference buffer. */
367 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
369 diff->diff_buf[ci] = (*cinfo->mem->alloc_darray)
370 ((j_common_ptr) cinfo, JPOOL_IMAGE,
371 (JDIMENSION) jround_up((long) compptr->width_in_data_units,
372 (long) compptr->h_samp_factor),
373 (JDIMENSION) compptr->v_samp_factor);
374 /* Prefill difference rows with zeros. We do this because only actual
375 * data is placed in the buffers during prediction/differencing, leaving
376 * any dummy differences at the right edge as zeros, which will encode
377 * to the smallest amount of data.
379 for (row = 0; row < compptr->v_samp_factor; row++)
380 MEMZERO(diff->diff_buf[ci][row],
381 jround_up((long) compptr->width_in_data_units,
382 (long) compptr->h_samp_factor) * SIZEOF(JDIFF));
385 /* Create the sample buffer. */
386 if (need_full_buffer) {
387 #ifdef FULL_SAMP_BUFFER_SUPPORTED
388 /* Allocate a full-image virtual array for each component, */
389 /* padded to a multiple of samp_factor differences in each direction. */
391 jpeg_component_info *compptr;
393 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
395 diff->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
396 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
397 (JDIMENSION) jround_up((long) compptr->width_in_data_units,
398 (long) compptr->h_samp_factor),
399 (JDIMENSION) jround_up((long) compptr->height_in_data_units,
400 (long) compptr->v_samp_factor),
401 (JDIMENSION) compptr->v_samp_factor);
404 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
407 diff->whole_image[0] = NULL; /* flag for no virtual arrays */
410 #endif /* C_LOSSLESS_SUPPORTED */