Coverage Report

Created: 2026-02-26 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.0.x/transupp.c
Line
Count
Source
1
/*
2
 * transupp.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1997-2019, Thomas G. Lane, Guido Vollbeding.
6
 * libjpeg-turbo Modifications:
7
 * Copyright (C) 2010, 2017, 2021-2022, 2024, 2026, D. R. Commander.
8
 * For conditions of distribution and use, see the accompanying README.ijg
9
 * file.
10
 *
11
 * This file contains image transformation routines and other utility code
12
 * used by the jpegtran sample application.  These are NOT part of the core
13
 * JPEG library.  But we keep these routines separate from jpegtran.c to
14
 * ease the task of maintaining jpegtran-like programs that have other user
15
 * interfaces.
16
 */
17
18
/* Although this file really shouldn't have access to the library internals,
19
 * it's helpful to let it call jround_up() and jcopy_block_row().
20
 */
21
#define JPEG_INTERNALS
22
23
#include "jinclude.h"
24
#include "jpeglib.h"
25
#include "transupp.h"           /* My own external interface */
26
#include "jpegapicomp.h"
27
#include <ctype.h>              /* to declare isdigit() */
28
29
30
#if JPEG_LIB_VERSION >= 70
31
#define dstinfo_min_DCT_h_scaled_size  dstinfo->min_DCT_h_scaled_size
32
#define dstinfo_min_DCT_v_scaled_size  dstinfo->min_DCT_v_scaled_size
33
#else
34
0
#define dstinfo_min_DCT_h_scaled_size  DCTSIZE
35
0
#define dstinfo_min_DCT_v_scaled_size  DCTSIZE
36
#endif
37
38
39
#if TRANSFORMS_SUPPORTED
40
41
/*
42
 * Lossless image transformation routines.  These routines work on DCT
43
 * coefficient arrays and thus do not require any lossy decompression
44
 * or recompression of the image.
45
 * Thanks to Guido Vollbeding for the initial design and code of this feature,
46
 * and to Ben Jackson for introducing the cropping feature.
47
 *
48
 * Horizontal flipping is done in-place, using a single top-to-bottom
49
 * pass through the virtual source array.  It will thus be much the
50
 * fastest option for images larger than main memory.
51
 *
52
 * The other routines require a set of destination virtual arrays, so they
53
 * need twice as much memory as jpegtran normally does.  The destination
54
 * arrays are always written in normal scan order (top to bottom) because
55
 * the virtual array manager expects this.  The source arrays will be scanned
56
 * in the corresponding order, which means multiple passes through the source
57
 * arrays for most of the transforms.  That could result in much thrashing
58
 * if the image is larger than main memory.
59
 *
60
 * If cropping or trimming is involved, the destination arrays may be smaller
61
 * than the source arrays.  Note it is not possible to do horizontal flip
62
 * in-place when a nonzero Y crop offset is specified, since we'd have to move
63
 * data from one block row to another but the virtual array manager doesn't
64
 * guarantee we can touch more than one row at a time.  So in that case,
65
 * we have to use a separate destination array.
66
 *
67
 * Some notes about the operating environment of the individual transform
68
 * routines:
69
 * 1. Both the source and destination virtual arrays are allocated from the
70
 *    source JPEG object, and therefore should be manipulated by calling the
71
 *    source's memory manager.
72
 * 2. The destination's component count should be used.  It may be smaller
73
 *    than the source's when forcing to grayscale.
74
 * 3. Likewise the destination's sampling factors should be used.  When
75
 *    forcing to grayscale the destination's sampling factors will be all 1,
76
 *    and we may as well take that as the effective iMCU size.
77
 * 4. When "trim" is in effect, the destination's dimensions will be the
78
 *    trimmed values but the source's will be untrimmed.
79
 * 5. When "crop" is in effect, the destination's dimensions will be the
80
 *    cropped values but the source's will be uncropped.  Each transform
81
 *    routine is responsible for picking up source data starting at the
82
 *    correct X and Y offset for the crop region.  (The X and Y offsets
83
 *    passed to the transform routines are measured in iMCU blocks of the
84
 *    destination.)
85
 * 6. All the routines assume that the source and destination buffers are
86
 *    padded out to a full iMCU boundary.  This is true, although for the
87
 *    source buffer it is an undocumented property of jdcoefct.c.
88
 */
89
90
91
LOCAL(void)
92
dequant_comp(j_decompress_ptr cinfo, jpeg_component_info *compptr,
93
             jvirt_barray_ptr coef_array, JQUANT_TBL *qtblptr1)
94
0
{
95
0
  JDIMENSION blk_x, blk_y;
96
0
  int offset_y, k;
97
0
  JQUANT_TBL *qtblptr;
98
0
  JBLOCKARRAY buffer;
99
0
  JBLOCKROW block;
100
0
  JCOEFPTR ptr;
101
102
0
  qtblptr = compptr->quant_table;
103
0
  for (blk_y = 0; blk_y < compptr->height_in_blocks;
104
0
       blk_y += compptr->v_samp_factor) {
105
0
    buffer = (*cinfo->mem->access_virt_barray)
106
0
      ((j_common_ptr)cinfo, coef_array, blk_y,
107
0
       (JDIMENSION)compptr->v_samp_factor, TRUE);
108
0
    for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
109
0
      block = buffer[offset_y];
110
0
      for (blk_x = 0; blk_x < compptr->width_in_blocks; blk_x++) {
111
0
        ptr = block[blk_x];
112
0
        for (k = 0; k < DCTSIZE2; k++)
113
0
          if (qtblptr->quantval[k] != qtblptr1->quantval[k]) {
114
0
            ptr[k] *= qtblptr->quantval[k];
115
0
            if (qtblptr1->quantval[k] != 0)
116
0
              ptr[k] /= qtblptr1->quantval[k];
117
0
          }
118
0
      }
119
0
    }
120
0
  }
121
0
}
122
123
124
LOCAL(void)
125
requant_comp(j_decompress_ptr cinfo, jpeg_component_info *compptr,
126
             jvirt_barray_ptr coef_array, JQUANT_TBL *qtblptr1)
127
0
{
128
0
  JDIMENSION blk_x, blk_y;
129
0
  int offset_y, k;
130
0
  JQUANT_TBL *qtblptr;
131
0
  JBLOCKARRAY buffer;
132
0
  JBLOCKROW block;
133
0
  JCOEFPTR ptr;
134
0
  JCOEF temp, qval;
135
136
0
  qtblptr = compptr->quant_table;
137
0
  for (blk_y = 0; blk_y < compptr->height_in_blocks;
138
0
       blk_y += compptr->v_samp_factor) {
139
0
    buffer = (*cinfo->mem->access_virt_barray)
140
0
      ((j_common_ptr)cinfo, coef_array, blk_y,
141
0
       (JDIMENSION)compptr->v_samp_factor, TRUE);
142
0
    for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
143
0
      block = buffer[offset_y];
144
0
      for (blk_x = 0; blk_x < compptr->width_in_blocks; blk_x++) {
145
0
        ptr = block[blk_x];
146
0
        for (k = 0; k < DCTSIZE2; k++) {
147
0
          temp = qtblptr->quantval[k];
148
0
          qval = qtblptr1->quantval[k];
149
0
          if (temp != qval && qval != 0) {
150
0
            temp *= ptr[k];
151
            /* The following quantization code is copied from jcdctmgr.c */
152
#ifdef FAST_DIVIDE
153
#define DIVIDE_BY(a, b)  a /= b
154
#else
155
0
#define DIVIDE_BY(a, b)  if (a >= b) a /= b;  else a = 0
156
0
#endif
157
0
            if (temp < 0) {
158
0
              temp = -temp;
159
0
              temp += qval >> 1; /* for rounding */
160
0
              DIVIDE_BY(temp, qval);
161
0
              temp = -temp;
162
0
            } else {
163
0
              temp += qval >> 1; /* for rounding */
164
0
              DIVIDE_BY(temp, qval);
165
0
            }
166
0
            ptr[k] = temp;
167
0
          }
168
0
        }
169
0
      }
170
0
    }
171
0
  }
172
0
}
173
174
175
/*
176
 * Calculate largest common denominator using Euclid's algorithm.
177
 */
178
LOCAL(JCOEF)
179
largest_common_denominator(JCOEF a, JCOEF b)
180
0
{
181
0
  JCOEF c;
182
183
0
  if (a == 0) return b;
184
0
  if (b == 0) return a;
185
186
0
  do {
187
0
    c = a % b;
188
0
    a = b;
189
0
    b = c;
190
0
  } while (c);
191
192
0
  return a;
193
0
}
194
195
196
LOCAL(void)
197
adjust_quant(j_decompress_ptr srcinfo, jvirt_barray_ptr *src_coef_arrays,
198
             j_decompress_ptr dropinfo, jvirt_barray_ptr *drop_coef_arrays,
199
             boolean trim, j_compress_ptr dstinfo)
200
0
{
201
0
  jpeg_component_info *compptr1, *compptr2;
202
0
  JQUANT_TBL *qtblptr1, *qtblptr2, *qtblptr3;
203
0
  int ci, k;
204
205
0
  for (ci = 0; ci < dstinfo->num_components && ci < dropinfo->num_components;
206
0
       ci++) {
207
0
    compptr1 = srcinfo->comp_info + ci;
208
0
    compptr2 = dropinfo->comp_info + ci;
209
0
    qtblptr1 = compptr1->quant_table;
210
0
    if (qtblptr1 == NULL)
211
0
      ERREXIT1(srcinfo, JERR_NO_QUANT_TABLE, compptr1->quant_tbl_no);
212
0
    qtblptr2 = compptr2->quant_table;
213
0
    if (qtblptr2 == NULL)
214
0
      ERREXIT1(dropinfo, JERR_NO_QUANT_TABLE, compptr2->quant_tbl_no);
215
0
    for (k = 0; k < DCTSIZE2; k++) {
216
0
      if (qtblptr1->quantval[k] != qtblptr2->quantval[k]) {
217
0
        if (trim)
218
0
          requant_comp(dropinfo, compptr2, drop_coef_arrays[ci], qtblptr1);
219
0
        else {
220
0
          qtblptr3 = dstinfo->quant_tbl_ptrs[compptr1->quant_tbl_no];
221
0
          for (k = 0; k < DCTSIZE2; k++)
222
0
            if (qtblptr1->quantval[k] != qtblptr2->quantval[k])
223
0
              qtblptr3->quantval[k] =
224
0
                largest_common_denominator(qtblptr1->quantval[k],
225
0
                                           qtblptr2->quantval[k]);
226
0
          dequant_comp(srcinfo, compptr1, src_coef_arrays[ci], qtblptr3);
227
0
          dequant_comp(dropinfo, compptr2, drop_coef_arrays[ci], qtblptr3);
228
0
        }
229
0
        break;
230
0
      }
231
0
    }
232
0
  }
233
0
}
234
235
236
LOCAL(void)
237
do_drop(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
238
        JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
239
        jvirt_barray_ptr *src_coef_arrays,
240
        j_decompress_ptr dropinfo, jvirt_barray_ptr *drop_coef_arrays,
241
        JDIMENSION drop_width, JDIMENSION drop_height)
242
/* Drop (insert) the contents of another image into the source image.  If the
243
 * number of components in the drop image is smaller than the number of
244
 * components in the destination image, then we fill in the remaining
245
 * components with zero.  This allows for dropping the contents of grayscale
246
 * images into (arbitrarily sampled) color images.
247
 */
248
0
{
249
0
  JDIMENSION comp_width, comp_height;
250
0
  JDIMENSION blk_y, x_drop_blocks, y_drop_blocks;
251
0
  int ci, offset_y;
252
0
  JBLOCKARRAY src_buffer, dst_buffer;
253
0
  jpeg_component_info *compptr;
254
255
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
256
0
    compptr = dstinfo->comp_info + ci;
257
0
    comp_width = drop_width * compptr->h_samp_factor;
258
0
    comp_height = drop_height * compptr->v_samp_factor;
259
0
    x_drop_blocks = x_crop_offset * compptr->h_samp_factor;
260
0
    y_drop_blocks = y_crop_offset * compptr->v_samp_factor;
261
0
    for (blk_y = 0; blk_y < comp_height; blk_y += compptr->v_samp_factor) {
262
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
263
0
        ((j_common_ptr)srcinfo, src_coef_arrays[ci], blk_y + y_drop_blocks,
264
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
265
0
      if (ci < dropinfo->num_components) {
266
0
        src_buffer = (*dropinfo->mem->access_virt_barray)
267
0
          ((j_common_ptr)dropinfo, drop_coef_arrays[ci], blk_y,
268
0
           (JDIMENSION)compptr->v_samp_factor, FALSE);
269
0
        for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
270
0
          jcopy_block_row(src_buffer[offset_y],
271
0
                          dst_buffer[offset_y] + x_drop_blocks, comp_width);
272
0
        }
273
0
      } else {
274
0
        for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
275
0
          memset(dst_buffer[offset_y] + x_drop_blocks, 0,
276
0
                 comp_width * sizeof(JBLOCK));
277
0
        }
278
0
      }
279
0
    }
280
0
  }
281
0
}
282
283
284
LOCAL(void)
285
do_crop(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
286
        JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
287
        jvirt_barray_ptr *src_coef_arrays,
288
        jvirt_barray_ptr *dst_coef_arrays)
289
/* Crop.  This is only used when no rotate/flip is requested with the crop. */
290
0
{
291
0
  JDIMENSION dst_blk_y, x_crop_blocks, y_crop_blocks;
292
0
  int ci, offset_y;
293
0
  JBLOCKARRAY src_buffer, dst_buffer;
294
0
  jpeg_component_info *compptr;
295
296
  /* We simply have to copy the right amount of data (the destination's
297
   * image size) starting at the given X and Y offsets in the source.
298
   */
299
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
300
0
    compptr = dstinfo->comp_info + ci;
301
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
302
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
303
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
304
0
         dst_blk_y += compptr->v_samp_factor) {
305
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
306
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
307
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
308
0
      src_buffer = (*srcinfo->mem->access_virt_barray)
309
0
        ((j_common_ptr)srcinfo, src_coef_arrays[ci], dst_blk_y + y_crop_blocks,
310
0
         (JDIMENSION)compptr->v_samp_factor, FALSE);
311
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
312
0
        jcopy_block_row(src_buffer[offset_y] + x_crop_blocks,
313
0
                        dst_buffer[offset_y], compptr->width_in_blocks);
314
0
      }
