Coverage Report

Created: 2026-04-12 06:05

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