Coverage Report

Created: 2026-10-17 06:27

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