4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1997-2011, Thomas G. Lane, Guido Vollbeding.
7 * Copyright (C) 2010, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README file.
10 * This file contains image transformation routines and other utility code
11 * used by the jpegtran sample application. These are NOT part of the core
12 * JPEG library. But we keep these routines separate from jpegtran.c to
13 * ease the task of maintaining jpegtran-like programs that have other user
17 #include <sal/config.h>
22 #include "transupp.h" /* My own external interface */
24 #include <ctype.h> /* to declare isdigit() */
26 /* Definition of jdiv_round_up is copied here from jutils.c in jpeg-8c.tar.gz,
27 just as the rest of this file appears to be copied here from transupp.c in
30 jdiv_round_up (long a
, long b
)
31 /* Compute a/b rounded up to next integer, ie, ceil(a/b) */
32 /* Assumes a >= 0, b > 0 */
34 return (a
+ b
- 1) / b
;
37 #if JPEG_LIB_VERSION >= 70
38 #define dstinfo_min_DCT_h_scaled_size dstinfo->min_DCT_h_scaled_size
39 #define dstinfo_min_DCT_v_scaled_size dstinfo->min_DCT_v_scaled_size
41 #define dstinfo_min_DCT_h_scaled_size DCTSIZE
42 #define dstinfo_min_DCT_v_scaled_size DCTSIZE
46 #if TRANSFORMS_SUPPORTED
49 * Lossless image transformation routines. These routines work on DCT
50 * coefficient arrays and thus do not require any lossy decompression
51 * or recompression of the image.
52 * Thanks to Guido Vollbeding for the initial design and code of this feature,
53 * and to Ben Jackson for introducing the cropping feature.
55 * Horizontal flipping is done in-place, using a single top-to-bottom
56 * pass through the virtual source array. It will thus be much the
57 * fastest option for images larger than main memory.
59 * The other routines require a set of destination virtual arrays, so they
60 * need twice as much memory as jpegtran normally does. The destination
61 * arrays are always written in normal scan order (top to bottom) because
62 * the virtual array manager expects this. The source arrays will be scanned
63 * in the corresponding order, which means multiple passes through the source
64 * arrays for most of the transforms. That could result in much thrashing
65 * if the image is larger than main memory.
67 * If cropping or trimming is involved, the destination arrays may be smaller
68 * than the source arrays. Note it is not possible to do horizontal flip
69 * in-place when a nonzero Y crop offset is specified, since we'd have to move
70 * data from one block row to another but the virtual array manager doesn't
71 * guarantee we can touch more than one row at a time. So in that case,
72 * we have to use a separate destination array.
74 * Some notes about the operating environment of the individual transform
76 * 1. Both the source and destination virtual arrays are allocated from the
77 * source JPEG object, and therefore should be manipulated by calling the
78 * source's memory manager.
79 * 2. The destination's component count should be used. It may be smaller
80 * than the source's when forcing to grayscale.
81 * 3. Likewise the destination's sampling factors should be used. When
82 * forcing to grayscale the destination's sampling factors will be all 1,
83 * and we may as well take that as the effective iMCU size.
84 * 4. When "trim" is in effect, the destination's dimensions will be the
85 * trimmed values but the source's will be untrimmed.
86 * 5. When "crop" is in effect, the destination's dimensions will be the
87 * cropped values but the source's will be uncropped. Each transform
88 * routine is responsible for picking up source data starting at the
89 * correct X and Y offset for the crop region. (The X and Y offsets
90 * passed to the transform routines are measured in iMCU blocks of the
92 * 6. All the routines assume that the source and destination buffers are
93 * padded out to a full iMCU boundary. This is true, although for the
94 * source buffer it is an undocumented property of jdcoefct.c.
97 static void lcl_jcopy_block_row (JBLOCKROW input_row
, JBLOCKROW output_row
, JDIMENSION num_blocks
)
98 /* Copy a row of coefficient blocks from one place to another. */
101 FMEMCOPY(output_row
, input_row
, num_blocks
* (DCTSIZE2
* SIZEOF(JCOEF
)));
103 JCOEFPTR inptr
, outptr
;
106 inptr
= (JCOEFPTR
) input_row
;
107 outptr
= (JCOEFPTR
) output_row
;
108 for (count
= (long) num_blocks
* DCTSIZE2
; count
> 0; count
--) {
109 *outptr
++ = *inptr
++;
115 do_crop (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
116 JDIMENSION x_crop_offset
, JDIMENSION y_crop_offset
,
117 jvirt_barray_ptr
*src_coef_arrays
,
118 jvirt_barray_ptr
*dst_coef_arrays
)
119 /* Crop. This is only used when no rotate/flip is requested with the crop. */
121 JDIMENSION dst_blk_y
, x_crop_blocks
, y_crop_blocks
;
123 JBLOCKARRAY src_buffer
, dst_buffer
;
124 jpeg_component_info
*compptr
;
126 /* We simply have to copy the right amount of data (the destination's
127 * image size) starting at the given X and Y offsets in the source.
129 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
130 compptr
= dstinfo
->comp_info
+ ci
;
131 x_crop_blocks
= x_crop_offset
* compptr
->h_samp_factor
;
132 y_crop_blocks
= y_crop_offset
* compptr
->v_samp_factor
;
133 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_blocks
;
134 dst_blk_y
+= compptr
->v_samp_factor
) {
135 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
136 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
137 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
138 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
139 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
140 dst_blk_y
+ y_crop_blocks
,
141 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
142 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
143 lcl_jcopy_block_row(src_buffer
[offset_y
] + x_crop_blocks
,
144 dst_buffer
[offset_y
],
145 compptr
->width_in_blocks
);
153 do_flip_h_no_crop (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
154 JDIMENSION x_crop_offset
,
155 jvirt_barray_ptr
*src_coef_arrays
)
156 /* Horizontal flip; done in-place, so no separate dest array is required.
157 * NB: this only works when y_crop_offset is zero.
160 JDIMENSION MCU_cols
, comp_width
, blk_x
, blk_y
, x_crop_blocks
;
165 jpeg_component_info
*compptr
;
167 /* Horizontal mirroring of DCT blocks is accomplished by swapping
168 * pairs of blocks in-place. Within a DCT block, we perform horizontal
169 * mirroring by changing the signs of odd-numbered columns.
170 * Partial iMCUs at the right edge are left untouched.
172 MCU_cols
= srcinfo
->output_width
/
173 (dstinfo
->max_h_samp_factor
* dstinfo_min_DCT_h_scaled_size
);
175 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
176 compptr
= dstinfo
->comp_info
+ ci
;
177 comp_width
= MCU_cols
* compptr
->h_samp_factor
;
178 x_crop_blocks
= x_crop_offset
* compptr
->h_samp_factor
;
179 for (blk_y
= 0; blk_y
< compptr
->height_in_blocks
;
180 blk_y
+= compptr
->v_samp_factor
) {
181 buffer
= (*srcinfo
->mem
->access_virt_barray
)
182 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
], blk_y
,
183 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
184 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
185 /* Do the mirroring */
186 for (blk_x
= 0; blk_x
* 2 < comp_width
; blk_x
++) {
187 ptr1
= buffer
[offset_y
][blk_x
];
188 ptr2
= buffer
[offset_y
][comp_width
- blk_x
- 1];
189 /* this unrolled loop doesn't need to know which row it's on... */
190 for (k
= 0; k
< DCTSIZE2
; k
+= 2) {
191 temp1
= *ptr1
; /* swap even column */
195 temp1
= *ptr1
; /* swap odd column with sign change */
201 if (x_crop_blocks
> 0) {
202 /* Now left-justify the portion of the data to be kept.
203 * We can't use a single lcl_jcopy_block_row() call because that routine
204 * depends on memcpy(), whose behavior is unspecified for overlapping
205 * source and destination areas. Sigh.
207 for (blk_x
= 0; blk_x
< compptr
->width_in_blocks
; blk_x
++) {
208 lcl_jcopy_block_row(buffer
[offset_y
] + blk_x
+ x_crop_blocks
,
209 buffer
[offset_y
] + blk_x
,
220 do_flip_h (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
221 JDIMENSION x_crop_offset
, JDIMENSION y_crop_offset
,
222 jvirt_barray_ptr
*src_coef_arrays
,
223 jvirt_barray_ptr
*dst_coef_arrays
)
224 /* Horizontal flip in general cropping case */
226 JDIMENSION MCU_cols
, comp_width
, dst_blk_x
, dst_blk_y
;
227 JDIMENSION x_crop_blocks
, y_crop_blocks
;
229 JBLOCKARRAY src_buffer
, dst_buffer
;
230 JBLOCKROW src_row_ptr
, dst_row_ptr
;
231 JCOEFPTR src_ptr
, dst_ptr
;
232 jpeg_component_info
*compptr
;
234 /* Here we must output into a separate array because we can't touch
235 * different rows of a single virtual array simultaneously. Otherwise,
236 * this is essentially the same as the routine above.
238 MCU_cols
= srcinfo
->output_width
/
239 (dstinfo
->max_h_samp_factor
* dstinfo_min_DCT_h_scaled_size
);
241 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
242 compptr
= dstinfo
->comp_info
+ ci
;
243 comp_width
= MCU_cols
* compptr
->h_samp_factor
;
244 x_crop_blocks
= x_crop_offset
* compptr
->h_samp_factor
;
245 y_crop_blocks
= y_crop_offset
* compptr
->v_samp_factor
;
246 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_blocks
;
247 dst_blk_y
+= compptr
->v_samp_factor
) {
248 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
249 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
250 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
251 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
252 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
253 dst_blk_y
+ y_crop_blocks
,
254 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
255 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
256 dst_row_ptr
= dst_buffer
[offset_y
];
257 src_row_ptr
= src_buffer
[offset_y
];
258 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_blocks
; dst_blk_x
++) {
259 if (x_crop_blocks
+ dst_blk_x
< comp_width
) {
260 /* Do the mirrorable blocks */
261 dst_ptr
= dst_row_ptr
[dst_blk_x
];
262 src_ptr
= src_row_ptr
[comp_width
- x_crop_blocks
- dst_blk_x
- 1];
263 /* this unrolled loop doesn't need to know which row it's on... */
264 for (k
= 0; k
< DCTSIZE2
; k
+= 2) {
265 *dst_ptr
++ = *src_ptr
++; /* copy even column */
266 *dst_ptr
++ = - *src_ptr
++; /* copy odd column with sign change */
269 /* Copy last partial block(s) verbatim */
270 lcl_jcopy_block_row(src_row_ptr
+ dst_blk_x
+ x_crop_blocks
,
271 dst_row_ptr
+ dst_blk_x
,
282 do_flip_v (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
283 JDIMENSION x_crop_offset
, JDIMENSION y_crop_offset
,
284 jvirt_barray_ptr
*src_coef_arrays
,
285 jvirt_barray_ptr
*dst_coef_arrays
)
288 JDIMENSION MCU_rows
, comp_height
, dst_blk_x
, dst_blk_y
;
289 JDIMENSION x_crop_blocks
, y_crop_blocks
;
290 int ci
, i
, j
, offset_y
;
291 JBLOCKARRAY src_buffer
, dst_buffer
;
292 JBLOCKROW src_row_ptr
, dst_row_ptr
;
293 JCOEFPTR src_ptr
, dst_ptr
;
294 jpeg_component_info
*compptr
;
296 /* We output into a separate array because we can't touch different
297 * rows of the source virtual array simultaneously. Otherwise, this
298 * is a pretty straightforward analog of horizontal flip.
299 * Within a DCT block, vertical mirroring is done by changing the signs
300 * of odd-numbered rows.
301 * Partial iMCUs at the bottom edge are copied verbatim.
303 MCU_rows
= srcinfo
->output_height
/
304 (dstinfo
->max_v_samp_factor
* dstinfo_min_DCT_v_scaled_size
);
306 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
307 compptr
= dstinfo
->comp_info
+ ci
;
308 comp_height
= MCU_rows
* compptr
->v_samp_factor
;
309 x_crop_blocks
= x_crop_offset
* compptr
->h_samp_factor
;
310 y_crop_blocks
= y_crop_offset
* compptr
->v_samp_factor
;
311 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_blocks
;
312 dst_blk_y
+= compptr
->v_samp_factor
) {
313 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
314 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
315 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
316 if (y_crop_blocks
+ dst_blk_y
< comp_height
) {
317 /* Row is within the mirrorable area. */
318 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
319 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
320 comp_height
- y_crop_blocks
- dst_blk_y
-
321 (JDIMENSION
) compptr
->v_samp_factor
,
322 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
324 /* Bottom-edge blocks will be copied verbatim. */
325 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
326 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
327 dst_blk_y
+ y_crop_blocks
,
328 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
330 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
331 if (y_crop_blocks
+ dst_blk_y
< comp_height
) {
332 /* Row is within the mirrorable area. */
333 dst_row_ptr
= dst_buffer
[offset_y
];
334 src_row_ptr
= src_buffer
[compptr
->v_samp_factor
- offset_y
- 1];
335 src_row_ptr
+= x_crop_blocks
;
336 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_blocks
;
338 dst_ptr
= dst_row_ptr
[dst_blk_x
];
339 src_ptr
= src_row_ptr
[dst_blk_x
];
340 for (i
= 0; i
< DCTSIZE
; i
+= 2) {
342 for (j
= 0; j
< DCTSIZE
; j
++)
343 *dst_ptr
++ = *src_ptr
++;
344 /* copy odd row with sign change */
345 for (j
= 0; j
< DCTSIZE
; j
++)
346 *dst_ptr
++ = - *src_ptr
++;
350 /* Just copy row verbatim. */
351 lcl_jcopy_block_row(src_buffer
[offset_y
] + x_crop_blocks
,
352 dst_buffer
[offset_y
],
353 compptr
->width_in_blocks
);
362 do_transpose (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
363 JDIMENSION x_crop_offset
, JDIMENSION y_crop_offset
,
364 jvirt_barray_ptr
*src_coef_arrays
,
365 jvirt_barray_ptr
*dst_coef_arrays
)
366 /* Transpose source into destination */
368 JDIMENSION dst_blk_x
, dst_blk_y
, x_crop_blocks
, y_crop_blocks
;
369 int ci
, i
, j
, offset_x
, offset_y
;
370 JBLOCKARRAY src_buffer
, dst_buffer
;
371 JCOEFPTR src_ptr
, dst_ptr
;
372 jpeg_component_info
*compptr
;
374 /* Transposing pixels within a block just requires transposing the
376 * Partial iMCUs at the edges require no special treatment; we simply
377 * process all the available DCT blocks for every component.
379 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
380 compptr
= dstinfo
->comp_info
+ ci
;
381 x_crop_blocks
= x_crop_offset
* compptr
->h_samp_factor
;
382 y_crop_blocks
= y_crop_offset
* compptr
->v_samp_factor
;
383 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_blocks
;
384 dst_blk_y
+= compptr
->v_samp_factor
) {
385 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
386 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
387 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
388 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
389 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_blocks
;
390 dst_blk_x
+= compptr
->h_samp_factor
) {
391 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
392 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
393 dst_blk_x
+ x_crop_blocks
,
394 (JDIMENSION
) compptr
->h_samp_factor
, FALSE
);
395 for (offset_x
= 0; offset_x
< compptr
->h_samp_factor
; offset_x
++) {
396 dst_ptr
= dst_buffer
[offset_y
][dst_blk_x
+ offset_x
];
397 src_ptr
= src_buffer
[offset_x
][dst_blk_y
+ offset_y
+ y_crop_blocks
];
398 for (i
= 0; i
< DCTSIZE
; i
++)
399 for (j
= 0; j
< DCTSIZE
; j
++)
400 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
410 do_rot_90 (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
411 JDIMENSION x_crop_offset
, JDIMENSION y_crop_offset
,
412 jvirt_barray_ptr
*src_coef_arrays
,
413 jvirt_barray_ptr
*dst_coef_arrays
)
414 /* 90 degree rotation is equivalent to
415 * 1. Transposing the image;
416 * 2. Horizontal mirroring.
417 * These two steps are merged into a single processing routine.
420 JDIMENSION MCU_cols
, comp_width
, dst_blk_x
, dst_blk_y
;
421 JDIMENSION x_crop_blocks
, y_crop_blocks
;
422 int ci
, i
, j
, offset_x
, offset_y
;
423 JBLOCKARRAY src_buffer
, dst_buffer
;
424 JCOEFPTR src_ptr
, dst_ptr
;
425 jpeg_component_info
*compptr
;
427 /* Because of the horizontal mirror step, we can't process partial iMCUs
428 * at the (output) right edge properly. They just get transposed and
431 MCU_cols
= srcinfo
->output_height
/
432 (dstinfo
->max_h_samp_factor
* dstinfo_min_DCT_h_scaled_size
);
434 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
435 compptr
= dstinfo
->comp_info
+ ci
;
436 comp_width
= MCU_cols
* compptr
->h_samp_factor
;
437 x_crop_blocks
= x_crop_offset
* compptr
->h_samp_factor
;
438 y_crop_blocks
= y_crop_offset
* compptr
->v_samp_factor
;
439 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_blocks
;
440 dst_blk_y
+= compptr
->v_samp_factor
) {
441 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
442 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
443 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
444 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
445 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_blocks
;
446 dst_blk_x
+= compptr
->h_samp_factor
) {
447 if (x_crop_blocks
+ dst_blk_x
< comp_width
) {
448 /* Block is within the mirrorable area. */
449 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
450 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
451 comp_width
- x_crop_blocks
- dst_blk_x
-
452 (JDIMENSION
) compptr
->h_samp_factor
,
453 (JDIMENSION
) compptr
->h_samp_factor
, FALSE
);
455 /* Edge blocks are transposed but not mirrored. */
456 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
457 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
458 dst_blk_x
+ x_crop_blocks
,
459 (JDIMENSION
) compptr
->h_samp_factor
, FALSE
);
461 for (offset_x
= 0; offset_x
< compptr
->h_samp_factor
; offset_x
++) {
462 dst_ptr
= dst_buffer
[offset_y
][dst_blk_x
+ offset_x
];
463 if (x_crop_blocks
+ dst_blk_x
< comp_width
) {
464 /* Block is within the mirrorable area. */
465 src_ptr
= src_buffer
[compptr
->h_samp_factor
- offset_x
- 1]
466 [dst_blk_y
+ offset_y
+ y_crop_blocks
];
467 for (i
= 0; i
< DCTSIZE
; i
++) {
468 for (j
= 0; j
< DCTSIZE
; j
++)
469 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
471 for (j
= 0; j
< DCTSIZE
; j
++)
472 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
475 /* Edge blocks are transposed but not mirrored. */
476 src_ptr
= src_buffer
[offset_x
]
477 [dst_blk_y
+ offset_y
+ y_crop_blocks
];
478 for (i
= 0; i
< DCTSIZE
; i
++)
479 for (j
= 0; j
< DCTSIZE
; j
++)
480 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
491 do_rot_270 (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
492 JDIMENSION x_crop_offset
, JDIMENSION y_crop_offset
,
493 jvirt_barray_ptr
*src_coef_arrays
,
494 jvirt_barray_ptr
*dst_coef_arrays
)
495 /* 270 degree rotation is equivalent to
496 * 1. Horizontal mirroring;
497 * 2. Transposing the image.
498 * These two steps are merged into a single processing routine.
501 JDIMENSION MCU_rows
, comp_height
, dst_blk_x
, dst_blk_y
;
502 JDIMENSION x_crop_blocks
, y_crop_blocks
;
503 int ci
, i
, j
, offset_x
, offset_y
;
504 JBLOCKARRAY src_buffer
, dst_buffer
;
505 JCOEFPTR src_ptr
, dst_ptr
;
506 jpeg_component_info
*compptr
;
508 /* Because of the horizontal mirror step, we can't process partial iMCUs
509 * at the (output) bottom edge properly. They just get transposed and
512 MCU_rows
= srcinfo
->output_width
/
513 (dstinfo
->max_v_samp_factor
* dstinfo_min_DCT_v_scaled_size
);
515 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
516 compptr
= dstinfo
->comp_info
+ ci
;
517 comp_height
= MCU_rows
* compptr
->v_samp_factor
;
518 x_crop_blocks
= x_crop_offset
* compptr
->h_samp_factor
;
519 y_crop_blocks
= y_crop_offset
* compptr
->v_samp_factor
;
520 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_blocks
;
521 dst_blk_y
+= compptr
->v_samp_factor
) {
522 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
523 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
524 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
525 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
526 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_blocks
;
527 dst_blk_x
+= compptr
->h_samp_factor
) {
528 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
529 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
530 dst_blk_x
+ x_crop_blocks
,
531 (JDIMENSION
) compptr
->h_samp_factor
, FALSE
);
532 for (offset_x
= 0; offset_x
< compptr
->h_samp_factor
; offset_x
++) {
533 dst_ptr
= dst_buffer
[offset_y
][dst_blk_x
+ offset_x
];
534 if (y_crop_blocks
+ dst_blk_y
< comp_height
) {
535 /* Block is within the mirrorable area. */
536 src_ptr
= src_buffer
[offset_x
]
537 [comp_height
- y_crop_blocks
- dst_blk_y
- offset_y
- 1];
538 for (i
= 0; i
< DCTSIZE
; i
++) {
539 for (j
= 0; j
< DCTSIZE
; j
++) {
540 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
542 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
546 /* Edge blocks are transposed but not mirrored. */
547 src_ptr
= src_buffer
[offset_x
]
548 [dst_blk_y
+ offset_y
+ y_crop_blocks
];
549 for (i
= 0; i
< DCTSIZE
; i
++)
550 for (j
= 0; j
< DCTSIZE
; j
++)
551 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
562 do_rot_180 (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
563 JDIMENSION x_crop_offset
, JDIMENSION y_crop_offset
,
564 jvirt_barray_ptr
*src_coef_arrays
,
565 jvirt_barray_ptr
*dst_coef_arrays
)
566 /* 180 degree rotation is equivalent to
567 * 1. Vertical mirroring;
568 * 2. Horizontal mirroring.
569 * These two steps are merged into a single processing routine.
572 JDIMENSION MCU_cols
, MCU_rows
, comp_width
, comp_height
, dst_blk_x
, dst_blk_y
;
573 JDIMENSION x_crop_blocks
, y_crop_blocks
;
574 int ci
, i
, j
, offset_y
;
575 JBLOCKARRAY src_buffer
, dst_buffer
;
576 JBLOCKROW src_row_ptr
, dst_row_ptr
;
577 JCOEFPTR src_ptr
, dst_ptr
;
578 jpeg_component_info
*compptr
;
580 MCU_cols
= srcinfo
->output_width
/
581 (dstinfo
->max_h_samp_factor
* dstinfo_min_DCT_h_scaled_size
);
582 MCU_rows
= srcinfo
->output_height
/
583 (dstinfo
->max_v_samp_factor
* dstinfo_min_DCT_v_scaled_size
);
585 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
586 compptr
= dstinfo
->comp_info
+ ci
;
587 comp_width
= MCU_cols
* compptr
->h_samp_factor
;
588 comp_height
= MCU_rows
* compptr
->v_samp_factor
;
589 x_crop_blocks
= x_crop_offset
* compptr
->h_samp_factor
;
590 y_crop_blocks
= y_crop_offset
* compptr
->v_samp_factor
;
591 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_blocks
;
592 dst_blk_y
+= compptr
->v_samp_factor
) {
593 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
594 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
595 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
596 if (y_crop_blocks
+ dst_blk_y
< comp_height
) {
597 /* Row is within the vertically mirrorable area. */
598 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
599 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
600 comp_height
- y_crop_blocks
- dst_blk_y
-
601 (JDIMENSION
) compptr
->v_samp_factor
,
602 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
604 /* Bottom-edge rows are only mirrored horizontally. */
605 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
606 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
607 dst_blk_y
+ y_crop_blocks
,
608 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
610 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
611 dst_row_ptr
= dst_buffer
[offset_y
];
612 if (y_crop_blocks
+ dst_blk_y
< comp_height
) {
613 /* Row is within the mirrorable area. */
614 src_row_ptr
= src_buffer
[compptr
->v_samp_factor
- offset_y
- 1];
615 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_blocks
; dst_blk_x
++) {
616 dst_ptr
= dst_row_ptr
[dst_blk_x
];
617 if (x_crop_blocks
+ dst_blk_x
< comp_width
) {
618 /* Process the blocks that can be mirrored both ways. */
619 src_ptr
= src_row_ptr
[comp_width
- x_crop_blocks
- dst_blk_x
- 1];
620 for (i
= 0; i
< DCTSIZE
; i
+= 2) {
621 /* For even row, negate every odd column. */
622 for (j
= 0; j
< DCTSIZE
; j
+= 2) {
623 *dst_ptr
++ = *src_ptr
++;
624 *dst_ptr
++ = - *src_ptr
++;
626 /* For odd row, negate every even column. */
627 for (j
= 0; j
< DCTSIZE
; j
+= 2) {
628 *dst_ptr
++ = - *src_ptr
++;
629 *dst_ptr
++ = *src_ptr
++;
633 /* Any remaining right-edge blocks are only mirrored vertically. */
634 src_ptr
= src_row_ptr
[x_crop_blocks
+ dst_blk_x
];
635 for (i
= 0; i
< DCTSIZE
; i
+= 2) {
636 for (j
= 0; j
< DCTSIZE
; j
++)
637 *dst_ptr
++ = *src_ptr
++;
638 for (j
= 0; j
< DCTSIZE
; j
++)
639 *dst_ptr
++ = - *src_ptr
++;
644 /* Remaining rows are just mirrored horizontally. */
645 src_row_ptr
= src_buffer
[offset_y
];
646 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_blocks
; dst_blk_x
++) {
647 if (x_crop_blocks
+ dst_blk_x
< comp_width
) {
648 /* Process the blocks that can be mirrored. */
649 dst_ptr
= dst_row_ptr
[dst_blk_x
];
650 src_ptr
= src_row_ptr
[comp_width
- x_crop_blocks
- dst_blk_x
- 1];
651 for (i
= 0; i
< DCTSIZE2
; i
+= 2) {
652 *dst_ptr
++ = *src_ptr
++;
653 *dst_ptr
++ = - *src_ptr
++;
656 /* Any remaining right-edge blocks are only copied. */
657 lcl_jcopy_block_row(src_row_ptr
+ dst_blk_x
+ x_crop_blocks
,
658 dst_row_ptr
+ dst_blk_x
,
670 do_transverse (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
671 JDIMENSION x_crop_offset
, JDIMENSION y_crop_offset
,
672 jvirt_barray_ptr
*src_coef_arrays
,
673 jvirt_barray_ptr
*dst_coef_arrays
)
674 /* Transverse transpose is equivalent to
675 * 1. 180 degree rotation;
678 * 1. Horizontal mirroring;
680 * 3. Horizontal mirroring.
681 * These steps are merged into a single processing routine.
684 JDIMENSION MCU_cols
, MCU_rows
, comp_width
, comp_height
, dst_blk_x
, dst_blk_y
;
685 JDIMENSION x_crop_blocks
, y_crop_blocks
;
686 int ci
, i
, j
, offset_x
, offset_y
;
687 JBLOCKARRAY src_buffer
, dst_buffer
;
688 JCOEFPTR src_ptr
, dst_ptr
;
689 jpeg_component_info
*compptr
;
691 MCU_cols
= srcinfo
->output_height
/
692 (dstinfo
->max_h_samp_factor
* dstinfo_min_DCT_h_scaled_size
);
693 MCU_rows
= srcinfo
->output_width
/
694 (dstinfo
->max_v_samp_factor
* dstinfo_min_DCT_v_scaled_size
);
696 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
697 compptr
= dstinfo
->comp_info
+ ci
;
698 comp_width
= MCU_cols
* compptr
->h_samp_factor
;
699 comp_height
= MCU_rows
* compptr
->v_samp_factor
;
700 x_crop_blocks
= x_crop_offset
* compptr
->h_samp_factor
;
701 y_crop_blocks
= y_crop_offset
* compptr
->v_samp_factor
;
702 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_blocks
;
703 dst_blk_y
+= compptr
->v_samp_factor
) {
704 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
705 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
706 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
707 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
708 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_blocks
;
709 dst_blk_x
+= compptr
->h_samp_factor
) {
710 if (x_crop_blocks
+ dst_blk_x
< comp_width
) {
711 /* Block is within the mirrorable area. */
712 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
713 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
714 comp_width
- x_crop_blocks
- dst_blk_x
-
715 (JDIMENSION
) compptr
->h_samp_factor
,
716 (JDIMENSION
) compptr
->h_samp_factor
, FALSE
);
718 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
719 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
720 dst_blk_x
+ x_crop_blocks
,
721 (JDIMENSION
) compptr
->h_samp_factor
, FALSE
);
723 for (offset_x
= 0; offset_x
< compptr
->h_samp_factor
; offset_x
++) {
724 dst_ptr
= dst_buffer
[offset_y
][dst_blk_x
+ offset_x
];
725 if (y_crop_blocks
+ dst_blk_y
< comp_height
) {
726 if (x_crop_blocks
+ dst_blk_x
< comp_width
) {
727 /* Block is within the mirrorable area. */
728 src_ptr
= src_buffer
[compptr
->h_samp_factor
- offset_x
- 1]
729 [comp_height
- y_crop_blocks
- dst_blk_y
- offset_y
- 1];
730 for (i
= 0; i
< DCTSIZE
; i
++) {
731 for (j
= 0; j
< DCTSIZE
; j
++) {
732 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
734 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
737 for (j
= 0; j
< DCTSIZE
; j
++) {
738 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
740 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
744 /* Right-edge blocks are mirrored in y only */
745 src_ptr
= src_buffer
[offset_x
]
746 [comp_height
- y_crop_blocks
- dst_blk_y
- offset_y
- 1];
747 for (i
= 0; i
< DCTSIZE
; i
++) {
748 for (j
= 0; j
< DCTSIZE
; j
++) {
749 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
751 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
756 if (x_crop_blocks
+ dst_blk_x
< comp_width
) {
757 /* Bottom-edge blocks are mirrored in x only */
758 src_ptr
= src_buffer
[compptr
->h_samp_factor
- offset_x
- 1]
759 [dst_blk_y
+ offset_y
+ y_crop_blocks
];
760 for (i
= 0; i
< DCTSIZE
; i
++) {
761 for (j
= 0; j
< DCTSIZE
; j
++)
762 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
764 for (j
= 0; j
< DCTSIZE
; j
++)
765 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
768 /* At lower right corner, just transpose, no mirroring */
769 src_ptr
= src_buffer
[offset_x
]
770 [dst_blk_y
+ offset_y
+ y_crop_blocks
];
771 for (i
= 0; i
< DCTSIZE
; i
++)
772 for (j
= 0; j
< DCTSIZE
; j
++)
773 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
784 /* Trim off any partial iMCUs on the indicated destination edge */
787 trim_right_edge (jpeg_transform_info
*info
, JDIMENSION full_width
)
791 MCU_cols
= info
->output_width
/ info
->iMCU_sample_width
;
792 if (MCU_cols
> 0 && info
->x_crop_offset
+ MCU_cols
==
793 full_width
/ info
->iMCU_sample_width
)
794 info
->output_width
= MCU_cols
* info
->iMCU_sample_width
;
798 trim_bottom_edge (jpeg_transform_info
*info
, JDIMENSION full_height
)
802 MCU_rows
= info
->output_height
/ info
->iMCU_sample_height
;
803 if (MCU_rows
> 0 && info
->y_crop_offset
+ MCU_rows
==
804 full_height
/ info
->iMCU_sample_height
)
805 info
->output_height
= MCU_rows
* info
->iMCU_sample_height
;
809 /* Request any required workspace.
811 * This routine figures out the size that the output image will be
812 * (which implies that all the transform parameters must be set before
815 * We allocate the workspace virtual arrays from the source decompression
816 * object, so that all the arrays (both the original data and the workspace)
817 * will be taken into account while making memory management decisions.
818 * Hence, this routine must be called after jpeg_read_header (which reads
819 * the image dimensions) and before jpeg_read_coefficients (which realizes
820 * the source's virtual arrays).
822 * This function returns FALSE right away if -perfect is given
823 * and transformation is not perfect. Otherwise returns TRUE.
827 jtransform_request_workspace (j_decompress_ptr srcinfo
,
828 jpeg_transform_info
*info
)
830 jvirt_barray_ptr
*coef_arrays
;
831 boolean need_workspace
, transpose_it
;
832 jpeg_component_info
*compptr
;
833 JDIMENSION xoffset
, yoffset
;
834 JDIMENSION width_in_iMCUs
, height_in_iMCUs
;
835 JDIMENSION width_in_blocks
, height_in_blocks
;
836 int ci
, h_samp_factor
, v_samp_factor
;
838 /* Determine number of components in output image */
839 if (info
->force_grayscale
&&
840 srcinfo
->jpeg_color_space
== JCS_YCbCr
&&
841 srcinfo
->num_components
== 3)
842 /* We'll only process the first component */
843 info
->num_components
= 1;
845 /* Process all the components */
846 info
->num_components
= srcinfo
->num_components
;
848 /* Compute output image dimensions and related values. */
849 #if JPEG_LIB_VERSION >= 80
850 jpeg_core_output_dimensions(srcinfo
);
852 srcinfo
->output_width
= srcinfo
->image_width
;
853 srcinfo
->output_height
= srcinfo
->image_height
;
856 /* Return right away if -perfect is given and transformation is not perfect.
859 if (info
->num_components
== 1) {
860 if (!jtransform_perfect_transform(srcinfo
->output_width
,
861 srcinfo
->output_height
,
862 srcinfo
->min_DCT_h_scaled_size_
,
863 srcinfo
->min_DCT_v_scaled_size_
,
867 if (!jtransform_perfect_transform(srcinfo
->output_width
,
868 srcinfo
->output_height
,
869 srcinfo
->max_h_samp_factor
* srcinfo
->min_DCT_h_scaled_size_
,
870 srcinfo
->max_v_samp_factor
* srcinfo
->min_DCT_v_scaled_size_
,
876 /* If there is only one output component, force the iMCU size to be 1;
877 * else use the source iMCU size. (This allows us to do the right thing
878 * when reducing color to grayscale, and also provides a handy way of
879 * cleaning up "funny" grayscale images whose sampling factors are not 1x1.)
881 switch (info
->transform
) {
882 case JXFORM_TRANSPOSE
:
883 case JXFORM_TRANSVERSE
:
886 info
->output_width
= srcinfo
->output_height
;
887 info
->output_height
= srcinfo
->output_width
;
888 if (info
->num_components
== 1) {
889 info
->iMCU_sample_width
= srcinfo
->min_DCT_v_scaled_size_
;
890 info
->iMCU_sample_height
= srcinfo
->min_DCT_h_scaled_size_
;
892 info
->iMCU_sample_width
=
893 srcinfo
->max_v_samp_factor
* srcinfo
->min_DCT_v_scaled_size_
;
894 info
->iMCU_sample_height
=
895 srcinfo
->max_h_samp_factor
* srcinfo
->min_DCT_h_scaled_size_
;
899 info
->output_width
= srcinfo
->output_width
;
900 info
->output_height
= srcinfo
->output_height
;
901 if (info
->num_components
== 1) {
902 info
->iMCU_sample_width
= srcinfo
->min_DCT_h_scaled_size_
;
903 info
->iMCU_sample_height
= srcinfo
->min_DCT_v_scaled_size_
;
905 info
->iMCU_sample_width
=
906 srcinfo
->max_h_samp_factor
* srcinfo
->min_DCT_h_scaled_size_
;
907 info
->iMCU_sample_height
=
908 srcinfo
->max_v_samp_factor
* srcinfo
->min_DCT_v_scaled_size_
;
913 /* If cropping has been requested, compute the crop area's position and
914 * dimensions, ensuring that its upper left corner falls at an iMCU boundary.
917 /* Insert default values for unset crop parameters */
918 if (info
->crop_xoffset_set
== JCROP_UNSET
)
919 info
->crop_xoffset
= 0; /* default to +0 */
920 if (info
->crop_yoffset_set
== JCROP_UNSET
)
921 info
->crop_yoffset
= 0; /* default to +0 */
922 if (info
->crop_xoffset
>= info
->output_width
||
923 info
->crop_yoffset
>= info
->output_height
)
924 ERREXIT(srcinfo
, JERR_CONVERSION_NOTIMPL
);
925 if (info
->crop_width_set
== JCROP_UNSET
)
926 info
->crop_width
= info
->output_width
- info
->crop_xoffset
;
927 if (info
->crop_height_set
== JCROP_UNSET
)
928 info
->crop_height
= info
->output_height
- info
->crop_yoffset
;
929 /* Ensure parameters are valid */
930 if (info
->crop_width
<= 0 || info
->crop_width
> info
->output_width
||
931 info
->crop_height
<= 0 || info
->crop_height
> info
->output_height
||
932 info
->crop_xoffset
> info
->output_width
- info
->crop_width
||
933 info
->crop_yoffset
> info
->output_height
- info
->crop_height
)
934 ERREXIT(srcinfo
, JERR_CONVERSION_NOTIMPL
);
935 /* Convert negative crop offsets into regular offsets */
936 if (info
->crop_xoffset_set
== JCROP_NEG
)
937 xoffset
= info
->output_width
- info
->crop_width
- info
->crop_xoffset
;
939 xoffset
= info
->crop_xoffset
;
940 if (info
->crop_yoffset_set
== JCROP_NEG
)
941 yoffset
= info
->output_height
- info
->crop_height
- info
->crop_yoffset
;
943 yoffset
= info
->crop_yoffset
;
944 /* Now adjust so that upper left corner falls at an iMCU boundary */
945 if (info
->crop_width_set
== JCROP_FORCE
)
946 info
->output_width
= info
->crop_width
;
949 info
->crop_width
+ (xoffset
% info
->iMCU_sample_width
);
950 if (info
->crop_height_set
== JCROP_FORCE
)
951 info
->output_height
= info
->crop_height
;
953 info
->output_height
=
954 info
->crop_height
+ (yoffset
% info
->iMCU_sample_height
);
955 /* Save x/y offsets measured in iMCUs */
956 info
->x_crop_offset
= xoffset
/ info
->iMCU_sample_width
;
957 info
->y_crop_offset
= yoffset
/ info
->iMCU_sample_height
;
959 info
->x_crop_offset
= 0;
960 info
->y_crop_offset
= 0;
963 /* Figure out whether we need workspace arrays,
964 * and if so whether they are transposed relative to the source.
966 need_workspace
= FALSE
;
967 transpose_it
= FALSE
;
968 switch (info
->transform
) {
970 if (info
->x_crop_offset
!= 0 || info
->y_crop_offset
!= 0)
971 need_workspace
= TRUE
;
972 /* No workspace needed if neither cropping nor transforming */
976 trim_right_edge(info
, srcinfo
->output_width
);
977 if (info
->y_crop_offset
!= 0 || info
->slow_hflip
)
978 need_workspace
= TRUE
;
979 /* do_flip_h_no_crop doesn't need a workspace array */
983 trim_bottom_edge(info
, srcinfo
->output_height
);
984 /* Need workspace arrays having same dimensions as source image. */
985 need_workspace
= TRUE
;
987 case JXFORM_TRANSPOSE
:
988 /* transpose does NOT have to trim anything */
989 /* Need workspace arrays having transposed dimensions. */
990 need_workspace
= TRUE
;
993 case JXFORM_TRANSVERSE
:
995 trim_right_edge(info
, srcinfo
->output_height
);
996 trim_bottom_edge(info
, srcinfo
->output_width
);
998 /* Need workspace arrays having transposed dimensions. */
999 need_workspace
= TRUE
;
1000 transpose_it
= TRUE
;
1004 trim_right_edge(info
, srcinfo
->output_height
);
1005 /* Need workspace arrays having transposed dimensions. */
1006 need_workspace
= TRUE
;
1007 transpose_it
= TRUE
;
1009 case JXFORM_ROT_180
:
1011 trim_right_edge(info
, srcinfo
->output_width
);
1012 trim_bottom_edge(info
, srcinfo
->output_height
);
1014 /* Need workspace arrays having same dimensions as source image. */
1015 need_workspace
= TRUE
;
1017 case JXFORM_ROT_270
:
1019 trim_bottom_edge(info
, srcinfo
->output_width
);
1020 /* Need workspace arrays having transposed dimensions. */
1021 need_workspace
= TRUE
;
1022 transpose_it
= TRUE
;
1026 /* Allocate workspace if needed.
1027 * Note that we allocate arrays padded out to the next iMCU boundary,
1028 * so that transform routines need not worry about missing edge blocks.
1030 if (need_workspace
) {
1031 coef_arrays
= (jvirt_barray_ptr
*)
1032 (*srcinfo
->mem
->alloc_small
) ((j_common_ptr
) srcinfo
, JPOOL_IMAGE
,
1033 SIZEOF(jvirt_barray_ptr
) * info
->num_components
);
1034 width_in_iMCUs
= (JDIMENSION
)
1035 jdiv_round_up((long) info
->output_width
,
1036 (long) info
->iMCU_sample_width
);
1037 height_in_iMCUs
= (JDIMENSION
)
1038 jdiv_round_up((long) info
->output_height
,
1039 (long) info
->iMCU_sample_height
);
1040 for (ci
= 0; ci
< info
->num_components
; ci
++) {
1041 compptr
= srcinfo
->comp_info
+ ci
;
1042 if (info
->num_components
== 1) {
1043 /* we're going to force samp factors to 1x1 in this case */
1044 h_samp_factor
= v_samp_factor
= 1;
1045 } else if (transpose_it
) {
1046 h_samp_factor
= compptr
->v_samp_factor
;
1047 v_samp_factor
= compptr
->h_samp_factor
;
1049 h_samp_factor
= compptr
->h_samp_factor
;
1050 v_samp_factor
= compptr
->v_samp_factor
;
1052 width_in_blocks
= width_in_iMCUs
* h_samp_factor
;
1053 height_in_blocks
= height_in_iMCUs
* v_samp_factor
;
1054 coef_arrays
[ci
] = (*srcinfo
->mem
->request_virt_barray
)
1055 ((j_common_ptr
) srcinfo
, JPOOL_IMAGE
, FALSE
,
1056 width_in_blocks
, height_in_blocks
, (JDIMENSION
) v_samp_factor
);
1058 info
->workspace_coef_arrays
= coef_arrays
;
1060 info
->workspace_coef_arrays
= NULL
;
1066 /* Transpose destination image parameters */
1069 transpose_critical_parameters (j_compress_ptr dstinfo
)
1071 int tblno
, i
, j
, ci
, itemp
;
1072 jpeg_component_info
*compptr
;
1073 JQUANT_TBL
*qtblptr
;
1077 /* Transpose image dimensions */
1078 jtemp
= dstinfo
->image_width
;
1079 dstinfo
->image_width
= dstinfo
->image_height
;
1080 dstinfo
->image_height
= jtemp
;
1081 #if JPEG_LIB_VERSION >= 70
1082 itemp
= dstinfo
->min_DCT_h_scaled_size
;
1083 dstinfo
->min_DCT_h_scaled_size
= dstinfo
->min_DCT_v_scaled_size
;
1084 dstinfo
->min_DCT_v_scaled_size
= itemp
;
1087 /* Transpose sampling factors */
1088 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
1089 compptr
= dstinfo
->comp_info
+ ci
;
1090 itemp
= compptr
->h_samp_factor
;
1091 compptr
->h_samp_factor
= compptr
->v_samp_factor
;
1092 compptr
->v_samp_factor
= itemp
;
1095 /* Transpose quantization tables */
1096 for (tblno
= 0; tblno
< NUM_QUANT_TBLS
; tblno
++) {
1097 qtblptr
= dstinfo
->quant_tbl_ptrs
[tblno
];
1098 if (qtblptr
!= NULL
) {
1099 for (i
= 0; i
< DCTSIZE
; i
++) {
1100 for (j
= 0; j
< i
; j
++) {
1101 qtemp
= qtblptr
->quantval
[i
*DCTSIZE
+j
];
1102 qtblptr
->quantval
[i
*DCTSIZE
+j
] = qtblptr
->quantval
[j
*DCTSIZE
+i
];
1103 qtblptr
->quantval
[j
*DCTSIZE
+i
] = qtemp
;
1111 /* Adjust Exif image parameters.
1113 * We try to adjust the Tags ExifImageWidth and ExifImageHeight if possible.
1116 #if JPEG_LIB_VERSION >= 70
1118 adjust_exif_parameters (JOCTET FAR
* data
, unsigned int length
,
1119 JDIMENSION new_width
, JDIMENSION new_height
)
1121 boolean is_motorola
; /* Flag for byte order */
1122 unsigned int number_of_tags
, tagnum
;
1123 unsigned int firstoffset
, offset
;
1124 JDIMENSION new_value
;
1126 if (length
< 12) return; /* Length of an IFD entry */
1128 /* Discover byte order */
1129 if (GETJOCTET(data
[0]) == 0x49 && GETJOCTET(data
[1]) == 0x49)
1130 is_motorola
= FALSE
;
1131 else if (GETJOCTET(data
[0]) == 0x4D && GETJOCTET(data
[1]) == 0x4D)
1136 /* Check Tag Mark */
1138 if (GETJOCTET(data
[2]) != 0) return;
1139 if (GETJOCTET(data
[3]) != 0x2A) return;
1141 if (GETJOCTET(data
[3]) != 0) return;
1142 if (GETJOCTET(data
[2]) != 0x2A) return;
1145 /* Get first IFD offset (offset to IFD0) */
1147 if (GETJOCTET(data
[4]) != 0) return;
1148 if (GETJOCTET(data
[5]) != 0) return;
1149 firstoffset
= GETJOCTET(data
[6]);
1151 firstoffset
+= GETJOCTET(data
[7]);
1153 if (GETJOCTET(data
[7]) != 0) return;
1154 if (GETJOCTET(data
[6]) != 0) return;
1155 firstoffset
= GETJOCTET(data
[5]);
1157 firstoffset
+= GETJOCTET(data
[4]);
1159 if (firstoffset
> length
- 2) return; /* check end of data segment */
1161 /* Get the number of directory entries contained in this IFD */
1163 number_of_tags
= GETJOCTET(data
[firstoffset
]);
1164 number_of_tags
<<= 8;
1165 number_of_tags
+= GETJOCTET(data
[firstoffset
+1]);
1167 number_of_tags
= GETJOCTET(data
[firstoffset
+1]);
1168 number_of_tags
<<= 8;
1169 number_of_tags
+= GETJOCTET(data
[firstoffset
]);
1171 if (number_of_tags
== 0) return;
1174 /* Search for ExifSubIFD offset Tag in IFD0 */
1176 if (firstoffset
> length
- 12) return; /* check end of data segment */
1177 /* Get Tag number */
1179 tagnum
= GETJOCTET(data
[firstoffset
]);
1181 tagnum
+= GETJOCTET(data
[firstoffset
+1]);
1183 tagnum
= GETJOCTET(data
[firstoffset
+1]);
1185 tagnum
+= GETJOCTET(data
[firstoffset
]);
1187 if (tagnum
== 0x8769) break; /* found ExifSubIFD offset Tag */
1188 if (--number_of_tags
== 0) return;
1192 /* Get the ExifSubIFD offset */
1194 if (GETJOCTET(data
[firstoffset
+8]) != 0) return;
1195 if (GETJOCTET(data
[firstoffset
+9]) != 0) return;
1196 offset
= GETJOCTET(data
[firstoffset
+10]);
1198 offset
+= GETJOCTET(data
[firstoffset
+11]);
1200 if (GETJOCTET(data
[firstoffset
+11]) != 0) return;
1201 if (GETJOCTET(data
[firstoffset
+10]) != 0) return;
1202 offset
= GETJOCTET(data
[firstoffset
+9]);
1204 offset
+= GETJOCTET(data
[firstoffset
+8]);
1206 if (offset
> length
- 2) return; /* check end of data segment */
1208 /* Get the number of directory entries contained in this SubIFD */
1210 number_of_tags
= GETJOCTET(data
[offset
]);
1211 number_of_tags
<<= 8;
1212 number_of_tags
+= GETJOCTET(data
[offset
+1]);
1214 number_of_tags
= GETJOCTET(data
[offset
+1]);
1215 number_of_tags
<<= 8;
1216 number_of_tags
+= GETJOCTET(data
[offset
]);
1218 if (number_of_tags
< 2) return;
1221 /* Search for ExifImageWidth and ExifImageHeight Tags in this SubIFD */
1223 if (offset
> length
- 12) return; /* check end of data segment */
1224 /* Get Tag number */
1226 tagnum
= GETJOCTET(data
[offset
]);
1228 tagnum
+= GETJOCTET(data
[offset
+1]);
1230 tagnum
= GETJOCTET(data
[offset
+1]);
1232 tagnum
+= GETJOCTET(data
[offset
]);
1234 if (tagnum
== 0xA002 || tagnum
== 0xA003) {
1235 if (tagnum
== 0xA002)
1236 new_value
= new_width
; /* ExifImageWidth Tag */
1238 new_value
= new_height
; /* ExifImageHeight Tag */
1240 data
[offset
+2] = 0; /* Format = unsigned long (4 octets) */
1242 data
[offset
+4] = 0; /* Number Of Components = 1 */
1248 data
[offset
+10] = (JOCTET
)((new_value
>> 8) & 0xFF);
1249 data
[offset
+11] = (JOCTET
)(new_value
& 0xFF);
1251 data
[offset
+2] = 4; /* Format = unsigned long (4 octets) */
1253 data
[offset
+4] = 1; /* Number Of Components = 1 */
1257 data
[offset
+8] = (JOCTET
)(new_value
& 0xFF);
1258 data
[offset
+9] = (JOCTET
)((new_value
>> 8) & 0xFF);
1259 data
[offset
+10] = 0;
1260 data
[offset
+11] = 0;
1264 } while (--number_of_tags
);
1269 /* Adjust output image parameters as needed.
1271 * This must be called after jpeg_copy_critical_parameters()
1272 * and before jpeg_write_coefficients().
1274 * The return value is the set of virtual coefficient arrays to be written
1275 * (either the ones allocated by jtransform_request_workspace, or the
1276 * original source data arrays). The caller will need to pass this value
1277 * to jpeg_write_coefficients().
1280 GLOBAL(jvirt_barray_ptr
*)
1281 jtransform_adjust_parameters (j_decompress_ptr srcinfo
,
1282 j_compress_ptr dstinfo
,
1283 jvirt_barray_ptr
*src_coef_arrays
,
1284 jpeg_transform_info
*info
)
1286 /* If force-to-grayscale is requested, adjust destination parameters */
1287 if (info
->force_grayscale
) {
1288 /* First, ensure we have YCbCr or grayscale data, and that the source's
1289 * Y channel is full resolution. (No reasonable person would make Y
1290 * be less than full resolution, so actually coping with that case
1291 * isn't worth extra code space. But we check it to avoid crashing.)
1293 if (((dstinfo
->jpeg_color_space
== JCS_YCbCr
&&
1294 dstinfo
->num_components
== 3) ||
1295 (dstinfo
->jpeg_color_space
== JCS_GRAYSCALE
&&
1296 dstinfo
->num_components
== 1)) &&
1297 srcinfo
->comp_info
[0].h_samp_factor
== srcinfo
->max_h_samp_factor
&&
1298 srcinfo
->comp_info
[0].v_samp_factor
== srcinfo
->max_v_samp_factor
) {
1299 /* We use jpeg_set_colorspace to make sure subsidiary settings get fixed
1300 * properly. Among other things, it sets the target h_samp_factor &
1301 * v_samp_factor to 1, which typically won't match the source.
1302 * We have to preserve the source's quantization table number, however.
1304 int sv_quant_tbl_no
= dstinfo
->comp_info
[0].quant_tbl_no
;
1305 jpeg_set_colorspace(dstinfo
, JCS_GRAYSCALE
);
1306 dstinfo
->comp_info
[0].quant_tbl_no
= sv_quant_tbl_no
;
1308 /* Sorry, can't do it */
1309 ERREXIT(dstinfo
, JERR_CONVERSION_NOTIMPL
);
1311 } else if (info
->num_components
== 1) {
1312 /* For a single-component source, we force the destination sampling factors
1313 * to 1x1, with or without force_grayscale. This is useful because some
1314 * decoders choke on grayscale images with other sampling factors.
1316 dstinfo
->comp_info
[0].h_samp_factor
= 1;
1317 dstinfo
->comp_info
[0].v_samp_factor
= 1;
1320 /* Correct the destination's image dimensions as necessary
1321 * for rotate/flip, resize, and crop operations.
1323 #if JPEG_LIB_VERSION >= 70
1324 dstinfo
->jpeg_width
= info
->output_width
;
1325 dstinfo
->jpeg_height
= info
->output_height
;
1328 /* Transpose destination image parameters */
1329 switch (info
->transform
) {
1330 case JXFORM_TRANSPOSE
:
1331 case JXFORM_TRANSVERSE
:
1333 case JXFORM_ROT_270
:
1334 #if JPEG_LIB_VERSION < 70
1335 dstinfo
->image_width
= info
->output_height
;
1336 dstinfo
->image_height
= info
->output_width
;
1338 transpose_critical_parameters(dstinfo
);
1341 #if JPEG_LIB_VERSION < 70
1342 dstinfo
->image_width
= info
->output_width
;
1343 dstinfo
->image_height
= info
->output_height
;
1348 /* Adjust Exif properties */
1349 if (srcinfo
->marker_list
!= NULL
&&
1350 srcinfo
->marker_list
->marker
== JPEG_APP0
+1 &&
1351 srcinfo
->marker_list
->data_length
>= 6 &&
1352 GETJOCTET(srcinfo
->marker_list
->data
[0]) == 0x45 &&
1353 GETJOCTET(srcinfo
->marker_list
->data
[1]) == 0x78 &&
1354 GETJOCTET(srcinfo
->marker_list
->data
[2]) == 0x69 &&
1355 GETJOCTET(srcinfo
->marker_list
->data
[3]) == 0x66 &&
1356 GETJOCTET(srcinfo
->marker_list
->data
[4]) == 0 &&
1357 GETJOCTET(srcinfo
->marker_list
->data
[5]) == 0) {
1358 /* Suppress output of JFIF marker */
1359 dstinfo
->write_JFIF_header
= FALSE
;
1360 #if JPEG_LIB_VERSION >= 70
1361 /* Adjust Exif image parameters */
1362 if (dstinfo
->jpeg_width
!= srcinfo
->image_width
||
1363 dstinfo
->jpeg_height
!= srcinfo
->image_height
)
1364 /* Align data segment to start of TIFF structure for parsing */
1365 adjust_exif_parameters(srcinfo
->marker_list
->data
+ 6,
1366 srcinfo
->marker_list
->data_length
- 6,
1367 dstinfo
->jpeg_width
, dstinfo
->jpeg_height
);
1371 /* Return the appropriate output data set */
1372 if (info
->workspace_coef_arrays
!= NULL
)
1373 return info
->workspace_coef_arrays
;
1374 return src_coef_arrays
;
1378 /* Execute the actual transformation, if any.
1380 * This must be called *after* jpeg_write_coefficients, because it depends
1381 * on jpeg_write_coefficients to have computed subsidiary values such as
1382 * the per-component width and height fields in the destination object.
1384 * Note that some transformations will modify the source data arrays!
1388 jtransform_execute_transform (j_decompress_ptr srcinfo
,
1389 j_compress_ptr dstinfo
,
1390 jvirt_barray_ptr
*src_coef_arrays
,
1391 jpeg_transform_info
*info
)
1393 jvirt_barray_ptr
*dst_coef_arrays
= info
->workspace_coef_arrays
;
1395 /* Note: conditions tested here should match those in switch statement
1396 * in jtransform_request_workspace()
1398 switch (info
->transform
) {
1400 if (info
->x_crop_offset
!= 0 || info
->y_crop_offset
!= 0)
1401 do_crop(srcinfo
, dstinfo
, info
->x_crop_offset
, info
->y_crop_offset
,
1402 src_coef_arrays
, dst_coef_arrays
);
1405 if (info
->y_crop_offset
!= 0 || info
->slow_hflip
)
1406 do_flip_h(srcinfo
, dstinfo
, info
->x_crop_offset
, info
->y_crop_offset
,
1407 src_coef_arrays
, dst_coef_arrays
);
1409 do_flip_h_no_crop(srcinfo
, dstinfo
, info
->x_crop_offset
,
1413 do_flip_v(srcinfo
, dstinfo
, info
->x_crop_offset
, info
->y_crop_offset
,
1414 src_coef_arrays
, dst_coef_arrays
);
1416 case JXFORM_TRANSPOSE
:
1417 do_transpose(srcinfo
, dstinfo
, info
->x_crop_offset
, info
->y_crop_offset
,
1418 src_coef_arrays
, dst_coef_arrays
);
1420 case JXFORM_TRANSVERSE
:
1421 do_transverse(srcinfo
, dstinfo
, info
->x_crop_offset
, info
->y_crop_offset
,
1422 src_coef_arrays
, dst_coef_arrays
);
1425 do_rot_90(srcinfo
, dstinfo
, info
->x_crop_offset
, info
->y_crop_offset
,
1426 src_coef_arrays
, dst_coef_arrays
);
1428 case JXFORM_ROT_180
:
1429 do_rot_180(srcinfo
, dstinfo
, info
->x_crop_offset
, info
->y_crop_offset
,
1430 src_coef_arrays
, dst_coef_arrays
);
1432 case JXFORM_ROT_270
:
1433 do_rot_270(srcinfo
, dstinfo
, info
->x_crop_offset
, info
->y_crop_offset
,
1434 src_coef_arrays
, dst_coef_arrays
);
1439 /* jtransform_perfect_transform
1441 * Determine whether lossless transformation is perfectly
1442 * possible for a specified image and transformation.
1445 * image_width, image_height: source image dimensions.
1446 * MCU_width, MCU_height: pixel dimensions of MCU.
1447 * transform: transformation identifier.
1448 * Parameter sources from initialized jpeg_struct
1449 * (after reading source header):
1450 * image_width = cinfo.image_width
1451 * image_height = cinfo.image_height
1452 * MCU_width = cinfo.max_h_samp_factor * cinfo.block_size
1453 * MCU_height = cinfo.max_v_samp_factor * cinfo.block_size
1455 * TRUE = perfect transformation possible
1456 * FALSE = perfect transformation not possible
1457 * (may use custom action then)
1461 jtransform_perfect_transform(JDIMENSION image_width
, JDIMENSION image_height
,
1462 int MCU_width
, int MCU_height
,
1463 JXFORM_CODE transform
)
1465 boolean result
= TRUE
; /* initialize TRUE */
1467 switch (transform
) {
1469 case JXFORM_ROT_270
:
1470 if (image_width
% (JDIMENSION
) MCU_width
)
1475 if (image_height
% (JDIMENSION
) MCU_height
)
1478 case JXFORM_TRANSVERSE
:
1479 case JXFORM_ROT_180
:
1480 if (image_width
% (JDIMENSION
) MCU_width
)
1482 if (image_height
% (JDIMENSION
) MCU_height
)
1492 #endif /* TRANSFORMS_SUPPORTED */
1495 /* Setup decompression object to save desired markers in memory.
1496 * This must be called before jpeg_read_header() to have the desired effect.
1500 jcopy_markers_setup (j_decompress_ptr srcinfo
, JCOPY_OPTION option
)
1502 #ifdef SAVE_MARKERS_SUPPORTED
1505 /* Save comments except under NONE option */
1506 if (option
!= JCOPYOPT_NONE
) {
1507 jpeg_save_markers(srcinfo
, JPEG_COM
, 0xFFFF);
1509 /* Save all types of APPn markers iff ALL option */
1510 if (option
== JCOPYOPT_ALL
) {
1511 for (m
= 0; m
< 16; m
++)
1512 jpeg_save_markers(srcinfo
, JPEG_APP0
+ m
, 0xFFFF);
1515 (void) srcinfo
; (void) option
;
1516 #endif /* SAVE_MARKERS_SUPPORTED */
1519 /* Copy markers saved in the given source object to the destination object.
1520 * This should be called just after jpeg_start_compress() or
1521 * jpeg_write_coefficients().
1522 * Note that those routines will have written the SOI, and also the
1523 * JFIF APP0 or Adobe APP14 markers if selected.
1527 jcopy_markers_execute (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
1528 JCOPY_OPTION option
)
1530 jpeg_saved_marker_ptr marker
;
1532 /* In the current implementation, we don't actually need to examine the
1533 * option flag here; we just copy everything that got saved.
1534 * But to avoid confusion, we do not output JFIF and Adobe APP14 markers
1535 * if the encoder library already wrote one.
1539 for (marker
= srcinfo
->marker_list
; marker
!= NULL
; marker
= marker
->next
) {
1540 if (dstinfo
->write_JFIF_header
&&
1541 marker
->marker
== JPEG_APP0
&&
1542 marker
->data_length
>= 5 &&
1543 GETJOCTET(marker
->data
[0]) == 0x4A &&
1544 GETJOCTET(marker
->data
[1]) == 0x46 &&
1545 GETJOCTET(marker
->data
[2]) == 0x49 &&
1546 GETJOCTET(marker
->data
[3]) == 0x46 &&
1547 GETJOCTET(marker
->data
[4]) == 0)
1548 continue; /* reject duplicate JFIF */
1549 if (dstinfo
->write_Adobe_marker
&&
1550 marker
->marker
== JPEG_APP0
+14 &&
1551 marker
->data_length
>= 5 &&
1552 GETJOCTET(marker
->data
[0]) == 0x41 &&
1553 GETJOCTET(marker
->data
[1]) == 0x64 &&
1554 GETJOCTET(marker
->data
[2]) == 0x6F &&
1555 GETJOCTET(marker
->data
[3]) == 0x62 &&
1556 GETJOCTET(marker
->data
[4]) == 0x65)
1557 continue; /* reject duplicate Adobe */
1558 #ifdef NEED_FAR_POINTERS
1559 /* We could use jpeg_write_marker if the data weren't FAR... */
1562 jpeg_write_m_header(dstinfo
, marker
->marker
, marker
->data_length
);
1563 for (i
= 0; i
< marker
->data_length
; i
++)
1564 jpeg_write_m_byte(dstinfo
, marker
->data
[i
]);
1567 jpeg_write_marker(dstinfo
, marker
->marker
,
1568 marker
->data
, marker
->data_length
);