Coverage Report

Created: 2026-10-17 07:27

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