Coverage Report

Created: 2025-06-13 06:18

/src/gdal/build/frmts/jpeg/libjpeg12/jdcoefct12.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdcoefct.c
3
 *
4
 * Copyright (C) 1994-1997, 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 coefficient buffer controller for decompression.
9
 * This controller is the top level of the JPEG decompressor proper.
10
 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
11
 *
12
 * In buffered-image mode, this controller is the interface between
13
 * input-oriented processing and output-oriented processing.
14
 * Also, the input side (only) is used when reading a file for transcoding.
15
 */
16
17
#define JPEG_INTERNALS
18
#include "jinclude.h"
19
#include "jpeglib.h"
20
21
#include "cpl_port.h"
22
23
/* Block smoothing is only applicable for progressive JPEG, so: */
24
#ifndef D_PROGRESSIVE_SUPPORTED
25
#undef BLOCK_SMOOTHING_SUPPORTED
26
#endif
27
28
/* Private buffer controller object */
29
30
typedef struct {
31
  struct jpeg_d_coef_controller pub; /* public fields */
32
33
  /* These variables keep track of the current location of the input side. */
34
  /* cinfo->input_iMCU_row is also used for this. */
35
  JDIMENSION MCU_ctr;   /* counts MCUs processed in current row */
36
  int MCU_vert_offset;    /* counts MCU rows within iMCU row */
37
  int MCU_rows_per_iMCU_row;  /* number of such rows needed */
38
39
  /* The output side's location is represented by cinfo->output_iMCU_row. */
40
41
  /* In single-pass modes, it's sufficient to buffer just one MCU.
42
   * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
43
   * and let the entropy decoder write into that workspace each time.
44
   * (On 80x86, the workspace is FAR even though it's not really very big;
45
   * this is to keep the module interfaces unchanged when a large coefficient
46
   * buffer is necessary.)
47
   * In multi-pass modes, this array points to the current MCU's blocks
48
   * within the virtual arrays; it is used only by the input side.
49
   */
50
  JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
51
52
#ifdef D_MULTISCAN_FILES_SUPPORTED
53
  /* In multi-pass modes, we need a virtual block array for each component. */
54
  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
55
#endif
56
57
#ifdef BLOCK_SMOOTHING_SUPPORTED
58
  /* When doing block smoothing, we latch coefficient Al values here */
59
  int * coef_bits_latch;
60
0
#define SAVED_COEFS  6    /* we save coef_bits[0..5] */
61
#endif
62
} my_coef_controller;
63
64
typedef my_coef_controller * my_coef_ptr;
65
66
/* Forward declarations */
67
METHODDEF(int) decompress_onepass
68
  JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
69
#ifdef D_MULTISCAN_FILES_SUPPORTED
70
METHODDEF(int) decompress_data
71
  JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
72
#endif
73
#ifdef BLOCK_SMOOTHING_SUPPORTED
74
LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
75
METHODDEF(int) decompress_smooth_data
76
  JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
77
#endif
78
79
80
LOCAL(void)
81
start_iMCU_row (j_decompress_ptr cinfo)
82
/* Reset within-iMCU-row counters for a new row (input side) */
83
0
{
84
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
85
86
  /* In an interleaved scan, an MCU row is the same as an iMCU row.
87
   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
88
   * But at the bottom of the image, process only what's left.
89
   */
90
0
  if (cinfo->comps_in_scan > 1) {
91
0
    coef->MCU_rows_per_iMCU_row = 1;
92
0
  } else {
93
0
    if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
94
0
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
95
0
    else
96
0
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
97
0
  }
98
99
0
  coef->MCU_ctr = 0;
100
0
  coef->MCU_vert_offset = 0;
101
0
}
102
103
104
/*
105
 * Initialize for an input processing pass.
106
 */
107
108
METHODDEF(void)
109
start_input_pass (j_decompress_ptr cinfo)
110
0
{
111
0
  cinfo->input_iMCU_row = 0;
112
0
  start_iMCU_row(cinfo);
113
0
}
114
115
116
/*
117
 * Initialize for an output processing pass.
118
 */
