Coverage Report

Created: 2026-04-28 06:57

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