315
0
    }
316
0
  }
317
0
}
318
319
320
LOCAL(void)
321
do_crop_ext_zero(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
322
                 JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
323
                 jvirt_barray_ptr *src_coef_arrays,
324
                 jvirt_barray_ptr *dst_coef_arrays)
325
/* Crop.  This is only used when no rotate/flip is requested with the crop.
326
 * Extension: If the destination size is larger than the source, we fill in the
327
 * expanded region with zero (neutral gray).  Note that we also have to zero
328
 * partial iMCUs at the right and bottom edge of the source image area in this
329
 * case.
330
 */
331
0
{
332
0
  JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height;
333
0
  JDIMENSION dst_blk_y, x_crop_blocks, y_crop_blocks;
334
0
  int ci, offset_y;
335
0
  JBLOCKARRAY src_buffer, dst_buffer;
336
0
  jpeg_component_info *compptr;
337
338
0
  MCU_cols = srcinfo->output_width /
339
0
             (dstinfo->max_h_samp_factor * dstinfo_min_DCT_h_scaled_size);
340
0
  MCU_rows = srcinfo->output_height /
341
0
             (dstinfo->max_v_samp_factor * dstinfo_min_DCT_v_scaled_size);
342
343
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
344
0
    compptr = dstinfo->comp_info + ci;
345
0
    comp_width = MCU_cols * compptr->h_samp_factor;
346
0
    comp_height = MCU_rows * compptr->v_samp_factor;
347
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
348
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
349
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
350
0
         dst_blk_y += compptr->v_samp_factor) {
351
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
352
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
353
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
354
0
      if (dstinfo->_jpeg_height > srcinfo->output_height) {
355
0
        if (dst_blk_y < y_crop_blocks ||
356
0
            dst_blk_y >= y_crop_blocks + comp_height) {
357
0
          for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
358
0
            memset(dst_buffer[offset_y], 0,
359
0
                   compptr->width_in_blocks * sizeof(JBLOCK));
360
0
          }
361
0
          continue;
362
0
        }
363
0
        src_buffer = (*srcinfo->mem->access_virt_barray)
364
0
          ((j_common_ptr)srcinfo, src_coef_arrays[ci],
365
0
           dst_blk_y - y_crop_blocks, (JDIMENSION)compptr->v_samp_factor,
366
0
           FALSE);
367
0
      } else {
368
0
        src_buffer = (*srcinfo->mem->access_virt_barray)
369
0
          ((j_common_ptr)srcinfo, src_coef_arrays[ci],
370
0
           dst_blk_y + y_crop_blocks, (JDIMENSION)compptr->v_samp_factor,
371
0
           FALSE);
372
0
      }
373
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
374
0
        if (dstinfo->_jpeg_width > srcinfo->output_width) {
375
0
          if (x_crop_blocks > 0) {
376
0
            memset(dst_buffer[offset_y], 0, x_crop_blocks * sizeof(JBLOCK));
377
0
          }
378
0
          jcopy_block_row(src_buffer[offset_y],
379
0
                          dst_buffer[offset_y] + x_crop_blocks, comp_width);
380
0
          if (compptr->width_in_blocks > x_crop_blocks + comp_width) {
381
0
            memset(dst_buffer[offset_y] + x_crop_blocks + comp_width, 0,
382
0
                   (compptr->width_in_blocks - x_crop_blocks - comp_width) *
383
0
                   sizeof(JBLOCK));
384
0
          }
385
0
        } else {
386
0
          jcopy_block_row(src_buffer[offset_y] + x_crop_blocks,
387
0
                          dst_buffer[offset_y], compptr->width_in_blocks);
388
0
        }
389
0
      }
390
0
    }
391
0
  }
392
0
}
393
394
395
LOCAL(void)
396
do_crop_ext_flat(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
397
                 JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
398
                 jvirt_barray_ptr *src_coef_arrays,
399
                 jvirt_barray_ptr *dst_coef_arrays)
400
/* Crop.  This is only used when no rotate/flip is requested with the crop.
401
 * Extension: The destination width is larger than the source, and we fill in
402
 * the expanded region with the DC coefficient of the adjacent block.  Note
403
 * that we also have to fill partial iMCUs at the right and bottom edge of the
404
 * source image area in this case.
405
 */
406
0
{
407
0
  JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height;
408
0
  JDIMENSION dst_blk_x, dst_blk_y, x_crop_blocks, y_crop_blocks;
409
0
  int ci, offset_y;
410
0
  JCOEF dc;
411
0
  JBLOCKARRAY src_buffer, dst_buffer;
412
0
  jpeg_component_info *compptr;
413
414
0
  MCU_cols = srcinfo->output_width /
415
0
             (dstinfo->max_h_samp_factor * dstinfo_min_DCT_h_scaled_size);
416
0
  MCU_rows = srcinfo->output_height /
417
0
             (dstinfo->max_v_samp_factor * dstinfo_min_DCT_v_scaled_size);
418
419
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
420
0
    compptr = dstinfo->comp_info + ci;
421
0
    comp_width = MCU_cols * compptr->h_samp_factor;
422
0
    comp_height = MCU_rows * compptr->v_samp_factor;
423
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
424
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
425
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
426
0
         dst_blk_y += compptr->v_samp_factor) {
427
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
428
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
429
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
430
0
      if (dstinfo->_jpeg_height > srcinfo->output_height) {
431
0
        if (dst_blk_y < y_crop_blocks ||
432
0
            dst_blk_y >= y_crop_blocks + comp_height) {
433
0
          for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
434
0
            memset(dst_buffer[offset_y], 0,
435
0
                   compptr->width_in_blocks * sizeof(JBLOCK));
436
0
          }
437
0
          continue;
438
0
        }
439
0
        src_buffer = (*srcinfo->mem->access_virt_barray)
440
0
          ((j_common_ptr)srcinfo, src_coef_arrays[ci],
441
0
           dst_blk_y - y_crop_blocks, (JDIMENSION)compptr->v_samp_factor,
442
0
           FALSE);
443
0
      } else {
444
0
        src_buffer = (*srcinfo->mem->access_virt_barray)
445
0
          ((j_common_ptr)srcinfo, src_coef_arrays[ci],
446
0
           dst_blk_y + y_crop_blocks, (JDIMENSION)compptr->v_samp_factor,
447
0
          FALSE);
448
0
      }
449
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
450
0
        if (x_crop_blocks > 0) {
451
0
          memset(dst_buffer[offset_y], 0, x_crop_blocks * sizeof(JBLOCK));
452
0
          dc = src_buffer[offset_y][0][0];
453
0
          for (dst_blk_x = 0; dst_blk_x < x_crop_blocks; dst_blk_x++) {
454
0
            dst_buffer[offset_y][dst_blk_x][0] = dc;
455
0
          }
456
0
        }
457
0
        jcopy_block_row(src_buffer[offset_y],
458
0
                        dst_buffer[offset_y] + x_crop_blocks, comp_width);
459
0
        if (compptr->width_in_blocks > x_crop_blocks + comp_width) {
460
0
          memset(dst_buffer[offset_y] + x_crop_blocks + comp_width, 0,
461
0
                 (compptr->width_in_blocks - x_crop_blocks - comp_width) *
462
0
                 sizeof(JBLOCK));
463
0
          dc = src_buffer[offset_y][comp_width - 1][0];
464
0
          for (dst_blk_x = x_crop_blocks + comp_width;
465
0
               dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
466
0
            dst_buffer[offset_y][dst_blk_x][0] = dc;
467
0
          }
468
0
        }
469
0
      }
470
0
    }
471
0
  }
472
0
}
473
474
475
LOCAL(void)
476
do_crop_ext_reflect(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
477
                    JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
478
                    jvirt_barray_ptr *src_coef_arrays,
479
                    jvirt_barray_ptr *dst_coef_arrays)
480
/* Crop.  This is only used when no rotate/flip is requested with the crop.
481
 * Extension: The destination width is larger than the source, and we fill in
482
 * the expanded region with repeated reflections of the source image.  Note
483
 * that we also have to fill partial iMCUs at the right and bottom edge of the
484
 * source image area in this case.
485
 */
486
0
{
487
0
  JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, src_blk_x;
488
0
  JDIMENSION dst_blk_x, dst_blk_y, x_crop_blocks, y_crop_blocks;
489
0
  int ci, k, offset_y;
490
0
  JBLOCKARRAY src_buffer, dst_buffer;
491
0
  JBLOCKROW src_row_ptr, dst_row_ptr;
492
0
  JCOEFPTR src_ptr, dst_ptr;
493
0
  jpeg_component_info *compptr;
494
495
0
  MCU_cols = srcinfo->output_width /
496
0
             (dstinfo->max_h_samp_factor * dstinfo_min_DCT_h_scaled_size);
497
0
  MCU_rows = srcinfo->output_height /
498
0
             (dstinfo->max_v_samp_factor * dstinfo_min_DCT_v_scaled_size);
499
500
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
501
0
    compptr = dstinfo->comp_info + ci;
502
0
    comp_width = MCU_cols * compptr->h_samp_factor;
503
0
    comp_height = MCU_rows * compptr->v_samp_factor;
504
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
505
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
506
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
507
0
         dst_blk_y += compptr->v_samp_factor) {
508
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
509
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
510
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
511
0
      if (dstinfo->_jpeg_height > srcinfo->output_height) {
512
0
        if (dst_blk_y < y_crop_blocks ||
513
0
            dst_blk_y >= y_crop_blocks + comp_height) {
514
0
          for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
515
0
            memset(dst_buffer[offset_y], 0,
516
0
                   compptr->width_in_blocks * sizeof(JBLOCK));
517
0
          }
518
0
          continue;
519
0
        }
520
0
        src_buffer = (*srcinfo->mem->access_virt_barray)
521
0
          ((j_common_ptr)srcinfo, src_coef_arrays[ci],
522
0
           dst_blk_y - y_crop_blocks, (JDIMENSION)compptr->v_samp_factor,
523
0
           FALSE);
524
0
      } else {
525
0
        src_buffer = (*srcinfo->mem->access_virt_barray)
526
0
          ((j_common_ptr)srcinfo, src_coef_arrays[ci],
527
0
           dst_blk_y + y_crop_blocks, (JDIMENSION)compptr->v_samp_factor,
528
0
           FALSE);
529
0
      }
530
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
531
        /* Copy source region */
532
0
        jcopy_block_row(src_buffer[offset_y],
533
0
                        dst_buffer[offset_y] + x_crop_blocks, comp_width);
534
0
        if (x_crop_blocks > 0) {
535
          /* Reflect to left */
536
0
          dst_row_ptr = dst_buffer[offset_y] + x_crop_blocks;
537
0
          for (dst_blk_x = x_crop_blocks; dst_blk_x > 0;) {
538
0
            src_row_ptr = dst_row_ptr;      /* (re)set axis of reflection */
539
0
            for (src_blk_x = comp_width; src_blk_x > 0 && dst_blk_x > 0;
540
0
                 src_blk_x--, dst_blk_x--) {
541
0
              dst_ptr = *(--dst_row_ptr);   /* destination goes left */
542
0
              src_ptr = *src_row_ptr++;     /* source goes right */
543
              /* This unrolled loop doesn't need to know which row it's on. */
544
0
              for (k = 0; k < DCTSIZE2; k += 2) {
545
0
                *dst_ptr++ = *src_ptr++;    /* copy even column */
546
0
                *dst_ptr++ = -(*src_ptr++); /* copy odd column with sign
547
                                               change */
548
0
              }
549
0
            }
550
0
          }
551
0
        }
552
0
        if (compptr->width_in_blocks > x_crop_blocks + comp_width) {
553
          /* Reflect to right */
554
0
          dst_row_ptr = dst_buffer[offset_y] + x_crop_blocks + comp_width;
555
0
          for (dst_blk_x = compptr->width_in_blocks - x_crop_blocks - comp_width;
556
0
               dst_blk_x > 0;) {
557
0
            src_row_ptr = dst_row_ptr;      /* (re)set axis of reflection */
558
0
            for (src_blk_x = comp_width; src_blk_x > 0 && dst_blk_x > 0;
559
0
                 src_blk_x--, dst_blk_x--) {
560
0
              dst_ptr = *dst_row_ptr++;     /* destination goes right */
561
0
              src_ptr = *(--src_row_ptr);   /* source goes left */
562
              /* This unrolled loop doesn't need to know which row it's on. */
563
0
              for (k = 0; k < DCTSIZE2; k += 2) {
564
0
                *dst_ptr++ = *src_ptr++;    /* copy even column */
565
0
                *dst_ptr++ = -(*src_ptr++); /* copy odd column with sign
566
                                               change */
567
0
              }
568
0
            }
569
0
          }
570
0
        }
571
0
      }
572
0
    }
573
0
  }
574
0
}
575
576
577
LOCAL(void)
578
do_wipe(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
579
        JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
580
        jvirt_barray_ptr *src_coef_arrays,
581
        JDIMENSION drop_width, JDIMENSION drop_height)
582
/* Wipe - discard image contents of specified region and fill with zero
583
 * (neutral gray)
584
 */
585
0
{
586
0
  JDIMENSION x_wipe_blocks, wipe_width;
587
0
  JDIMENSION y_wipe_blocks, wipe_bottom;
588
0
  int ci, offset_y;
589
0
  JBLOCKARRAY buffer;
590
0
  jpeg_component_info *compptr;
591
592
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
593
0
    compptr = dstinfo->comp_info + ci;
594
0
    x_wipe_blocks = x_crop_offset * compptr->h_samp_factor;
595
0
    wipe_width = drop_width * compptr->h_samp_factor;
596
0
    y_wipe_blocks = y_crop_offset * compptr->v_samp_factor;
597
0
    wipe_bottom = drop_height * compptr->v_samp_factor + y_wipe_blocks;
598
0
    for (; y_wipe_blocks < wipe_bottom;
599
0
         y_wipe_blocks += compptr->v_samp_factor) {
600
0
      buffer = (*srcinfo->mem->access_virt_barray)
601
0
        ((j_common_ptr)srcinfo, src_coef_arrays[ci], y_wipe_blocks,
602
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
603
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
604
0
        memset(buffer[offset_y] + x_wipe_blocks, 0,
605
0
               wipe_width * sizeof(JBLOCK));
606
0
      }
607
0
    }
608
0
  }
609
0
}
610
611
612
LOCAL(void)
613
do_flatten(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
614
           JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
615
           jvirt_barray_ptr *src_coef_arrays,
616
           JDIMENSION drop_width, JDIMENSION drop_height)
