Coverage Report

Created: 2026-10-17 06:27

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