119
120
METHODDEF(void)
121
start_output_pass (j_decompress_ptr cinfo)
122
0
{
123
0
#ifdef BLOCK_SMOOTHING_SUPPORTED
124
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
125
126
  /* If multipass, check to see whether to use block smoothing on this pass */
127
0
  if (coef->pub.coef_arrays != NULL) {
128
0
    if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
129
0
      coef->pub.decompress_data = decompress_smooth_data;
130
0
    else
131
0
      coef->pub.decompress_data = decompress_data;
132
0
  }
133
0
#endif
134
0
  cinfo->output_iMCU_row = 0;
135
0
}
136
137
138
/*
139
 * Decompress and return some data in the single-pass case.
140
 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
141
 * Input and output must run in lockstep since we have only a one-MCU buffer.
142
 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
143
 *
144
 * NB: output_buf contains a plane for each component in image,
145
 * which we index according to the component's SOF position.
146
 */
147
148
METHODDEF(int)
149
decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
150
0
{
151
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
152
0
  JDIMENSION MCU_col_num; /* index of current MCU within row */
153
0
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
154
0
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
155
0
  int blkn, ci, xindex, yindex, yoffset, useful_width;
156
0
  JSAMPARRAY output_ptr;
157
0
  JDIMENSION start_col, output_col;
158
0
  jpeg_component_info *compptr;
159
0
  inverse_DCT_method_ptr inverse_DCT;
160
161
  /* Loop to process as much as one whole iMCU row */
162
0
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
163
0
       yoffset++) {
164
0
    for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
165
0
   MCU_col_num++) {
166
      /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */
167
0
      jzero_far((void FAR *) coef->MCU_buffer[0],
168
0
    (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
169
0
      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
170
  /* Suspension forced; update state counters and exit */
171
0
  coef->MCU_vert_offset = yoffset;
172
0
  coef->MCU_ctr = MCU_col_num;
173
0
  return JPEG_SUSPENDED;
174
0
      }
175
      /* Determine where data should go in output_buf and do the IDCT thing.
176
       * We skip dummy blocks at the right and bottom edges (but blkn gets
177
       * incremented past them!).  Note the inner loop relies on having
178
       * allocated the MCU_buffer[] blocks sequentially.
179
       */
180
0
      blkn = 0;     /* index of current DCT block within MCU */
181
0
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
182
0
  compptr = cinfo->cur_comp_info[ci];
183
  /* Don't bother to IDCT an uninteresting component. */
184
0
  if (! compptr->component_needed) {
185
0
    blkn += compptr->MCU_blocks;
186
0
    continue;
187
0
  }
188
0
  inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
189
0
  useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
190
0
                : compptr->last_col_width;
191
0
  output_ptr = output_buf[compptr->component_index] +
192
0
    yoffset * compptr->DCT_scaled_size;
193
0
  start_col = MCU_col_num * compptr->MCU_sample_width;
194
0
  for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
195
0
    if (cinfo->input_iMCU_row < last_iMCU_row ||
196
0
        yoffset+yindex < compptr->last_row_height) {
197
0
      output_col = start_col;
198
0
      for (xindex = 0; xindex < useful_width; xindex++) {
199
0
        (*inverse_DCT) (cinfo, compptr,
200
0
            (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
201
0
            output_ptr, output_col);
202
0
        output_col += compptr->DCT_scaled_size;
203
0
      }
204
0
    }
205
0
    blkn += compptr->MCU_width;
206
0
    output_ptr += compptr->DCT_scaled_size;
207
0
  }
208
0
      }
209
0
    }
210
    /* Completed an MCU row, but perhaps not an iMCU row */
211
0
    coef->MCU_ctr = 0;
212
0
  }
213
  /* Completed the iMCU row, advance counters for next one */
214
0
  cinfo->output_iMCU_row++;
215
0
  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
216
0
    start_iMCU_row(cinfo);
217
0
    return JPEG_ROW_COMPLETED;
218
0
  }
219
  /* Completed the scan */
220
0
  (*cinfo->inputctl->finish_input_pass) (cinfo);
221
0
  return JPEG_SCAN_COMPLETED;
222
0
}
223
224
225
/*
226
 * Dummy consume-input routine for single-pass operation.
227
 */