617
/* Flatten - discard image contents of specified region, similarly to wipe,
618
 * but fill with the average of adjacent blocks instead of zero.
619
 */
620
0
{
621
0
  JDIMENSION x_wipe_blocks, wipe_width, wipe_right;
622
0
  JDIMENSION y_wipe_blocks, wipe_bottom, blk_x;
623
0
  int ci, offset_y, dc_left_value, dc_right_value, average;
624
0
  JBLOCKARRAY buffer;
625
0
  jpeg_component_info *compptr;
626
627
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
628
0
    compptr = dstinfo->comp_info + ci;
629
0
    x_wipe_blocks = x_crop_offset * compptr->h_samp_factor;
630
0
    wipe_width = drop_width * compptr->h_samp_factor;
631
0
    wipe_right = wipe_width + x_wipe_blocks;
632
0
    y_wipe_blocks = y_crop_offset * compptr->v_samp_factor;
633
0
    wipe_bottom = drop_height * compptr->v_samp_factor + y_wipe_blocks;
634
0
    for (; y_wipe_blocks < wipe_bottom;
635
0
         y_wipe_blocks += compptr->v_samp_factor) {
636
0
      buffer = (*srcinfo->mem->access_virt_barray)
637
0
        ((j_common_ptr)srcinfo, src_coef_arrays[ci], y_wipe_blocks,
638
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
639
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
640
0
        memset(buffer[offset_y] + x_wipe_blocks, 0,
641
0
               wipe_width * sizeof(JBLOCK));
642
0
        if (x_wipe_blocks > 0) {
643
0
          dc_left_value = buffer[offset_y][x_wipe_blocks - 1][0];
644
0
          if (wipe_right < compptr->width_in_blocks) {
645
0
            dc_right_value = buffer[offset_y][wipe_right][0];
646
0
            average = (dc_left_value + dc_right_value) >> 1;
647
0
          } else {
648
0
            average = dc_left_value;
649
0
          }
650
0
        } else if (wipe_right < compptr->width_in_blocks) {
651
0
          average = buffer[offset_y][wipe_right][0];
652
0
        } else continue;
653
0
        for (blk_x = x_wipe_blocks; blk_x < wipe_right; blk_x++) {
654
0
          buffer[offset_y][blk_x][0] = (JCOEF)average;
655
0
        }
656
0
      }
657
0
    }
658
0
  }
659
0
}
660
661
662
LOCAL(void)
663
do_reflect(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
664
           JDIMENSION x_crop_offset, jvirt_barray_ptr *src_coef_arrays,
665
           JDIMENSION drop_width, JDIMENSION drop_height)
666
/* Reflect - discard image contents of specified region, similarly to wipe,
667
 * but fill with repeated reflections of the outside region instead of zero.
668
 * NB: y_crop_offset is assumed to be zero.
669
 */
670
0
{
671
0
  JDIMENSION x_wipe_blocks, wipe_width;
672
0
  JDIMENSION y_wipe_blocks, wipe_bottom;
673
0
  JDIMENSION src_blk_x, dst_blk_x;
674
0
  int ci, k, offset_y;
675
0
  JBLOCKARRAY buffer;
676
0
  JBLOCKROW src_row_ptr, dst_row_ptr;
677
0
  JCOEFPTR src_ptr, dst_ptr;
678
0
  jpeg_component_info *compptr;
679
680
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
681
0
    compptr = dstinfo->comp_info + ci;
682
0
    x_wipe_blocks = x_crop_offset * compptr->h_samp_factor;
683
0
    wipe_width = drop_width * compptr->h_samp_factor;
684
0
    wipe_bottom = drop_height * compptr->v_samp_factor;
685
0
    for (y_wipe_blocks = 0; y_wipe_blocks < wipe_bottom;
686
0
         y_wipe_blocks += compptr->v_samp_factor) {
687
0
      buffer = (*srcinfo->mem->access_virt_barray)
688
0
        ((j_common_ptr)srcinfo, src_coef_arrays[ci], y_wipe_blocks,
689
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
690
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
691
0
        if (x_wipe_blocks > 0) {
692
          /* Reflect from left */
693
0
          dst_row_ptr = buffer[offset_y] + x_wipe_blocks;
694
0
          for (dst_blk_x = wipe_width; dst_blk_x > 0;) {
695
0
            src_row_ptr = dst_row_ptr;     /* (re)set axis of reflection */
696
0
            for (src_blk_x = x_wipe_blocks;
697
0
                 src_blk_x > 0 && dst_blk_x > 0; src_blk_x--, dst_blk_x--) {
698
0
              dst_ptr = *dst_row_ptr++;    /* destination goes right */
699
0
              src_ptr = *(--src_row_ptr);  /* source goes left */
700
              /* this unrolled loop doesn't need to know which row it's on... */
701
0
              for (k = 0; k < DCTSIZE2; k += 2) {
702
0
                *dst_ptr++ = *src_ptr++;   /* copy even column */
703
0
                *dst_ptr++ = -(*src_ptr++); /* copy odd column with sign change */
704
0
              }
705
0
            }
706
0
          }
707
0
        } else if (compptr->width_in_blocks > x_wipe_blocks + wipe_width) {
708
          /* Reflect from right */
709
0
          dst_row_ptr = buffer[offset_y] + x_wipe_blocks + wipe_width;
710
0
          for (dst_blk_x = wipe_width; dst_blk_x > 0;) {
711
0
            src_row_ptr = dst_row_ptr;     /* (re)set axis of reflection */
712
0
            src_blk_x = compptr->width_in_blocks - x_wipe_blocks - wipe_width;
713
0
            for (; src_blk_x > 0 && dst_blk_x > 0; src_blk_x--, dst_blk_x--) {
714
0
              dst_ptr = *(--dst_row_ptr);  /* destination goes left */
715
0
              src_ptr = *src_row_ptr++;    /* source goes right */
716
              /* this unrolled loop doesn't need to know which row it's on... */
717
0
              for (k = 0; k < DCTSIZE2; k += 2) {
718
0
                *dst_ptr++ = *src_ptr++;   /* copy even column */
719
0
                *dst_ptr++ = -(*src_ptr++); /* copy odd column with sign change */
720
0
              }
721
0
            }
722
0
          }
723
0
        } else {
724
0
          memset(buffer[offset_y] + x_wipe_blocks, 0,
725
0
                 wipe_width * sizeof(JBLOCK));
726
0
        }
727
0
      }
728
0
    }
729
0
  }
730
0
}
731
732
733
LOCAL(void)
734
do_flip_h_no_crop(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
735
                  JDIMENSION x_crop_offset, jvirt_barray_ptr *src_coef_arrays)
736
/* Horizontal flip; done in-place, so no separate dest array is required.
737
 * NB: this only works when y_crop_offset is zero.
738
 */
739
0
{
740
0
  JDIMENSION MCU_cols, comp_width, blk_x, blk_y, x_crop_blocks;
741
0
  int ci, k, offset_y;
742
0
  JBLOCKARRAY buffer;
743
0
  JCOEFPTR ptr1, ptr2;
744
0
  JCOEF temp1, temp2;
745
0
  jpeg_component_info *compptr;
746
747
  /* Horizontal mirroring of DCT blocks is accomplished by swapping
748
   * pairs of blocks in-place.  Within a DCT block, we perform horizontal
749
   * mirroring by changing the signs of odd-numbered columns.
750
   * Partial iMCUs at the right edge are left untouched.
751
   */
752
0
  MCU_cols = srcinfo->output_width /
753
0
             (dstinfo->max_h_samp_factor * dstinfo_min_DCT_h_scaled_size);
754
755
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
756
0
    compptr = dstinfo->comp_info + ci;
757
0
    comp_width = MCU_cols * compptr->h_samp_factor;
758
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
759
0
    for (blk_y = 0; blk_y < compptr->height_in_blocks;
760
0
         blk_y += compptr->v_samp_factor) {
761
0
      buffer = (*srcinfo->mem->access_virt_barray)
762
0
        ((j_common_ptr)srcinfo, src_coef_arrays[ci], blk_y,
763
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
764
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
765
        /* Do the mirroring */
766
0
        for (blk_x = 0; blk_x * 2 < comp_width; blk_x++) {
767
0
          ptr1 = buffer[offset_y][blk_x];
768
0
          ptr2 = buffer[offset_y][comp_width - blk_x - 1];
769
          /* this unrolled loop doesn't need to know which row it's on... */
770
0
          for (k = 0; k < DCTSIZE2; k += 2) {
771
0
            temp1 = *ptr1;      /* swap even column */
772
0
            temp2 = *ptr2;
773
0
            *ptr1++ = temp2;
774
0
            *ptr2++ = temp1;
775
0
            temp1 = *ptr1;      /* swap odd column with sign change */
776
0
            temp2 = *ptr2;
777
0
            *ptr1++ = -temp2;
778
0
            *ptr2++ = -temp1;
779
0
          }
780
0
        }
781
0
        if (x_crop_blocks > 0) {
782
          /* Now left-justify the portion of the data to be kept.
783
           * We can't use a single jcopy_block_row() call because that routine
784
           * depends on memcpy(), whose behavior is unspecified for overlapping
785
           * source and destination areas.  Sigh.
786
           */
787
0
          for (blk_x = 0; blk_x < compptr->width_in_blocks; blk_x++) {
788
0
            jcopy_block_row(buffer[offset_y] + blk_x + x_crop_blocks,
789
0
                            buffer[offset_y] + blk_x, (JDIMENSION)1);
790
0
          }
791
0
        }
792
0
      }
793
0
    }
794
0
  }
795
0
}
796
797
798
LOCAL(void)
799
do_flip_h(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
800
          JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
801
          jvirt_barray_ptr *src_coef_arrays,
802
          jvirt_barray_ptr *dst_coef_arrays)
803
/* Horizontal flip in general cropping case */
804
0
{
805
0
  JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y;
806
0
  JDIMENSION x_crop_blocks, y_crop_blocks;
807
0
  int ci, k, offset_y;
808
0
  JBLOCKARRAY src_buffer, dst_buffer;
809
0
  JBLOCKROW src_row_ptr, dst_row_ptr;
810
0
  JCOEFPTR src_ptr, dst_ptr;
811
0
  jpeg_component_info *compptr;
812
813
  /* Here we must output into a separate array because we can't touch
814
   * different rows of a single virtual array simultaneously.  Otherwise,
815
   * this is essentially the same as the routine above.
816
   */
817
0
  MCU_cols = srcinfo->output_width /
818
0
             (dstinfo->max_h_samp_factor * dstinfo_min_DCT_h_scaled_size);
819
820
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
821
0
    compptr = dstinfo->comp_info + ci;
822
0
    comp_width = MCU_cols * compptr->h_samp_factor;
823
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
824
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
825
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
826
0
         dst_blk_y += compptr->v_samp_factor) {
827
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
828
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
829
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
830
0
      src_buffer = (*srcinfo->mem->access_virt_barray)
831
0
        ((j_common_ptr)srcinfo, src_coef_arrays[ci], dst_blk_y + y_crop_blocks,
832
0
         (JDIMENSION)compptr->v_samp_factor, FALSE);
833
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
834
0
        dst_row_ptr = dst_buffer[offset_y];
835
0
        src_row_ptr = src_buffer[offset_y];
836
0
        for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
837
0
             dst_blk_x++) {
838
0
          if (x_crop_blocks + dst_blk_x < comp_width) {
839
            /* Do the mirrorable blocks */
840
0
            dst_ptr = dst_row_ptr[dst_blk_x];
841
0
            src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1];
842
            /* this unrolled loop doesn't need to know which row it's on... */
843
0
            for (k = 0; k < DCTSIZE2; k += 2) {
844
0
              *dst_ptr++ = *src_ptr++;    /* copy even column */
845
0
              *dst_ptr++ = -(*src_ptr++); /* copy odd column with sign
846
                                             change */
847
0
            }
848
0
          } else {
849
            /* Copy last partial block(s) verbatim */
850
0
            jcopy_block_row(src_row_ptr + dst_blk_x + x_crop_blocks,
851
0
                            dst_row_ptr + dst_blk_x, (JDIMENSION)1);
852
0
          }
853
0
        }
854
0
      }
855
0
    }
856
0
  }
857
0
}
858
859
860
LOCAL(void)
861
do_flip_v(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
862
          JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
863
          jvirt_barray_ptr *src_coef_arrays,
864
          jvirt_barray_ptr *dst_coef_arrays)
865
/* Vertical flip */
866
0
{
867
0
  JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
868
0
  JDIMENSION x_crop_blocks, y_crop_blocks;
869
0
  int ci, i, j, offset_y;
870
0
  JBLOCKARRAY src_buffer, dst_buffer;
871
0
  JBLOCKROW src_row_ptr, dst_row_ptr;
872
0
  JCOEFPTR src_ptr, dst_ptr;
873
0
  jpeg_component_info *compptr;
874
875
  /* We output into a separate array because we can't touch different
876
   * rows of the source virtual array simultaneously.  Otherwise, this
877
   * is a pretty straightforward analog of horizontal flip.
878
   * Within a DCT block, vertical mirroring is done by changing the signs
879
   * of odd-numbered rows.
880
   * Partial iMCUs at the bottom edge are copied verbatim.
881
   */
882
0
  MCU_rows = srcinfo->output_height /
883
0
             (dstinfo->max_v_samp_factor * dstinfo_min_DCT_v_scaled_size);
884
885
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
886
0
    compptr = dstinfo->comp_info + ci;
887
0
    comp_height = MCU_rows * compptr->v_samp_factor;
888
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
889
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
890
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
891
0
         dst_blk_y += compptr->v_samp_factor) {
892
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
893
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
894
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
895
0
      if (y_crop_blocks + dst_blk_y < comp_height) {
896
        /* Row is within the mirrorable area. */
897
0
        src_buffer = (*srcinfo->mem->access_virt_barray)
898
0
          ((j_common_ptr)srcinfo, src_coef_arrays[ci],
899
0
           comp_height - y_crop_blocks - dst_blk_y -
900
0
           (JDIMENSION)compptr->v_samp_factor,
901
0
           (JDIMENSION)compptr->v_samp_factor, FALSE);
902
0
      } else {
903
        /* Bottom-edge blocks will be copied verbatim. */
904
0
        src_buffer = (*srcinfo->mem->access_virt_barray)
905
0
          ((j_common_ptr)srcinfo, src_coef_arrays[ci],
906
0
           dst_blk_y + y_crop_blocks,
907
0
           (JDIMENSION)compptr->v_samp_factor, FALSE);
908
0
      }
909
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
910
0
        if (y_crop_blocks + dst_blk_y < comp_height) {
911
          /* Row is within the mirrorable area. */
912
0
          dst_row_ptr = dst_buffer[offset_y];
913
0
          src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
914
0
          src_row_ptr += x_crop_blocks;
915
0
          for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
916
0
               dst_blk_x++) {
917
0
            dst_ptr = dst_row_ptr[dst_blk_x];
918
0
            src_ptr = src_row_ptr[dst_blk_x];
919
0
            for (i = 0; i < DCTSIZE; i += 2) {
920
              /* copy even row */
921
0
              for (j = 0; j < DCTSIZE; j++)
922
0
                *dst_ptr++ = *src_ptr++;
923
              /* copy odd row with sign change */
924
0
              for (j = 0; j < DCTSIZE; j++)
925
0
                *dst_ptr++ = -(*src_ptr++);
926
0
            }
927
0
          }
928
0
        } else {
929
          /* Just copy row verbatim. */
930
0
          jcopy_block_row(src_buffer[offset_y] + x_crop_blocks,
931
0
                          dst_buffer[offset_y], compptr->width_in_blocks);
932
0
        }
