/src/libtiff/libtiff/tif_jpeg.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 1994-1997 Sam Leffler |
3 | | * Copyright (c) 1994-1997 Silicon Graphics, Inc. |
4 | | * |
5 | | * Permission to use, copy, modify, distribute, and sell this software and |
6 | | * its documentation for any purpose is hereby granted without fee, provided |
7 | | * that (i) the above copyright notices and this permission notice appear in |
8 | | * all copies of the software and related documentation, and (ii) the names of |
9 | | * Sam Leffler and Silicon Graphics may not be used in any advertising or |
10 | | * publicity relating to the software without the specific, prior written |
11 | | * permission of Sam Leffler and Silicon Graphics. |
12 | | * |
13 | | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
14 | | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
15 | | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
16 | | * |
17 | | * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
18 | | * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
19 | | * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
20 | | * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
21 | | * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
22 | | * OF THIS SOFTWARE. |
23 | | */ |
24 | | |
25 | | #define WIN32_LEAN_AND_MEAN |
26 | | #define VC_EXTRALEAN |
27 | | |
28 | | #include "tiffiop.h" |
29 | | #include <stdlib.h> |
30 | | |
31 | | #ifdef JPEG_SUPPORT |
32 | | |
33 | | /* |
34 | | * TIFF Library |
35 | | * |
36 | | * JPEG Compression support per TIFF Technical Note #2 |
37 | | * (*not* per the original TIFF 6.0 spec). |
38 | | * |
39 | | * This file is simply an interface to the libjpeg library written by |
40 | | * the Independent JPEG Group. You need release 5 or later of the IJG |
41 | | * code, which you can find on the Internet at ftp.uu.net:/graphics/jpeg/. |
42 | | * |
43 | | * Contributed by Tom Lane <tgl@sss.pgh.pa.us>. |
44 | | */ |
45 | | #include <setjmp.h> |
46 | | |
47 | | /* Settings that are independent of libjpeg ABI. Used when reinitializing the */ |
48 | | /* JPEGState from libjpegs 8 bit to libjpeg 12 bits, which have potentially */ |
49 | | /* different ABI */ |
50 | | typedef struct |
51 | | { |
52 | | TIFFVGetMethod vgetparent; /* super-class method */ |
53 | | TIFFVSetMethod vsetparent; /* super-class method */ |
54 | | TIFFPrintMethod printdir; /* super-class method */ |
55 | | TIFFStripMethod defsparent; /* super-class method */ |
56 | | TIFFTileMethod deftparent; /* super-class method */ |
57 | | |
58 | | /* pseudo-tag fields */ |
59 | | void *jpegtables; /* JPEGTables tag value, or NULL */ |
60 | | uint32_t jpegtables_length; /* number of bytes in same */ |
61 | | int jpegquality; /* Compression quality level */ |
62 | | int jpegcolormode; /* Auto RGB<=>YCbCr convert? */ |
63 | | int jpegtablesmode; /* What to put in JPEGTables */ |
64 | | |
65 | | int ycbcrsampling_fetched; |
66 | | int max_allowed_scan_number; |
67 | | int has_warned_about_progressive_mode; |
68 | | } JPEGOtherSettings; |
69 | | |
70 | | int TIFFFillStrip(TIFF *tif, uint32_t strip); |
71 | | int TIFFFillTile(TIFF *tif, uint32_t tile); |
72 | | int TIFFReInitJPEG_12(TIFF *tif, const JPEGOtherSettings *otherSettings, |
73 | | int scheme, int is_encode); |
74 | | int TIFFJPEGIsFullStripRequired_12(TIFF *tif); |
75 | | |
76 | | #include "jerror.h" |
77 | | #include "jpeglib.h" |
78 | | |
79 | | /* Do optional compile-time version check */ |
80 | | #if defined(EXPECTED_JPEG_LIB_VERSION) && !defined(LIBJPEG_12_PATH) |
81 | | #if EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION |
82 | | #error EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION |
83 | | #endif |
84 | | #endif |
85 | | |
86 | | /* |
87 | | * Do we want to do special processing suitable for when JSAMPLE is a |
88 | | * 16bit value? |
89 | | */ |
90 | | |
91 | | /* HAVE_JPEGTURBO_DUAL_MODE_8_12 is defined for libjpeg-turbo >= 3.0 which |
92 | | * adds a dual-mode 8/12 bit API in the same library. |
93 | | * (note: libjpeg-turbo 2.2 was actually released as 3.0) |
94 | | */ |
95 | | |
96 | | #if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) |
97 | | #define JPEG_DUAL_MODE_8_12 |
98 | | /* Start by undefining BITS_IN_JSAMPLE which is always set to 8 in libjpeg-turbo |
99 | | * >= 3.0 Cf |
100 | | * https://github.com/libjpeg-turbo/libjpeg-turbo/commit/8b9bc4b9635a2a047fb23ebe70c9acd728d3f99b |
101 | | */ |
102 | | #undef BITS_IN_JSAMPLE |
103 | | /* libjpeg-turbo >= 3.0 adds J12xxxx datatypes for the 12-bit mode. */ |
104 | | #if defined(FROM_TIF_JPEG_12) |
105 | 0 | #define BITS_IN_JSAMPLE 12 |
106 | 58.7M | #define TIFF_JSAMPLE J12SAMPLE |
107 | 5 | #define TIFF_JSAMPARRAY J12SAMPARRAY |
108 | | #define TIFF_JSAMPIMAGE J12SAMPIMAGE |
109 | 45.3k | #define TIFF_JSAMPROW J12SAMPROW |
110 | | #else |
111 | 0 | #define BITS_IN_JSAMPLE 8 |
112 | 0 | #define TIFF_JSAMPLE JSAMPLE |
113 | 12 | #define TIFF_JSAMPARRAY JSAMPARRAY |
114 | | #define TIFF_JSAMPIMAGE JSAMPIMAGE |
115 | 0 | #define TIFF_JSAMPROW JSAMPROW |
116 | | #endif |
117 | | #else |
118 | | #define TIFF_JSAMPLE JSAMPLE |
119 | | #define TIFF_JSAMPARRAY JSAMPARRAY |
120 | | #define TIFF_JSAMPIMAGE JSAMPIMAGE |
121 | | #define TIFF_JSAMPROW JSAMPROW |
122 | | #endif |
123 | | |
124 | | #if defined(JPEG_LIB_MK1) |
125 | | #define JPEG_LIB_MK1_OR_12BIT 1 |
126 | | #elif BITS_IN_JSAMPLE == 12 |
127 | | #define JPEG_LIB_MK1_OR_12BIT 1 |
128 | | #endif |
129 | | |
130 | | /* |
131 | | * We are using width_in_blocks which is supposed to be private to |
132 | | * libjpeg. Unfortunately, the libjpeg delivered with Cygwin has |
133 | | * renamed this member to width_in_data_units. Since the header has |
134 | | * also renamed a define, use that unique define name in order to |
135 | | * detect the problem header and adjust to suit. |
136 | | */ |
137 | | #if defined(D_MAX_DATA_UNITS_IN_MCU) |
138 | | #define width_in_blocks width_in_data_units |
139 | | #endif |
140 | | |
141 | | /* |
142 | | * On some machines it may be worthwhile to use _setjmp or sigsetjmp |
143 | | * in place of plain setjmp. These macros will make it easier. |
144 | | */ |
145 | 10.7M | #define SETJMP(jbuf) setjmp(jbuf) |
146 | 132k | #define LONGJMP(jbuf, code) longjmp(jbuf, code) |
147 | | #define JMP_BUF jmp_buf |
148 | | |
149 | | #ifndef TIFF_jpeg_destination_mgr_defined |
150 | | #define TIFF_jpeg_destination_mgr_defined |
151 | | typedef struct jpeg_destination_mgr jpeg_destination_mgr; |
152 | | #endif |
153 | | |
154 | | #ifndef TIFF_jpeg_source_mgr_defined |
155 | | #define TIFF_jpeg_source_mgr_defined |
156 | | typedef struct jpeg_source_mgr jpeg_source_mgr; |
157 | | #endif |
158 | | |
159 | | #ifndef TIFF_jpeg_error_mgr_defined |
160 | | #define TIFF_jpeg_error_mgr_defined |
161 | | typedef struct jpeg_error_mgr jpeg_error_mgr; |
162 | | #endif |
163 | | |
164 | | /* |
165 | | * State block for each open TIFF file using |
166 | | * libjpeg to do JPEG compression/decompression. |
167 | | * |
168 | | * libjpeg's visible state is either a jpeg_compress_struct |
169 | | * or jpeg_decompress_struct depending on which way we |
170 | | * are going. comm can be used to refer to the fields |
171 | | * which are common to both. |
172 | | * |
173 | | * NB: cinfo is required to be the first member of JPEGState, |
174 | | * so we can safely cast JPEGState* -> jpeg_xxx_struct* |
175 | | * and vice versa! |
176 | | */ |
177 | | typedef struct |
178 | | { |
179 | | union |
180 | | { |
181 | | struct jpeg_compress_struct c; |
182 | | struct jpeg_decompress_struct d; |
183 | | struct jpeg_common_struct comm; |
184 | | } cinfo; /* NB: must be first */ |
185 | | int cinfo_initialized; |
186 | | |
187 | | jpeg_error_mgr err; /* libjpeg error manager */ |
188 | | JMP_BUF exit_jmpbuf; /* for catching libjpeg failures */ |
189 | | |
190 | | struct jpeg_progress_mgr progress; |
191 | | /* |
192 | | * The following two members could be a union, but |
193 | | * they're small enough that it's not worth the effort. |
194 | | */ |
195 | | jpeg_destination_mgr dest; /* data dest for compression */ |
196 | | jpeg_source_mgr src; /* data source for decompression */ |
197 | | /* private state */ |
198 | | TIFF *tif; /* back link needed by some code */ |
199 | | uint16_t photometric; /* copy of PhotometricInterpretation */ |
200 | | uint16_t h_sampling; /* luminance sampling factors */ |
201 | | uint16_t v_sampling; |
202 | | tmsize_t bytesperline; /* decompressed bytes per scanline */ |
203 | | /* pointers to intermediate buffers when processing downsampled data */ |
204 | | TIFF_JSAMPARRAY ds_buffer[MAX_COMPONENTS]; |
205 | | int scancount; /* number of "scanlines" accumulated */ |
206 | | int samplesperclump; |
207 | | |
208 | | JPEGOtherSettings otherSettings; |
209 | | |
210 | | int encode_raw_error; |
211 | | } JPEGState; |
212 | | |
213 | 296k | #define JState(tif) ((JPEGState *)(tif)->tif_data) |
214 | | |
215 | | static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); |
216 | | static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); |
217 | | static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); |
218 | | static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); |
219 | | static int JPEGInitializeLibJPEG(TIFF *tif, int decode); |
220 | | static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); |
221 | | |
222 | | #define FIELD_JPEGTABLES (FIELD_CODEC + 0) |
223 | | |
224 | | static const TIFFField jpegFields[] = { |
225 | | {TIFFTAG_JPEGTABLES, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, |
226 | | FIELD_JPEGTABLES, FALSE, TRUE, "JPEGTables", NULL}, |
227 | | {TIFFTAG_JPEGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, |
228 | | TRUE, FALSE, "", NULL}, |
229 | | {TIFFTAG_JPEGCOLORMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, |
230 | | FALSE, FALSE, "", NULL}, |
231 | | {TIFFTAG_JPEGTABLESMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, |
232 | | FALSE, FALSE, "", NULL}}; |
233 | | |
234 | | /* |
235 | | * libjpeg interface layer. |
236 | | * |
237 | | * We use setjmp/longjmp to return control to libtiff |
238 | | * when a fatal error is encountered within the JPEG |
239 | | * library. We also direct libjpeg error and warning |
240 | | * messages through the appropriate libtiff handlers. |
241 | | */ |
242 | | |
243 | | /* |
244 | | * Error handling routines (these replace corresponding |
245 | | * IJG routines from jerror.c). These are used for both |
246 | | * compression and decompression. |
247 | | */ |
248 | | static void TIFFjpeg_error_exit(j_common_ptr cinfo) |
249 | 132k | { |
250 | 132k | JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */ |
251 | 132k | char buffer[JMSG_LENGTH_MAX]; |
252 | | |
253 | 132k | (*cinfo->err->format_message)(cinfo, buffer); |
254 | 132k | TIFFErrorExtR(sp->tif, "JPEGLib", "%s", |
255 | 132k | buffer); /* display the error message */ |
256 | 132k | jpeg_abort(cinfo); /* clean up libjpeg state */ |
257 | 132k | LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */ |
258 | 132k | } tif_jpeg.c:TIFFjpeg_error_exit Line | Count | Source | 249 | 132k | { | 250 | 132k | JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */ | 251 | 132k | char buffer[JMSG_LENGTH_MAX]; | 252 | | | 253 | 132k | (*cinfo->err->format_message)(cinfo, buffer); | 254 | 132k | TIFFErrorExtR(sp->tif, "JPEGLib", "%s", | 255 | 132k | buffer); /* display the error message */ | 256 | 132k | jpeg_abort(cinfo); /* clean up libjpeg state */ | 257 | 132k | LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */ | 258 | 132k | } |
tif_jpeg_12.c:TIFFjpeg_error_exit Line | Count | Source | 249 | 202 | { | 250 | 202 | JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */ | 251 | 202 | char buffer[JMSG_LENGTH_MAX]; | 252 | | | 253 | 202 | (*cinfo->err->format_message)(cinfo, buffer); | 254 | 202 | TIFFErrorExtR(sp->tif, "JPEGLib", "%s", | 255 | 202 | buffer); /* display the error message */ | 256 | 202 | jpeg_abort(cinfo); /* clean up libjpeg state */ | 257 | 202 | LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */ | 258 | 202 | } |
|
259 | | |
260 | | /* |
261 | | * This routine is invoked only for warning messages, |
262 | | * since error_exit does its own thing and trace_level |
263 | | * is never set > 0. |
264 | | */ |
265 | | static void TIFFjpeg_output_message(j_common_ptr cinfo) |
266 | 78.7k | { |
267 | 78.7k | char buffer[JMSG_LENGTH_MAX]; |
268 | | |
269 | 78.7k | (*cinfo->err->format_message)(cinfo, buffer); |
270 | 78.7k | TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer); |
271 | 78.7k | } tif_jpeg.c:TIFFjpeg_output_message Line | Count | Source | 266 | 78.3k | { | 267 | 78.3k | char buffer[JMSG_LENGTH_MAX]; | 268 | | | 269 | 78.3k | (*cinfo->err->format_message)(cinfo, buffer); | 270 | 78.3k | TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer); | 271 | 78.3k | } |
tif_jpeg_12.c:TIFFjpeg_output_message Line | Count | Source | 266 | 361 | { | 267 | 361 | char buffer[JMSG_LENGTH_MAX]; | 268 | | | 269 | 361 | (*cinfo->err->format_message)(cinfo, buffer); | 270 | 361 | TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer); | 271 | 361 | } |
|
272 | | |
273 | | /* Avoid the risk of denial-of-service on crafted JPEGs with an insane */ |
274 | | /* number of scans. */ |
275 | | /* See |
276 | | * http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf |
277 | | */ |
278 | | static void TIFFjpeg_progress_monitor(j_common_ptr cinfo) |
279 | 33.3M | { |
280 | 33.3M | JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */ |
281 | 33.3M | if (cinfo->is_decompressor) |
282 | 33.3M | { |
283 | 33.3M | const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number; |
284 | 33.3M | if (scan_no >= sp->otherSettings.max_allowed_scan_number) |
285 | 6 | { |
286 | 6 | TIFFErrorExtR( |
287 | 6 | ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor", |
288 | 6 | "Scan number %d exceeds maximum scans (%d). This limit " |
289 | 6 | "can be raised through the " |
290 | 6 | "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER " |
291 | 6 | "environment variable.", |
292 | 6 | scan_no, sp->otherSettings.max_allowed_scan_number); |
293 | | |
294 | 6 | jpeg_abort(cinfo); /* clean up libjpeg state */ |
295 | 6 | LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */ |
296 | 6 | } |
297 | 33.3M | } |
298 | 33.3M | } tif_jpeg.c:TIFFjpeg_progress_monitor Line | Count | Source | 279 | 32.2M | { | 280 | 32.2M | JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */ | 281 | 32.2M | if (cinfo->is_decompressor) | 282 | 32.2M | { | 283 | 32.2M | const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number; | 284 | 32.2M | if (scan_no >= sp->otherSettings.max_allowed_scan_number) | 285 | 5 | { | 286 | 5 | TIFFErrorExtR( | 287 | 5 | ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor", | 288 | 5 | "Scan number %d exceeds maximum scans (%d). This limit " | 289 | 5 | "can be raised through the " | 290 | 5 | "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER " | 291 | 5 | "environment variable.", | 292 | 5 | scan_no, sp->otherSettings.max_allowed_scan_number); | 293 | | | 294 | 5 | jpeg_abort(cinfo); /* clean up libjpeg state */ | 295 | 5 | LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */ | 296 | 5 | } | 297 | 32.2M | } | 298 | 32.2M | } |
tif_jpeg_12.c:TIFFjpeg_progress_monitor Line | Count | Source | 279 | 1.04M | { | 280 | 1.04M | JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */ | 281 | 1.04M | if (cinfo->is_decompressor) | 282 | 1.04M | { | 283 | 1.04M | const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number; | 284 | 1.04M | if (scan_no >= sp->otherSettings.max_allowed_scan_number) | 285 | 1 | { | 286 | 1 | TIFFErrorExtR( | 287 | 1 | ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor", | 288 | 1 | "Scan number %d exceeds maximum scans (%d). This limit " | 289 | 1 | "can be raised through the " | 290 | 1 | "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER " | 291 | 1 | "environment variable.", | 292 | 1 | scan_no, sp->otherSettings.max_allowed_scan_number); | 293 | | | 294 | 1 | jpeg_abort(cinfo); /* clean up libjpeg state */ | 295 | 1 | LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */ | 296 | 1 | } | 297 | 1.04M | } | 298 | 1.04M | } |
|
299 | | |
300 | | /* |
301 | | * Interface routines. This layer of routines exists |
302 | | * primarily to limit side-effects from using setjmp. |
303 | | * Also, normal/error returns are converted into return |
304 | | * values per libtiff practice. |
305 | | */ |
306 | 10.7M | #define CALLJPEG(sp, fail, op) (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op)) |
307 | 203k | #define CALLVJPEG(sp, op) CALLJPEG(sp, 0, ((op), 1)) |
308 | | |
309 | | static int TIFFjpeg_create_compress(JPEGState *sp) |
310 | 0 | { |
311 | | /* initialize JPEG error handling */ |
312 | 0 | sp->cinfo.c.err = jpeg_std_error(&sp->err); |
313 | 0 | sp->err.error_exit = TIFFjpeg_error_exit; |
314 | 0 | sp->err.output_message = TIFFjpeg_output_message; |
315 | | |
316 | | /* set client_data to avoid UMR warning from tools like Purify */ |
317 | 0 | sp->cinfo.c.client_data = NULL; |
318 | |
|
319 | 0 | return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c)); |
320 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_create_compress Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_create_compress |
321 | | |
322 | | static int TIFFjpeg_create_decompress(JPEGState *sp) |
323 | 2.37k | { |
324 | | /* initialize JPEG error handling */ |
325 | 2.37k | sp->cinfo.d.err = jpeg_std_error(&sp->err); |
326 | 2.37k | sp->err.error_exit = TIFFjpeg_error_exit; |
327 | 2.37k | sp->err.output_message = TIFFjpeg_output_message; |
328 | | |
329 | | /* set client_data to avoid UMR warning from tools like Purify */ |
330 | 2.37k | sp->cinfo.d.client_data = NULL; |
331 | | |
332 | 2.37k | return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d)); |
333 | 2.37k | } tif_jpeg.c:TIFFjpeg_create_decompress Line | Count | Source | 323 | 2.01k | { | 324 | | /* initialize JPEG error handling */ | 325 | 2.01k | sp->cinfo.d.err = jpeg_std_error(&sp->err); | 326 | 2.01k | sp->err.error_exit = TIFFjpeg_error_exit; | 327 | 2.01k | sp->err.output_message = TIFFjpeg_output_message; | 328 | | | 329 | | /* set client_data to avoid UMR warning from tools like Purify */ | 330 | 2.01k | sp->cinfo.d.client_data = NULL; | 331 | | | 332 | 2.01k | return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d)); | 333 | 2.01k | } |
tif_jpeg_12.c:TIFFjpeg_create_decompress Line | Count | Source | 323 | 361 | { | 324 | | /* initialize JPEG error handling */ | 325 | 361 | sp->cinfo.d.err = jpeg_std_error(&sp->err); | 326 | 361 | sp->err.error_exit = TIFFjpeg_error_exit; | 327 | 361 | sp->err.output_message = TIFFjpeg_output_message; | 328 | | | 329 | | /* set client_data to avoid UMR warning from tools like Purify */ | 330 | 361 | sp->cinfo.d.client_data = NULL; | 331 | | | 332 | 361 | return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d)); | 333 | 361 | } |
|
334 | | |
335 | | static int TIFFjpeg_set_defaults(JPEGState *sp) |
336 | 0 | { |
337 | 0 | return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c)); |
338 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_set_defaults Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_set_defaults |
339 | | |
340 | | static int TIFFjpeg_set_colorspace(JPEGState *sp, J_COLOR_SPACE colorspace) |
341 | 0 | { |
342 | 0 | return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace)); |
343 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_set_colorspace Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_set_colorspace |
344 | | |
345 | | static int TIFFjpeg_set_quality(JPEGState *sp, int quality, |
346 | | boolean force_baseline) |
347 | 0 | { |
348 | 0 | return CALLVJPEG(sp, |
349 | 0 | jpeg_set_quality(&sp->cinfo.c, quality, force_baseline)); |
350 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_set_quality Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_set_quality |
351 | | |
352 | | static int TIFFjpeg_suppress_tables(JPEGState *sp, boolean suppress) |
353 | 0 | { |
354 | 0 | return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress)); |
355 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_suppress_tables Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_suppress_tables |
356 | | |
357 | | static int TIFFjpeg_start_compress(JPEGState *sp, boolean write_all_tables) |
358 | 0 | { |
359 | 0 | return CALLVJPEG(sp, jpeg_start_compress(&sp->cinfo.c, write_all_tables)); |
360 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_start_compress Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_start_compress |
361 | | |
362 | | static int TIFFjpeg_write_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines, |
363 | | int num_lines) |
364 | 0 | { |
365 | | #if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12 |
366 | 0 | return CALLJPEG(sp, -1, |
367 | | (int)jpeg12_write_scanlines(&sp->cinfo.c, scanlines, |
368 | | (JDIMENSION)num_lines)); |
369 | | #else |
370 | 0 | return CALLJPEG(sp, -1, |
371 | | (int)jpeg_write_scanlines(&sp->cinfo.c, scanlines, |
372 | | (JDIMENSION)num_lines)); |
373 | | #endif |
374 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_write_scanlines Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_write_scanlines |
375 | | |
376 | | static int TIFFjpeg_write_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data, |
377 | | int num_lines) |
378 | 0 | { |
379 | | #if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12 |
380 | 0 | return CALLJPEG( |
381 | | sp, -1, |
382 | | (int)jpeg12_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines)); |
383 | | #else |
384 | 0 | return CALLJPEG( |
385 | | sp, -1, |
386 | | (int)jpeg_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines)); |
387 | | #endif |
388 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_write_raw_data Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_write_raw_data |
389 | | |
390 | | static int TIFFjpeg_finish_compress(JPEGState *sp) |
391 | 0 | { |
392 | 0 | return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c)); |
393 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_finish_compress Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_finish_compress |
394 | | |
395 | | static int TIFFjpeg_write_tables(JPEGState *sp) |
396 | 0 | { |
397 | 0 | return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c)); |
398 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_write_tables Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_write_tables |
399 | | |
400 | | static int TIFFjpeg_read_header(JPEGState *sp, boolean require_image) |
401 | 155k | { |
402 | 155k | return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image)); |
403 | 155k | } tif_jpeg.c:TIFFjpeg_read_header Line | Count | Source | 401 | 155k | { | 402 | 155k | return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image)); | 403 | 155k | } |
tif_jpeg_12.c:TIFFjpeg_read_header Line | Count | Source | 401 | 361 | { | 402 | 361 | return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image)); | 403 | 361 | } |
|
404 | | |
405 | | static int TIFFjpeg_has_multiple_scans(JPEGState *sp) |
406 | 43.9k | { |
407 | 43.9k | return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d)); |
408 | 43.9k | } tif_jpeg.c:TIFFjpeg_has_multiple_scans Line | Count | Source | 406 | 43.5k | { | 407 | 43.5k | return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d)); | 408 | 43.5k | } |
tif_jpeg_12.c:TIFFjpeg_has_multiple_scans Line | Count | Source | 406 | 357 | { | 407 | 357 | return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d)); | 408 | 357 | } |
|
409 | | |
410 | | static int TIFFjpeg_start_decompress(JPEGState *sp) |
411 | 43.1k | { |
412 | 43.1k | const char *sz_max_allowed_scan_number; |
413 | | /* progress monitor */ |
414 | 43.1k | sp->cinfo.d.progress = &sp->progress; |
415 | 43.1k | sp->progress.progress_monitor = TIFFjpeg_progress_monitor; |
416 | 43.1k | sp->otherSettings.max_allowed_scan_number = 100; |
417 | 43.1k | sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER"); |
418 | 43.1k | if (sz_max_allowed_scan_number) |
419 | 0 | sp->otherSettings.max_allowed_scan_number = |
420 | 0 | atoi(sz_max_allowed_scan_number); |
421 | | |
422 | 43.1k | return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d)); |
423 | 43.1k | } tif_jpeg.c:TIFFjpeg_start_decompress Line | Count | Source | 411 | 42.8k | { | 412 | 42.8k | const char *sz_max_allowed_scan_number; | 413 | | /* progress monitor */ | 414 | 42.8k | sp->cinfo.d.progress = &sp->progress; | 415 | 42.8k | sp->progress.progress_monitor = TIFFjpeg_progress_monitor; | 416 | 42.8k | sp->otherSettings.max_allowed_scan_number = 100; | 417 | 42.8k | sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER"); | 418 | 42.8k | if (sz_max_allowed_scan_number) | 419 | 0 | sp->otherSettings.max_allowed_scan_number = | 420 | 0 | atoi(sz_max_allowed_scan_number); | 421 | | | 422 | 42.8k | return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d)); | 423 | 42.8k | } |
tif_jpeg_12.c:TIFFjpeg_start_decompress Line | Count | Source | 411 | 357 | { | 412 | 357 | const char *sz_max_allowed_scan_number; | 413 | | /* progress monitor */ | 414 | 357 | sp->cinfo.d.progress = &sp->progress; | 415 | 357 | sp->progress.progress_monitor = TIFFjpeg_progress_monitor; | 416 | 357 | sp->otherSettings.max_allowed_scan_number = 100; | 417 | 357 | sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER"); | 418 | 357 | if (sz_max_allowed_scan_number) | 419 | 0 | sp->otherSettings.max_allowed_scan_number = | 420 | 0 | atoi(sz_max_allowed_scan_number); | 421 | | | 422 | 357 | return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d)); | 423 | 357 | } |
|
424 | | |
425 | | static int TIFFjpeg_read_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines, |
426 | | int max_lines) |
427 | 10.2M | { |
428 | | #if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12 |
429 | 45.3k | return CALLJPEG(sp, -1, |
430 | | (int)jpeg12_read_scanlines(&sp->cinfo.d, scanlines, |
431 | | (JDIMENSION)max_lines)); |
432 | | #else |
433 | 10.2M | return CALLJPEG(sp, -1, |
434 | | (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines, |
435 | | (JDIMENSION)max_lines)); |
436 | | #endif |
437 | 10.2M | } tif_jpeg.c:TIFFjpeg_read_scanlines Line | Count | Source | 427 | 10.2M | { | 428 | | #if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12 | 429 | | return CALLJPEG(sp, -1, | 430 | | (int)jpeg12_read_scanlines(&sp->cinfo.d, scanlines, | 431 | | (JDIMENSION)max_lines)); | 432 | | #else | 433 | 10.2M | return CALLJPEG(sp, -1, | 434 | 10.2M | (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines, | 435 | 10.2M | (JDIMENSION)max_lines)); | 436 | 10.2M | #endif | 437 | 10.2M | } |
tif_jpeg_12.c:TIFFjpeg_read_scanlines Line | Count | Source | 427 | 45.3k | { | 428 | 45.3k | #if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12 | 429 | 45.3k | return CALLJPEG(sp, -1, | 430 | 45.3k | (int)jpeg12_read_scanlines(&sp->cinfo.d, scanlines, | 431 | 45.3k | (JDIMENSION)max_lines)); | 432 | | #else | 433 | | return CALLJPEG(sp, -1, | 434 | | (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines, | 435 | | (JDIMENSION)max_lines)); | 436 | | #endif | 437 | 45.3k | } |
|
438 | | |
439 | | static int TIFFjpeg_read_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data, |
440 | | int max_lines) |
441 | 0 | { |
442 | | #if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12 |
443 | 0 | return CALLJPEG( |
444 | | sp, -1, |
445 | | (int)jpeg12_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines)); |
446 | | #else |
447 | 0 | return CALLJPEG( |
448 | | sp, -1, |
449 | | (int)jpeg_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines)); |
450 | | #endif |
451 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_read_raw_data Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_read_raw_data |
452 | | |
453 | | static int TIFFjpeg_finish_decompress(JPEGState *sp) |
454 | 26.4k | { |
455 | 26.4k | return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d)); |
456 | 26.4k | } tif_jpeg.c:TIFFjpeg_finish_decompress Line | Count | Source | 454 | 26.2k | { | 455 | 26.2k | return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d)); | 456 | 26.2k | } |
tif_jpeg_12.c:TIFFjpeg_finish_decompress Line | Count | Source | 454 | 122 | { | 455 | 122 | return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d)); | 456 | 122 | } |
|
457 | | |
458 | | static int TIFFjpeg_abort(JPEGState *sp) |
459 | 155k | { |
460 | 155k | return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm)); |
461 | 155k | } tif_jpeg.c:TIFFjpeg_abort Line | Count | Source | 459 | 155k | { | 460 | 155k | return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm)); | 461 | 155k | } |
tif_jpeg_12.c:TIFFjpeg_abort Line | Count | Source | 459 | 361 | { | 460 | 361 | return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm)); | 461 | 361 | } |
|
462 | | |
463 | | static int TIFFjpeg_destroy(JPEGState *sp) |
464 | 2.37k | { |
465 | 2.37k | return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm)); |
466 | 2.37k | } tif_jpeg.c:TIFFjpeg_destroy Line | Count | Source | 464 | 2.01k | { | 465 | 2.01k | return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm)); | 466 | 2.01k | } |
tif_jpeg_12.c:TIFFjpeg_destroy Line | Count | Source | 464 | 361 | { | 465 | 361 | return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm)); | 466 | 361 | } |
|
467 | | |
468 | | static JSAMPARRAY TIFFjpeg_alloc_sarray(JPEGState *sp, int pool_id, |
469 | | JDIMENSION samplesperrow, |
470 | | JDIMENSION numrows) |
471 | 51 | { |
472 | 51 | return CALLJPEG(sp, (JSAMPARRAY)NULL, |
473 | 51 | (*sp->cinfo.comm.mem->alloc_sarray)( |
474 | 51 | &sp->cinfo.comm, pool_id, samplesperrow, numrows)); |
475 | 51 | } tif_jpeg.c:TIFFjpeg_alloc_sarray Line | Count | Source | 471 | 36 | { | 472 | 36 | return CALLJPEG(sp, (JSAMPARRAY)NULL, | 473 | 36 | (*sp->cinfo.comm.mem->alloc_sarray)( | 474 | 36 | &sp->cinfo.comm, pool_id, samplesperrow, numrows)); | 475 | 36 | } |
tif_jpeg_12.c:TIFFjpeg_alloc_sarray Line | Count | Source | 471 | 15 | { | 472 | 15 | return CALLJPEG(sp, (JSAMPARRAY)NULL, | 473 | 15 | (*sp->cinfo.comm.mem->alloc_sarray)( | 474 | 15 | &sp->cinfo.comm, pool_id, samplesperrow, numrows)); | 475 | 15 | } |
|
476 | | |
477 | | /* |
478 | | * JPEG library destination data manager. |
479 | | * These routines direct compressed data from libjpeg into the |
480 | | * libtiff output buffer. |
481 | | */ |
482 | | |
483 | | static void std_init_destination(j_compress_ptr cinfo) |
484 | 0 | { |
485 | 0 | JPEGState *sp = (JPEGState *)cinfo; |
486 | 0 | TIFF *tif = sp->tif; |
487 | |
|
488 | 0 | sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata; |
489 | 0 | sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize; |
490 | 0 | } Unexecuted instantiation: tif_jpeg.c:std_init_destination Unexecuted instantiation: tif_jpeg_12.c:std_init_destination |
491 | | |
492 | | static boolean std_empty_output_buffer(j_compress_ptr cinfo) |
493 | 0 | { |
494 | 0 | JPEGState *sp = (JPEGState *)cinfo; |
495 | 0 | TIFF *tif = sp->tif; |
496 | | |
497 | | /* the entire buffer has been filled */ |
498 | 0 | tif->tif_rawcc = tif->tif_rawdatasize; |
499 | |
|
500 | | #ifdef IPPJ_HUFF |
501 | | /* |
502 | | * The Intel IPP performance library does not necessarily fill up |
503 | | * the whole output buffer on each pass, so only dump out the parts |
504 | | * that have been filled. |
505 | | * http://trac.osgeo.org/gdal/wiki/JpegIPP |
506 | | */ |
507 | | if (sp->dest.free_in_buffer >= 0) |
508 | | { |
509 | | tif->tif_rawcc = tif->tif_rawdatasize - sp->dest.free_in_buffer; |
510 | | } |
511 | | #endif |
512 | |
|
513 | 0 | if (!TIFFFlushData1(tif)) |
514 | 0 | return FALSE; |
515 | 0 | sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata; |
516 | 0 | sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize; |
517 | |
|
518 | 0 | return (TRUE); |
519 | 0 | } Unexecuted instantiation: tif_jpeg.c:std_empty_output_buffer Unexecuted instantiation: tif_jpeg_12.c:std_empty_output_buffer |
520 | | |
521 | | static void std_term_destination(j_compress_ptr cinfo) |
522 | 0 | { |
523 | 0 | JPEGState *sp = (JPEGState *)cinfo; |
524 | 0 | TIFF *tif = sp->tif; |
525 | |
|
526 | 0 | tif->tif_rawcp = (uint8_t *)sp->dest.next_output_byte; |
527 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - (tmsize_t)sp->dest.free_in_buffer; |
528 | | /* NB: libtiff does the final buffer flush */ |
529 | 0 | } Unexecuted instantiation: tif_jpeg.c:std_term_destination Unexecuted instantiation: tif_jpeg_12.c:std_term_destination |
530 | | |
531 | | static void TIFFjpeg_data_dest(JPEGState *sp, TIFF *tif) |
532 | 0 | { |
533 | 0 | (void)tif; |
534 | 0 | sp->cinfo.c.dest = &sp->dest; |
535 | 0 | sp->dest.init_destination = std_init_destination; |
536 | 0 | sp->dest.empty_output_buffer = std_empty_output_buffer; |
537 | 0 | sp->dest.term_destination = std_term_destination; |
538 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_data_dest Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_data_dest |
539 | | |
540 | | /* |
541 | | * Alternate destination manager for outputting to JPEGTables field. |
542 | | */ |
543 | | |
544 | | static void tables_init_destination(j_compress_ptr cinfo) |
545 | 0 | { |
546 | 0 | JPEGState *sp = (JPEGState *)cinfo; |
547 | | |
548 | | /* while building, otherSettings.jpegtables_length is allocated buffer size |
549 | | */ |
550 | 0 | sp->dest.next_output_byte = (JOCTET *)sp->otherSettings.jpegtables; |
551 | 0 | sp->dest.free_in_buffer = (size_t)sp->otherSettings.jpegtables_length; |
552 | 0 | } Unexecuted instantiation: tif_jpeg.c:tables_init_destination Unexecuted instantiation: tif_jpeg_12.c:tables_init_destination |
553 | | |
554 | | static boolean tables_empty_output_buffer(j_compress_ptr cinfo) |
555 | 0 | { |
556 | 0 | JPEGState *sp = (JPEGState *)cinfo; |
557 | 0 | void *newbuf; |
558 | | |
559 | | /* the entire buffer has been filled; enlarge it by 1000 bytes */ |
560 | 0 | newbuf = |
561 | 0 | _TIFFreallocExt(sp->tif, (void *)sp->otherSettings.jpegtables, |
562 | 0 | (tmsize_t)(sp->otherSettings.jpegtables_length + 1000)); |
563 | 0 | if (newbuf == NULL) |
564 | 0 | ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 100); |
565 | 0 | sp->dest.next_output_byte = |
566 | 0 | (JOCTET *)newbuf + sp->otherSettings.jpegtables_length; |
567 | 0 | sp->dest.free_in_buffer = (size_t)1000; |
568 | 0 | sp->otherSettings.jpegtables = newbuf; |
569 | 0 | sp->otherSettings.jpegtables_length += 1000; |
570 | 0 | return (TRUE); |
571 | 0 | } Unexecuted instantiation: tif_jpeg.c:tables_empty_output_buffer Unexecuted instantiation: tif_jpeg_12.c:tables_empty_output_buffer |
572 | | |
573 | | static void tables_term_destination(j_compress_ptr cinfo) |
574 | 0 | { |
575 | 0 | JPEGState *sp = (JPEGState *)cinfo; |
576 | | |
577 | | /* set tables length to number of bytes actually emitted */ |
578 | 0 | sp->otherSettings.jpegtables_length -= (uint32_t)sp->dest.free_in_buffer; |
579 | 0 | } Unexecuted instantiation: tif_jpeg.c:tables_term_destination Unexecuted instantiation: tif_jpeg_12.c:tables_term_destination |
580 | | |
581 | | static int TIFFjpeg_tables_dest(JPEGState *sp, TIFF *tif) |
582 | 0 | { |
583 | 0 | (void)tif; |
584 | | /* |
585 | | * Allocate a working buffer for building tables. |
586 | | * Initial size is 1000 bytes, which is usually adequate. |
587 | | */ |
588 | 0 | if (sp->otherSettings.jpegtables) |
589 | 0 | _TIFFfreeExt(tif, sp->otherSettings.jpegtables); |
590 | 0 | sp->otherSettings.jpegtables_length = 1000; |
591 | 0 | sp->otherSettings.jpegtables = (void *)_TIFFmallocExt( |
592 | 0 | tif, (tmsize_t)sp->otherSettings.jpegtables_length); |
593 | 0 | if (sp->otherSettings.jpegtables == NULL) |
594 | 0 | { |
595 | 0 | sp->otherSettings.jpegtables_length = 0; |
596 | 0 | TIFFErrorExtR(sp->tif, "TIFFjpeg_tables_dest", |
597 | 0 | "No space for JPEGTables"); |
598 | 0 | return (0); |
599 | 0 | } |
600 | 0 | sp->cinfo.c.dest = &sp->dest; |
601 | 0 | sp->dest.init_destination = tables_init_destination; |
602 | 0 | sp->dest.empty_output_buffer = tables_empty_output_buffer; |
603 | 0 | sp->dest.term_destination = tables_term_destination; |
604 | 0 | return (1); |
605 | 0 | } Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_tables_dest Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_tables_dest |
606 | | |
607 | | /* |
608 | | * JPEG library source data manager. |
609 | | * These routines supply compressed data to libjpeg. |
610 | | */ |
611 | | |
612 | | static void std_init_source(j_decompress_ptr cinfo) |
613 | 155k | { |
614 | 155k | JPEGState *sp = (JPEGState *)cinfo; |
615 | 155k | TIFF *tif = sp->tif; |
616 | | |
617 | 155k | sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata; |
618 | 155k | sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc; |
619 | 155k | } tif_jpeg.c:std_init_source Line | Count | Source | 613 | 155k | { | 614 | 155k | JPEGState *sp = (JPEGState *)cinfo; | 615 | 155k | TIFF *tif = sp->tif; | 616 | | | 617 | 155k | sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata; | 618 | 155k | sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc; | 619 | 155k | } |
tif_jpeg_12.c:std_init_source Line | Count | Source | 613 | 361 | { | 614 | 361 | JPEGState *sp = (JPEGState *)cinfo; | 615 | 361 | TIFF *tif = sp->tif; | 616 | | | 617 | 361 | sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata; | 618 | 361 | sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc; | 619 | 361 | } |
|
620 | | |
621 | | static boolean std_fill_input_buffer(j_decompress_ptr cinfo) |
622 | 206k | { |
623 | 206k | JPEGState *sp = (JPEGState *)cinfo; |
624 | 206k | static const JOCTET dummy_EOI[2] = {0xFF, JPEG_EOI}; |
625 | | |
626 | | #ifdef IPPJ_HUFF |
627 | | /* |
628 | | * The Intel IPP performance library does not necessarily read the whole |
629 | | * input buffer in one pass, so it is possible to get here with data |
630 | | * yet to read. |
631 | | * |
632 | | * We just return without doing anything, until the entire buffer has |
633 | | * been read. |
634 | | * http://trac.osgeo.org/gdal/wiki/JpegIPP |
635 | | */ |
636 | | if (sp->src.bytes_in_buffer > 0) |
637 | | { |
638 | | return (TRUE); |
639 | | } |
640 | | #endif |
641 | | |
642 | | /* |
643 | | * Normally the whole strip/tile is read and so we don't need to do |
644 | | * a fill. In the case of CHUNKY_STRIP_READ_SUPPORT we might not have |
645 | | * all the data, but the rawdata is refreshed between scanlines and |
646 | | * we push this into the io machinery in JPEGDecode(). |
647 | | * http://trac.osgeo.org/gdal/ticket/3894 |
648 | | */ |
649 | | |
650 | 206k | WARNMS(cinfo, JWRN_JPEG_EOF); |
651 | | /* insert a fake EOI marker */ |
652 | 206k | sp->src.next_input_byte = dummy_EOI; |
653 | 206k | sp->src.bytes_in_buffer = 2; |
654 | 206k | return (TRUE); |
655 | 206k | } tif_jpeg.c:std_fill_input_buffer Line | Count | Source | 622 | 206k | { | 623 | 206k | JPEGState *sp = (JPEGState *)cinfo; | 624 | 206k | static const JOCTET dummy_EOI[2] = {0xFF, JPEG_EOI}; | 625 | | | 626 | | #ifdef IPPJ_HUFF | 627 | | /* | 628 | | * The Intel IPP performance library does not necessarily read the whole | 629 | | * input buffer in one pass, so it is possible to get here with data | 630 | | * yet to read. | 631 | | * | 632 | | * We just return without doing anything, until the entire buffer has | 633 | | * been read. | 634 | | * http://trac.osgeo.org/gdal/wiki/JpegIPP | 635 | | */ | 636 | | if (sp->src.bytes_in_buffer > 0) | 637 | | { | 638 | | return (TRUE); | 639 | | } | 640 | | #endif | 641 | | | 642 | | /* | 643 | | * Normally the whole strip/tile is read and so we don't need to do | 644 | | * a fill. In the case of CHUNKY_STRIP_READ_SUPPORT we might not have | 645 | | * all the data, but the rawdata is refreshed between scanlines and | 646 | | * we push this into the io machinery in JPEGDecode(). | 647 | | * http://trac.osgeo.org/gdal/ticket/3894 | 648 | | */ | 649 | | | 650 | 206k | WARNMS(cinfo, JWRN_JPEG_EOF); | 651 | | /* insert a fake EOI marker */ | 652 | 206k | sp->src.next_input_byte = dummy_EOI; | 653 | 206k | sp->src.bytes_in_buffer = 2; | 654 | 206k | return (TRUE); | 655 | 206k | } |
tif_jpeg_12.c:std_fill_input_buffer Line | Count | Source | 622 | 419 | { | 623 | 419 | JPEGState *sp = (JPEGState *)cinfo; | 624 | 419 | static const JOCTET dummy_EOI[2] = {0xFF, JPEG_EOI}; | 625 | | | 626 | | #ifdef IPPJ_HUFF | 627 | | /* | 628 | | * The Intel IPP performance library does not necessarily read the whole | 629 | | * input buffer in one pass, so it is possible to get here with data | 630 | | * yet to read. | 631 | | * | 632 | | * We just return without doing anything, until the entire buffer has | 633 | | * been read. | 634 | | * http://trac.osgeo.org/gdal/wiki/JpegIPP | 635 | | */ | 636 | | if (sp->src.bytes_in_buffer > 0) | 637 | | { | 638 | | return (TRUE); | 639 | | } | 640 | | #endif | 641 | | | 642 | | /* | 643 | | * Normally the whole strip/tile is read and so we don't need to do | 644 | | * a fill. In the case of CHUNKY_STRIP_READ_SUPPORT we might not have | 645 | | * all the data, but the rawdata is refreshed between scanlines and | 646 | | * we push this into the io machinery in JPEGDecode(). | 647 | | * http://trac.osgeo.org/gdal/ticket/3894 | 648 | | */ | 649 | | | 650 | 419 | WARNMS(cinfo, JWRN_JPEG_EOF); | 651 | | /* insert a fake EOI marker */ | 652 | 419 | sp->src.next_input_byte = dummy_EOI; | 653 | 419 | sp->src.bytes_in_buffer = 2; | 654 | 419 | return (TRUE); | 655 | 419 | } |
|
656 | | |
657 | | static void std_skip_input_data(j_decompress_ptr cinfo, long num_bytes) |
658 | 39.5k | { |
659 | 39.5k | JPEGState *sp = (JPEGState *)cinfo; |
660 | | |
661 | 39.5k | if (num_bytes > 0) |
662 | 39.5k | { |
663 | 39.5k | if ((size_t)num_bytes > sp->src.bytes_in_buffer) |
664 | 9.91k | { |
665 | | /* oops, buffer overrun */ |
666 | 9.91k | (void)std_fill_input_buffer(cinfo); |
667 | 9.91k | } |
668 | 29.6k | else |
669 | 29.6k | { |
670 | 29.6k | sp->src.next_input_byte += (size_t)num_bytes; |
671 | 29.6k | sp->src.bytes_in_buffer -= (size_t)num_bytes; |
672 | 29.6k | } |
673 | 39.5k | } |
674 | 39.5k | } tif_jpeg.c:std_skip_input_data Line | Count | Source | 658 | 35.2k | { | 659 | 35.2k | JPEGState *sp = (JPEGState *)cinfo; | 660 | | | 661 | 35.2k | if (num_bytes > 0) | 662 | 35.2k | { | 663 | 35.2k | if ((size_t)num_bytes > sp->src.bytes_in_buffer) | 664 | 9.86k | { | 665 | | /* oops, buffer overrun */ | 666 | 9.86k | (void)std_fill_input_buffer(cinfo); | 667 | 9.86k | } | 668 | 25.3k | else | 669 | 25.3k | { | 670 | 25.3k | sp->src.next_input_byte += (size_t)num_bytes; | 671 | 25.3k | sp->src.bytes_in_buffer -= (size_t)num_bytes; | 672 | 25.3k | } | 673 | 35.2k | } | 674 | 35.2k | } |
tif_jpeg_12.c:std_skip_input_data Line | Count | Source | 658 | 4.30k | { | 659 | 4.30k | JPEGState *sp = (JPEGState *)cinfo; | 660 | | | 661 | 4.30k | if (num_bytes > 0) | 662 | 4.30k | { | 663 | 4.30k | if ((size_t)num_bytes > sp->src.bytes_in_buffer) | 664 | 49 | { | 665 | | /* oops, buffer overrun */ | 666 | 49 | (void)std_fill_input_buffer(cinfo); | 667 | 49 | } | 668 | 4.26k | else | 669 | 4.26k | { | 670 | 4.26k | sp->src.next_input_byte += (size_t)num_bytes; | 671 | 4.26k | sp->src.bytes_in_buffer -= (size_t)num_bytes; | 672 | 4.26k | } | 673 | 4.30k | } | 674 | 4.30k | } |
|
675 | | |
676 | | static void std_term_source(j_decompress_ptr cinfo) |
677 | 20.5k | { |
678 | | /* No work necessary here */ |
679 | 20.5k | (void)cinfo; |
680 | 20.5k | } tif_jpeg.c:std_term_source Line | Count | Source | 677 | 20.4k | { | 678 | | /* No work necessary here */ | 679 | 20.4k | (void)cinfo; | 680 | 20.4k | } |
tif_jpeg_12.c:std_term_source Line | Count | Source | 677 | 120 | { | 678 | | /* No work necessary here */ | 679 | 120 | (void)cinfo; | 680 | 120 | } |
|
681 | | |
682 | | static void TIFFjpeg_data_src(JPEGState *sp) |
683 | 2.38k | { |
684 | 2.38k | sp->cinfo.d.src = &sp->src; |
685 | 2.38k | sp->src.init_source = std_init_source; |
686 | 2.38k | sp->src.fill_input_buffer = std_fill_input_buffer; |
687 | 2.38k | sp->src.skip_input_data = std_skip_input_data; |
688 | 2.38k | sp->src.resync_to_restart = jpeg_resync_to_restart; |
689 | 2.38k | sp->src.term_source = std_term_source; |
690 | 2.38k | sp->src.bytes_in_buffer = 0; /* for safety */ |
691 | 2.38k | sp->src.next_input_byte = NULL; |
692 | 2.38k | } tif_jpeg.c:TIFFjpeg_data_src Line | Count | Source | 683 | 2.01k | { | 684 | 2.01k | sp->cinfo.d.src = &sp->src; | 685 | 2.01k | sp->src.init_source = std_init_source; | 686 | 2.01k | sp->src.fill_input_buffer = std_fill_input_buffer; | 687 | 2.01k | sp->src.skip_input_data = std_skip_input_data; | 688 | 2.01k | sp->src.resync_to_restart = jpeg_resync_to_restart; | 689 | 2.01k | sp->src.term_source = std_term_source; | 690 | 2.01k | sp->src.bytes_in_buffer = 0; /* for safety */ | 691 | 2.01k | sp->src.next_input_byte = NULL; | 692 | 2.01k | } |
tif_jpeg_12.c:TIFFjpeg_data_src Line | Count | Source | 683 | 361 | { | 684 | 361 | sp->cinfo.d.src = &sp->src; | 685 | 361 | sp->src.init_source = std_init_source; | 686 | 361 | sp->src.fill_input_buffer = std_fill_input_buffer; | 687 | 361 | sp->src.skip_input_data = std_skip_input_data; | 688 | 361 | sp->src.resync_to_restart = jpeg_resync_to_restart; | 689 | 361 | sp->src.term_source = std_term_source; | 690 | 361 | sp->src.bytes_in_buffer = 0; /* for safety */ | 691 | 361 | sp->src.next_input_byte = NULL; | 692 | 361 | } |
|
693 | | |
694 | | /* |
695 | | * Alternate source manager for reading from JPEGTables. |
696 | | * We can share all the code except for the init routine. |
697 | | */ |
698 | | |
699 | | static void tables_init_source(j_decompress_ptr cinfo) |
700 | 9 | { |
701 | 9 | JPEGState *sp = (JPEGState *)cinfo; |
702 | | |
703 | 9 | sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables; |
704 | 9 | sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length; |
705 | 9 | } tif_jpeg.c:tables_init_source Line | Count | Source | 700 | 9 | { | 701 | 9 | JPEGState *sp = (JPEGState *)cinfo; | 702 | | | 703 | 9 | sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables; | 704 | 9 | sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length; | 705 | 9 | } |
Unexecuted instantiation: tif_jpeg_12.c:tables_init_source |
706 | | |
707 | | static void TIFFjpeg_tables_src(JPEGState *sp) |
708 | 10 | { |
709 | 10 | TIFFjpeg_data_src(sp); |
710 | 10 | sp->src.init_source = tables_init_source; |
711 | 10 | } tif_jpeg.c:TIFFjpeg_tables_src Line | Count | Source | 708 | 10 | { | 709 | 10 | TIFFjpeg_data_src(sp); | 710 | 10 | sp->src.init_source = tables_init_source; | 711 | 10 | } |
Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_tables_src |
712 | | |
713 | | /* |
714 | | * Allocate downsampled-data buffers needed for downsampled I/O. |
715 | | * We use values computed in jpeg_start_compress or jpeg_start_decompress. |
716 | | * We use libjpeg's allocator so that buffers will be released automatically |
717 | | * when done with strip/tile. |
718 | | * This is also a handy place to compute samplesperclump, bytesperline. |
719 | | */ |
720 | | static int alloc_downsampled_buffers(TIFF *tif, jpeg_component_info *comp_info, |
721 | | int num_components) |
722 | 17 | { |
723 | 17 | JPEGState *sp = JState(tif); |
724 | 17 | int ci; |
725 | 17 | jpeg_component_info *compptr; |
726 | 17 | TIFF_JSAMPARRAY buf; |
727 | 17 | int samples_per_clump = 0; |
728 | | |
729 | 68 | for (ci = 0, compptr = comp_info; ci < num_components; ci++, compptr++) |
730 | 51 | { |
731 | 51 | samples_per_clump += compptr->h_samp_factor * compptr->v_samp_factor; |
732 | 51 | buf = (TIFF_JSAMPARRAY)TIFFjpeg_alloc_sarray( |
733 | 51 | sp, JPOOL_IMAGE, compptr->width_in_blocks * DCTSIZE, |
734 | 51 | (JDIMENSION)(compptr->v_samp_factor * DCTSIZE)); |
735 | 51 | if (buf == NULL) |
736 | 0 | return (0); |
737 | 51 | sp->ds_buffer[ci] = buf; |
738 | 51 | } |
739 | 17 | sp->samplesperclump = samples_per_clump; |
740 | 17 | return (1); |
741 | 17 | } tif_jpeg.c:alloc_downsampled_buffers Line | Count | Source | 722 | 12 | { | 723 | 12 | JPEGState *sp = JState(tif); | 724 | 12 | int ci; | 725 | 12 | jpeg_component_info *compptr; | 726 | 12 | TIFF_JSAMPARRAY buf; | 727 | 12 | int samples_per_clump = 0; | 728 | | | 729 | 48 | for (ci = 0, compptr = comp_info; ci < num_components; ci++, compptr++) | 730 | 36 | { | 731 | 36 | samples_per_clump += compptr->h_samp_factor * compptr->v_samp_factor; | 732 | 36 | buf = (TIFF_JSAMPARRAY)TIFFjpeg_alloc_sarray( | 733 | 36 | sp, JPOOL_IMAGE, compptr->width_in_blocks * DCTSIZE, | 734 | 36 | (JDIMENSION)(compptr->v_samp_factor * DCTSIZE)); | 735 | 36 | if (buf == NULL) | 736 | 0 | return (0); | 737 | 36 | sp->ds_buffer[ci] = buf; | 738 | 36 | } | 739 | 12 | sp->samplesperclump = samples_per_clump; | 740 | 12 | return (1); | 741 | 12 | } |
tif_jpeg_12.c:alloc_downsampled_buffers Line | Count | Source | 722 | 5 | { | 723 | 5 | JPEGState *sp = JState(tif); | 724 | 5 | int ci; | 725 | 5 | jpeg_component_info *compptr; | 726 | 5 | TIFF_JSAMPARRAY buf; | 727 | 5 | int samples_per_clump = 0; | 728 | | | 729 | 20 | for (ci = 0, compptr = comp_info; ci < num_components; ci++, compptr++) | 730 | 15 | { | 731 | 15 | samples_per_clump += compptr->h_samp_factor * compptr->v_samp_factor; | 732 | 15 | buf = (TIFF_JSAMPARRAY)TIFFjpeg_alloc_sarray( | 733 | 15 | sp, JPOOL_IMAGE, compptr->width_in_blocks * DCTSIZE, | 734 | 15 | (JDIMENSION)(compptr->v_samp_factor * DCTSIZE)); | 735 | 15 | if (buf == NULL) | 736 | 0 | return (0); | 737 | 15 | sp->ds_buffer[ci] = buf; | 738 | 15 | } | 739 | 5 | sp->samplesperclump = samples_per_clump; | 740 | 5 | return (1); | 741 | 5 | } |
|
742 | | |
743 | | /* |
744 | | * JPEG Decoding. |
745 | | */ |
746 | | |
747 | | #ifdef CHECK_JPEG_YCBCR_SUBSAMPLING |
748 | | |
749 | 118 | #define JPEG_MARKER_SOF0 0xC0 |
750 | 379 | #define JPEG_MARKER_SOF1 0xC1 |
751 | 389 | #define JPEG_MARKER_SOF2 0xC2 |
752 | 728 | #define JPEG_MARKER_SOF9 0xC9 |
753 | 829 | #define JPEG_MARKER_SOF10 0xCA |
754 | 259k | #define JPEG_MARKER_DHT 0xC4 |
755 | 58.1k | #define JPEG_MARKER_SOI 0xD8 |
756 | 258k | #define JPEG_MARKER_SOS 0xDA |
757 | 257k | #define JPEG_MARKER_DQT 0xDB |
758 | 259k | #define JPEG_MARKER_DRI 0xDD |
759 | 2.21M | #define JPEG_MARKER_APP0 0xE0 |
760 | 97.8k | #define JPEG_MARKER_COM 0xFE |
761 | | struct JPEGFixupTagsSubsamplingData |
762 | | { |
763 | | TIFF *tif; |
764 | | void *buffer; |
765 | | uint32_t buffersize; |
766 | | uint8_t *buffercurrentbyte; |
767 | | uint32_t bufferbytesleft; |
768 | | uint64_t fileoffset; |
769 | | uint64_t filebytesleft; |
770 | | uint8_t filepositioned; |
771 | | }; |
772 | | static void JPEGFixupTagsSubsampling(TIFF *tif); |
773 | | static int |
774 | | JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData *data); |
775 | | static int |
776 | | JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData *data, |
777 | | uint8_t *result); |
778 | | static int |
779 | | JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData *data, |
780 | | uint16_t *result); |
781 | | static void |
782 | | JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData *data, |
783 | | uint16_t skiplength); |
784 | | |
785 | | #endif |
786 | | |
787 | | static int JPEGFixupTags(TIFF *tif) |
788 | 3.59k | { |
789 | 3.59k | #ifdef CHECK_JPEG_YCBCR_SUBSAMPLING |
790 | 3.59k | JPEGState *sp = JState(tif); |
791 | 3.59k | if ((tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR) && |
792 | 3.59k | (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) && |
793 | 3.59k | (tif->tif_dir.td_samplesperpixel == 3) && |
794 | 3.59k | !sp->otherSettings.ycbcrsampling_fetched) |
795 | 1.56k | JPEGFixupTagsSubsampling(tif); |
796 | 3.59k | #endif |
797 | | |
798 | 3.59k | return (1); |
799 | 3.59k | } Line | Count | Source | 788 | 3.59k | { | 789 | 3.59k | #ifdef CHECK_JPEG_YCBCR_SUBSAMPLING | 790 | 3.59k | JPEGState *sp = JState(tif); | 791 | 3.59k | if ((tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR) && | 792 | 3.59k | (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) && | 793 | 3.59k | (tif->tif_dir.td_samplesperpixel == 3) && | 794 | 3.59k | !sp->otherSettings.ycbcrsampling_fetched) | 795 | 1.56k | JPEGFixupTagsSubsampling(tif); | 796 | 3.59k | #endif | 797 | | | 798 | 3.59k | return (1); | 799 | 3.59k | } |
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTags |
800 | | |
801 | | #ifdef CHECK_JPEG_YCBCR_SUBSAMPLING |
802 | | |
803 | | static void JPEGFixupTagsSubsampling(TIFF *tif) |
804 | 1.56k | { |
805 | | /* |
806 | | * Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in |
807 | | * the TIFF tags, but still use non-default (2,2) values within the jpeg |
808 | | * data stream itself. In order for TIFF applications to work properly |
809 | | * - for instance to get the strip buffer size right - it is imperative |
810 | | * that the subsampling be available before we start reading the image |
811 | | * data normally. This function will attempt to analyze the first strip in |
812 | | * order to get the sampling values from the jpeg data stream. |
813 | | * |
814 | | * Note that JPEGPreDeocode() will produce a fairly loud warning when the |
815 | | * discovered sampling does not match the default sampling (2,2) or whatever |
816 | | * was actually in the tiff tags. |
817 | | * |
818 | | * See the bug in bugzilla for details: |
819 | | * |
820 | | * http://bugzilla.remotesensing.org/show_bug.cgi?id=168 |
821 | | * |
822 | | * Frank Warmerdam, July 2002 |
823 | | * Joris Van Damme, May 2007 |
824 | | */ |
825 | 1.56k | static const char module[] = "JPEGFixupTagsSubsampling"; |
826 | 1.56k | struct JPEGFixupTagsSubsamplingData m; |
827 | 1.56k | uint64_t fileoffset = TIFFGetStrileOffset(tif, 0); |
828 | | |
829 | 1.56k | if (fileoffset == 0) |
830 | 4 | { |
831 | | /* Do not even try to check if the first strip/tile does not |
832 | | yet exist, as occurs when GDAL has created a new NULL file |
833 | | for instance. */ |
834 | 4 | return; |
835 | 4 | } |
836 | | |
837 | 1.55k | m.tif = tif; |
838 | 1.55k | m.buffersize = 2048; |
839 | 1.55k | m.buffer = _TIFFmallocExt(tif, m.buffersize); |
840 | 1.55k | if (m.buffer == NULL) |
841 | 0 | { |
842 | 0 | TIFFWarningExtR(tif, module, |
843 | 0 | "Unable to allocate memory for auto-correcting of " |
844 | 0 | "subsampling values; auto-correcting skipped"); |
845 | 0 | return; |
846 | 0 | } |
847 | 1.55k | m.buffercurrentbyte = NULL; |
848 | 1.55k | m.bufferbytesleft = 0; |
849 | 1.55k | m.fileoffset = fileoffset; |
850 | 1.55k | m.filepositioned = 0; |
851 | 1.55k | m.filebytesleft = TIFFGetStrileByteCount(tif, 0); |
852 | 1.55k | if (!JPEGFixupTagsSubsamplingSec(&m)) |
853 | 1.26k | TIFFWarningExtR( |
854 | 1.26k | tif, module, |
855 | 1.26k | "Unable to auto-correct subsampling values, likely corrupt JPEG " |
856 | 1.26k | "compressed data in first strip/tile; auto-correcting skipped"); |
857 | 1.55k | _TIFFfreeExt(tif, m.buffer); |
858 | 1.55k | } tif_jpeg.c:JPEGFixupTagsSubsampling Line | Count | Source | 804 | 1.56k | { | 805 | | /* | 806 | | * Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in | 807 | | * the TIFF tags, but still use non-default (2,2) values within the jpeg | 808 | | * data stream itself. In order for TIFF applications to work properly | 809 | | * - for instance to get the strip buffer size right - it is imperative | 810 | | * that the subsampling be available before we start reading the image | 811 | | * data normally. This function will attempt to analyze the first strip in | 812 | | * order to get the sampling values from the jpeg data stream. | 813 | | * | 814 | | * Note that JPEGPreDeocode() will produce a fairly loud warning when the | 815 | | * discovered sampling does not match the default sampling (2,2) or whatever | 816 | | * was actually in the tiff tags. | 817 | | * | 818 | | * See the bug in bugzilla for details: | 819 | | * | 820 | | * http://bugzilla.remotesensing.org/show_bug.cgi?id=168 | 821 | | * | 822 | | * Frank Warmerdam, July 2002 | 823 | | * Joris Van Damme, May 2007 | 824 | | */ | 825 | 1.56k | static const char module[] = "JPEGFixupTagsSubsampling"; | 826 | 1.56k | struct JPEGFixupTagsSubsamplingData m; | 827 | 1.56k | uint64_t fileoffset = TIFFGetStrileOffset(tif, 0); | 828 | | | 829 | 1.56k | if (fileoffset == 0) | 830 | 4 | { | 831 | | /* Do not even try to check if the first strip/tile does not | 832 | | yet exist, as occurs when GDAL has created a new NULL file | 833 | | for instance. */ | 834 | 4 | return; | 835 | 4 | } | 836 | | | 837 | 1.55k | m.tif = tif; | 838 | 1.55k | m.buffersize = 2048; | 839 | 1.55k | m.buffer = _TIFFmallocExt(tif, m.buffersize); | 840 | 1.55k | if (m.buffer == NULL) | 841 | 0 | { | 842 | 0 | TIFFWarningExtR(tif, module, | 843 | 0 | "Unable to allocate memory for auto-correcting of " | 844 | 0 | "subsampling values; auto-correcting skipped"); | 845 | 0 | return; | 846 | 0 | } | 847 | 1.55k | m.buffercurrentbyte = NULL; | 848 | 1.55k | m.bufferbytesleft = 0; | 849 | 1.55k | m.fileoffset = fileoffset; | 850 | 1.55k | m.filepositioned = 0; | 851 | 1.55k | m.filebytesleft = TIFFGetStrileByteCount(tif, 0); | 852 | 1.55k | if (!JPEGFixupTagsSubsamplingSec(&m)) | 853 | 1.26k | TIFFWarningExtR( | 854 | 1.26k | tif, module, | 855 | 1.26k | "Unable to auto-correct subsampling values, likely corrupt JPEG " | 856 | 1.26k | "compressed data in first strip/tile; auto-correcting skipped"); | 857 | 1.55k | _TIFFfreeExt(tif, m.buffer); | 858 | 1.55k | } |
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsampling |
859 | | |
860 | | static int |
861 | | JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData *data) |
862 | 1.55k | { |
863 | 1.55k | static const char module[] = "JPEGFixupTagsSubsamplingSec"; |
864 | 1.55k | uint8_t m; |
865 | 319k | while (1) |
866 | 319k | { |
867 | 2.28M | while (1) |
868 | 2.28M | { |
869 | 2.28M | if (!JPEGFixupTagsSubsamplingReadByte(data, &m)) |
870 | 258 | return (0); |
871 | 2.28M | if (m == 255) |
872 | 319k | break; |
873 | 2.28M | } |
874 | 711k | while (1) |
875 | 711k | { |
876 | 711k | if (!JPEGFixupTagsSubsamplingReadByte(data, &m)) |
877 | 74 | return (0); |
878 | 710k | if (m != 255) |
879 | 319k | break; |
880 | 710k | } |
881 | 319k | switch (m) |
882 | 319k | { |
883 | 58.1k | case JPEG_MARKER_SOI: |
884 | | /* this type of marker has no data and should be skipped */ |
885 | 58.1k | break; |
886 | 97.8k | case JPEG_MARKER_COM: |
887 | 103k | case JPEG_MARKER_APP0: |
888 | 104k | case JPEG_MARKER_APP0 + 1: |
889 | 104k | case JPEG_MARKER_APP0 + 2: |
890 | 109k | case JPEG_MARKER_APP0 + 3: |
891 | 111k | case JPEG_MARKER_APP0 + 4: |
892 | 113k | case JPEG_MARKER_APP0 + 5: |
893 | 113k | case JPEG_MARKER_APP0 + 6: |
894 | 115k | case JPEG_MARKER_APP0 + 7: |
895 | 115k | case JPEG_MARKER_APP0 + 8: |
896 | 116k | case JPEG_MARKER_APP0 + 9: |
897 | 117k | case JPEG_MARKER_APP0 + 10: |
898 | 180k | case JPEG_MARKER_APP0 + 11: |
899 | 182k | case JPEG_MARKER_APP0 + 12: |
900 | 183k | case JPEG_MARKER_APP0 + 13: |
901 | 186k | case JPEG_MARKER_APP0 + 14: |
902 | 253k | case JPEG_MARKER_APP0 + 15: |
903 | 257k | case JPEG_MARKER_DQT: |
904 | 258k | case JPEG_MARKER_SOS: |
905 | 259k | case JPEG_MARKER_DHT: |
906 | 259k | case JPEG_MARKER_DRI: |
907 | | /* this type of marker has data, but it has no use to us and |
908 | | * should be skipped */ |
909 | 259k | { |
910 | 259k | uint16_t n; |
911 | 259k | if (!JPEGFixupTagsSubsamplingReadWord(data, &n)) |
912 | 262 | return (0); |
913 | 259k | if (n < 2) |
914 | 14 | return (0); |
915 | 259k | n -= 2; |
916 | 259k | if (n > 0) |
917 | 28.1k | JPEGFixupTagsSubsamplingSkip(data, n); |
918 | 259k | } |
919 | 0 | break; |
920 | 118 | case JPEG_MARKER_SOF0: /* Baseline sequential Huffman */ |
921 | 379 | case JPEG_MARKER_SOF1: /* Extended sequential Huffman */ |
922 | 389 | case JPEG_MARKER_SOF2: /* Progressive Huffman: normally not allowed |
923 | | by TechNote, but that doesn't hurt |
924 | | supporting it */ |
925 | 728 | case JPEG_MARKER_SOF9: /* Extended sequential arithmetic */ |
926 | 829 | case JPEG_MARKER_SOF10: /* Progressive arithmetic: normally not |
927 | | allowed by TechNote, but that doesn't |
928 | | hurt supporting it */ |
929 | | /* this marker contains the subsampling factors we're scanning |
930 | | * for */ |
931 | 829 | { |
932 | 829 | uint16_t n; |
933 | 829 | uint16_t o; |
934 | 829 | uint8_t p; |
935 | 829 | uint8_t ph, pv; |
936 | 829 | if (!JPEGFixupTagsSubsamplingReadWord(data, &n)) |
937 | 210 | return (0); |
938 | 619 | if (n != 8 + data->tif->tif_dir.td_samplesperpixel * 3) |
939 | 5 | return (0); |
940 | 614 | JPEGFixupTagsSubsamplingSkip(data, 7); |
941 | 614 | if (!JPEGFixupTagsSubsamplingReadByte(data, &p)) |
942 | 159 | return (0); |
943 | 455 | ph = (p >> 4); |
944 | 455 | pv = (p & 15); |
945 | 455 | JPEGFixupTagsSubsamplingSkip(data, 1); |
946 | 1.07k | for (o = 1; o < data->tif->tif_dir.td_samplesperpixel; o++) |
947 | 800 | { |
948 | 800 | JPEGFixupTagsSubsamplingSkip(data, 1); |
949 | 800 | if (!JPEGFixupTagsSubsamplingReadByte(data, &p)) |
950 | 163 | return (0); |
951 | 637 | if (p != 0x11) |
952 | 20 | { |
953 | 20 | TIFFWarningExtR(data->tif, module, |
954 | 20 | "Subsampling values inside JPEG " |
955 | 20 | "compressed data " |
956 | 20 | "have no TIFF equivalent, " |
957 | 20 | "auto-correction of TIFF " |
958 | 20 | "subsampling values failed"); |
959 | 20 | return (1); |
960 | 20 | } |
961 | 617 | JPEGFixupTagsSubsamplingSkip(data, 1); |
962 | 617 | } |
963 | 272 | if (((ph != 1) && (ph != 2) && (ph != 4)) || |
964 | 272 | ((pv != 1) && (pv != 2) && (pv != 4))) |
965 | 2 | { |
966 | 2 | TIFFWarningExtR(data->tif, module, |
967 | 2 | "Subsampling values inside JPEG " |
968 | 2 | "compressed data have no TIFF " |
969 | 2 | "equivalent, auto-correction of TIFF " |
970 | 2 | "subsampling values failed"); |
971 | 2 | return (1); |
972 | 2 | } |
973 | 270 | if ((ph != data->tif->tif_dir.td_ycbcrsubsampling[0]) || |
974 | 270 | (pv != data->tif->tif_dir.td_ycbcrsubsampling[1])) |
975 | 239 | { |
976 | 239 | TIFFWarningExtR( |
977 | 239 | data->tif, module, |
978 | 239 | "Auto-corrected former TIFF subsampling values " |
979 | 239 | "[%" PRIu16 ",%" PRIu16 |
980 | 239 | "] to match subsampling values inside JPEG " |
981 | 239 | "compressed data [%" PRIu8 ",%" PRIu8 "]", |
982 | 239 | data->tif->tif_dir.td_ycbcrsubsampling[0], |
983 | 239 | data->tif->tif_dir.td_ycbcrsubsampling[1], ph, pv); |
984 | 239 | data->tif->tif_dir.td_ycbcrsubsampling[0] = ph; |
985 | 239 | data->tif->tif_dir.td_ycbcrsubsampling[1] = pv; |
986 | 239 | } |
987 | 270 | } |
988 | 0 | return (1); |
989 | 119 | default: |
990 | 119 | return (0); |
991 | 319k | } |
992 | 319k | } |
993 | 1.55k | } tif_jpeg.c:JPEGFixupTagsSubsamplingSec Line | Count | Source | 862 | 1.55k | { | 863 | 1.55k | static const char module[] = "JPEGFixupTagsSubsamplingSec"; | 864 | 1.55k | uint8_t m; | 865 | 319k | while (1) | 866 | 319k | { | 867 | 2.28M | while (1) | 868 | 2.28M | { | 869 | 2.28M | if (!JPEGFixupTagsSubsamplingReadByte(data, &m)) | 870 | 258 | return (0); | 871 | 2.28M | if (m == 255) | 872 | 319k | break; | 873 | 2.28M | } | 874 | 711k | while (1) | 875 | 711k | { | 876 | 711k | if (!JPEGFixupTagsSubsamplingReadByte(data, &m)) | 877 | 74 | return (0); | 878 | 710k | if (m != 255) | 879 | 319k | break; | 880 | 710k | } | 881 | 319k | switch (m) | 882 | 319k | { | 883 | 58.1k | case JPEG_MARKER_SOI: | 884 | | /* this type of marker has no data and should be skipped */ | 885 | 58.1k | break; | 886 | 97.8k | case JPEG_MARKER_COM: | 887 | 103k | case JPEG_MARKER_APP0: | 888 | 104k | case JPEG_MARKER_APP0 + 1: | 889 | 104k | case JPEG_MARKER_APP0 + 2: | 890 | 109k | case JPEG_MARKER_APP0 + 3: | 891 | 111k | case JPEG_MARKER_APP0 + 4: | 892 | 113k | case JPEG_MARKER_APP0 + 5: | 893 | 113k | case JPEG_MARKER_APP0 + 6: | 894 | 115k | case JPEG_MARKER_APP0 + 7: | 895 | 115k | case JPEG_MARKER_APP0 + 8: | 896 | 116k | case JPEG_MARKER_APP0 + 9: | 897 | 117k | case JPEG_MARKER_APP0 + 10: | 898 | 180k | case JPEG_MARKER_APP0 + 11: | 899 | 182k | case JPEG_MARKER_APP0 + 12: | 900 | 183k | case JPEG_MARKER_APP0 + 13: | 901 | 186k | case JPEG_MARKER_APP0 + 14: | 902 | 253k | case JPEG_MARKER_APP0 + 15: | 903 | 257k | case JPEG_MARKER_DQT: | 904 | 258k | case JPEG_MARKER_SOS: | 905 | 259k | case JPEG_MARKER_DHT: | 906 | 259k | case JPEG_MARKER_DRI: | 907 | | /* this type of marker has data, but it has no use to us and | 908 | | * should be skipped */ | 909 | 259k | { | 910 | 259k | uint16_t n; | 911 | 259k | if (!JPEGFixupTagsSubsamplingReadWord(data, &n)) | 912 | 262 | return (0); | 913 | 259k | if (n < 2) | 914 | 14 | return (0); | 915 | 259k | n -= 2; | 916 | 259k | if (n > 0) | 917 | 28.1k | JPEGFixupTagsSubsamplingSkip(data, n); | 918 | 259k | } | 919 | 0 | break; | 920 | 118 | case JPEG_MARKER_SOF0: /* Baseline sequential Huffman */ | 921 | 379 | case JPEG_MARKER_SOF1: /* Extended sequential Huffman */ | 922 | 389 | case JPEG_MARKER_SOF2: /* Progressive Huffman: normally not allowed | 923 | | by TechNote, but that doesn't hurt | 924 | | supporting it */ | 925 | 728 | case JPEG_MARKER_SOF9: /* Extended sequential arithmetic */ | 926 | 829 | case JPEG_MARKER_SOF10: /* Progressive arithmetic: normally not | 927 | | allowed by TechNote, but that doesn't | 928 | | hurt supporting it */ | 929 | | /* this marker contains the subsampling factors we're scanning | 930 | | * for */ | 931 | 829 | { | 932 | 829 | uint16_t n; | 933 | 829 | uint16_t o; | 934 | 829 | uint8_t p; | 935 | 829 | uint8_t ph, pv; | 936 | 829 | if (!JPEGFixupTagsSubsamplingReadWord(data, &n)) | 937 | 210 | return (0); | 938 | 619 | if (n != 8 + data->tif->tif_dir.td_samplesperpixel * 3) | 939 | 5 | return (0); | 940 | 614 | JPEGFixupTagsSubsamplingSkip(data, 7); | 941 | 614 | if (!JPEGFixupTagsSubsamplingReadByte(data, &p)) | 942 | 159 | return (0); | 943 | 455 | ph = (p >> 4); | 944 | 455 | pv = (p & 15); | 945 | 455 | JPEGFixupTagsSubsamplingSkip(data, 1); | 946 | 1.07k | for (o = 1; o < data->tif->tif_dir.td_samplesperpixel; o++) | 947 | 800 | { | 948 | 800 | JPEGFixupTagsSubsamplingSkip(data, 1); | 949 | 800 | if (!JPEGFixupTagsSubsamplingReadByte(data, &p)) | 950 | 163 | return (0); | 951 | 637 | if (p != 0x11) | 952 | 20 | { | 953 | 20 | TIFFWarningExtR(data->tif, module, | 954 | 20 | "Subsampling values inside JPEG " | 955 | 20 | "compressed data " | 956 | 20 | "have no TIFF equivalent, " | 957 | 20 | "auto-correction of TIFF " | 958 | 20 | "subsampling values failed"); | 959 | 20 | return (1); | 960 | 20 | } | 961 | 617 | JPEGFixupTagsSubsamplingSkip(data, 1); | 962 | 617 | } | 963 | 272 | if (((ph != 1) && (ph != 2) && (ph != 4)) || | 964 | 272 | ((pv != 1) && (pv != 2) && (pv != 4))) | 965 | 2 | { | 966 | 2 | TIFFWarningExtR(data->tif, module, | 967 | 2 | "Subsampling values inside JPEG " | 968 | 2 | "compressed data have no TIFF " | 969 | 2 | "equivalent, auto-correction of TIFF " | 970 | 2 | "subsampling values failed"); | 971 | 2 | return (1); | 972 | 2 | } | 973 | 270 | if ((ph != data->tif->tif_dir.td_ycbcrsubsampling[0]) || | 974 | 270 | (pv != data->tif->tif_dir.td_ycbcrsubsampling[1])) | 975 | 239 | { | 976 | 239 | TIFFWarningExtR( | 977 | 239 | data->tif, module, | 978 | 239 | "Auto-corrected former TIFF subsampling values " | 979 | 239 | "[%" PRIu16 ",%" PRIu16 | 980 | 239 | "] to match subsampling values inside JPEG " | 981 | 239 | "compressed data [%" PRIu8 ",%" PRIu8 "]", | 982 | 239 | data->tif->tif_dir.td_ycbcrsubsampling[0], | 983 | 239 | data->tif->tif_dir.td_ycbcrsubsampling[1], ph, pv); | 984 | 239 | data->tif->tif_dir.td_ycbcrsubsampling[0] = ph; | 985 | 239 | data->tif->tif_dir.td_ycbcrsubsampling[1] = pv; | 986 | 239 | } | 987 | 270 | } | 988 | 0 | return (1); | 989 | 119 | default: | 990 | 119 | return (0); | 991 | 319k | } | 992 | 319k | } | 993 | 1.55k | } |
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingSec |
994 | | |
995 | | static int |
996 | | JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData *data, |
997 | | uint8_t *result) |
998 | 3.51M | { |
999 | 3.51M | if (data->bufferbytesleft == 0) |
1000 | 5.22k | { |
1001 | 5.22k | uint32_t m; |
1002 | 5.22k | if (data->filebytesleft == 0) |
1003 | 75 | return (0); |
1004 | 5.14k | if (!data->filepositioned) |
1005 | 2.88k | { |
1006 | 2.88k | if (TIFFSeekFile(data->tif, data->fileoffset, SEEK_SET) == |
1007 | 2.88k | (toff_t)-1) |
1008 | 211 | { |
1009 | 211 | return 0; |
1010 | 211 | } |
1011 | 2.67k | data->filepositioned = 1; |
1012 | 2.67k | } |
1013 | 4.93k | m = data->buffersize; |
1014 | 4.93k | if ((uint64_t)m > data->filebytesleft) |
1015 | 446 | m = (uint32_t)data->filebytesleft; |
1016 | 4.93k | assert(m < 0x80000000UL); |
1017 | 4.93k | if (TIFFReadFile(data->tif, data->buffer, (tmsize_t)m) != (tmsize_t)m) |
1018 | 840 | return (0); |
1019 | 4.09k | data->buffercurrentbyte = data->buffer; |
1020 | 4.09k | data->bufferbytesleft = m; |
1021 | 4.09k | data->fileoffset += m; |
1022 | 4.09k | data->filebytesleft -= m; |
1023 | 4.09k | } |
1024 | 3.51M | *result = *data->buffercurrentbyte; |
1025 | 3.51M | data->buffercurrentbyte++; |
1026 | 3.51M | data->bufferbytesleft--; |
1027 | 3.51M | return (1); |
1028 | 3.51M | } tif_jpeg.c:JPEGFixupTagsSubsamplingReadByte Line | Count | Source | 998 | 3.51M | { | 999 | 3.51M | if (data->bufferbytesleft == 0) | 1000 | 5.22k | { | 1001 | 5.22k | uint32_t m; | 1002 | 5.22k | if (data->filebytesleft == 0) | 1003 | 75 | return (0); | 1004 | 5.14k | if (!data->filepositioned) | 1005 | 2.88k | { | 1006 | 2.88k | if (TIFFSeekFile(data->tif, data->fileoffset, SEEK_SET) == | 1007 | 2.88k | (toff_t)-1) | 1008 | 211 | { | 1009 | 211 | return 0; | 1010 | 211 | } | 1011 | 2.67k | data->filepositioned = 1; | 1012 | 2.67k | } | 1013 | 4.93k | m = data->buffersize; | 1014 | 4.93k | if ((uint64_t)m > data->filebytesleft) | 1015 | 446 | m = (uint32_t)data->filebytesleft; | 1016 | 4.93k | assert(m < 0x80000000UL); | 1017 | 4.93k | if (TIFFReadFile(data->tif, data->buffer, (tmsize_t)m) != (tmsize_t)m) | 1018 | 840 | return (0); | 1019 | 4.09k | data->buffercurrentbyte = data->buffer; | 1020 | 4.09k | data->bufferbytesleft = m; | 1021 | 4.09k | data->fileoffset += m; | 1022 | 4.09k | data->filebytesleft -= m; | 1023 | 4.09k | } | 1024 | 3.51M | *result = *data->buffercurrentbyte; | 1025 | 3.51M | data->buffercurrentbyte++; | 1026 | 3.51M | data->bufferbytesleft--; | 1027 | 3.51M | return (1); | 1028 | 3.51M | } |
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingReadByte |
1029 | | |
1030 | | static int |
1031 | | JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData *data, |
1032 | | uint16_t *result) |
1033 | 260k | { |
1034 | 260k | uint8_t ma; |
1035 | 260k | uint8_t mb; |
1036 | 260k | if (!JPEGFixupTagsSubsamplingReadByte(data, &ma)) |
1037 | 191 | return (0); |
1038 | 260k | if (!JPEGFixupTagsSubsamplingReadByte(data, &mb)) |
1039 | 281 | return (0); |
1040 | 260k | *result = (ma << 8) | mb; |
1041 | 260k | return (1); |
1042 | 260k | } tif_jpeg.c:JPEGFixupTagsSubsamplingReadWord Line | Count | Source | 1033 | 260k | { | 1034 | 260k | uint8_t ma; | 1035 | 260k | uint8_t mb; | 1036 | 260k | if (!JPEGFixupTagsSubsamplingReadByte(data, &ma)) | 1037 | 191 | return (0); | 1038 | 260k | if (!JPEGFixupTagsSubsamplingReadByte(data, &mb)) | 1039 | 281 | return (0); | 1040 | 260k | *result = (ma << 8) | mb; | 1041 | 260k | return (1); | 1042 | 260k | } |
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingReadWord |
1043 | | |
1044 | | static void |
1045 | | JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData *data, |
1046 | | uint16_t skiplength) |
1047 | 30.6k | { |
1048 | 30.6k | if ((uint32_t)skiplength <= data->bufferbytesleft) |
1049 | 29.2k | { |
1050 | 29.2k | data->buffercurrentbyte += skiplength; |
1051 | 29.2k | data->bufferbytesleft -= skiplength; |
1052 | 29.2k | } |
1053 | 1.46k | else |
1054 | 1.46k | { |
1055 | 1.46k | uint16_t m; |
1056 | 1.46k | m = (uint16_t)(skiplength - data->bufferbytesleft); |
1057 | 1.46k | if (m <= data->filebytesleft) |
1058 | 1.41k | { |
1059 | 1.41k | data->bufferbytesleft = 0; |
1060 | 1.41k | data->fileoffset += m; |
1061 | 1.41k | data->filebytesleft -= m; |
1062 | 1.41k | data->filepositioned = 0; |
1063 | 1.41k | } |
1064 | 43 | else |
1065 | 43 | { |
1066 | 43 | data->bufferbytesleft = 0; |
1067 | 43 | data->filebytesleft = 0; |
1068 | 43 | } |
1069 | 1.46k | } |
1070 | 30.6k | } tif_jpeg.c:JPEGFixupTagsSubsamplingSkip Line | Count | Source | 1047 | 30.6k | { | 1048 | 30.6k | if ((uint32_t)skiplength <= data->bufferbytesleft) | 1049 | 29.2k | { | 1050 | 29.2k | data->buffercurrentbyte += skiplength; | 1051 | 29.2k | data->bufferbytesleft -= skiplength; | 1052 | 29.2k | } | 1053 | 1.46k | else | 1054 | 1.46k | { | 1055 | 1.46k | uint16_t m; | 1056 | 1.46k | m = (uint16_t)(skiplength - data->bufferbytesleft); | 1057 | 1.46k | if (m <= data->filebytesleft) | 1058 | 1.41k | { | 1059 | 1.41k | data->bufferbytesleft = 0; | 1060 | 1.41k | data->fileoffset += m; | 1061 | 1.41k | data->filebytesleft -= m; | 1062 | 1.41k | data->filepositioned = 0; | 1063 | 1.41k | } | 1064 | 43 | else | 1065 | 43 | { | 1066 | 43 | data->bufferbytesleft = 0; | 1067 | 43 | data->filebytesleft = 0; | 1068 | 43 | } | 1069 | 1.46k | } | 1070 | 30.6k | } |
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingSkip |
1071 | | |
1072 | | #endif |
1073 | | |
1074 | | static int JPEGSetupDecode(TIFF *tif) |
1075 | 2.73k | { |
1076 | 2.73k | JPEGState *sp = JState(tif); |
1077 | 2.73k | TIFFDirectory *td = &tif->tif_dir; |
1078 | | |
1079 | | #if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12) |
1080 | 2.37k | if (tif->tif_dir.td_bitspersample == 12) |
1081 | 361 | { |
1082 | | /* We pass a pointer to a copy of otherSettings, since */ |
1083 | | /* TIFFReInitJPEG_12() will clear sp */ |
1084 | 361 | JPEGOtherSettings savedOtherSettings = sp->otherSettings; |
1085 | 361 | return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0); |
1086 | 361 | } |
1087 | 2.01k | #endif |
1088 | | |
1089 | 2.37k | JPEGInitializeLibJPEG(tif, TRUE); |
1090 | | |
1091 | 2.01k | assert(sp != NULL); |
1092 | 2.37k | assert(sp->cinfo.comm.is_decompressor); |
1093 | | |
1094 | | /* Read JPEGTables if it is present */ |
1095 | 2.37k | if (TIFFFieldSet(tif, FIELD_JPEGTABLES)) |
1096 | 10 | { |
1097 | 10 | TIFFjpeg_tables_src(sp); |
1098 | 10 | if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY) |
1099 | 4 | { |
1100 | 4 | TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field"); |
1101 | 4 | return (0); |
1102 | 4 | } |
1103 | 10 | } |
1104 | | |
1105 | | /* Grab parameters that are same for all strips/tiles */ |
1106 | 2.37k | sp->photometric = td->td_photometric; |
1107 | 2.37k | switch (sp->photometric) |
1108 | 2.37k | { |
1109 | 360 | case PHOTOMETRIC_YCBCR: |
1110 | 360 | sp->h_sampling = td->td_ycbcrsubsampling[0]; |
1111 | 360 | sp->v_sampling = td->td_ycbcrsubsampling[1]; |
1112 | 360 | break; |
1113 | 2.01k | default: |
1114 | | /* TIFF 6.0 forbids subsampling of all other color spaces */ |
1115 | 2.01k | sp->h_sampling = 1; |
1116 | 2.01k | sp->v_sampling = 1; |
1117 | 2.01k | break; |
1118 | 2.37k | } |
1119 | | |
1120 | | /* Set up for reading normal data */ |
1121 | 2.37k | TIFFjpeg_data_src(sp); |
1122 | 2.37k | tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */ |
1123 | 2.37k | return (1); |
1124 | 2.37k | } tif_jpeg.c:JPEGSetupDecode Line | Count | Source | 1075 | 2.37k | { | 1076 | 2.37k | JPEGState *sp = JState(tif); | 1077 | 2.37k | TIFFDirectory *td = &tif->tif_dir; | 1078 | | | 1079 | 2.37k | #if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12) | 1080 | 2.37k | if (tif->tif_dir.td_bitspersample == 12) | 1081 | 361 | { | 1082 | | /* We pass a pointer to a copy of otherSettings, since */ | 1083 | | /* TIFFReInitJPEG_12() will clear sp */ | 1084 | 361 | JPEGOtherSettings savedOtherSettings = sp->otherSettings; | 1085 | 361 | return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0); | 1086 | 361 | } | 1087 | 2.01k | #endif | 1088 | | | 1089 | 2.01k | JPEGInitializeLibJPEG(tif, TRUE); | 1090 | | | 1091 | 2.01k | assert(sp != NULL); | 1092 | 2.01k | assert(sp->cinfo.comm.is_decompressor); | 1093 | | | 1094 | | /* Read JPEGTables if it is present */ | 1095 | 2.01k | if (TIFFFieldSet(tif, FIELD_JPEGTABLES)) | 1096 | 10 | { | 1097 | 10 | TIFFjpeg_tables_src(sp); | 1098 | 10 | if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY) | 1099 | 4 | { | 1100 | 4 | TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field"); | 1101 | 4 | return (0); | 1102 | 4 | } | 1103 | 10 | } | 1104 | | | 1105 | | /* Grab parameters that are same for all strips/tiles */ | 1106 | 2.00k | sp->photometric = td->td_photometric; | 1107 | 2.00k | switch (sp->photometric) | 1108 | 2.00k | { | 1109 | 324 | case PHOTOMETRIC_YCBCR: | 1110 | 324 | sp->h_sampling = td->td_ycbcrsubsampling[0]; | 1111 | 324 | sp->v_sampling = td->td_ycbcrsubsampling[1]; | 1112 | 324 | break; | 1113 | 1.68k | default: | 1114 | | /* TIFF 6.0 forbids subsampling of all other color spaces */ | 1115 | 1.68k | sp->h_sampling = 1; | 1116 | 1.68k | sp->v_sampling = 1; | 1117 | 1.68k | break; | 1118 | 2.00k | } | 1119 | | | 1120 | | /* Set up for reading normal data */ | 1121 | 2.00k | TIFFjpeg_data_src(sp); | 1122 | 2.00k | tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */ | 1123 | 2.00k | return (1); | 1124 | 2.00k | } |
tif_jpeg_12.c:JPEGSetupDecode Line | Count | Source | 1075 | 361 | { | 1076 | 361 | JPEGState *sp = JState(tif); | 1077 | 361 | TIFFDirectory *td = &tif->tif_dir; | 1078 | | | 1079 | | #if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12) | 1080 | | if (tif->tif_dir.td_bitspersample == 12) | 1081 | | { | 1082 | | /* We pass a pointer to a copy of otherSettings, since */ | 1083 | | /* TIFFReInitJPEG_12() will clear sp */ | 1084 | | JPEGOtherSettings savedOtherSettings = sp->otherSettings; | 1085 | | return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0); | 1086 | | } | 1087 | | #endif | 1088 | | | 1089 | 361 | JPEGInitializeLibJPEG(tif, TRUE); | 1090 | | | 1091 | 361 | assert(sp != NULL); | 1092 | 361 | assert(sp->cinfo.comm.is_decompressor); | 1093 | | | 1094 | | /* Read JPEGTables if it is present */ | 1095 | 361 | if (TIFFFieldSet(tif, FIELD_JPEGTABLES)) | 1096 | 0 | { | 1097 | 0 | TIFFjpeg_tables_src(sp); | 1098 | 0 | if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY) | 1099 | 0 | { | 1100 | 0 | TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field"); | 1101 | 0 | return (0); | 1102 | 0 | } | 1103 | 0 | } | 1104 | | | 1105 | | /* Grab parameters that are same for all strips/tiles */ | 1106 | 361 | sp->photometric = td->td_photometric; | 1107 | 361 | switch (sp->photometric) | 1108 | 361 | { | 1109 | 36 | case PHOTOMETRIC_YCBCR: | 1110 | 36 | sp->h_sampling = td->td_ycbcrsubsampling[0]; | 1111 | 36 | sp->v_sampling = td->td_ycbcrsubsampling[1]; | 1112 | 36 | break; | 1113 | 325 | default: | 1114 | | /* TIFF 6.0 forbids subsampling of all other color spaces */ | 1115 | 325 | sp->h_sampling = 1; | 1116 | 325 | sp->v_sampling = 1; | 1117 | 325 | break; | 1118 | 361 | } | 1119 | | | 1120 | | /* Set up for reading normal data */ | 1121 | 361 | TIFFjpeg_data_src(sp); | 1122 | 361 | tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */ | 1123 | 361 | return (1); | 1124 | 361 | } |
|
1125 | | |
1126 | | /* Returns 1 if the full strip should be read, even when doing scanline per */ |
1127 | | /* scanline decoding. This happens when the JPEG stream uses multiple scans. */ |
1128 | | /* Currently only called in CHUNKY_STRIP_READ_SUPPORT mode through */ |
1129 | | /* scanline interface. */ |
1130 | | /* Only reads tif->tif_dir.td_bitspersample, tif->tif_rawdata and */ |
1131 | | /* tif->tif_rawcc members. */ |
1132 | | /* Can be called independently of the usual setup/predecode/decode states */ |
1133 | | int TIFFJPEGIsFullStripRequired(TIFF *tif) |
1134 | 0 | { |
1135 | 0 | int ret; |
1136 | 0 | JPEGState state; |
1137 | |
|
1138 | | #if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12) |
1139 | 0 | if (tif->tif_dir.td_bitspersample == 12) |
1140 | 0 | return TIFFJPEGIsFullStripRequired_12(tif); |
1141 | 0 | #endif |
1142 | | |
1143 | 0 | memset(&state, 0, sizeof(JPEGState)); |
1144 | 0 | state.tif = tif; |
1145 | |
|
1146 | 0 | TIFFjpeg_create_decompress(&state); |
1147 | |
|
1148 | 0 | TIFFjpeg_data_src(&state); |
1149 | |
|
1150 | 0 | if (TIFFjpeg_read_header(&state, TRUE) != JPEG_HEADER_OK) |
1151 | 0 | { |
1152 | 0 | TIFFjpeg_destroy(&state); |
1153 | 0 | return (0); |
1154 | 0 | } |
1155 | 0 | ret = TIFFjpeg_has_multiple_scans(&state); |
1156 | |
|
1157 | 0 | TIFFjpeg_destroy(&state); |
1158 | |
|
1159 | 0 | return ret; |
1160 | 0 | } Unexecuted instantiation: TIFFJPEGIsFullStripRequired Unexecuted instantiation: TIFFJPEGIsFullStripRequired_12 |
1161 | | |
1162 | | /* |
1163 | | * Set up for decoding a strip or tile. |
1164 | | */ |
1165 | | /*ARGSUSED*/ static int JPEGPreDecode(TIFF *tif, uint16_t s) |
1166 | 155k | { |
1167 | 155k | JPEGState *sp = JState(tif); |
1168 | 155k | TIFFDirectory *td = &tif->tif_dir; |
1169 | 155k | static const char module[] = "JPEGPreDecode"; |
1170 | 155k | uint32_t segment_width, segment_height; |
1171 | 155k | int downsampled_output; |
1172 | 155k | int ci; |
1173 | | |
1174 | 155k | assert(sp != NULL); |
1175 | | |
1176 | 155k | if (sp->cinfo.comm.is_decompressor == 0) |
1177 | 0 | { |
1178 | 0 | tif->tif_setupdecode(tif); |
1179 | 0 | } |
1180 | | |
1181 | 155k | assert(sp->cinfo.comm.is_decompressor); |
1182 | | /* |
1183 | | * Reset decoder state from any previous strip/tile, |
1184 | | * in case application didn't read the whole strip. |
1185 | | */ |
1186 | 155k | if (!TIFFjpeg_abort(sp)) |
1187 | 0 | return (0); |
1188 | | /* |
1189 | | * Read the header for this strip/tile. |
1190 | | */ |
1191 | | |
1192 | 155k | if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK) |
1193 | 110k | return (0); |
1194 | | |
1195 | 45.5k | tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte; |
1196 | 45.5k | tif->tif_rawcc = sp->src.bytes_in_buffer; |
1197 | | |
1198 | | /* |
1199 | | * Check image parameters and set decompression parameters. |
1200 | | */ |
1201 | 45.5k | if (isTiled(tif)) |
1202 | 44.4k | { |
1203 | 44.4k | segment_width = td->td_tilewidth; |
1204 | 44.4k | segment_height = td->td_tilelength; |
1205 | 44.4k | sp->bytesperline = TIFFTileRowSize(tif); |
1206 | 44.4k | } |
1207 | 1.13k | else |
1208 | 1.13k | { |
1209 | 1.13k | segment_width = td->td_imagewidth; |
1210 | 1.13k | segment_height = td->td_imagelength - tif->tif_row; |
1211 | 1.13k | if (segment_height > td->td_rowsperstrip) |
1212 | 730 | segment_height = td->td_rowsperstrip; |
1213 | 1.13k | sp->bytesperline = TIFFScanlineSize(tif); |
1214 | 1.13k | } |
1215 | 45.5k | if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) |
1216 | 64 | { |
1217 | | /* |
1218 | | * For PC 2, scale down the expected strip/tile size |
1219 | | * to match a downsampled component |
1220 | | */ |
1221 | 64 | if (sp->h_sampling == 0 || sp->v_sampling == 0) |
1222 | 0 | { |
1223 | 0 | TIFFErrorExtR(tif, module, |
1224 | 0 | "JPEG horizontal or vertical sampling is zero"); |
1225 | 0 | return (0); |
1226 | 0 | } |
1227 | 64 | segment_width = TIFFhowmany_32(segment_width, sp->h_sampling); |
1228 | 64 | segment_height = TIFFhowmany_32(segment_height, sp->v_sampling); |
1229 | 64 | } |
1230 | 45.5k | if (sp->cinfo.d.image_width < segment_width || |
1231 | 45.5k | sp->cinfo.d.image_height < segment_height) |
1232 | 44.5k | { |
1233 | 44.5k | TIFFWarningExtR(tif, module, |
1234 | 44.5k | "Improper JPEG strip/tile size, " |
1235 | 44.5k | "expected %" PRIu32 "x%" PRIu32 ", got %ux%u", |
1236 | 44.5k | segment_width, segment_height, sp->cinfo.d.image_width, |
1237 | 44.5k | sp->cinfo.d.image_height); |
1238 | 44.5k | } |
1239 | 45.5k | if (sp->cinfo.d.image_width == segment_width && |
1240 | 45.5k | sp->cinfo.d.image_height > segment_height && |
1241 | 45.5k | tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif)) |
1242 | 122 | { |
1243 | | /* Some files have a last strip, that should be truncated, */ |
1244 | | /* but their JPEG codestream has still the maximum strip */ |
1245 | | /* height. Warn about this as this is non compliant, but */ |
1246 | | /* we can safely recover from that. */ |
1247 | 122 | TIFFWarningExtR(tif, module, |
1248 | 122 | "JPEG strip size exceeds expected dimensions," |
1249 | 122 | " expected %" PRIu32 "x%" PRIu32 ", got %ux%u", |
1250 | 122 | segment_width, segment_height, sp->cinfo.d.image_width, |
1251 | 122 | sp->cinfo.d.image_height); |
1252 | 122 | } |
1253 | 45.4k | else if (sp->cinfo.d.image_width > segment_width || |
1254 | 45.4k | sp->cinfo.d.image_height > segment_height) |
1255 | 1.10k | { |
1256 | | /* |
1257 | | * This case could be dangerous, if the strip or tile size has |
1258 | | * been reported as less than the amount of data jpeg will |
1259 | | * return, some potential security issues arise. Catch this |
1260 | | * case and error out. |
1261 | | */ |
1262 | 1.10k | TIFFErrorExtR(tif, module, |
1263 | 1.10k | "JPEG strip/tile size exceeds expected dimensions," |
1264 | 1.10k | " expected %" PRIu32 "x%" PRIu32 ", got %ux%u", |
1265 | 1.10k | segment_width, segment_height, sp->cinfo.d.image_width, |
1266 | 1.10k | sp->cinfo.d.image_height); |
1267 | 1.10k | return (0); |
1268 | 1.10k | } |
1269 | 44.4k | if (sp->cinfo.d.num_components != |
1270 | 44.4k | (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel |
1271 | 44.4k | : 1)) |
1272 | 289 | { |
1273 | 289 | TIFFErrorExtR(tif, module, "Improper JPEG component count"); |
1274 | 289 | return (0); |
1275 | 289 | } |
1276 | | #ifdef JPEG_LIB_MK1 |
1277 | | if (12 != td->td_bitspersample && 8 != td->td_bitspersample) |
1278 | | { |
1279 | | TIFFErrorExtR(tif, module, "Improper JPEG data precision"); |
1280 | | return (0); |
1281 | | } |
1282 | | sp->cinfo.d.data_precision = td->td_bitspersample; |
1283 | | sp->cinfo.d.bits_in_jsample = td->td_bitspersample; |
1284 | | #else |
1285 | 44.1k | if (sp->cinfo.d.data_precision != td->td_bitspersample) |
1286 | 260 | { |
1287 | 260 | TIFFErrorExtR(tif, module, "Improper JPEG data precision"); |
1288 | 260 | return (0); |
1289 | 260 | } |
1290 | 43.9k | #endif |
1291 | | |
1292 | 43.9k | if (sp->cinfo.d.progressive_mode && |
1293 | 43.9k | !sp->otherSettings.has_warned_about_progressive_mode) |
1294 | 789 | { |
1295 | 789 | TIFFWarningExtR(tif, module, |
1296 | 789 | "The JPEG strip/tile is encoded with progressive mode, " |
1297 | 789 | "which is normally not legal for JPEG-in-TIFF.\n" |
1298 | 789 | "libtiff should be able to decode it, but it might " |
1299 | 789 | "cause compatibility issues with other readers"); |
1300 | 789 | sp->otherSettings.has_warned_about_progressive_mode = TRUE; |
1301 | 789 | } |
1302 | | |
1303 | | /* In some cases, libjpeg needs to allocate a lot of memory */ |
1304 | | /* http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf |
1305 | | */ |
1306 | 43.9k | if (TIFFjpeg_has_multiple_scans(sp)) |
1307 | 30.2k | { |
1308 | | /* In this case libjpeg will need to allocate memory or backing */ |
1309 | | /* store for all coefficients */ |
1310 | | /* See call to jinit_d_coef_controller() from master_selection() */ |
1311 | | /* in libjpeg */ |
1312 | | |
1313 | | /* 1 MB for regular libjpeg usage */ |
1314 | 30.2k | toff_t nRequiredMemory = 1024 * 1024; |
1315 | | |
1316 | 94.5k | for (ci = 0; ci < sp->cinfo.d.num_components; ci++) |
1317 | 64.2k | { |
1318 | 64.2k | const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]); |
1319 | 64.2k | if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0) |
1320 | 64.2k | { |
1321 | 64.2k | nRequiredMemory += |
1322 | 64.2k | (toff_t)(((compptr->width_in_blocks + |
1323 | 64.2k | compptr->h_samp_factor - 1) / |
1324 | 64.2k | compptr->h_samp_factor)) * |
1325 | 64.2k | ((compptr->height_in_blocks + compptr->v_samp_factor - 1) / |
1326 | 64.2k | compptr->v_samp_factor) * |
1327 | 64.2k | sizeof(JBLOCK); |
1328 | 64.2k | } |
1329 | 64.2k | } |
1330 | | |
1331 | 30.2k | if (sp->cinfo.d.mem->max_memory_to_use > 0 && |
1332 | 30.2k | nRequiredMemory > (toff_t)(sp->cinfo.d.mem->max_memory_to_use) && |
1333 | 30.2k | getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL) |
1334 | 0 | { |
1335 | 0 | TIFFErrorExtR( |
1336 | 0 | tif, module, |
1337 | 0 | "Reading this image would require libjpeg to allocate " |
1338 | 0 | "at least %" PRIu64 " bytes. " |
1339 | 0 | "This is disabled since above the %ld threshold. " |
1340 | 0 | "You may override this restriction by defining the " |
1341 | 0 | "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, " |
1342 | 0 | "or setting the JPEGMEM environment variable to a value " |
1343 | 0 | "greater " |
1344 | 0 | "or equal to '%" PRIu64 "M'", |
1345 | 0 | nRequiredMemory, sp->cinfo.d.mem->max_memory_to_use, |
1346 | 0 | (nRequiredMemory + 1000000u - 1u) / 1000000u); |
1347 | 0 | return 0; |
1348 | 0 | } |
1349 | 30.2k | } |
1350 | | |
1351 | 43.9k | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
1352 | 43.2k | { |
1353 | | /* Component 0 should have expected sampling factors */ |
1354 | 43.2k | if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling || |
1355 | 43.2k | sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) |
1356 | 495 | { |
1357 | 495 | TIFFErrorExtR(tif, module, |
1358 | 495 | "Improper JPEG sampling factors %d,%d\n" |
1359 | 495 | "Apparently should be %" PRIu16 ",%" PRIu16 ".", |
1360 | 495 | sp->cinfo.d.comp_info[0].h_samp_factor, |
1361 | 495 | sp->cinfo.d.comp_info[0].v_samp_factor, |
1362 | 495 | sp->h_sampling, sp->v_sampling); |
1363 | 495 | return (0); |
1364 | 495 | } |
1365 | | /* Rest should have sampling factors 1,1 */ |
1366 | 76.8k | for (ci = 1; ci < sp->cinfo.d.num_components; ci++) |
1367 | 34.0k | { |
1368 | 34.0k | if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 || |
1369 | 34.0k | sp->cinfo.d.comp_info[ci].v_samp_factor != 1) |
1370 | 0 | { |
1371 | 0 | TIFFErrorExtR(tif, module, "Improper JPEG sampling factors"); |
1372 | 0 | return (0); |
1373 | 0 | } |
1374 | 34.0k | } |
1375 | 42.7k | } |
1376 | 632 | else |
1377 | 632 | { |
1378 | | /* PC 2's single component should have sampling factors 1,1 */ |
1379 | 632 | if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 || |
1380 | 632 | sp->cinfo.d.comp_info[0].v_samp_factor != 1) |
1381 | 249 | { |
1382 | 249 | TIFFErrorExtR(tif, module, "Improper JPEG sampling factors"); |
1383 | 249 | return (0); |
1384 | 249 | } |
1385 | 632 | } |
1386 | 43.1k | downsampled_output = FALSE; |
1387 | 43.1k | if (td->td_planarconfig == PLANARCONFIG_CONTIG && |
1388 | 43.1k | sp->photometric == PHOTOMETRIC_YCBCR && |
1389 | 43.1k | sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB) |
1390 | 5.97k | { |
1391 | | /* Convert YCbCr to RGB */ |
1392 | 5.97k | sp->cinfo.d.jpeg_color_space = JCS_YCbCr; |
1393 | 5.97k | sp->cinfo.d.out_color_space = JCS_RGB; |
1394 | 5.97k | } |
1395 | 37.1k | else |
1396 | 37.1k | { |
1397 | | /* Suppress colorspace handling */ |
1398 | 37.1k | sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN; |
1399 | 37.1k | sp->cinfo.d.out_color_space = JCS_UNKNOWN; |
1400 | 37.1k | if (td->td_planarconfig == PLANARCONFIG_CONTIG && |
1401 | 37.1k | (sp->h_sampling != 1 || sp->v_sampling != 1)) |
1402 | 49 | downsampled_output = TRUE; |
1403 | | /* XXX what about up-sampling? */ |
1404 | 37.1k | } |
1405 | 43.1k | if (downsampled_output) |
1406 | 49 | { |
1407 | | /* Need to use raw-data interface to libjpeg */ |
1408 | 49 | sp->cinfo.d.raw_data_out = TRUE; |
1409 | | #if JPEG_LIB_VERSION >= 70 |
1410 | | sp->cinfo.d.do_fancy_upsampling = FALSE; |
1411 | | #endif /* JPEG_LIB_VERSION >= 70 */ |
1412 | 49 | tif->tif_decoderow = DecodeRowError; |
1413 | 49 | tif->tif_decodestrip = JPEGDecodeRaw; |
1414 | 49 | tif->tif_decodetile = JPEGDecodeRaw; |
1415 | 49 | } |
1416 | 43.1k | else |
1417 | 43.1k | { |
1418 | | /* Use normal interface to libjpeg */ |
1419 | 43.1k | sp->cinfo.d.raw_data_out = FALSE; |
1420 | 43.1k | tif->tif_decoderow = JPEGDecode; |
1421 | 43.1k | tif->tif_decodestrip = JPEGDecode; |
1422 | 43.1k | tif->tif_decodetile = JPEGDecode; |
1423 | 43.1k | } |
1424 | | /* Start JPEG decompressor */ |
1425 | 43.1k | if (!TIFFjpeg_start_decompress(sp)) |
1426 | 13.4k | return (0); |
1427 | | /* Allocate downsampled-data buffers if needed */ |
1428 | 29.7k | if (downsampled_output) |
1429 | 17 | { |
1430 | 17 | if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info, |
1431 | 17 | sp->cinfo.d.num_components)) |
1432 | 0 | return (0); |
1433 | 17 | sp->scancount = DCTSIZE; /* mark buffer empty */ |
1434 | 17 | } |
1435 | 29.7k | return (1); |
1436 | 29.7k | } Line | Count | Source | 1166 | 155k | { | 1167 | 155k | JPEGState *sp = JState(tif); | 1168 | 155k | TIFFDirectory *td = &tif->tif_dir; | 1169 | 155k | static const char module[] = "JPEGPreDecode"; | 1170 | 155k | uint32_t segment_width, segment_height; | 1171 | 155k | int downsampled_output; | 1172 | 155k | int ci; | 1173 | | | 1174 | 155k | assert(sp != NULL); | 1175 | | | 1176 | 155k | if (sp->cinfo.comm.is_decompressor == 0) | 1177 | 0 | { | 1178 | 0 | tif->tif_setupdecode(tif); | 1179 | 0 | } | 1180 | | | 1181 | 155k | assert(sp->cinfo.comm.is_decompressor); | 1182 | | /* | 1183 | | * Reset decoder state from any previous strip/tile, | 1184 | | * in case application didn't read the whole strip. | 1185 | | */ | 1186 | 155k | if (!TIFFjpeg_abort(sp)) | 1187 | 0 | return (0); | 1188 | | /* | 1189 | | * Read the header for this strip/tile. | 1190 | | */ | 1191 | | | 1192 | 155k | if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK) | 1193 | 110k | return (0); | 1194 | | | 1195 | 45.2k | tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte; | 1196 | 45.2k | tif->tif_rawcc = sp->src.bytes_in_buffer; | 1197 | | | 1198 | | /* | 1199 | | * Check image parameters and set decompression parameters. | 1200 | | */ | 1201 | 45.2k | if (isTiled(tif)) | 1202 | 44.4k | { | 1203 | 44.4k | segment_width = td->td_tilewidth; | 1204 | 44.4k | segment_height = td->td_tilelength; | 1205 | 44.4k | sp->bytesperline = TIFFTileRowSize(tif); | 1206 | 44.4k | } | 1207 | 777 | else | 1208 | 777 | { | 1209 | 777 | segment_width = td->td_imagewidth; | 1210 | 777 | segment_height = td->td_imagelength - tif->tif_row; | 1211 | 777 | if (segment_height > td->td_rowsperstrip) | 1212 | 730 | segment_height = td->td_rowsperstrip; | 1213 | 777 | sp->bytesperline = TIFFScanlineSize(tif); | 1214 | 777 | } | 1215 | 45.2k | if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) | 1216 | 64 | { | 1217 | | /* | 1218 | | * For PC 2, scale down the expected strip/tile size | 1219 | | * to match a downsampled component | 1220 | | */ | 1221 | 64 | if (sp->h_sampling == 0 || sp->v_sampling == 0) | 1222 | 0 | { | 1223 | 0 | TIFFErrorExtR(tif, module, | 1224 | 0 | "JPEG horizontal or vertical sampling is zero"); | 1225 | 0 | return (0); | 1226 | 0 | } | 1227 | 64 | segment_width = TIFFhowmany_32(segment_width, sp->h_sampling); | 1228 | 64 | segment_height = TIFFhowmany_32(segment_height, sp->v_sampling); | 1229 | 64 | } | 1230 | 45.2k | if (sp->cinfo.d.image_width < segment_width || | 1231 | 45.2k | sp->cinfo.d.image_height < segment_height) | 1232 | 44.2k | { | 1233 | 44.2k | TIFFWarningExtR(tif, module, | 1234 | 44.2k | "Improper JPEG strip/tile size, " | 1235 | 44.2k | "expected %" PRIu32 "x%" PRIu32 ", got %ux%u", | 1236 | 44.2k | segment_width, segment_height, sp->cinfo.d.image_width, | 1237 | 44.2k | sp->cinfo.d.image_height); | 1238 | 44.2k | } | 1239 | 45.2k | if (sp->cinfo.d.image_width == segment_width && | 1240 | 45.2k | sp->cinfo.d.image_height > segment_height && | 1241 | 45.2k | tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif)) | 1242 | 17 | { | 1243 | | /* Some files have a last strip, that should be truncated, */ | 1244 | | /* but their JPEG codestream has still the maximum strip */ | 1245 | | /* height. Warn about this as this is non compliant, but */ | 1246 | | /* we can safely recover from that. */ | 1247 | 17 | TIFFWarningExtR(tif, module, | 1248 | 17 | "JPEG strip size exceeds expected dimensions," | 1249 | 17 | " expected %" PRIu32 "x%" PRIu32 ", got %ux%u", | 1250 | 17 | segment_width, segment_height, sp->cinfo.d.image_width, | 1251 | 17 | sp->cinfo.d.image_height); | 1252 | 17 | } | 1253 | 45.1k | else if (sp->cinfo.d.image_width > segment_width || | 1254 | 45.1k | sp->cinfo.d.image_height > segment_height) | 1255 | 1.10k | { | 1256 | | /* | 1257 | | * This case could be dangerous, if the strip or tile size has | 1258 | | * been reported as less than the amount of data jpeg will | 1259 | | * return, some potential security issues arise. Catch this | 1260 | | * case and error out. | 1261 | | */ | 1262 | 1.10k | TIFFErrorExtR(tif, module, | 1263 | 1.10k | "JPEG strip/tile size exceeds expected dimensions," | 1264 | 1.10k | " expected %" PRIu32 "x%" PRIu32 ", got %ux%u", | 1265 | 1.10k | segment_width, segment_height, sp->cinfo.d.image_width, | 1266 | 1.10k | sp->cinfo.d.image_height); | 1267 | 1.10k | return (0); | 1268 | 1.10k | } | 1269 | 44.0k | if (sp->cinfo.d.num_components != | 1270 | 44.0k | (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel | 1271 | 44.0k | : 1)) | 1272 | 289 | { | 1273 | 289 | TIFFErrorExtR(tif, module, "Improper JPEG component count"); | 1274 | 289 | return (0); | 1275 | 289 | } | 1276 | | #ifdef JPEG_LIB_MK1 | 1277 | | if (12 != td->td_bitspersample && 8 != td->td_bitspersample) | 1278 | | { | 1279 | | TIFFErrorExtR(tif, module, "Improper JPEG data precision"); | 1280 | | return (0); | 1281 | | } | 1282 | | sp->cinfo.d.data_precision = td->td_bitspersample; | 1283 | | sp->cinfo.d.bits_in_jsample = td->td_bitspersample; | 1284 | | #else | 1285 | 43.8k | if (sp->cinfo.d.data_precision != td->td_bitspersample) | 1286 | 259 | { | 1287 | 259 | TIFFErrorExtR(tif, module, "Improper JPEG data precision"); | 1288 | 259 | return (0); | 1289 | 259 | } | 1290 | 43.5k | #endif | 1291 | | | 1292 | 43.5k | if (sp->cinfo.d.progressive_mode && | 1293 | 43.5k | !sp->otherSettings.has_warned_about_progressive_mode) | 1294 | 643 | { | 1295 | 643 | TIFFWarningExtR(tif, module, | 1296 | 643 | "The JPEG strip/tile is encoded with progressive mode, " | 1297 | 643 | "which is normally not legal for JPEG-in-TIFF.\n" | 1298 | 643 | "libtiff should be able to decode it, but it might " | 1299 | 643 | "cause compatibility issues with other readers"); | 1300 | 643 | sp->otherSettings.has_warned_about_progressive_mode = TRUE; | 1301 | 643 | } | 1302 | | | 1303 | | /* In some cases, libjpeg needs to allocate a lot of memory */ | 1304 | | /* http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf | 1305 | | */ | 1306 | 43.5k | if (TIFFjpeg_has_multiple_scans(sp)) | 1307 | 29.9k | { | 1308 | | /* In this case libjpeg will need to allocate memory or backing */ | 1309 | | /* store for all coefficients */ | 1310 | | /* See call to jinit_d_coef_controller() from master_selection() */ | 1311 | | /* in libjpeg */ | 1312 | | | 1313 | | /* 1 MB for regular libjpeg usage */ | 1314 | 29.9k | toff_t nRequiredMemory = 1024 * 1024; | 1315 | | | 1316 | 93.1k | for (ci = 0; ci < sp->cinfo.d.num_components; ci++) | 1317 | 63.2k | { | 1318 | 63.2k | const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]); | 1319 | 63.2k | if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0) | 1320 | 63.2k | { | 1321 | 63.2k | nRequiredMemory += | 1322 | 63.2k | (toff_t)(((compptr->width_in_blocks + | 1323 | 63.2k | compptr->h_samp_factor - 1) / | 1324 | 63.2k | compptr->h_samp_factor)) * | 1325 | 63.2k | ((compptr->height_in_blocks + compptr->v_samp_factor - 1) / | 1326 | 63.2k | compptr->v_samp_factor) * | 1327 | 63.2k | sizeof(JBLOCK); | 1328 | 63.2k | } | 1329 | 63.2k | } | 1330 | | | 1331 | 29.9k | if (sp->cinfo.d.mem->max_memory_to_use > 0 && | 1332 | 29.9k | nRequiredMemory > (toff_t)(sp->cinfo.d.mem->max_memory_to_use) && | 1333 | 29.9k | getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL) | 1334 | 0 | { | 1335 | 0 | TIFFErrorExtR( | 1336 | 0 | tif, module, | 1337 | 0 | "Reading this image would require libjpeg to allocate " | 1338 | 0 | "at least %" PRIu64 " bytes. " | 1339 | 0 | "This is disabled since above the %ld threshold. " | 1340 | 0 | "You may override this restriction by defining the " | 1341 | 0 | "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, " | 1342 | 0 | "or setting the JPEGMEM environment variable to a value " | 1343 | 0 | "greater " | 1344 | 0 | "or equal to '%" PRIu64 "M'", | 1345 | 0 | nRequiredMemory, sp->cinfo.d.mem->max_memory_to_use, | 1346 | 0 | (nRequiredMemory + 1000000u - 1u) / 1000000u); | 1347 | 0 | return 0; | 1348 | 0 | } | 1349 | 29.9k | } | 1350 | | | 1351 | 43.5k | if (td->td_planarconfig == PLANARCONFIG_CONTIG) | 1352 | 42.9k | { | 1353 | | /* Component 0 should have expected sampling factors */ | 1354 | 42.9k | if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling || | 1355 | 42.9k | sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) | 1356 | 495 | { | 1357 | 495 | TIFFErrorExtR(tif, module, | 1358 | 495 | "Improper JPEG sampling factors %d,%d\n" | 1359 | 495 | "Apparently should be %" PRIu16 ",%" PRIu16 ".", | 1360 | 495 | sp->cinfo.d.comp_info[0].h_samp_factor, | 1361 | 495 | sp->cinfo.d.comp_info[0].v_samp_factor, | 1362 | 495 | sp->h_sampling, sp->v_sampling); | 1363 | 495 | return (0); | 1364 | 495 | } | 1365 | | /* Rest should have sampling factors 1,1 */ | 1366 | 75.7k | for (ci = 1; ci < sp->cinfo.d.num_components; ci++) | 1367 | 33.3k | { | 1368 | 33.3k | if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 || | 1369 | 33.3k | sp->cinfo.d.comp_info[ci].v_samp_factor != 1) | 1370 | 0 | { | 1371 | 0 | TIFFErrorExtR(tif, module, "Improper JPEG sampling factors"); | 1372 | 0 | return (0); | 1373 | 0 | } | 1374 | 33.3k | } | 1375 | 42.4k | } | 1376 | 632 | else | 1377 | 632 | { | 1378 | | /* PC 2's single component should have sampling factors 1,1 */ | 1379 | 632 | if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 || | 1380 | 632 | sp->cinfo.d.comp_info[0].v_samp_factor != 1) | 1381 | 249 | { | 1382 | 249 | TIFFErrorExtR(tif, module, "Improper JPEG sampling factors"); | 1383 | 249 | return (0); | 1384 | 249 | } | 1385 | 632 | } | 1386 | 42.8k | downsampled_output = FALSE; | 1387 | 42.8k | if (td->td_planarconfig == PLANARCONFIG_CONTIG && | 1388 | 42.8k | sp->photometric == PHOTOMETRIC_YCBCR && | 1389 | 42.8k | sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB) | 1390 | 5.97k | { | 1391 | | /* Convert YCbCr to RGB */ | 1392 | 5.97k | sp->cinfo.d.jpeg_color_space = JCS_YCbCr; | 1393 | 5.97k | sp->cinfo.d.out_color_space = JCS_RGB; | 1394 | 5.97k | } | 1395 | 36.8k | else | 1396 | 36.8k | { | 1397 | | /* Suppress colorspace handling */ | 1398 | 36.8k | sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN; | 1399 | 36.8k | sp->cinfo.d.out_color_space = JCS_UNKNOWN; | 1400 | 36.8k | if (td->td_planarconfig == PLANARCONFIG_CONTIG && | 1401 | 36.8k | (sp->h_sampling != 1 || sp->v_sampling != 1)) | 1402 | 14 | downsampled_output = TRUE; | 1403 | | /* XXX what about up-sampling? */ | 1404 | 36.8k | } | 1405 | 42.8k | if (downsampled_output) | 1406 | 14 | { | 1407 | | /* Need to use raw-data interface to libjpeg */ | 1408 | 14 | sp->cinfo.d.raw_data_out = TRUE; | 1409 | | #if JPEG_LIB_VERSION >= 70 | 1410 | | sp->cinfo.d.do_fancy_upsampling = FALSE; | 1411 | | #endif /* JPEG_LIB_VERSION >= 70 */ | 1412 | 14 | tif->tif_decoderow = DecodeRowError; | 1413 | 14 | tif->tif_decodestrip = JPEGDecodeRaw; | 1414 | 14 | tif->tif_decodetile = JPEGDecodeRaw; | 1415 | 14 | } | 1416 | 42.7k | else | 1417 | 42.7k | { | 1418 | | /* Use normal interface to libjpeg */ | 1419 | 42.7k | sp->cinfo.d.raw_data_out = FALSE; | 1420 | 42.7k | tif->tif_decoderow = JPEGDecode; | 1421 | 42.7k | tif->tif_decodestrip = JPEGDecode; | 1422 | 42.7k | tif->tif_decodetile = JPEGDecode; | 1423 | 42.7k | } | 1424 | | /* Start JPEG decompressor */ | 1425 | 42.8k | if (!TIFFjpeg_start_decompress(sp)) | 1426 | 13.3k | return (0); | 1427 | | /* Allocate downsampled-data buffers if needed */ | 1428 | 29.4k | if (downsampled_output) | 1429 | 12 | { | 1430 | 12 | if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info, | 1431 | 12 | sp->cinfo.d.num_components)) | 1432 | 0 | return (0); | 1433 | 12 | sp->scancount = DCTSIZE; /* mark buffer empty */ | 1434 | 12 | } | 1435 | 29.4k | return (1); | 1436 | 29.4k | } |
tif_jpeg_12.c:JPEGPreDecode Line | Count | Source | 1166 | 361 | { | 1167 | 361 | JPEGState *sp = JState(tif); | 1168 | 361 | TIFFDirectory *td = &tif->tif_dir; | 1169 | 361 | static const char module[] = "JPEGPreDecode"; | 1170 | 361 | uint32_t segment_width, segment_height; | 1171 | 361 | int downsampled_output; | 1172 | 361 | int ci; | 1173 | | | 1174 | 361 | assert(sp != NULL); | 1175 | | | 1176 | 361 | if (sp->cinfo.comm.is_decompressor == 0) | 1177 | 0 | { | 1178 | 0 | tif->tif_setupdecode(tif); | 1179 | 0 | } | 1180 | | | 1181 | 361 | assert(sp->cinfo.comm.is_decompressor); | 1182 | | /* | 1183 | | * Reset decoder state from any previous strip/tile, | 1184 | | * in case application didn't read the whole strip. | 1185 | | */ | 1186 | 361 | if (!TIFFjpeg_abort(sp)) | 1187 | 0 | return (0); | 1188 | | /* | 1189 | | * Read the header for this strip/tile. | 1190 | | */ | 1191 | | | 1192 | 361 | if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK) | 1193 | 3 | return (0); | 1194 | | | 1195 | 358 | tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte; | 1196 | 358 | tif->tif_rawcc = sp->src.bytes_in_buffer; | 1197 | | | 1198 | | /* | 1199 | | * Check image parameters and set decompression parameters. | 1200 | | */ | 1201 | 358 | if (isTiled(tif)) | 1202 | 0 | { | 1203 | 0 | segment_width = td->td_tilewidth; | 1204 | 0 | segment_height = td->td_tilelength; | 1205 | 0 | sp->bytesperline = TIFFTileRowSize(tif); | 1206 | 0 | } | 1207 | 358 | else | 1208 | 358 | { | 1209 | 358 | segment_width = td->td_imagewidth; | 1210 | 358 | segment_height = td->td_imagelength - tif->tif_row; | 1211 | 358 | if (segment_height > td->td_rowsperstrip) | 1212 | 0 | segment_height = td->td_rowsperstrip; | 1213 | 358 | sp->bytesperline = TIFFScanlineSize(tif); | 1214 | 358 | } | 1215 | 358 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) | 1216 | 0 | { | 1217 | | /* | 1218 | | * For PC 2, scale down the expected strip/tile size | 1219 | | * to match a downsampled component | 1220 | | */ | 1221 | 0 | if (sp->h_sampling == 0 || sp->v_sampling == 0) | 1222 | 0 | { | 1223 | 0 | TIFFErrorExtR(tif, module, | 1224 | 0 | "JPEG horizontal or vertical sampling is zero"); | 1225 | 0 | return (0); | 1226 | 0 | } | 1227 | 0 | segment_width = TIFFhowmany_32(segment_width, sp->h_sampling); | 1228 | 0 | segment_height = TIFFhowmany_32(segment_height, sp->v_sampling); | 1229 | 0 | } | 1230 | 358 | if (sp->cinfo.d.image_width < segment_width || | 1231 | 358 | sp->cinfo.d.image_height < segment_height) | 1232 | 253 | { | 1233 | 253 | TIFFWarningExtR(tif, module, | 1234 | 253 | "Improper JPEG strip/tile size, " | 1235 | 253 | "expected %" PRIu32 "x%" PRIu32 ", got %ux%u", | 1236 | 253 | segment_width, segment_height, sp->cinfo.d.image_width, | 1237 | 253 | sp->cinfo.d.image_height); | 1238 | 253 | } | 1239 | 358 | if (sp->cinfo.d.image_width == segment_width && | 1240 | 358 | sp->cinfo.d.image_height > segment_height && | 1241 | 358 | tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif)) | 1242 | 105 | { | 1243 | | /* Some files have a last strip, that should be truncated, */ | 1244 | | /* but their JPEG codestream has still the maximum strip */ | 1245 | | /* height. Warn about this as this is non compliant, but */ | 1246 | | /* we can safely recover from that. */ | 1247 | 105 | TIFFWarningExtR(tif, module, | 1248 | 105 | "JPEG strip size exceeds expected dimensions," | 1249 | 105 | " expected %" PRIu32 "x%" PRIu32 ", got %ux%u", | 1250 | 105 | segment_width, segment_height, sp->cinfo.d.image_width, | 1251 | 105 | sp->cinfo.d.image_height); | 1252 | 105 | } | 1253 | 253 | else if (sp->cinfo.d.image_width > segment_width || | 1254 | 253 | sp->cinfo.d.image_height > segment_height) | 1255 | 0 | { | 1256 | | /* | 1257 | | * This case could be dangerous, if the strip or tile size has | 1258 | | * been reported as less than the amount of data jpeg will | 1259 | | * return, some potential security issues arise. Catch this | 1260 | | * case and error out. | 1261 | | */ | 1262 | 0 | TIFFErrorExtR(tif, module, | 1263 | 0 | "JPEG strip/tile size exceeds expected dimensions," | 1264 | 0 | " expected %" PRIu32 "x%" PRIu32 ", got %ux%u", | 1265 | 0 | segment_width, segment_height, sp->cinfo.d.image_width, | 1266 | 0 | sp->cinfo.d.image_height); | 1267 | 0 | return (0); | 1268 | 0 | } | 1269 | 358 | if (sp->cinfo.d.num_components != | 1270 | 358 | (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel | 1271 | 358 | : 1)) | 1272 | 0 | { | 1273 | 0 | TIFFErrorExtR(tif, module, "Improper JPEG component count"); | 1274 | 0 | return (0); | 1275 | 0 | } | 1276 | | #ifdef JPEG_LIB_MK1 | 1277 | | if (12 != td->td_bitspersample && 8 != td->td_bitspersample) | 1278 | | { | 1279 | | TIFFErrorExtR(tif, module, "Improper JPEG data precision"); | 1280 | | return (0); | 1281 | | } | 1282 | | sp->cinfo.d.data_precision = td->td_bitspersample; | 1283 | | sp->cinfo.d.bits_in_jsample = td->td_bitspersample; | 1284 | | #else | 1285 | 358 | if (sp->cinfo.d.data_precision != td->td_bitspersample) | 1286 | 1 | { | 1287 | 1 | TIFFErrorExtR(tif, module, "Improper JPEG data precision"); | 1288 | 1 | return (0); | 1289 | 1 | } | 1290 | 357 | #endif | 1291 | | | 1292 | 357 | if (sp->cinfo.d.progressive_mode && | 1293 | 357 | !sp->otherSettings.has_warned_about_progressive_mode) | 1294 | 146 | { | 1295 | 146 | TIFFWarningExtR(tif, module, | 1296 | 146 | "The JPEG strip/tile is encoded with progressive mode, " | 1297 | 146 | "which is normally not legal for JPEG-in-TIFF.\n" | 1298 | 146 | "libtiff should be able to decode it, but it might " | 1299 | 146 | "cause compatibility issues with other readers"); | 1300 | 146 | sp->otherSettings.has_warned_about_progressive_mode = TRUE; | 1301 | 146 | } | 1302 | | | 1303 | | /* In some cases, libjpeg needs to allocate a lot of memory */ | 1304 | | /* http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf | 1305 | | */ | 1306 | 357 | if (TIFFjpeg_has_multiple_scans(sp)) | 1307 | 352 | { | 1308 | | /* In this case libjpeg will need to allocate memory or backing */ | 1309 | | /* store for all coefficients */ | 1310 | | /* See call to jinit_d_coef_controller() from master_selection() */ | 1311 | | /* in libjpeg */ | 1312 | | | 1313 | | /* 1 MB for regular libjpeg usage */ | 1314 | 352 | toff_t nRequiredMemory = 1024 * 1024; | 1315 | | | 1316 | 1.40k | for (ci = 0; ci < sp->cinfo.d.num_components; ci++) | 1317 | 1.05k | { | 1318 | 1.05k | const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]); | 1319 | 1.05k | if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0) | 1320 | 1.05k | { | 1321 | 1.05k | nRequiredMemory += | 1322 | 1.05k | (toff_t)(((compptr->width_in_blocks + | 1323 | 1.05k | compptr->h_samp_factor - 1) / | 1324 | 1.05k | compptr->h_samp_factor)) * | 1325 | 1.05k | ((compptr->height_in_blocks + compptr->v_samp_factor - 1) / | 1326 | 1.05k | compptr->v_samp_factor) * | 1327 | 1.05k | sizeof(JBLOCK); | 1328 | 1.05k | } | 1329 | 1.05k | } | 1330 | | | 1331 | 352 | if (sp->cinfo.d.mem->max_memory_to_use > 0 && | 1332 | 352 | nRequiredMemory > (toff_t)(sp->cinfo.d.mem->max_memory_to_use) && | 1333 | 352 | getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL) | 1334 | 0 | { | 1335 | 0 | TIFFErrorExtR( | 1336 | 0 | tif, module, | 1337 | 0 | "Reading this image would require libjpeg to allocate " | 1338 | 0 | "at least %" PRIu64 " bytes. " | 1339 | 0 | "This is disabled since above the %ld threshold. " | 1340 | 0 | "You may override this restriction by defining the " | 1341 | 0 | "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, " | 1342 | 0 | "or setting the JPEGMEM environment variable to a value " | 1343 | 0 | "greater " | 1344 | 0 | "or equal to '%" PRIu64 "M'", | 1345 | 0 | nRequiredMemory, sp->cinfo.d.mem->max_memory_to_use, | 1346 | 0 | (nRequiredMemory + 1000000u - 1u) / 1000000u); | 1347 | 0 | return 0; | 1348 | 0 | } | 1349 | 352 | } | 1350 | | | 1351 | 357 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) | 1352 | 357 | { | 1353 | | /* Component 0 should have expected sampling factors */ | 1354 | 357 | if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling || | 1355 | 357 | sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) | 1356 | 0 | { | 1357 | 0 | TIFFErrorExtR(tif, module, | 1358 | 0 | "Improper JPEG sampling factors %d,%d\n" | 1359 | 0 | "Apparently should be %" PRIu16 ",%" PRIu16 ".", | 1360 | 0 | sp->cinfo.d.comp_info[0].h_samp_factor, | 1361 | 0 | sp->cinfo.d.comp_info[0].v_samp_factor, | 1362 | 0 | sp->h_sampling, sp->v_sampling); | 1363 | 0 | return (0); | 1364 | 0 | } | 1365 | | /* Rest should have sampling factors 1,1 */ | 1366 | 1.07k | for (ci = 1; ci < sp->cinfo.d.num_components; ci++) | 1367 | 714 | { | 1368 | 714 | if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 || | 1369 | 714 | sp->cinfo.d.comp_info[ci].v_samp_factor != 1) | 1370 | 0 | { | 1371 | 0 | TIFFErrorExtR(tif, module, "Improper JPEG sampling factors"); | 1372 | 0 | return (0); | 1373 | 0 | } | 1374 | 714 | } | 1375 | 357 | } | 1376 | 0 | else | 1377 | 0 | { | 1378 | | /* PC 2's single component should have sampling factors 1,1 */ | 1379 | 0 | if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 || | 1380 | 0 | sp->cinfo.d.comp_info[0].v_samp_factor != 1) | 1381 | 0 | { | 1382 | 0 | TIFFErrorExtR(tif, module, "Improper JPEG sampling factors"); | 1383 | 0 | return (0); | 1384 | 0 | } | 1385 | 0 | } | 1386 | 357 | downsampled_output = FALSE; | 1387 | 357 | if (td->td_planarconfig == PLANARCONFIG_CONTIG && | 1388 | 357 | sp->photometric == PHOTOMETRIC_YCBCR && | 1389 | 357 | sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB) | 1390 | 0 | { | 1391 | | /* Convert YCbCr to RGB */ | 1392 | 0 | sp->cinfo.d.jpeg_color_space = JCS_YCbCr; | 1393 | 0 | sp->cinfo.d.out_color_space = JCS_RGB; | 1394 | 0 | } | 1395 | 357 | else | 1396 | 357 | { | 1397 | | /* Suppress colorspace handling */ | 1398 | 357 | sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN; | 1399 | 357 | sp->cinfo.d.out_color_space = JCS_UNKNOWN; | 1400 | 357 | if (td->td_planarconfig == PLANARCONFIG_CONTIG && | 1401 | 357 | (sp->h_sampling != 1 || sp->v_sampling != 1)) | 1402 | 35 | downsampled_output = TRUE; | 1403 | | /* XXX what about up-sampling? */ | 1404 | 357 | } | 1405 | 357 | if (downsampled_output) | 1406 | 35 | { | 1407 | | /* Need to use raw-data interface to libjpeg */ | 1408 | 35 | sp->cinfo.d.raw_data_out = TRUE; | 1409 | | #if JPEG_LIB_VERSION >= 70 | 1410 | | sp->cinfo.d.do_fancy_upsampling = FALSE; | 1411 | | #endif /* JPEG_LIB_VERSION >= 70 */ | 1412 | 35 | tif->tif_decoderow = DecodeRowError; | 1413 | 35 | tif->tif_decodestrip = JPEGDecodeRaw; | 1414 | 35 | tif->tif_decodetile = JPEGDecodeRaw; | 1415 | 35 | } | 1416 | 322 | else | 1417 | 322 | { | 1418 | | /* Use normal interface to libjpeg */ | 1419 | 322 | sp->cinfo.d.raw_data_out = FALSE; | 1420 | 322 | tif->tif_decoderow = JPEGDecode; | 1421 | 322 | tif->tif_decodestrip = JPEGDecode; | 1422 | 322 | tif->tif_decodetile = JPEGDecode; | 1423 | 322 | } | 1424 | | /* Start JPEG decompressor */ | 1425 | 357 | if (!TIFFjpeg_start_decompress(sp)) | 1426 | 131 | return (0); | 1427 | | /* Allocate downsampled-data buffers if needed */ | 1428 | 226 | if (downsampled_output) | 1429 | 5 | { | 1430 | 5 | if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info, | 1431 | 5 | sp->cinfo.d.num_components)) | 1432 | 0 | return (0); | 1433 | 5 | sp->scancount = DCTSIZE; /* mark buffer empty */ | 1434 | 5 | } | 1435 | 226 | return (1); | 1436 | 226 | } |
|
1437 | | |
1438 | | /* |
1439 | | * Decode a chunk of pixels. |
1440 | | * "Standard" case: returned data is not downsampled. |
1441 | | */ |
1442 | | #if !JPEG_LIB_MK1_OR_12BIT |
1443 | | static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) |
1444 | 30.6k | { |
1445 | 30.6k | JPEGState *sp = JState(tif); |
1446 | 30.6k | tmsize_t nrows; |
1447 | 30.6k | (void)s; |
1448 | | |
1449 | | /* |
1450 | | ** Update available information, buffer may have been refilled |
1451 | | ** between decode requests |
1452 | | */ |
1453 | 30.6k | sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp; |
1454 | 30.6k | sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc; |
1455 | | |
1456 | 30.6k | if (sp->bytesperline == 0) |
1457 | 0 | { |
1458 | 0 | memset(buf, 0, (size_t)cc); |
1459 | 0 | return 0; |
1460 | 0 | } |
1461 | | |
1462 | 30.6k | nrows = cc / sp->bytesperline; |
1463 | 30.6k | if (cc % sp->bytesperline) |
1464 | 0 | TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read"); |
1465 | | |
1466 | 30.6k | if (nrows > (tmsize_t)sp->cinfo.d.image_height) |
1467 | 27.4k | nrows = sp->cinfo.d.image_height; |
1468 | | |
1469 | | /* data is expected to be read in multiples of a scanline */ |
1470 | 30.6k | if (nrows) |
1471 | 30.6k | { |
1472 | 30.6k | do |
1473 | 10.2M | { |
1474 | | /* |
1475 | | * In the libjpeg6b-9a 8bit case. We read directly into |
1476 | | * the TIFF buffer. |
1477 | | */ |
1478 | 10.2M | JSAMPROW bufptr = (JSAMPROW)buf; |
1479 | | |
1480 | 10.2M | if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1) |
1481 | 3.12k | { |
1482 | 3.12k | memset(buf, 0, (size_t)cc); |
1483 | 3.12k | return (0); |
1484 | 3.12k | } |
1485 | | |
1486 | 10.2M | ++tif->tif_row; |
1487 | 10.2M | buf += sp->bytesperline; |
1488 | 10.2M | cc -= sp->bytesperline; |
1489 | 10.2M | } while (--nrows > 0); |
1490 | 30.6k | } |
1491 | | |
1492 | | /* Update information on consumed data */ |
1493 | 27.5k | tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte; |
1494 | 27.5k | tif->tif_rawcc = sp->src.bytes_in_buffer; |
1495 | | |
1496 | | /* Close down the decompressor if we've finished the strip or tile. */ |
1497 | 27.5k | return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height || |
1498 | 27.5k | TIFFjpeg_finish_decompress(sp); |
1499 | 30.6k | } |
1500 | | #endif /* !JPEG_LIB_MK1_OR_12BIT */ |
1501 | | |
1502 | | #if JPEG_LIB_MK1_OR_12BIT |
1503 | | /*ARGSUSED*/ static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, |
1504 | | uint16_t s) |
1505 | 45.3k | { |
1506 | 45.3k | JPEGState *sp = JState(tif); |
1507 | 45.3k | tmsize_t nrows; |
1508 | 45.3k | (void)s; |
1509 | | |
1510 | | /* |
1511 | | ** Update available information, buffer may have been refilled |
1512 | | ** between decode requests |
1513 | | */ |
1514 | 45.3k | sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp; |
1515 | 45.3k | sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc; |
1516 | | |
1517 | 45.3k | if (sp->bytesperline == 0) |
1518 | 0 | { |
1519 | 0 | memset(buf, 0, (size_t)cc); |
1520 | 0 | return 0; |
1521 | 0 | } |
1522 | | |
1523 | 45.3k | nrows = cc / sp->bytesperline; |
1524 | 45.3k | if (cc % sp->bytesperline) |
1525 | 0 | TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read"); |
1526 | | |
1527 | 45.3k | if (nrows > (tmsize_t)sp->cinfo.d.image_height) |
1528 | 0 | nrows = sp->cinfo.d.image_height; |
1529 | | |
1530 | | /* data is expected to be read in multiples of a scanline */ |
1531 | 45.3k | if (nrows) |
1532 | 45.3k | { |
1533 | 45.3k | TIFF_JSAMPROW line_work_buf = NULL; |
1534 | | |
1535 | | /* |
1536 | | * For 6B, only use temporary buffer for 12 bit imagery. |
1537 | | * For Mk1 always use it. |
1538 | | */ |
1539 | 45.3k | if (sp->cinfo.d.data_precision == 12) |
1540 | 45.3k | { |
1541 | 45.3k | line_work_buf = (TIFF_JSAMPROW)_TIFFmallocExt( |
1542 | 45.3k | tif, sizeof(short) * sp->cinfo.d.output_width * |
1543 | 45.3k | sp->cinfo.d.num_components); |
1544 | 45.3k | } |
1545 | | |
1546 | 45.3k | do |
1547 | 45.3k | { |
1548 | 45.3k | if (line_work_buf != NULL) |
1549 | 45.3k | { |
1550 | | /* |
1551 | | * In the MK1 case, we always read into a 16bit |
1552 | | * buffer, and then pack down to 12bit or 8bit. |
1553 | | * In 6B case we only read into 16 bit buffer |
1554 | | * for 12bit data, which we need to repack. |
1555 | | */ |
1556 | 45.3k | if (TIFFjpeg_read_scanlines(sp, &line_work_buf, 1) != 1) |
1557 | 67 | { |
1558 | 67 | memset(buf, 0, (size_t)cc); |
1559 | 67 | return (0); |
1560 | 67 | } |
1561 | | |
1562 | 45.2k | if (sp->cinfo.d.data_precision == 12) |
1563 | 45.2k | { |
1564 | 45.2k | int value_pairs = (sp->cinfo.d.output_width * |
1565 | 45.2k | sp->cinfo.d.num_components) / |
1566 | 45.2k | 2; |
1567 | 45.2k | int iPair; |
1568 | | |
1569 | 58.7M | for (iPair = 0; iPair < value_pairs; iPair++) |
1570 | 58.7M | { |
1571 | 58.7M | unsigned char *out_ptr = |
1572 | 58.7M | ((unsigned char *)buf) + iPair * 3; |
1573 | 58.7M | TIFF_JSAMPLE *in_ptr = line_work_buf + iPair * 2; |
1574 | | |
1575 | 58.7M | out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4); |
1576 | 58.7M | out_ptr[1] = |
1577 | 58.7M | (unsigned char)(((in_ptr[0] & 0xf) << 4) | |
1578 | 58.7M | ((in_ptr[1] & 0xf00) >> 8)); |
1579 | 58.7M | out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0)); |
1580 | 58.7M | } |
1581 | 45.2k | } |
1582 | 0 | else if (sp->cinfo.d.data_precision == 8) |
1583 | 0 | { |
1584 | 0 | int value_count = |
1585 | 0 | (sp->cinfo.d.output_width * sp->cinfo.d.num_components); |
1586 | 0 | int iValue; |
1587 | |
|
1588 | 0 | for (iValue = 0; iValue < value_count; iValue++) |
1589 | 0 | { |
1590 | 0 | ((unsigned char *)buf)[iValue] = |
1591 | 0 | line_work_buf[iValue] & 0xff; |
1592 | 0 | } |
1593 | 0 | } |
1594 | 45.2k | } |
1595 | | |
1596 | 45.2k | ++tif->tif_row; |
1597 | 45.2k | buf += sp->bytesperline; |
1598 | 45.2k | cc -= sp->bytesperline; |
1599 | 45.2k | } while (--nrows > 0); |
1600 | | |
1601 | 45.2k | if (line_work_buf != NULL) |
1602 | 45.2k | _TIFFfreeExt(tif, line_work_buf); |
1603 | 45.2k | } |
1604 | | |
1605 | | /* Update information on consumed data */ |
1606 | 45.2k | tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte; |
1607 | 45.2k | tif->tif_rawcc = sp->src.bytes_in_buffer; |
1608 | | |
1609 | | /* Close down the decompressor if we've finished the strip or tile. */ |
1610 | 45.2k | return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height || |
1611 | 45.2k | TIFFjpeg_finish_decompress(sp); |
1612 | 45.3k | } |
1613 | | #endif /* JPEG_LIB_MK1_OR_12BIT */ |
1614 | | |
1615 | | /*ARGSUSED*/ static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc, |
1616 | | uint16_t s) |
1617 | | |
1618 | 17 | { |
1619 | 17 | (void)buf; |
1620 | 17 | (void)cc; |
1621 | 17 | (void)s; |
1622 | | |
1623 | 17 | TIFFErrorExtR( |
1624 | 17 | tif, "TIFFReadScanline", |
1625 | 17 | "scanline oriented access is not supported for downsampled JPEG " |
1626 | 17 | "compressed images, consider enabling TIFFTAG_JPEGCOLORMODE as " |
1627 | 17 | "JPEGCOLORMODE_RGB."); |
1628 | 17 | return 0; |
1629 | 17 | } tif_jpeg.c:DecodeRowError Line | Count | Source | 1618 | 12 | { | 1619 | 12 | (void)buf; | 1620 | 12 | (void)cc; | 1621 | 12 | (void)s; | 1622 | | | 1623 | 12 | TIFFErrorExtR( | 1624 | 12 | tif, "TIFFReadScanline", | 1625 | 12 | "scanline oriented access is not supported for downsampled JPEG " | 1626 | 12 | "compressed images, consider enabling TIFFTAG_JPEGCOLORMODE as " | 1627 | 12 | "JPEGCOLORMODE_RGB."); | 1628 | 12 | return 0; | 1629 | 12 | } |
tif_jpeg_12.c:DecodeRowError Line | Count | Source | 1618 | 5 | { | 1619 | 5 | (void)buf; | 1620 | 5 | (void)cc; | 1621 | 5 | (void)s; | 1622 | | | 1623 | 5 | TIFFErrorExtR( | 1624 | 5 | tif, "TIFFReadScanline", | 1625 | 5 | "scanline oriented access is not supported for downsampled JPEG " | 1626 | 5 | "compressed images, consider enabling TIFFTAG_JPEGCOLORMODE as " | 1627 | 5 | "JPEGCOLORMODE_RGB."); | 1628 | 5 | return 0; | 1629 | 5 | } |
|
1630 | | |
1631 | | /* |
1632 | | * Decode a chunk of pixels. |
1633 | | * Returned data is downsampled per sampling factors. |
1634 | | */ |
1635 | | /*ARGSUSED*/ static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, |
1636 | | uint16_t s) |
1637 | 0 | { |
1638 | 0 | JPEGState *sp = JState(tif); |
1639 | 0 | tmsize_t nrows; |
1640 | 0 | TIFFDirectory *td = &tif->tif_dir; |
1641 | 0 | (void)s; |
1642 | |
|
1643 | 0 | nrows = sp->cinfo.d.image_height; |
1644 | | /* For last strip, limit number of rows to its truncated height */ |
1645 | | /* even if the codestream height is larger (which is not compliant, */ |
1646 | | /* but that we tolerate) */ |
1647 | 0 | if ((uint32_t)nrows > td->td_imagelength - tif->tif_row && !isTiled(tif)) |
1648 | 0 | nrows = td->td_imagelength - tif->tif_row; |
1649 | |
|
1650 | | #if defined(JPEG_LIB_MK1_OR_12BIT) |
1651 | | unsigned short *tmpbuf = NULL; |
1652 | | #endif |
1653 | | |
1654 | | /* data is expected to be read in multiples of a scanline */ |
1655 | 0 | if (nrows != 0) |
1656 | 0 | { |
1657 | | |
1658 | | /* Cb,Cr both have sampling factors 1, so this is correct */ |
1659 | 0 | JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width; |
1660 | 0 | int samples_per_clump = sp->samplesperclump; |
1661 | |
|
1662 | | #if defined(JPEG_LIB_MK1_OR_12BIT) |
1663 | | tmpbuf = _TIFFmallocExt(tif, sizeof(unsigned short) * |
1664 | | sp->cinfo.d.output_width * |
1665 | | sp->cinfo.d.num_components); |
1666 | 0 | if (tmpbuf == NULL) |
1667 | 0 | { |
1668 | 0 | TIFFErrorExtR(tif, "JPEGDecodeRaw", "Out of memory"); |
1669 | 0 | return 0; |
1670 | 0 | } |
1671 | 0 | #endif |
1672 | | |
1673 | 0 | do |
1674 | 0 | { |
1675 | 0 | jpeg_component_info *compptr; |
1676 | 0 | int ci, clumpoffset; |
1677 | |
|
1678 | 0 | if (cc < sp->bytesperline) |
1679 | 0 | { |
1680 | 0 | TIFFErrorExtR( |
1681 | 0 | tif, "JPEGDecodeRaw", |
1682 | 0 | "application buffer not large enough for all data."); |
1683 | 0 | goto error; |
1684 | 0 | } |
1685 | | |
1686 | | /* Reload downsampled-data buffer if needed */ |
1687 | 0 | if (sp->scancount >= DCTSIZE) |
1688 | 0 | { |
1689 | 0 | int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE; |
1690 | 0 | if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n) != n) |
1691 | 0 | goto error; |
1692 | 0 | sp->scancount = 0; |
1693 | 0 | } |
1694 | | /* |
1695 | | * Fastest way to unseparate data is to make one pass |
1696 | | * over the scanline for each row of each component. |
1697 | | */ |
1698 | 0 | clumpoffset = 0; /* first sample in clump */ |
1699 | 0 | for (ci = 0, compptr = sp->cinfo.d.comp_info; |
1700 | 0 | ci < sp->cinfo.d.num_components; ci++, compptr++) |
1701 | 0 | { |
1702 | 0 | int hsamp = compptr->h_samp_factor; |
1703 | 0 | int vsamp = compptr->v_samp_factor; |
1704 | 0 | int ypos; |
1705 | |
|
1706 | 0 | for (ypos = 0; ypos < vsamp; ypos++) |
1707 | 0 | { |
1708 | 0 | TIFF_JSAMPLE *inptr = |
1709 | 0 | sp->ds_buffer[ci][sp->scancount * vsamp + ypos]; |
1710 | 0 | JDIMENSION nclump; |
1711 | | #if defined(JPEG_LIB_MK1_OR_12BIT) |
1712 | 0 | TIFF_JSAMPLE *outptr = (TIFF_JSAMPLE *)tmpbuf + clumpoffset; |
1713 | | #else |
1714 | 0 | TIFF_JSAMPLE *outptr = (TIFF_JSAMPLE *)buf + clumpoffset; |
1715 | 0 | if (cc < (tmsize_t)(clumpoffset + |
1716 | 0 | (tmsize_t)samples_per_clump * |
1717 | 0 | (clumps_per_line - 1) + |
1718 | 0 | hsamp)) |
1719 | 0 | { |
1720 | 0 | TIFFErrorExtR( |
1721 | 0 | tif, "JPEGDecodeRaw", |
1722 | 0 | "application buffer not large enough for all data, " |
1723 | 0 | "possible subsampling issue"); |
1724 | 0 | goto error; |
1725 | 0 | } |
1726 | 0 | #endif |
1727 | | |
1728 | 0 | if (hsamp == 1) |
1729 | 0 | { |
1730 | | /* fast path for at least Cb and Cr */ |
1731 | 0 | for (nclump = clumps_per_line; nclump-- > 0;) |
1732 | 0 | { |
1733 | 0 | outptr[0] = *inptr++; |
1734 | 0 | outptr += samples_per_clump; |
1735 | 0 | } |
1736 | 0 | } |
1737 | 0 | else |
1738 | 0 | { |
1739 | 0 | int xpos; |
1740 | | |
1741 | | /* general case */ |
1742 | 0 | for (nclump = clumps_per_line; nclump-- > 0;) |
1743 | 0 | { |
1744 | 0 | for (xpos = 0; xpos < hsamp; xpos++) |
1745 | 0 | outptr[xpos] = *inptr++; |
1746 | 0 | outptr += samples_per_clump; |
1747 | 0 | } |
1748 | 0 | } |
1749 | 0 | clumpoffset += hsamp; |
1750 | 0 | } |
1751 | 0 | } |
1752 | | |
1753 | | #if defined(JPEG_LIB_MK1_OR_12BIT) |
1754 | | { |
1755 | 0 | if (sp->cinfo.d.data_precision == 8) |
1756 | 0 | { |
1757 | 0 | int i = 0; |
1758 | 0 | int len = |
1759 | 0 | sp->cinfo.d.output_width * sp->cinfo.d.num_components; |
1760 | 0 | for (i = 0; i < len; i++) |
1761 | 0 | { |
1762 | 0 | ((unsigned char *)buf)[i] = tmpbuf[i] & 0xff; |
1763 | 0 | } |
1764 | 0 | } |
1765 | 0 | else |
1766 | 0 | { /* 12-bit */ |
1767 | 0 | int value_pairs = (sp->cinfo.d.output_width * |
1768 | 0 | sp->cinfo.d.num_components) / |
1769 | 0 | 2; |
1770 | 0 | int iPair; |
1771 | 0 | for (iPair = 0; iPair < value_pairs; iPair++) |
1772 | 0 | { |
1773 | 0 | unsigned char *out_ptr = |
1774 | 0 | ((unsigned char *)buf) + iPair * 3; |
1775 | 0 | TIFF_JSAMPLE *in_ptr = |
1776 | 0 | (TIFF_JSAMPLE *)(tmpbuf + iPair * 2); |
1777 | 0 | out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4); |
1778 | 0 | out_ptr[1] = |
1779 | 0 | (unsigned char)(((in_ptr[0] & 0xf) << 4) | |
1780 | 0 | ((in_ptr[1] & 0xf00) >> 8)); |
1781 | 0 | out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0)); |
1782 | 0 | } |
1783 | 0 | } |
1784 | | } |
1785 | | #endif |
1786 | | |
1787 | 0 | sp->scancount++; |
1788 | 0 | tif->tif_row += sp->v_sampling; |
1789 | |
|
1790 | 0 | buf += sp->bytesperline; |
1791 | 0 | cc -= sp->bytesperline; |
1792 | |
|
1793 | 0 | nrows -= sp->v_sampling; |
1794 | 0 | } while (nrows > 0); |
1795 | | |
1796 | | #if defined(JPEG_LIB_MK1_OR_12BIT) |
1797 | 0 | _TIFFfreeExt(tif, tmpbuf); |
1798 | 0 | #endif |
1799 | 0 | } |
1800 | | |
1801 | | /* Close down the decompressor if done. */ |
1802 | 0 | return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height || |
1803 | 0 | TIFFjpeg_finish_decompress(sp); |
1804 | | |
1805 | 0 | error: |
1806 | | #if defined(JPEG_LIB_MK1_OR_12BIT) |
1807 | | _TIFFfreeExt(tif, tmpbuf); |
1808 | | #endif |
1809 | 0 | return 0; |
1810 | 0 | } Unexecuted instantiation: tif_jpeg.c:JPEGDecodeRaw Unexecuted instantiation: tif_jpeg_12.c:JPEGDecodeRaw |
1811 | | |
1812 | | /* |
1813 | | * JPEG Encoding. |
1814 | | */ |
1815 | | |
1816 | | static void unsuppress_quant_table(JPEGState *sp, int tblno) |
1817 | 0 | { |
1818 | 0 | JQUANT_TBL *qtbl; |
1819 | |
|
1820 | 0 | if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL) |
1821 | 0 | qtbl->sent_table = FALSE; |
1822 | 0 | } Unexecuted instantiation: tif_jpeg.c:unsuppress_quant_table Unexecuted instantiation: tif_jpeg_12.c:unsuppress_quant_table |
1823 | | |
1824 | | static void suppress_quant_table(JPEGState *sp, int tblno) |
1825 | 0 | { |
1826 | 0 | JQUANT_TBL *qtbl; |
1827 | |
|
1828 | 0 | if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL) |
1829 | 0 | qtbl->sent_table = TRUE; |
1830 | 0 | } Unexecuted instantiation: tif_jpeg.c:suppress_quant_table Unexecuted instantiation: tif_jpeg_12.c:suppress_quant_table |
1831 | | |
1832 | | static void unsuppress_huff_table(JPEGState *sp, int tblno) |
1833 | 0 | { |
1834 | 0 | JHUFF_TBL *htbl; |
1835 | |
|
1836 | 0 | if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL) |
1837 | 0 | htbl->sent_table = FALSE; |
1838 | 0 | if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL) |
1839 | 0 | htbl->sent_table = FALSE; |
1840 | 0 | } Unexecuted instantiation: tif_jpeg.c:unsuppress_huff_table Unexecuted instantiation: tif_jpeg_12.c:unsuppress_huff_table |
1841 | | |
1842 | | static void suppress_huff_table(JPEGState *sp, int tblno) |
1843 | 0 | { |
1844 | 0 | JHUFF_TBL *htbl; |
1845 | |
|
1846 | 0 | if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL) |
1847 | 0 | htbl->sent_table = TRUE; |
1848 | 0 | if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL) |
1849 | 0 | htbl->sent_table = TRUE; |
1850 | 0 | } Unexecuted instantiation: tif_jpeg.c:suppress_huff_table Unexecuted instantiation: tif_jpeg_12.c:suppress_huff_table |
1851 | | |
1852 | | static int prepare_JPEGTables(TIFF *tif) |
1853 | 0 | { |
1854 | 0 | JPEGState *sp = JState(tif); |
1855 | | |
1856 | | /* Initialize quant tables for current quality setting */ |
1857 | 0 | if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE)) |
1858 | 0 | return (0); |
1859 | | /* Mark only the tables we want for output */ |
1860 | | /* NB: chrominance tables are currently used only with YCbCr */ |
1861 | 0 | if (!TIFFjpeg_suppress_tables(sp, TRUE)) |
1862 | 0 | return (0); |
1863 | 0 | if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT) |
1864 | 0 | { |
1865 | 0 | unsuppress_quant_table(sp, 0); |
1866 | 0 | if (sp->photometric == PHOTOMETRIC_YCBCR) |
1867 | 0 | unsuppress_quant_table(sp, 1); |
1868 | 0 | } |
1869 | 0 | if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) |
1870 | 0 | { |
1871 | 0 | unsuppress_huff_table(sp, 0); |
1872 | 0 | if (sp->photometric == PHOTOMETRIC_YCBCR) |
1873 | 0 | unsuppress_huff_table(sp, 1); |
1874 | 0 | } |
1875 | | /* Direct libjpeg output into otherSettings.jpegtables */ |
1876 | 0 | if (!TIFFjpeg_tables_dest(sp, tif)) |
1877 | 0 | return (0); |
1878 | | /* Emit tables-only datastream */ |
1879 | 0 | if (!TIFFjpeg_write_tables(sp)) |
1880 | 0 | return (0); |
1881 | | |
1882 | 0 | return (1); |
1883 | 0 | } Unexecuted instantiation: tif_jpeg.c:prepare_JPEGTables Unexecuted instantiation: tif_jpeg_12.c:prepare_JPEGTables |
1884 | | |
1885 | | #if defined(JPEG_LIB_VERSION_MAJOR) && \ |
1886 | | (JPEG_LIB_VERSION_MAJOR > 9 || \ |
1887 | | (JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4)) |
1888 | | /* This is a modified version of std_huff_tables() from jcparam.c |
1889 | | * in libjpeg-9d because it no longer initializes default Huffman |
1890 | | * tables in jpeg_set_defaults(). */ |
1891 | | static void TIFF_std_huff_tables(j_compress_ptr cinfo) |
1892 | | { |
1893 | | |
1894 | | if (cinfo->dc_huff_tbl_ptrs[0] == NULL) |
1895 | | { |
1896 | | (void)jpeg_std_huff_table((j_common_ptr)cinfo, TRUE, 0); |
1897 | | } |
1898 | | if (cinfo->ac_huff_tbl_ptrs[0] == NULL) |
1899 | | { |
1900 | | (void)jpeg_std_huff_table((j_common_ptr)cinfo, FALSE, 0); |
1901 | | } |
1902 | | if (cinfo->dc_huff_tbl_ptrs[1] == NULL) |
1903 | | { |
1904 | | (void)jpeg_std_huff_table((j_common_ptr)cinfo, TRUE, 1); |
1905 | | } |
1906 | | if (cinfo->ac_huff_tbl_ptrs[1] == NULL) |
1907 | | { |
1908 | | (void)jpeg_std_huff_table((j_common_ptr)cinfo, FALSE, 1); |
1909 | | } |
1910 | | } |
1911 | | #endif |
1912 | | |
1913 | | static int JPEGSetupEncode(TIFF *tif) |
1914 | 0 | { |
1915 | 0 | JPEGState *sp = JState(tif); |
1916 | 0 | TIFFDirectory *td = &tif->tif_dir; |
1917 | 0 | static const char module[] = "JPEGSetupEncode"; |
1918 | |
|
1919 | | #if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12) |
1920 | 0 | if (tif->tif_dir.td_bitspersample == 12) |
1921 | 0 | { |
1922 | | /* We pass a pointer to a copy of otherSettings, since */ |
1923 | | /* TIFFReInitJPEG_12() will clear sp */ |
1924 | 0 | JPEGOtherSettings savedOtherSettings = sp->otherSettings; |
1925 | 0 | return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 1); |
1926 | 0 | } |
1927 | 0 | #endif |
1928 | | |
1929 | 0 | JPEGInitializeLibJPEG(tif, FALSE); |
1930 | |
|
1931 | 0 | assert(sp != NULL); |
1932 | 0 | assert(!sp->cinfo.comm.is_decompressor); |
1933 | | |
1934 | 0 | sp->photometric = td->td_photometric; |
1935 | | |
1936 | | /* |
1937 | | * Initialize all JPEG parameters to default values. |
1938 | | * Note that jpeg_set_defaults needs legal values for |
1939 | | * in_color_space and input_components. |
1940 | | */ |
1941 | 0 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
1942 | 0 | { |
1943 | 0 | sp->cinfo.c.input_components = td->td_samplesperpixel; |
1944 | 0 | if (sp->photometric == PHOTOMETRIC_YCBCR) |
1945 | 0 | { |
1946 | 0 | if (sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB) |
1947 | 0 | { |
1948 | 0 | sp->cinfo.c.in_color_space = JCS_RGB; |
1949 | 0 | } |
1950 | 0 | else |
1951 | 0 | { |
1952 | 0 | sp->cinfo.c.in_color_space = JCS_YCbCr; |
1953 | 0 | } |
1954 | 0 | } |
1955 | 0 | else |
1956 | 0 | { |
1957 | 0 | if ((td->td_photometric == PHOTOMETRIC_MINISWHITE || |
1958 | 0 | td->td_photometric == PHOTOMETRIC_MINISBLACK) && |
1959 | 0 | td->td_samplesperpixel == 1) |
1960 | 0 | sp->cinfo.c.in_color_space = JCS_GRAYSCALE; |
1961 | 0 | else if (td->td_photometric == PHOTOMETRIC_RGB && |
1962 | 0 | td->td_samplesperpixel == 3) |
1963 | 0 | sp->cinfo.c.in_color_space = JCS_RGB; |
1964 | 0 | else if (td->td_photometric == PHOTOMETRIC_SEPARATED && |
1965 | 0 | td->td_samplesperpixel == 4) |
1966 | 0 | sp->cinfo.c.in_color_space = JCS_CMYK; |
1967 | 0 | else |
1968 | 0 | sp->cinfo.c.in_color_space = JCS_UNKNOWN; |
1969 | 0 | } |
1970 | 0 | } |
1971 | 0 | else |
1972 | 0 | { |
1973 | 0 | sp->cinfo.c.input_components = 1; |
1974 | 0 | sp->cinfo.c.in_color_space = JCS_UNKNOWN; |
1975 | 0 | } |
1976 | 0 | if (!TIFFjpeg_set_defaults(sp)) |
1977 | 0 | return (0); |
1978 | | |
1979 | | /* mozjpeg by default enables progressive JPEG, which is illegal in |
1980 | | * JPEG-in-TIFF */ |
1981 | | /* So explicitly disable it. */ |
1982 | 0 | if (sp->cinfo.c.num_scans != 0 && |
1983 | 0 | (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0) |
1984 | 0 | { |
1985 | | /* it has been found that mozjpeg could create corrupt strips/tiles */ |
1986 | | /* in non optimize_coding mode. */ |
1987 | 0 | TIFFWarningExtR( |
1988 | 0 | tif, module, |
1989 | 0 | "mozjpeg library likely detected. Disable emission of " |
1990 | 0 | "Huffman tables in JpegTables tag, and use optimize_coding " |
1991 | 0 | "to avoid potential issues"); |
1992 | 0 | sp->otherSettings.jpegtablesmode &= ~JPEGTABLESMODE_HUFF; |
1993 | 0 | } |
1994 | 0 | sp->cinfo.c.num_scans = 0; |
1995 | 0 | sp->cinfo.c.scan_info = NULL; |
1996 | | |
1997 | | /* Set per-file parameters */ |
1998 | 0 | switch (sp->photometric) |
1999 | 0 | { |
2000 | 0 | case PHOTOMETRIC_YCBCR: |
2001 | 0 | sp->h_sampling = td->td_ycbcrsubsampling[0]; |
2002 | 0 | sp->v_sampling = td->td_ycbcrsubsampling[1]; |
2003 | 0 | if (sp->h_sampling == 0 || sp->v_sampling == 0) |
2004 | 0 | { |
2005 | 0 | TIFFErrorExtR(tif, module, |
2006 | 0 | "Invalig horizontal/vertical sampling value"); |
2007 | 0 | return (0); |
2008 | 0 | } |
2009 | 0 | if (td->td_bitspersample > 16) |
2010 | 0 | { |
2011 | 0 | TIFFErrorExtR(tif, module, |
2012 | 0 | "BitsPerSample %" PRIu16 " not allowed for JPEG", |
2013 | 0 | td->td_bitspersample); |
2014 | 0 | return (0); |
2015 | 0 | } |
2016 | | |
2017 | | /* |
2018 | | * A ReferenceBlackWhite field *must* be present since the |
2019 | | * default value is inappropriate for YCbCr. Fill in the |
2020 | | * proper value if application didn't set it. |
2021 | | */ |
2022 | 0 | { |
2023 | 0 | float *ref; |
2024 | 0 | if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE, &ref)) |
2025 | 0 | { |
2026 | 0 | float refbw[6]; |
2027 | 0 | long top = 1L << td->td_bitspersample; |
2028 | 0 | refbw[0] = 0; |
2029 | 0 | refbw[1] = (float)(top - 1L); |
2030 | 0 | refbw[2] = (float)(top >> 1); |
2031 | 0 | refbw[3] = refbw[1]; |
2032 | 0 | refbw[4] = refbw[2]; |
2033 | 0 | refbw[5] = refbw[1]; |
2034 | 0 | TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw); |
2035 | 0 | } |
2036 | 0 | } |
2037 | 0 | break; |
2038 | 0 | case PHOTOMETRIC_PALETTE: /* disallowed by Tech Note */ |
2039 | 0 | case PHOTOMETRIC_MASK: |
2040 | 0 | TIFFErrorExtR(tif, module, |
2041 | 0 | "PhotometricInterpretation %" PRIu16 |
2042 | 0 | " not allowed for JPEG", |
2043 | 0 | sp->photometric); |
2044 | 0 | return (0); |
2045 | 0 | default: |
2046 | | /* TIFF 6.0 forbids subsampling of all other color spaces */ |
2047 | 0 | sp->h_sampling = 1; |
2048 | 0 | sp->v_sampling = 1; |
2049 | 0 | break; |
2050 | 0 | } |
2051 | | |
2052 | | /* Verify miscellaneous parameters */ |
2053 | | |
2054 | | /* |
2055 | | * This would need work if libtiff ever supports different |
2056 | | * depths for different components, or if libjpeg ever supports |
2057 | | * run-time selection of depth. Neither is imminent. |
2058 | | */ |
2059 | | #ifdef JPEG_LIB_MK1 |
2060 | | /* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */ |
2061 | | if (td->td_bitspersample != 8 && td->td_bitspersample != 12) |
2062 | | #else |
2063 | 0 | if (td->td_bitspersample != BITS_IN_JSAMPLE) |
2064 | 0 | #endif |
2065 | 0 | { |
2066 | 0 | TIFFErrorExtR(tif, module, |
2067 | 0 | "BitsPerSample %" PRIu16 " not allowed for JPEG", |
2068 | 0 | td->td_bitspersample); |
2069 | 0 | return (0); |
2070 | 0 | } |
2071 | 0 | sp->cinfo.c.data_precision = td->td_bitspersample; |
2072 | | #ifdef JPEG_LIB_MK1 |
2073 | | sp->cinfo.c.bits_in_jsample = td->td_bitspersample; |
2074 | | #endif |
2075 | 0 | if (isTiled(tif)) |
2076 | 0 | { |
2077 | 0 | if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0) |
2078 | 0 | { |
2079 | 0 | TIFFErrorExtR(tif, module, |
2080 | 0 | "JPEG tile height must be multiple of %" PRIu32, |
2081 | 0 | (uint32_t)(sp->v_sampling * DCTSIZE)); |
2082 | 0 | return (0); |
2083 | 0 | } |
2084 | 0 | if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0) |
2085 | 0 | { |
2086 | 0 | TIFFErrorExtR(tif, module, |
2087 | 0 | "JPEG tile width must be multiple of %" PRIu32, |
2088 | 0 | (uint32_t)(sp->h_sampling * DCTSIZE)); |
2089 | 0 | return (0); |
2090 | 0 | } |
2091 | 0 | } |
2092 | 0 | else |
2093 | 0 | { |
2094 | 0 | if (td->td_rowsperstrip < td->td_imagelength && |
2095 | 0 | (td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0) |
2096 | 0 | { |
2097 | 0 | TIFFErrorExtR(tif, module, |
2098 | 0 | "RowsPerStrip must be multiple of %" PRIu32 |
2099 | 0 | " for JPEG", |
2100 | 0 | (uint32_t)(sp->v_sampling * DCTSIZE)); |
2101 | 0 | return (0); |
2102 | 0 | } |
2103 | 0 | } |
2104 | | |
2105 | | /* Create a JPEGTables field if appropriate */ |
2106 | 0 | if (sp->otherSettings.jpegtablesmode & |
2107 | 0 | (JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF)) |
2108 | 0 | { |
2109 | 0 | if (sp->otherSettings.jpegtables == NULL || |
2110 | 0 | memcmp(sp->otherSettings.jpegtables, "\0\0\0\0\0\0\0\0\0", 8) == 0) |
2111 | 0 | { |
2112 | | #if defined(JPEG_LIB_VERSION_MAJOR) && \ |
2113 | | (JPEG_LIB_VERSION_MAJOR > 9 || \ |
2114 | | (JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4)) |
2115 | | if ((sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0 && |
2116 | | (sp->cinfo.c.dc_huff_tbl_ptrs[0] == NULL || |
2117 | | sp->cinfo.c.dc_huff_tbl_ptrs[1] == NULL || |
2118 | | sp->cinfo.c.ac_huff_tbl_ptrs[0] == NULL || |
2119 | | sp->cinfo.c.ac_huff_tbl_ptrs[1] == NULL)) |
2120 | | { |
2121 | | /* libjpeg-9d no longer initializes default Huffman tables in */ |
2122 | | /* jpeg_set_defaults() */ |
2123 | | TIFF_std_huff_tables(&sp->cinfo.c); |
2124 | | } |
2125 | | #endif |
2126 | |
|
2127 | 0 | if (!prepare_JPEGTables(tif)) |
2128 | 0 | return (0); |
2129 | | /* Mark the field present */ |
2130 | | /* Can't use TIFFSetField since BEENWRITING is already set! */ |
2131 | 0 | tif->tif_flags |= TIFF_DIRTYDIRECT; |
2132 | 0 | TIFFSetFieldBit(tif, FIELD_JPEGTABLES); |
2133 | 0 | } |
2134 | 0 | } |
2135 | 0 | else |
2136 | 0 | { |
2137 | | /* We do not support application-supplied JPEGTables, */ |
2138 | | /* so mark the field not present */ |
2139 | 0 | TIFFClrFieldBit(tif, FIELD_JPEGTABLES); |
2140 | 0 | } |
2141 | | |
2142 | | /* Direct libjpeg output to libtiff's output buffer */ |
2143 | 0 | TIFFjpeg_data_dest(sp, tif); |
2144 | |
|
2145 | 0 | return (1); |
2146 | 0 | } Unexecuted instantiation: tif_jpeg.c:JPEGSetupEncode Unexecuted instantiation: tif_jpeg_12.c:JPEGSetupEncode |
2147 | | |
2148 | | /* |
2149 | | * Set encoding state at the start of a strip or tile. |
2150 | | */ |
2151 | | static int JPEGPreEncode(TIFF *tif, uint16_t s) |
2152 | 0 | { |
2153 | 0 | JPEGState *sp = JState(tif); |
2154 | 0 | TIFFDirectory *td = &tif->tif_dir; |
2155 | 0 | static const char module[] = "JPEGPreEncode"; |
2156 | 0 | uint32_t segment_width, segment_height; |
2157 | 0 | int downsampled_input; |
2158 | |
|
2159 | 0 | assert(sp != NULL); |
2160 | | |
2161 | 0 | if (sp->cinfo.comm.is_decompressor == 1) |
2162 | 0 | { |
2163 | 0 | tif->tif_setupencode(tif); |
2164 | 0 | } |
2165 | |
|
2166 | 0 | assert(!sp->cinfo.comm.is_decompressor); |
2167 | | /* |
2168 | | * Set encoding parameters for this strip/tile. |
2169 | | */ |
2170 | 0 | if (isTiled(tif)) |
2171 | 0 | { |
2172 | 0 | segment_width = td->td_tilewidth; |
2173 | 0 | segment_height = td->td_tilelength; |
2174 | 0 | sp->bytesperline = TIFFTileRowSize(tif); |
2175 | 0 | } |
2176 | 0 | else |
2177 | 0 | { |
2178 | 0 | segment_width = td->td_imagewidth; |
2179 | 0 | segment_height = td->td_imagelength - tif->tif_row; |
2180 | 0 | if (segment_height > td->td_rowsperstrip) |
2181 | 0 | segment_height = td->td_rowsperstrip; |
2182 | 0 | sp->bytesperline = TIFFScanlineSize(tif); |
2183 | 0 | } |
2184 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) |
2185 | 0 | { |
2186 | | /* for PC 2, scale down the strip/tile size |
2187 | | * to match a downsampled component |
2188 | | */ |
2189 | 0 | if (sp->h_sampling == 0 || sp->v_sampling == 0) |
2190 | 0 | { |
2191 | 0 | TIFFErrorExtR(tif, module, |
2192 | 0 | "JPEG horizontal or vertical sampling is zero"); |
2193 | 0 | return (0); |
2194 | 0 | } |
2195 | 0 | segment_width = TIFFhowmany_32(segment_width, sp->h_sampling); |
2196 | 0 | segment_height = TIFFhowmany_32(segment_height, sp->v_sampling); |
2197 | 0 | } |
2198 | 0 | if (segment_width > (uint32_t)JPEG_MAX_DIMENSION || |
2199 | 0 | segment_height > (uint32_t)JPEG_MAX_DIMENSION) |
2200 | 0 | { |
2201 | 0 | TIFFErrorExtR(tif, module, |
2202 | 0 | "Strip/tile too large for JPEG. Maximum dimension is %d", |
2203 | 0 | (int)JPEG_MAX_DIMENSION); |
2204 | 0 | return (0); |
2205 | 0 | } |
2206 | 0 | sp->cinfo.c.image_width = segment_width; |
2207 | 0 | sp->cinfo.c.image_height = segment_height; |
2208 | 0 | downsampled_input = FALSE; |
2209 | 0 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
2210 | 0 | { |
2211 | 0 | sp->cinfo.c.input_components = td->td_samplesperpixel; |
2212 | 0 | if (sp->photometric == PHOTOMETRIC_YCBCR) |
2213 | 0 | { |
2214 | 0 | if (sp->otherSettings.jpegcolormode != JPEGCOLORMODE_RGB) |
2215 | 0 | { |
2216 | 0 | if (sp->h_sampling != 1 || sp->v_sampling != 1) |
2217 | 0 | downsampled_input = TRUE; |
2218 | 0 | } |
2219 | 0 | if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr)) |
2220 | 0 | return (0); |
2221 | | /* |
2222 | | * Set Y sampling factors; |
2223 | | * we assume jpeg_set_colorspace() set the rest to 1 |
2224 | | */ |
2225 | 0 | sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling; |
2226 | 0 | sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling; |
2227 | 0 | } |
2228 | 0 | else |
2229 | 0 | { |
2230 | 0 | if (!TIFFjpeg_set_colorspace(sp, sp->cinfo.c.in_color_space)) |
2231 | 0 | return (0); |
2232 | | /* jpeg_set_colorspace set all sampling factors to 1 */ |
2233 | 0 | } |
2234 | 0 | } |
2235 | 0 | else |
2236 | 0 | { |
2237 | 0 | if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN)) |
2238 | 0 | return (0); |
2239 | 0 | sp->cinfo.c.comp_info[0].component_id = s; |
2240 | | /* jpeg_set_colorspace() set sampling factors to 1 */ |
2241 | 0 | if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0) |
2242 | 0 | { |
2243 | 0 | sp->cinfo.c.comp_info[0].quant_tbl_no = 1; |
2244 | 0 | sp->cinfo.c.comp_info[0].dc_tbl_no = 1; |
2245 | 0 | sp->cinfo.c.comp_info[0].ac_tbl_no = 1; |
2246 | 0 | } |
2247 | 0 | } |
2248 | | /* ensure libjpeg won't write any extraneous markers */ |
2249 | 0 | sp->cinfo.c.write_JFIF_header = FALSE; |
2250 | 0 | sp->cinfo.c.write_Adobe_marker = FALSE; |
2251 | | /* set up table handling correctly */ |
2252 | | /* calling TIFFjpeg_set_quality() causes quantization tables to be flagged |
2253 | | */ |
2254 | | /* as being to be emitted, which we don't want in the JPEGTABLESMODE_QUANT |
2255 | | */ |
2256 | | /* mode, so we must manually suppress them. However TIFFjpeg_set_quality() |
2257 | | */ |
2258 | | /* should really be called when dealing with files with directories with */ |
2259 | | /* mixed qualities. see http://trac.osgeo.org/gdal/ticket/3539 */ |
2260 | 0 | if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE)) |
2261 | 0 | return (0); |
2262 | 0 | if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT) |
2263 | 0 | { |
2264 | 0 | suppress_quant_table(sp, 0); |
2265 | 0 | suppress_quant_table(sp, 1); |
2266 | 0 | } |
2267 | 0 | else |
2268 | 0 | { |
2269 | 0 | unsuppress_quant_table(sp, 0); |
2270 | 0 | unsuppress_quant_table(sp, 1); |
2271 | 0 | } |
2272 | 0 | if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) |
2273 | 0 | { |
2274 | | /* Explicit suppression is only needed if we did not go through the */ |
2275 | | /* prepare_JPEGTables() code path, which may be the case if updating */ |
2276 | | /* an existing file */ |
2277 | 0 | suppress_huff_table(sp, 0); |
2278 | 0 | suppress_huff_table(sp, 1); |
2279 | 0 | sp->cinfo.c.optimize_coding = FALSE; |
2280 | 0 | } |
2281 | 0 | else |
2282 | 0 | sp->cinfo.c.optimize_coding = TRUE; |
2283 | 0 | if (downsampled_input) |
2284 | 0 | { |
2285 | | /* Need to use raw-data interface to libjpeg */ |
2286 | 0 | sp->cinfo.c.raw_data_in = TRUE; |
2287 | 0 | tif->tif_encoderow = JPEGEncodeRaw; |
2288 | 0 | tif->tif_encodestrip = JPEGEncodeRaw; |
2289 | 0 | tif->tif_encodetile = JPEGEncodeRaw; |
2290 | 0 | } |
2291 | 0 | else |
2292 | 0 | { |
2293 | | /* Use normal interface to libjpeg */ |
2294 | 0 | sp->cinfo.c.raw_data_in = FALSE; |
2295 | 0 | tif->tif_encoderow = JPEGEncode; |
2296 | 0 | tif->tif_encodestrip = JPEGEncode; |
2297 | 0 | tif->tif_encodetile = JPEGEncode; |
2298 | 0 | } |
2299 | | /* Start JPEG compressor */ |
2300 | 0 | if (!TIFFjpeg_start_compress(sp, FALSE)) |
2301 | 0 | return (0); |
2302 | | /* Allocate downsampled-data buffers if needed */ |
2303 | 0 | if (downsampled_input) |
2304 | 0 | { |
2305 | 0 | if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info, |
2306 | 0 | sp->cinfo.c.num_components)) |
2307 | 0 | return (0); |
2308 | 0 | } |
2309 | 0 | sp->scancount = 0; |
2310 | 0 | sp->encode_raw_error = FALSE; |
2311 | |
|
2312 | 0 | return (1); |
2313 | 0 | } Unexecuted instantiation: tif_jpeg.c:JPEGPreEncode Unexecuted instantiation: tif_jpeg_12.c:JPEGPreEncode |
2314 | | |
2315 | | /* |
2316 | | * Encode a chunk of pixels. |
2317 | | * "Standard" case: incoming data is not downsampled. |
2318 | | */ |
2319 | | static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) |
2320 | 0 | { |
2321 | 0 | JPEGState *sp = JState(tif); |
2322 | 0 | tmsize_t nrows; |
2323 | 0 | TIFF_JSAMPROW bufptr[1]; |
2324 | 0 | short *line16 = NULL; |
2325 | 0 | int line16_count = 0; |
2326 | |
|
2327 | 0 | (void)s; |
2328 | 0 | assert(sp != NULL); |
2329 | | /* data is expected to be supplied in multiples of a scanline */ |
2330 | 0 | nrows = cc / sp->bytesperline; |
2331 | 0 | if (cc % sp->bytesperline) |
2332 | 0 | TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded"); |
2333 | | |
2334 | | /* The last strip will be limited to image size */ |
2335 | 0 | if (!isTiled(tif) && tif->tif_row + nrows > tif->tif_dir.td_imagelength) |
2336 | 0 | nrows = tif->tif_dir.td_imagelength - tif->tif_row; |
2337 | |
|
2338 | 0 | if (sp->cinfo.c.data_precision == 12) |
2339 | 0 | { |
2340 | 0 | line16_count = (int)((sp->bytesperline * 2) / 3); |
2341 | 0 | line16 = (short *)_TIFFmallocExt(tif, sizeof(short) * line16_count); |
2342 | 0 | if (!line16) |
2343 | 0 | { |
2344 | 0 | TIFFErrorExtR(tif, "JPEGEncode", "Failed to allocate memory"); |
2345 | |
|
2346 | 0 | return 0; |
2347 | 0 | } |
2348 | 0 | } |
2349 | | |
2350 | 0 | while (nrows-- > 0) |
2351 | 0 | { |
2352 | |
|
2353 | 0 | if (sp->cinfo.c.data_precision == 12) |
2354 | 0 | { |
2355 | |
|
2356 | 0 | int value_pairs = line16_count / 2; |
2357 | 0 | int iPair; |
2358 | |
|
2359 | 0 | bufptr[0] = (TIFF_JSAMPROW)line16; |
2360 | |
|
2361 | 0 | for (iPair = 0; iPair < value_pairs; iPair++) |
2362 | 0 | { |
2363 | 0 | unsigned char *in_ptr = ((unsigned char *)buf) + iPair * 3; |
2364 | 0 | TIFF_JSAMPLE *out_ptr = (TIFF_JSAMPLE *)(line16 + iPair * 2); |
2365 | |
|
2366 | 0 | out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4); |
2367 | 0 | out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2]; |
2368 | 0 | } |
2369 | 0 | } |
2370 | 0 | else |
2371 | 0 | { |
2372 | 0 | bufptr[0] = (TIFF_JSAMPROW)buf; |
2373 | 0 | } |
2374 | 0 | if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1) |
2375 | 0 | return (0); |
2376 | 0 | if (nrows > 0) |
2377 | 0 | tif->tif_row++; |
2378 | 0 | buf += sp->bytesperline; |
2379 | 0 | } |
2380 | | |
2381 | 0 | if (sp->cinfo.c.data_precision == 12) |
2382 | 0 | { |
2383 | 0 | _TIFFfreeExt(tif, line16); |
2384 | 0 | } |
2385 | |
|
2386 | 0 | return (1); |
2387 | 0 | } Unexecuted instantiation: tif_jpeg.c:JPEGEncode Unexecuted instantiation: tif_jpeg_12.c:JPEGEncode |
2388 | | |
2389 | | /* |
2390 | | * Encode a chunk of pixels. |
2391 | | * Incoming data is expected to be downsampled per sampling factors. |
2392 | | */ |
2393 | | static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) |
2394 | 0 | { |
2395 | 0 | JPEGState *sp = JState(tif); |
2396 | 0 | TIFF_JSAMPLE *inptr; |
2397 | 0 | TIFF_JSAMPLE *outptr; |
2398 | 0 | tmsize_t nrows; |
2399 | 0 | JDIMENSION clumps_per_line, nclump; |
2400 | 0 | int clumpoffset, ci, xpos, ypos; |
2401 | 0 | jpeg_component_info *compptr; |
2402 | 0 | int samples_per_clump = sp->samplesperclump; |
2403 | 0 | tmsize_t bytesperclumpline; |
2404 | |
|
2405 | 0 | (void)s; |
2406 | 0 | assert(sp != NULL); |
2407 | | |
2408 | 0 | if (sp->encode_raw_error) |
2409 | 0 | { |
2410 | 0 | TIFFErrorExtR(tif, tif->tif_name, "JPEGEncodeRaw() already failed"); |
2411 | 0 | return 0; |
2412 | 0 | } |
2413 | | |
2414 | | /* data is expected to be supplied in multiples of a clumpline */ |
2415 | | /* a clumpline is equivalent to v_sampling desubsampled scanlines */ |
2416 | | /* TODO: the following calculation of bytesperclumpline, should substitute |
2417 | | * calculation of sp->bytesperline, except that it is per v_sampling lines |
2418 | | */ |
2419 | 0 | bytesperclumpline = |
2420 | 0 | ((((tmsize_t)sp->cinfo.c.image_width + sp->h_sampling - 1) / |
2421 | 0 | sp->h_sampling) * |
2422 | 0 | ((tmsize_t)sp->h_sampling * sp->v_sampling + 2) * |
2423 | 0 | sp->cinfo.c.data_precision + |
2424 | 0 | 7) / |
2425 | 0 | 8; |
2426 | |
|
2427 | 0 | nrows = (cc / bytesperclumpline) * sp->v_sampling; |
2428 | 0 | if (cc % bytesperclumpline) |
2429 | 0 | TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded"); |
2430 | | |
2431 | | /* Cb,Cr both have sampling factors 1, so this is correct */ |
2432 | 0 | clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width; |
2433 | |
|
2434 | 0 | while (nrows > 0) |
2435 | 0 | { |
2436 | | /* |
2437 | | * Fastest way to separate the data is to make one pass |
2438 | | * over the scanline for each row of each component. |
2439 | | */ |
2440 | 0 | clumpoffset = 0; /* first sample in clump */ |
2441 | 0 | for (ci = 0, compptr = sp->cinfo.c.comp_info; |
2442 | 0 | ci < sp->cinfo.c.num_components; ci++, compptr++) |
2443 | 0 | { |
2444 | 0 | int hsamp = compptr->h_samp_factor; |
2445 | 0 | int vsamp = compptr->v_samp_factor; |
2446 | 0 | int padding = (int)(compptr->width_in_blocks * DCTSIZE - |
2447 | 0 | clumps_per_line * hsamp); |
2448 | 0 | for (ypos = 0; ypos < vsamp; ypos++) |
2449 | 0 | { |
2450 | 0 | inptr = ((TIFF_JSAMPLE *)buf) + clumpoffset; |
2451 | 0 | outptr = sp->ds_buffer[ci][sp->scancount * vsamp + ypos]; |
2452 | 0 | if (hsamp == 1) |
2453 | 0 | { |
2454 | | /* fast path for at least Cb and Cr */ |
2455 | 0 | for (nclump = clumps_per_line; nclump-- > 0;) |
2456 | 0 | { |
2457 | 0 | *outptr++ = inptr[0]; |
2458 | 0 | inptr += samples_per_clump; |
2459 | 0 | } |
2460 | 0 | } |
2461 | 0 | else |
2462 | 0 | { |
2463 | | /* general case */ |
2464 | 0 | for (nclump = clumps_per_line; nclump-- > 0;) |
2465 | 0 | { |
2466 | 0 | for (xpos = 0; xpos < hsamp; xpos++) |
2467 | 0 | *outptr++ = inptr[xpos]; |
2468 | 0 | inptr += samples_per_clump; |
2469 | 0 | } |
2470 | 0 | } |
2471 | | /* pad each scanline as needed */ |
2472 | 0 | for (xpos = 0; xpos < padding; xpos++) |
2473 | 0 | { |
2474 | 0 | *outptr = outptr[-1]; |
2475 | 0 | outptr++; |
2476 | 0 | } |
2477 | 0 | clumpoffset += hsamp; |
2478 | 0 | } |
2479 | 0 | } |
2480 | 0 | sp->scancount++; |
2481 | 0 | if (sp->scancount >= DCTSIZE) |
2482 | 0 | { |
2483 | 0 | int n = sp->cinfo.c.max_v_samp_factor * DCTSIZE; |
2484 | 0 | if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n) |
2485 | 0 | { |
2486 | 0 | sp->encode_raw_error = TRUE; |
2487 | 0 | return (0); |
2488 | 0 | } |
2489 | 0 | sp->scancount = 0; |
2490 | 0 | } |
2491 | 0 | tif->tif_row += sp->v_sampling; |
2492 | 0 | buf += bytesperclumpline; |
2493 | 0 | nrows -= sp->v_sampling; |
2494 | 0 | } |
2495 | 0 | return (1); |
2496 | 0 | } Unexecuted instantiation: tif_jpeg.c:JPEGEncodeRaw Unexecuted instantiation: tif_jpeg_12.c:JPEGEncodeRaw |
2497 | | |
2498 | | /* |
2499 | | * Finish up at the end of a strip or tile. |
2500 | | */ |
2501 | | static int JPEGPostEncode(TIFF *tif) |
2502 | 0 | { |
2503 | 0 | JPEGState *sp = JState(tif); |
2504 | |
|
2505 | 0 | if (sp->scancount > 0) |
2506 | 0 | { |
2507 | | /* |
2508 | | * Need to emit a partial bufferload of downsampled data. |
2509 | | * Pad the data vertically. |
2510 | | */ |
2511 | 0 | int ci, ypos, n; |
2512 | 0 | jpeg_component_info *compptr; |
2513 | |
|
2514 | 0 | for (ci = 0, compptr = sp->cinfo.c.comp_info; |
2515 | 0 | ci < sp->cinfo.c.num_components; ci++, compptr++) |
2516 | 0 | { |
2517 | 0 | int vsamp = compptr->v_samp_factor; |
2518 | 0 | tmsize_t row_width = |
2519 | 0 | compptr->width_in_blocks * DCTSIZE * sizeof(JSAMPLE); |
2520 | 0 | for (ypos = sp->scancount * vsamp; ypos < DCTSIZE * vsamp; ypos++) |
2521 | 0 | { |
2522 | 0 | _TIFFmemcpy((void *)sp->ds_buffer[ci][ypos], |
2523 | 0 | (void *)sp->ds_buffer[ci][ypos - 1], row_width); |
2524 | 0 | } |
2525 | 0 | } |
2526 | 0 | n = sp->cinfo.c.max_v_samp_factor * DCTSIZE; |
2527 | 0 | if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n) |
2528 | 0 | return (0); |
2529 | 0 | } |
2530 | | |
2531 | 0 | return (TIFFjpeg_finish_compress(JState(tif))); |
2532 | 0 | } Unexecuted instantiation: tif_jpeg.c:JPEGPostEncode Unexecuted instantiation: tif_jpeg_12.c:JPEGPostEncode |
2533 | | |
2534 | | static void JPEGCleanup(TIFF *tif) |
2535 | 3.62k | { |
2536 | 3.62k | JPEGState *sp = JState(tif); |
2537 | | |
2538 | 3.62k | assert(sp != 0); |
2539 | | |
2540 | 3.62k | tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent; |
2541 | 3.62k | tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent; |
2542 | 3.62k | tif->tif_tagmethods.printdir = sp->otherSettings.printdir; |
2543 | 3.62k | if (sp->cinfo_initialized) |
2544 | 2.37k | TIFFjpeg_destroy(sp); /* release libjpeg resources */ |
2545 | 3.62k | if (sp->otherSettings.jpegtables) /* tag value */ |
2546 | 8 | _TIFFfreeExt(tif, sp->otherSettings.jpegtables); |
2547 | 3.62k | _TIFFfreeExt(tif, tif->tif_data); /* release local state */ |
2548 | 3.62k | tif->tif_data = NULL; |
2549 | | |
2550 | 3.62k | _TIFFSetDefaultCompressionState(tif); |
2551 | 3.62k | } Line | Count | Source | 2535 | 3.26k | { | 2536 | 3.26k | JPEGState *sp = JState(tif); | 2537 | | | 2538 | 3.26k | assert(sp != 0); | 2539 | | | 2540 | 3.26k | tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent; | 2541 | 3.26k | tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent; | 2542 | 3.26k | tif->tif_tagmethods.printdir = sp->otherSettings.printdir; | 2543 | 3.26k | if (sp->cinfo_initialized) | 2544 | 2.01k | TIFFjpeg_destroy(sp); /* release libjpeg resources */ | 2545 | 3.26k | if (sp->otherSettings.jpegtables) /* tag value */ | 2546 | 8 | _TIFFfreeExt(tif, sp->otherSettings.jpegtables); | 2547 | 3.26k | _TIFFfreeExt(tif, tif->tif_data); /* release local state */ | 2548 | 3.26k | tif->tif_data = NULL; | 2549 | | | 2550 | 3.26k | _TIFFSetDefaultCompressionState(tif); | 2551 | 3.26k | } |
tif_jpeg_12.c:JPEGCleanup Line | Count | Source | 2535 | 361 | { | 2536 | 361 | JPEGState *sp = JState(tif); | 2537 | | | 2538 | 361 | assert(sp != 0); | 2539 | | | 2540 | 361 | tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent; | 2541 | 361 | tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent; | 2542 | 361 | tif->tif_tagmethods.printdir = sp->otherSettings.printdir; | 2543 | 361 | if (sp->cinfo_initialized) | 2544 | 361 | TIFFjpeg_destroy(sp); /* release libjpeg resources */ | 2545 | 361 | if (sp->otherSettings.jpegtables) /* tag value */ | 2546 | 0 | _TIFFfreeExt(tif, sp->otherSettings.jpegtables); | 2547 | 361 | _TIFFfreeExt(tif, tif->tif_data); /* release local state */ | 2548 | 361 | tif->tif_data = NULL; | 2549 | | | 2550 | 361 | _TIFFSetDefaultCompressionState(tif); | 2551 | 361 | } |
|
2552 | | |
2553 | | static void JPEGResetUpsampled(TIFF *tif) |
2554 | 1.92k | { |
2555 | 1.92k | JPEGState *sp = JState(tif); |
2556 | 1.92k | TIFFDirectory *td = &tif->tif_dir; |
2557 | | |
2558 | | /* |
2559 | | * Mark whether returned data is up-sampled or not so TIFFStripSize |
2560 | | * and TIFFTileSize return values that reflect the true amount of |
2561 | | * data. |
2562 | | */ |
2563 | 1.92k | tif->tif_flags &= ~TIFF_UPSAMPLED; |
2564 | 1.92k | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
2565 | 1.92k | { |
2566 | 1.92k | if (td->td_photometric == PHOTOMETRIC_YCBCR && |
2567 | 1.92k | sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB) |
2568 | 312 | { |
2569 | 312 | tif->tif_flags |= TIFF_UPSAMPLED; |
2570 | 312 | } |
2571 | 1.61k | else |
2572 | 1.61k | { |
2573 | | #ifdef notdef |
2574 | | if (td->td_ycbcrsubsampling[0] != 1 || |
2575 | | td->td_ycbcrsubsampling[1] != 1) |
2576 | | ; /* XXX what about up-sampling? */ |
2577 | | #endif |
2578 | 1.61k | } |
2579 | 1.92k | } |
2580 | | |
2581 | | /* |
2582 | | * Must recalculate cached tile size in case sampling state changed. |
2583 | | * Should we really be doing this now if image size isn't set? |
2584 | | */ |
2585 | 1.92k | if (tif->tif_tilesize > 0) |
2586 | 288 | tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1); |
2587 | 1.92k | if (tif->tif_scanlinesize > 0) |
2588 | 312 | tif->tif_scanlinesize = TIFFScanlineSize(tif); |
2589 | 1.92k | } tif_jpeg.c:JPEGResetUpsampled Line | Count | Source | 2554 | 1.92k | { | 2555 | 1.92k | JPEGState *sp = JState(tif); | 2556 | 1.92k | TIFFDirectory *td = &tif->tif_dir; | 2557 | | | 2558 | | /* | 2559 | | * Mark whether returned data is up-sampled or not so TIFFStripSize | 2560 | | * and TIFFTileSize return values that reflect the true amount of | 2561 | | * data. | 2562 | | */ | 2563 | 1.92k | tif->tif_flags &= ~TIFF_UPSAMPLED; | 2564 | 1.92k | if (td->td_planarconfig == PLANARCONFIG_CONTIG) | 2565 | 1.92k | { | 2566 | 1.92k | if (td->td_photometric == PHOTOMETRIC_YCBCR && | 2567 | 1.92k | sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB) | 2568 | 312 | { | 2569 | 312 | tif->tif_flags |= TIFF_UPSAMPLED; | 2570 | 312 | } | 2571 | 1.61k | else | 2572 | 1.61k | { | 2573 | | #ifdef notdef | 2574 | | if (td->td_ycbcrsubsampling[0] != 1 || | 2575 | | td->td_ycbcrsubsampling[1] != 1) | 2576 | | ; /* XXX what about up-sampling? */ | 2577 | | #endif | 2578 | 1.61k | } | 2579 | 1.92k | } | 2580 | | | 2581 | | /* | 2582 | | * Must recalculate cached tile size in case sampling state changed. | 2583 | | * Should we really be doing this now if image size isn't set? | 2584 | | */ | 2585 | 1.92k | if (tif->tif_tilesize > 0) | 2586 | 288 | tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1); | 2587 | 1.92k | if (tif->tif_scanlinesize > 0) | 2588 | 312 | tif->tif_scanlinesize = TIFFScanlineSize(tif); | 2589 | 1.92k | } |
Unexecuted instantiation: tif_jpeg_12.c:JPEGResetUpsampled |
2590 | | |
2591 | | static int JPEGVSetField(TIFF *tif, uint32_t tag, va_list ap) |
2592 | 16.1k | { |
2593 | 16.1k | JPEGState *sp = JState(tif); |
2594 | 16.1k | const TIFFField *fip; |
2595 | 16.1k | uint32_t v32; |
2596 | | |
2597 | 16.1k | assert(sp != NULL); |
2598 | | |
2599 | 16.1k | switch (tag) |
2600 | 16.1k | { |
2601 | 9 | case TIFFTAG_JPEGTABLES: |
2602 | 9 | v32 = (uint32_t)va_arg(ap, uint32_t); |
2603 | 9 | if (v32 == 0) |
2604 | 1 | { |
2605 | | /* XXX */ |
2606 | 1 | return (0); |
2607 | 1 | } |
2608 | 8 | _TIFFsetByteArrayExt(tif, &sp->otherSettings.jpegtables, |
2609 | 8 | va_arg(ap, void *), v32); |
2610 | 8 | sp->otherSettings.jpegtables_length = v32; |
2611 | 8 | TIFFSetFieldBit(tif, FIELD_JPEGTABLES); |
2612 | 8 | break; |
2613 | 0 | case TIFFTAG_JPEGQUALITY: |
2614 | 0 | sp->otherSettings.jpegquality = (int)va_arg(ap, int); |
2615 | 0 | return (1); /* pseudo tag */ |
2616 | 312 | case TIFFTAG_JPEGCOLORMODE: |
2617 | 312 | sp->otherSettings.jpegcolormode = (int)va_arg(ap, int); |
2618 | 312 | JPEGResetUpsampled(tif); |
2619 | 312 | return (1); /* pseudo tag */ |
2620 | 1.61k | case TIFFTAG_PHOTOMETRIC: |
2621 | 1.61k | { |
2622 | 1.61k | int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap); |
2623 | 1.61k | JPEGResetUpsampled(tif); |
2624 | 1.61k | return ret_value; |
2625 | 9 | } |
2626 | 0 | case TIFFTAG_JPEGTABLESMODE: |
2627 | 0 | sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int); |
2628 | 0 | return (1); /* pseudo tag */ |
2629 | 4 | case TIFFTAG_YCBCRSUBSAMPLING: |
2630 | | /* mark the fact that we have a real ycbcrsubsampling! */ |
2631 | 4 | sp->otherSettings.ycbcrsampling_fetched = 1; |
2632 | | /* should we be recomputing upsampling info here? */ |
2633 | 4 | return (*sp->otherSettings.vsetparent)(tif, tag, ap); |
2634 | 14.1k | default: |
2635 | 14.1k | return (*sp->otherSettings.vsetparent)(tif, tag, ap); |
2636 | 16.1k | } |
2637 | | |
2638 | 8 | if ((fip = TIFFFieldWithTag(tif, tag)) != NULL) |
2639 | 8 | { |
2640 | 8 | TIFFSetFieldBit(tif, fip->field_bit); |
2641 | 8 | } |
2642 | 0 | else |
2643 | 0 | { |
2644 | 0 | return (0); |
2645 | 0 | } |
2646 | | |
2647 | 8 | tif->tif_flags |= TIFF_DIRTYDIRECT; |
2648 | 8 | return (1); |
2649 | 8 | } Line | Count | Source | 2592 | 16.1k | { | 2593 | 16.1k | JPEGState *sp = JState(tif); | 2594 | 16.1k | const TIFFField *fip; | 2595 | 16.1k | uint32_t v32; | 2596 | | | 2597 | 16.1k | assert(sp != NULL); | 2598 | | | 2599 | 16.1k | switch (tag) | 2600 | 16.1k | { | 2601 | 9 | case TIFFTAG_JPEGTABLES: | 2602 | 9 | v32 = (uint32_t)va_arg(ap, uint32_t); | 2603 | 9 | if (v32 == 0) | 2604 | 1 | { | 2605 | | /* XXX */ | 2606 | 1 | return (0); | 2607 | 1 | } | 2608 | 8 | _TIFFsetByteArrayExt(tif, &sp->otherSettings.jpegtables, | 2609 | 8 | va_arg(ap, void *), v32); | 2610 | 8 | sp->otherSettings.jpegtables_length = v32; | 2611 | 8 | TIFFSetFieldBit(tif, FIELD_JPEGTABLES); | 2612 | 8 | break; | 2613 | 0 | case TIFFTAG_JPEGQUALITY: | 2614 | 0 | sp->otherSettings.jpegquality = (int)va_arg(ap, int); | 2615 | 0 | return (1); /* pseudo tag */ | 2616 | 312 | case TIFFTAG_JPEGCOLORMODE: | 2617 | 312 | sp->otherSettings.jpegcolormode = (int)va_arg(ap, int); | 2618 | 312 | JPEGResetUpsampled(tif); | 2619 | 312 | return (1); /* pseudo tag */ | 2620 | 1.61k | case TIFFTAG_PHOTOMETRIC: | 2621 | 1.61k | { | 2622 | 1.61k | int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap); | 2623 | 1.61k | JPEGResetUpsampled(tif); | 2624 | 1.61k | return ret_value; | 2625 | 9 | } | 2626 | 0 | case TIFFTAG_JPEGTABLESMODE: | 2627 | 0 | sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int); | 2628 | 0 | return (1); /* pseudo tag */ | 2629 | 4 | case TIFFTAG_YCBCRSUBSAMPLING: | 2630 | | /* mark the fact that we have a real ycbcrsubsampling! */ | 2631 | 4 | sp->otherSettings.ycbcrsampling_fetched = 1; | 2632 | | /* should we be recomputing upsampling info here? */ | 2633 | 4 | return (*sp->otherSettings.vsetparent)(tif, tag, ap); | 2634 | 14.1k | default: | 2635 | 14.1k | return (*sp->otherSettings.vsetparent)(tif, tag, ap); | 2636 | 16.1k | } | 2637 | | | 2638 | 8 | if ((fip = TIFFFieldWithTag(tif, tag)) != NULL) | 2639 | 8 | { | 2640 | 8 | TIFFSetFieldBit(tif, fip->field_bit); | 2641 | 8 | } | 2642 | 0 | else | 2643 | 0 | { | 2644 | 0 | return (0); | 2645 | 0 | } | 2646 | | | 2647 | 8 | tif->tif_flags |= TIFF_DIRTYDIRECT; | 2648 | 8 | return (1); | 2649 | 8 | } |
Unexecuted instantiation: tif_jpeg_12.c:JPEGVSetField |
2650 | | |
2651 | | static int JPEGVGetField(TIFF *tif, uint32_t tag, va_list ap) |
2652 | 26.5k | { |
2653 | 26.5k | JPEGState *sp = JState(tif); |
2654 | | |
2655 | 26.5k | assert(sp != NULL); |
2656 | | |
2657 | 26.5k | switch (tag) |
2658 | 26.5k | { |
2659 | 0 | case TIFFTAG_JPEGTABLES: |
2660 | 0 | *va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length; |
2661 | 0 | *va_arg(ap, const void **) = sp->otherSettings.jpegtables; |
2662 | 0 | break; |
2663 | 0 | case TIFFTAG_JPEGQUALITY: |
2664 | 0 | *va_arg(ap, int *) = sp->otherSettings.jpegquality; |
2665 | 0 | break; |
2666 | 0 | case TIFFTAG_JPEGCOLORMODE: |
2667 | 0 | *va_arg(ap, int *) = sp->otherSettings.jpegcolormode; |
2668 | 0 | break; |
2669 | 0 | case TIFFTAG_JPEGTABLESMODE: |
2670 | 0 | *va_arg(ap, int *) = sp->otherSettings.jpegtablesmode; |
2671 | 0 | break; |
2672 | 26.5k | default: |
2673 | 26.5k | return (*sp->otherSettings.vgetparent)(tif, tag, ap); |
2674 | 26.5k | } |
2675 | 0 | return (1); |
2676 | 26.5k | } Line | Count | Source | 2652 | 26.5k | { | 2653 | 26.5k | JPEGState *sp = JState(tif); | 2654 | | | 2655 | 26.5k | assert(sp != NULL); | 2656 | | | 2657 | 26.5k | switch (tag) | 2658 | 26.5k | { | 2659 | 0 | case TIFFTAG_JPEGTABLES: | 2660 | 0 | *va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length; | 2661 | 0 | *va_arg(ap, const void **) = sp->otherSettings.jpegtables; | 2662 | 0 | break; | 2663 | 0 | case TIFFTAG_JPEGQUALITY: | 2664 | 0 | *va_arg(ap, int *) = sp->otherSettings.jpegquality; | 2665 | 0 | break; | 2666 | 0 | case TIFFTAG_JPEGCOLORMODE: | 2667 | 0 | *va_arg(ap, int *) = sp->otherSettings.jpegcolormode; | 2668 | 0 | break; | 2669 | 0 | case TIFFTAG_JPEGTABLESMODE: | 2670 | 0 | *va_arg(ap, int *) = sp->otherSettings.jpegtablesmode; | 2671 | 0 | break; | 2672 | 26.5k | default: | 2673 | 26.5k | return (*sp->otherSettings.vgetparent)(tif, tag, ap); | 2674 | 26.5k | } | 2675 | 0 | return (1); | 2676 | 26.5k | } |
Unexecuted instantiation: tif_jpeg_12.c:JPEGVGetField |
2677 | | |
2678 | | static void JPEGPrintDir(TIFF *tif, FILE *fd, long flags) |
2679 | 0 | { |
2680 | 0 | JPEGState *sp = JState(tif); |
2681 | |
|
2682 | 0 | assert(sp != NULL); |
2683 | 0 | (void)flags; |
2684 | |
|
2685 | 0 | if (sp != NULL) |
2686 | 0 | { |
2687 | 0 | if (TIFFFieldSet(tif, FIELD_JPEGTABLES)) |
2688 | 0 | fprintf(fd, " JPEG Tables: (%" PRIu32 " bytes)\n", |
2689 | 0 | sp->otherSettings.jpegtables_length); |
2690 | 0 | if (sp->otherSettings.printdir) |
2691 | 0 | (*sp->otherSettings.printdir)(tif, fd, flags); |
2692 | 0 | } |
2693 | 0 | } Unexecuted instantiation: tif_jpeg.c:JPEGPrintDir Unexecuted instantiation: tif_jpeg_12.c:JPEGPrintDir |
2694 | | |
2695 | | static uint32_t JPEGDefaultStripSize(TIFF *tif, uint32_t s) |
2696 | 0 | { |
2697 | 0 | JPEGState *sp = JState(tif); |
2698 | 0 | TIFFDirectory *td = &tif->tif_dir; |
2699 | |
|
2700 | 0 | s = (*sp->otherSettings.defsparent)(tif, s); |
2701 | 0 | if (s < td->td_imagelength) |
2702 | 0 | s = TIFFroundup_32(s, td->td_ycbcrsubsampling[1] * DCTSIZE); |
2703 | 0 | return (s); |
2704 | 0 | } Unexecuted instantiation: tif_jpeg.c:JPEGDefaultStripSize Unexecuted instantiation: tif_jpeg_12.c:JPEGDefaultStripSize |
2705 | | |
2706 | | static void JPEGDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th) |
2707 | 0 | { |
2708 | 0 | JPEGState *sp = JState(tif); |
2709 | 0 | TIFFDirectory *td = &tif->tif_dir; |
2710 | |
|
2711 | 0 | (*sp->otherSettings.deftparent)(tif, tw, th); |
2712 | 0 | *tw = TIFFroundup_32(*tw, td->td_ycbcrsubsampling[0] * DCTSIZE); |
2713 | 0 | *th = TIFFroundup_32(*th, td->td_ycbcrsubsampling[1] * DCTSIZE); |
2714 | 0 | } Unexecuted instantiation: tif_jpeg.c:JPEGDefaultTileSize Unexecuted instantiation: tif_jpeg_12.c:JPEGDefaultTileSize |
2715 | | |
2716 | | /* |
2717 | | * The JPEG library initialized used to be done in TIFFInitJPEG(), but |
2718 | | * now that we allow a TIFF file to be opened in update mode it is necessary |
2719 | | * to have some way of deciding whether compression or decompression is |
2720 | | * desired other than looking at tif->tif_mode. We accomplish this by |
2721 | | * examining {TILE/STRIP}BYTECOUNTS to see if there is a non-zero entry. |
2722 | | * If so, we assume decompression is desired. |
2723 | | * |
2724 | | * This is tricky, because TIFFInitJPEG() is called while the directory is |
2725 | | * being read, and generally speaking the BYTECOUNTS tag won't have been read |
2726 | | * at that point. So we try to defer jpeg library initialization till we |
2727 | | * do have that tag ... basically any access that might require the compressor |
2728 | | * or decompressor that occurs after the reading of the directory. |
2729 | | * |
2730 | | * In an ideal world compressors or decompressors would be setup |
2731 | | * at the point where a single tile or strip was accessed (for read or write) |
2732 | | * so that stuff like update of missing tiles, or replacement of tiles could |
2733 | | * be done. However, we aren't trying to crack that nut just yet ... |
2734 | | * |
2735 | | * NFW, Feb 3rd, 2003. |
2736 | | */ |
2737 | | |
2738 | | static int JPEGInitializeLibJPEG(TIFF *tif, int decompress) |
2739 | 2.37k | { |
2740 | 2.37k | JPEGState *sp = JState(tif); |
2741 | | |
2742 | 2.37k | if (sp->cinfo_initialized) |
2743 | 2 | { |
2744 | 2 | if (!decompress && sp->cinfo.comm.is_decompressor) |
2745 | 0 | TIFFjpeg_destroy(sp); |
2746 | 2 | else if (decompress && !sp->cinfo.comm.is_decompressor) |
2747 | 0 | TIFFjpeg_destroy(sp); |
2748 | 2 | else |
2749 | 2 | return 1; |
2750 | | |
2751 | 0 | sp->cinfo_initialized = 0; |
2752 | 0 | } |
2753 | | |
2754 | | /* |
2755 | | * Initialize libjpeg. |
2756 | | */ |
2757 | 2.37k | if (decompress) |
2758 | 2.37k | { |
2759 | 2.37k | if (!TIFFjpeg_create_decompress(sp)) |
2760 | 0 | return (0); |
2761 | 2.37k | } |
2762 | 0 | else |
2763 | 0 | { |
2764 | 0 | if (!TIFFjpeg_create_compress(sp)) |
2765 | 0 | return (0); |
2766 | 0 | #ifndef TIFF_JPEG_MAX_MEMORY_TO_USE |
2767 | 0 | #define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024) |
2768 | 0 | #endif |
2769 | | /* libjpeg turbo 1.5.2 honours max_memory_to_use, but has no backing */ |
2770 | | /* store implementation, so better not set max_memory_to_use ourselves. |
2771 | | */ |
2772 | | /* See https://github.com/libjpeg-turbo/libjpeg-turbo/issues/162 */ |
2773 | 0 | if (sp->cinfo.c.mem->max_memory_to_use > 0) |
2774 | 0 | { |
2775 | | /* This is to address bug related in ticket GDAL #1795. */ |
2776 | 0 | if (getenv("JPEGMEM") == NULL) |
2777 | 0 | { |
2778 | | /* Increase the max memory usable. This helps when creating |
2779 | | * files */ |
2780 | | /* with "big" tile, without using libjpeg temporary files. */ |
2781 | | /* For example a 512x512 tile with 3 bands */ |
2782 | | /* requires 1.5 MB which is above libjpeg 1MB default */ |
2783 | 0 | if (sp->cinfo.c.mem->max_memory_to_use < |
2784 | 0 | TIFF_JPEG_MAX_MEMORY_TO_USE) |
2785 | 0 | sp->cinfo.c.mem->max_memory_to_use = |
2786 | 0 | TIFF_JPEG_MAX_MEMORY_TO_USE; |
2787 | 0 | } |
2788 | 0 | } |
2789 | 0 | } |
2790 | | |
2791 | 2.37k | sp->cinfo_initialized = TRUE; |
2792 | | |
2793 | 2.37k | return 1; |
2794 | 2.37k | } tif_jpeg.c:JPEGInitializeLibJPEG Line | Count | Source | 2739 | 2.01k | { | 2740 | 2.01k | JPEGState *sp = JState(tif); | 2741 | | | 2742 | 2.01k | if (sp->cinfo_initialized) | 2743 | 2 | { | 2744 | 2 | if (!decompress && sp->cinfo.comm.is_decompressor) | 2745 | 0 | TIFFjpeg_destroy(sp); | 2746 | 2 | else if (decompress && !sp->cinfo.comm.is_decompressor) | 2747 | 0 | TIFFjpeg_destroy(sp); | 2748 | 2 | else | 2749 | 2 | return 1; | 2750 | | | 2751 | 0 | sp->cinfo_initialized = 0; | 2752 | 0 | } | 2753 | | | 2754 | | /* | 2755 | | * Initialize libjpeg. | 2756 | | */ | 2757 | 2.01k | if (decompress) | 2758 | 2.01k | { | 2759 | 2.01k | if (!TIFFjpeg_create_decompress(sp)) | 2760 | 0 | return (0); | 2761 | 2.01k | } | 2762 | 0 | else | 2763 | 0 | { | 2764 | 0 | if (!TIFFjpeg_create_compress(sp)) | 2765 | 0 | return (0); | 2766 | 0 | #ifndef TIFF_JPEG_MAX_MEMORY_TO_USE | 2767 | 0 | #define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024) | 2768 | 0 | #endif | 2769 | | /* libjpeg turbo 1.5.2 honours max_memory_to_use, but has no backing */ | 2770 | | /* store implementation, so better not set max_memory_to_use ourselves. | 2771 | | */ | 2772 | | /* See https://github.com/libjpeg-turbo/libjpeg-turbo/issues/162 */ | 2773 | 0 | if (sp->cinfo.c.mem->max_memory_to_use > 0) | 2774 | 0 | { | 2775 | | /* This is to address bug related in ticket GDAL #1795. */ | 2776 | 0 | if (getenv("JPEGMEM") == NULL) | 2777 | 0 | { | 2778 | | /* Increase the max memory usable. This helps when creating | 2779 | | * files */ | 2780 | | /* with "big" tile, without using libjpeg temporary files. */ | 2781 | | /* For example a 512x512 tile with 3 bands */ | 2782 | | /* requires 1.5 MB which is above libjpeg 1MB default */ | 2783 | 0 | if (sp->cinfo.c.mem->max_memory_to_use < | 2784 | 0 | TIFF_JPEG_MAX_MEMORY_TO_USE) | 2785 | 0 | sp->cinfo.c.mem->max_memory_to_use = | 2786 | 0 | TIFF_JPEG_MAX_MEMORY_TO_USE; | 2787 | 0 | } | 2788 | 0 | } | 2789 | 0 | } | 2790 | | | 2791 | 2.01k | sp->cinfo_initialized = TRUE; | 2792 | | | 2793 | 2.01k | return 1; | 2794 | 2.01k | } |
tif_jpeg_12.c:JPEGInitializeLibJPEG Line | Count | Source | 2739 | 361 | { | 2740 | 361 | JPEGState *sp = JState(tif); | 2741 | | | 2742 | 361 | if (sp->cinfo_initialized) | 2743 | 0 | { | 2744 | 0 | if (!decompress && sp->cinfo.comm.is_decompressor) | 2745 | 0 | TIFFjpeg_destroy(sp); | 2746 | 0 | else if (decompress && !sp->cinfo.comm.is_decompressor) | 2747 | 0 | TIFFjpeg_destroy(sp); | 2748 | 0 | else | 2749 | 0 | return 1; | 2750 | | | 2751 | 0 | sp->cinfo_initialized = 0; | 2752 | 0 | } | 2753 | | | 2754 | | /* | 2755 | | * Initialize libjpeg. | 2756 | | */ | 2757 | 361 | if (decompress) | 2758 | 361 | { | 2759 | 361 | if (!TIFFjpeg_create_decompress(sp)) | 2760 | 0 | return (0); | 2761 | 361 | } | 2762 | 0 | else | 2763 | 0 | { | 2764 | 0 | if (!TIFFjpeg_create_compress(sp)) | 2765 | 0 | return (0); | 2766 | 0 | #ifndef TIFF_JPEG_MAX_MEMORY_TO_USE | 2767 | 0 | #define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024) | 2768 | 0 | #endif | 2769 | | /* libjpeg turbo 1.5.2 honours max_memory_to_use, but has no backing */ | 2770 | | /* store implementation, so better not set max_memory_to_use ourselves. | 2771 | | */ | 2772 | | /* See https://github.com/libjpeg-turbo/libjpeg-turbo/issues/162 */ | 2773 | 0 | if (sp->cinfo.c.mem->max_memory_to_use > 0) | 2774 | 0 | { | 2775 | | /* This is to address bug related in ticket GDAL #1795. */ | 2776 | 0 | if (getenv("JPEGMEM") == NULL) | 2777 | 0 | { | 2778 | | /* Increase the max memory usable. This helps when creating | 2779 | | * files */ | 2780 | | /* with "big" tile, without using libjpeg temporary files. */ | 2781 | | /* For example a 512x512 tile with 3 bands */ | 2782 | | /* requires 1.5 MB which is above libjpeg 1MB default */ | 2783 | 0 | if (sp->cinfo.c.mem->max_memory_to_use < | 2784 | 0 | TIFF_JPEG_MAX_MEMORY_TO_USE) | 2785 | 0 | sp->cinfo.c.mem->max_memory_to_use = | 2786 | 0 | TIFF_JPEG_MAX_MEMORY_TO_USE; | 2787 | 0 | } | 2788 | 0 | } | 2789 | 0 | } | 2790 | | | 2791 | 361 | sp->cinfo_initialized = TRUE; | 2792 | | | 2793 | 361 | return 1; | 2794 | 361 | } |
|
2795 | | |
2796 | | /* Common to tif_jpeg.c and tif_jpeg_12.c */ |
2797 | | static void TIFFInitJPEGCommon(TIFF *tif) |
2798 | 3.98k | { |
2799 | 3.98k | JPEGState *sp; |
2800 | | |
2801 | 3.98k | sp = JState(tif); |
2802 | 3.98k | sp->tif = tif; /* back link */ |
2803 | | |
2804 | | /* Default values for codec-specific fields */ |
2805 | 3.98k | sp->otherSettings.jpegtables = NULL; |
2806 | 3.98k | sp->otherSettings.jpegtables_length = 0; |
2807 | 3.98k | sp->otherSettings.jpegquality = 75; /* Default IJG quality */ |
2808 | 3.98k | sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW; |
2809 | 3.98k | sp->otherSettings.jpegtablesmode = |
2810 | 3.98k | JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF; |
2811 | 3.98k | sp->otherSettings.ycbcrsampling_fetched = 0; |
2812 | | |
2813 | 3.98k | tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */ |
2814 | 3.98k | tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */ |
2815 | 3.98k | tif->tif_tagmethods.printdir = JPEGPrintDir; /* hook for codec tags */ |
2816 | | |
2817 | | /* |
2818 | | * Install codec methods. |
2819 | | */ |
2820 | 3.98k | tif->tif_fixuptags = JPEGFixupTags; |
2821 | 3.98k | tif->tif_setupdecode = JPEGSetupDecode; |
2822 | 3.98k | tif->tif_predecode = JPEGPreDecode; |
2823 | 3.98k | tif->tif_decoderow = JPEGDecode; |
2824 | 3.98k | tif->tif_decodestrip = JPEGDecode; |
2825 | 3.98k | tif->tif_decodetile = JPEGDecode; |
2826 | 3.98k | tif->tif_setupencode = JPEGSetupEncode; |
2827 | 3.98k | tif->tif_preencode = JPEGPreEncode; |
2828 | 3.98k | tif->tif_postencode = JPEGPostEncode; |
2829 | 3.98k | tif->tif_encoderow = JPEGEncode; |
2830 | 3.98k | tif->tif_encodestrip = JPEGEncode; |
2831 | 3.98k | tif->tif_encodetile = JPEGEncode; |
2832 | 3.98k | tif->tif_cleanup = JPEGCleanup; |
2833 | | |
2834 | 3.98k | tif->tif_defstripsize = JPEGDefaultStripSize; |
2835 | 3.98k | tif->tif_deftilesize = JPEGDefaultTileSize; |
2836 | 3.98k | tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */ |
2837 | 3.98k | sp->cinfo_initialized = FALSE; |
2838 | 3.98k | } tif_jpeg.c:TIFFInitJPEGCommon Line | Count | Source | 2798 | 3.62k | { | 2799 | 3.62k | JPEGState *sp; | 2800 | | | 2801 | 3.62k | sp = JState(tif); | 2802 | 3.62k | sp->tif = tif; /* back link */ | 2803 | | | 2804 | | /* Default values for codec-specific fields */ | 2805 | 3.62k | sp->otherSettings.jpegtables = NULL; | 2806 | 3.62k | sp->otherSettings.jpegtables_length = 0; | 2807 | 3.62k | sp->otherSettings.jpegquality = 75; /* Default IJG quality */ | 2808 | 3.62k | sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW; | 2809 | 3.62k | sp->otherSettings.jpegtablesmode = | 2810 | 3.62k | JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF; | 2811 | 3.62k | sp->otherSettings.ycbcrsampling_fetched = 0; | 2812 | | | 2813 | 3.62k | tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */ | 2814 | 3.62k | tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */ | 2815 | 3.62k | tif->tif_tagmethods.printdir = JPEGPrintDir; /* hook for codec tags */ | 2816 | | | 2817 | | /* | 2818 | | * Install codec methods. | 2819 | | */ | 2820 | 3.62k | tif->tif_fixuptags = JPEGFixupTags; | 2821 | 3.62k | tif->tif_setupdecode = JPEGSetupDecode; | 2822 | 3.62k | tif->tif_predecode = JPEGPreDecode; | 2823 | 3.62k | tif->tif_decoderow = JPEGDecode; | 2824 | 3.62k | tif->tif_decodestrip = JPEGDecode; | 2825 | 3.62k | tif->tif_decodetile = JPEGDecode; | 2826 | 3.62k | tif->tif_setupencode = JPEGSetupEncode; | 2827 | 3.62k | tif->tif_preencode = JPEGPreEncode; | 2828 | 3.62k | tif->tif_postencode = JPEGPostEncode; | 2829 | 3.62k | tif->tif_encoderow = JPEGEncode; | 2830 | 3.62k | tif->tif_encodestrip = JPEGEncode; | 2831 | 3.62k | tif->tif_encodetile = JPEGEncode; | 2832 | 3.62k | tif->tif_cleanup = JPEGCleanup; | 2833 | | | 2834 | 3.62k | tif->tif_defstripsize = JPEGDefaultStripSize; | 2835 | 3.62k | tif->tif_deftilesize = JPEGDefaultTileSize; | 2836 | 3.62k | tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */ | 2837 | 3.62k | sp->cinfo_initialized = FALSE; | 2838 | 3.62k | } |
tif_jpeg_12.c:TIFFInitJPEGCommon Line | Count | Source | 2798 | 361 | { | 2799 | 361 | JPEGState *sp; | 2800 | | | 2801 | 361 | sp = JState(tif); | 2802 | 361 | sp->tif = tif; /* back link */ | 2803 | | | 2804 | | /* Default values for codec-specific fields */ | 2805 | 361 | sp->otherSettings.jpegtables = NULL; | 2806 | 361 | sp->otherSettings.jpegtables_length = 0; | 2807 | 361 | sp->otherSettings.jpegquality = 75; /* Default IJG quality */ | 2808 | 361 | sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW; | 2809 | 361 | sp->otherSettings.jpegtablesmode = | 2810 | 361 | JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF; | 2811 | 361 | sp->otherSettings.ycbcrsampling_fetched = 0; | 2812 | | | 2813 | 361 | tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */ | 2814 | 361 | tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */ | 2815 | 361 | tif->tif_tagmethods.printdir = JPEGPrintDir; /* hook for codec tags */ | 2816 | | | 2817 | | /* | 2818 | | * Install codec methods. | 2819 | | */ | 2820 | 361 | tif->tif_fixuptags = JPEGFixupTags; | 2821 | 361 | tif->tif_setupdecode = JPEGSetupDecode; | 2822 | 361 | tif->tif_predecode = JPEGPreDecode; | 2823 | 361 | tif->tif_decoderow = JPEGDecode; | 2824 | 361 | tif->tif_decodestrip = JPEGDecode; | 2825 | 361 | tif->tif_decodetile = JPEGDecode; | 2826 | 361 | tif->tif_setupencode = JPEGSetupEncode; | 2827 | 361 | tif->tif_preencode = JPEGPreEncode; | 2828 | 361 | tif->tif_postencode = JPEGPostEncode; | 2829 | 361 | tif->tif_encoderow = JPEGEncode; | 2830 | 361 | tif->tif_encodestrip = JPEGEncode; | 2831 | 361 | tif->tif_encodetile = JPEGEncode; | 2832 | 361 | tif->tif_cleanup = JPEGCleanup; | 2833 | | | 2834 | 361 | tif->tif_defstripsize = JPEGDefaultStripSize; | 2835 | 361 | tif->tif_deftilesize = JPEGDefaultTileSize; | 2836 | 361 | tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */ | 2837 | 361 | sp->cinfo_initialized = FALSE; | 2838 | 361 | } |
|
2839 | | |
2840 | | int TIFFInitJPEG(TIFF *tif, int scheme) |
2841 | 3.62k | { |
2842 | 3.62k | JPEGState *sp; |
2843 | | |
2844 | 3.62k | (void)scheme; |
2845 | 3.62k | assert(scheme == COMPRESSION_JPEG); |
2846 | | |
2847 | | /* |
2848 | | * Merge codec-specific tag information. |
2849 | | */ |
2850 | 3.62k | if (!_TIFFMergeFields(tif, jpegFields, TIFFArrayCount(jpegFields))) |
2851 | 0 | { |
2852 | 0 | TIFFErrorExtR(tif, "TIFFInitJPEG", |
2853 | 0 | "Merging JPEG codec-specific tags failed"); |
2854 | 0 | return 0; |
2855 | 0 | } |
2856 | | |
2857 | | /* |
2858 | | * Allocate state block so tag methods have storage to record values. |
2859 | | */ |
2860 | 3.62k | tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(JPEGState)); |
2861 | | |
2862 | 3.62k | if (tif->tif_data == NULL) |
2863 | 0 | { |
2864 | 0 | TIFFErrorExtR(tif, "TIFFInitJPEG", "No space for JPEG state block"); |
2865 | 0 | return 0; |
2866 | 0 | } |
2867 | 3.62k | _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState)); |
2868 | | |
2869 | 3.62k | sp = JState(tif); |
2870 | | /* |
2871 | | * Override parent get/set field methods. |
2872 | | */ |
2873 | 3.62k | sp->otherSettings.vgetparent = tif->tif_tagmethods.vgetfield; |
2874 | 3.62k | sp->otherSettings.vsetparent = tif->tif_tagmethods.vsetfield; |
2875 | 3.62k | sp->otherSettings.printdir = tif->tif_tagmethods.printdir; |
2876 | | |
2877 | 3.62k | sp->otherSettings.defsparent = tif->tif_defstripsize; |
2878 | 3.62k | sp->otherSettings.deftparent = tif->tif_deftilesize; |
2879 | | |
2880 | 3.62k | TIFFInitJPEGCommon(tif); |
2881 | | |
2882 | | /* |
2883 | | ** Create a JPEGTables field if no directory has yet been created. |
2884 | | ** We do this just to ensure that sufficient space is reserved for |
2885 | | ** the JPEGTables field. It will be properly created the right |
2886 | | ** size later. |
2887 | | */ |
2888 | 3.62k | if (tif->tif_diroff == 0) |
2889 | 0 | { |
2890 | 0 | #define SIZE_OF_JPEGTABLES 2000 |
2891 | | /* |
2892 | | The following line assumes incorrectly that all JPEG-in-TIFF files will |
2893 | | have a JPEGTABLES tag generated and causes null-filled JPEGTABLES tags |
2894 | | to be written when the JPEG data is placed with TIFFWriteRawStrip. The |
2895 | | field bit should be set, anyway, later when actual JPEGTABLES header is |
2896 | | generated, so removing it here hopefully is harmless. |
2897 | | TIFFSetFieldBit(tif, FIELD_JPEGTABLES); |
2898 | | */ |
2899 | 0 | sp->otherSettings.jpegtables_length = SIZE_OF_JPEGTABLES; |
2900 | 0 | sp->otherSettings.jpegtables = |
2901 | 0 | (void *)_TIFFmallocExt(tif, sp->otherSettings.jpegtables_length); |
2902 | 0 | if (sp->otherSettings.jpegtables) |
2903 | 0 | { |
2904 | 0 | _TIFFmemset(sp->otherSettings.jpegtables, 0, SIZE_OF_JPEGTABLES); |
2905 | 0 | } |
2906 | 0 | else |
2907 | 0 | { |
2908 | 0 | TIFFErrorExtR(tif, "TIFFInitJPEG", |
2909 | 0 | "Failed to allocate memory for JPEG tables"); |
2910 | 0 | return 0; |
2911 | 0 | } |
2912 | 0 | #undef SIZE_OF_JPEGTABLES |
2913 | 0 | } |
2914 | 3.62k | return 1; |
2915 | 3.62k | } Line | Count | Source | 2841 | 3.62k | { | 2842 | 3.62k | JPEGState *sp; | 2843 | | | 2844 | 3.62k | (void)scheme; | 2845 | 3.62k | assert(scheme == COMPRESSION_JPEG); | 2846 | | | 2847 | | /* | 2848 | | * Merge codec-specific tag information. | 2849 | | */ | 2850 | 3.62k | if (!_TIFFMergeFields(tif, jpegFields, TIFFArrayCount(jpegFields))) | 2851 | 0 | { | 2852 | 0 | TIFFErrorExtR(tif, "TIFFInitJPEG", | 2853 | 0 | "Merging JPEG codec-specific tags failed"); | 2854 | 0 | return 0; | 2855 | 0 | } | 2856 | | | 2857 | | /* | 2858 | | * Allocate state block so tag methods have storage to record values. | 2859 | | */ | 2860 | 3.62k | tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(JPEGState)); | 2861 | | | 2862 | 3.62k | if (tif->tif_data == NULL) | 2863 | 0 | { | 2864 | 0 | TIFFErrorExtR(tif, "TIFFInitJPEG", "No space for JPEG state block"); | 2865 | 0 | return 0; | 2866 | 0 | } | 2867 | 3.62k | _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState)); | 2868 | | | 2869 | 3.62k | sp = JState(tif); | 2870 | | /* | 2871 | | * Override parent get/set field methods. | 2872 | | */ | 2873 | 3.62k | sp->otherSettings.vgetparent = tif->tif_tagmethods.vgetfield; | 2874 | 3.62k | sp->otherSettings.vsetparent = tif->tif_tagmethods.vsetfield; | 2875 | 3.62k | sp->otherSettings.printdir = tif->tif_tagmethods.printdir; | 2876 | | | 2877 | 3.62k | sp->otherSettings.defsparent = tif->tif_defstripsize; | 2878 | 3.62k | sp->otherSettings.deftparent = tif->tif_deftilesize; | 2879 | | | 2880 | 3.62k | TIFFInitJPEGCommon(tif); | 2881 | | | 2882 | | /* | 2883 | | ** Create a JPEGTables field if no directory has yet been created. | 2884 | | ** We do this just to ensure that sufficient space is reserved for | 2885 | | ** the JPEGTables field. It will be properly created the right | 2886 | | ** size later. | 2887 | | */ | 2888 | 3.62k | if (tif->tif_diroff == 0) | 2889 | 0 | { | 2890 | 0 | #define SIZE_OF_JPEGTABLES 2000 | 2891 | | /* | 2892 | | The following line assumes incorrectly that all JPEG-in-TIFF files will | 2893 | | have a JPEGTABLES tag generated and causes null-filled JPEGTABLES tags | 2894 | | to be written when the JPEG data is placed with TIFFWriteRawStrip. The | 2895 | | field bit should be set, anyway, later when actual JPEGTABLES header is | 2896 | | generated, so removing it here hopefully is harmless. | 2897 | | TIFFSetFieldBit(tif, FIELD_JPEGTABLES); | 2898 | | */ | 2899 | 0 | sp->otherSettings.jpegtables_length = SIZE_OF_JPEGTABLES; | 2900 | 0 | sp->otherSettings.jpegtables = | 2901 | 0 | (void *)_TIFFmallocExt(tif, sp->otherSettings.jpegtables_length); | 2902 | 0 | if (sp->otherSettings.jpegtables) | 2903 | 0 | { | 2904 | 0 | _TIFFmemset(sp->otherSettings.jpegtables, 0, SIZE_OF_JPEGTABLES); | 2905 | 0 | } | 2906 | 0 | else | 2907 | 0 | { | 2908 | 0 | TIFFErrorExtR(tif, "TIFFInitJPEG", | 2909 | 0 | "Failed to allocate memory for JPEG tables"); | 2910 | 0 | return 0; | 2911 | 0 | } | 2912 | 0 | #undef SIZE_OF_JPEGTABLES | 2913 | 0 | } | 2914 | 3.62k | return 1; | 2915 | 3.62k | } |
Unexecuted instantiation: TIFFInitJPEG_12 |
2916 | | #endif /* JPEG_SUPPORT */ |