228
229
METHODDEF(int)
230
dummy_consume_data (CPL_UNUSED j_decompress_ptr cinfo)
231
0
{
232
0
  return JPEG_SUSPENDED; /* Always indicate nothing was done */
233
0
}
234
235
236
#ifdef D_MULTISCAN_FILES_SUPPORTED
237
238
/*
239
 * Consume input data and store it in the full-image coefficient buffer.
240
 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
241
 * ie, v_samp_factor block rows for each component in the scan.
242
 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
243
 */
244
245
METHODDEF(int)
246
consume_data (j_decompress_ptr cinfo)
247
0
{
248
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
249
0
  JDIMENSION MCU_col_num; /* index of current MCU within row */
250
0
  int blkn, ci, xindex, yindex, yoffset;
251
0
  JDIMENSION start_col;
252
0
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
253
0
  JBLOCKROW buffer_ptr;
254
0
  jpeg_component_info *compptr;
255
256
  /* Align the virtual buffers for the components used in this scan. */
257
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
258
0
    compptr = cinfo->cur_comp_info[ci];
259
0
    buffer[ci] = (*cinfo->mem->access_virt_barray)
260
0
      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
261
0
       cinfo->input_iMCU_row * compptr->v_samp_factor,
262
0
       (JDIMENSION) compptr->v_samp_factor, TRUE);
263
    /* Note: entropy decoder expects buffer to be zeroed,
264
     * but this is handled automatically by the memory manager
265
     * because we requested a pre-zeroed array.
266
     */
267
0
  }
268
269
  /* Loop to process one whole iMCU row */
270
0
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
271
0
       yoffset++) {
272
0
    for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
273
0
   MCU_col_num++) {
274
      /* Construct list of pointers to DCT blocks belonging to this MCU */
275
0
      blkn = 0;     /* index of current DCT block within MCU */
276
0
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
277
0
  compptr = cinfo->cur_comp_info[ci];
278
0
  start_col = MCU_col_num * compptr->MCU_width;
279
0
  for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
280
0
    buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
281
0
    for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
282
0
      coef->MCU_buffer[blkn++] = buffer_ptr++;
283
0
    }
284
0
  }
285
0
      }
286
      /* Try to fetch the MCU. */
287
0
      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
288
  /* Suspension forced; update state counters and exit */
289
0
  coef->MCU_vert_offset = yoffset;
290
0
  coef->MCU_ctr = MCU_col_num;
291
0
  return JPEG_SUSPENDED;
292
0
      }
293
0
    }
294
    /* Completed an MCU row, but perhaps not an iMCU row */
295
0
    coef->MCU_ctr = 0;
296
0
  }
297
  /* Completed the iMCU row, advance counters for next one */
298
0
  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
299
0
    start_iMCU_row(cinfo);
300
0
    return JPEG_ROW_COMPLETED;
301
0
  }
302
  /* Completed the scan */
303
0
  (*cinfo->inputctl->finish_input_pass) (cinfo);
304
0
  return JPEG_SCAN_COMPLETED;
305
0
}
306
307
308
/*
309
 * Decompress and return some data in the multi-pass case.
310
 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
311
 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
312
 *
313
 * NB: output_buf contains a plane for each component in image.
314
 */