933
0
      }
934
0
    }
935
0
  }
936
0
}
937
938
939
LOCAL(void)
940
do_transpose(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
941
             JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
942
             jvirt_barray_ptr *src_coef_arrays,
943
             jvirt_barray_ptr *dst_coef_arrays)
944
/* Transpose source into destination */
945
0
{
946
0
  JDIMENSION dst_blk_x, dst_blk_y, x_crop_blocks, y_crop_blocks;
947
0
  int ci, i, j, offset_x, offset_y;
948
0
  JBLOCKARRAY src_buffer, dst_buffer;
949
0
  JCOEFPTR src_ptr, dst_ptr;
950
0
  jpeg_component_info *compptr;
951
952
  /* Transposing pixels within a block just requires transposing the
953
   * DCT coefficients.
954
   * Partial iMCUs at the edges require no special treatment; we simply
955
   * process all the available DCT blocks for every component.
956
   */
957
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
958
0
    compptr = dstinfo->comp_info + ci;
959
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
960
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
961
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
962
0
         dst_blk_y += compptr->v_samp_factor) {
963
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
964
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
965
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
966
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
967
0
        for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
968
0
             dst_blk_x += compptr->h_samp_factor) {
969
0
          src_buffer = (*srcinfo->mem->access_virt_barray)
970
0
            ((j_common_ptr)srcinfo, src_coef_arrays[ci],
971
0
             dst_blk_x + x_crop_blocks,
972
0
             (JDIMENSION)compptr->h_samp_factor, FALSE);
973
0
          for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
974
0
            dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
975
0
            src_ptr =
976
0
              src_buffer[offset_x][dst_blk_y + offset_y + y_crop_blocks];
977
0
            for (i = 0; i < DCTSIZE; i++)
978
0
              for (j = 0; j < DCTSIZE; j++)
979
0
                dst_ptr[j * DCTSIZE + i] = src_ptr[i * DCTSIZE + j];
980
0
          }
981
0
        }
982
0
      }
983
0
    }
984
0
  }
985
0
}
986
987
988
LOCAL(void)
989
do_rot_90(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
990
          JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
991
          jvirt_barray_ptr *src_coef_arrays,
992
          jvirt_barray_ptr *dst_coef_arrays)
993
/* 90 degree rotation is equivalent to
994
 *   1. Transposing the image;
995
 *   2. Horizontal mirroring.
996
 * These two steps are merged into a single processing routine.
997
 */
998
0
{
999
0
  JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y;
1000
0
  JDIMENSION x_crop_blocks, y_crop_blocks;
1001
0
  int ci, i, j, offset_x, offset_y;
1002
0
  JBLOCKARRAY src_buffer, dst_buffer;
1003
0
  JCOEFPTR src_ptr, dst_ptr;
1004
0
  jpeg_component_info *compptr;
1005
1006
  /* Because of the horizontal mirror step, we can't process partial iMCUs
1007
   * at the (output) right edge properly.  They just get transposed and
1008
   * not mirrored.
1009
   */
1010
0
  MCU_cols = srcinfo->output_height /
1011
0
             (dstinfo->max_h_samp_factor * dstinfo_min_DCT_h_scaled_size);
1012
1013
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
1014
0
    compptr = dstinfo->comp_info + ci;
1015
0
    comp_width = MCU_cols * compptr->h_samp_factor;
1016
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
1017
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
1018
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
1019
0
         dst_blk_y += compptr->v_samp_factor) {
1020
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
1021
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
1022
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
1023
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
1024
0
        for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
1025
0
             dst_blk_x += compptr->h_samp_factor) {
1026
0
          if (x_crop_blocks + dst_blk_x < comp_width) {
1027
            /* Block is within the mirrorable area. */
1028
0
            src_buffer = (*srcinfo->mem->access_virt_barray)
1029
0
              ((j_common_ptr)srcinfo, src_coef_arrays[ci],
1030
0
               comp_width - x_crop_blocks - dst_blk_x -
1031
0
               (JDIMENSION)compptr->h_samp_factor,
1032
0
               (JDIMENSION)compptr->h_samp_factor, FALSE);
1033
0
          } else {
1034
            /* Edge blocks are transposed but not mirrored. */
1035
0
            src_buffer = (*srcinfo->mem->access_virt_barray)
1036
0
              ((j_common_ptr)srcinfo, src_coef_arrays[ci],
1037
0
               dst_blk_x + x_crop_blocks,
1038
0
               (JDIMENSION)compptr->h_samp_factor, FALSE);
1039
0
          }
1040
0
          for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
1041
0
            dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
1042
0
            if (x_crop_blocks + dst_blk_x < comp_width) {
1043
              /* Block is within the mirrorable area. */
1044
0
              src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1]
1045
0
                [dst_blk_y + offset_y + y_crop_blocks];
1046
0
              for (i = 0; i < DCTSIZE; i++) {
1047
0
                for (j = 0; j < DCTSIZE; j++)
1048
0
                  dst_ptr[j * DCTSIZE + i] = src_ptr[i * DCTSIZE + j];
1049
0
                i++;
1050
0
                for (j = 0; j < DCTSIZE; j++)
1051
0
                  dst_ptr[j * DCTSIZE + i] = -src_ptr[i * DCTSIZE + j];
1052
0
              }
1053
0
            } else {
1054
              /* Edge blocks are transposed but not mirrored. */
1055
0
              src_ptr = src_buffer[offset_x]
1056
0
                [dst_blk_y + offset_y + y_crop_blocks];
1057
0
              for (i = 0; i < DCTSIZE; i++)
1058
0
                for (j = 0; j < DCTSIZE; j++)
1059
0
                  dst_ptr[j * DCTSIZE + i] = src_ptr[i * DCTSIZE + j];
1060
0
            }
1061
0
          }
1062
0
        }
1063
0
      }
1064
0
    }
1065
0
  }
1066
0
}
1067
1068
1069
LOCAL(void)
1070
do_rot_270(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
1071
           JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
1072
           jvirt_barray_ptr *src_coef_arrays,
1073
           jvirt_barray_ptr *dst_coef_arrays)
1074
/* 270 degree rotation is equivalent to
1075
 *   1. Horizontal mirroring;
1076
 *   2. Transposing the image.
1077
 * These two steps are merged into a single processing routine.
1078
 */
1079
0
{
1080
0
  JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
1081
0
  JDIMENSION x_crop_blocks, y_crop_blocks;
1082
0
  int ci, i, j, offset_x, offset_y;
1083
0
  JBLOCKARRAY src_buffer, dst_buffer;
1084
0
  JCOEFPTR src_ptr, dst_ptr;
1085
0
  jpeg_component_info *compptr;
1086
1087
  /* Because of the horizontal mirror step, we can't process partial iMCUs
1088
   * at the (output) bottom edge properly.  They just get transposed and
1089
   * not mirrored.
1090
   */
1091
0
  MCU_rows = srcinfo->output_width /
1092
0
             (dstinfo->max_v_samp_factor * dstinfo_min_DCT_v_scaled_size);
1093
1094
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
1095
0
    compptr = dstinfo->comp_info + ci;
1096
0
    comp_height = MCU_rows * compptr->v_samp_factor;
1097
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
1098
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
1099
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
1100
0
         dst_blk_y += compptr->v_samp_factor) {
1101
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
1102
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
1103
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
1104
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
1105
0
        for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
1106
0
             dst_blk_x += compptr->h_samp_factor) {
1107
0
          src_buffer = (*srcinfo->mem->access_virt_barray)
1108
0
            ((j_common_ptr)srcinfo, src_coef_arrays[ci],
1109
0
             dst_blk_x + x_crop_blocks,
1110
0
             (JDIMENSION)compptr->h_samp_factor, FALSE);
1111
0
          for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
1112
0
            dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
1113
0
            if (y_crop_blocks + dst_blk_y < comp_height) {
1114
              /* Block is within the mirrorable area. */
1115
0
              src_ptr = src_buffer[offset_x]
1116
0
                [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1];
1117
0
              for (i = 0; i < DCTSIZE; i++) {
1118
0
                for (j = 0; j < DCTSIZE; j++) {
1119
0
                  dst_ptr[j * DCTSIZE + i] = src_ptr[i * DCTSIZE + j];
1120
0
                  j++;
1121
0
                  dst_ptr[j * DCTSIZE + i] = -src_ptr[i * DCTSIZE + j];
1122
0
                }
1123
0
              }
1124
0
            } else {
1125
              /* Edge blocks are transposed but not mirrored. */
1126
0
              src_ptr = src_buffer[offset_x]
1127
0
                [dst_blk_y + offset_y + y_crop_blocks];
1128
0
              for (i = 0; i < DCTSIZE; i++)
1129
0
                for (j = 0; j < DCTSIZE; j++)
1130
0
                  dst_ptr[j * DCTSIZE + i] = src_ptr[i * DCTSIZE + j];
1131
0
            }
1132
0
          }
1133
0
        }
1134
0
      }
1135
0
    }
1136
0
  }
1137
0
}
1138
1139
1140
LOCAL(void)
1141
do_rot_180(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
1142
           JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
1143
           jvirt_barray_ptr *src_coef_arrays,
1144
           jvirt_barray_ptr *dst_coef_arrays)
1145
/* 180 degree rotation is equivalent to
1146
 *   1. Vertical mirroring;
1147
 *   2. Horizontal mirroring.
1148
 * These two steps are merged into a single processing routine.
1149
 */
1150
0
{
1151
0
  JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
1152
0
  JDIMENSION x_crop_blocks, y_crop_blocks;
1153
0
  int ci, i, j, offset_y;
1154
0
  JBLOCKARRAY src_buffer, dst_buffer;
1155
0
  JBLOCKROW src_row_ptr, dst_row_ptr;
1156
0
  JCOEFPTR src_ptr, dst_ptr;
1157
0
  jpeg_component_info *compptr;
1158
1159
0
  MCU_cols = srcinfo->output_width /
1160
0
             (dstinfo->max_h_samp_factor * dstinfo_min_DCT_h_scaled_size);
1161
0
  MCU_rows = srcinfo->output_height /
1162
0
             (dstinfo->max_v_samp_factor * dstinfo_min_DCT_v_scaled_size);
1163
1164
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
1165
0
    compptr = dstinfo->comp_info + ci;
1166
0
    comp_width = MCU_cols * compptr->h_samp_factor;
1167
0
    comp_height = MCU_rows * compptr->v_samp_factor;
1168
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
1169
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
1170
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
1171
0
         dst_blk_y += compptr->v_samp_factor) {
1172
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
1173
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
1174
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
1175
0
      if (y_crop_blocks + dst_blk_y < comp_height) {
1176
        /* Row is within the vertically mirrorable area. */
1177
0
        src_buffer = (*srcinfo->mem->access_virt_barray)
1178
0
          ((j_common_ptr)srcinfo, src_coef_arrays[ci],
1179
0
           comp_height - y_crop_blocks - dst_blk_y -
1180
0
           (JDIMENSION)compptr->v_samp_factor,
1181
0
           (JDIMENSION)compptr->v_samp_factor, FALSE);
1182
0
      } else {
1183
        /* Bottom-edge rows are only mirrored horizontally. */
1184
0
        src_buffer = (*srcinfo->mem->access_virt_barray)
1185
0
          ((j_common_ptr)srcinfo, src_coef_arrays[ci],
1186
0
           dst_blk_y + y_crop_blocks,
1187
0
           (JDIMENSION)compptr->v_samp_factor, FALSE);
1188
0
      }
1189
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
1190
0
        dst_row_ptr = dst_buffer[offset_y];
1191
0
        if (y_crop_blocks + dst_blk_y < comp_height) {
1192
          /* Row is within the mirrorable area. */
1193
0
          src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
1194
0
          for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
1195
0
               dst_blk_x++) {
1196
0
            dst_ptr = dst_row_ptr[dst_blk_x];
1197
0
            if (x_crop_blocks + dst_blk_x < comp_width) {
1198
              /* Process the blocks that can be mirrored both ways. */
1199
0
              src_ptr =
1200
0
                src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1];
1201
0
              for (i = 0; i < DCTSIZE; i += 2) {
1202
                /* For even row, negate every odd column. */
1203
0
                for (j = 0; j < DCTSIZE; j += 2) {
1204
0
                  *dst_ptr++ = *src_ptr++;
1205
0
                  *dst_ptr++ = -(*src_ptr++);
1206
0
                }
1207
                /* For odd row, negate every even column. */
1208
0
                for (j = 0; j < DCTSIZE; j += 2) {
1209
0
                  *dst_ptr++ = -(*src_ptr++);
1210
0
                  *dst_ptr++ = *src_ptr++;
1211
0
                }
1212
0
              }
1213
0
            } else {
1214
              /* Any remaining right-edge blocks are only mirrored vertically. */
1215
0
              src_ptr = src_row_ptr[x_crop_blocks + dst_blk_x];
1216
0
              for (i = 0; i < DCTSIZE; i += 2) {
1217
0
                for (j = 0; j < DCTSIZE; j++)
1218
0
                  *dst_ptr++ = *src_ptr++;
1219
0
                for (j = 0; j < DCTSIZE; j++)
1220
0
                  *dst_ptr++ = -(*src_ptr++);
1221
0
              }
1222
0
            }
1223
0
          }
