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