315
316
METHODDEF(int)
317
decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
318
0
{
319
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
320
0
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
321
0
  JDIMENSION block_num;
322
0
  int ci, block_row, block_rows;
323
0
  JBLOCKARRAY buffer;
324
0
  JBLOCKROW buffer_ptr;
325
0
  JSAMPARRAY output_ptr;
326
0
  JDIMENSION output_col;
327
0
  jpeg_component_info *compptr;
328
0
  inverse_DCT_method_ptr inverse_DCT;
329
330
  /* Force some input to be done if we are getting ahead of the input. */
331
0
  while (cinfo->input_scan_number < cinfo->output_scan_number ||
332
0
   (cinfo->input_scan_number == cinfo->output_scan_number &&
333
0
    cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
334
0
    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
335
0
      return JPEG_SUSPENDED;
336
0
  }
337
338
  /* OK, output from the virtual arrays. */
339
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
340
0
       ci++, compptr++) {
341
    /* Don't bother to IDCT an uninteresting component. */
342
0
    if (! compptr->component_needed)
343
0
      continue;
344
    /* Align the virtual buffer for this component. */
345
0
    buffer = (*cinfo->mem->access_virt_barray)
346
0
      ((j_common_ptr) cinfo, coef->whole_image[ci],
347
0
       cinfo->output_iMCU_row * compptr->v_samp_factor,
348
0
       (JDIMENSION) compptr->v_samp_factor, FALSE);
349
    /* Count non-dummy DCT block rows in this iMCU row. */
350
0
    if (cinfo->output_iMCU_row < last_iMCU_row)
351
0
      block_rows = compptr->v_samp_factor;
352
0
    else {
353
      /* NB: can't use last_row_height here; it is input-side-dependent! */
354
0
      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
355
0
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
356
0
    }
357
0
    inverse_DCT = cinfo->idct->inverse_DCT[ci];
358
0
    output_ptr = output_buf[ci];
359
    /* Loop over all DCT blocks to be processed. */
360
0
    for (block_row = 0; block_row < block_rows; block_row++) {
361
0
      buffer_ptr = buffer[block_row];
362
0
      output_col = 0;
363
0
      for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
364
0
  (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
365
0
      output_ptr, output_col);
366
0
  buffer_ptr++;
367
0
  output_col += compptr->DCT_scaled_size;
368
0
      }
369
0
      output_ptr += compptr->DCT_scaled_size;
370
0
    }
371
0
  }
372
373
0
  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
374
0
    return JPEG_ROW_COMPLETED;
375
0
  return JPEG_SCAN_COMPLETED;
376
0
}
377
378
#endif /* D_MULTISCAN_FILES_SUPPORTED */
379
380
381
#ifdef BLOCK_SMOOTHING_SUPPORTED
382
383
/*
384
 * This code applies interblock smoothing as described by section K.8
385
 * of the JPEG standard: the first 5 AC coefficients are estimated from
386
 * the DC values of a DCT block and its 8 neighboring blocks.
387
 * We apply smoothing only for progressive JPEG decoding, and only if
388
 * the coefficients it can estimate are not yet known to full precision.
389
 */
390
391
/* Natural-order array positions of the first 5 zigzag-order coefficients */
392
0
#define Q01_POS  1
393
0
#define Q10_POS  8
394
0
#define Q20_POS  16
395
0
#define Q11_POS  9
396
0
#define Q02_POS  2
397
398
/*
399
 * Determine whether block smoothing is applicable and safe.
400
 * We also latch the current states of the coef_bits[] entries for the
401
 * AC coefficients; otherwise, if the input side of the decompressor
402
 * advances into a new scan, we might think the coefficients are known
403
 * more accurately than they really are.
404
 */
405
406
LOCAL(boolean)
407
smoothing_ok (j_decompress_ptr cinfo)
408
0
{
409
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
410
0
  boolean smoothing_useful = FALSE;
411
0
  int ci, coefi;
412
0
  jpeg_component_info *compptr;
413
0
  JQUANT_TBL * qtable;
414
0
  int * coef_bits;
415
0
  int * coef_bits_latch;
416
417
0
  if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
418
0
    return FALSE;
419
420
  /* Allocate latch area if not already done */
421
0
  if (coef->coef_bits_latch == NULL)
422
0
    coef->coef_bits_latch = (int *)
423
0
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
424
0
          cinfo->num_components *
425
0
          (SAVED_COEFS * SIZEOF(int)));
426
0
  coef_bits_latch = coef->coef_bits_latch;
427
428
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
429
0
       ci++, compptr++) {
430
    /* All components' quantization values must already be latched. */
431
0
    if ((qtable = compptr->quant_table) == NULL)
432
0
      return FALSE;
433
    /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
434
0
    if (qtable->quantval[0] == 0 ||
435
0
  qtable->quantval[Q01_POS] == 0 ||
436
0
  qtable->quantval[Q10_POS] == 0 ||
437
0
  qtable->quantval[Q20_POS] == 0 ||
438
0
  qtable->quantval[Q11_POS] == 0 ||
439
0
  qtable->quantval[Q02_POS] == 0)
440
0
      return FALSE;
441
    /* DC values must be at least partly known for all components. */
442
0
    coef_bits = cinfo->coef_bits[ci];
443
0
    if (coef_bits[0] < 0)
444
0
      return FALSE;
445
    /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
446
0
    for (coefi = 1; coefi <= 5; coefi++) {
447
0
      coef_bits_latch[coefi] = coef_bits[coefi];
448
0
      if (coef_bits[coefi] != 0)
449
0
  smoothing_useful = TRUE;
450
0
    }
451
0
    coef_bits_latch += SAVED_COEFS;
452
0
  }