1224
0
        } else {
1225
          /* Remaining rows are just mirrored horizontally. */
1226
0
          src_row_ptr = src_buffer[offset_y];
1227
0
          for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
1228
0
               dst_blk_x++) {
1229
0
            if (x_crop_blocks + dst_blk_x < comp_width) {
1230
              /* Process the blocks that can be mirrored. */
1231
0
              dst_ptr = dst_row_ptr[dst_blk_x];
1232
0
              src_ptr =
1233
0
                src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1];
1234
0
              for (i = 0; i < DCTSIZE2; i += 2) {
1235
0
                *dst_ptr++ = *src_ptr++;
1236
0
                *dst_ptr++ = -(*src_ptr++);
1237
0
              }
1238
0
            } else {
1239
              /* Any remaining right-edge blocks are only copied. */
1240
0
              jcopy_block_row(src_row_ptr + dst_blk_x + x_crop_blocks,
1241
0
                              dst_row_ptr + dst_blk_x, (JDIMENSION)1);
1242
0
            }
1243
0
          }
1244
0
        }
1245
0
      }
1246
0
    }
1247
0
  }
1248
0
}
1249
1250
1251
LOCAL(void)
1252
do_transverse(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
1253
              JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
1254
              jvirt_barray_ptr *src_coef_arrays,
1255
              jvirt_barray_ptr *dst_coef_arrays)
1256
/* Transverse transpose is equivalent to
1257
 *   1. 180 degree rotation;
1258
 *   2. Transposition;
1259
 * or
1260
 *   1. Horizontal mirroring;
1261
 *   2. Transposition;
1262
 *   3. Horizontal mirroring.
1263
 * These steps are merged into a single processing routine.
1264
 */
1265
0
{
1266
0
  JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
1267
0
  JDIMENSION x_crop_blocks, y_crop_blocks;
1268
0
  int ci, i, j, offset_x, offset_y;
1269
0
  JBLOCKARRAY src_buffer, dst_buffer;
1270
0
  JCOEFPTR src_ptr, dst_ptr;
1271
0
  jpeg_component_info *compptr;
1272
1273
0
  MCU_cols = srcinfo->output_height /
1274
0
             (dstinfo->max_h_samp_factor * dstinfo_min_DCT_h_scaled_size);
1275
0
  MCU_rows = srcinfo->output_width /
1276
0
             (dstinfo->max_v_samp_factor * dstinfo_min_DCT_v_scaled_size);
1277
1278
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
1279
0
    compptr = dstinfo->comp_info + ci;
1280
0
    comp_width = MCU_cols * compptr->h_samp_factor;
1281
0
    comp_height = MCU_rows * compptr->v_samp_factor;
1282
0
    x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
1283
0
    y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
1284
0
    for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
1285
0
         dst_blk_y += compptr->v_samp_factor) {
1286
0
      dst_buffer = (*srcinfo->mem->access_virt_barray)
1287
0
        ((j_common_ptr)srcinfo, dst_coef_arrays[ci], dst_blk_y,
1288
0
         (JDIMENSION)compptr->v_samp_factor, TRUE);
1289
0
      for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
1290
0
        for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
1291
0
             dst_blk_x += compptr->h_samp_factor) {
1292
0
          if (x_crop_blocks + dst_blk_x < comp_width) {
1293
            /* Block is within the mirrorable area. */
1294
0
            src_buffer = (*srcinfo->mem->access_virt_barray)
1295
0
              ((j_common_ptr)srcinfo, src_coef_arrays[ci],
1296
0
               comp_width - x_crop_blocks - dst_blk_x -
1297
0
               (JDIMENSION)compptr->h_samp_factor,
1298
0
               (JDIMENSION)compptr->h_samp_factor, FALSE);
1299
0
          } else {
1300
0
            src_buffer = (*srcinfo->mem->access_virt_barray)
1301
0
              ((j_common_ptr)srcinfo, src_coef_arrays[ci],
1302
0
               dst_blk_x + x_crop_blocks,
1303
0
               (JDIMENSION)compptr->h_samp_factor, FALSE);
1304
0
          }
1305
0
          for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
1306
0
            dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
1307
0
            if (y_crop_blocks + dst_blk_y < comp_height) {
1308
0
              if (x_crop_blocks + dst_blk_x < comp_width) {
1309
                /* Block is within the mirrorable area. */
1310
0
                src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1]
1311
0
                  [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1];
1312
0
                for (i = 0; i < DCTSIZE; i++) {
1313
0
                  for (j = 0; j < DCTSIZE; j++) {
1314
0
                    dst_ptr[j * DCTSIZE + i] = src_ptr[i * DCTSIZE + j];
1315
0
                    j++;
1316
0
                    dst_ptr[j * DCTSIZE + i] = -src_ptr[i * DCTSIZE + j];
1317
0
                  }
1318
0
                  i++;
1319
0
                  for (j = 0; j < DCTSIZE; j++) {
1320
0
                    dst_ptr[j * DCTSIZE + i] = -src_ptr[i * DCTSIZE + j];
1321
0
                    j++;
1322
0
                    dst_ptr[j * DCTSIZE + i] = src_ptr[i * DCTSIZE + j];
1323
0
                  }
1324
0
                }
1325
0
              } else {
1326
                /* Right-edge blocks are mirrored in y only */
1327
0
                src_ptr = src_buffer[offset_x]
1328
0
                  [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1];
1329
0
                for (i = 0; i < DCTSIZE; i++) {
1330
0
                  for (j = 0; j < DCTSIZE; j++) {
1331
0
                    dst_ptr[j * DCTSIZE + i] = src_ptr[i * DCTSIZE + j];
1332
0
                    j++;
1333
0
                    dst_ptr[j * DCTSIZE + i] = -src_ptr[i * DCTSIZE + j];
1334
0
                  }
1335
0
                }
1336
0
              }
1337
0
            } else {
1338
0
              if (x_crop_blocks + dst_blk_x < comp_width) {
1339
                /* Bottom-edge blocks are mirrored in x only */
1340
0
                src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1]
1341
0
                  [dst_blk_y + offset_y + y_crop_blocks];
1342
0
                for (i = 0; i < DCTSIZE; i++) {
1343
0
                  for (j = 0; j < DCTSIZE; j++)
1344
0
                    dst_ptr[j * DCTSIZE + i] = src_ptr[i * DCTSIZE + j];
1345
0
                  i++;
1346
0
                  for (j = 0; j < DCTSIZE; j++)
1347
0
                    dst_ptr[j * DCTSIZE + i] = -src_ptr[i * DCTSIZE + j];
1348
0
                }
1349
0
              } else {
1350
                /* At lower right corner, just transpose, no mirroring */
1351
0
                src_ptr = src_buffer[offset_x]
1352
0
                  [dst_blk_y + offset_y + y_crop_blocks];
1353
0
                for (i = 0; i < DCTSIZE; i++)
1354
0
                  for (j = 0; j < DCTSIZE; j++)
1355
0
                    dst_ptr[j * DCTSIZE + i] = src_ptr[i * DCTSIZE + j];
1356
0
              }
1357
0
            }
1358
0
          }
1359
0
        }
1360
0
      }
1361
0
    }
1362
0
  }
1363
0
}
1364
1365
1366
/* Parse an unsigned integer: subroutine for jtransform_parse_crop_spec.
1367
 * Returns TRUE if valid integer found, FALSE if not.
1368
 * *strptr is advanced over the digit string, and *result is set to its value.
1369
 */
1370
1371
LOCAL(boolean)
1372
jt_read_integer(const char **strptr, JDIMENSION *result)
1373
0
{
1374
0
  const char *ptr = *strptr;
1375
0
  JDIMENSION val = 0;
1376
1377
0
  for (; isdigit(*ptr); ptr++) {
1378
0
    val = val * 10 + (JDIMENSION)(*ptr - '0');
1379
0
  }
1380
0
  *result = val;
1381
0
  if (ptr == *strptr)
1382
0
    return FALSE;               /* oops, no digits */
1383
0
  *strptr = ptr;
1384
0
  return TRUE;
1385
0
}
1386
1387
1388
/* Parse a crop specification (written in X11 geometry style).
1389
 * The routine returns TRUE if the spec string is valid, FALSE if not.
1390
 *
1391
 * The crop spec string should have the format
1392
 *      <width>[{fr}]x<height>[{fr}]{+-}<xoffset>{+-}<yoffset>
1393
 * where width, height, xoffset, and yoffset are unsigned integers.
1394
 * Each of the elements can be omitted to indicate a default value.
1395
 * (A weakness of this style is that it is not possible to omit xoffset
1396
 * while specifying yoffset, since they look alike.)
1397
 *
1398
 * This code is loosely based on XParseGeometry from the X11 distribution.
1399
 */
1400
1401
GLOBAL(boolean)
1402
jtransform_parse_crop_spec(jpeg_transform_info *info, const char *spec)
1403
0
{
1404
0
  info->crop = FALSE;
1405
0
  info->crop_width_set = JCROP_UNSET;
1406
0
  info->crop_height_set = JCROP_UNSET;
1407
0
  info->crop_xoffset_set = JCROP_UNSET;
1408
0
  info->crop_yoffset_set = JCROP_UNSET;
1409
1410
0
  if (isdigit(*spec)) {
1411
    /* fetch width */
1412
0
    if (!jt_read_integer(&spec, &info->crop_width))
1413
0
      return FALSE;
1414
0
    if (*spec == 'f' || *spec == 'F') {
1415
0
      spec++;
1416
0
      info->crop_width_set = JCROP_FORCE;
1417
0
    } else if (*spec == 'r' || *spec == 'R') {
1418
0
      spec++;
1419
0
      info->crop_width_set = JCROP_REFLECT;
1420
0
    } else
1421
0
      info->crop_width_set = JCROP_POS;
1422
0
  }
1423
0
  if (*spec == 'x' || *spec == 'X') {
1424
    /* fetch height */
1425
0
    spec++;
1426
0
    if (!jt_read_integer(&spec, &info->crop_height))
1427
0
      return FALSE;
1428
0
    if (*spec == 'f' || *spec == 'F') {
1429
0
      spec++;
1430
0
      info->crop_height_set = JCROP_FORCE;
1431
0
    } else if (*spec == 'r' || *spec == 'R') {
1432
0
      spec++;
1433
0
      info->crop_height_set = JCROP_REFLECT;
1434
0
    } else
1435
0
      info->crop_height_set = JCROP_POS;
1436
0
  }
1437
0
  if (*spec == '+' || *spec == '-') {
1438
    /* fetch xoffset */
1439
0
    info->crop_xoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS;
1440
0
    spec++;
1441
0
    if (!jt_read_integer(&spec, &info->crop_xoffset))
1442
0
      return FALSE;
1443
0
  }
1444
0
  if (*spec == '+' || *spec == '-') {
1445
    /* fetch yoffset */
1446
0
    info->crop_yoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS;
1447
0
    spec++;
1448
0
    if (!jt_read_integer(&spec, &info->crop_yoffset))
1449
0
      return FALSE;
1450
0
  }
1451
  /* We had better have gotten to the end of the string. */
1452
0
  if (*spec != '\0')
1453
0
    return FALSE;
1454
0
  info->crop = TRUE;
1455
0
  return TRUE;
1456
0
}
1457
1458
1459
/* Trim off any partial iMCUs on the indicated destination edge */
1460
1461
LOCAL(void)
1462
trim_right_edge(jpeg_transform_info *info, JDIMENSION full_width)
1463
0
{
1464
0
  JDIMENSION MCU_cols;
1465
1466
0
  MCU_cols = info->output_width / info->iMCU_sample_width;
1467
0
  if (MCU_cols > 0 && info->x_crop_offset + MCU_cols ==
1468
0
      full_width / info->iMCU_sample_width)
1469
0
    info->output_width = MCU_cols * info->iMCU_sample_width;
1470
0
}
1471
1472
LOCAL(void)
1473
trim_bottom_edge(jpeg_transform_info *info, JDIMENSION full_height)
1474
0
{
1475
0
  JDIMENSION MCU_rows;
1476
1477
0
  MCU_rows = info->output_height / info->iMCU_sample_height;
1478
0
  if (MCU_rows > 0 && info->y_crop_offset + MCU_rows ==
1479
0
      full_height / info->iMCU_sample_height)
1480
0
    info->output_height = MCU_rows * info->iMCU_sample_height;
1481
0
}
1482
1483
1484
/* Request any required workspace.
1485
 *
1486
 * This routine figures out the size that the output image will be
1487
 * (which implies that all the transform parameters must be set before
1488
 * it is called).
1489
 *
1490
 * We allocate the workspace virtual arrays from the source decompression
1491
 * object, so that all the arrays (both the original data and the workspace)
1492
 * will be taken into account while making memory management decisions.
1493
 * Hence, this routine must be called after jpeg_read_header (which reads
1494
 * the image dimensions) and before jpeg_read_coefficients (which realizes
1495
 * the source's virtual arrays).
1496
 *
1497
 * This function returns FALSE right away if -perfect is given
1498
 * and transformation is not perfect.  Otherwise returns TRUE.
1499
 */
1500
1501
GLOBAL(boolean)
1502
jtransform_request_workspace(j_decompress_ptr srcinfo,
1503
                             jpeg_transform_info *info)
