Coverage Report

Created: 2024-05-15 07:08

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