453
454
0
  return smoothing_useful;
455
0
}
456
457
458
/*
459
 * Variant of decompress_data for use when doing block smoothing.
460
 */
461
462
METHODDEF(int)
463
decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
464
0
{
465
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
466
0
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
467
0
  JDIMENSION block_num, last_block_column;
468
0
  int ci, block_row, block_rows, access_rows;
469
0
  JBLOCKARRAY buffer;
470
0
  JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
471
0
  JSAMPARRAY output_ptr;
472
0
  JDIMENSION output_col;
473
0
  jpeg_component_info *compptr;
474
0
  inverse_DCT_method_ptr inverse_DCT;
475
0
  boolean first_row, last_row;
476
0
  JBLOCK workspace;
477
0
  int *coef_bits;
478
0
  JQUANT_TBL *quanttbl;
479
0
  INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
480
0
  int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
481
0
  int Al, pred;
482
483
  /* Force some input to be done if we are getting ahead of the input. */
484
0
  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
485
0
   ! cinfo->inputctl->eoi_reached) {
486
0
    if (cinfo->input_scan_number == cinfo->output_scan_number) {
487
      /* If input is working on current scan, we ordinarily want it to
488
       * have completed the current row.  But if input scan is DC,
489
       * we want it to keep one row ahead so that next block row's DC
490
       * values are up to date.
491
       */
492
0
      JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
493
0
      if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
494
0
  break;
495
0
    }
496
0
    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
497
0
      return JPEG_SUSPENDED;
498
0
  }
499
500
  /* OK, output from the virtual arrays. */