1504
0
{
1505
0
  jvirt_barray_ptr *coef_arrays;
1506
0
  boolean need_workspace, transpose_it;
1507
0
  jpeg_component_info *compptr;
1508
0
  JDIMENSION xoffset, yoffset, dtemp;
1509
0
  JDIMENSION width_in_iMCUs, height_in_iMCUs;
1510
0
  JDIMENSION width_in_blocks, height_in_blocks;
1511
0
  int itemp, ci, h_samp_factor, v_samp_factor;
1512
1513
  /* Determine number of components in output image */
1514
0
  if (info->force_grayscale &&
1515
0
      srcinfo->jpeg_color_space == JCS_YCbCr &&
1516
0
      srcinfo->num_components == 3)
1517
    /* We'll only process the first component */
1518
0
    info->num_components = 1;
1519
0
  else
1520
    /* Process all the components */
1521
0
    info->num_components = srcinfo->num_components;
1522
1523
  /* Compute output image dimensions and related values. */
1524
#if JPEG_LIB_VERSION >= 80
1525
  jpeg_core_output_dimensions(srcinfo);
1526
#else
1527
0
  srcinfo->output_width = srcinfo->image_width;
1528
0
  srcinfo->output_height = srcinfo->image_height;
1529
0
#endif
1530
1531
  /* Return right away if -perfect is given and transformation is not perfect.
1532
   */
1533
0
  if (info->perfect) {
1534
0
    if (info->num_components == 1) {
1535
0
      if (!jtransform_perfect_transform(srcinfo->output_width,
1536
0
          srcinfo->output_height,
1537
0
          srcinfo->_min_DCT_h_scaled_size,
1538
0
          srcinfo->_min_DCT_v_scaled_size,
1539
0
          info->transform))
1540
0
        return FALSE;
1541
0
    } else {
1542
0
      if (!jtransform_perfect_transform(srcinfo->output_width,
1543
0
          srcinfo->output_height,
1544
0
          srcinfo->max_h_samp_factor * srcinfo->_min_DCT_h_scaled_size,
1545
0
          srcinfo->max_v_samp_factor * srcinfo->_min_DCT_v_scaled_size,
1546
0
          info->transform))
1547
0
        return FALSE;
1548
0
    }
1549
0
  }
1550
1551
  /* If there is only one output component, force the iMCU size to be 1;
1552
   * else use the source iMCU size.  (This allows us to do the right thing
1553
   * when reducing color to grayscale, and also provides a handy way of
1554
   * cleaning up "funny" grayscale images whose sampling factors are not 1x1.)
1555
   */
1556
0
  switch (info->transform) {
1557
0
  case JXFORM_TRANSPOSE:
1558
0
  case JXFORM_TRANSVERSE:
1559
0
  case JXFORM_ROT_90:
1560
0
  case JXFORM_ROT_270:
1561
0
    info->output_width = srcinfo->output_height;
1562
0
    info->output_height = srcinfo->output_width;
1563
0
    if (info->num_components == 1) {
1564
0
      info->iMCU_sample_width = srcinfo->_min_DCT_v_scaled_size;
1565
0
      info->iMCU_sample_height = srcinfo->_min_DCT_h_scaled_size;
1566
0
    } else {
1567
0
      info->iMCU_sample_width =
1568
0
        srcinfo->max_v_samp_factor * srcinfo->_min_DCT_v_scaled_size;
1569
0
      info->iMCU_sample_height =
1570
0
        srcinfo->max_h_samp_factor * srcinfo->_min_DCT_h_scaled_size;
1571
0
    }
1572
0
    break;
1573
0
  default:
1574
0
    info->output_width = srcinfo->output_width;
1575
0
    info->output_height = srcinfo->output_height;
1576
0
    if (info->num_components == 1) {
1577
0
      info->iMCU_sample_width = srcinfo->_min_DCT_h_scaled_size;
1578
0
      info->iMCU_sample_height = srcinfo->_min_DCT_v_scaled_size;
1579
0
    } else {
1580
0
      info->iMCU_sample_width =
1581
0
        srcinfo->max_h_samp_factor * srcinfo->_min_DCT_h_scaled_size;
1582
0
      info->iMCU_sample_height =
1583
0
        srcinfo->max_v_samp_factor * srcinfo->_min_DCT_v_scaled_size;
1584
0
    }
1585
0
    break;
1586
0
  }
1587
1588
  /* If cropping has been requested, compute the crop area's position and
1589
   * dimensions, ensuring that its upper left corner falls at an iMCU boundary.
1590
   */
1591
0
  if (info->crop) {
1592
    /* Insert default values for unset crop parameters */
1593
0
    if (info->crop_xoffset_set == JCROP_UNSET)
1594
0
      info->crop_xoffset = 0;   /* default to +0 */
1595
0
    if (info->crop_yoffset_set == JCROP_UNSET)
1596
0
      info->crop_yoffset = 0;   /* default to +0 */
1597
0
    if (info->crop_width_set == JCROP_UNSET) {
1598
0
      if (info->crop_xoffset >= info->output_width)
1599
0
        ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
1600
0
      info->crop_width = info->output_width - info->crop_xoffset;
1601
0
    } else {
1602
      /* Check for crop extension */
1603
0
      if (info->crop_width > info->output_width) {
1604
        /* Crop extension does not work when transforming! */
1605
0
        if (info->transform != JXFORM_NONE ||
1606
0
            info->crop_xoffset >= info->crop_width ||
1607
0
            info->crop_xoffset > info->crop_width - info->output_width)
1608
0
          ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
1609
0
      } else {
1610
0
        if (info->crop_xoffset >= info->output_width ||
1611
0
            info->crop_width <= 0 ||
1612
0
            info->crop_xoffset > info->output_width - info->crop_width)
1613
0
          ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
1614
0
      }
1615
0
    }
1616
0
    if (info->crop_height_set == JCROP_UNSET) {
1617
0
      if (info->crop_yoffset >= info->output_height)
1618
0
        ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
1619
0
      info->crop_height = info->output_height - info->crop_yoffset;
1620
0
    } else {
1621
      /* Check for crop extension */
1622
0
      if (info->crop_height > info->output_height) {
1623
        /* Crop extension does not work when transforming! */
1624
0
        if (info->transform != JXFORM_NONE ||
1625
0
            info->crop_yoffset >= info->crop_height ||
1626
0
            info->crop_yoffset > info->crop_height - info->output_height)
1627
0
          ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
1628
0
      } else {
1629
0
        if (info->crop_yoffset >= info->output_height ||
1630
0
            info->crop_height <= 0 ||
1631
0
            info->crop_yoffset > info->output_height - info->crop_height)
1632
0
          ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
1633
0
      }
1634
0
    }
1635
    /* Convert negative crop offsets into regular offsets */
1636
0
    if (info->crop_xoffset_set != JCROP_NEG)
1637
0
      xoffset = info->crop_xoffset;
1638
0
    else if (info->crop_width > info->output_width) /* crop extension */
1639
0
      xoffset = info->crop_width - info->output_width - info->crop_xoffset;
1640
0
    else
1641
0
      xoffset = info->output_width - info->crop_width - info->crop_xoffset;
1642
0
    if (info->crop_yoffset_set != JCROP_NEG)
1643
0
      yoffset = info->crop_yoffset;
1644
0
    else if (info->crop_height > info->output_height) /* crop extension */
1645
0
      yoffset = info->crop_height - info->output_height - info->crop_yoffset;
1646
0
    else
1647
0
      yoffset = info->output_height - info->crop_height - info->crop_yoffset;
1648
    /* Now adjust so that upper left corner falls at an iMCU boundary */
1649
0
    switch (info->transform) {
1650
0
    case JXFORM_DROP:
1651
      /* Ensure the effective drop region will not exceed the requested */
1652
0
      itemp = info->iMCU_sample_width;
1653
0
      dtemp = itemp - 1 - ((xoffset + itemp - 1) % itemp);
1654
0
      xoffset += dtemp;
1655
0
      if (info->crop_width <= dtemp)
1656
0
        info->drop_width = 0;
1657
0
      else if (xoffset + info->crop_width - dtemp == info->output_width)
1658
        /* Matching right edge: include partial iMCU */
1659
0
        info->drop_width = (info->crop_width - dtemp + itemp - 1) / itemp;
1660
0
      else
1661
0
        info->drop_width = (info->crop_width - dtemp) / itemp;
1662
0
      itemp = info->iMCU_sample_height;
1663
0
      dtemp = itemp - 1 - ((yoffset + itemp - 1) % itemp);
1664
0
      yoffset += dtemp;
1665
0
      if (info->crop_height <= dtemp)
1666
0
        info->drop_height = 0;
1667
0
      else if (yoffset + info->crop_height - dtemp == info->output_height)
1668
        /* Matching bottom edge: include partial iMCU */
1669
0
        info->drop_height = (info->crop_height - dtemp + itemp - 1) / itemp;
1670
0
      else
1671
0
        info->drop_height = (info->crop_height - dtemp) / itemp;
1672
      /* Check if sampling factors match for dropping */
1673
0
      if (info->drop_width != 0 && info->drop_height != 0)
1674
0
        for (ci = 0; ci < info->num_components &&
1675
0
                     ci < info->drop_ptr->num_components; ci++) {
1676
0
          if (info->drop_ptr->comp_info[ci].h_samp_factor *
1677
0
              srcinfo->max_h_samp_factor !=
1678
0
              srcinfo->comp_info[ci].h_samp_factor *
1679
0
              info->drop_ptr->max_h_samp_factor)
1680
0
            ERREXIT6(srcinfo, JERR_BAD_DROP_SAMPLING, ci,
1681
0
              info->drop_ptr->comp_info[ci].h_samp_factor,
1682
0
              info->drop_ptr->max_h_samp_factor,
1683
0
              srcinfo->comp_info[ci].h_samp_factor,
1684
0
              srcinfo->max_h_samp_factor, 'h');
1685
0
          if (info->drop_ptr->comp_info[ci].v_samp_factor *
1686
0
              srcinfo->max_v_samp_factor !=
1687
0
              srcinfo->comp_info[ci].v_samp_factor *
1688
0
              info->drop_ptr->max_v_samp_factor)
1689
0
            ERREXIT6(srcinfo, JERR_BAD_DROP_SAMPLING, ci,
1690
0
              info->drop_ptr->comp_info[ci].v_samp_factor,
1691
0
              info->drop_ptr->max_v_samp_factor,
1692
0
              srcinfo->comp_info[ci].v_samp_factor,
1693
0
              srcinfo->max_v_samp_factor, 'v');
1694
0
        }
1695
0
      break;
1696
0
    case JXFORM_WIPE:
1697
      /* Ensure the effective wipe region will cover the requested */
1698
0
      info->drop_width = (JDIMENSION)jdiv_round_up
1699
0
        ((long)(info->crop_width + (xoffset % info->iMCU_sample_width)),
1700
0
         (long)info->iMCU_sample_width);
1701
0
      info->drop_height = (JDIMENSION)jdiv_round_up
1702
0
        ((long)(info->crop_height + (yoffset % info->iMCU_sample_height)),
1703
0
         (long)info->iMCU_sample_height);
1704
0
      break;
1705
0
    default:
1706
      /* Ensure the effective crop region will cover the requested */
1707
0
      if (info->crop_width_set == JCROP_FORCE ||
1708
0
          info->crop_width > info->output_width)
1709
0
        info->output_width = info->crop_width;
1710
0
      else
1711
0
        info->output_width =
1712
0
          info->crop_width + (xoffset % info->iMCU_sample_width);
1713
0
      if (info->crop_height_set == JCROP_FORCE ||
1714
0
          info->crop_height > info->output_height)
1715
0
        info->output_height = info->crop_height;
1716
0
      else
1717
0
        info->output_height =
1718
0
          info->crop_height + (yoffset % info->iMCU_sample_height);
1719
0
    }
1720
    /* Save x/y offsets measured in iMCUs */
1721
0
    info->x_crop_offset = xoffset / info->iMCU_sample_width;
1722
0
    info->y_crop_offset = yoffset / info->iMCU_sample_height;
1723
0
  } else {
1724
0
    info->x_crop_offset = 0;
1725
0
    info->y_crop_offset = 0;
1726
0
  }
1727
1728
  /* Figure out whether we need workspace arrays,
1729
   * and if so whether they are transposed relative to the source.
1730
   */
1731
0
  need_workspace = FALSE;
1732
0
  transpose_it = FALSE;
1733
0
  switch (info->transform) {
1734
0
  case JXFORM_NONE:
1735
0
    if (info->x_crop_offset != 0 || info->y_crop_offset != 0 ||
1736
0
        info->output_width > srcinfo->output_width ||
1737
0
        info->output_height > srcinfo->output_height)
1738
0
      need_workspace = TRUE;
1739
    /* No workspace needed if neither cropping nor transforming */
1740
0
    break;
1741
0
  case JXFORM_FLIP_H:
1742
0
    if (info->trim)
1743
0
      trim_right_edge(info, srcinfo->output_width);
1744
0
    if (info->y_crop_offset != 0 || info->slow_hflip)
1745
0
      need_workspace = TRUE;
1746
    /* do_flip_h_no_crop doesn't need a workspace array */
1747
0
    break;
1748
0
  case JXFORM_FLIP_V:
1749
0
    if (info->trim)
1750
0
      trim_bottom_edge(info, srcinfo->output_height);
1751
    /* Need workspace arrays having same dimensions as source image. */
1752
0
    need_workspace = TRUE;
1753
0
    break;
1754
0
  case JXFORM_TRANSPOSE:
1755
    /* transpose does NOT have to trim anything */
1756
    /* Need workspace arrays having transposed dimensions. */
1757
0
    need_workspace = TRUE;
1758
0
    transpose_it = TRUE;
1759
0
    break;
1760
0
  case JXFORM_TRANSVERSE:
1761
0
    if (info->trim) {
1762
0
      trim_right_edge(info, srcinfo->output_height);
1763
0
      trim_bottom_edge(info, srcinfo->output_width);
1764
0
    }
1765
    /* Need workspace arrays having transposed dimensions. */
1766
0
    need_workspace = TRUE;
1767
0
    transpose_it = TRUE;
1768
0
    break;
1769
0
  case JXFORM_ROT_90:
1770
0
    if (info->trim)
1771
0
      trim_right_edge(info, srcinfo->output_height);
1772
    /* Need workspace arrays having transposed dimensions. */
1773
0
    need_workspace = TRUE;
1774
0
    transpose_it = TRUE;
1775
0
    break;
1776
0
  case JXFORM_ROT_180:
1777
0
    if (info->trim) {
1778
0
      trim_right_edge(info, srcinfo->output_width);
1779
0
      trim_bottom_edge(info, srcinfo->output_height);
1780
0
    }
1781
    /* Need workspace arrays having same dimensions as source image. */
1782
0
    need_workspace = TRUE;
1783
0
    break;
1784
0
  case JXFORM_ROT_270:
1785
0
    if (info->trim)
1786
0
      trim_bottom_edge(info, srcinfo->output_width);
1787
    /* Need workspace arrays having transposed dimensions. */
1788
0
    need_workspace = TRUE;
1789
0
    transpose_it = TRUE;
1790
0
    break;
1791
0
  case JXFORM_WIPE:
1792
0
    break;
1793
0
  case JXFORM_DROP:
1794
0
    break;
1795
0
  }
1796
1797
  /* Allocate workspace if needed.
1798
   * Note that we allocate arrays padded out to the next iMCU boundary,
1799
   * so that transform routines need not worry about missing edge blocks.
1800
   */
1801
0
  if (need_workspace) {
1802
0
    coef_arrays = (jvirt_barray_ptr *)
1803
0
      (*srcinfo->mem->alloc_small) ((j_common_ptr)srcinfo, JPOOL_IMAGE,
1804
0
                sizeof(jvirt_barray_ptr) * info->num_components);
1805
0
    width_in_iMCUs = (JDIMENSION)
1806
0
      jdiv_round_up((long)info->output_width, (long)info->iMCU_sample_width);
1807
0
    height_in_iMCUs = (JDIMENSION)
1808
0
      jdiv_round_up((long)info->output_height, (long)info->iMCU_sample_height);
1809
0
    for (ci = 0; ci < info->num_components; ci++) {
1810
0
      compptr = srcinfo->comp_info + ci;
1811
0
      if (info->num_components == 1) {
1812
        /* we're going to force samp factors to 1x1 in this case */
1813
0
        h_samp_factor = v_samp_factor = 1;
1814
0
      } else if (transpose_it) {
1815
0
        h_samp_factor = compptr->v_samp_factor;
1816
0
        v_samp_factor = compptr->h_samp_factor;
1817
0
      } else {
1818
0
        h_samp_factor = compptr->h_samp_factor;
1819
0
        v_samp_factor = compptr->v_samp_factor;
1820
0
      }
1821
0
      width_in_blocks = width_in_iMCUs * h_samp_factor;
1822
0
      height_in_blocks = height_in_iMCUs * v_samp_factor;
1823
0
      coef_arrays[ci] = (*srcinfo->mem->request_virt_barray)
1824
0
        ((j_common_ptr)srcinfo, JPOOL_IMAGE, FALSE,
1825
0
         width_in_blocks, height_in_blocks, (JDIMENSION)v_samp_factor);
1826
0
    }
1827
0
    info->workspace_coef_arrays = coef_arrays;
1828
0
  } else
1829
0
    info->workspace_coef_arrays = NULL;
1830
1831
0
  return TRUE;
1832
0
}
1833
1834
1835
/* Transpose destination image parameters */
1836
1837
LOCAL(void)
1838
transpose_critical_parameters(j_compress_ptr dstinfo)
1839
0
{
1840
0
  int tblno, i, j, ci, itemp;
1841
0
  jpeg_component_info *compptr;
1842
0
  JQUANT_TBL *qtblptr;
1843
0
  JDIMENSION jtemp;
1844
0
  UINT16 qtemp;
1845
1846
  /* Transpose image dimensions */
1847
0
  jtemp = dstinfo->image_width;
1848
0
  dstinfo->image_width = dstinfo->image_height;
1849
0
  dstinfo->image_height = jtemp;
1850
#if JPEG_LIB_VERSION >= 70
1851
  itemp = dstinfo->min_DCT_h_scaled_size;
1852
  dstinfo->min_DCT_h_scaled_size = dstinfo->min_DCT_v_scaled_size;
1853
  dstinfo->min_DCT_v_scaled_size = itemp;
1854
#endif
1855
1856
  /* Transpose sampling factors */
1857
0
  for (ci = 0; ci < dstinfo->num_components; ci++) {
1858
0
    compptr = dstinfo->comp_info + ci;
1859
0
    itemp = compptr->h_samp_factor;
1860
0
    compptr->h_samp_factor = compptr->v_samp_factor;
1861
0
    compptr->v_samp_factor = itemp;
1862
0
  }
1863
1864
  /* Transpose quantization tables */
1865
0
  for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
1866
0
    qtblptr = dstinfo->quant_tbl_ptrs[tblno];
1867
0
    if (qtblptr != NULL) {
1868
0
      for (i = 0; i < DCTSIZE; i++) {
1869
0
        for (j = 0; j < i; j++) {
1870
0
          qtemp = qtblptr->quantval[i * DCTSIZE + j];
1871
0
          qtblptr->quantval[i * DCTSIZE + j] =
1872
0
            qtblptr->quantval[j * DCTSIZE + i];
1873
0
          qtblptr->quantval[j * DCTSIZE + i] = qtemp;
1874
0
        }
1875
0
      }
1876
0
    }
1877
0
  }
