1 /*
2  * jddiffct.c
3  *
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.
7  *
8  * This file contains the [un]difference buffer controller for decompression.
9  * This controller is the top level of the lossless JPEG decompressor proper.
10  * The difference buffer lies between the entropy decoding and
11  * prediction/undifferencing steps.  The undifference buffer lies between the
12  * prediction/undifferencing and scaling steps.
13  *
14  * In buffered-image mode, this controller is the interface between
15  * input-oriented processing and output-oriented processing.
16  */
17 
18 #define JPEG_INTERNALS
19 #include "jinclude16.h"
20 #include "jpeglib16.h"
21 #include "jlossls16.h"
22 
23 
24 #ifdef D_LOSSLESS_SUPPORTED
25 
26 /* Private buffer controller object */
27 
28 typedef struct {
29   /* These variables keep track of the current location of the input side. */
30   /* cinfo->input_iMCU_row is also used for this. */
31   JDIMENSION MCU_ctr;       /* counts MCUs processed in current row */
32   unsigned int restart_rows_to_go;  /* MCU-rows left in this restart interval */
33   unsigned int MCU_vert_offset;     /* counts MCU rows within iMCU row */
34   unsigned int MCU_rows_per_iMCU_row;   /* number of such rows needed */
35 
36   /* The output side's location is represented by cinfo->output_iMCU_row. */
37 
38   JDIFFARRAY diff_buf[MAX_COMPONENTS];  /* iMCU row of differences */
39   JDIFFARRAY undiff_buf[MAX_COMPONENTS]; /* iMCU row of undiff'd samples */
40 
41 #ifdef D_MULTISCAN_FILES_SUPPORTED
42   /* In multi-pass modes, we need a virtual sample array for each component. */
43   jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
44 #endif
45 } d_diff_controller;
46 
47 typedef d_diff_controller * d_diff_ptr;
48 
49 /* Forward declarations */
50 METHODDEF(int) decompress_data
51     JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
52 #ifdef D_MULTISCAN_FILES_SUPPORTED
53 METHODDEF(int) output_data
54     JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
55 #endif
56 
57 
58 LOCAL(void)
start_iMCU_row(j_decompress_ptr cinfo)59 start_iMCU_row (j_decompress_ptr cinfo)
60 /* Reset within-iMCU-row counters for a new row (input side) */
61 {
62   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
63   d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
64 
65   /* In an interleaved scan, an MCU row is the same as an iMCU row.
66    * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
67    * But at the bottom of the image, process only what's left.
68    */
69   if (cinfo->comps_in_scan > 1) {
70     diff->MCU_rows_per_iMCU_row = 1;
71   } else {
72     if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
73       diff->MCU_rows_per_iMCU_row = (JDIMENSION)cinfo->cur_comp_info[0]->v_samp_factor;
74     else
75       diff->MCU_rows_per_iMCU_row = (JDIMENSION)cinfo->cur_comp_info[0]->last_row_height;
76   }
77 
78   diff->MCU_ctr = 0;
79   diff->MCU_vert_offset = 0;
80 }
81 
82 
83 /*
84  * Initialize for an input processing pass.
85  */
86 
87 METHODDEF(void)
start_input_pass(j_decompress_ptr cinfo)88 start_input_pass (j_decompress_ptr cinfo)
89 {
90   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
91   d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
92 
93   /* Check that the restart interval is an integer multiple of the number
94    * of MCU in an MCU-row.
95    */
96   if (cinfo->restart_interval % cinfo->MCUs_per_row != 0)
97     ERREXIT2(cinfo, JERR_BAD_RESTART,
98          (int)cinfo->restart_interval, (int)cinfo->MCUs_per_row);
99 
100   /* Initialize restart counter */
101   diff->restart_rows_to_go = cinfo->restart_interval / cinfo->MCUs_per_row;
102 
103   cinfo->input_iMCU_row = 0;
104   start_iMCU_row(cinfo);
105 }
106 
107 
108 /*
109  * Check for a restart marker & resynchronize decoder, undifferencer.
110  * Returns FALSE if must suspend.
111  */
112 
113 METHODDEF(boolean)
process_restart(j_decompress_ptr cinfo)114 process_restart (j_decompress_ptr cinfo)
115 {
116   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
117   d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
118 
119   if (! (*losslsd->entropy_process_restart) (cinfo))
120     return FALSE;
121 
122   (*losslsd->predict_process_restart) (cinfo);
123 
124   /* Reset restart counter */
125   diff->restart_rows_to_go = cinfo->restart_interval / cinfo->MCUs_per_row;
126 
127   return TRUE;
128 }
129 
130 
131 /*
132  * Initialize for an output processing pass.
133  */
134 
135 METHODDEF(void)
start_output_pass(j_decompress_ptr cinfo)136 start_output_pass (j_decompress_ptr cinfo)
137 {
138   cinfo->output_iMCU_row = 0;
139 }
140 
141 
142 /*
143  * Decompress and return some data in the supplied buffer.
144  * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
145  * Input and output must run in lockstep since we have only a one-MCU buffer.
146  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
147  *
148  * NB: output_buf contains a plane for each component in image,
149  * which we index according to the component's SOF position.
150  */
151 
152 METHODDEF(int)
decompress_data(j_decompress_ptr cinfo,JSAMPIMAGE output_buf)153 decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
154 {
155   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
156   d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
157   JDIMENSION MCU_col_num;   /* index of current MCU within row */
158   JDIMENSION MCU_count;     /* number of MCUs decoded */
159   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
160   int comp, ci, row, prev_row;
161   unsigned int yoffset;
162   jpeg_component_info *compptr;
163 
164   /* Loop to process as much as one whole iMCU row */
165   for (yoffset = diff->MCU_vert_offset; yoffset < diff->MCU_rows_per_iMCU_row;
166        yoffset++) {
167 
168     /* Process restart marker if needed; may have to suspend */
169     if (cinfo->restart_interval) {
170       if (diff->restart_rows_to_go == 0)
171     if (! process_restart(cinfo))
172       return JPEG_SUSPENDED;
173     }
174 
175     MCU_col_num = diff->MCU_ctr;
176     /* Try to fetch an MCU-row (or remaining portion of suspended MCU-row). */
177     MCU_count =
178       (*losslsd->entropy_decode_mcus) (cinfo,
179                        diff->diff_buf, yoffset, MCU_col_num,
180                        cinfo->MCUs_per_row - MCU_col_num);
181     if (MCU_count != cinfo->MCUs_per_row - MCU_col_num) {
182       /* Suspension forced; update state counters and exit */
183       diff->MCU_vert_offset = yoffset;
184       diff->MCU_ctr += MCU_count;
185       return JPEG_SUSPENDED;
186     }
187 
188     /* Account for restart interval (no-op if not using restarts) */
189     diff->restart_rows_to_go--;
190 
191     /* Completed an MCU row, but perhaps not an iMCU row */
192     diff->MCU_ctr = 0;
193   }
194 
195   /*
196    * Undifference and scale each scanline of the disassembled MCU-row
197    * separately.  We do not process dummy samples at the end of a scanline
198    * or dummy rows at the end of the image.
199    */
200   for (comp = 0; comp < cinfo->comps_in_scan; comp++) {
201     compptr = cinfo->cur_comp_info[comp];
202     ci = compptr->component_index;
203     for (row = 0, prev_row = compptr->v_samp_factor - 1;
204      row < (cinfo->input_iMCU_row == last_iMCU_row ?
205         compptr->last_row_height : compptr->v_samp_factor);
206      prev_row = row, row++) {
207       (*losslsd->predict_undifference[ci]) (cinfo, ci,
208                         diff->diff_buf[ci][row],
209                         diff->undiff_buf[ci][prev_row],
210                         diff->undiff_buf[ci][row],
211                         compptr->width_in_data_units);
212       (*losslsd->scaler_scale) (cinfo, diff->undiff_buf[ci][row],
213                 output_buf[ci][row],
214                 compptr->width_in_data_units);
215     }
216   }
217 
218   /* Completed the iMCU row, advance counters for next one.
219    *
220    * NB: output_data will increment output_iMCU_row.
221    * This counter is not needed for the single-pass case
222    * or the input side of the multi-pass case.
223    */
224   if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
225     start_iMCU_row(cinfo);
226     return JPEG_ROW_COMPLETED;
227   }
228   /* Completed the scan */
229   (*cinfo->inputctl->finish_input_pass) (cinfo);
230   return JPEG_SCAN_COMPLETED;
231 }
232 
233 
234 /*
235  * Dummy consume-input routine for single-pass operation.
236  */
237 
238 METHODDEF(int)
dummy_consume_data(j_decompress_ptr cinfo)239 dummy_consume_data (j_decompress_ptr cinfo)
240 {
241   return JPEG_SUSPENDED;    /* Always indicate nothing was done */
242 }
243 
244 
245 #ifdef D_MULTISCAN_FILES_SUPPORTED
246 
247 /*
248  * Consume input data and store it in the full-image sample buffer.
249  * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
250  * ie, v_samp_factor rows for each component in the scan.
251  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
252  */
253 
254 METHODDEF(int)
consume_data(j_decompress_ptr cinfo)255 consume_data (j_decompress_ptr cinfo)
256 {
257   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
258   d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
259   /* JDIMENSION MCU_col_num; */ /* index of current MCU within row */
260   /* JDIMENSION MCU_count; */   /* number of MCUs decoded */
261   /* JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; */
262   int comp, ci /* , yoffset, row, prev_row */;
263   JSAMPARRAY buffer[MAX_COMPS_IN_SCAN];
264   jpeg_component_info *compptr;
265 
266   /* Align the virtual buffers for the components used in this scan. */
267   for (comp = 0; comp < cinfo->comps_in_scan; comp++) {
268     compptr = cinfo->cur_comp_info[comp];
269     ci = compptr->component_index;
270     buffer[ci] = (*cinfo->mem->access_virt_sarray)
271       ((j_common_ptr) cinfo, diff->whole_image[ci],
272        cinfo->input_iMCU_row * (JDIMENSION)compptr->v_samp_factor,
273        (JDIMENSION) compptr->v_samp_factor, TRUE);
274   }
275 
276   return decompress_data(cinfo, buffer);
277 }
278 
279 
280 /*
281  * Output some data from the full-image buffer sample in the multi-pass case.
282  * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
283  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
284  *
285  * NB: output_buf contains a plane for each component in image.
286  */
287 
288 METHODDEF(int)
output_data(j_decompress_ptr cinfo,JSAMPIMAGE output_buf)289 output_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
290 {
291   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
292   d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
293   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
294   int ci, samp_rows, row;
295   JSAMPARRAY buffer;
296   jpeg_component_info *compptr;
297 
298   /* Force some input to be done if we are getting ahead of the input. */
299   while (cinfo->input_scan_number < cinfo->output_scan_number ||
300      (cinfo->input_scan_number == cinfo->output_scan_number &&
301       cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
302     if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
303       return JPEG_SUSPENDED;
304   }
305 
306   /* OK, output from the virtual arrays. */
307   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
308        ci++, compptr++) {
309     /* Align the virtual buffer for this component. */
310     buffer = (*cinfo->mem->access_virt_sarray)
311       ((j_common_ptr) cinfo, diff->whole_image[ci],
312        cinfo->output_iMCU_row * (JDIMENSION)compptr->v_samp_factor,
313        (JDIMENSION) compptr->v_samp_factor, FALSE);
314 
315     if (cinfo->output_iMCU_row < last_iMCU_row)
316       samp_rows = compptr->v_samp_factor;
317     else {
318       /* NB: can't use last_row_height here; it is input-side-dependent! */
319       samp_rows = (int)compptr->height_in_data_units % compptr->v_samp_factor;
320       if (samp_rows == 0) samp_rows = compptr->v_samp_factor;
321     }
322 
323     for (row = 0; row < samp_rows; row++) {
324       MEMCOPY(output_buf[ci][row], buffer[row],
325           compptr->width_in_data_units * SIZEOF(JSAMPLE));
326     }
327   }
328 
329   if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
330     return JPEG_ROW_COMPLETED;
331   return JPEG_SCAN_COMPLETED;
332 }
333 
334 #endif /* D_MULTISCAN_FILES_SUPPORTED */
335 
336 
337 /*
338  * Initialize difference buffer controller.
339  */
340 
341 GLOBAL(void)
jinit_d_diff_controller(j_decompress_ptr cinfo,boolean need_full_buffer)342 jinit_d_diff_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
343 {
344   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
345   d_diff_ptr diff;
346   int ci;
347   jpeg_component_info *compptr;
348 
349   diff = (d_diff_ptr)
350     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
351                 SIZEOF(d_diff_controller));
352   losslsd->diff_private = (void *) diff;
353   losslsd->diff_start_input_pass = start_input_pass;
354   losslsd->pub.start_output_pass = start_output_pass;
355 
356   /* Create the [un]difference buffers. */
357   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
358        ci++, compptr++) {
359     diff->diff_buf[ci] = (*cinfo->mem->alloc_darray)
360       ((j_common_ptr) cinfo, JPOOL_IMAGE,
361        (JDIMENSION) jround_up((long) compptr->width_in_data_units,
362                   (long) compptr->h_samp_factor),
363        (JDIMENSION) compptr->v_samp_factor);
364     diff->undiff_buf[ci] = (*cinfo->mem->alloc_darray)
365       ((j_common_ptr) cinfo, JPOOL_IMAGE,
366        (JDIMENSION) jround_up((long) compptr->width_in_data_units,
367                   (long) compptr->h_samp_factor),
368        (JDIMENSION) compptr->v_samp_factor);
369   }
370 
371   if (need_full_buffer) {
372 #ifdef D_MULTISCAN_FILES_SUPPORTED
373     /* Allocate a full-image virtual array for each component. */
374     int access_rows;
375 
376     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
377      ci++, compptr++) {
378       access_rows = compptr->v_samp_factor;
379       diff->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
380     ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
381      (JDIMENSION) jround_up((long) compptr->width_in_data_units,
382                 (long) compptr->h_samp_factor),
383      (JDIMENSION) jround_up((long) compptr->height_in_data_units,
384                 (long) compptr->v_samp_factor),
385      (JDIMENSION) access_rows);
386     }
387     losslsd->pub.consume_data = consume_data;
388     losslsd->pub.decompress_data = output_data;
389 #else
390     ERREXIT(cinfo, JERR_NOT_COMPILED);
391 #endif
392   } else {
393     losslsd->pub.consume_data = dummy_consume_data;
394     losslsd->pub.decompress_data = decompress_data;
395     diff->whole_image[0] = NULL; /* flag for no virtual arrays */
396   }
397 }
398 
399 #endif /* D_LOSSLESS_SUPPORTED */
400