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