1878
0
}
1879
1880
1881
/* Adjust Exif image parameters.
1882
 *
1883
 * We try to adjust the Tags ExifImageWidth and ExifImageHeight if possible.
1884
 */
1885
1886
LOCAL(void)
1887
adjust_exif_parameters(JOCTET *data, unsigned int length, JDIMENSION new_width,
1888
                       JDIMENSION new_height)
1889
0
{
1890
0
  boolean is_motorola; /* Flag for byte order */
1891
0
  unsigned int number_of_tags, tagnum;
1892
0
  unsigned int firstoffset, offset;
1893
0
  JDIMENSION new_value;
1894
1895
0
  if (length < 12) return; /* Length of an IFD entry */
1896
1897
  /* Discover byte order */
1898
0
  if (data[0] == 0x49 && data[1] == 0x49)
1899
0
    is_motorola = FALSE;
1900
0
  else if (data[0] == 0x4D && data[1] == 0x4D)
1901
0
    is_motorola = TRUE;
1902
0
  else
1903
0
    return;
1904
1905
  /* Check Tag Mark */
1906
0
  if (is_motorola) {
1907
0
    if (data[2] != 0) return;
1908
0
    if (data[3] != 0x2A) return;
1909
0
  } else {
1910
0
    if (data[3] != 0) return;
1911
0
    if (data[2] != 0x2A) return;
1912
0
  }
1913
1914
  /* Get first IFD offset (offset to IFD0) */
1915
0
  if (is_motorola) {
1916
0
    if (data[4] != 0) return;
1917
0
    if (data[5] != 0) return;
1918
0
    firstoffset = data[6];
1919
0
    firstoffset <<= 8;
1920
0
    firstoffset += data[7];
1921
0
  } else {
1922
0
    if (data[7] != 0) return;
1923
0
    if (data[6] != 0) return;
1924
0
    firstoffset = data[5];
1925
0
    firstoffset <<= 8;
1926
0
    firstoffset += data[4];
1927
0
  }
1928
0
  if (firstoffset > length - 2) return; /* check end of data segment */
1929
1930
  /* Get the number of directory entries contained in this IFD */
1931
0
  if (is_motorola) {
1932
0
    number_of_tags = data[firstoffset];
1933
0
    number_of_tags <<= 8;
1934
0
    number_of_tags += data[firstoffset + 1];
1935
0
  } else {
1936
0
    number_of_tags = data[firstoffset + 1];
1937
0
    number_of_tags <<= 8;
1938
0
    number_of_tags += data[firstoffset];
1939
0
  }
1940
0
  if (number_of_tags == 0) return;
1941
0
  firstoffset += 2;
1942
1943
  /* Search for ExifSubIFD offset Tag in IFD0 */
1944
0
  for (;;) {
1945
0
    if (firstoffset > length - 12) return; /* check end of data segment */
1946
    /* Get Tag number */
1947
0
    if (is_motorola) {
1948
0
      tagnum = data[firstoffset];
1949
0
      tagnum <<= 8;
1950
0
      tagnum += data[firstoffset + 1];
1951
0
    } else {
1952
0
      tagnum = data[firstoffset + 1];
1953
0
      tagnum <<= 8;
1954
0
      tagnum += data[firstoffset];
1955
0
    }
1956
0
    if (tagnum == 0x8769) break; /* found ExifSubIFD offset Tag */
1957
0
    if (--number_of_tags == 0) return;
1958
0
    firstoffset += 12;
1959
0
  }
1960
1961
  /* Get the ExifSubIFD offset */
1962
0
  if (is_motorola) {
1963
0
    if (data[firstoffset + 8] != 0) return;
1964
0
    if (data[firstoffset + 9] != 0) return;
1965
0
    offset = data[firstoffset + 10];
1966
0
    offset <<= 8;
1967
0
    offset += data[firstoffset + 11];
1968
0
  } else {
1969
0
    if (data[firstoffset + 11] != 0) return;
1970
0
    if (data[firstoffset + 10] != 0) return;
1971
0
    offset = data[firstoffset + 9];
1972
0
    offset <<= 8;
1973
0
    offset += data[firstoffset + 8];
1974
0
  }
1975
0
  if (offset > length - 2) return; /* check end of data segment */
1976
1977
  /* Get the number of directory entries contained in this SubIFD */
1978
0
  if (is_motorola) {
1979
0
    number_of_tags = data[offset];
1980
0
    number_of_tags <<= 8;
1981
0
    number_of_tags += data[offset + 1];
1982
0
  } else {
1983
0
    number_of_tags = data[offset + 1];
1984
0
    number_of_tags <<= 8;
1985
0
    number_of_tags += data[offset];
1986
0
  }
1987
0
  if (number_of_tags < 2) return;
1988
0
  offset += 2;
1989
1990
  /* Search for ExifImageWidth and ExifImageHeight Tags in this SubIFD */
1991
0
  do {
1992
0
    if (offset > length - 12) return; /* check end of data segment */
1993
    /* Get Tag number */
1994
0
    if (is_motorola) {
1995
0
      tagnum = data[offset];
1996
0
      tagnum <<= 8;
1997
0
      tagnum += data[offset + 1];
1998
0
    } else {
1999
0
      tagnum = data[offset + 1];
2000
0
      tagnum <<= 8;
2001
0
      tagnum += data[offset];
2002
0
    }
2003
0
    if (tagnum == 0xA002 || tagnum == 0xA003) {
2004
0
      if (tagnum == 0xA002)
2005
0
        new_value = new_width; /* ExifImageWidth Tag */
2006
0
      else
2007
0
        new_value = new_height; /* ExifImageHeight Tag */
2008
0
      if (is_motorola) {
2009
0
        data[offset + 2] = 0; /* Format = unsigned long (4 octets) */
2010
0
        data[offset + 3] = 4;
2011
0
        data[offset + 4] = 0; /* Number Of Components = 1 */
2012
0
        data[offset + 5] = 0;
2013
0
        data[offset + 6] = 0;
2014
0
        data[offset + 7] = 1;
2015
0
        data[offset + 8] = 0;
2016
0
        data[offset + 9] = 0;
2017
0
        data[offset + 10] = (JOCTET)((new_value >> 8) & 0xFF);
2018
0
        data[offset + 11] = (JOCTET)(new_value & 0xFF);
2019
0
      } else {
2020
0
        data[offset + 2] = 4; /* Format = unsigned long (4 octets) */
2021
0
        data[offset + 3] = 0;
2022
0
        data[offset + 4] = 1; /* Number Of Components = 1 */
2023
0
        data[offset + 5] = 0;
2024
0
        data[offset + 6] = 0;
2025
0
        data[offset + 7] = 0;
2026
0
        data[offset + 8] = (JOCTET)(new_value & 0xFF);
2027
0
        data[offset + 9] = (JOCTET)((new_value >> 8) & 0xFF);
2028
0
        data[offset + 10] = 0;
2029
0
        data[offset + 11] = 0;
2030
0
      }
2031
0
    }
2032
0
    offset += 12;
2033
0
  } while (--number_of_tags);
2034
0
}
2035
2036
2037
/* Adjust output image parameters as needed.
2038
 *
2039
 * This must be called after jpeg_copy_critical_parameters()
2040
 * and before jpeg_write_coefficients().
2041
 *
2042
 * The return value is the set of virtual coefficient arrays to be written
2043
 * (either the ones allocated by jtransform_request_workspace, or the
2044
 * original source data arrays).  The caller will need to pass this value
2045
 * to jpeg_write_coefficients().
2046
 */
2047
2048
GLOBAL(jvirt_barray_ptr *)
2049
jtransform_adjust_parameters(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
2050
                             jvirt_barray_ptr *src_coef_arrays,
2051
                             jpeg_transform_info *info)
