Coverage Report

Created: 2026-03-12 07:03

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