501
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
502
0
       ci++, compptr++) {
503
    /* Don't bother to IDCT an uninteresting component. */
504
0
    if (! compptr->component_needed)
505
0
      continue;
506
    /* Count non-dummy DCT block rows in this iMCU row. */
507
0
    if (cinfo->output_iMCU_row < last_iMCU_row) {
508
0
      block_rows = compptr->v_samp_factor;
509
0
      access_rows = block_rows * 2; /* this and next iMCU row */
510
0
      last_row = FALSE;
511
0
    } else {
512
      /* NB: can't use last_row_height here; it is input-side-dependent! */
513
0
      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
514
0
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
515
0
      access_rows = block_rows; /* this iMCU row only */
516
0
      last_row = TRUE;
517
0
    }
518
    /* Align the virtual buffer for this component. */
519
0
    if (cinfo->output_iMCU_row > 0) {
520
0
      access_rows += compptr->v_samp_factor; /* prior iMCU row too */
521
0
      buffer = (*cinfo->mem->access_virt_barray)
522
0
  ((j_common_ptr) cinfo, coef->whole_image[ci],
523
0
   (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
524
0
   (JDIMENSION) access_rows, FALSE);
525
0
      buffer += compptr->v_samp_factor; /* point to current iMCU row */
526
0
      first_row = FALSE;
527
0
    } else {
528
0
      buffer = (*cinfo->mem->access_virt_barray)
529
0
  ((j_common_ptr) cinfo, coef->whole_image[ci],
530
0
   (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
531
0
      first_row = TRUE;
532
0
    }
533
    /* Fetch component-dependent info */
534
0
    coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
535
0
    quanttbl = compptr->quant_table;
536
0
    Q00 = quanttbl->quantval[0];
537
0
    Q01 = quanttbl->quantval[Q01_POS];
538
0
    Q10 = quanttbl->quantval[Q10_POS];
539
0
    Q20 = quanttbl->quantval[Q20_POS];
540
0
    Q11 = quanttbl->quantval[Q11_POS];
541
0
    Q02 = quanttbl->quantval[Q02_POS];
542
0
    inverse_DCT = cinfo->idct->inverse_DCT[ci];
543
0
    output_ptr = output_buf[ci];
544
    /* Loop over all DCT blocks to be processed. */
545
0
    for (block_row = 0; block_row < block_rows; block_row++) {
546
0
      buffer_ptr = buffer[block_row];
547
0
      if (first_row && block_row == 0)
548
0
  prev_block_row = buffer_ptr;
549
0
      else
550
0
  prev_block_row = buffer[block_row-1];
551
0
      if (last_row && block_row == block_rows-1)
552
0
  next_block_row = buffer_ptr;
553
0
      else
554
0
  next_block_row = buffer[block_row+1];
555
      /* We fetch the surrounding DC values using a sliding-register approach.
556
       * Initialize all nine here so as to do the right thing on narrow pics.
557
       */
558
0
      DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
559
0
      DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
560
0
      DC7 = DC8 = DC9 = (int) next_block_row[0][0];
561
0
      output_col = 0;
562
0
      last_block_column = compptr->width_in_blocks - 1;
563
0
      for (block_num = 0; block_num <= last_block_column; block_num++) {
564
  /* Fetch current DCT block into workspace so we can modify it. */
565
0
  jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
566
  /* Update DC values */
567
0
  if (block_num < last_block_column) {
568
0
    DC3 = (int) prev_block_row[1][0];
569
0
    DC6 = (int) buffer_ptr[1][0];
570
0
    DC9 = (int) next_block_row[1][0];
571
0
  }
572
  /* Compute coefficient estimates per K.8.
573
   * An estimate is applied only if coefficient is still zero,
574
   * and is not known to be fully accurate.
575
   */
576
  /* AC01 */
577
0
  if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
578
0
    num = 36 * Q00 * (DC4 - DC6);
579
0
    if (num >= 0) {
580
0
      pred = (int) (((Q01<<7) + num) / (Q01<<8));
581
0
      if (Al > 0 && pred >= (1<<Al))
582
0
        pred = (1<<Al)-1;
583
0
    } else {
584
0
      pred = (int) (((Q01<<7) - num) / (Q01<<8));
585
0
      if (Al > 0 && pred >= (1<<Al))
586
0
        pred = (1<<Al)-1;
587
0
      pred = -pred;
588
0
    }
589
0
    workspace[1] = (JCOEF) pred;
590
0
  }
591
  /* AC10 */
592
0
  if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
593
0
    num = 36 * Q00 * (DC2 - DC8);
594
0
    if (num >= 0) {
595
0
      pred = (int) (((Q10<<7) + num) / (Q10<<8));
596
0
      if (Al > 0 && pred >= (1<<Al))
597
0
        pred = (1<<Al)-1;
598
0
    } else {
599
0
      pred = (int) (((Q10<<7) - num) / (Q10<<8));
600
0
      if (Al > 0 && pred >= (1<<Al))
601
0
        pred = (1<<Al)-1;
602
0
      pred = -pred;
603
0
    }
604
0
    workspace[8] = (JCOEF) pred;
605
0
  }
606
  /* AC20 */
607
0
  if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
608
0
    num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
609
0
    if (num >= 0) {
610
0
      pred = (int) (((Q20<<7) + num) / (Q20<<8));
611
0
      if (Al > 0 && pred >= (1<<Al))
612
0
        pred = (1<<Al)-1;
613
0
    } else {
614
0
      pred = (int) (((Q20<<7) - num) / (Q20<<8));
615
0
      if (Al > 0 && pred >= (1<<Al))
616
0
        pred = (1<<Al)-1;
617
0
      pred = -pred;
618
0
    }
619
0
    workspace[16] = (JCOEF) pred;
620
0
  }
621
  /* AC11 */
622
0
  if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
623
0
    num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
624
0
    if (num >= 0) {
625
0
      pred = (int) (((Q11<<7) + num) / (Q11<<8));
626
0
      if (Al > 0 && pred >= (1<<Al))
627
0
        pred = (1<<Al)-1;
628
0
    } else {
629
0
      pred = (int) (((Q11<<7) - num) / (Q11<<8));
630
0
      if (Al > 0 && pred >= (1<<Al))
631
0
        pred = (1<<Al)-1;
632
0
      pred = -pred;
633
0
    }
634
0
    workspace[9] = (JCOEF) pred;
635
0
  }
636
  /* AC02 */
637
0
  if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
638
0
    num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
639
0
    if (num >= 0) {
640
0
      pred = (int) (((Q02<<7) + num) / (Q02<<8));
641
0
      if (Al > 0 && pred >= (1<<Al))
642
0
        pred = (1<<Al)-1;
643
0
    } else {
644
0
      pred = (int) (((Q02<<7) - num) / (Q02<<8));
645
0
      if (Al > 0 && pred >= (1<<Al))
646
0
        pred = (1<<Al)-1;
647
0
      pred = -pred;
648
0
    }
649
0
    workspace[2] = (JCOEF) pred;
650
0
  }
651
  /* OK, do the IDCT */
652
0
  (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
653
0
      output_ptr, output_col);
654
  /* Advance for next column */
655
0
  DC1 = DC2; DC2 = DC3;
656
0
  DC4 = DC5; DC5 = DC6;
657
0
  DC7 = DC8; DC8 = DC9;
658
0
  buffer_ptr++;
659
0
    prev_block_row++;
660
0
    next_block_row++;
661
0
  output_col += compptr->DCT_scaled_size;
662
0
      }
