Coverage Report

Created: 2026-05-30 06:16

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