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