663
0
      output_ptr += compptr->DCT_scaled_size;
664
0
    }
665
0
  }
666
667
0
  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
668
0
    return JPEG_ROW_COMPLETED;
669
0
  return JPEG_SCAN_COMPLETED;
670
0
}
671
672
#endif /* BLOCK_SMOOTHING_SUPPORTED */
673
674
675
/*
676
 * Initialize coefficient buffer controller.
677
 */
678
679
GLOBAL(void)
680
jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
681
0
{
682
0
  my_coef_ptr coef;
683
684
0
  coef = (my_coef_ptr)
685
0
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
686
0
        SIZEOF(my_coef_controller));
687
0
  cinfo->coef = (struct jpeg_d_coef_controller *) coef;
688
0
  coef->pub.start_input_pass = start_input_pass;
689
0
  coef->pub.start_output_pass = start_output_pass;
690
0
#ifdef BLOCK_SMOOTHING_SUPPORTED
691
0
  coef->coef_bits_latch = NULL;
692
0
#endif
693
694
  /* Create the coefficient buffer. */
695
0
  if (need_full_buffer) {
696
0
#ifdef D_MULTISCAN_FILES_SUPPORTED
697
    /* Allocate a full-image virtual array for each component, */
698
    /* padded to a multiple of samp_factor DCT blocks in each direction. */
699
    /* Note we ask for a pre-zeroed array. */
700
0
    int ci, access_rows;
701
0
    jpeg_component_info *compptr;
702
703
0
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
704
0
   ci++, compptr++) {
705
0
      access_rows = compptr->v_samp_factor;
706
0
#ifdef BLOCK_SMOOTHING_SUPPORTED
707
      /* If block smoothing could be used, need a bigger window */
708
0
      if (cinfo->progressive_mode)
709
0
  access_rows *= 3;
710
0
#endif
711
0
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
712
0
  ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
713
0
   (JDIMENSION) jround_up((long) compptr->width_in_blocks,
714
0
        (long) compptr->h_samp_factor),
715
0
   (JDIMENSION) jround_up((long) compptr->height_in_blocks,
716
0
        (long) compptr->v_samp_factor),
717
0
   (JDIMENSION) access_rows);
718
0
    }
719
0
    coef->pub.consume_data = consume_data;
720
0
    coef->pub.decompress_data = decompress_data;
721
0
    coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
722
#else
723
    ERREXIT(cinfo, JERR_NOT_COMPILED);
724
#endif
725
0
  } else {
726
    /* We only need a single-MCU buffer. */
727
0
    JBLOCKROW buffer;
728
0
    int i;
729
730
0
    buffer = (JBLOCKROW)
731
0
      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
732
0
          D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
733
0
    for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
734
0
      coef->MCU_buffer[i] = buffer + i;
735
0
    }
736
0
    coef->pub.consume_data = dummy_consume_data;
737
0
    coef->pub.decompress_data = decompress_onepass;
738
0
    coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
739
0
  }
740
0
}