8 * Copyright (C) 1994-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains the difference buffer controller for compression.
13 * This controller is the top level of the lossless JPEG compressor proper.
14 * The difference buffer lies between prediction/differencing and entropy
18 #define JPEG_INTERNALS
21 #include "jlossls.h" /* Private declarations for lossless codec */
24 #ifdef C_LOSSLESS_SUPPORTED
26 /* We use a full-image sample buffer when doing Huffman optimization,
27 * and also for writing multiple-scan JPEG files. In all cases, the
28 * full-image buffer is filled during the first pass, and the scaling,
29 * prediction and differencing steps are run during subsequent passes.
31 #ifdef ENTROPY_OPT_SUPPORTED
32 #define FULL_SAMP_BUFFER_SUPPORTED
34 #ifdef C_MULTISCAN_FILES_SUPPORTED
35 #define FULL_SAMP_BUFFER_SUPPORTED
40 /* Private buffer controller object */
43 JDIMENSION iMCU_row_num
; /* iMCU row # within image */
44 JDIMENSION mcu_ctr
; /* counts MCUs processed in current row */
45 int MCU_vert_offset
; /* counts MCU rows within iMCU row */
46 int MCU_rows_per_iMCU_row
; /* number of such rows needed */
48 JSAMPROW cur_row
[MAX_COMPONENTS
]; /* row of point transformed samples */
49 JSAMPROW prev_row
[MAX_COMPONENTS
]; /* previous row of Pt'd samples */
50 JDIFFARRAY diff_buf
[MAX_COMPONENTS
]; /* iMCU row of differences */
52 /* In multi-pass modes, we need a virtual sample array for each component. */
53 jvirt_sarray_ptr whole_image
[MAX_COMPONENTS
];
56 typedef c_diff_controller
* c_diff_ptr
;
59 /* Forward declarations */
60 METHODDEF(boolean
) compress_data
61 JPP((j_compress_ptr cinfo
, JSAMPIMAGE input_buf
));
62 #ifdef FULL_SAMP_BUFFER_SUPPORTED
63 METHODDEF(boolean
) compress_first_pass
64 JPP((j_compress_ptr cinfo
, JSAMPIMAGE input_buf
));
65 METHODDEF(boolean
) compress_output
66 JPP((j_compress_ptr cinfo
, JSAMPIMAGE input_buf
));
71 start_iMCU_row (j_compress_ptr cinfo
)
72 /* Reset within-iMCU-row counters for a new row */
74 j_lossless_c_ptr losslsc
= (j_lossless_c_ptr
) cinfo
->codec
;
75 c_diff_ptr diff
= (c_diff_ptr
) losslsc
->diff_private
;
77 /* In an interleaved scan, an MCU row is the same as an iMCU row.
78 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
79 * But at the bottom of the image, process only what's left.
81 if (cinfo
->comps_in_scan
> 1) {
82 diff
->MCU_rows_per_iMCU_row
= 1;
84 if (diff
->iMCU_row_num
< (cinfo
->total_iMCU_rows
-1))
85 diff
->MCU_rows_per_iMCU_row
= cinfo
->cur_comp_info
[0]->v_samp_factor
;
87 diff
->MCU_rows_per_iMCU_row
= cinfo
->cur_comp_info
[0]->last_row_height
;
91 diff
->MCU_vert_offset
= 0;
96 * Initialize for a processing pass.
100 start_pass_diff (j_compress_ptr cinfo
, J_BUF_MODE pass_mode
)
102 j_lossless_c_ptr losslsc
= (j_lossless_c_ptr
) cinfo
->codec
;
103 c_diff_ptr diff
= (c_diff_ptr
) losslsc
->diff_private
;
105 diff
->iMCU_row_num
= 0;
106 start_iMCU_row(cinfo
);
110 if (diff
->whole_image
[0] != NULL
)
111 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
112 losslsc
->pub
.compress_data
= compress_data
;
114 #ifdef FULL_SAMP_BUFFER_SUPPORTED
115 case JBUF_SAVE_AND_PASS
:
116 if (diff
->whole_image
[0] == NULL
)
117 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
118 losslsc
->pub
.compress_data
= compress_first_pass
;
120 case JBUF_CRANK_DEST
:
121 if (diff
->whole_image
[0] == NULL
)
122 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
123 losslsc
->pub
.compress_data
= compress_output
;
127 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
133 #define SWAP_ROWS(rowa,rowb) {JSAMPROW temp; temp=rowa; rowa=rowb; rowb=temp;}
136 * Process some data in the single-pass case.
137 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
138 * per call, ie, v_samp_factor rows for each component in the image.
139 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
141 * NB: input_buf contains a plane for each component in image,
142 * which we index according to the component's SOF position.
146 compress_data (j_compress_ptr cinfo
, JSAMPIMAGE input_buf
)
148 j_lossless_c_ptr losslsc
= (j_lossless_c_ptr
) cinfo
->codec
;
149 c_diff_ptr diff
= (c_diff_ptr
) losslsc
->diff_private
;
150 JDIMENSION MCU_col_num
; /* index of current MCU within row */
151 JDIMENSION MCU_count
; /* number of MCUs encoded */
152 JDIMENSION last_MCU_col
= cinfo
->MCUs_per_row
- 1;
153 JDIMENSION last_iMCU_row
= cinfo
->total_iMCU_rows
- 1;
154 int comp
, ci
, yoffset
, samp_row
, samp_rows
, samps_across
;
155 jpeg_component_info
*compptr
;
157 /* Loop to write as much as one whole iMCU row */
158 for (yoffset
= diff
->MCU_vert_offset
; yoffset
< diff
->MCU_rows_per_iMCU_row
;
161 MCU_col_num
= diff
->mcu_ctr
;
163 /* Scale and predict each scanline of the MCU-row separately.
165 * Note: We only do this if we are at the start of a MCU-row, ie,
166 * we don't want to reprocess a row suspended by the output.
168 if (MCU_col_num
== 0) {
169 for (comp
= 0; comp
< cinfo
->comps_in_scan
; comp
++) {
170 compptr
= cinfo
->cur_comp_info
[comp
];
171 ci
= compptr
->component_index
;
172 if (diff
->iMCU_row_num
< last_iMCU_row
)
173 samp_rows
= compptr
->v_samp_factor
;
175 /* NB: can't use last_row_height here, since may not be set! */
176 samp_rows
= (int) (compptr
->height_in_data_units
% compptr
->v_samp_factor
);
177 if (samp_rows
== 0) samp_rows
= compptr
->v_samp_factor
;
179 /* Fill dummy difference rows at the bottom edge with zeros, which
180 * will encode to the smallest amount of data.
182 for (samp_row
= samp_rows
; samp_row
< compptr
->v_samp_factor
;
184 MEMZERO(diff
->diff_buf
[ci
][samp_row
],
185 jround_up((long) compptr
->width_in_data_units
,
186 (long) compptr
->h_samp_factor
) * SIZEOF(JDIFF
));
189 samps_across
= compptr
->width_in_data_units
;
191 for (samp_row
= 0; samp_row
< samp_rows
; samp_row
++) {
192 (*losslsc
->scaler_scale
) (cinfo
,
193 input_buf
[ci
][samp_row
],
194 diff
->cur_row
[ci
], samps_across
);
195 (*losslsc
->predict_difference
[ci
]) (cinfo
, ci
,
198 diff
->diff_buf
[ci
][samp_row
],
200 SWAP_ROWS(diff
->cur_row
[ci
], diff
->prev_row
[ci
]);
205 /* Try to write the MCU-row (or remaining portion of suspended MCU-row). */
207 (*losslsc
->entropy_encode_mcus
) (cinfo
,
208 diff
->diff_buf
, yoffset
, MCU_col_num
,
209 cinfo
->MCUs_per_row
- MCU_col_num
);
210 if (MCU_count
!= cinfo
->MCUs_per_row
- MCU_col_num
) {
211 /* Suspension forced; update state counters and exit */
212 diff
->MCU_vert_offset
= yoffset
;
213 diff
->mcu_ctr
+= MCU_col_num
;
217 /* Completed an MCU row, but perhaps not an iMCU row */
221 /* Completed the iMCU row, advance counters for next one */
222 diff
->iMCU_row_num
++;
223 start_iMCU_row(cinfo
);
228 #ifdef FULL_SAMP_BUFFER_SUPPORTED
231 * Process some data in the first pass of a multi-pass case.
232 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
233 * per call, ie, v_samp_factor rows for each component in the image.
234 * This amount of data is read from the source buffer and saved into the
237 * We must also emit the data to the compressor. This is conveniently
238 * done by calling compress_output() after we've loaded the current strip
239 * of the virtual arrays.
241 * NB: input_buf contains a plane for each component in image. All components
242 * are loaded into the virtual arrays in this pass. However, it may be that
243 * only a subset of the components are emitted to the compressor during
244 * this first pass; be careful about looking at the scan-dependent variables
245 * (MCU dimensions, etc).
249 compress_first_pass (j_compress_ptr cinfo
, JSAMPIMAGE input_buf
)
251 j_lossless_c_ptr losslsc
= (j_lossless_c_ptr
) cinfo
->codec
;
252 c_diff_ptr diff
= (c_diff_ptr
) losslsc
->diff_private
;
253 JDIMENSION last_iMCU_row
= cinfo
->total_iMCU_rows
- 1;
254 JDIMENSION samps_across
;
255 int ci
, samp_row
, samp_rows
;
256 JSAMPARRAY buffer
[MAX_COMPONENTS
];
257 jpeg_component_info
*compptr
;
259 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
261 /* Align the virtual buffers for this component. */
262 buffer
[ci
] = (*cinfo
->mem
->access_virt_sarray
)
263 ((j_common_ptr
) cinfo
, diff
->whole_image
[ci
],
264 diff
->iMCU_row_num
* compptr
->v_samp_factor
,
265 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
267 /* Count non-dummy sample rows in this iMCU row. */
268 if (diff
->iMCU_row_num
< last_iMCU_row
)
269 samp_rows
= compptr
->v_samp_factor
;
271 /* NB: can't use last_row_height here, since may not be set! */
272 samp_rows
= (int) (compptr
->height_in_data_units
% compptr
->v_samp_factor
);
273 if (samp_rows
== 0) samp_rows
= compptr
->v_samp_factor
;
275 samps_across
= compptr
->width_in_data_units
;
277 /* Perform point transform scaling and prediction/differencing for all
278 * non-dummy rows in this iMCU row. Each call on these functions
279 * process a complete row of samples.
281 for (samp_row
= 0; samp_row
< samp_rows
; samp_row
++) {
282 MEMCOPY(buffer
[ci
][samp_row
], input_buf
[ci
][samp_row
],
283 samps_across
* SIZEOF(JSAMPLE
));
287 /* NB: compress_output will increment iMCU_row_num if successful.
288 * A suspension return will result in redoing all the work above next time.
291 /* Emit data to the compressor, sharing code with subsequent passes */
292 return compress_output(cinfo
, input_buf
);
297 * Process some data in subsequent passes of a multi-pass case.
298 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
299 * per call, ie, v_samp_factor rows for each component in the scan.
300 * The data is obtained from the virtual arrays and fed to the compressor.
301 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
303 * NB: input_buf is ignored; it is likely to be a NULL pointer.
307 compress_output (j_compress_ptr cinfo
, JSAMPIMAGE input_buf
)
309 j_lossless_c_ptr losslsc
= (j_lossless_c_ptr
) cinfo
->codec
;
310 c_diff_ptr diff
= (c_diff_ptr
) losslsc
->diff_private
;
311 JDIMENSION MCU_col_num
; /* index of current MCU within row */
312 JDIMENSION MCU_count
; /* number of MCUs encoded */
313 int comp
, ci
, yoffset
;
314 JSAMPARRAY buffer
[MAX_COMPONENTS
];
315 jpeg_component_info
*compptr
;
317 /* Align the virtual buffers for the components used in this scan.
318 * NB: during first pass, this is safe only because the buffers will
319 * already be aligned properly, so jmemmgr.c won't need to do any I/O.
321 for (comp
= 0; comp
< cinfo
->comps_in_scan
; comp
++) {
322 compptr
= cinfo
->cur_comp_info
[comp
];
323 ci
= compptr
->component_index
;
324 buffer
[ci
] = (*cinfo
->mem
->access_virt_sarray
)
325 ((j_common_ptr
) cinfo
, diff
->whole_image
[ci
],
326 diff
->iMCU_row_num
* compptr
->v_samp_factor
,
327 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
330 return compress_data(cinfo
, buffer
);
333 #endif /* FULL_SAMP_BUFFER_SUPPORTED */
337 * Initialize difference buffer controller.
341 jinit_c_diff_controller (j_compress_ptr cinfo
, boolean need_full_buffer
)
343 j_lossless_c_ptr losslsc
= (j_lossless_c_ptr
) cinfo
->codec
;
346 jpeg_component_info
*compptr
;
349 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
350 SIZEOF(c_diff_controller
));
351 losslsc
->diff_private
= (void *) diff
;
352 losslsc
->diff_start_pass
= start_pass_diff
;
354 /* Create the prediction row buffers. */
355 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
357 diff
->cur_row
[ci
] = *(*cinfo
->mem
->alloc_sarray
)
358 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
359 (JDIMENSION
) jround_up((long) compptr
->width_in_data_units
,
360 (long) compptr
->h_samp_factor
),
362 diff
->prev_row
[ci
] = *(*cinfo
->mem
->alloc_sarray
)
363 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
364 (JDIMENSION
) jround_up((long) compptr
->width_in_data_units
,
365 (long) compptr
->h_samp_factor
),
369 /* Create the difference buffer. */
370 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
372 diff
->diff_buf
[ci
] = (*cinfo
->mem
->alloc_darray
)
373 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
374 (JDIMENSION
) jround_up((long) compptr
->width_in_data_units
,
375 (long) compptr
->h_samp_factor
),
376 (JDIMENSION
) compptr
->v_samp_factor
);
377 /* Prefill difference rows with zeros. We do this because only actual
378 * data is placed in the buffers during prediction/differencing, leaving
379 * any dummy differences at the right edge as zeros, which will encode
380 * to the smallest amount of data.
382 for (row
= 0; row
< compptr
->v_samp_factor
; row
++)
383 MEMZERO(diff
->diff_buf
[ci
][row
],
384 jround_up((long) compptr
->width_in_data_units
,
385 (long) compptr
->h_samp_factor
) * SIZEOF(JDIFF
));
388 /* Create the sample buffer. */
389 if (need_full_buffer
) {
390 #ifdef FULL_SAMP_BUFFER_SUPPORTED
391 /* Allocate a full-image virtual array for each component, */
392 /* padded to a multiple of samp_factor differences in each direction. */
394 jpeg_component_info
*compptr
;
396 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
398 diff
->whole_image
[ci
] = (*cinfo
->mem
->request_virt_sarray
)
399 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, FALSE
,
400 (JDIMENSION
) jround_up((long) compptr
->width_in_data_units
,
401 (long) compptr
->h_samp_factor
),
402 (JDIMENSION
) jround_up((long) compptr
->height_in_data_units
,
403 (long) compptr
->v_samp_factor
),
404 (JDIMENSION
) compptr
->v_samp_factor
);
407 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
410 diff
->whole_image
[0] = NULL
; /* flag for no virtual arrays */
413 #endif /* C_LOSSLESS_SUPPORTED */