2052
0
{
2053
  /* If force-to-grayscale is requested, adjust destination parameters */
2054
0
  if (info->force_grayscale) {
2055
    /* First, ensure we have YCbCr or grayscale data, and that the source's
2056
     * Y channel is full resolution.  (No reasonable person would make Y
2057
     * be less than full resolution, so actually coping with that case
2058
     * isn't worth extra code space.  But we check it to avoid crashing.)
2059
     */
2060
0
    if (((dstinfo->jpeg_color_space == JCS_YCbCr &&
2061
0
          dstinfo->num_components == 3) ||
2062
0
         (dstinfo->jpeg_color_space == JCS_GRAYSCALE &&
2063
0
          dstinfo->num_components == 1)) &&
2064
0
        srcinfo->comp_info[0].h_samp_factor == srcinfo->max_h_samp_factor &&
2065
0
        srcinfo->comp_info[0].v_samp_factor == srcinfo->max_v_samp_factor) {
2066
      /* We use jpeg_set_colorspace to make sure subsidiary settings get fixed
2067
       * properly.  Among other things, it sets the target h_samp_factor &
2068
       * v_samp_factor to 1, which typically won't match the source.
2069
       * We have to preserve the source's quantization table number, however.
2070
       */
2071
0
      int sv_quant_tbl_no = dstinfo->comp_info[0].quant_tbl_no;
2072
0
      jpeg_set_colorspace(dstinfo, JCS_GRAYSCALE);
2073
0
      dstinfo->comp_info[0].quant_tbl_no = sv_quant_tbl_no;
2074
0
    } else {
2075
      /* Sorry, can't do it */
2076
0
      ERREXIT(dstinfo, JERR_CONVERSION_NOTIMPL);
2077
0
    }
2078
0
  } else if (info->num_components == 1) {
2079
    /* For a single-component source, we force the destination sampling factors
2080
     * to 1x1, with or without force_grayscale.  This is useful because some
2081
     * decoders choke on grayscale images with other sampling factors.
2082
     */
2083
0
    dstinfo->comp_info[0].h_samp_factor = 1;
2084
0
    dstinfo->comp_info[0].v_samp_factor = 1;
2085
0
  }
2086
2087
  /* Correct the destination's image dimensions as necessary
2088
   * for rotate/flip, resize, and crop operations.
2089
   */
2090
#if JPEG_LIB_VERSION >= 80
2091
  dstinfo->jpeg_width = info->output_width;
2092
  dstinfo->jpeg_height = info->output_height;
2093
#endif
2094
2095
  /* Transpose destination image parameters, adjust quantization */
2096
0
  switch (info->transform) {
2097
0
  case JXFORM_TRANSPOSE:
2098
0
  case JXFORM_TRANSVERSE:
2099
0
  case JXFORM_ROT_90:
2100
0
  case JXFORM_ROT_270:
2101
0
#if JPEG_LIB_VERSION < 80
2102
0
    dstinfo->image_width = info->output_height;
2103
0
    dstinfo->image_height = info->output_width;
2104
0
#endif
2105
0
    transpose_critical_parameters(dstinfo);
2106
0
    break;
2107
0
  case JXFORM_DROP:
2108
0
    if (info->drop_width != 0 && info->drop_height != 0)
2109
0
      adjust_quant(srcinfo, src_coef_arrays,
2110
0
                   info->drop_ptr, info->drop_coef_arrays,
2111
0
                   info->trim, dstinfo);
2112
0
    break;
2113
0
  default:
2114
0
#if JPEG_LIB_VERSION < 80
2115
0
    dstinfo->image_width = info->output_width;
2116
0
    dstinfo->image_height = info->output_height;
2117
0
#endif
2118
0
    break;
2119
0
  }
2120
2121
  /* Adjust Exif properties */
2122
0
  if (srcinfo->marker_list != NULL &&
2123
0
      srcinfo->marker_list->marker == JPEG_APP0 + 1 &&
2124
0
      srcinfo->marker_list->data_length >= 6 &&
2125
0
      srcinfo->marker_list->data[0] == 0x45 &&
2126
0
      srcinfo->marker_list->data[1] == 0x78 &&
2127
0
      srcinfo->marker_list->data[2] == 0x69 &&
2128
0
      srcinfo->marker_list->data[3] == 0x66 &&
2129
0
      srcinfo->marker_list->data[4] == 0 &&
2130
0
      srcinfo->marker_list->data[5] == 0) {
2131
    /* Suppress output of JFIF marker */
2132
0
    dstinfo->write_JFIF_header = FALSE;
2133
    /* Adjust Exif image parameters */
2134
#if JPEG_LIB_VERSION >= 80
2135
    if (dstinfo->jpeg_width != srcinfo->image_width ||
2136
        dstinfo->jpeg_height != srcinfo->image_height)
2137
      /* Align data segment to start of TIFF structure for parsing */
2138
      adjust_exif_parameters(srcinfo->marker_list->data + 6,
2139
                             srcinfo->marker_list->data_length - 6,
2140
                             dstinfo->jpeg_width, dstinfo->jpeg_height);
2141
#else
2142
0
    if (dstinfo->image_width != srcinfo->image_width ||
2143
0
        dstinfo->image_height != srcinfo->image_height)
2144
      /* Align data segment to start of TIFF structure for parsing */
2145
0
      adjust_exif_parameters(srcinfo->marker_list->data + 6,
2146
0
                             srcinfo->marker_list->data_length - 6,
2147
0
                             dstinfo->image_width, dstinfo->image_height);
2148
0
#endif
2149
0
  }
2150
2151
  /* Return the appropriate output data set */
2152
0
  if (info->workspace_coef_arrays != NULL)
2153
0
    return info->workspace_coef_arrays;
2154
0
  return src_coef_arrays;
2155
0
}
2156
2157
2158
/* Execute the actual transformation, if any.
2159
 *
2160
 * This must be called *after* jpeg_write_coefficients, because it depends
2161
 * on jpeg_write_coefficients to have computed subsidiary values such as
2162
 * the per-component width and height fields in the destination object.
2163
 *
2164
 * Note that some transformations will modify the source data arrays!
2165
 */
2166
2167
GLOBAL(void)
2168
jtransform_execute_transform(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
2169
                             jvirt_barray_ptr *src_coef_arrays,
2170
                             jpeg_transform_info *info)
2171
0
{
2172
0
  jvirt_barray_ptr *dst_coef_arrays = info->workspace_coef_arrays;
2173
2174
  /* Note: conditions tested here should match those in switch statement
2175
   * in jtransform_request_workspace()
2176
   */
2177
0
  switch (info->transform) {
2178
0
  case JXFORM_NONE:
2179
0
    if (info->output_width > srcinfo->output_width ||
2180
0
        info->output_height > srcinfo->output_height) {
2181
0
      if (info->output_width > srcinfo->output_width &&
2182
0
          info->crop_width_set == JCROP_REFLECT)
2183
0
        do_crop_ext_reflect(srcinfo, dstinfo,
2184
0
                            info->x_crop_offset, info->y_crop_offset,
2185
0
                            src_coef_arrays, dst_coef_arrays);
2186
0
      else if (info->output_width > srcinfo->output_width &&
2187
0
               info->crop_width_set == JCROP_FORCE)
2188
0
        do_crop_ext_flat(srcinfo, dstinfo,
2189
0
                         info->x_crop_offset, info->y_crop_offset,
2190
0
                         src_coef_arrays, dst_coef_arrays);
2191
0
      else
2192
0
        do_crop_ext_zero(srcinfo, dstinfo,
2193
0
                         info->x_crop_offset, info->y_crop_offset,
2194
0
                         src_coef_arrays, dst_coef_arrays);
2195
0
    } else if (info->x_crop_offset != 0 || info->y_crop_offset != 0)
2196
0
      do_crop(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2197
0
              src_coef_arrays, dst_coef_arrays);
2198
0
    break;
2199
0
  case JXFORM_FLIP_H:
2200
0
    if (info->y_crop_offset != 0 || info->slow_hflip)
2201
0
      do_flip_h(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2202
0
                src_coef_arrays, dst_coef_arrays);
2203
0
    else
2204
0
      do_flip_h_no_crop(srcinfo, dstinfo, info->x_crop_offset,
2205
0
                        src_coef_arrays);
2206
0
    break;
2207
0
  case JXFORM_FLIP_V:
2208
0
    do_flip_v(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2209
0
              src_coef_arrays, dst_coef_arrays);
2210
0
    break;
2211
0
  case JXFORM_TRANSPOSE:
2212
0
    do_transpose(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2213
0
                 src_coef_arrays, dst_coef_arrays);
2214
0
    break;
2215
0
  case JXFORM_TRANSVERSE:
2216
0
    do_transverse(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2217
0
                  src_coef_arrays, dst_coef_arrays);
2218
0
    break;
2219
0
  case JXFORM_ROT_90:
2220
0
    do_rot_90(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2221
0
              src_coef_arrays, dst_coef_arrays);
2222
0
    break;
2223
0
  case JXFORM_ROT_180:
2224
0
    do_rot_180(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2225
0
               src_coef_arrays, dst_coef_arrays);
2226
0
    break;
2227
0
  case JXFORM_ROT_270:
2228
0
    do_rot_270(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2229
0
               src_coef_arrays, dst_coef_arrays);
2230
0
    break;
2231
0
  case JXFORM_WIPE:
2232
0
    if (info->crop_width_set == JCROP_REFLECT &&
2233
0
        info->y_crop_offset == 0 && info->drop_height ==
2234
0
        (JDIMENSION)jdiv_round_up
2235
0
          ((long)info->output_height, (long)info->iMCU_sample_height) &&
2236
0
        (info->x_crop_offset == 0 ||
2237
0
         info->x_crop_offset + info->drop_width ==
2238
0
         (JDIMENSION)jdiv_round_up
2239
0
           ((long)info->output_width, (long)info->iMCU_sample_width)))
2240
0
      do_reflect(srcinfo, dstinfo, info->x_crop_offset,
2241
0
                 src_coef_arrays, info->drop_width, info->drop_height);
2242
0
    else if (info->crop_width_set == JCROP_FORCE)
2243
0
      do_flatten(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2244
0
                 src_coef_arrays, info->drop_width, info->drop_height);
2245
0
    else
2246
0
      do_wipe(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2247
0
              src_coef_arrays, info->drop_width, info->drop_height);
2248
0
    break;
2249
0
  case JXFORM_DROP:
2250
0
    if (info->drop_width != 0 && info->drop_height != 0)
2251
0
      do_drop(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
2252
0
              src_coef_arrays, info->drop_ptr, info->drop_coef_arrays,
2253
0
              info->drop_width, info->drop_height);
2254
0
    break;
2255
0
  }
2256
0
}
2257
2258
/* jtransform_perfect_transform
2259
 *
2260
 * Determine whether lossless transformation is perfectly
2261
 * possible for a specified image and transformation.
2262
 *
2263
 * Inputs:
2264
 *   image_width, image_height: source image dimensions.
2265
 *   MCU_width, MCU_height: pixel dimensions of MCU.
2266
 *   transform: transformation identifier.
2267
 * Parameter sources from initialized jpeg_struct
2268
 * (after reading source header):
2269
 *   image_width = cinfo.image_width
2270
 *   image_height = cinfo.image_height
2271
 *   MCU_width = cinfo.max_h_samp_factor * cinfo.block_size
2272
 *   MCU_height = cinfo.max_v_samp_factor * cinfo.block_size
2273
 * Result:
2274
 *   TRUE = perfect transformation possible
2275
 *   FALSE = perfect transformation not possible
2276
 *           (may use custom action then)
2277
 */
2278
2279
GLOBAL(boolean)
2280
jtransform_perfect_transform(JDIMENSION image_width, JDIMENSION image_height,
2281
                             int MCU_width, int MCU_height,
2282
                             JXFORM_CODE transform)
2283
0
{
2284
0
  boolean result = TRUE; /* initialize TRUE */
2285
2286
0
  switch (transform) {
2287
0
  case JXFORM_FLIP_H:
2288
0
  case JXFORM_ROT_270:
2289
0
    if (image_width % (JDIMENSION)MCU_width)
2290
0
      result = FALSE;
2291
0
    break;
2292
0
  case JXFORM_FLIP_V:
2293
0
  case JXFORM_ROT_90:
2294
0
    if (image_height % (JDIMENSION)MCU_height)
2295
0
      result = FALSE;
2296
0
    break;
2297
0
  case JXFORM_TRANSVERSE:
2298
0
  case JXFORM_ROT_180:
2299
0
    if (image_width % (JDIMENSION)MCU_width)
2300
0
      result = FALSE;
2301
0
    if (image_height % (JDIMENSION)MCU_height)
2302
0
      result = FALSE;
2303
0
    break;
2304
0
  default:
2305
0
    break;
2306
0
  }
2307
2308
0
  return result;
2309
0
}
2310
2311
#endif /* TRANSFORMS_SUPPORTED */
2312
2313
2314
/* Setup decompression object to save desired markers in memory.
2315
 * This must be called before jpeg_read_header() to have the desired effect.
2316
 */
2317
2318
GLOBAL(void)
2319
jcopy_markers_setup(j_decompress_ptr srcinfo, JCOPY_OPTION option)
2320
0
{
2321
0
#ifdef SAVE_MARKERS_SUPPORTED
2322
0
  int m;
2323
2324
  /* Save comments unless JCOPYOPT_NONE or JCOPYOPT_ICC specified */
2325
0
  if (option != JCOPYOPT_NONE && option != JCOPYOPT_ICC) {
2326
0
    jpeg_save_markers(srcinfo, JPEG_COM, 0xFFFF);
2327
0
  }
2328
  /* Save all APPn markers iff JCOPYOPT_ALL* specified ... */
2329
0
  if (option == JCOPYOPT_ALL || option == JCOPYOPT_ALL_EXCEPT_ICC) {
2330
0
    for (m = 0; m < 16; m++) {
2331
      /* ... except APP2 markers if JCOPYOPT_ALL_EXCEPT_ICC specified */
2332
0
      if (option == JCOPYOPT_ALL_EXCEPT_ICC && m == 2)
2333
0
        continue;
2334
0
      jpeg_save_markers(srcinfo, JPEG_APP0 + m, 0xFFFF);
2335
0
    }
2336
0
  }
2337
  /* Save only APP2 markers if JCOPYOPT_ICC specified */
2338
0
  if (option == JCOPYOPT_ICC) {
2339
0
    jpeg_save_markers(srcinfo, JPEG_APP0 + 2, 0xFFFF);
2340
0
  }
2341
0
#endif /* SAVE_MARKERS_SUPPORTED */
2342
0
}
2343
2344
/* Copy markers saved in the given source object to the destination object.
2345
 * This should be called just after jpeg_start_compress() or
2346
 * jpeg_write_coefficients().
2347
 * Note that those routines will have written the SOI, and also the
2348
 * JFIF APP0 or Adobe APP14 markers if selected.
2349
 */
2350
2351
GLOBAL(void)
2352
jcopy_markers_execute(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
2353
                      JCOPY_OPTION option)
2354
0
{
2355
0
  jpeg_saved_marker_ptr marker;
2356
2357
0
  for (marker = srcinfo->marker_list; marker != NULL; marker = marker->next) {
2358
0
    if (option == JCOPYOPT_NONE)
2359
0
      continue;
2360
0
    else if (option == JCOPYOPT_COMMENTS) {
2361
0
      if (marker->marker != JPEG_COM)
2362
0
        continue;
2363
0
    } else if (option == JCOPYOPT_ALL_EXCEPT_ICC) {
2364
0
      if (marker->marker == JPEG_APP0 + 2)
2365
0
        continue;
2366
0
    } else if (option == JCOPYOPT_ICC) {
2367
0
      if (marker->marker != JPEG_APP0 + 2)
2368
0
        continue;
2369
0
    }
2370
    /* To avoid confusion, we do not output JFIF and Adobe APP14 markers if the
2371
     * encoder library already wrote one.
2372
     */
2373
0
    if (dstinfo->write_JFIF_header &&
2374
0
        marker->marker == JPEG_APP0 &&
2375
0
        marker->data_length >= 5 &&
2376
0
        marker->data[0] == 0x4A &&
2377
0
        marker->data[1] == 0x46 &&
2378
0
        marker->data[2] == 0x49 &&
2379
0
        marker->data[3] == 0x46 &&
2380
0
        marker->data[4] == 0)
2381
0
      continue;                 /* reject duplicate JFIF */
2382
0
    if (dstinfo->write_Adobe_marker &&
2383
0
        marker->marker == JPEG_APP0 + 14 &&
2384
0
        marker->data_length >= 5 &&
2385
0
        marker->data[0] == 0x41 &&
2386
0
        marker->data[1] == 0x64 &&
2387
0
        marker->data[2] == 0x6F &&
2388
0
        marker->data[3] == 0x62 &&
2389
0
        marker->data[4] == 0x65)
2390
0
      continue;                 /* reject duplicate Adobe */
2391
0
    jpeg_write_marker(dstinfo, marker->marker,
2392
0
                      marker->data, marker->data_length);
2393
0
  }
2394
0
}