Line 0
Link Here
|
|
|
1 |
/* |
2 |
* transupp.c |
3 |
* |
4 |
* Copyright (C) 1997-2009, Thomas G. Lane, Guido Vollbeding. |
5 |
* This file is part of the Independent JPEG Group's software. |
6 |
* For conditions of distribution and use, see the accompanying README file. |
7 |
* |
8 |
* This file contains image transformation routines and other utility code |
9 |
* used by the jpegtran sample application. These are NOT part of the core |
10 |
* JPEG library. But we keep these routines separate from jpegtran.c to |
11 |
* ease the task of maintaining jpegtran-like programs that have other user |
12 |
* interfaces. |
13 |
*/ |
14 |
|
15 |
/* Although this file really shouldn't have access to the library internals, |
16 |
* it's helpful to let it call jround_up() and jcopy_block_row(). |
17 |
*/ |
18 |
#define JPEG_INTERNALS |
19 |
|
20 |
// LibJPEG includes |
21 |
|
22 |
extern "C" |
23 |
{ |
24 |
#include "jinclude.h" |
25 |
#include "jpeglib.h" |
26 |
} |
27 |
|
28 |
// Local includes |
29 |
|
30 |
#include "transupp.h" /* My own external interface */ |
31 |
#include <ctype.h> /* to declare isdigit() */ |
32 |
|
33 |
namespace KIPIJPEGLossLessPlugin |
34 |
{ |
35 |
|
36 |
#if TRANSFORMS_SUPPORTED |
37 |
|
38 |
/* |
39 |
* Lossless image transformation routines. These routines work on DCT |
40 |
* coefficient arrays and thus do not require any lossy decompression |
41 |
* or recompression of the image. |
42 |
* Thanks to Guido Vollbeding for the initial design and code of this feature, |
43 |
* and to Ben Jackson for introducing the cropping feature. |
44 |
* |
45 |
* Horizontal flipping is done in-place, using a single top-to-bottom |
46 |
* pass through the virtual source array. It will thus be much the |
47 |
* fastest option for images larger than main memory. |
48 |
* |
49 |
* The other routines require a set of destination virtual arrays, so they |
50 |
* need twice as much memory as jpegtran normally does. The destination |
51 |
* arrays are always written in normal scan order (top to bottom) because |
52 |
* the virtual array manager expects this. The source arrays will be scanned |
53 |
* in the corresponding order, which means multiple passes through the source |
54 |
* arrays for most of the transforms. That could result in much thrashing |
55 |
* if the image is larger than main memory. |
56 |
* |
57 |
* If cropping or trimming is involved, the destination arrays may be smaller |
58 |
* than the source arrays. Note it is not possible to do horizontal flip |
59 |
* in-place when a nonzero Y crop offset is specified, since we'd have to move |
60 |
* data from one block row to another but the virtual array manager doesn't |
61 |
* guarantee we can touch more than one row at a time. So in that case, |
62 |
* we have to use a separate destination array. |
63 |
* |
64 |
* Some notes about the operating environment of the individual transform |
65 |
* routines: |
66 |
* 1. Both the source and destination virtual arrays are allocated from the |
67 |
* source JPEG object, and therefore should be manipulated by calling the |
68 |
* source's memory manager. |
69 |
* 2. The destination's component count should be used. It may be smaller |
70 |
* than the source's when forcing to grayscale. |
71 |
* 3. Likewise the destination's sampling factors should be used. When |
72 |
* forcing to grayscale the destination's sampling factors will be all 1, |
73 |
* and we may as well take that as the effective iMCU size. |
74 |
* 4. When "trim" is in effect, the destination's dimensions will be the |
75 |
* trimmed values but the source's will be untrimmed. |
76 |
* 5. When "crop" is in effect, the destination's dimensions will be the |
77 |
* cropped values but the source's will be uncropped. Each transform |
78 |
* routine is responsible for picking up source data starting at the |
79 |
* correct X and Y offset for the crop region. (The X and Y offsets |
80 |
* passed to the transform routines are measured in iMCU blocks of the |
81 |
* destination.) |
82 |
* 6. All the routines assume that the source and destination buffers are |
83 |
* padded out to a full iMCU boundary. This is true, although for the |
84 |
* source buffer it is an undocumented property of jdcoefct.c. |
85 |
*/ |
86 |
|
87 |
|
88 |
|
89 |
LOCAL(void) |
90 |
do_crop (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
91 |
JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, |
92 |
jvirt_barray_ptr *src_coef_arrays, |
93 |
jvirt_barray_ptr *dst_coef_arrays) |
94 |
/* Crop. This is only used when no rotate/flip is requested with the crop. */ |
95 |
{ |
96 |
JDIMENSION dst_blk_y, x_crop_blocks, y_crop_blocks; |
97 |
int ci, offset_y; |
98 |
JBLOCKARRAY src_buffer, dst_buffer; |
99 |
jpeg_component_info *compptr; |
100 |
|
101 |
/* We simply have to copy the right amount of data (the destination's |
102 |
* image size) starting at the given X and Y offsets in the source. |
103 |
*/ |
104 |
for (ci = 0; ci < dstinfo->num_components; ci++) { |
105 |
compptr = dstinfo->comp_info + ci; |
106 |
x_crop_blocks = x_crop_offset * compptr->h_samp_factor; |
107 |
y_crop_blocks = y_crop_offset * compptr->v_samp_factor; |
108 |
for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; |
109 |
dst_blk_y += compptr->v_samp_factor) { |
110 |
dst_buffer = (*srcinfo->mem->access_virt_barray) |
111 |
((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, |
112 |
(JDIMENSION) compptr->v_samp_factor, TRUE); |
113 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
114 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
115 |
dst_blk_y + y_crop_blocks, |
116 |
(JDIMENSION) compptr->v_samp_factor, FALSE); |
117 |
for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { |
118 |
jcopy_block_row(src_buffer[offset_y] + x_crop_blocks, |
119 |
dst_buffer[offset_y], |
120 |
compptr->width_in_blocks); |
121 |
} |
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
|
127 |
LOCAL(void) |
128 |
do_flip_h_no_crop (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
129 |
JDIMENSION x_crop_offset, |
130 |
jvirt_barray_ptr *src_coef_arrays) |
131 |
/* Horizontal flip; done in-place, so no separate dest array is required. |
132 |
* NB: this only works when y_crop_offset is zero. |
133 |
*/ |
134 |
{ |
135 |
JDIMENSION MCU_cols, comp_width, blk_x, blk_y, x_crop_blocks; |
136 |
int ci, k, offset_y; |
137 |
JBLOCKARRAY buffer; |
138 |
JCOEFPTR ptr1, ptr2; |
139 |
JCOEF temp1, temp2; |
140 |
jpeg_component_info *compptr; |
141 |
|
142 |
/* Horizontal mirroring of DCT blocks is accomplished by swapping |
143 |
* pairs of blocks in-place. Within a DCT block, we perform horizontal |
144 |
* mirroring by changing the signs of odd-numbered columns. |
145 |
* Partial iMCUs at the right edge are left untouched. |
146 |
*/ |
147 |
MCU_cols = srcinfo->output_width / |
148 |
(dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size); |
149 |
|
150 |
for (ci = 0; ci < dstinfo->num_components; ci++) { |
151 |
compptr = dstinfo->comp_info + ci; |
152 |
comp_width = MCU_cols * compptr->h_samp_factor; |
153 |
x_crop_blocks = x_crop_offset * compptr->h_samp_factor; |
154 |
for (blk_y = 0; blk_y < compptr->height_in_blocks; |
155 |
blk_y += compptr->v_samp_factor) { |
156 |
buffer = (*srcinfo->mem->access_virt_barray) |
157 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], blk_y, |
158 |
(JDIMENSION) compptr->v_samp_factor, TRUE); |
159 |
for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { |
160 |
/* Do the mirroring */ |
161 |
for (blk_x = 0; blk_x * 2 < comp_width; blk_x++) { |
162 |
ptr1 = buffer[offset_y][blk_x]; |
163 |
ptr2 = buffer[offset_y][comp_width - blk_x - 1]; |
164 |
/* this unrolled loop doesn't need to know which row it's on... */ |
165 |
for (k = 0; k < DCTSIZE2; k += 2) { |
166 |
temp1 = *ptr1; /* swap even column */ |
167 |
temp2 = *ptr2; |
168 |
*ptr1++ = temp2; |
169 |
*ptr2++ = temp1; |
170 |
temp1 = *ptr1; /* swap odd column with sign change */ |
171 |
temp2 = *ptr2; |
172 |
*ptr1++ = -temp2; |
173 |
*ptr2++ = -temp1; |
174 |
} |
175 |
} |
176 |
if (x_crop_blocks > 0) { |
177 |
/* Now left-justify the portion of the data to be kept. |
178 |
* We can't use a single jcopy_block_row() call because that routine |
179 |
* depends on memcpy(), whose behavior is unspecified for overlapping |
180 |
* source and destination areas. Sigh. |
181 |
*/ |
182 |
for (blk_x = 0; blk_x < compptr->width_in_blocks; blk_x++) { |
183 |
jcopy_block_row(buffer[offset_y] + blk_x + x_crop_blocks, |
184 |
buffer[offset_y] + blk_x, |
185 |
(JDIMENSION) 1); |
186 |
} |
187 |
} |
188 |
} |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
|
194 |
LOCAL(void) |
195 |
do_flip_h (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
196 |
JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, |
197 |
jvirt_barray_ptr *src_coef_arrays, |
198 |
jvirt_barray_ptr *dst_coef_arrays) |
199 |
/* Horizontal flip in general cropping case */ |
200 |
{ |
201 |
JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y; |
202 |
JDIMENSION x_crop_blocks, y_crop_blocks; |
203 |
int ci, k, offset_y; |
204 |
JBLOCKARRAY src_buffer, dst_buffer; |
205 |
JBLOCKROW src_row_ptr, dst_row_ptr; |
206 |
JCOEFPTR src_ptr, dst_ptr; |
207 |
jpeg_component_info *compptr; |
208 |
|
209 |
/* Here we must output into a separate array because we can't touch |
210 |
* different rows of a single virtual array simultaneously. Otherwise, |
211 |
* this is essentially the same as the routine above. |
212 |
*/ |
213 |
MCU_cols = srcinfo->output_width / |
214 |
(dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size); |
215 |
|
216 |
for (ci = 0; ci < dstinfo->num_components; ci++) { |
217 |
compptr = dstinfo->comp_info + ci; |
218 |
comp_width = MCU_cols * compptr->h_samp_factor; |
219 |
x_crop_blocks = x_crop_offset * compptr->h_samp_factor; |
220 |
y_crop_blocks = y_crop_offset * compptr->v_samp_factor; |
221 |
for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; |
222 |
dst_blk_y += compptr->v_samp_factor) { |
223 |
dst_buffer = (*srcinfo->mem->access_virt_barray) |
224 |
((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, |
225 |
(JDIMENSION) compptr->v_samp_factor, TRUE); |
226 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
227 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
228 |
dst_blk_y + y_crop_blocks, |
229 |
(JDIMENSION) compptr->v_samp_factor, FALSE); |
230 |
for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { |
231 |
dst_row_ptr = dst_buffer[offset_y]; |
232 |
src_row_ptr = src_buffer[offset_y]; |
233 |
for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) { |
234 |
if (x_crop_blocks + dst_blk_x < comp_width) { |
235 |
/* Do the mirrorable blocks */ |
236 |
dst_ptr = dst_row_ptr[dst_blk_x]; |
237 |
src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1]; |
238 |
/* this unrolled loop doesn't need to know which row it's on... */ |
239 |
for (k = 0; k < DCTSIZE2; k += 2) { |
240 |
*dst_ptr++ = *src_ptr++; /* copy even column */ |
241 |
*dst_ptr++ = - *src_ptr++; /* copy odd column with sign change */ |
242 |
} |
243 |
} else { |
244 |
/* Copy last partial block(s) verbatim */ |
245 |
jcopy_block_row(src_row_ptr + dst_blk_x + x_crop_blocks, |
246 |
dst_row_ptr + dst_blk_x, |
247 |
(JDIMENSION) 1); |
248 |
} |
249 |
} |
250 |
} |
251 |
} |
252 |
} |
253 |
} |
254 |
|
255 |
|
256 |
LOCAL(void) |
257 |
do_flip_v (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
258 |
JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, |
259 |
jvirt_barray_ptr *src_coef_arrays, |
260 |
jvirt_barray_ptr *dst_coef_arrays) |
261 |
/* Vertical flip */ |
262 |
{ |
263 |
JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y; |
264 |
JDIMENSION x_crop_blocks, y_crop_blocks; |
265 |
int ci, i, j, offset_y; |
266 |
JBLOCKARRAY src_buffer, dst_buffer; |
267 |
JBLOCKROW src_row_ptr, dst_row_ptr; |
268 |
JCOEFPTR src_ptr, dst_ptr; |
269 |
jpeg_component_info *compptr; |
270 |
|
271 |
/* We output into a separate array because we can't touch different |
272 |
* rows of the source virtual array simultaneously. Otherwise, this |
273 |
* is a pretty straightforward analog of horizontal flip. |
274 |
* Within a DCT block, vertical mirroring is done by changing the signs |
275 |
* of odd-numbered rows. |
276 |
* Partial iMCUs at the bottom edge are copied verbatim. |
277 |
*/ |
278 |
MCU_rows = srcinfo->output_height / |
279 |
(dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size); |
280 |
|
281 |
for (ci = 0; ci < dstinfo->num_components; ci++) { |
282 |
compptr = dstinfo->comp_info + ci; |
283 |
comp_height = MCU_rows * compptr->v_samp_factor; |
284 |
x_crop_blocks = x_crop_offset * compptr->h_samp_factor; |
285 |
y_crop_blocks = y_crop_offset * compptr->v_samp_factor; |
286 |
for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; |
287 |
dst_blk_y += compptr->v_samp_factor) { |
288 |
dst_buffer = (*srcinfo->mem->access_virt_barray) |
289 |
((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, |
290 |
(JDIMENSION) compptr->v_samp_factor, TRUE); |
291 |
if (y_crop_blocks + dst_blk_y < comp_height) { |
292 |
/* Row is within the mirrorable area. */ |
293 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
294 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
295 |
comp_height - y_crop_blocks - dst_blk_y - |
296 |
(JDIMENSION) compptr->v_samp_factor, |
297 |
(JDIMENSION) compptr->v_samp_factor, FALSE); |
298 |
} else { |
299 |
/* Bottom-edge blocks will be copied verbatim. */ |
300 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
301 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
302 |
dst_blk_y + y_crop_blocks, |
303 |
(JDIMENSION) compptr->v_samp_factor, FALSE); |
304 |
} |
305 |
for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { |
306 |
if (y_crop_blocks + dst_blk_y < comp_height) { |
307 |
/* Row is within the mirrorable area. */ |
308 |
dst_row_ptr = dst_buffer[offset_y]; |
309 |
src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1]; |
310 |
src_row_ptr += x_crop_blocks; |
311 |
for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; |
312 |
dst_blk_x++) { |
313 |
dst_ptr = dst_row_ptr[dst_blk_x]; |
314 |
src_ptr = src_row_ptr[dst_blk_x]; |
315 |
for (i = 0; i < DCTSIZE; i += 2) { |
316 |
/* copy even row */ |
317 |
for (j = 0; j < DCTSIZE; j++) |
318 |
*dst_ptr++ = *src_ptr++; |
319 |
/* copy odd row with sign change */ |
320 |
for (j = 0; j < DCTSIZE; j++) |
321 |
*dst_ptr++ = - *src_ptr++; |
322 |
} |
323 |
} |
324 |
} else { |
325 |
/* Just copy row verbatim. */ |
326 |
jcopy_block_row(src_buffer[offset_y] + x_crop_blocks, |
327 |
dst_buffer[offset_y], |
328 |
compptr->width_in_blocks); |
329 |
} |
330 |
} |
331 |
} |
332 |
} |
333 |
} |
334 |
|
335 |
|
336 |
LOCAL(void) |
337 |
do_transpose (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
338 |
JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, |
339 |
jvirt_barray_ptr *src_coef_arrays, |
340 |
jvirt_barray_ptr *dst_coef_arrays) |
341 |
/* Transpose source into destination */ |
342 |
{ |
343 |
JDIMENSION dst_blk_x, dst_blk_y, x_crop_blocks, y_crop_blocks; |
344 |
int ci, i, j, offset_x, offset_y; |
345 |
JBLOCKARRAY src_buffer, dst_buffer; |
346 |
JCOEFPTR src_ptr, dst_ptr; |
347 |
jpeg_component_info *compptr; |
348 |
|
349 |
/* Transposing pixels within a block just requires transposing the |
350 |
* DCT coefficients. |
351 |
* Partial iMCUs at the edges require no special treatment; we simply |
352 |
* process all the available DCT blocks for every component. |
353 |
*/ |
354 |
for (ci = 0; ci < dstinfo->num_components; ci++) { |
355 |
compptr = dstinfo->comp_info + ci; |
356 |
x_crop_blocks = x_crop_offset * compptr->h_samp_factor; |
357 |
y_crop_blocks = y_crop_offset * compptr->v_samp_factor; |
358 |
for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; |
359 |
dst_blk_y += compptr->v_samp_factor) { |
360 |
dst_buffer = (*srcinfo->mem->access_virt_barray) |
361 |
((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, |
362 |
(JDIMENSION) compptr->v_samp_factor, TRUE); |
363 |
for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { |
364 |
for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; |
365 |
dst_blk_x += compptr->h_samp_factor) { |
366 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
367 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
368 |
dst_blk_x + x_crop_blocks, |
369 |
(JDIMENSION) compptr->h_samp_factor, FALSE); |
370 |
for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) { |
371 |
dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x]; |
372 |
src_ptr = src_buffer[offset_x][dst_blk_y + offset_y + y_crop_blocks]; |
373 |
for (i = 0; i < DCTSIZE; i++) |
374 |
for (j = 0; j < DCTSIZE; j++) |
375 |
dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; |
376 |
} |
377 |
} |
378 |
} |
379 |
} |
380 |
} |
381 |
} |
382 |
|
383 |
|
384 |
LOCAL(void) |
385 |
do_rot_90 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
386 |
JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, |
387 |
jvirt_barray_ptr *src_coef_arrays, |
388 |
jvirt_barray_ptr *dst_coef_arrays) |
389 |
/* 90 degree rotation is equivalent to |
390 |
* 1. Transposing the image; |
391 |
* 2. Horizontal mirroring. |
392 |
* These two steps are merged into a single processing routine. |
393 |
*/ |
394 |
{ |
395 |
JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y; |
396 |
JDIMENSION x_crop_blocks, y_crop_blocks; |
397 |
int ci, i, j, offset_x, offset_y; |
398 |
JBLOCKARRAY src_buffer, dst_buffer; |
399 |
JCOEFPTR src_ptr, dst_ptr; |
400 |
jpeg_component_info *compptr; |
401 |
|
402 |
/* Because of the horizontal mirror step, we can't process partial iMCUs |
403 |
* at the (output) right edge properly. They just get transposed and |
404 |
* not mirrored. |
405 |
*/ |
406 |
MCU_cols = srcinfo->output_height / |
407 |
(dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size); |
408 |
|
409 |
for (ci = 0; ci < dstinfo->num_components; ci++) { |
410 |
compptr = dstinfo->comp_info + ci; |
411 |
comp_width = MCU_cols * compptr->h_samp_factor; |
412 |
x_crop_blocks = x_crop_offset * compptr->h_samp_factor; |
413 |
y_crop_blocks = y_crop_offset * compptr->v_samp_factor; |
414 |
for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; |
415 |
dst_blk_y += compptr->v_samp_factor) { |
416 |
dst_buffer = (*srcinfo->mem->access_virt_barray) |
417 |
((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, |
418 |
(JDIMENSION) compptr->v_samp_factor, TRUE); |
419 |
for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { |
420 |
for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; |
421 |
dst_blk_x += compptr->h_samp_factor) { |
422 |
if (x_crop_blocks + dst_blk_x < comp_width) { |
423 |
/* Block is within the mirrorable area. */ |
424 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
425 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
426 |
comp_width - x_crop_blocks - dst_blk_x - |
427 |
(JDIMENSION) compptr->h_samp_factor, |
428 |
(JDIMENSION) compptr->h_samp_factor, FALSE); |
429 |
} else { |
430 |
/* Edge blocks are transposed but not mirrored. */ |
431 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
432 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
433 |
dst_blk_x + x_crop_blocks, |
434 |
(JDIMENSION) compptr->h_samp_factor, FALSE); |
435 |
} |
436 |
for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) { |
437 |
dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x]; |
438 |
if (x_crop_blocks + dst_blk_x < comp_width) { |
439 |
/* Block is within the mirrorable area. */ |
440 |
src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1] |
441 |
[dst_blk_y + offset_y + y_crop_blocks]; |
442 |
for (i = 0; i < DCTSIZE; i++) { |
443 |
for (j = 0; j < DCTSIZE; j++) |
444 |
dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; |
445 |
i++; |
446 |
for (j = 0; j < DCTSIZE; j++) |
447 |
dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; |
448 |
} |
449 |
} else { |
450 |
/* Edge blocks are transposed but not mirrored. */ |
451 |
src_ptr = src_buffer[offset_x] |
452 |
[dst_blk_y + offset_y + y_crop_blocks]; |
453 |
for (i = 0; i < DCTSIZE; i++) |
454 |
for (j = 0; j < DCTSIZE; j++) |
455 |
dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; |
456 |
} |
457 |
} |
458 |
} |
459 |
} |
460 |
} |
461 |
} |
462 |
} |
463 |
|
464 |
|
465 |
LOCAL(void) |
466 |
do_rot_270 (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 |
/* 270 degree rotation is equivalent to |
471 |
* 1. Horizontal mirroring; |
472 |
* 2. Transposing the image. |
473 |
* These two steps are merged into a single processing routine. |
474 |
*/ |
475 |
{ |
476 |
JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y; |
477 |
JDIMENSION x_crop_blocks, y_crop_blocks; |
478 |
int ci, i, j, offset_x, offset_y; |
479 |
JBLOCKARRAY src_buffer, dst_buffer; |
480 |
JCOEFPTR src_ptr, dst_ptr; |
481 |
jpeg_component_info *compptr; |
482 |
|
483 |
/* Because of the horizontal mirror step, we can't process partial iMCUs |
484 |
* at the (output) bottom edge properly. They just get transposed and |
485 |
* not mirrored. |
486 |
*/ |
487 |
MCU_rows = srcinfo->output_width / |
488 |
(dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size); |
489 |
|
490 |
for (ci = 0; ci < dstinfo->num_components; ci++) { |
491 |
compptr = dstinfo->comp_info + ci; |
492 |
comp_height = MCU_rows * compptr->v_samp_factor; |
493 |
x_crop_blocks = x_crop_offset * compptr->h_samp_factor; |
494 |
y_crop_blocks = y_crop_offset * compptr->v_samp_factor; |
495 |
for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; |
496 |
dst_blk_y += compptr->v_samp_factor) { |
497 |
dst_buffer = (*srcinfo->mem->access_virt_barray) |
498 |
((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, |
499 |
(JDIMENSION) compptr->v_samp_factor, TRUE); |
500 |
for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { |
501 |
for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; |
502 |
dst_blk_x += compptr->h_samp_factor) { |
503 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
504 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
505 |
dst_blk_x + x_crop_blocks, |
506 |
(JDIMENSION) compptr->h_samp_factor, FALSE); |
507 |
for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) { |
508 |
dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x]; |
509 |
if (y_crop_blocks + dst_blk_y < comp_height) { |
510 |
/* Block is within the mirrorable area. */ |
511 |
src_ptr = src_buffer[offset_x] |
512 |
[comp_height - y_crop_blocks - dst_blk_y - offset_y - 1]; |
513 |
for (i = 0; i < DCTSIZE; i++) { |
514 |
for (j = 0; j < DCTSIZE; j++) { |
515 |
dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; |
516 |
j++; |
517 |
dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; |
518 |
} |
519 |
} |
520 |
} else { |
521 |
/* Edge blocks are transposed but not mirrored. */ |
522 |
src_ptr = src_buffer[offset_x] |
523 |
[dst_blk_y + offset_y + y_crop_blocks]; |
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]; |
527 |
} |
528 |
} |
529 |
} |
530 |
} |
531 |
} |
532 |
} |
533 |
} |
534 |
|
535 |
|
536 |
LOCAL(void) |
537 |
do_rot_180 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
538 |
JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, |
539 |
jvirt_barray_ptr *src_coef_arrays, |
540 |
jvirt_barray_ptr *dst_coef_arrays) |
541 |
/* 180 degree rotation is equivalent to |
542 |
* 1. Vertical mirroring; |
543 |
* 2. Horizontal mirroring. |
544 |
* These two steps are merged into a single processing routine. |
545 |
*/ |
546 |
{ |
547 |
JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y; |
548 |
JDIMENSION x_crop_blocks, y_crop_blocks; |
549 |
int ci, i, j, offset_y; |
550 |
JBLOCKARRAY src_buffer, dst_buffer; |
551 |
JBLOCKROW src_row_ptr, dst_row_ptr; |
552 |
JCOEFPTR src_ptr, dst_ptr; |
553 |
jpeg_component_info *compptr; |
554 |
|
555 |
MCU_cols = srcinfo->output_width / |
556 |
(dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size); |
557 |
MCU_rows = srcinfo->output_height / |
558 |
(dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size); |
559 |
|
560 |
for (ci = 0; ci < dstinfo->num_components; ci++) { |
561 |
compptr = dstinfo->comp_info + ci; |
562 |
comp_width = MCU_cols * compptr->h_samp_factor; |
563 |
comp_height = MCU_rows * compptr->v_samp_factor; |
564 |
x_crop_blocks = x_crop_offset * compptr->h_samp_factor; |
565 |
y_crop_blocks = y_crop_offset * compptr->v_samp_factor; |
566 |
for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; |
567 |
dst_blk_y += compptr->v_samp_factor) { |
568 |
dst_buffer = (*srcinfo->mem->access_virt_barray) |
569 |
((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, |
570 |
(JDIMENSION) compptr->v_samp_factor, TRUE); |
571 |
if (y_crop_blocks + dst_blk_y < comp_height) { |
572 |
/* Row is within the vertically mirrorable area. */ |
573 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
574 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
575 |
comp_height - y_crop_blocks - dst_blk_y - |
576 |
(JDIMENSION) compptr->v_samp_factor, |
577 |
(JDIMENSION) compptr->v_samp_factor, FALSE); |
578 |
} else { |
579 |
/* Bottom-edge rows are only mirrored horizontally. */ |
580 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
581 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
582 |
dst_blk_y + y_crop_blocks, |
583 |
(JDIMENSION) compptr->v_samp_factor, FALSE); |
584 |
} |
585 |
for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { |
586 |
dst_row_ptr = dst_buffer[offset_y]; |
587 |
if (y_crop_blocks + dst_blk_y < comp_height) { |
588 |
/* Row is within the mirrorable area. */ |
589 |
src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1]; |
590 |
for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) { |
591 |
dst_ptr = dst_row_ptr[dst_blk_x]; |
592 |
if (x_crop_blocks + dst_blk_x < comp_width) { |
593 |
/* Process the blocks that can be mirrored both ways. */ |
594 |
src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1]; |
595 |
for (i = 0; i < DCTSIZE; i += 2) { |
596 |
/* For even row, negate every odd column. */ |
597 |
for (j = 0; j < DCTSIZE; j += 2) { |
598 |
*dst_ptr++ = *src_ptr++; |
599 |
*dst_ptr++ = - *src_ptr++; |
600 |
} |
601 |
/* For odd row, negate every even column. */ |
602 |
for (j = 0; j < DCTSIZE; j += 2) { |
603 |
*dst_ptr++ = - *src_ptr++; |
604 |
*dst_ptr++ = *src_ptr++; |
605 |
} |
606 |
} |
607 |
} else { |
608 |
/* Any remaining right-edge blocks are only mirrored vertically. */ |
609 |
src_ptr = src_row_ptr[x_crop_blocks + dst_blk_x]; |
610 |
for (i = 0; i < DCTSIZE; i += 2) { |
611 |
for (j = 0; j < DCTSIZE; j++) |
612 |
*dst_ptr++ = *src_ptr++; |
613 |
for (j = 0; j < DCTSIZE; j++) |
614 |
*dst_ptr++ = - *src_ptr++; |
615 |
} |
616 |
} |
617 |
} |
618 |
} else { |
619 |
/* Remaining rows are just mirrored horizontally. */ |
620 |
src_row_ptr = src_buffer[offset_y]; |
621 |
for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) { |
622 |
if (x_crop_blocks + dst_blk_x < comp_width) { |
623 |
/* Process the blocks that can be mirrored. */ |
624 |
dst_ptr = dst_row_ptr[dst_blk_x]; |
625 |
src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1]; |
626 |
for (i = 0; i < DCTSIZE2; i += 2) { |
627 |
*dst_ptr++ = *src_ptr++; |
628 |
*dst_ptr++ = - *src_ptr++; |
629 |
} |
630 |
} else { |
631 |
/* Any remaining right-edge blocks are only copied. */ |
632 |
jcopy_block_row(src_row_ptr + dst_blk_x + x_crop_blocks, |
633 |
dst_row_ptr + dst_blk_x, |
634 |
(JDIMENSION) 1); |
635 |
} |
636 |
} |
637 |
} |
638 |
} |
639 |
} |
640 |
} |
641 |
} |
642 |
|
643 |
|
644 |
LOCAL(void) |
645 |
do_transverse (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
646 |
JDIMENSION x_crop_offset, JDIMENSION y_crop_offset, |
647 |
jvirt_barray_ptr *src_coef_arrays, |
648 |
jvirt_barray_ptr *dst_coef_arrays) |
649 |
/* Transverse transpose is equivalent to |
650 |
* 1. 180 degree rotation; |
651 |
* 2. Transposition; |
652 |
* or |
653 |
* 1. Horizontal mirroring; |
654 |
* 2. Transposition; |
655 |
* 3. Horizontal mirroring. |
656 |
* These steps are merged into a single processing routine. |
657 |
*/ |
658 |
{ |
659 |
JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y; |
660 |
JDIMENSION x_crop_blocks, y_crop_blocks; |
661 |
int ci, i, j, offset_x, offset_y; |
662 |
JBLOCKARRAY src_buffer, dst_buffer; |
663 |
JCOEFPTR src_ptr, dst_ptr; |
664 |
jpeg_component_info *compptr; |
665 |
|
666 |
MCU_cols = srcinfo->output_height / |
667 |
(dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size); |
668 |
MCU_rows = srcinfo->output_width / |
669 |
(dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size); |
670 |
|
671 |
for (ci = 0; ci < dstinfo->num_components; ci++) { |
672 |
compptr = dstinfo->comp_info + ci; |
673 |
comp_width = MCU_cols * compptr->h_samp_factor; |
674 |
comp_height = MCU_rows * compptr->v_samp_factor; |
675 |
x_crop_blocks = x_crop_offset * compptr->h_samp_factor; |
676 |
y_crop_blocks = y_crop_offset * compptr->v_samp_factor; |
677 |
for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks; |
678 |
dst_blk_y += compptr->v_samp_factor) { |
679 |
dst_buffer = (*srcinfo->mem->access_virt_barray) |
680 |
((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y, |
681 |
(JDIMENSION) compptr->v_samp_factor, TRUE); |
682 |
for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) { |
683 |
for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; |
684 |
dst_blk_x += compptr->h_samp_factor) { |
685 |
if (x_crop_blocks + dst_blk_x < comp_width) { |
686 |
/* Block is within the mirrorable area. */ |
687 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
688 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
689 |
comp_width - x_crop_blocks - dst_blk_x - |
690 |
(JDIMENSION) compptr->h_samp_factor, |
691 |
(JDIMENSION) compptr->h_samp_factor, FALSE); |
692 |
} else { |
693 |
src_buffer = (*srcinfo->mem->access_virt_barray) |
694 |
((j_common_ptr) srcinfo, src_coef_arrays[ci], |
695 |
dst_blk_x + x_crop_blocks, |
696 |
(JDIMENSION) compptr->h_samp_factor, FALSE); |
697 |
} |
698 |
for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) { |
699 |
dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x]; |
700 |
if (y_crop_blocks + dst_blk_y < comp_height) { |
701 |
if (x_crop_blocks + dst_blk_x < comp_width) { |
702 |
/* Block is within the mirrorable area. */ |
703 |
src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1] |
704 |
[comp_height - y_crop_blocks - dst_blk_y - offset_y - 1]; |
705 |
for (i = 0; i < DCTSIZE; i++) { |
706 |
for (j = 0; j < DCTSIZE; j++) { |
707 |
dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; |
708 |
j++; |
709 |
dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; |
710 |
} |
711 |
i++; |
712 |
for (j = 0; j < DCTSIZE; j++) { |
713 |
dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; |
714 |
j++; |
715 |
dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; |
716 |
} |
717 |
} |
718 |
} else { |
719 |
/* Right-edge blocks are mirrored in y only */ |
720 |
src_ptr = src_buffer[offset_x] |
721 |
[comp_height - y_crop_blocks - dst_blk_y - offset_y - 1]; |
722 |
for (i = 0; i < DCTSIZE; i++) { |
723 |
for (j = 0; j < DCTSIZE; j++) { |
724 |
dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; |
725 |
j++; |
726 |
dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; |
727 |
} |
728 |
} |
729 |
} |
730 |
} else { |
731 |
if (x_crop_blocks + dst_blk_x < comp_width) { |
732 |
/* Bottom-edge blocks are mirrored in x only */ |
733 |
src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1] |
734 |
[dst_blk_y + offset_y + y_crop_blocks]; |
735 |
for (i = 0; i < DCTSIZE; i++) { |
736 |
for (j = 0; j < DCTSIZE; j++) |
737 |
dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; |
738 |
i++; |
739 |
for (j = 0; j < DCTSIZE; j++) |
740 |
dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j]; |
741 |
} |
742 |
} else { |
743 |
/* At lower right corner, just transpose, no mirroring */ |
744 |
src_ptr = src_buffer[offset_x] |
745 |
[dst_blk_y + offset_y + y_crop_blocks]; |
746 |
for (i = 0; i < DCTSIZE; i++) |
747 |
for (j = 0; j < DCTSIZE; j++) |
748 |
dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j]; |
749 |
} |
750 |
} |
751 |
} |
752 |
} |
753 |
} |
754 |
} |
755 |
} |
756 |
} |
757 |
|
758 |
|
759 |
/* Parse an unsigned integer: subroutine for jtransform_parse_crop_spec. |
760 |
* Returns TRUE if valid integer found, FALSE if not. |
761 |
* *strptr is advanced over the digit string, and *result is set to its value. |
762 |
*/ |
763 |
|
764 |
LOCAL(boolean) |
765 |
jt_read_integer (const char ** strptr, JDIMENSION * result) |
766 |
{ |
767 |
const char * ptr = *strptr; |
768 |
JDIMENSION val = 0; |
769 |
|
770 |
for (; isdigit(*ptr); ptr++) { |
771 |
val = val * 10 + (JDIMENSION) (*ptr - '0'); |
772 |
} |
773 |
*result = val; |
774 |
if (ptr == *strptr) |
775 |
return FALSE; /* oops, no digits */ |
776 |
*strptr = ptr; |
777 |
return TRUE; |
778 |
} |
779 |
|
780 |
|
781 |
/* Parse a crop specification (written in X11 geometry style). |
782 |
* The routine returns TRUE if the spec string is valid, FALSE if not. |
783 |
* |
784 |
* The crop spec string should have the format |
785 |
* <width>x<height>{+-}<xoffset>{+-}<yoffset> |
786 |
* where width, height, xoffset, and yoffset are unsigned integers. |
787 |
* Each of the elements can be omitted to indicate a default value. |
788 |
* (A weakness of this style is that it is not possible to omit xoffset |
789 |
* while specifying yoffset, since they look alike.) |
790 |
* |
791 |
* This code is loosely based on XParseGeometry from the X11 distribution. |
792 |
*/ |
793 |
|
794 |
GLOBAL(boolean) |
795 |
jtransform_parse_crop_spec (jpeg_transform_info *info, const char *spec) |
796 |
{ |
797 |
info->crop = FALSE; |
798 |
info->crop_width_set = JCROP_UNSET; |
799 |
info->crop_height_set = JCROP_UNSET; |
800 |
info->crop_xoffset_set = JCROP_UNSET; |
801 |
info->crop_yoffset_set = JCROP_UNSET; |
802 |
|
803 |
if (isdigit(*spec)) { |
804 |
/* fetch width */ |
805 |
if (! jt_read_integer(&spec, &info->crop_width)) |
806 |
return FALSE; |
807 |
info->crop_width_set = JCROP_POS; |
808 |
} |
809 |
if (*spec == 'x' || *spec == 'X') { |
810 |
/* fetch height */ |
811 |
spec++; |
812 |
if (! jt_read_integer(&spec, &info->crop_height)) |
813 |
return FALSE; |
814 |
info->crop_height_set = JCROP_POS; |
815 |
} |
816 |
if (*spec == '+' || *spec == '-') { |
817 |
/* fetch xoffset */ |
818 |
info->crop_xoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS; |
819 |
spec++; |
820 |
if (! jt_read_integer(&spec, &info->crop_xoffset)) |
821 |
return FALSE; |
822 |
} |
823 |
if (*spec == '+' || *spec == '-') { |
824 |
/* fetch yoffset */ |
825 |
info->crop_yoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS; |
826 |
spec++; |
827 |
if (! jt_read_integer(&spec, &info->crop_yoffset)) |
828 |
return FALSE; |
829 |
} |
830 |
/* We had better have gotten to the end of the string. */ |
831 |
if (*spec != '\0') |
832 |
return FALSE; |
833 |
info->crop = TRUE; |
834 |
return TRUE; |
835 |
} |
836 |
|
837 |
|
838 |
/* Trim off any partial iMCUs on the indicated destination edge */ |
839 |
|
840 |
LOCAL(void) |
841 |
trim_right_edge (jpeg_transform_info *info, JDIMENSION full_width) |
842 |
{ |
843 |
JDIMENSION MCU_cols; |
844 |
|
845 |
MCU_cols = info->output_width / info->iMCU_sample_width; |
846 |
if (MCU_cols > 0 && info->x_crop_offset + MCU_cols == |
847 |
full_width / info->iMCU_sample_width) |
848 |
info->output_width = MCU_cols * info->iMCU_sample_width; |
849 |
} |
850 |
|
851 |
LOCAL(void) |
852 |
trim_bottom_edge (jpeg_transform_info *info, JDIMENSION full_height) |
853 |
{ |
854 |
JDIMENSION MCU_rows; |
855 |
|
856 |
MCU_rows = info->output_height / info->iMCU_sample_height; |
857 |
if (MCU_rows > 0 && info->y_crop_offset + MCU_rows == |
858 |
full_height / info->iMCU_sample_height) |
859 |
info->output_height = MCU_rows * info->iMCU_sample_height; |
860 |
} |
861 |
|
862 |
|
863 |
/* Request any required workspace. |
864 |
* |
865 |
* This routine figures out the size that the output image will be |
866 |
* (which implies that all the transform parameters must be set before |
867 |
* it is called). |
868 |
* |
869 |
* We allocate the workspace virtual arrays from the source decompression |
870 |
* object, so that all the arrays (both the original data and the workspace) |
871 |
* will be taken into account while making memory management decisions. |
872 |
* Hence, this routine must be called after jpeg_read_header (which reads |
873 |
* the image dimensions) and before jpeg_read_coefficients (which realizes |
874 |
* the source's virtual arrays). |
875 |
* |
876 |
* This function returns FALSE right away if -perfect is given |
877 |
* and transformation is not perfect. Otherwise returns TRUE. |
878 |
*/ |
879 |
|
880 |
GLOBAL(boolean) |
881 |
jtransform_request_workspace (j_decompress_ptr srcinfo, |
882 |
jpeg_transform_info *info) |
883 |
{ |
884 |
jvirt_barray_ptr *coef_arrays; |
885 |
boolean need_workspace, transpose_it; |
886 |
jpeg_component_info *compptr; |
887 |
JDIMENSION xoffset, yoffset; |
888 |
JDIMENSION width_in_iMCUs, height_in_iMCUs; |
889 |
JDIMENSION width_in_blocks, height_in_blocks; |
890 |
int ci, h_samp_factor, v_samp_factor; |
891 |
|
892 |
/* Determine number of components in output image */ |
893 |
if (info->force_grayscale && |
894 |
srcinfo->jpeg_color_space == JCS_YCbCr && |
895 |
srcinfo->num_components == 3) |
896 |
/* We'll only process the first component */ |
897 |
info->num_components = 1; |
898 |
else |
899 |
/* Process all the components */ |
900 |
info->num_components = srcinfo->num_components; |
901 |
|
902 |
/* Compute output image dimensions and related values. */ |
903 |
jpeg_core_output_dimensions(srcinfo); |
904 |
|
905 |
/* Return right away if -perfect is given and transformation is not perfect. |
906 |
*/ |
907 |
if (info->perfect) { |
908 |
if (info->num_components == 1) { |
909 |
if (!jtransform_perfect_transform(srcinfo->output_width, |
910 |
srcinfo->output_height, |
911 |
srcinfo->min_DCT_h_scaled_size, |
912 |
srcinfo->min_DCT_v_scaled_size, |
913 |
info->transform)) |
914 |
return FALSE; |
915 |
} else { |
916 |
if (!jtransform_perfect_transform(srcinfo->output_width, |
917 |
srcinfo->output_height, |
918 |
srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size, |
919 |
srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size, |
920 |
info->transform)) |
921 |
return FALSE; |
922 |
} |
923 |
} |
924 |
|
925 |
/* If there is only one output component, force the iMCU size to be 1; |
926 |
* else use the source iMCU size. (This allows us to do the right thing |
927 |
* when reducing color to grayscale, and also provides a handy way of |
928 |
* cleaning up "funny" grayscale images whose sampling factors are not 1x1.) |
929 |
*/ |
930 |
switch (info->transform) { |
931 |
case JXFORM_TRANSPOSE: |
932 |
case JXFORM_TRANSVERSE: |
933 |
case JXFORM_ROT_90: |
934 |
case JXFORM_ROT_270: |
935 |
info->output_width = srcinfo->output_height; |
936 |
info->output_height = srcinfo->output_width; |
937 |
if (info->num_components == 1) { |
938 |
info->iMCU_sample_width = srcinfo->min_DCT_v_scaled_size; |
939 |
info->iMCU_sample_height = srcinfo->min_DCT_h_scaled_size; |
940 |
} else { |
941 |
info->iMCU_sample_width = |
942 |
srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size; |
943 |
info->iMCU_sample_height = |
944 |
srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size; |
945 |
} |
946 |
break; |
947 |
default: |
948 |
info->output_width = srcinfo->output_width; |
949 |
info->output_height = srcinfo->output_height; |
950 |
if (info->num_components == 1) { |
951 |
info->iMCU_sample_width = srcinfo->min_DCT_h_scaled_size; |
952 |
info->iMCU_sample_height = srcinfo->min_DCT_v_scaled_size; |
953 |
} else { |
954 |
info->iMCU_sample_width = |
955 |
srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size; |
956 |
info->iMCU_sample_height = |
957 |
srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size; |
958 |
} |
959 |
break; |
960 |
} |
961 |
|
962 |
/* If cropping has been requested, compute the crop area's position and |
963 |
* dimensions, ensuring that its upper left corner falls at an iMCU boundary. |
964 |
*/ |
965 |
if (info->crop) { |
966 |
/* Insert default values for unset crop parameters */ |
967 |
if (info->crop_xoffset_set == JCROP_UNSET) |
968 |
info->crop_xoffset = 0; /* default to +0 */ |
969 |
if (info->crop_yoffset_set == JCROP_UNSET) |
970 |
info->crop_yoffset = 0; /* default to +0 */ |
971 |
if (info->crop_xoffset >= info->output_width || |
972 |
info->crop_yoffset >= info->output_height) |
973 |
ERREXIT(srcinfo, JERR_BAD_CROP_SPEC); |
974 |
if (info->crop_width_set == JCROP_UNSET) |
975 |
info->crop_width = info->output_width - info->crop_xoffset; |
976 |
if (info->crop_height_set == JCROP_UNSET) |
977 |
info->crop_height = info->output_height - info->crop_yoffset; |
978 |
/* Ensure parameters are valid */ |
979 |
if (info->crop_width <= 0 || info->crop_width > info->output_width || |
980 |
info->crop_height <= 0 || info->crop_height > info->output_height || |
981 |
info->crop_xoffset > info->output_width - info->crop_width || |
982 |
info->crop_yoffset > info->output_height - info->crop_height) |
983 |
ERREXIT(srcinfo, JERR_BAD_CROP_SPEC); |
984 |
/* Convert negative crop offsets into regular offsets */ |
985 |
if (info->crop_xoffset_set == JCROP_NEG) |
986 |
xoffset = info->output_width - info->crop_width - info->crop_xoffset; |
987 |
else |
988 |
xoffset = info->crop_xoffset; |
989 |
if (info->crop_yoffset_set == JCROP_NEG) |
990 |
yoffset = info->output_height - info->crop_height - info->crop_yoffset; |
991 |
else |
992 |
yoffset = info->crop_yoffset; |
993 |
/* Now adjust so that upper left corner falls at an iMCU boundary */ |
994 |
info->output_width = |
995 |
info->crop_width + (xoffset % info->iMCU_sample_width); |
996 |
info->output_height = |
997 |
info->crop_height + (yoffset % info->iMCU_sample_height); |
998 |
/* Save x/y offsets measured in iMCUs */ |
999 |
info->x_crop_offset = xoffset / info->iMCU_sample_width; |
1000 |
info->y_crop_offset = yoffset / info->iMCU_sample_height; |
1001 |
} else { |
1002 |
info->x_crop_offset = 0; |
1003 |
info->y_crop_offset = 0; |
1004 |
} |
1005 |
|
1006 |
/* Figure out whether we need workspace arrays, |
1007 |
* and if so whether they are transposed relative to the source. |
1008 |
*/ |
1009 |
need_workspace = FALSE; |
1010 |
transpose_it = FALSE; |
1011 |
switch (info->transform) { |
1012 |
case JXFORM_NONE: |
1013 |
if (info->x_crop_offset != 0 || info->y_crop_offset != 0) |
1014 |
need_workspace = TRUE; |
1015 |
/* No workspace needed if neither cropping nor transforming */ |
1016 |
break; |
1017 |
case JXFORM_FLIP_H: |
1018 |
if (info->trim) |
1019 |
trim_right_edge(info, srcinfo->output_width); |
1020 |
if (info->y_crop_offset != 0) |
1021 |
need_workspace = TRUE; |
1022 |
/* do_flip_h_no_crop doesn't need a workspace array */ |
1023 |
break; |
1024 |
case JXFORM_FLIP_V: |
1025 |
if (info->trim) |
1026 |
trim_bottom_edge(info, srcinfo->output_height); |
1027 |
/* Need workspace arrays having same dimensions as source image. */ |
1028 |
need_workspace = TRUE; |
1029 |
break; |
1030 |
case JXFORM_TRANSPOSE: |
1031 |
/* transpose does NOT have to trim anything */ |
1032 |
/* Need workspace arrays having transposed dimensions. */ |
1033 |
need_workspace = TRUE; |
1034 |
transpose_it = TRUE; |
1035 |
break; |
1036 |
case JXFORM_TRANSVERSE: |
1037 |
if (info->trim) { |
1038 |
trim_right_edge(info, srcinfo->output_height); |
1039 |
trim_bottom_edge(info, srcinfo->output_width); |
1040 |
} |
1041 |
/* Need workspace arrays having transposed dimensions. */ |
1042 |
need_workspace = TRUE; |
1043 |
transpose_it = TRUE; |
1044 |
break; |
1045 |
case JXFORM_ROT_90: |
1046 |
if (info->trim) |
1047 |
trim_right_edge(info, srcinfo->output_height); |
1048 |
/* Need workspace arrays having transposed dimensions. */ |
1049 |
need_workspace = TRUE; |
1050 |
transpose_it = TRUE; |
1051 |
break; |
1052 |
case JXFORM_ROT_180: |
1053 |
if (info->trim) { |
1054 |
trim_right_edge(info, srcinfo->output_width); |
1055 |
trim_bottom_edge(info, srcinfo->output_height); |
1056 |
} |
1057 |
/* Need workspace arrays having same dimensions as source image. */ |
1058 |
need_workspace = TRUE; |
1059 |
break; |
1060 |
case JXFORM_ROT_270: |
1061 |
if (info->trim) |
1062 |
trim_bottom_edge(info, srcinfo->output_width); |
1063 |
/* Need workspace arrays having transposed dimensions. */ |
1064 |
need_workspace = TRUE; |
1065 |
transpose_it = TRUE; |
1066 |
break; |
1067 |
} |
1068 |
|
1069 |
/* Allocate workspace if needed. |
1070 |
* Note that we allocate arrays padded out to the next iMCU boundary, |
1071 |
* so that transform routines need not worry about missing edge blocks. |
1072 |
*/ |
1073 |
if (need_workspace) { |
1074 |
coef_arrays = (jvirt_barray_ptr *) |
1075 |
(*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE, |
1076 |
SIZEOF(jvirt_barray_ptr) * info->num_components); |
1077 |
width_in_iMCUs = (JDIMENSION) |
1078 |
jdiv_round_up((long) info->output_width, |
1079 |
(long) info->iMCU_sample_width); |
1080 |
height_in_iMCUs = (JDIMENSION) |
1081 |
jdiv_round_up((long) info->output_height, |
1082 |
(long) info->iMCU_sample_height); |
1083 |
for (ci = 0; ci < info->num_components; ci++) { |
1084 |
compptr = srcinfo->comp_info + ci; |
1085 |
if (info->num_components == 1) { |
1086 |
/* we're going to force samp factors to 1x1 in this case */ |
1087 |
h_samp_factor = v_samp_factor = 1; |
1088 |
} else if (transpose_it) { |
1089 |
h_samp_factor = compptr->v_samp_factor; |
1090 |
v_samp_factor = compptr->h_samp_factor; |
1091 |
} else { |
1092 |
h_samp_factor = compptr->h_samp_factor; |
1093 |
v_samp_factor = compptr->v_samp_factor; |
1094 |
} |
1095 |
width_in_blocks = width_in_iMCUs * h_samp_factor; |
1096 |
height_in_blocks = height_in_iMCUs * v_samp_factor; |
1097 |
coef_arrays[ci] = (*srcinfo->mem->request_virt_barray) |
1098 |
((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE, |
1099 |
width_in_blocks, height_in_blocks, (JDIMENSION) v_samp_factor); |
1100 |
} |
1101 |
info->workspace_coef_arrays = coef_arrays; |
1102 |
} else |
1103 |
info->workspace_coef_arrays = NULL; |
1104 |
|
1105 |
return TRUE; |
1106 |
} |
1107 |
|
1108 |
|
1109 |
/* Transpose destination image parameters */ |
1110 |
|
1111 |
LOCAL(void) |
1112 |
transpose_critical_parameters (j_compress_ptr dstinfo) |
1113 |
{ |
1114 |
int tblno, i, j, ci, itemp; |
1115 |
jpeg_component_info *compptr; |
1116 |
JQUANT_TBL *qtblptr; |
1117 |
JDIMENSION jtemp; |
1118 |
UINT16 qtemp; |
1119 |
|
1120 |
/* Transpose image dimensions */ |
1121 |
jtemp = dstinfo->image_width; |
1122 |
dstinfo->image_width = dstinfo->image_height; |
1123 |
dstinfo->image_height = jtemp; |
1124 |
itemp = dstinfo->min_DCT_h_scaled_size; |
1125 |
dstinfo->min_DCT_h_scaled_size = dstinfo->min_DCT_v_scaled_size; |
1126 |
dstinfo->min_DCT_v_scaled_size = itemp; |
1127 |
|
1128 |
/* Transpose sampling factors */ |
1129 |
for (ci = 0; ci < dstinfo->num_components; ci++) { |
1130 |
compptr = dstinfo->comp_info + ci; |
1131 |
itemp = compptr->h_samp_factor; |
1132 |
compptr->h_samp_factor = compptr->v_samp_factor; |
1133 |
compptr->v_samp_factor = itemp; |
1134 |
} |
1135 |
|
1136 |
/* Transpose quantization tables */ |
1137 |
for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { |
1138 |
qtblptr = dstinfo->quant_tbl_ptrs[tblno]; |
1139 |
if (qtblptr != NULL) { |
1140 |
for (i = 0; i < DCTSIZE; i++) { |
1141 |
for (j = 0; j < i; j++) { |
1142 |
qtemp = qtblptr->quantval[i*DCTSIZE+j]; |
1143 |
qtblptr->quantval[i*DCTSIZE+j] = qtblptr->quantval[j*DCTSIZE+i]; |
1144 |
qtblptr->quantval[j*DCTSIZE+i] = qtemp; |
1145 |
} |
1146 |
} |
1147 |
} |
1148 |
} |
1149 |
} |
1150 |
|
1151 |
|
1152 |
/* Adjust Exif image parameters. |
1153 |
* |
1154 |
* We try to adjust the Tags ExifImageWidth and ExifImageHeight if possible. |
1155 |
*/ |
1156 |
|
1157 |
LOCAL(void) |
1158 |
adjust_exif_parameters (JOCTET FAR * data, unsigned int length, |
1159 |
JDIMENSION new_width, JDIMENSION new_height) |
1160 |
{ |
1161 |
boolean is_motorola; /* Flag for byte order */ |
1162 |
unsigned int number_of_tags, tagnum; |
1163 |
unsigned int firstoffset, offset; |
1164 |
JDIMENSION new_value; |
1165 |
|
1166 |
if (length < 12) return; /* Length of an IFD entry */ |
1167 |
|
1168 |
/* Discover byte order */ |
1169 |
if (GETJOCTET(data[0]) == 0x49 && GETJOCTET(data[1]) == 0x49) |
1170 |
is_motorola = FALSE; |
1171 |
else if (GETJOCTET(data[0]) == 0x4D && GETJOCTET(data[1]) == 0x4D) |
1172 |
is_motorola = TRUE; |
1173 |
else |
1174 |
return; |
1175 |
|
1176 |
/* Check Tag Mark */ |
1177 |
if (is_motorola) { |
1178 |
if (GETJOCTET(data[2]) != 0) return; |
1179 |
if (GETJOCTET(data[3]) != 0x2A) return; |
1180 |
} else { |
1181 |
if (GETJOCTET(data[3]) != 0) return; |
1182 |
if (GETJOCTET(data[2]) != 0x2A) return; |
1183 |
} |
1184 |
|
1185 |
/* Get first IFD offset (offset to IFD0) */ |
1186 |
if (is_motorola) { |
1187 |
if (GETJOCTET(data[4]) != 0) return; |
1188 |
if (GETJOCTET(data[5]) != 0) return; |
1189 |
firstoffset = GETJOCTET(data[6]); |
1190 |
firstoffset <<= 8; |
1191 |
firstoffset += GETJOCTET(data[7]); |
1192 |
} else { |
1193 |
if (GETJOCTET(data[7]) != 0) return; |
1194 |
if (GETJOCTET(data[6]) != 0) return; |
1195 |
firstoffset = GETJOCTET(data[5]); |
1196 |
firstoffset <<= 8; |
1197 |
firstoffset += GETJOCTET(data[4]); |
1198 |
} |
1199 |
if (firstoffset > length - 2) return; /* check end of data segment */ |
1200 |
|
1201 |
/* Get the number of directory entries contained in this IFD */ |
1202 |
if (is_motorola) { |
1203 |
number_of_tags = GETJOCTET(data[firstoffset]); |
1204 |
number_of_tags <<= 8; |
1205 |
number_of_tags += GETJOCTET(data[firstoffset+1]); |
1206 |
} else { |
1207 |
number_of_tags = GETJOCTET(data[firstoffset+1]); |
1208 |
number_of_tags <<= 8; |
1209 |
number_of_tags += GETJOCTET(data[firstoffset]); |
1210 |
} |
1211 |
if (number_of_tags == 0) return; |
1212 |
firstoffset += 2; |
1213 |
|
1214 |
/* Search for ExifSubIFD offset Tag in IFD0 */ |
1215 |
for (;;) { |
1216 |
if (firstoffset > length - 12) return; /* check end of data segment */ |
1217 |
/* Get Tag number */ |
1218 |
if (is_motorola) { |
1219 |
tagnum = GETJOCTET(data[firstoffset]); |
1220 |
tagnum <<= 8; |
1221 |
tagnum += GETJOCTET(data[firstoffset+1]); |
1222 |
} else { |
1223 |
tagnum = GETJOCTET(data[firstoffset+1]); |
1224 |
tagnum <<= 8; |
1225 |
tagnum += GETJOCTET(data[firstoffset]); |
1226 |
} |
1227 |
if (tagnum == 0x8769) break; /* found ExifSubIFD offset Tag */ |
1228 |
if (--number_of_tags == 0) return; |
1229 |
firstoffset += 12; |
1230 |
} |
1231 |
|
1232 |
/* Get the ExifSubIFD offset */ |
1233 |
if (is_motorola) { |
1234 |
if (GETJOCTET(data[firstoffset+8]) != 0) return; |
1235 |
if (GETJOCTET(data[firstoffset+9]) != 0) return; |
1236 |
offset = GETJOCTET(data[firstoffset+10]); |
1237 |
offset <<= 8; |
1238 |
offset += GETJOCTET(data[firstoffset+11]); |
1239 |
} else { |
1240 |
if (GETJOCTET(data[firstoffset+11]) != 0) return; |
1241 |
if (GETJOCTET(data[firstoffset+10]) != 0) return; |
1242 |
offset = GETJOCTET(data[firstoffset+9]); |
1243 |
offset <<= 8; |
1244 |
offset += GETJOCTET(data[firstoffset+8]); |
1245 |
} |
1246 |
if (offset > length - 2) return; /* check end of data segment */ |
1247 |
|
1248 |
/* Get the number of directory entries contained in this SubIFD */ |
1249 |
if (is_motorola) { |
1250 |
number_of_tags = GETJOCTET(data[offset]); |
1251 |
number_of_tags <<= 8; |
1252 |
number_of_tags += GETJOCTET(data[offset+1]); |
1253 |
} else { |
1254 |
number_of_tags = GETJOCTET(data[offset+1]); |
1255 |
number_of_tags <<= 8; |
1256 |
number_of_tags += GETJOCTET(data[offset]); |
1257 |
} |
1258 |
if (number_of_tags < 2) return; |
1259 |
offset += 2; |
1260 |
|
1261 |
/* Search for ExifImageWidth and ExifImageHeight Tags in this SubIFD */ |
1262 |
do { |
1263 |
if (offset > length - 12) return; /* check end of data segment */ |
1264 |
/* Get Tag number */ |
1265 |
if (is_motorola) { |
1266 |
tagnum = GETJOCTET(data[offset]); |
1267 |
tagnum <<= 8; |
1268 |
tagnum += GETJOCTET(data[offset+1]); |
1269 |
} else { |
1270 |
tagnum = GETJOCTET(data[offset+1]); |
1271 |
tagnum <<= 8; |
1272 |
tagnum += GETJOCTET(data[offset]); |
1273 |
} |
1274 |
if (tagnum == 0xA002 || tagnum == 0xA003) { |
1275 |
if (tagnum == 0xA002) |
1276 |
new_value = new_width; /* ExifImageWidth Tag */ |
1277 |
else |
1278 |
new_value = new_height; /* ExifImageHeight Tag */ |
1279 |
if (is_motorola) { |
1280 |
data[offset+2] = 0; /* Format = unsigned long (4 octets) */ |
1281 |
data[offset+3] = 4; |
1282 |
data[offset+4] = 0; /* Number Of Components = 1 */ |
1283 |
data[offset+5] = 0; |
1284 |
data[offset+6] = 0; |
1285 |
data[offset+7] = 1; |
1286 |
data[offset+8] = 0; |
1287 |
data[offset+9] = 0; |
1288 |
data[offset+10] = (JOCTET)((new_value >> 8) & 0xFF); |
1289 |
data[offset+11] = (JOCTET)(new_value & 0xFF); |
1290 |
} else { |
1291 |
data[offset+2] = 4; /* Format = unsigned long (4 octets) */ |
1292 |
data[offset+3] = 0; |
1293 |
data[offset+4] = 1; /* Number Of Components = 1 */ |
1294 |
data[offset+5] = 0; |
1295 |
data[offset+6] = 0; |
1296 |
data[offset+7] = 0; |
1297 |
data[offset+8] = (JOCTET)(new_value & 0xFF); |
1298 |
data[offset+9] = (JOCTET)((new_value >> 8) & 0xFF); |
1299 |
data[offset+10] = 0; |
1300 |
data[offset+11] = 0; |
1301 |
} |
1302 |
} |
1303 |
offset += 12; |
1304 |
} while (--number_of_tags); |
1305 |
} |
1306 |
|
1307 |
|
1308 |
/* Adjust output image parameters as needed. |
1309 |
* |
1310 |
* This must be called after jpeg_copy_critical_parameters() |
1311 |
* and before jpeg_write_coefficients(). |
1312 |
* |
1313 |
* The return value is the set of virtual coefficient arrays to be written |
1314 |
* (either the ones allocated by jtransform_request_workspace, or the |
1315 |
* original source data arrays). The caller will need to pass this value |
1316 |
* to jpeg_write_coefficients(). |
1317 |
*/ |
1318 |
|
1319 |
GLOBAL(jvirt_barray_ptr *) |
1320 |
jtransform_adjust_parameters (j_decompress_ptr srcinfo, |
1321 |
j_compress_ptr dstinfo, |
1322 |
jvirt_barray_ptr *src_coef_arrays, |
1323 |
jpeg_transform_info *info) |
1324 |
{ |
1325 |
/* If force-to-grayscale is requested, adjust destination parameters */ |
1326 |
if (info->force_grayscale) { |
1327 |
/* First, ensure we have YCbCr or grayscale data, and that the source's |
1328 |
* Y channel is full resolution. (No reasonable person would make Y |
1329 |
* be less than full resolution, so actually coping with that case |
1330 |
* isn't worth extra code space. But we check it to avoid crashing.) |
1331 |
*/ |
1332 |
if (((dstinfo->jpeg_color_space == JCS_YCbCr && |
1333 |
dstinfo->num_components == 3) || |
1334 |
(dstinfo->jpeg_color_space == JCS_GRAYSCALE && |
1335 |
dstinfo->num_components == 1)) && |
1336 |
srcinfo->comp_info[0].h_samp_factor == srcinfo->max_h_samp_factor && |
1337 |
srcinfo->comp_info[0].v_samp_factor == srcinfo->max_v_samp_factor) { |
1338 |
/* We use jpeg_set_colorspace to make sure subsidiary settings get fixed |
1339 |
* properly. Among other things, it sets the target h_samp_factor & |
1340 |
* v_samp_factor to 1, which typically won't match the source. |
1341 |
* We have to preserve the source's quantization table number, however. |
1342 |
*/ |
1343 |
int sv_quant_tbl_no = dstinfo->comp_info[0].quant_tbl_no; |
1344 |
jpeg_set_colorspace(dstinfo, JCS_GRAYSCALE); |
1345 |
dstinfo->comp_info[0].quant_tbl_no = sv_quant_tbl_no; |
1346 |
} else { |
1347 |
/* Sorry, can't do it */ |
1348 |
ERREXIT(dstinfo, JERR_CONVERSION_NOTIMPL); |
1349 |
} |
1350 |
} else if (info->num_components == 1) { |
1351 |
/* For a single-component source, we force the destination sampling factors |
1352 |
* to 1x1, with or without force_grayscale. This is useful because some |
1353 |
* decoders choke on grayscale images with other sampling factors. |
1354 |
*/ |
1355 |
dstinfo->comp_info[0].h_samp_factor = 1; |
1356 |
dstinfo->comp_info[0].v_samp_factor = 1; |
1357 |
} |
1358 |
|
1359 |
/* Correct the destination's image dimensions as necessary |
1360 |
* for rotate/flip, resize, and crop operations. |
1361 |
*/ |
1362 |
dstinfo->jpeg_width = info->output_width; |
1363 |
dstinfo->jpeg_height = info->output_height; |
1364 |
|
1365 |
/* Transpose destination image parameters */ |
1366 |
switch (info->transform) { |
1367 |
case JXFORM_TRANSPOSE: |
1368 |
case JXFORM_TRANSVERSE: |
1369 |
case JXFORM_ROT_90: |
1370 |
case JXFORM_ROT_270: |
1371 |
transpose_critical_parameters(dstinfo); |
1372 |
break; |
1373 |
default: |
1374 |
break; |
1375 |
} |
1376 |
|
1377 |
/* Adjust Exif properties */ |
1378 |
if (srcinfo->marker_list != NULL && |
1379 |
srcinfo->marker_list->marker == JPEG_APP0+1 && |
1380 |
srcinfo->marker_list->data_length >= 6 && |
1381 |
GETJOCTET(srcinfo->marker_list->data[0]) == 0x45 && |
1382 |
GETJOCTET(srcinfo->marker_list->data[1]) == 0x78 && |
1383 |
GETJOCTET(srcinfo->marker_list->data[2]) == 0x69 && |
1384 |
GETJOCTET(srcinfo->marker_list->data[3]) == 0x66 && |
1385 |
GETJOCTET(srcinfo->marker_list->data[4]) == 0 && |
1386 |
GETJOCTET(srcinfo->marker_list->data[5]) == 0) { |
1387 |
/* Suppress output of JFIF marker */ |
1388 |
dstinfo->write_JFIF_header = FALSE; |
1389 |
/* Adjust Exif image parameters */ |
1390 |
if (dstinfo->jpeg_width != srcinfo->image_width || |
1391 |
dstinfo->jpeg_height != srcinfo->image_height) |
1392 |
/* Align data segment to start of TIFF structure for parsing */ |
1393 |
adjust_exif_parameters(srcinfo->marker_list->data + 6, |
1394 |
srcinfo->marker_list->data_length - 6, |
1395 |
dstinfo->jpeg_width, dstinfo->jpeg_height); |
1396 |
} |
1397 |
|
1398 |
/* Return the appropriate output data set */ |
1399 |
if (info->workspace_coef_arrays != NULL) |
1400 |
return info->workspace_coef_arrays; |
1401 |
return src_coef_arrays; |
1402 |
} |
1403 |
|
1404 |
|
1405 |
/* Execute the actual transformation, if any. |
1406 |
* |
1407 |
* This must be called *after* jpeg_write_coefficients, because it depends |
1408 |
* on jpeg_write_coefficients to have computed subsidiary values such as |
1409 |
* the per-component width and height fields in the destination object. |
1410 |
* |
1411 |
* Note that some transformations will modify the source data arrays! |
1412 |
*/ |
1413 |
|
1414 |
GLOBAL(void) |
1415 |
jtransform_execute_transform (j_decompress_ptr srcinfo, |
1416 |
j_compress_ptr dstinfo, |
1417 |
jvirt_barray_ptr *src_coef_arrays, |
1418 |
jpeg_transform_info *info) |
1419 |
{ |
1420 |
jvirt_barray_ptr *dst_coef_arrays = info->workspace_coef_arrays; |
1421 |
|
1422 |
/* Note: conditions tested here should match those in switch statement |
1423 |
* in jtransform_request_workspace() |
1424 |
*/ |
1425 |
switch (info->transform) { |
1426 |
case JXFORM_NONE: |
1427 |
if (info->x_crop_offset != 0 || info->y_crop_offset != 0) |
1428 |
do_crop(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, |
1429 |
src_coef_arrays, dst_coef_arrays); |
1430 |
break; |
1431 |
case JXFORM_FLIP_H: |
1432 |
if (info->y_crop_offset != 0) |
1433 |
do_flip_h(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, |
1434 |
src_coef_arrays, dst_coef_arrays); |
1435 |
else |
1436 |
do_flip_h_no_crop(srcinfo, dstinfo, info->x_crop_offset, |
1437 |
src_coef_arrays); |
1438 |
break; |
1439 |
case JXFORM_FLIP_V: |
1440 |
do_flip_v(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, |
1441 |
src_coef_arrays, dst_coef_arrays); |
1442 |
break; |
1443 |
case JXFORM_TRANSPOSE: |
1444 |
do_transpose(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, |
1445 |
src_coef_arrays, dst_coef_arrays); |
1446 |
break; |
1447 |
case JXFORM_TRANSVERSE: |
1448 |
do_transverse(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, |
1449 |
src_coef_arrays, dst_coef_arrays); |
1450 |
break; |
1451 |
case JXFORM_ROT_90: |
1452 |
do_rot_90(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, |
1453 |
src_coef_arrays, dst_coef_arrays); |
1454 |
break; |
1455 |
case JXFORM_ROT_180: |
1456 |
do_rot_180(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, |
1457 |
src_coef_arrays, dst_coef_arrays); |
1458 |
break; |
1459 |
case JXFORM_ROT_270: |
1460 |
do_rot_270(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset, |
1461 |
src_coef_arrays, dst_coef_arrays); |
1462 |
break; |
1463 |
} |
1464 |
} |
1465 |
|
1466 |
/* jtransform_perfect_transform |
1467 |
* |
1468 |
* Determine whether lossless transformation is perfectly |
1469 |
* possible for a specified image and transformation. |
1470 |
* |
1471 |
* Inputs: |
1472 |
* image_width, image_height: source image dimensions. |
1473 |
* MCU_width, MCU_height: pixel dimensions of MCU. |
1474 |
* transform: transformation identifier. |
1475 |
* Parameter sources from initialized jpeg_struct |
1476 |
* (after reading source header): |
1477 |
* image_width = cinfo.image_width |
1478 |
* image_height = cinfo.image_height |
1479 |
* MCU_width = cinfo.max_h_samp_factor * cinfo.block_size |
1480 |
* MCU_height = cinfo.max_v_samp_factor * cinfo.block_size |
1481 |
* Result: |
1482 |
* TRUE = perfect transformation possible |
1483 |
* FALSE = perfect transformation not possible |
1484 |
* (may use custom action then) |
1485 |
*/ |
1486 |
|
1487 |
GLOBAL(boolean) |
1488 |
jtransform_perfect_transform(JDIMENSION image_width, JDIMENSION image_height, |
1489 |
int MCU_width, int MCU_height, |
1490 |
JXFORM_CODE transform) |
1491 |
{ |
1492 |
boolean result = TRUE; /* initialize TRUE */ |
1493 |
|
1494 |
switch (transform) { |
1495 |
case JXFORM_FLIP_H: |
1496 |
case JXFORM_ROT_270: |
1497 |
if (image_width % (JDIMENSION) MCU_width) |
1498 |
result = FALSE; |
1499 |
break; |
1500 |
case JXFORM_FLIP_V: |
1501 |
case JXFORM_ROT_90: |
1502 |
if (image_height % (JDIMENSION) MCU_height) |
1503 |
result = FALSE; |
1504 |
break; |
1505 |
case JXFORM_TRANSVERSE: |
1506 |
case JXFORM_ROT_180: |
1507 |
if (image_width % (JDIMENSION) MCU_width) |
1508 |
result = FALSE; |
1509 |
if (image_height % (JDIMENSION) MCU_height) |
1510 |
result = FALSE; |
1511 |
break; |
1512 |
default: |
1513 |
break; |
1514 |
} |
1515 |
|
1516 |
return result; |
1517 |
} |
1518 |
|
1519 |
#endif /* TRANSFORMS_SUPPORTED */ |
1520 |
|
1521 |
|
1522 |
/* Setup decompression object to save desired markers in memory. |
1523 |
* This must be called before jpeg_read_header() to have the desired effect. |
1524 |
*/ |
1525 |
|
1526 |
GLOBAL(void) |
1527 |
jcopy_markers_setup (j_decompress_ptr srcinfo, JCOPY_OPTION option) |
1528 |
{ |
1529 |
#ifdef SAVE_MARKERS_SUPPORTED |
1530 |
int m; |
1531 |
|
1532 |
/* Save comments except under NONE option */ |
1533 |
if (option != JCOPYOPT_NONE) { |
1534 |
jpeg_save_markers(srcinfo, JPEG_COM, 0xFFFF); |
1535 |
} |
1536 |
/* Save all types of APPn markers iff ALL option */ |
1537 |
if (option == JCOPYOPT_ALL) { |
1538 |
for (m = 0; m < 16; m++) |
1539 |
jpeg_save_markers(srcinfo, JPEG_APP0 + m, 0xFFFF); |
1540 |
} |
1541 |
#endif /* SAVE_MARKERS_SUPPORTED */ |
1542 |
} |
1543 |
|
1544 |
/* Copy markers saved in the given source object to the destination object. |
1545 |
* This should be called just after jpeg_start_compress() or |
1546 |
* jpeg_write_coefficients(). |
1547 |
* Note that those routines will have written the SOI, and also the |
1548 |
* JFIF APP0 or Adobe APP14 markers if selected. |
1549 |
*/ |
1550 |
|
1551 |
GLOBAL(void) |
1552 |
jcopy_markers_execute (j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
1553 |
JCOPY_OPTION /*option*/) |
1554 |
{ |
1555 |
jpeg_saved_marker_ptr marker; |
1556 |
|
1557 |
/* In the current implementation, we don't actually need to examine the |
1558 |
* option flag here; we just copy everything that got saved. |
1559 |
* But to avoid confusion, we do not output JFIF and Adobe APP14 markers |
1560 |
* if the encoder library already wrote one. |
1561 |
*/ |
1562 |
for (marker = srcinfo->marker_list; marker != NULL; marker = marker->next) { |
1563 |
if (dstinfo->write_JFIF_header && |
1564 |
marker->marker == JPEG_APP0 && |
1565 |
marker->data_length >= 5 && |
1566 |
GETJOCTET(marker->data[0]) == 0x4A && |
1567 |
GETJOCTET(marker->data[1]) == 0x46 && |
1568 |
GETJOCTET(marker->data[2]) == 0x49 && |
1569 |
GETJOCTET(marker->data[3]) == 0x46 && |
1570 |
GETJOCTET(marker->data[4]) == 0) |
1571 |
continue; /* reject duplicate JFIF */ |
1572 |
if (dstinfo->write_Adobe_marker && |
1573 |
marker->marker == JPEG_APP0+14 && |
1574 |
marker->data_length >= 5 && |
1575 |
GETJOCTET(marker->data[0]) == 0x41 && |
1576 |
GETJOCTET(marker->data[1]) == 0x64 && |
1577 |
GETJOCTET(marker->data[2]) == 0x6F && |
1578 |
GETJOCTET(marker->data[3]) == 0x62 && |
1579 |
GETJOCTET(marker->data[4]) == 0x65) |
1580 |
continue; /* reject duplicate Adobe */ |
1581 |
#ifdef NEED_FAR_POINTERS |
1582 |
/* We could use jpeg_write_marker if the data weren't FAR... */ |
1583 |
{ |
1584 |
unsigned int i; |
1585 |
jpeg_write_m_header(dstinfo, marker->marker, marker->data_length); |
1586 |
for (i = 0; i < marker->data_length; i++) |
1587 |
jpeg_write_m_byte(dstinfo, marker->data[i]); |
1588 |
} |
1589 |
#else |
1590 |
jpeg_write_marker(dstinfo, marker->marker, |
1591 |
marker->data, marker->data_length); |
1592 |
#endif |
1593 |
} |
1594 |
} |
1595 |
|
1596 |
} // namespace KIPIJPEGLossLessPlugin |