8 * Copyright (C) 1997-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains image transformation routines and other utility code
13 * used by the jpegtran sample application. These are NOT part of the core
14 * JPEG library. But we keep these routines separate from jpegtran.c to
15 * ease the task of maintaining jpegtran-like programs that have other user
19 /* Although this file really shouldn't have access to the library internals,
20 * it's helpful to let it call jround_up() and jcopy_block_row().
22 #define JPEG_INTERNALS
26 #include "transupp.h" /* My own external interface */
29 #if TRANSFORMS_SUPPORTED
32 * Lossless image transformation routines. These routines work on DCT
33 * coefficient arrays and thus do not require any lossy decompression
34 * or recompression of the image.
35 * Thanks to Guido Vollbeding for the initial design and code of this feature.
37 * Horizontal flipping is done in-place, using a single top-to-bottom
38 * pass through the virtual source array. It will thus be much the
39 * fastest option for images larger than main memory.
41 * The other routines require a set of destination virtual arrays, so they
42 * need twice as much memory as jpegtran normally does. The destination
43 * arrays are always written in normal scan order (top to bottom) because
44 * the virtual array manager expects this. The source arrays will be scanned
45 * in the corresponding order, which means multiple passes through the source
46 * arrays for most of the transforms. That could result in much thrashing
47 * if the image is larger than main memory.
49 * Some notes about the operating environment of the individual transform
51 * 1. Both the source and destination virtual arrays are allocated from the
52 * source JPEG object, and therefore should be manipulated by calling the
53 * source's memory manager.
54 * 2. The destination's component count should be used. It may be smaller
55 * than the source's when forcing to grayscale.
56 * 3. Likewise the destination's sampling factors should be used. When
57 * forcing to grayscale the destination's sampling factors will be all 1,
58 * and we may as well take that as the effective iMCU size.
59 * 4. When "trim" is in effect, the destination's dimensions will be the
60 * trimmed values but the source's will be untrimmed.
61 * 5. All the routines assume that the source and destination buffers are
62 * padded out to a full iMCU boundary. This is true, although for the
63 * source buffer it is an undocumented property of jdcoefct.c.
64 * Notes 2,3,4 boil down to this: generally we should use the destination's
65 * dimensions and ignore the source's.
70 do_flip_h (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
71 jvirt_barray_ptr
*src_coef_arrays
)
72 /* Horizontal flip; done in-place, so no separate dest array is required */
74 JDIMENSION MCU_cols
, comp_width
, blk_x
, blk_y
;
79 jpeg_component_info
*compptr
;
81 /* Horizontal mirroring of DCT blocks is accomplished by swapping
82 * pairs of blocks in-place. Within a DCT block, we perform horizontal
83 * mirroring by changing the signs of odd-numbered columns.
84 * Partial iMCUs at the right edge are left untouched.
86 MCU_cols
= dstinfo
->image_width
/ (dstinfo
->max_h_samp_factor
* DCTSIZE
);
88 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
89 compptr
= dstinfo
->comp_info
+ ci
;
90 comp_width
= MCU_cols
* compptr
->h_samp_factor
;
91 for (blk_y
= 0; blk_y
< compptr
->height_in_data_units
;
92 blk_y
+= compptr
->v_samp_factor
) {
93 buffer
= (*srcinfo
->mem
->access_virt_barray
)
94 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
], blk_y
,
95 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
96 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
97 for (blk_x
= 0; blk_x
* 2 < comp_width
; blk_x
++) {
98 ptr1
= buffer
[offset_y
][blk_x
];
99 ptr2
= buffer
[offset_y
][comp_width
- blk_x
- 1];
100 /* this unrolled loop doesn't need to know which row it's on... */
101 for (k
= 0; k
< DCTSIZE2
; k
+= 2) {
102 temp1
= *ptr1
; /* swap even column */
106 temp1
= *ptr1
; /* swap odd column with sign change */
119 do_flip_v (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
120 jvirt_barray_ptr
*src_coef_arrays
,
121 jvirt_barray_ptr
*dst_coef_arrays
)
124 JDIMENSION MCU_rows
, comp_height
, dst_blk_x
, dst_blk_y
;
125 int ci
, i
, j
, offset_y
;
126 JBLOCKARRAY src_buffer
, dst_buffer
;
127 JBLOCKROW src_row_ptr
, dst_row_ptr
;
128 JCOEFPTR src_ptr
, dst_ptr
;
129 jpeg_component_info
*compptr
;
131 /* We output into a separate array because we can't touch different
132 * rows of the source virtual array simultaneously. Otherwise, this
133 * is a pretty straightforward analog of horizontal flip.
134 * Within a DCT block, vertical mirroring is done by changing the signs
135 * of odd-numbered rows.
136 * Partial iMCUs at the bottom edge are copied verbatim.
138 MCU_rows
= dstinfo
->image_height
/ (dstinfo
->max_v_samp_factor
* DCTSIZE
);
140 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
141 compptr
= dstinfo
->comp_info
+ ci
;
142 comp_height
= MCU_rows
* compptr
->v_samp_factor
;
143 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_data_units
;
144 dst_blk_y
+= compptr
->v_samp_factor
) {
145 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
146 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
147 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
148 if (dst_blk_y
< comp_height
) {
149 /* Row is within the mirrorable area. */
150 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
151 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
152 comp_height
- dst_blk_y
- (JDIMENSION
) compptr
->v_samp_factor
,
153 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
155 /* Bottom-edge blocks will be copied verbatim. */
156 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
157 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
], dst_blk_y
,
158 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
160 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
161 if (dst_blk_y
< comp_height
) {
162 /* Row is within the mirrorable area. */
163 dst_row_ptr
= dst_buffer
[offset_y
];
164 src_row_ptr
= src_buffer
[compptr
->v_samp_factor
- offset_y
- 1];
165 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_data_units
;
167 dst_ptr
= dst_row_ptr
[dst_blk_x
];
168 src_ptr
= src_row_ptr
[dst_blk_x
];
169 for (i
= 0; i
< DCTSIZE
; i
+= 2) {
171 for (j
= 0; j
< DCTSIZE
; j
++)
172 *dst_ptr
++ = *src_ptr
++;
173 /* copy odd row with sign change */
174 for (j
= 0; j
< DCTSIZE
; j
++)
175 *dst_ptr
++ = - *src_ptr
++;
179 /* Just copy row verbatim. */
180 jcopy_block_row(src_buffer
[offset_y
], dst_buffer
[offset_y
],
181 compptr
->width_in_data_units
);
190 do_transpose (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
191 jvirt_barray_ptr
*src_coef_arrays
,
192 jvirt_barray_ptr
*dst_coef_arrays
)
193 /* Transpose source into destination */
195 JDIMENSION dst_blk_x
, dst_blk_y
;
196 int ci
, i
, j
, offset_x
, offset_y
;
197 JBLOCKARRAY src_buffer
, dst_buffer
;
198 JCOEFPTR src_ptr
, dst_ptr
;
199 jpeg_component_info
*compptr
;
201 /* Transposing pixels within a block just requires transposing the
203 * Partial iMCUs at the edges require no special treatment; we simply
204 * process all the available DCT blocks for every component.
206 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
207 compptr
= dstinfo
->comp_info
+ ci
;
208 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_data_units
;
209 dst_blk_y
+= compptr
->v_samp_factor
) {
210 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
211 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
212 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
213 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
214 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_data_units
;
215 dst_blk_x
+= compptr
->h_samp_factor
) {
216 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
217 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
], dst_blk_x
,
218 (JDIMENSION
) compptr
->h_samp_factor
, FALSE
);
219 for (offset_x
= 0; offset_x
< compptr
->h_samp_factor
; offset_x
++) {
220 src_ptr
= src_buffer
[offset_x
][dst_blk_y
+ offset_y
];
221 dst_ptr
= dst_buffer
[offset_y
][dst_blk_x
+ offset_x
];
222 for (i
= 0; i
< DCTSIZE
; i
++)
223 for (j
= 0; j
< DCTSIZE
; j
++)
224 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
234 do_rot_90 (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
235 jvirt_barray_ptr
*src_coef_arrays
,
236 jvirt_barray_ptr
*dst_coef_arrays
)
237 /* 90 degree rotation is equivalent to
238 * 1. Transposing the image;
239 * 2. Horizontal mirroring.
240 * These two steps are merged into a single processing routine.
243 JDIMENSION MCU_cols
, comp_width
, dst_blk_x
, dst_blk_y
;
244 int ci
, i
, j
, offset_x
, offset_y
;
245 JBLOCKARRAY src_buffer
, dst_buffer
;
246 JCOEFPTR src_ptr
, dst_ptr
;
247 jpeg_component_info
*compptr
;
249 /* Because of the horizontal mirror step, we can't process partial iMCUs
250 * at the (output) right edge properly. They just get transposed and
253 MCU_cols
= dstinfo
->image_width
/ (dstinfo
->max_h_samp_factor
* DCTSIZE
);
255 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
256 compptr
= dstinfo
->comp_info
+ ci
;
257 comp_width
= MCU_cols
* compptr
->h_samp_factor
;
258 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_data_units
;
259 dst_blk_y
+= compptr
->v_samp_factor
) {
260 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
261 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
262 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
263 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
264 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_data_units
;
265 dst_blk_x
+= compptr
->h_samp_factor
) {
266 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
267 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
], dst_blk_x
,
268 (JDIMENSION
) compptr
->h_samp_factor
, FALSE
);
269 for (offset_x
= 0; offset_x
< compptr
->h_samp_factor
; offset_x
++) {
270 src_ptr
= src_buffer
[offset_x
][dst_blk_y
+ offset_y
];
271 if (dst_blk_x
< comp_width
) {
272 /* Block is within the mirrorable area. */
273 dst_ptr
= dst_buffer
[offset_y
]
274 [comp_width
- dst_blk_x
- offset_x
- 1];
275 for (i
= 0; i
< DCTSIZE
; i
++) {
276 for (j
= 0; j
< DCTSIZE
; j
++)
277 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
279 for (j
= 0; j
< DCTSIZE
; j
++)
280 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
283 /* Edge blocks are transposed but not mirrored. */
284 dst_ptr
= dst_buffer
[offset_y
][dst_blk_x
+ offset_x
];
285 for (i
= 0; i
< DCTSIZE
; i
++)
286 for (j
= 0; j
< DCTSIZE
; j
++)
287 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
298 do_rot_270 (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
299 jvirt_barray_ptr
*src_coef_arrays
,
300 jvirt_barray_ptr
*dst_coef_arrays
)
301 /* 270 degree rotation is equivalent to
302 * 1. Horizontal mirroring;
303 * 2. Transposing the image.
304 * These two steps are merged into a single processing routine.
307 JDIMENSION MCU_rows
, comp_height
, dst_blk_x
, dst_blk_y
;
308 int ci
, i
, j
, offset_x
, offset_y
;
309 JBLOCKARRAY src_buffer
, dst_buffer
;
310 JCOEFPTR src_ptr
, dst_ptr
;
311 jpeg_component_info
*compptr
;
313 /* Because of the horizontal mirror step, we can't process partial iMCUs
314 * at the (output) bottom edge properly. They just get transposed and
317 MCU_rows
= dstinfo
->image_height
/ (dstinfo
->max_v_samp_factor
* DCTSIZE
);
319 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
320 compptr
= dstinfo
->comp_info
+ ci
;
321 comp_height
= MCU_rows
* compptr
->v_samp_factor
;
322 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_data_units
;
323 dst_blk_y
+= compptr
->v_samp_factor
) {
324 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
325 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
326 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
327 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
328 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_data_units
;
329 dst_blk_x
+= compptr
->h_samp_factor
) {
330 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
331 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
], dst_blk_x
,
332 (JDIMENSION
) compptr
->h_samp_factor
, FALSE
);
333 for (offset_x
= 0; offset_x
< compptr
->h_samp_factor
; offset_x
++) {
334 dst_ptr
= dst_buffer
[offset_y
][dst_blk_x
+ offset_x
];
335 if (dst_blk_y
< comp_height
) {
336 /* Block is within the mirrorable area. */
337 src_ptr
= src_buffer
[offset_x
]
338 [comp_height
- dst_blk_y
- offset_y
- 1];
339 for (i
= 0; i
< DCTSIZE
; i
++) {
340 for (j
= 0; j
< DCTSIZE
; j
++) {
341 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
343 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
347 /* Edge blocks are transposed but not mirrored. */
348 src_ptr
= src_buffer
[offset_x
][dst_blk_y
+ offset_y
];
349 for (i
= 0; i
< DCTSIZE
; i
++)
350 for (j
= 0; j
< DCTSIZE
; j
++)
351 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
362 do_rot_180 (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
363 jvirt_barray_ptr
*src_coef_arrays
,
364 jvirt_barray_ptr
*dst_coef_arrays
)
365 /* 180 degree rotation is equivalent to
366 * 1. Vertical mirroring;
367 * 2. Horizontal mirroring.
368 * These two steps are merged into a single processing routine.
371 JDIMENSION MCU_cols
, MCU_rows
, comp_width
, comp_height
, dst_blk_x
, dst_blk_y
;
372 int ci
, i
, j
, offset_y
;
373 JBLOCKARRAY src_buffer
, dst_buffer
;
374 JBLOCKROW src_row_ptr
, dst_row_ptr
;
375 JCOEFPTR src_ptr
, dst_ptr
;
376 jpeg_component_info
*compptr
;
378 MCU_cols
= dstinfo
->image_width
/ (dstinfo
->max_h_samp_factor
* DCTSIZE
);
379 MCU_rows
= dstinfo
->image_height
/ (dstinfo
->max_v_samp_factor
* DCTSIZE
);
381 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
382 compptr
= dstinfo
->comp_info
+ ci
;
383 comp_width
= MCU_cols
* compptr
->h_samp_factor
;
384 comp_height
= MCU_rows
* compptr
->v_samp_factor
;
385 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_data_units
;
386 dst_blk_y
+= compptr
->v_samp_factor
) {
387 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
388 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
389 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
390 if (dst_blk_y
< comp_height
) {
391 /* Row is within the vertically mirrorable area. */
392 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
393 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
],
394 comp_height
- dst_blk_y
- (JDIMENSION
) compptr
->v_samp_factor
,
395 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
397 /* Bottom-edge rows are only mirrored horizontally. */
398 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
399 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
], dst_blk_y
,
400 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
402 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
403 if (dst_blk_y
< comp_height
) {
404 /* Row is within the mirrorable area. */
405 dst_row_ptr
= dst_buffer
[offset_y
];
406 src_row_ptr
= src_buffer
[compptr
->v_samp_factor
- offset_y
- 1];
407 /* Process the blocks that can be mirrored both ways. */
408 for (dst_blk_x
= 0; dst_blk_x
< comp_width
; dst_blk_x
++) {
409 dst_ptr
= dst_row_ptr
[dst_blk_x
];
410 src_ptr
= src_row_ptr
[comp_width
- dst_blk_x
- 1];
411 for (i
= 0; i
< DCTSIZE
; i
+= 2) {
412 /* For even row, negate every odd column. */
413 for (j
= 0; j
< DCTSIZE
; j
+= 2) {
414 *dst_ptr
++ = *src_ptr
++;
415 *dst_ptr
++ = - *src_ptr
++;
417 /* For odd row, negate every even column. */
418 for (j
= 0; j
< DCTSIZE
; j
+= 2) {
419 *dst_ptr
++ = - *src_ptr
++;
420 *dst_ptr
++ = *src_ptr
++;
424 /* Any remaining right-edge blocks are only mirrored vertically. */
425 for (; dst_blk_x
< compptr
->width_in_data_units
; dst_blk_x
++) {
426 dst_ptr
= dst_row_ptr
[dst_blk_x
];
427 src_ptr
= src_row_ptr
[dst_blk_x
];
428 for (i
= 0; i
< DCTSIZE
; i
+= 2) {
429 for (j
= 0; j
< DCTSIZE
; j
++)
430 *dst_ptr
++ = *src_ptr
++;
431 for (j
= 0; j
< DCTSIZE
; j
++)
432 *dst_ptr
++ = - *src_ptr
++;
436 /* Remaining rows are just mirrored horizontally. */
437 dst_row_ptr
= dst_buffer
[offset_y
];
438 src_row_ptr
= src_buffer
[offset_y
];
439 /* Process the blocks that can be mirrored. */
440 for (dst_blk_x
= 0; dst_blk_x
< comp_width
; dst_blk_x
++) {
441 dst_ptr
= dst_row_ptr
[dst_blk_x
];
442 src_ptr
= src_row_ptr
[comp_width
- dst_blk_x
- 1];
443 for (i
= 0; i
< DCTSIZE2
; i
+= 2) {
444 *dst_ptr
++ = *src_ptr
++;
445 *dst_ptr
++ = - *src_ptr
++;
448 /* Any remaining right-edge blocks are only copied. */
449 for (; dst_blk_x
< compptr
->width_in_data_units
; dst_blk_x
++) {
450 dst_ptr
= dst_row_ptr
[dst_blk_x
];
451 src_ptr
= src_row_ptr
[dst_blk_x
];
452 for (i
= 0; i
< DCTSIZE2
; i
++)
453 *dst_ptr
++ = *src_ptr
++;
463 do_transverse (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
464 jvirt_barray_ptr
*src_coef_arrays
,
465 jvirt_barray_ptr
*dst_coef_arrays
)
466 /* Transverse transpose is equivalent to
467 * 1. 180 degree rotation;
470 * 1. Horizontal mirroring;
472 * 3. Horizontal mirroring.
473 * These steps are merged into a single processing routine.
476 JDIMENSION MCU_cols
, MCU_rows
, comp_width
, comp_height
, dst_blk_x
, dst_blk_y
;
477 int ci
, i
, j
, offset_x
, offset_y
;
478 JBLOCKARRAY src_buffer
, dst_buffer
;
479 JCOEFPTR src_ptr
, dst_ptr
;
480 jpeg_component_info
*compptr
;
482 MCU_cols
= dstinfo
->image_width
/ (dstinfo
->max_h_samp_factor
* DCTSIZE
);
483 MCU_rows
= dstinfo
->image_height
/ (dstinfo
->max_v_samp_factor
* DCTSIZE
);
485 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
486 compptr
= dstinfo
->comp_info
+ ci
;
487 comp_width
= MCU_cols
* compptr
->h_samp_factor
;
488 comp_height
= MCU_rows
* compptr
->v_samp_factor
;
489 for (dst_blk_y
= 0; dst_blk_y
< compptr
->height_in_data_units
;
490 dst_blk_y
+= compptr
->v_samp_factor
) {
491 dst_buffer
= (*srcinfo
->mem
->access_virt_barray
)
492 ((j_common_ptr
) srcinfo
, dst_coef_arrays
[ci
], dst_blk_y
,
493 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
494 for (offset_y
= 0; offset_y
< compptr
->v_samp_factor
; offset_y
++) {
495 for (dst_blk_x
= 0; dst_blk_x
< compptr
->width_in_data_units
;
496 dst_blk_x
+= compptr
->h_samp_factor
) {
497 src_buffer
= (*srcinfo
->mem
->access_virt_barray
)
498 ((j_common_ptr
) srcinfo
, src_coef_arrays
[ci
], dst_blk_x
,
499 (JDIMENSION
) compptr
->h_samp_factor
, FALSE
);
500 for (offset_x
= 0; offset_x
< compptr
->h_samp_factor
; offset_x
++) {
501 if (dst_blk_y
< comp_height
) {
502 src_ptr
= src_buffer
[offset_x
]
503 [comp_height
- dst_blk_y
- offset_y
- 1];
504 if (dst_blk_x
< comp_width
) {
505 /* Block is within the mirrorable area. */
506 dst_ptr
= dst_buffer
[offset_y
]
507 [comp_width
- dst_blk_x
- offset_x
- 1];
508 for (i
= 0; i
< DCTSIZE
; i
++) {
509 for (j
= 0; j
< DCTSIZE
; j
++) {
510 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
512 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
515 for (j
= 0; j
< DCTSIZE
; j
++) {
516 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
518 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
522 /* Right-edge blocks are mirrored in y only */
523 dst_ptr
= dst_buffer
[offset_y
][dst_blk_x
+ offset_x
];
524 for (i
= 0; i
< DCTSIZE
; i
++) {
525 for (j
= 0; j
< DCTSIZE
; j
++) {
526 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
528 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
533 src_ptr
= src_buffer
[offset_x
][dst_blk_y
+ offset_y
];
534 if (dst_blk_x
< comp_width
) {
535 /* Bottom-edge blocks are mirrored in x only */
536 dst_ptr
= dst_buffer
[offset_y
]
537 [comp_width
- dst_blk_x
- offset_x
- 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 for (j
= 0; j
< DCTSIZE
; j
++)
543 dst_ptr
[j
*DCTSIZE
+i
] = -src_ptr
[i
*DCTSIZE
+j
];
546 /* At lower right corner, just transpose, no mirroring */
547 dst_ptr
= dst_buffer
[offset_y
][dst_blk_x
+ offset_x
];
548 for (i
= 0; i
< DCTSIZE
; i
++)
549 for (j
= 0; j
< DCTSIZE
; j
++)
550 dst_ptr
[j
*DCTSIZE
+i
] = src_ptr
[i
*DCTSIZE
+j
];
561 /* Request any required workspace.
563 * We allocate the workspace virtual arrays from the source decompression
564 * object, so that all the arrays (both the original data and the workspace)
565 * will be taken into account while making memory management decisions.
566 * Hence, this routine must be called after jpeg_read_header (which reads
567 * the image dimensions) and before jpeg_read_coefficients (which realizes
568 * the source's virtual arrays).
572 jtransform_request_workspace (j_decompress_ptr srcinfo
,
573 jpeg_transform_info
*info
)
575 jvirt_barray_ptr
*coef_arrays
= NULL
;
576 jpeg_component_info
*compptr
;
579 if (info
->force_grayscale
&&
580 srcinfo
->jpeg_color_space
== JCS_YCbCr
&&
581 srcinfo
->num_components
== 3) {
582 /* We'll only process the first component */
583 info
->num_components
= 1;
585 /* Process all the components */
586 info
->num_components
= srcinfo
->num_components
;
589 switch (info
->transform
) {
592 /* Don't need a workspace array */
596 /* Need workspace arrays having same dimensions as source image.
597 * Note that we allocate arrays padded out to the next iMCU boundary,
598 * so that transform routines need not worry about missing edge blocks.
600 coef_arrays
= (jvirt_barray_ptr
*)
601 (*srcinfo
->mem
->alloc_small
) ((j_common_ptr
) srcinfo
, JPOOL_IMAGE
,
602 SIZEOF(jvirt_barray_ptr
) * info
->num_components
);
603 for (ci
= 0; ci
< info
->num_components
; ci
++) {
604 compptr
= srcinfo
->comp_info
+ ci
;
605 coef_arrays
[ci
] = (*srcinfo
->mem
->request_virt_barray
)
606 ((j_common_ptr
) srcinfo
, JPOOL_IMAGE
, FALSE
,
607 (JDIMENSION
) jround_up((long) compptr
->width_in_data_units
,
608 (long) compptr
->h_samp_factor
),
609 (JDIMENSION
) jround_up((long) compptr
->height_in_data_units
,
610 (long) compptr
->v_samp_factor
),
611 (JDIMENSION
) compptr
->v_samp_factor
);
614 case JXFORM_TRANSPOSE
:
615 case JXFORM_TRANSVERSE
:
618 /* Need workspace arrays having transposed dimensions.
619 * Note that we allocate arrays padded out to the next iMCU boundary,
620 * so that transform routines need not worry about missing edge blocks.
622 coef_arrays
= (jvirt_barray_ptr
*)
623 (*srcinfo
->mem
->alloc_small
) ((j_common_ptr
) srcinfo
, JPOOL_IMAGE
,
624 SIZEOF(jvirt_barray_ptr
) * info
->num_components
);
625 for (ci
= 0; ci
< info
->num_components
; ci
++) {
626 compptr
= srcinfo
->comp_info
+ ci
;
627 coef_arrays
[ci
] = (*srcinfo
->mem
->request_virt_barray
)
628 ((j_common_ptr
) srcinfo
, JPOOL_IMAGE
, FALSE
,
629 (JDIMENSION
) jround_up((long) compptr
->height_in_data_units
,
630 (long) compptr
->v_samp_factor
),
631 (JDIMENSION
) jround_up((long) compptr
->width_in_data_units
,
632 (long) compptr
->h_samp_factor
),
633 (JDIMENSION
) compptr
->h_samp_factor
);
637 info
->workspace_coef_arrays
= coef_arrays
;
641 /* Transpose destination image parameters */
644 transpose_critical_parameters (j_compress_ptr dstinfo
)
646 int tblno
, i
, j
, ci
, itemp
;
647 jpeg_component_info
*compptr
;
652 /* Transpose basic image dimensions */
653 dtemp
= dstinfo
->image_width
;
654 dstinfo
->image_width
= dstinfo
->image_height
;
655 dstinfo
->image_height
= dtemp
;
657 /* Transpose sampling factors */
658 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
659 compptr
= dstinfo
->comp_info
+ ci
;
660 itemp
= compptr
->h_samp_factor
;
661 compptr
->h_samp_factor
= compptr
->v_samp_factor
;
662 compptr
->v_samp_factor
= itemp
;
665 /* Transpose quantization tables */
666 for (tblno
= 0; tblno
< NUM_QUANT_TBLS
; tblno
++) {
667 qtblptr
= dstinfo
->quant_tbl_ptrs
[tblno
];
668 if (qtblptr
!= NULL
) {
669 for (i
= 0; i
< DCTSIZE
; i
++) {
670 for (j
= 0; j
< i
; j
++) {
671 qtemp
= qtblptr
->quantval
[i
*DCTSIZE
+j
];
672 qtblptr
->quantval
[i
*DCTSIZE
+j
] = qtblptr
->quantval
[j
*DCTSIZE
+i
];
673 qtblptr
->quantval
[j
*DCTSIZE
+i
] = qtemp
;
681 /* Trim off any partial iMCUs on the indicated destination edge */
684 trim_right_edge (j_compress_ptr dstinfo
)
686 int ci
, max_h_samp_factor
;
689 /* We have to compute max_h_samp_factor ourselves,
690 * because it hasn't been set yet in the destination
691 * (and we don't want to use the source's value).
693 max_h_samp_factor
= 1;
694 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
695 int h_samp_factor
= dstinfo
->comp_info
[ci
].h_samp_factor
;
696 max_h_samp_factor
= MAX(max_h_samp_factor
, h_samp_factor
);
698 MCU_cols
= dstinfo
->image_width
/ (max_h_samp_factor
* DCTSIZE
);
699 if (MCU_cols
> 0) /* can't trim to 0 pixels */
700 dstinfo
->image_width
= MCU_cols
* (max_h_samp_factor
* DCTSIZE
);
704 trim_bottom_edge (j_compress_ptr dstinfo
)
706 int ci
, max_v_samp_factor
;
709 /* We have to compute max_v_samp_factor ourselves,
710 * because it hasn't been set yet in the destination
711 * (and we don't want to use the source's value).
713 max_v_samp_factor
= 1;
714 for (ci
= 0; ci
< dstinfo
->num_components
; ci
++) {
715 int v_samp_factor
= dstinfo
->comp_info
[ci
].v_samp_factor
;
716 max_v_samp_factor
= MAX(max_v_samp_factor
, v_samp_factor
);
718 MCU_rows
= dstinfo
->image_height
/ (max_v_samp_factor
* DCTSIZE
);
719 if (MCU_rows
> 0) /* can't trim to 0 pixels */
720 dstinfo
->image_height
= MCU_rows
* (max_v_samp_factor
* DCTSIZE
);
724 /* Adjust output image parameters as needed.
726 * This must be called after jpeg_copy_critical_parameters()
727 * and before jpeg_write_coefficients().
729 * The return value is the set of virtual coefficient arrays to be written
730 * (either the ones allocated by jtransform_request_workspace, or the
731 * original source data arrays). The caller will need to pass this value
732 * to jpeg_write_coefficients().
735 JGLOBAL(jvirt_barray_ptr
*)
736 jtransform_adjust_parameters (j_decompress_ptr srcinfo
,
737 j_compress_ptr dstinfo
,
738 jvirt_barray_ptr
*src_coef_arrays
,
739 jpeg_transform_info
*info
)
741 /* If force-to-grayscale is requested, adjust destination parameters */
742 if (info
->force_grayscale
) {
743 /* We use jpeg_set_colorspace to make sure subsidiary settings get fixed
744 * properly. Among other things, the target h_samp_factor & v_samp_factor
745 * will get set to 1, which typically won't match the source.
746 * In fact we do this even if the source is already grayscale; that
747 * provides an easy way of coercing a grayscale JPEG with funny sampling
748 * factors to the customary 1,1. (Some decoders fail on other factors.)
750 if ((dstinfo
->jpeg_color_space
== JCS_YCbCr
&&
751 dstinfo
->num_components
== 3) ||
752 (dstinfo
->jpeg_color_space
== JCS_GRAYSCALE
&&
753 dstinfo
->num_components
== 1)) {
754 /* We have to preserve the source's quantization table number. */
755 int sv_quant_tbl_no
= dstinfo
->comp_info
[0].quant_tbl_no
;
756 jpeg_set_colorspace(dstinfo
, JCS_GRAYSCALE
);
757 dstinfo
->comp_info
[0].quant_tbl_no
= sv_quant_tbl_no
;
759 /* Sorry, can't do it */
760 ERREXIT(dstinfo
, JERR_CONVERSION_NOTIMPL
);
764 /* Correct the destination's image dimensions etc if necessary */
765 switch (info
->transform
) {
771 trim_right_edge(dstinfo
);
775 trim_bottom_edge(dstinfo
);
777 case JXFORM_TRANSPOSE
:
778 transpose_critical_parameters(dstinfo
);
779 /* transpose does NOT have to trim anything */
781 case JXFORM_TRANSVERSE
:
782 transpose_critical_parameters(dstinfo
);
784 trim_right_edge(dstinfo
);
785 trim_bottom_edge(dstinfo
);
789 transpose_critical_parameters(dstinfo
);
791 trim_right_edge(dstinfo
);
795 trim_right_edge(dstinfo
);
796 trim_bottom_edge(dstinfo
);
800 transpose_critical_parameters(dstinfo
);
802 trim_bottom_edge(dstinfo
);
806 /* Return the appropriate output data set */
807 if (info
->workspace_coef_arrays
!= NULL
)
808 return info
->workspace_coef_arrays
;
809 return src_coef_arrays
;
813 /* Execute the actual transformation, if any.
815 * This must be called *after* jpeg_write_coefficients, because it depends
816 * on jpeg_write_coefficients to have computed subsidiary values such as
817 * the per-component width and height fields in the destination object.
819 * Note that some transformations will modify the source data arrays!
823 jtransform_execute_transformation (j_decompress_ptr srcinfo
,
824 j_compress_ptr dstinfo
,
825 jvirt_barray_ptr
*src_coef_arrays
,
826 jpeg_transform_info
*info
)
828 jvirt_barray_ptr
*dst_coef_arrays
= info
->workspace_coef_arrays
;
830 switch (info
->transform
) {
834 do_flip_h(srcinfo
, dstinfo
, src_coef_arrays
);
837 do_flip_v(srcinfo
, dstinfo
, src_coef_arrays
, dst_coef_arrays
);
839 case JXFORM_TRANSPOSE
:
840 do_transpose(srcinfo
, dstinfo
, src_coef_arrays
, dst_coef_arrays
);
842 case JXFORM_TRANSVERSE
:
843 do_transverse(srcinfo
, dstinfo
, src_coef_arrays
, dst_coef_arrays
);
846 do_rot_90(srcinfo
, dstinfo
, src_coef_arrays
, dst_coef_arrays
);
849 do_rot_180(srcinfo
, dstinfo
, src_coef_arrays
, dst_coef_arrays
);
852 do_rot_270(srcinfo
, dstinfo
, src_coef_arrays
, dst_coef_arrays
);
857 #endif /* TRANSFORMS_SUPPORTED */
860 /* Setup decompression object to save desired markers in memory.
861 * This must be called before jpeg_read_header() to have the desired effect.
865 jcopy_markers_setup (j_decompress_ptr srcinfo
, JCOPY_OPTION option
)
867 #ifdef SAVE_MARKERS_SUPPORTED
870 /* Save comments except under NONE option */
871 if (option
!= JCOPYOPT_NONE
) {
872 jpeg_save_markers(srcinfo
, JPEG_COM
, 0xFFFF);
874 /* Save all types of APPn markers iff ALL option */
875 if (option
== JCOPYOPT_ALL
) {
876 for (m
= 0; m
< 16; m
++)
877 jpeg_save_markers(srcinfo
, JPEG_APP0
+ m
, 0xFFFF);
879 #endif /* SAVE_MARKERS_SUPPORTED */
882 /* Copy markers saved in the given source object to the destination object.
883 * This should be called just after jpeg_start_compress() or
884 * jpeg_write_coefficients().
885 * Note that those routines will have written the SOI, and also the
886 * JFIF APP0 or Adobe APP14 markers if selected.
890 jcopy_markers_execute (j_decompress_ptr srcinfo
, j_compress_ptr dstinfo
,
893 jpeg_saved_marker_ptr marker
;
895 /* In the current implementation, we don't actually need to examine the
896 * option flag here; we just copy everything that got saved.
897 * But to avoid confusion, we do not output JFIF and Adobe APP14 markers
898 * if the encoder library already wrote one.
900 for (marker
= srcinfo
->marker_list
; marker
!= NULL
; marker
= marker
->next
) {
901 if (dstinfo
->write_JFIF_header
&&
902 marker
->marker
== JPEG_APP0
&&
903 marker
->data_length
>= 5 &&
904 GETJOCTET(marker
->data
[0]) == 0x4A &&
905 GETJOCTET(marker
->data
[1]) == 0x46 &&
906 GETJOCTET(marker
->data
[2]) == 0x49 &&
907 GETJOCTET(marker
->data
[3]) == 0x46 &&
908 GETJOCTET(marker
->data
[4]) == 0)
909 continue; /* reject duplicate JFIF */
910 if (dstinfo
->write_Adobe_marker
&&
911 marker
->marker
== JPEG_APP0
+14 &&
912 marker
->data_length
>= 5 &&
913 GETJOCTET(marker
->data
[0]) == 0x41 &&
914 GETJOCTET(marker
->data
[1]) == 0x64 &&
915 GETJOCTET(marker
->data
[2]) == 0x6F &&
916 GETJOCTET(marker
->data
[3]) == 0x62 &&
917 GETJOCTET(marker
->data
[4]) == 0x65)
918 continue; /* reject duplicate Adobe */
919 #ifdef NEED_FAR_POINTERS
920 /* We could use jpeg_write_marker if the data weren't FAR... */
923 jpeg_write_m_header(dstinfo
, marker
->marker
, marker
->data_length
);
924 for (i
= 0; i
< marker
->data_length
; i
++)
925 jpeg_write_m_byte(dstinfo
, marker
->data
[i
]);
928 jpeg_write_marker(dstinfo
, marker
->marker
,
929 marker
->data
, marker
->data_length
);