LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
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
30#include "tiffiop.h"
31#include <stdlib.h>
32
33#ifdef JPEG_SUPPORT
34
35/*
36 * TIFF Library
37 *
38 * JPEG Compression support per TIFF Technical Note #2
39 * (*not* per the original TIFF 6.0 spec).
40 *
41 * This file is simply an interface to the libjpeg library written by
42 * the Independent JPEG Group. You need release 5 or later of the IJG
43 * code, which you can find on the Internet at ftp.uu.net:/graphics/jpeg/.
44 *
45 * Contributed by Tom Lane <tgl@sss.pgh.pa.us>.
46 */
47#include <setjmp.h>
48
49/* Settings that are independent of libjpeg ABI. Used when reinitializing the */
50/* JPEGState from libjpegs 8 bit to libjpeg 12 bits, which have potentially */
51/* different ABI */
100
101int TIFFFillStrip(TIFF *tif, uint32_t strip);
102int TIFFFillTile(TIFF *tif, uint32_t tile);
124int TIFFReInitJPEG_12(TIFF *tif, const JPEGOtherSettings *otherSettings,
125 int scheme, int is_encode);
140
141#include "jerror.h"
142#include "jpeglib.h"
143
144/* Do optional compile-time version check */
145#if defined(EXPECTED_JPEG_LIB_VERSION) && !defined(LIBJPEG_12_PATH)
146#if EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION
147#error EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION
148#endif
149#endif
150
151/*
152 * Do we want to do special processing suitable for when JSAMPLE is a
153 * 16bit value?
154 */
155
156/* HAVE_JPEGTURBO_DUAL_MODE_8_12 is defined for libjpeg-turbo >= 3.0 which
157 * adds a dual-mode 8/12 bit API in the same library.
158 * (note: libjpeg-turbo 2.2 was actually released as 3.0)
159 */
160
161#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12)
171#define JPEG_DUAL_MODE_8_12
172/* Start by undefining BITS_IN_JSAMPLE which is always set to 8 in libjpeg-turbo
173 * >= 3.0 Cf
174 * https://github.com/libjpeg-turbo/libjpeg-turbo/commit/8b9bc4b9635a2a047fb23ebe70c9acd728d3f99b
175 */
176#undef BITS_IN_JSAMPLE
177/* libjpeg-turbo >= 3.0 adds J12xxxx datatypes for the 12-bit mode. */
178#if defined(FROM_TIF_JPEG_12)
187#define BITS_IN_JSAMPLE 12
195#define TIFF_JSAMPLE J12SAMPLE
203#define TIFF_JSAMPARRAY J12SAMPARRAY
211#define TIFF_JSAMPIMAGE J12SAMPIMAGE
219#define TIFF_JSAMPROW J12SAMPROW
220#else
228#define BITS_IN_JSAMPLE 8
236#define TIFF_JSAMPLE JSAMPLE
244#define TIFF_JSAMPARRAY JSAMPARRAY
252#define TIFF_JSAMPIMAGE JSAMPIMAGE
260#define TIFF_JSAMPROW JSAMPROW
261#endif
262#else
270#define TIFF_JSAMPLE JSAMPLE
278#define TIFF_JSAMPARRAY JSAMPARRAY
286#define TIFF_JSAMPIMAGE JSAMPIMAGE
294#define TIFF_JSAMPROW JSAMPROW
295#endif
296
297#if defined(JPEG_LIB_MK1)
307#define JPEG_LIB_MK1_OR_12BIT 1
308#elif BITS_IN_JSAMPLE == 12
316#define JPEG_LIB_MK1_OR_12BIT 1
317#endif
318
319/*
320 * We are using width_in_blocks which is supposed to be private to
321 * libjpeg. Unfortunately, the libjpeg delivered with Cygwin has
322 * renamed this member to width_in_data_units. Since the header has
323 * also renamed a define, use that unique define name in order to
324 * detect the problem header and adjust to suit.
325 */
326#if defined(D_MAX_DATA_UNITS_IN_MCU)
336#define width_in_blocks width_in_data_units
337#endif
338
339/*
340 * On some machines it may be worthwhile to use _setjmp or sigsetjmp
341 * in place of plain setjmp. These macros will make it easier.
342 */
355#define SETJMP(jbuf) setjmp(jbuf)
372#define LONGJMP(jbuf, code) longjmp(jbuf, code)
380#define JMP_BUF jmp_buf
381
389typedef struct jpeg_destination_mgr tiff_jpeg_destination_mgr;
397typedef struct jpeg_source_mgr tiff_jpeg_source_mgr;
405typedef struct jpeg_error_mgr tiff_jpeg_error_mgr;
406
407/*
408 * State block for each open TIFF file using
409 * libjpeg to do JPEG compression/decompression.
410 *
411 * libjpeg's visible state is either a jpeg_compress_struct
412 * or jpeg_decompress_struct depending on which way we
413 * are going. comm can be used to refer to the fields
414 * which are common to both.
415 *
416 * NB: cinfo is required to be the first member of JPEGState,
417 * so we can safely cast JPEGState* -> jpeg_xxx_struct*
418 * and vice versa!
419 */
420#ifdef _MSC_VER
421#pragma warning(push)
422#pragma warning(disable : 4324) /* structure padding due to alignment */
423#endif
433typedef struct
434{
437 union
438 {
440 struct jpeg_compress_struct c;
444 struct jpeg_decompress_struct d;
449 struct jpeg_common_struct comm;
450 } cinfo;
454
461
463 struct jpeg_progress_mgr progress;
464
471
475 uint16_t photometric;
477 uint16_t h_sampling;
479 uint16_t v_sampling;
483 uint32_t strile_width;
486
491 TIFF_JSAMPARRAY ds_buffer[MAX_COMPONENTS];
496
502
505} JPEGState;
506#ifdef _MSC_VER
507#pragma warning(pop)
508#endif
509
519#define JState(tif) ((JPEGState *)(tif)->tif_data)
520
521static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
522static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
523static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
524static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
525static int JPEGInitializeLibJPEG(TIFF *tif, int decompress);
526static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
527
536#define FIELD_JPEGTABLES (FIELD_CODEC + 0)
537
545static const TIFFField jpegFields[] = {
547 FIELD_JPEGTABLES, FALSE, TRUE, "JPEGTables", NULL},
549 TRUE, FALSE, "", NULL},
551 FALSE, FALSE, "", NULL},
553 FALSE, FALSE, "", NULL}};
554
555/*
556 * libjpeg interface layer.
557 *
558 * We use setjmp/longjmp to return control to libtiff
559 * when a fatal error is encountered within the JPEG
560 * library. We also direct libjpeg error and warning
561 * messages through the appropriate libtiff handlers.
562 */
563
564/*
565 * Error handling routines (these replace corresponding
566 * IJG routines from jerror.c). These are used for both
567 * compression and decompression.
568 */
582static void TIFFjpeg_error_exit(j_common_ptr cinfo)
583{
584 JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
585 char buffer[JMSG_LENGTH_MAX];
586
587 (*cinfo->err->format_message)(cinfo, buffer);
588 TIFFErrorExtR(sp->tif, "JPEGLib", "%s",
589 buffer); /* display the error message */
590 jpeg_abort(cinfo); /* clean up libjpeg state */
591 LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
592}
593
594/*
595 * This routine is invoked only for warning messages,
596 * since error_exit does its own thing and trace_level
597 * is never set > 0.
598 */
610static void TIFFjpeg_output_message(j_common_ptr cinfo)
611{
612 char buffer[JMSG_LENGTH_MAX];
613
614 (*cinfo->err->format_message)(cinfo, buffer);
615 TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer);
616}
617
618/* Avoid the risk of denial-of-service on crafted JPEGs with an insane */
619/* number of scans. */
620/* See
621 * http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf
622 */
636static void TIFFjpeg_progress_monitor(j_common_ptr cinfo)
637{
638 JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
639 if (cinfo->is_decompressor)
640 {
641 const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number;
642 if (scan_no >= sp->otherSettings.max_allowed_scan_number)
643 {
645 ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor",
646 "Scan number %d exceeds maximum scans (%d). This limit "
647 "can be raised through the "
648 "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER "
649 "environment variable.",
651
652 jpeg_abort(cinfo); /* clean up libjpeg state */
653 LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
654 }
655 }
656}
657
658/*
659 * Interface routines. This layer of routines exists
660 * primarily to limit side-effects from using setjmp.
661 * Also, normal/error returns are converted into return
662 * values per libtiff practice.
663 */
687#define CALLJPEG(sp, fail, op) (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op))
701#define CALLVJPEG(sp, op) CALLJPEG(sp, 0, ((op), 1))
702
721{
722 /* initialize JPEG error handling */
723 sp->cinfo.c.err = jpeg_std_error(&sp->err);
724 sp->err.error_exit = TIFFjpeg_error_exit;
725 sp->err.output_message = TIFFjpeg_output_message;
726
727 /* set client_data to avoid UMR warning from tools like Purify */
728 sp->cinfo.c.client_data = NULL;
729
730 return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c));
731}
732
751{
752 /* initialize JPEG error handling */
753 sp->cinfo.d.err = jpeg_std_error(&sp->err);
754 sp->err.error_exit = TIFFjpeg_error_exit;
755 sp->err.output_message = TIFFjpeg_output_message;
756
757 /* set client_data to avoid UMR warning from tools like Purify */
758 sp->cinfo.d.client_data = NULL;
759
760 return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d));
761}
762
779{
780 return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c));
781}
782
799static int TIFFjpeg_set_colorspace(JPEGState *sp, J_COLOR_SPACE colorspace)
800{
801 return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace));
802}
803
823static int TIFFjpeg_set_quality(JPEGState *sp, int quality,
824 boolean force_baseline)
825{
826 return CALLVJPEG(sp,
827 jpeg_set_quality(&sp->cinfo.c, quality, force_baseline));
828}
829
846static int TIFFjpeg_suppress_tables(JPEGState *sp, boolean suppress)
847{
848 return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress));
849}
850
868static int TIFFjpeg_start_compress(JPEGState *sp, boolean write_all_tables)
869{
870 return CALLVJPEG(sp, jpeg_start_compress(&sp->cinfo.c, write_all_tables));
871}
872
895 int num_lines)
896{
897#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
898 return CALLJPEG(sp, -1,
899 (int)jpeg12_write_scanlines(&sp->cinfo.c, scanlines,
900 (JDIMENSION)num_lines));
901#else
902 return CALLJPEG(sp, -1,
903 (int)jpeg_write_scanlines(&sp->cinfo.c, scanlines,
904 (JDIMENSION)num_lines));
905#endif
906}
907
930 int num_lines)
931{
932#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
933 return CALLJPEG(
934 sp, -1,
935 (int)jpeg12_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines));
936#else
937 return CALLJPEG(
938 sp, -1,
939 (int)jpeg_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines));
940#endif
941}
942
959{
960 return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c));
961}
962
979{
980 return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c));
981}
982
1000static int TIFFjpeg_read_header(JPEGState *sp, boolean require_image)
1001{
1002 return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image));
1003}
1004
1021{
1022 return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d));
1023}
1024
1046{
1047 const char *sz_max_allowed_scan_number;
1048 /* progress monitor */
1049 sp->cinfo.d.progress = &sp->progress;
1050 sp->progress.progress_monitor = TIFFjpeg_progress_monitor;
1052 sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER");
1053 if (sz_max_allowed_scan_number)
1055 (int)_TIFFParseUInt32(sz_max_allowed_scan_number, 100, INT_MAX);
1056
1057 return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d));
1058}
1059
1082 int max_lines)
1083{
1084#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
1085 return CALLJPEG(sp, -1,
1086 (int)jpeg12_read_scanlines(&sp->cinfo.d, scanlines,
1087 (JDIMENSION)max_lines));
1088#else
1089 return CALLJPEG(sp, -1,
1090 (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines,
1091 (JDIMENSION)max_lines));
1092#endif
1093}
1094
1117 int max_lines)
1118{
1119#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
1120 return CALLJPEG(
1121 sp, -1,
1122 (int)jpeg12_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines));
1123#else
1124 return CALLJPEG(
1125 sp, -1,
1126 (int)jpeg_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines));
1127#endif
1128}
1129
1152static tmsize_t JPEGRawBytesPerClumpLine(TIFF *tif, uint32_t width,
1153 uint16_t h_sampling,
1154 uint16_t v_sampling,
1155 int data_precision, const char *module)
1156{
1157 uint64_t clumps_per_line;
1158 uint64_t samples_per_clump;
1159 uint64_t samples_per_clumpline;
1160 uint64_t bits_per_clumpline;
1161 uint64_t bytes_per_clumpline;
1162
1163 if (h_sampling == 0 || v_sampling == 0 || data_precision <= 0)
1164 {
1165 TIFFErrorExtR(tif, module, "Invalid JPEG sampling or precision");
1166 return 0;
1167 }
1168
1169 clumps_per_line = TIFFhowmany_64(width, h_sampling);
1170 samples_per_clump = _TIFFAdd64(
1171 tif, _TIFFMultiply64(tif, h_sampling, v_sampling, module), 2, module);
1172 samples_per_clumpline =
1173 _TIFFMultiply64(tif, clumps_per_line, samples_per_clump, module);
1174 bits_per_clumpline = _TIFFMultiply64(tif, samples_per_clumpline,
1175 (uint64_t)data_precision, module);
1176 bytes_per_clumpline = TIFFhowmany8_64(bits_per_clumpline);
1177 return _TIFFCastUInt64ToSSize(tif, bytes_per_clumpline, module);
1178}
1179
1180/*
1181 * Read/write the packed 12-bit byte layout used by the existing JPEG
1182 * 12-bit pair-based conversion:
1183 *
1184 * sample0: byte0[7:0] + byte1[7:4]
1185 * sample1: byte1[3:0] + byte2[7:0]
1186 *
1187 * This is packed sample layout, not a host-endian typed load. These helpers
1188 * preserve the previous byte layout and only make odd final samples explicit.
1189 * FillOrder bit reversal is not applied here.
1190 */
1206static TIFF_JSAMPLE JPEGReadPacked12Sample(const uint8_t *buf,
1207 tmsize_t sample_index)
1208{
1209 const uint8_t *p = buf + (sample_index / 2) * 3;
1210 if ((sample_index & 1) != 0)
1211 return (TIFF_JSAMPLE)(((p[1] & 0x0f) << 8) | p[2]);
1212 return (TIFF_JSAMPLE)((p[0] << 4) | ((p[1] & 0xf0) >> 4));
1213}
1214
1231static void TIFF_ATTRIBUTE((unused))
1232 JPEGWritePacked12Sample(uint8_t *buf, tmsize_t sample_index,
1233 TIFF_JSAMPLE sample)
1234{
1235 uint16_t v = (uint16_t)sample & 0x0fff;
1236 uint8_t *p = buf + (sample_index / 2) * 3;
1237 if ((sample_index & 1) != 0)
1238 {
1239 p[1] = (uint8_t)((p[1] & 0xf0) | ((v >> 8) & 0x0f));
1240 p[2] = (uint8_t)(v & 0xff);
1241 }
1242 else
1243 {
1244 p[0] = (uint8_t)(v >> 4);
1245 p[1] = (uint8_t)((v & 0x0f) << 4);
1246 }
1247}
1248
1264{
1265 return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d));
1266}
1267
1284{
1285 return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm));
1286}
1287
1304{
1305 return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm));
1306}
1307
1330static JSAMPARRAY TIFFjpeg_alloc_sarray(JPEGState *sp, int pool_id,
1331 JDIMENSION samplesperrow,
1332 JDIMENSION numrows)
1333{
1334 return CALLJPEG(sp, (JSAMPARRAY)NULL,
1335 (*sp->cinfo.comm.mem->alloc_sarray)(
1336 &sp->cinfo.comm, pool_id, samplesperrow, numrows));
1337}
1338
1339/*
1340 * JPEG library destination data manager.
1341 * These routines direct compressed data from libjpeg into the
1342 * libtiff output buffer.
1343 */
1344
1356static void std_init_destination(j_compress_ptr cinfo)
1357{
1358 JPEGState *sp = (JPEGState *)cinfo;
1359 TIFF *tif = sp->tif;
1360
1361 sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
1362 sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
1363}
1364
1378static boolean std_empty_output_buffer(j_compress_ptr cinfo)
1379{
1380 JPEGState *sp = (JPEGState *)cinfo;
1381 TIFF *tif = sp->tif;
1382
1383 /* the entire buffer has been filled */
1384 tif->tif_rawcc = tif->tif_rawdatasize;
1385
1386#ifdef IPPJ_HUFF
1387 /*
1388 * The Intel IPP performance library does not necessarily fill up
1389 * the whole output buffer on each pass, so only dump out the parts
1390 * that have been filled.
1391 * http://trac.osgeo.org/gdal/wiki/JpegIPP
1392 */
1393 if (sp->dest.free_in_buffer >= 0)
1394 {
1395 tif->tif_rawcc = tif->tif_rawdatasize - sp->dest.free_in_buffer;
1396 }
1397#endif
1398
1399 if (!TIFFFlushData1(tif))
1400 return FALSE;
1401 sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
1402 sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
1403
1404 return (TRUE);
1405}
1406
1418static void std_term_destination(j_compress_ptr cinfo)
1419{
1420 JPEGState *sp = (JPEGState *)cinfo;
1421 TIFF *tif = sp->tif;
1422
1423 tif->tif_rawcp = (uint8_t *)sp->dest.next_output_byte;
1424 tif->tif_rawcc = tif->tif_rawdatasize - (tmsize_t)sp->dest.free_in_buffer;
1425 /* NB: libtiff does the final buffer flush */
1426}
1427
1441static void TIFFjpeg_data_dest(JPEGState *sp, TIFF *tif)
1442{
1443 (void)tif;
1444 sp->cinfo.c.dest = &sp->dest;
1445 sp->dest.init_destination = std_init_destination;
1446 sp->dest.empty_output_buffer = std_empty_output_buffer;
1447 sp->dest.term_destination = std_term_destination;
1448}
1449
1450/*
1451 * Alternate destination manager for outputting to JPEGTables field.
1452 */
1453
1465static void tables_init_destination(j_compress_ptr cinfo)
1466{
1467 JPEGState *sp = (JPEGState *)cinfo;
1468
1469 /* while building, otherSettings.jpegtables_length is allocated buffer size
1470 */
1471 sp->dest.next_output_byte = (JOCTET *)sp->otherSettings.jpegtables;
1472 sp->dest.free_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
1473}
1474
1490static boolean tables_empty_output_buffer(j_compress_ptr cinfo)
1491{
1492 JPEGState *sp = (JPEGState *)cinfo;
1493 void *newbuf;
1494
1495 /* the entire buffer has been filled; enlarge it by 1000 bytes */
1496 newbuf =
1499 if (newbuf == NULL)
1500 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 100);
1501 sp->dest.next_output_byte =
1502 (JOCTET *)newbuf + sp->otherSettings.jpegtables_length;
1503 sp->dest.free_in_buffer = (size_t)1000;
1504 sp->otherSettings.jpegtables = newbuf;
1505 sp->otherSettings.jpegtables_length += 1000;
1506 return (TRUE);
1507}
1508
1520static void tables_term_destination(j_compress_ptr cinfo)
1521{
1522 JPEGState *sp = (JPEGState *)cinfo;
1523
1524 /* set tables length to number of bytes actually emitted */
1525 sp->otherSettings.jpegtables_length -= (uint32_t)sp->dest.free_in_buffer;
1526}
1527
1544{
1545 (void)tif;
1546 /*
1547 * Allocate a working buffer for building tables.
1548 * Initial size is 1000 bytes, which is usually adequate.
1549 */
1550 if (sp->otherSettings.jpegtables)
1555 if (sp->otherSettings.jpegtables == NULL)
1556 {
1558 TIFFErrorExtR(sp->tif, "TIFFjpeg_tables_dest",
1559 "No space for JPEGTables");
1560 return (0);
1561 }
1562 sp->cinfo.c.dest = &sp->dest;
1563 sp->dest.init_destination = tables_init_destination;
1564 sp->dest.empty_output_buffer = tables_empty_output_buffer;
1565 sp->dest.term_destination = tables_term_destination;
1566 return (1);
1567}
1568
1569/*
1570 * JPEG library source data manager.
1571 * These routines supply compressed data to libjpeg.
1572 */
1573
1585static void std_init_source(j_decompress_ptr cinfo)
1586{
1587 JPEGState *sp = (JPEGState *)cinfo;
1588 TIFF *tif = sp->tif;
1589
1590 sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata;
1591 sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
1592}
1593
1608static boolean std_fill_input_buffer(j_decompress_ptr cinfo)
1609{
1610 JPEGState *sp = (JPEGState *)cinfo;
1611 static const JOCTET dummy_EOI[2] = {0xFF, JPEG_EOI};
1612
1613#ifdef IPPJ_HUFF
1614 /*
1615 * The Intel IPP performance library does not necessarily read the whole
1616 * input buffer in one pass, so it is possible to get here with data
1617 * yet to read.
1618 *
1619 * We just return without doing anything, until the entire buffer has
1620 * been read.
1621 * http://trac.osgeo.org/gdal/wiki/JpegIPP
1622 */
1623 if (sp->src.bytes_in_buffer > 0)
1624 {
1625 return (TRUE);
1626 }
1627#endif
1628
1629 /*
1630 * Normally the whole strip/tile is read and so we don't need to do
1631 * a fill. In the case of CHUNKY_STRIP_READ_SUPPORT we might not have
1632 * all the data, but the rawdata is refreshed between scanlines and
1633 * we push this into the io machinery in JPEGDecode().
1634 * http://trac.osgeo.org/gdal/ticket/3894
1635 */
1636
1637 WARNMS(cinfo, JWRN_JPEG_EOF);
1638 /* insert a fake EOI marker */
1639 sp->src.next_input_byte = dummy_EOI;
1640 sp->src.bytes_in_buffer = 2;
1641 return (TRUE);
1642}
1643
1658static void std_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
1659{
1660 JPEGState *sp = (JPEGState *)cinfo;
1661
1662 if (num_bytes > 0)
1663 {
1664 if ((size_t)num_bytes > sp->src.bytes_in_buffer)
1665 {
1666 /* oops, buffer overrun */
1667 (void)std_fill_input_buffer(cinfo);
1668 }
1669 else
1670 {
1671 sp->src.next_input_byte += (size_t)num_bytes;
1672 sp->src.bytes_in_buffer -= (size_t)num_bytes;
1673 }
1674 }
1675}
1676
1686static void std_term_source(j_decompress_ptr cinfo)
1687{
1688 /* No work necessary here */
1689 (void)cinfo;
1690}
1691
1704{
1705 sp->cinfo.d.src = &sp->src;
1706 sp->src.init_source = std_init_source;
1707 sp->src.fill_input_buffer = std_fill_input_buffer;
1708 sp->src.skip_input_data = std_skip_input_data;
1709 sp->src.resync_to_restart = jpeg_resync_to_restart;
1710 sp->src.term_source = std_term_source;
1711 sp->src.bytes_in_buffer = 0; /* for safety */
1712 sp->src.next_input_byte = NULL;
1713}
1714
1715/*
1716 * Alternate source manager for reading from JPEGTables.
1717 * We can share all the code except for the init routine.
1718 */
1719
1731static void tables_init_source(j_decompress_ptr cinfo)
1732{
1733 JPEGState *sp = (JPEGState *)cinfo;
1734
1735 sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables;
1736 sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
1737}
1738
1751{
1753 sp->src.init_source = tables_init_source;
1754}
1755
1756/*
1757 * Allocate downsampled-data buffers needed for downsampled I/O.
1758 * We use values computed in jpeg_start_compress or jpeg_start_decompress.
1759 * We use libjpeg's allocator so that buffers will be released automatically
1760 * when done with strip/tile.
1761 * This is also a handy place to compute samplesperclump, bytesperline.
1762 */
1784static int alloc_downsampled_buffers(TIFF *tif, jpeg_component_info *comp_info,
1785 int num_components)
1786{
1787 JPEGState *sp = JState(tif);
1788 int ci;
1789 jpeg_component_info *compptr;
1790 TIFF_JSAMPARRAY buf;
1791 int samples_per_clump = 0;
1792
1793 for (ci = 0, compptr = comp_info; ci < num_components; ci++, compptr++)
1794 {
1795 samples_per_clump += compptr->h_samp_factor * compptr->v_samp_factor;
1797 sp, JPOOL_IMAGE, compptr->width_in_blocks * DCTSIZE,
1798 (JDIMENSION)(compptr->v_samp_factor * DCTSIZE));
1799 if (buf == NULL)
1800 return (0);
1801 sp->ds_buffer[ci] = buf;
1802 }
1803 sp->samplesperclump = samples_per_clump;
1804 return (1);
1805}
1806
1807/*
1808 * JPEG Decoding.
1809 */
1810
1811#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
1812
1820#define JPEG_MARKER_SOF0 0xC0
1828#define JPEG_MARKER_SOF1 0xC1
1836#define JPEG_MARKER_SOF2 0xC2
1844#define JPEG_MARKER_SOF9 0xC9
1852#define JPEG_MARKER_SOF10 0xCA
1860#define JPEG_MARKER_DHT 0xC4
1868#define JPEG_MARKER_SOI 0xD8
1876#define JPEG_MARKER_SOS 0xDA
1884#define JPEG_MARKER_DQT 0xDB
1892#define JPEG_MARKER_DRI 0xDD
1901#define JPEG_MARKER_APP0 0xE0
1909#define JPEG_MARKER_COM 0xFE
1937static void JPEGFixupTagsSubsampling(TIFF *tif);
1938static int
1940static int
1942 uint8_t *result);
1943static int
1945 uint16_t *result);
1946static void
1948 uint16_t skiplength);
1949
1950#endif
1951
1967static int JPEGFixupTags(TIFF *tif)
1968{
1969#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
1970 JPEGState *sp = JState(tif);
1973 (tif->tif_dir.td_samplesperpixel == 3) &&
1976#endif
1977
1978 return (1);
1979}
1980
1981#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
1982
1998{
1999 /*
2000 * Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in
2001 * the TIFF tags, but still use non-default (2,2) values within the jpeg
2002 * data stream itself. In order for TIFF applications to work properly
2003 * - for instance to get the strip buffer size right - it is imperative
2004 * that the subsampling be available before we start reading the image
2005 * data normally. This function will attempt to analyze the first strip in
2006 * order to get the sampling values from the jpeg data stream.
2007 *
2008 * Note that JPEGPreDeocode() will produce a fairly loud warning when the
2009 * discovered sampling does not match the default sampling (2,2) or whatever
2010 * was actually in the tiff tags.
2011 *
2012 * See the bug in bugzilla for details:
2013 *
2014 * http://bugzilla.remotesensing.org/show_bug.cgi?id=168
2015 *
2016 * Frank Warmerdam, July 2002
2017 * Joris Van Damme, May 2007
2018 */
2019 static const char module[] = "JPEGFixupTagsSubsampling";
2021 uint64_t fileoffset = TIFFGetStrileOffset(tif, 0);
2022
2023 if (fileoffset == 0)
2024 {
2025 /* Do not even try to check if the first strip/tile does not
2026 yet exist, as occurs when GDAL has created a new NULL file
2027 for instance. */
2028 return;
2029 }
2030
2031 m.tif = tif;
2032 m.buffersize = 2048;
2034 if (m.buffer == NULL)
2035 {
2036 TIFFWarningExtR(tif, module,
2037 "Unable to allocate memory for auto-correcting of "
2038 "subsampling values; auto-correcting skipped");
2039 return;
2040 }
2041 m.buffercurrentbyte = NULL;
2042 m.bufferbytesleft = 0;
2044 m.filepositioned = 0;
2048 tif, module,
2049 "Unable to auto-correct subsampling values, likely corrupt JPEG "
2050 "compressed data in first strip/tile; auto-correcting skipped");
2052}
2053
2070static int
2072{
2073 static const char module[] = "JPEGFixupTagsSubsamplingSec";
2074 uint8_t m;
2075 while (1)
2076 {
2077 while (1)
2078 {
2079 if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
2080 return (0);
2081 if (m == 255)
2082 break;
2083 }
2084 while (1)
2085 {
2086 if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
2087 return (0);
2088 if (m != 255)
2089 break;
2090 }
2091 switch (m)
2092 {
2093 case JPEG_MARKER_SOI:
2094 /* this type of marker has no data and should be skipped */
2095 break;
2096 case JPEG_MARKER_COM:
2097 case JPEG_MARKER_APP0:
2098 case JPEG_MARKER_APP0 + 1:
2099 case JPEG_MARKER_APP0 + 2:
2100 case JPEG_MARKER_APP0 + 3:
2101 case JPEG_MARKER_APP0 + 4:
2102 case JPEG_MARKER_APP0 + 5:
2103 case JPEG_MARKER_APP0 + 6:
2104 case JPEG_MARKER_APP0 + 7:
2105 case JPEG_MARKER_APP0 + 8:
2106 case JPEG_MARKER_APP0 + 9:
2107 case JPEG_MARKER_APP0 + 10:
2108 case JPEG_MARKER_APP0 + 11:
2109 case JPEG_MARKER_APP0 + 12:
2110 case JPEG_MARKER_APP0 + 13:
2111 case JPEG_MARKER_APP0 + 14:
2112 case JPEG_MARKER_APP0 + 15:
2113 case JPEG_MARKER_DQT:
2114 case JPEG_MARKER_SOS:
2115 case JPEG_MARKER_DHT:
2116 case JPEG_MARKER_DRI:
2117 /* this type of marker has data, but it has no use to us and
2118 * should be skipped */
2119 {
2120 uint16_t n;
2121 if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
2122 return (0);
2123 if (n < 2)
2124 return (0);
2125 n = (uint16_t)(n - 2);
2126 if (n > 0)
2128 }
2129 break;
2130 case JPEG_MARKER_SOF0: /* Baseline sequential Huffman */
2131 case JPEG_MARKER_SOF1: /* Extended sequential Huffman */
2132 case JPEG_MARKER_SOF2: /* Progressive Huffman: normally not allowed
2133 by TechNote, but that doesn't hurt
2134 supporting it */
2135 case JPEG_MARKER_SOF9: /* Extended sequential arithmetic */
2136 case JPEG_MARKER_SOF10: /* Progressive arithmetic: normally not
2137 allowed by TechNote, but that doesn't
2138 hurt supporting it */
2139 /* this marker contains the subsampling factors we're scanning
2140 * for */
2141 {
2142 uint16_t n;
2143 uint16_t o;
2144 uint8_t p;
2145 uint8_t ph, pv;
2146 if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
2147 return (0);
2148 if (n != 8 + data->tif->tif_dir.td_samplesperpixel * 3)
2149 return (0);
2151 if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
2152 return (0);
2153 ph = (p >> 4);
2154 pv = (p & 15);
2156 for (o = 1; o < data->tif->tif_dir.td_samplesperpixel; o++)
2157 {
2159 if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
2160 return (0);
2161 if (p != 0x11)
2162 {
2163 TIFFWarningExtR(data->tif, module,
2164 "Subsampling values inside JPEG "
2165 "compressed data "
2166 "have no TIFF equivalent, "
2167 "auto-correction of TIFF "
2168 "subsampling values failed");
2169 return (1);
2170 }
2172 }
2173 if (((ph != 1) && (ph != 2) && (ph != 4)) ||
2174 ((pv != 1) && (pv != 2) && (pv != 4)))
2175 {
2176 TIFFWarningExtR(data->tif, module,
2177 "Subsampling values inside JPEG "
2178 "compressed data have no TIFF "
2179 "equivalent, auto-correction of TIFF "
2180 "subsampling values failed");
2181 return (1);
2182 }
2183 if ((ph != data->tif->tif_dir.td_ycbcrsubsampling[0]) ||
2184 (pv != data->tif->tif_dir.td_ycbcrsubsampling[1]))
2185 {
2187 data->tif, module,
2188 "Auto-corrected former TIFF subsampling values "
2189 "[%" PRIu16 ",%" PRIu16
2190 "] to match subsampling values inside JPEG "
2191 "compressed data [%" PRIu8 ",%" PRIu8 "]",
2193 data->tif->tif_dir.td_ycbcrsubsampling[1], ph, pv);
2194 data->tif->tif_dir.td_ycbcrsubsampling[0] = ph;
2195 data->tif->tif_dir.td_ycbcrsubsampling[1] = pv;
2196 }
2197 }
2198 return (1);
2199 default:
2200 return (0);
2201 }
2202 }
2203}
2204
2221static int
2223 uint8_t *result)
2224{
2225 if (data->bufferbytesleft == 0)
2226 {
2227 uint32_t m;
2228 if (data->filebytesleft == 0)
2229 return (0);
2230 if (!data->filepositioned)
2231 {
2232 if (TIFFSeekFile(data->tif, data->fileoffset, SEEK_SET) ==
2233 (toff_t)-1)
2234 {
2235 return 0;
2236 }
2237 data->filepositioned = 1;
2238 }
2239 m = data->buffersize;
2240 if ((uint64_t)m > data->filebytesleft)
2241 m = (uint32_t)data->filebytesleft;
2242 assert(m < 0x80000000UL);
2243 if (TIFFReadFile(data->tif, data->buffer, (tmsize_t)m) != (tmsize_t)m)
2244 return (0);
2245 data->buffercurrentbyte = (uint8_t *)data->buffer;
2246 data->bufferbytesleft = m;
2247 data->fileoffset += m;
2248 data->filebytesleft -= m;
2249 }
2250 *result = *data->buffercurrentbyte;
2251 data->buffercurrentbyte++;
2252 data->bufferbytesleft--;
2253 return (1);
2254}
2255
2271static int
2273 uint16_t *result)
2274{
2275 uint8_t ma;
2276 uint8_t mb;
2277 if (!JPEGFixupTagsSubsamplingReadByte(data, &ma))
2278 return (0);
2279 if (!JPEGFixupTagsSubsamplingReadByte(data, &mb))
2280 return (0);
2281 *result = (uint16_t)((ma << 8) | mb);
2282 return (1);
2283}
2284
2300static void
2302 uint16_t skiplength)
2303{
2304 if ((uint32_t)skiplength <= data->bufferbytesleft)
2305 {
2306 data->buffercurrentbyte += skiplength;
2307 data->bufferbytesleft -= skiplength;
2308 }
2309 else
2310 {
2311 uint16_t m;
2312 m = (uint16_t)(skiplength - data->bufferbytesleft);
2313 if (m <= data->filebytesleft)
2314 {
2315 data->bufferbytesleft = 0;
2316 data->fileoffset += m;
2317 data->filebytesleft -= m;
2318 data->filepositioned = 0;
2319 }
2320 else
2321 {
2322 data->bufferbytesleft = 0;
2323 data->filebytesleft = 0;
2324 }
2325 }
2326}
2327
2328#endif
2329
2347{
2348 JPEGState *sp = JState(tif);
2349 TIFFDirectory *td = &tif->tif_dir;
2350
2351#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
2352 if (tif->tif_dir.td_bitspersample == 12)
2353 {
2354 /* We pass a pointer to a copy of otherSettings, since */
2355 /* TIFFReInitJPEG_12() will clear sp */
2356 JPEGOtherSettings savedOtherSettings = sp->otherSettings;
2357 return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0);
2358 }
2359#endif
2360
2362 {
2363 TIFFErrorExtR(tif, "JPEGSetupDecode",
2364 "Failed to initialize libjpeg decompressor");
2365 return (0);
2366 }
2367
2368 assert(sp != NULL);
2369 assert(sp->cinfo.comm.is_decompressor);
2370
2371 /* Read JPEGTables if it is present */
2373 {
2375 if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY)
2376 {
2377 TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field");
2378 return (0);
2379 }
2380 }
2381
2382 /* Grab parameters that are same for all strips/tiles */
2383 sp->photometric = td->td_photometric;
2384 switch (sp->photometric)
2385 {
2386 case PHOTOMETRIC_YCBCR:
2387 sp->h_sampling = td->td_ycbcrsubsampling[0];
2388 sp->v_sampling = td->td_ycbcrsubsampling[1];
2389 break;
2390 default:
2391 /* TIFF 6.0 forbids subsampling of all other color spaces */
2392 sp->h_sampling = 1;
2393 sp->v_sampling = 1;
2394 break;
2395 }
2396
2397 /* Set up for reading normal data */
2399 tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */
2400 return (1);
2401}
2402
2403/* Returns 1 if the full strip should be read, even when doing scanline per */
2404/* scanline decoding. This happens when the JPEG stream uses multiple scans. */
2405/* Currently only called in CHUNKY_STRIP_READ_SUPPORT mode through */
2406/* scanline interface. */
2407/* Only reads tif->tif_dir.td_bitspersample, tif->tif_rawdata and */
2408/* tif->tif_rawcc members. */
2409/* Can be called independently of the usual setup/predecode/decode states */
2411{
2412 int ret;
2413 JPEGState state;
2414
2415#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
2416 if (tif->tif_dir.td_bitspersample == 12)
2418#endif
2419
2420 memset(&state, 0, sizeof(JPEGState));
2421 state.tif = tif;
2422
2424
2425 TIFFjpeg_data_src(&state);
2426
2427 if (TIFFjpeg_read_header(&state, TRUE) != JPEG_HEADER_OK)
2428 {
2429 TIFFjpeg_destroy(&state);
2430 return (0);
2431 }
2432 ret = TIFFjpeg_has_multiple_scans(&state);
2433
2434 TIFFjpeg_destroy(&state);
2435
2436 return ret;
2437}
2438
2459{
2460 JPEGState *sp = JState(tif);
2461 TIFFDirectory *td = &tif->tif_dir;
2462
2463 /*
2464 * Check image parameters and set decompression parameters.
2465 */
2466 if (isTiled(tif))
2467 {
2468 sp->strile_width = td->td_tilewidth;
2469 sp->strile_height = td->td_tilelength;
2471 }
2472 else
2473 {
2474 sp->strile_width = td->td_imagewidth;
2476 if (sp->strile_height > td->td_rowsperstrip)
2479 }
2480 if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
2481 {
2482 /*
2483 * For PC 2, scale down the expected strip/tile size
2484 * to match a downsampled component
2485 */
2486 if (sp->h_sampling == 0 || sp->v_sampling == 0)
2487 {
2488 TIFFErrorExtR(tif, "JPEGComputeStrileWidthHeightBytesPerLine",
2489 "JPEG horizontal or vertical sampling is zero");
2490 return (0);
2491 }
2494 }
2495
2496 return 1;
2497}
2498
2499/*
2500 * Set up for decoding a strip or tile.
2501 */
2502/*ARGSUSED*/
2532static int JPEGPreDecode(TIFF *tif, uint16_t s)
2533{
2534 JPEGState *sp = JState(tif);
2535 TIFFDirectory *td = &tif->tif_dir;
2536 static const char module[] = "JPEGPreDecode";
2537 int downsampled_output;
2538 int ci;
2539
2540 assert(sp != NULL);
2541
2542 if (sp->cinfo.comm.is_decompressor == 0)
2543 {
2545 }
2546
2547 assert(sp->cinfo.comm.is_decompressor);
2548 /*
2549 * Reset decoder state from any previous strip/tile,
2550 * in case application didn't read the whole strip.
2551 */
2552 if (!TIFFjpeg_abort(sp))
2553 return (0);
2554 /*
2555 * Read the header for this strip/tile.
2556 */
2557
2558 if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
2559 return (0);
2560
2561 tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
2562 tif->tif_rawcc = (tmsize_t)sp->src.bytes_in_buffer;
2563
2565 return 0;
2566
2567 if (sp->cinfo.d.image_width < sp->strile_width ||
2568 sp->cinfo.d.image_height < sp->strile_height)
2569 {
2570 TIFFWarningExtR(tif, module,
2571 "Improper JPEG strip/tile size, "
2572 "expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
2573 sp->strile_width, sp->strile_height,
2574 sp->cinfo.d.image_width, sp->cinfo.d.image_height);
2575 }
2576 if (sp->cinfo.d.image_width == sp->strile_width &&
2577 sp->cinfo.d.image_height > sp->strile_height &&
2579 !isTiled(tif))
2580 {
2581 /* Some files have a last strip, that should be truncated, */
2582 /* but their JPEG codestream has still the maximum strip */
2583 /* height. Warn about this as this is non compliant, but */
2584 /* we can safely recover from that. */
2585 TIFFWarningExtR(tif, module,
2586 "JPEG strip size exceeds expected dimensions,"
2587 " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
2588 sp->strile_width, sp->strile_height,
2589 sp->cinfo.d.image_width, sp->cinfo.d.image_height);
2590 }
2591 else if (sp->cinfo.d.image_width > sp->strile_width ||
2592 sp->cinfo.d.image_height > sp->strile_height)
2593 {
2594 /*
2595 * This case could be dangerous, if the strip or tile size has
2596 * been reported as less than the amount of data jpeg will
2597 * return, some potential security issues arise. Catch this
2598 * case and error out.
2599 */
2600 TIFFErrorExtR(tif, module,
2601 "JPEG strip/tile size exceeds expected dimensions,"
2602 " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
2603 sp->strile_width, sp->strile_height,
2604 sp->cinfo.d.image_width, sp->cinfo.d.image_height);
2605 return (0);
2606 }
2607 if (sp->cinfo.d.num_components !=
2609 : 1))
2610 {
2611 TIFFErrorExtR(tif, module, "Improper JPEG component count");
2612 return (0);
2613 }
2614#ifdef JPEG_LIB_MK1
2615 if (12 != td->td_bitspersample && 8 != td->td_bitspersample)
2616 {
2617 TIFFErrorExtR(tif, module, "Improper JPEG data precision");
2618 return (0);
2619 }
2620 sp->cinfo.d.data_precision = td->td_bitspersample;
2621 sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
2622#else
2623 if (td->td_bitspersample != BITS_IN_JSAMPLE ||
2624 sp->cinfo.d.data_precision != td->td_bitspersample)
2625 {
2626 TIFFErrorExtR(tif, module, "Improper JPEG data precision");
2627 return (0);
2628 }
2629#endif
2630
2631 if (sp->cinfo.d.progressive_mode &&
2633 {
2634 TIFFWarningExtR(tif, module,
2635 "The JPEG strip/tile is encoded with progressive mode, "
2636 "which is normally not legal for JPEG-in-TIFF.\n"
2637 "libtiff should be able to decode it, but it might "
2638 "cause compatibility issues with other readers");
2640 }
2641
2642 /* In some cases, libjpeg needs to allocate a lot of memory */
2643 /* http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf
2644 */
2646 {
2647 /* In this case libjpeg will need to allocate memory or backing */
2648 /* store for all coefficients */
2649 /* See call to jinit_d_coef_controller() from master_selection() */
2650 /* in libjpeg */
2651
2652 /* 1 MB for regular libjpeg usage */
2653 toff_t nRequiredMemory = 1024 * 1024;
2654
2655 for (ci = 0; ci < sp->cinfo.d.num_components; ci++)
2656 {
2657 const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]);
2658 if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0)
2659 {
2660 nRequiredMemory +=
2661 (toff_t)((JDIMENSION)(((int)compptr->width_in_blocks +
2662 compptr->h_samp_factor - 1) /
2663 compptr->h_samp_factor)) *
2664 (JDIMENSION)(((int)compptr->height_in_blocks +
2665 compptr->v_samp_factor - 1) /
2666 compptr->v_samp_factor) *
2667 sizeof(JBLOCK);
2668 }
2669 }
2670
2671 if (sp->cinfo.d.mem->max_memory_to_use > 0 &&
2672 nRequiredMemory > (toff_t)(sp->cinfo.d.mem->max_memory_to_use) &&
2673 getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL)
2674 {
2676 tif, module,
2677 "Reading this image would require libjpeg to allocate "
2678 "at least %" PRIu64 " bytes. "
2679 "This is disabled since above the %ld threshold. "
2680 "You may override this restriction by defining the "
2681 "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, "
2682 "or setting the JPEGMEM environment variable to a value "
2683 "greater "
2684 "or equal to '%" PRIu64 "M'",
2685 nRequiredMemory, sp->cinfo.d.mem->max_memory_to_use,
2686 (nRequiredMemory + 1000000u - 1u) / 1000000u);
2687 return 0;
2688 }
2689 }
2690
2692 {
2693 /* Component 0 should have expected sampling factors */
2694 if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
2695 sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling)
2696 {
2697 TIFFErrorExtR(tif, module,
2698 "Improper JPEG sampling factors %d,%d\n"
2699 "Apparently should be %" PRIu16 ",%" PRIu16 ".",
2700 sp->cinfo.d.comp_info[0].h_samp_factor,
2701 sp->cinfo.d.comp_info[0].v_samp_factor,
2702 sp->h_sampling, sp->v_sampling);
2703 return (0);
2704 }
2705 /* Rest should have sampling factors 1,1 */
2706 for (ci = 1; ci < sp->cinfo.d.num_components; ci++)
2707 {
2708 if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
2709 sp->cinfo.d.comp_info[ci].v_samp_factor != 1)
2710 {
2711 TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
2712 return (0);
2713 }
2714 }
2715 }
2716 else
2717 {
2718 /* PC 2's single component should have sampling factors 1,1 */
2719 if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
2720 sp->cinfo.d.comp_info[0].v_samp_factor != 1)
2721 {
2722 TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
2723 return (0);
2724 }
2725 }
2726 downsampled_output = FALSE;
2730 {
2731 /* Convert YCbCr to RGB */
2732 sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
2733 sp->cinfo.d.out_color_space = JCS_RGB;
2734 }
2735 else
2736 {
2737 /* Suppress colorspace handling */
2738 sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
2739 sp->cinfo.d.out_color_space = JCS_UNKNOWN;
2741 (sp->h_sampling != 1 || sp->v_sampling != 1))
2742 downsampled_output = TRUE;
2743 /* XXX what about up-sampling? */
2744 }
2745 if (downsampled_output)
2746 {
2747 /* Need to use raw-data interface to libjpeg */
2748 sp->cinfo.d.raw_data_out = TRUE;
2749#if JPEG_LIB_VERSION >= 70
2750 sp->cinfo.d.do_fancy_upsampling = FALSE;
2751#endif /* JPEG_LIB_VERSION >= 70 */
2755 }
2756 else
2757 {
2758 /* Use normal interface to libjpeg */
2759 sp->cinfo.d.raw_data_out = FALSE;
2763 }
2764 /* Start JPEG decompressor */
2766 return (0);
2767 /* Allocate downsampled-data buffers if needed */
2768 if (downsampled_output)
2769 {
2770 if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info,
2771 sp->cinfo.d.num_components))
2772 return (0);
2773 sp->scancount = DCTSIZE; /* mark buffer empty */
2774 }
2775 return (1);
2776}
2777
2778/*
2779 * Decode a chunk of pixels.
2780 * "Standard" case: returned data is not downsampled.
2781 */
2782#if !defined(JPEG_LIB_MK1_OR_12BIT)
2807static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
2808{
2809 JPEGState *sp = JState(tif);
2810 tmsize_t nrows;
2811 (void)s;
2812
2813 /*
2814 ** Update available information, buffer may have been refilled
2815 ** between decode requests
2816 */
2817 sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp;
2818 sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
2819
2820 if (sp->bytesperline == 0)
2821 {
2822 memset(buf, 0, (size_t)cc);
2823 return 0;
2824 }
2825
2826 nrows = cc / sp->bytesperline;
2827 if (cc % sp->bytesperline)
2828 TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read");
2829
2830 if (nrows > (tmsize_t)sp->cinfo.d.image_height)
2831 {
2832 /* Initialize the output buffer if the JPEG image is smaller than the
2833 * strile height. */
2834 memset(buf, 0, (size_t)cc);
2835 nrows = sp->cinfo.d.image_height;
2836 }
2837 else if (sp->strile_width > sp->cinfo.d.image_width)
2838 {
2839 /* Initialize the output buffer if the JPEG image is smaller than the
2840 * strile width. */
2841 memset(buf, 0, (size_t)cc);
2842 }
2843
2844 /* data is expected to be read in multiples of a scanline */
2845 if (nrows)
2846 {
2847 do
2848 {
2849 /*
2850 * In the libjpeg6b-9a 8bit case. We read directly into
2851 * the TIFF buffer.
2852 */
2853 JSAMPROW bufptr = (JSAMPROW)buf;
2854
2855 if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1)
2856 {
2857 memset(buf, 0, (size_t)cc);
2858 return (0);
2859 }
2860
2861 ++tif->tif_dir.td_row;
2862 buf += sp->bytesperline;
2863 cc -= sp->bytesperline;
2864 } while (--nrows > 0);
2865 }
2866
2867 /* Update information on consumed data */
2868 tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
2869 tif->tif_rawcc = (tmsize_t)sp->src.bytes_in_buffer;
2870
2871 /* Close down the decompressor if we've finished the strip or tile. */
2872 return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
2874}
2875#endif /* !defined(JPEG_LIB_MK1_OR_12BIT) */
2876
2877#if defined(JPEG_LIB_MK1_OR_12BIT)
2878/*ARGSUSED*/
2911static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
2912{
2913 JPEGState *sp = JState(tif);
2914 tmsize_t nrows;
2915 (void)s;
2916
2917 /*
2918 ** Update available information, buffer may have been refilled
2919 ** between decode requests
2920 */
2921 sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp;
2922 sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
2923
2924 if (sp->bytesperline == 0)
2925 {
2926 memset(buf, 0, (size_t)cc);
2927 return 0;
2928 }
2929
2930 nrows = cc / sp->bytesperline;
2931 if (cc % sp->bytesperline)
2932 TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read");
2933
2934 if (nrows > (tmsize_t)sp->cinfo.d.image_height)
2935 {
2936 /* Initialize the output buffer if the JPEG image is smaller than the
2937 * strile height. */
2938 memset(buf, 0, (size_t)cc);
2939 nrows = sp->cinfo.d.image_height;
2940 }
2941 else if (sp->strile_width > sp->cinfo.d.image_width)
2942 {
2943 /* Initialize the output buffer if the JPEG image is smaller than the
2944 * strile width. */
2945 memset(buf, 0, (size_t)cc);
2946 }
2947
2948 /* data is expected to be read in multiples of a scanline */
2949 if (nrows)
2950 {
2951 TIFF_JSAMPROW line_work_buf = NULL;
2952
2953 /*
2954 * For 6B, only use temporary buffer for 12 bit imagery.
2955 * For Mk1 always use it.
2956 */
2957 if (sp->cinfo.d.data_precision == 12)
2958 {
2959 line_work_buf = (TIFF_JSAMPROW)_TIFFmallocExt(
2960 tif, (tmsize_t)((size_t)sizeof(short) *
2961 (size_t)sp->cinfo.d.output_width *
2962 (size_t)sp->cinfo.d.num_components));
2963 }
2964
2965 do
2966 {
2967 if (line_work_buf != NULL)
2968 {
2969 /*
2970 * In the MK1 case, we always read into a 16bit
2971 * buffer, and then pack down to 12bit or 8bit.
2972 * In 6B case we only read into 16 bit buffer
2973 * for 12bit data, which we need to repack.
2974 */
2975 if (TIFFjpeg_read_scanlines(sp, &line_work_buf, 1) != 1)
2976 {
2977 memset(buf, 0, (size_t)cc);
2978 _TIFFfreeExt(tif, line_work_buf);
2979 return (0);
2980 }
2981
2982 if (sp->cinfo.d.data_precision == 12)
2983 {
2984 tmsize_t value_count =
2985 (tmsize_t)sp->cinfo.d.output_width *
2986 sp->cinfo.d.num_components;
2987 tmsize_t iPair;
2988
2989 for (iPair = 0; iPair < value_count / 2; iPair++)
2990 {
2991 unsigned char *out_ptr =
2992 ((unsigned char *)buf) + iPair * 3;
2993 TIFF_JSAMPLE *in_ptr = line_work_buf + iPair * 2;
2994
2995 out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4);
2996 out_ptr[1] =
2997 (unsigned char)(((in_ptr[0] & 0xf) << 4) |
2998 ((in_ptr[1] & 0xf00) >> 8));
2999 out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0));
3000 }
3001 if ((value_count & 1) != 0)
3002 JPEGWritePacked12Sample((uint8_t *)buf, value_count - 1,
3003 line_work_buf[value_count - 1]);
3004 }
3005 else if (sp->cinfo.d.data_precision == 8)
3006 {
3007 tmsize_t value_count =
3008 (tmsize_t)sp->cinfo.d.output_width *
3009 sp->cinfo.d.num_components;
3010 tmsize_t iValue;
3011
3012 for (iValue = 0; iValue < value_count; iValue++)
3013 {
3014 ((unsigned char *)buf)[iValue] =
3015 (unsigned char)(line_work_buf[iValue] & 0xff);
3016 }
3017 }
3018 }
3019
3020 ++tif->tif_dir.td_row;
3021 buf += sp->bytesperline;
3022 cc -= sp->bytesperline;
3023 } while (--nrows > 0);
3024
3025 if (line_work_buf != NULL)
3026 _TIFFfreeExt(tif, line_work_buf);
3027 }
3028
3029 /* Update information on consumed data */
3030 tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
3031 tif->tif_rawcc = (tmsize_t)sp->src.bytes_in_buffer;
3032
3033 /* Close down the decompressor if we've finished the strip or tile. */
3034 return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
3036}
3037#endif /* defined(JPEG_LIB_MK1_OR_12BIT) */
3038
3039/*ARGSUSED*/
3054static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
3055
3056{
3057 (void)buf;
3058 (void)cc;
3059 (void)s;
3060
3062 tif, "TIFFReadScanline",
3063 "scanline oriented access is not supported for downsampled JPEG "
3064 "compressed images, consider enabling TIFFTAG_JPEGCOLORMODE as "
3065 "JPEGCOLORMODE_RGB.");
3066 return 0;
3067}
3068
3069/*
3070 * Decode a chunk of pixels.
3071 * Returned data is downsampled per sampling factors.
3072 */
3073/*ARGSUSED*/
3104static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
3105{
3106 JPEGState *sp = JState(tif);
3107 tmsize_t nrows;
3108 TIFFDirectory *td = &tif->tif_dir;
3109 (void)s;
3110
3111 nrows = sp->cinfo.d.image_height;
3112 /* For last strip, limit number of rows to its truncated height */
3113 /* even if the codestream height is larger (which is not compliant, */
3114 /* but that we tolerate) */
3115 if ((uint32_t)nrows > td->td_imagelength - tif->tif_dir.td_row &&
3116 !isTiled(tif))
3117 nrows = td->td_imagelength - tif->tif_dir.td_row;
3118
3119 /* The downsampled-data decode loop below only writes up to the JPEG
3120 * codestream dimensions (and, due to the clump stride, may not cover the
3121 * full caller buffer even for compliant files). Zero the whole buffer so
3122 * no uninitialised heap is ever returned, mirroring the guard JPEGDecode
3123 * received in 65759931ab6e (#826). */
3124 memset(buf, 0, (size_t)cc);
3125
3126#if defined(JPEG_LIB_MK1_OR_12BIT)
3127 unsigned short *tmpbuf = NULL;
3128#endif
3129
3130 /* data is expected to be read in multiples of a scanline */
3131 if (nrows != 0)
3132 {
3133
3134 /* Cb,Cr both have sampling factors 1, so this is correct */
3135 JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width;
3136 int samples_per_clump = sp->samplesperclump;
3137 tmsize_t samplesperclumpline;
3138 tmsize_t bytesperclumpline;
3139 uint64_t samplesperclumpline64;
3140
3141 /* The loop below writes tmpbuf in raw clump layout: each clump holds
3142 * samples_per_clump samples and there are clumps_per_line clumps.
3143 * Thus the required number of elements is their product, rather than
3144 * output_width * num_components. The two expressions happen to be
3145 * equal for some subsampling configurations (for example 2x2), but
3146 * are not equivalent in general. */
3147 samplesperclumpline64 =
3148 _TIFFMultiply64(tif, (uint64_t)clumps_per_line,
3149 (uint64_t)samples_per_clump, "JPEGDecodeRaw");
3150 samplesperclumpline =
3151 _TIFFCastUInt64ToSSize(tif, samplesperclumpline64, "JPEGDecodeRaw");
3152 bytesperclumpline = JPEGRawBytesPerClumpLine(
3153 tif, sp->strile_width, sp->h_sampling, sp->v_sampling,
3154 sp->cinfo.d.data_precision, "JPEGDecodeRaw");
3155 if (samplesperclumpline == 0 || bytesperclumpline == 0)
3156 return 0;
3157
3158#if defined(JPEG_LIB_MK1_OR_12BIT)
3159 tmpbuf = (unsigned short *)_TIFFCheckMalloc(
3160 tif, samplesperclumpline, sizeof(unsigned short), "JPEGDecodeRaw");
3161 if (tmpbuf == NULL)
3162 {
3163 TIFFErrorExtR(tif, "JPEGDecodeRaw", "Out of memory");
3164 return 0;
3165 }
3166#endif
3167
3168 do
3169 {
3170 jpeg_component_info *compptr;
3171 int ci, clumpoffset;
3172
3173 if (cc < bytesperclumpline)
3174 {
3176 tif, "JPEGDecodeRaw",
3177 "application buffer not large enough for all data.");
3178 goto error;
3179 }
3180
3181 /* Reload downsampled-data buffer if needed */
3182 if (sp->scancount >= DCTSIZE)
3183 {
3184 int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE;
3185 if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n) != n)
3186 goto error;
3187 sp->scancount = 0;
3188 }
3189 /*
3190 * Fastest way to unseparate data is to make one pass
3191 * over the scanline for each row of each component.
3192 */
3193 clumpoffset = 0; /* first sample in clump */
3194 for (ci = 0, compptr = sp->cinfo.d.comp_info;
3195 ci < sp->cinfo.d.num_components; ci++, compptr++)
3196 {
3197 int hsamp = compptr->h_samp_factor;
3198 int vsamp = compptr->v_samp_factor;
3199 int ypos;
3200
3201 for (ypos = 0; ypos < vsamp; ypos++)
3202 {
3203 TIFF_JSAMPLE *inptr =
3204 sp->ds_buffer[ci][sp->scancount * vsamp + ypos];
3205 JDIMENSION nclump;
3206#if defined(JPEG_LIB_MK1_OR_12BIT)
3207 TIFF_JSAMPLE *outptr = (TIFF_JSAMPLE *)tmpbuf + clumpoffset;
3208#else
3209 uint8_t *outptr = buf + clumpoffset;
3210 if (cc < (tmsize_t)(clumpoffset +
3211 (tmsize_t)samples_per_clump *
3212 (clumps_per_line - 1) +
3213 hsamp))
3214 {
3216 tif, "JPEGDecodeRaw",
3217 "application buffer not large enough for all data, "
3218 "possible subsampling issue");
3219 goto error;
3220 }
3221#endif
3222
3223 if (hsamp == 1)
3224 {
3225 /* fast path for at least Cb and Cr */
3226 for (nclump = clumps_per_line; nclump-- > 0;)
3227 {
3228 outptr[0] = (TIFF_JSAMPLE)*inptr++;
3229 outptr += samples_per_clump;
3230 }
3231 }
3232 else
3233 {
3234 int xpos;
3235
3236 /* general case */
3237 for (nclump = clumps_per_line; nclump-- > 0;)
3238 {
3239 for (xpos = 0; xpos < hsamp; xpos++)
3240 outptr[xpos] = (TIFF_JSAMPLE)*inptr++;
3241 outptr += samples_per_clump;
3242 }
3243 }
3244 clumpoffset += hsamp;
3245 }
3246 }
3247
3248#if defined(JPEG_LIB_MK1_OR_12BIT)
3249 {
3250 if (sp->cinfo.d.data_precision == 8)
3251 {
3252 tmsize_t i = 0;
3253 tmsize_t len = samplesperclumpline;
3254 for (i = 0; i < len; i++)
3255 {
3256 ((unsigned char *)buf)[i] =
3257 (unsigned char)(tmpbuf[i] & 0xff);
3258 }
3259 }
3260 else
3261 { /* 12-bit */
3262 tmsize_t iPair;
3263 for (iPair = 0; iPair < samplesperclumpline / 2; iPair++)
3264 {
3265 unsigned char *out_ptr =
3266 ((unsigned char *)buf) + iPair * 3;
3267 TIFF_JSAMPLE *in_ptr =
3268 (TIFF_JSAMPLE *)(tmpbuf + iPair * 2);
3269
3270 out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4);
3271 out_ptr[1] =
3272 (unsigned char)(((in_ptr[0] & 0xf) << 4) |
3273 ((in_ptr[1] & 0xf00) >> 8));
3274 out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0));
3275 }
3276 if ((samplesperclumpline & 1) != 0)
3278 (uint8_t *)buf, samplesperclumpline - 1,
3279 (TIFF_JSAMPLE)tmpbuf[samplesperclumpline - 1]);
3280 }
3281 }
3282#endif
3283
3284 sp->scancount++;
3285 tif->tif_dir.td_row += sp->v_sampling;
3286
3287 buf += bytesperclumpline;
3288 cc -= bytesperclumpline;
3289
3290 nrows -= sp->v_sampling;
3291 } while (nrows > 0);
3292
3293#if defined(JPEG_LIB_MK1_OR_12BIT)
3294 _TIFFfreeExt(tif, tmpbuf);
3295#endif
3296 }
3297
3298 /* Close down the decompressor if done. */
3299 return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
3301
3302error:
3303#if defined(JPEG_LIB_MK1_OR_12BIT)
3304 _TIFFfreeExt(tif, tmpbuf);
3305#endif
3306 return 0;
3307}
3308
3309/*
3310 * JPEG Encoding.
3311 */
3312
3325static void unsuppress_quant_table(JPEGState *sp, int tblno)
3326{
3327 JQUANT_TBL *qtbl;
3328
3329 if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
3330 qtbl->sent_table = FALSE;
3331}
3332
3345static void suppress_quant_table(JPEGState *sp, int tblno)
3346{
3347 JQUANT_TBL *qtbl;
3348
3349 if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
3350 qtbl->sent_table = TRUE;
3351}
3352
3366static void unsuppress_huff_table(JPEGState *sp, int tblno)
3367{
3368 JHUFF_TBL *htbl;
3369
3370 if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
3371 htbl->sent_table = FALSE;
3372 if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
3373 htbl->sent_table = FALSE;
3374}
3375
3389static void suppress_huff_table(JPEGState *sp, int tblno)
3390{
3391 JHUFF_TBL *htbl;
3392
3393 if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
3394 htbl->sent_table = TRUE;
3395 if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
3396 htbl->sent_table = TRUE;
3397}
3398
3414{
3415 JPEGState *sp = JState(tif);
3416
3417 /* Initialize quant tables for current quality setting */
3419 return (0);
3420 /* Mark only the tables we want for output */
3421 /* NB: chrominance tables are currently used only with YCbCr */
3423 return (0);
3425 {
3427 if (sp->photometric == PHOTOMETRIC_YCBCR)
3429 }
3431 {
3432 unsuppress_huff_table(sp, 0);
3433 if (sp->photometric == PHOTOMETRIC_YCBCR)
3434 unsuppress_huff_table(sp, 1);
3435 }
3436 /* Direct libjpeg output into otherSettings.jpegtables */
3437 if (!TIFFjpeg_tables_dest(sp, tif))
3438 return (0);
3439 /* Emit tables-only datastream */
3440 if (!TIFFjpeg_write_tables(sp))
3441 return (0);
3442
3443 return (1);
3444}
3445
3446#if defined(JPEG_LIB_VERSION_MAJOR) && \
3447 (JPEG_LIB_VERSION_MAJOR > 9 || \
3448 (JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4))
3449/* This is a modified version of std_huff_tables() from jcparam.c
3450 * in libjpeg-9d because it no longer initializes default Huffman
3451 * tables in jpeg_set_defaults(). */
3467static void TIFF_std_huff_tables(j_compress_ptr cinfo)
3468{
3469
3470 if (cinfo->dc_huff_tbl_ptrs[0] == NULL)
3471 {
3472 (void)jpeg_std_huff_table((j_common_ptr)cinfo, TRUE, 0);
3473 }
3474 if (cinfo->ac_huff_tbl_ptrs[0] == NULL)
3475 {
3476 (void)jpeg_std_huff_table((j_common_ptr)cinfo, FALSE, 0);
3477 }
3478 if (cinfo->dc_huff_tbl_ptrs[1] == NULL)
3479 {
3480 (void)jpeg_std_huff_table((j_common_ptr)cinfo, TRUE, 1);
3481 }
3482 if (cinfo->ac_huff_tbl_ptrs[1] == NULL)
3483 {
3484 (void)jpeg_std_huff_table((j_common_ptr)cinfo, FALSE, 1);
3485 }
3486}
3487#endif
3488
3509{
3510 JPEGState *sp = JState(tif);
3511 TIFFDirectory *td = &tif->tif_dir;
3512 static const char module[] = "JPEGSetupEncode";
3513
3514#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
3515 if (tif->tif_dir.td_bitspersample == 12)
3516 {
3517 /* We pass a pointer to a copy of otherSettings, since */
3518 /* TIFFReInitJPEG_12() will clear sp */
3519 JPEGOtherSettings savedOtherSettings = sp->otherSettings;
3520 return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 1);
3521 }
3522#endif
3523
3525 {
3526 TIFFErrorExtR(tif, module, "Failed to initialize libjpeg compressor");
3527 return (0);
3528 }
3529
3530 assert(sp != NULL);
3531 assert(!sp->cinfo.comm.is_decompressor);
3532
3533 sp->photometric = td->td_photometric;
3534
3535 /*
3536 * Initialize all JPEG parameters to default values.
3537 * Note that jpeg_set_defaults needs legal values for
3538 * in_color_space and input_components.
3539 */
3541 {
3542 sp->cinfo.c.input_components = td->td_samplesperpixel;
3543 if (sp->photometric == PHOTOMETRIC_YCBCR)
3544 {
3546 {
3547 sp->cinfo.c.in_color_space = JCS_RGB;
3548 }
3549 else
3550 {
3551 sp->cinfo.c.in_color_space = JCS_YCbCr;
3552 }
3553 }
3554 else
3555 {
3558 td->td_samplesperpixel == 1)
3559 sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
3560 else if (td->td_photometric == PHOTOMETRIC_RGB &&
3561 td->td_samplesperpixel == 3)
3562 sp->cinfo.c.in_color_space = JCS_RGB;
3563 else if (td->td_photometric == PHOTOMETRIC_SEPARATED &&
3564 td->td_samplesperpixel == 4)
3565 sp->cinfo.c.in_color_space = JCS_CMYK;
3566 else
3567 sp->cinfo.c.in_color_space = JCS_UNKNOWN;
3568 }
3569 }
3570 else
3571 {
3572 sp->cinfo.c.input_components = 1;
3573 sp->cinfo.c.in_color_space = JCS_UNKNOWN;
3574 }
3575 if (!TIFFjpeg_set_defaults(sp))
3576 return (0);
3577
3578 /* mozjpeg by default enables progressive JPEG, which is illegal in
3579 * JPEG-in-TIFF */
3580 /* So explicitly disable it. */
3581 if (sp->cinfo.c.num_scans != 0 &&
3583 {
3584 /* it has been found that mozjpeg could create corrupt strips/tiles */
3585 /* in non optimize_coding mode. */
3587 tif, module,
3588 "mozjpeg library likely detected. Disable emission of "
3589 "Huffman tables in JpegTables tag, and use optimize_coding "
3590 "to avoid potential issues");
3591 sp->otherSettings.jpegtablesmode &= ~JPEGTABLESMODE_HUFF;
3592 }
3593 sp->cinfo.c.num_scans = 0;
3594 sp->cinfo.c.scan_info = NULL;
3595
3596 /* Set per-file parameters */
3597 switch (sp->photometric)
3598 {
3599 case PHOTOMETRIC_YCBCR:
3600 sp->h_sampling = td->td_ycbcrsubsampling[0];
3601 sp->v_sampling = td->td_ycbcrsubsampling[1];
3602 if (sp->h_sampling == 0 || sp->v_sampling == 0)
3603 {
3604 TIFFErrorExtR(tif, module,
3605 "Invalig horizontal/vertical sampling value");
3606 return (0);
3607 }
3608 if (td->td_bitspersample > 16)
3609 {
3610 TIFFErrorExtR(tif, module,
3611 "BitsPerSample %" PRIu16 " not allowed for JPEG",
3612 td->td_bitspersample);
3613 return (0);
3614 }
3615
3616 /*
3617 * A ReferenceBlackWhite field *must* be present since the
3618 * default value is inappropriate for YCbCr. Fill in the
3619 * proper value if application didn't set it.
3620 */
3621 {
3622 float *ref;
3624 {
3625 float refbw[6];
3626 uint32_t top = 1U << td->td_bitspersample;
3627 refbw[0] = 0;
3628 refbw[1] = (float)(top - 1);
3629 refbw[2] = (float)(top >> 1);
3630 refbw[3] = refbw[1];
3631 refbw[4] = refbw[2];
3632 refbw[5] = refbw[1];
3634 }
3635 }
3636 break;
3637 case PHOTOMETRIC_PALETTE: /* disallowed by Tech Note */
3638 case PHOTOMETRIC_MASK:
3639 TIFFErrorExtR(tif, module,
3640 "PhotometricInterpretation %" PRIu16
3641 " not allowed for JPEG",
3642 sp->photometric);
3643 return (0);
3644 default:
3645 /* TIFF 6.0 forbids subsampling of all other color spaces */
3646 sp->h_sampling = 1;
3647 sp->v_sampling = 1;
3648 break;
3649 }
3650
3651 /* Verify miscellaneous parameters */
3652
3653 /*
3654 * This would need work if libtiff ever supports different
3655 * depths for different components, or if libjpeg ever supports
3656 * run-time selection of depth. Neither is imminent.
3657 */
3658#ifdef JPEG_LIB_MK1
3659 /* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */
3660 if (td->td_bitspersample != 8 && td->td_bitspersample != 12)
3661#else
3662 if (td->td_bitspersample != BITS_IN_JSAMPLE)
3663#endif
3664 {
3665 TIFFErrorExtR(tif, module,
3666 "BitsPerSample %" PRIu16 " not allowed for JPEG",
3667 td->td_bitspersample);
3668 return (0);
3669 }
3670 sp->cinfo.c.data_precision = td->td_bitspersample;
3671#ifdef JPEG_LIB_MK1
3672 sp->cinfo.c.bits_in_jsample = td->td_bitspersample;
3673#endif
3674 if (isTiled(tif))
3675 {
3676 if ((td->td_tilelength % ((uint32_t)sp->v_sampling * DCTSIZE)) != 0)
3677 {
3678 TIFFErrorExtR(tif, module,
3679 "JPEG tile height must be multiple of %" PRIu32,
3680 (uint32_t)sp->v_sampling * DCTSIZE);
3681 return (0);
3682 }
3683 if ((td->td_tilewidth % ((uint32_t)sp->h_sampling * DCTSIZE)) != 0)
3684 {
3685 TIFFErrorExtR(tif, module,
3686 "JPEG tile width must be multiple of %" PRIu32,
3687 (uint32_t)sp->h_sampling * DCTSIZE);
3688 return (0);
3689 }
3690 }
3691 else
3692 {
3693 if (td->td_rowsperstrip < td->td_imagelength &&
3694 (td->td_rowsperstrip % ((uint32_t)sp->v_sampling * DCTSIZE)) != 0)
3695 {
3696 TIFFErrorExtR(tif, module,
3697 "RowsPerStrip must be multiple of %" PRIu32
3698 " for JPEG",
3699 (uint32_t)sp->v_sampling * DCTSIZE);
3700 return (0);
3701 }
3702 }
3703
3704 /* Create a JPEGTables field if appropriate */
3707 {
3708 if (sp->otherSettings.jpegtables == NULL ||
3709 memcmp(sp->otherSettings.jpegtables, "\0\0\0\0\0\0\0\0\0", 8) == 0)
3710 {
3711#if defined(JPEG_LIB_VERSION_MAJOR) && \
3712 (JPEG_LIB_VERSION_MAJOR > 9 || \
3713 (JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4))
3715 (sp->cinfo.c.dc_huff_tbl_ptrs[0] == NULL ||
3716 sp->cinfo.c.dc_huff_tbl_ptrs[1] == NULL ||
3717 sp->cinfo.c.ac_huff_tbl_ptrs[0] == NULL ||
3718 sp->cinfo.c.ac_huff_tbl_ptrs[1] == NULL))
3719 {
3720 /* libjpeg-9d no longer initializes default Huffman tables in */
3721 /* jpeg_set_defaults() */
3723 }
3724#endif
3725
3726 if (!prepare_JPEGTables(tif))
3727 return (0);
3728 /* Mark the field present */
3729 /* Can't use TIFFSetField since BEENWRITING is already set! */
3732 }
3733 }
3734 else
3735 {
3736 /* We do not support application-supplied JPEGTables, */
3737 /* so mark the field not present */
3739 }
3740
3741 /* Direct libjpeg output to libtiff's output buffer */
3743
3744 return (1);
3745}
3746
3747/*
3748 * Set encoding state at the start of a strip or tile.
3749 */
3770static int JPEGPreEncode(TIFF *tif, uint16_t s)
3771{
3772 JPEGState *sp = JState(tif);
3773 TIFFDirectory *td = &tif->tif_dir;
3774 static const char module[] = "JPEGPreEncode";
3775 int downsampled_input;
3776
3777 assert(sp != NULL);
3778
3779 if (sp->cinfo.comm.is_decompressor == 1)
3780 {
3782 }
3783
3784 assert(!sp->cinfo.comm.is_decompressor);
3785
3787 return 0;
3788
3789 if (sp->strile_width > (uint32_t)JPEG_MAX_DIMENSION ||
3790 sp->strile_height > (uint32_t)JPEG_MAX_DIMENSION)
3791 {
3792 TIFFErrorExtR(tif, module,
3793 "Strip/tile too large for JPEG. Maximum dimension is %d",
3794 (int)JPEG_MAX_DIMENSION);
3795 return (0);
3796 }
3797 sp->cinfo.c.image_width = sp->strile_width;
3798 sp->cinfo.c.image_height = sp->strile_height;
3799 downsampled_input = FALSE;
3801 {
3802 sp->cinfo.c.input_components = td->td_samplesperpixel;
3803 if (sp->photometric == PHOTOMETRIC_YCBCR)
3804 {
3806 {
3807 if (sp->h_sampling != 1 || sp->v_sampling != 1)
3808 downsampled_input = TRUE;
3809 }
3810 if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr))
3811 return (0);
3812 /*
3813 * Set Y sampling factors;
3814 * we assume jpeg_set_colorspace() set the rest to 1
3815 */
3816 sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
3817 sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
3818 }
3819 else
3820 {
3821 if (!TIFFjpeg_set_colorspace(sp, sp->cinfo.c.in_color_space))
3822 return (0);
3823 /* jpeg_set_colorspace set all sampling factors to 1 */
3824 }
3825 }
3826 else
3827 {
3828 if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
3829 return (0);
3830 sp->cinfo.c.comp_info[0].component_id = s;
3831 /* jpeg_set_colorspace() set sampling factors to 1 */
3832 if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0)
3833 {
3834 sp->cinfo.c.comp_info[0].quant_tbl_no = 1;
3835 sp->cinfo.c.comp_info[0].dc_tbl_no = 1;
3836 sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
3837 }
3838 }
3839 /* ensure libjpeg won't write any extraneous markers */
3840 sp->cinfo.c.write_JFIF_header = FALSE;
3841 sp->cinfo.c.write_Adobe_marker = FALSE;
3842 /* set up table handling correctly */
3843 /* calling TIFFjpeg_set_quality() causes quantization tables to be flagged
3844 */
3845 /* as being to be emitted, which we don't want in the JPEGTABLESMODE_QUANT
3846 */
3847 /* mode, so we must manually suppress them. However TIFFjpeg_set_quality()
3848 */
3849 /* should really be called when dealing with files with directories with */
3850 /* mixed qualities. see http://trac.osgeo.org/gdal/ticket/3539 */
3852 return (0);
3854 {
3855 suppress_quant_table(sp, 0);
3856 suppress_quant_table(sp, 1);
3857 }
3858 else
3859 {
3862 }
3864 {
3865 /* Explicit suppression is only needed if we did not go through the */
3866 /* prepare_JPEGTables() code path, which may be the case if updating */
3867 /* an existing file */
3868 suppress_huff_table(sp, 0);
3869 suppress_huff_table(sp, 1);
3870
3871 /* We want to keep optimize_coding = TRUE for 12-bit JPEG */
3872 /* See lengthy explanation at
3873 * https://gitlab.com/libtiff/libtiff/-/work_items/773#note_3009836854
3874 */
3875 if (sp->cinfo.c.data_precision == 8)
3876 sp->cinfo.c.optimize_coding = FALSE;
3877 }
3878 else
3879 sp->cinfo.c.optimize_coding = TRUE;
3880 if (downsampled_input)
3881 {
3882 /* Need to use raw-data interface to libjpeg */
3883 sp->cinfo.c.raw_data_in = TRUE;
3887 }
3888 else
3889 {
3890 /* Use normal interface to libjpeg */
3891 sp->cinfo.c.raw_data_in = FALSE;
3895 }
3896 /* Start JPEG compressor */
3898 return (0);
3899 /* Allocate downsampled-data buffers if needed */
3900 if (downsampled_input)
3901 {
3902 if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info,
3903 sp->cinfo.c.num_components))
3904 return (0);
3905 }
3906 sp->scancount = 0;
3907 sp->encode_raw_error = FALSE;
3908
3909 return (1);
3910}
3911
3912/*
3913 * Encode a chunk of pixels.
3914 * "Standard" case: incoming data is not downsampled.
3915 */
3938static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
3939{
3940 JPEGState *sp = JState(tif);
3941 tmsize_t nrows;
3942 TIFF_JSAMPROW bufptr[1];
3943 short *line16 = NULL;
3944 int line16_count = 0;
3945
3946 (void)s;
3947 assert(sp != NULL);
3948 /* data is expected to be supplied in multiples of a scanline */
3949 nrows = cc / sp->bytesperline;
3950 if (cc % sp->bytesperline)
3951 TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded");
3952
3953 /* The last strip will be limited to image size */
3954 if (!isTiled(tif) &&
3957
3958 if (sp->cinfo.c.data_precision == 12)
3959 {
3960 line16_count = (int)((sp->bytesperline * 2) / 3);
3961 line16 = (short *)_TIFFmallocExt(
3962 tif, (tmsize_t)(sizeof(short) * (size_t)line16_count));
3963 if (!line16)
3964 {
3965 TIFFErrorExtR(tif, "JPEGEncode", "Failed to allocate memory");
3966
3967 return 0;
3968 }
3969 }
3970
3971 while (nrows-- > 0)
3972 {
3973
3974 if (sp->cinfo.c.data_precision == 12)
3975 {
3976
3977 int value_pairs = line16_count / 2;
3978 int iPair;
3979
3980 bufptr[0] = (TIFF_JSAMPROW)line16;
3981
3982 for (iPair = 0; iPair < value_pairs; iPair++)
3983 {
3984 unsigned char *in_ptr = ((unsigned char *)buf) + iPair * 3;
3985 TIFF_JSAMPLE *out_ptr = (TIFF_JSAMPLE *)(line16 + iPair * 2);
3986
3987 out_ptr[0] = (TIFF_JSAMPLE)((in_ptr[0] << 4) |
3988 ((in_ptr[1] & 0xf0) >> 4));
3989 out_ptr[1] =
3990 (TIFF_JSAMPLE)(((in_ptr[1] & 0x0f) << 8) | in_ptr[2]);
3991 }
3992 if ((line16_count & 1) != 0)
3993 {
3994 line16[line16_count - 1] = (short)JPEGReadPacked12Sample(
3995 (const uint8_t *)buf, line16_count - 1);
3996 }
3997 }
3998 else
3999 {
4000 bufptr[0] = (TIFF_JSAMPROW)buf;
4001 }
4002 if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
4003 return (0);
4004 if (nrows > 0)
4005 tif->tif_dir.td_row++;
4006 buf += sp->bytesperline;
4007 }
4008
4009 if (sp->cinfo.c.data_precision == 12)
4010 {
4011 _TIFFfreeExt(tif, line16);
4012 }
4013
4014 return (1);
4015}
4016
4017/*
4018 * Encode a chunk of pixels.
4019 * Incoming data is expected to be downsampled per sampling factors.
4020 */
4043static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
4044{
4045 JPEGState *sp = JState(tif);
4046 TIFF_JSAMPLE *outptr;
4047 tmsize_t nrows;
4048 JDIMENSION clumps_per_line, nclump;
4049 int clumpoffset, ci, xpos, ypos;
4050 jpeg_component_info *compptr;
4051 int samples_per_clump = sp->samplesperclump;
4052 tmsize_t bytesperclumpline;
4053
4054 (void)s;
4055 assert(sp != NULL);
4056
4057 if (sp->encode_raw_error)
4058 {
4059 TIFFErrorExtR(tif, tif->tif_name, "JPEGEncodeRaw() already failed");
4060 return 0;
4061 }
4062
4063 /* data is expected to be supplied in multiples of a clumpline */
4064 /* a clumpline is equivalent to v_sampling desubsampled scanlines */
4065 /* TODO: the following calculation of bytesperclumpline, should substitute
4066 * calculation of sp->bytesperline, except that it is per v_sampling lines
4067 */
4068 bytesperclumpline = JPEGRawBytesPerClumpLine(
4069 tif, sp->cinfo.c.image_width, sp->h_sampling, sp->v_sampling,
4070 sp->cinfo.c.data_precision, "JPEGEncodeRaw");
4071 if (bytesperclumpline == 0)
4072 return 0;
4073
4074 nrows = (cc / bytesperclumpline) * sp->v_sampling;
4075 if (cc % bytesperclumpline)
4076 TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded");
4077
4078 /* Cb,Cr both have sampling factors 1, so this is correct */
4079 clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width;
4080
4081 while (nrows > 0)
4082 {
4083 /*
4084 * Fastest way to separate the data is to make one pass
4085 * over the scanline for each row of each component.
4086 */
4087 clumpoffset = 0; /* first sample in clump */
4088 for (ci = 0, compptr = sp->cinfo.c.comp_info;
4089 ci < sp->cinfo.c.num_components; ci++, compptr++)
4090 {
4091 int hsamp = compptr->h_samp_factor;
4092 int vsamp = compptr->v_samp_factor;
4093 int padding =
4094 (int)(compptr->width_in_blocks * DCTSIZE -
4095 (JDIMENSION)clumps_per_line * (JDIMENSION)hsamp);
4096 for (ypos = 0; ypos < vsamp; ypos++)
4097 {
4098 outptr = sp->ds_buffer[ci][sp->scancount * vsamp + ypos];
4099 if (sp->cinfo.c.data_precision == 12)
4100 {
4101 tmsize_t sample_index = clumpoffset;
4102 if (hsamp == 1)
4103 {
4104 for (nclump = clumps_per_line; nclump-- > 0;)
4105 {
4106 *outptr++ =
4107 JPEGReadPacked12Sample(buf, sample_index);
4108 sample_index += samples_per_clump;
4109 }
4110 }
4111 else
4112 {
4113 for (nclump = clumps_per_line; nclump-- > 0;)
4114 {
4115 for (xpos = 0; xpos < hsamp; xpos++)
4116 *outptr++ = JPEGReadPacked12Sample(
4117 buf, sample_index + xpos);
4118 sample_index += samples_per_clump;
4119 }
4120 }
4121 }
4122 else
4123 {
4124 const uint8_t *inptr8 = buf + clumpoffset;
4125 if (hsamp == 1)
4126 {
4127 /* fast path for at least Cb and Cr */
4128 for (nclump = clumps_per_line; nclump-- > 0;)
4129 {
4130 *outptr++ = (TIFF_JSAMPLE)inptr8[0];
4131 inptr8 += samples_per_clump;
4132 }
4133 }
4134 else
4135 {
4136 /* general case */
4137 for (nclump = clumps_per_line; nclump-- > 0;)
4138 {
4139 for (xpos = 0; xpos < hsamp; xpos++)
4140 *outptr++ = (TIFF_JSAMPLE)inptr8[xpos];
4141 inptr8 += samples_per_clump;
4142 }
4143 }
4144 }
4145 /* pad each scanline as needed */
4146 for (xpos = 0; xpos < padding; xpos++)
4147 {
4148 *outptr = outptr[-1];
4149 outptr++;
4150 }
4151 clumpoffset += hsamp;
4152 }
4153 }
4154 sp->scancount++;
4155 if (sp->scancount >= DCTSIZE)
4156 {
4157 int n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
4158 if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
4159 {
4160 sp->encode_raw_error = TRUE;
4161 return (0);
4162 }
4163 sp->scancount = 0;
4164 }
4165 tif->tif_dir.td_row += sp->v_sampling;
4166 buf += bytesperclumpline;
4167 nrows -= sp->v_sampling;
4168 }
4169 return (1);
4170}
4171
4172/*
4173 * Finish up at the end of a strip or tile.
4174 */
4193{
4194 JPEGState *sp = JState(tif);
4195
4196 if (sp->scancount > 0)
4197 {
4198 /*
4199 * Need to emit a partial bufferload of downsampled data.
4200 * Pad the data vertically.
4201 */
4202 int ci, ypos, n;
4203 jpeg_component_info *compptr;
4204
4205 for (ci = 0, compptr = sp->cinfo.c.comp_info;
4206 ci < sp->cinfo.c.num_components; ci++, compptr++)
4207 {
4208 int vsamp = compptr->v_samp_factor;
4209 tmsize_t row_width = (tmsize_t)compptr->width_in_blocks * DCTSIZE *
4210 (tmsize_t)sizeof(TIFF_JSAMPLE);
4211 for (ypos = sp->scancount * vsamp; ypos < DCTSIZE * vsamp; ypos++)
4212 {
4213 _TIFFmemcpy((void *)sp->ds_buffer[ci][ypos],
4214 (void *)sp->ds_buffer[ci][ypos - 1], row_width);
4215 }
4216 }
4217 n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
4218 if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
4219 return (0);
4220 }
4221
4223}
4224
4238static void JPEGCleanup(TIFF *tif)
4239{
4240 JPEGState *sp = JState(tif);
4241
4242 assert(sp != 0);
4243
4247 if (sp->cinfo_initialized)
4248 TIFFjpeg_destroy(sp); /* release libjpeg resources */
4249 if (sp->otherSettings.jpegtables) /* tag value */
4251 _TIFFfreeExt(tif, tif->tif_data); /* release local state */
4252 tif->tif_data = NULL;
4253
4255}
4256
4271{
4272 JPEGState *sp = JState(tif);
4273 TIFFDirectory *td = &tif->tif_dir;
4274
4275 /*
4276 * Mark whether returned data is up-sampled or not so TIFFStripSize
4277 * and TIFFTileSize return values that reflect the true amount of
4278 * data.
4279 */
4280 tif->tif_flags &= ~TIFF_UPSAMPLED;
4282 {
4283 if (td->td_photometric == PHOTOMETRIC_YCBCR &&
4285 {
4287 }
4288 else
4289 {
4290#ifdef notdef
4291 if (td->td_ycbcrsubsampling[0] != 1 ||
4292 td->td_ycbcrsubsampling[1] != 1)
4293 ; /* XXX what about up-sampling? */
4294#endif
4295 }
4296 }
4297
4298 /*
4299 * Must recalculate cached tile size in case sampling state changed.
4300 * Should we really be doing this now if image size isn't set?
4301 */
4302 if (tif->tif_dir.td_tilesize > 0)
4304 isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
4305 if (tif->tif_dir.td_scanlinesize > 0)
4307}
4308
4334static int JPEGVSetField(TIFF *tif, uint32_t tag, va_list ap)
4335{
4336 JPEGState *sp = JState(tif);
4337 const TIFFField *fip;
4338 uint32_t v32;
4339
4340 assert(sp != NULL);
4341
4342 switch (tag)
4343 {
4344 case TIFFTAG_JPEGTABLES:
4345 v32 = (uint32_t)va_arg(ap, uint32_t);
4346 if (v32 == 0)
4347 {
4348 /* XXX */
4349 return (0);
4350 }
4352 va_arg(ap, void *), v32);
4355 break;
4357 sp->otherSettings.jpegquality = (int)va_arg(ap, int);
4358 return (1); /* pseudo tag */
4360 sp->otherSettings.jpegcolormode = (int)va_arg(ap, int);
4362 return (1); /* pseudo tag */
4364 {
4365 int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap);
4367 return ret_value;
4368 }
4370 sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int);
4371 return (1); /* pseudo tag */
4373 /* mark the fact that we have a real ycbcrsubsampling! */
4375 /* should we be recomputing upsampling info here? */
4376 return (*sp->otherSettings.vsetparent)(tif, tag, ap);
4377 default:
4378 return (*sp->otherSettings.vsetparent)(tif, tag, ap);
4379 }
4380
4381 if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
4382 {
4384 }
4385 else
4386 {
4387 return (0);
4388 }
4389
4391 return (1);
4392}
4393
4412static int JPEGVGetField(TIFF *tif, uint32_t tag, va_list ap)
4413{
4414 JPEGState *sp = JState(tif);
4415
4416 assert(sp != NULL);
4417
4418 switch (tag)
4419 {
4420 case TIFFTAG_JPEGTABLES:
4421 *va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length;
4422 *va_arg(ap, const void **) = sp->otherSettings.jpegtables;
4423 break;
4425 *va_arg(ap, int *) = sp->otherSettings.jpegquality;
4426 break;
4428 *va_arg(ap, int *) = sp->otherSettings.jpegcolormode;
4429 break;
4431 *va_arg(ap, int *) = sp->otherSettings.jpegtablesmode;
4432 break;
4433 default:
4434 return (*sp->otherSettings.vgetparent)(tif, tag, ap);
4435 }
4436 return (1);
4437}
4438
4453static void JPEGPrintDir(TIFF *tif, FILE *fd, long flags)
4454{
4455 JPEGState *sp = JState(tif);
4456
4457 assert(sp != NULL);
4458 (void)flags;
4459
4460 if (sp != NULL)
4461 {
4463 fprintf(fd, " JPEG Tables: (%" PRIu32 " bytes)\n",
4465 if (sp->otherSettings.printdir)
4466 (*sp->otherSettings.printdir)(tif, fd, flags);
4467 }
4468}
4469
4490static uint32_t JPEGDefaultStripSize(TIFF *tif, uint32_t s)
4491{
4492 JPEGState *sp = JState(tif);
4493 TIFFDirectory *td = &tif->tif_dir;
4494
4495 s = (*sp->otherSettings.defsparent)(tif, s);
4496 if (s < td->td_imagelength)
4497 s = TIFFroundup_32(s, td->td_ycbcrsubsampling[1] * DCTSIZE);
4498 return (s);
4499}
4500
4516static void JPEGDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
4517{
4518 JPEGState *sp = JState(tif);
4519 TIFFDirectory *td = &tif->tif_dir;
4520
4521 (*sp->otherSettings.deftparent)(tif, tw, th);
4522 *tw = TIFFroundup_32(*tw, td->td_ycbcrsubsampling[0] * DCTSIZE);
4523 *th = TIFFroundup_32(*th, td->td_ycbcrsubsampling[1] * DCTSIZE);
4524}
4525
4526/*
4527 * The JPEG library initialized used to be done in TIFFInitJPEG(), but
4528 * now that we allow a TIFF file to be opened in update mode it is necessary
4529 * to have some way of deciding whether compression or decompression is
4530 * desired other than looking at tif->tif_mode. We accomplish this by
4531 * examining {TILE/STRIP}BYTECOUNTS to see if there is a non-zero entry.
4532 * If so, we assume decompression is desired.
4533 *
4534 * This is tricky, because TIFFInitJPEG() is called while the directory is
4535 * being read, and generally speaking the BYTECOUNTS tag won't have been read
4536 * at that point. So we try to defer jpeg library initialization till we
4537 * do have that tag ... basically any access that might require the compressor
4538 * or decompressor that occurs after the reading of the directory.
4539 *
4540 * In an ideal world compressors or decompressors would be setup
4541 * at the point where a single tile or strip was accessed (for read or write)
4542 * so that stuff like update of missing tiles, or replacement of tiles could
4543 * be done. However, we aren't trying to crack that nut just yet ...
4544 *
4545 * NFW, Feb 3rd, 2003.
4546 */
4547
4566static int JPEGInitializeLibJPEG(TIFF *tif, int decompress)
4567{
4568 JPEGState *sp = JState(tif);
4569
4570 if (sp->cinfo_initialized)
4571 {
4572 if (!decompress && sp->cinfo.comm.is_decompressor)
4573 TIFFjpeg_destroy(sp);
4574 else if (decompress && !sp->cinfo.comm.is_decompressor)
4575 TIFFjpeg_destroy(sp);
4576 else
4577 return 1;
4578
4579 sp->cinfo_initialized = 0;
4580 }
4581
4582 /*
4583 * Initialize libjpeg.
4584 */
4585 if (decompress)
4586 {
4588 return (0);
4589 }
4590 else
4591 {
4592 if (!TIFFjpeg_create_compress(sp))
4593 return (0);
4594#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE
4595#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024)
4596#endif
4597 /* libjpeg turbo 1.5.2 honours max_memory_to_use, but has no backing */
4598 /* store implementation, so better not set max_memory_to_use ourselves.
4599 */
4600 /* See https://github.com/libjpeg-turbo/libjpeg-turbo/issues/162 */
4601 if (sp->cinfo.c.mem->max_memory_to_use > 0)
4602 {
4603 /* This is to address bug related in ticket GDAL #1795. */
4604 if (getenv("JPEGMEM") == NULL)
4605 {
4606 /* Increase the max memory usable. This helps when creating
4607 * files */
4608 /* with "big" tile, without using libjpeg temporary files. */
4609 /* For example a 512x512 tile with 3 bands */
4610 /* requires 1.5 MB which is above libjpeg 1MB default */
4611 if (sp->cinfo.c.mem->max_memory_to_use <
4613 sp->cinfo.c.mem->max_memory_to_use =
4615 }
4616 }
4617 }
4618
4619 sp->cinfo_initialized = TRUE;
4620
4621 return 1;
4622}
4623
4639{
4640 /* See README_for_libtiff_developpers.md for raw data used to estimate
4641 * the maximum compression rate. */
4642
4643 const JPEGState *sp = JState(tif);
4647 {
4648 if (sp->h_sampling == 2 && sp->v_sampling == 2)
4649 {
4650 if (tif->tif_dir.td_bitspersample == 12)
4651 return 768;
4652 else
4653 return 512;
4654 }
4655
4656 return 0; /* unknown */
4657 }
4658
4659 if (tif->tif_dir.td_bitspersample == 12)
4660 return 384;
4661 else
4662 return 256;
4663}
4664
4665/* Common to tif_jpeg.c and tif_jpeg_12.c */
4681{
4682 JPEGState *sp;
4683
4684 sp = JState(tif);
4685 sp->tif = tif; /* back link */
4686
4687 /* Default values for codec-specific fields */
4688 sp->otherSettings.jpegtables = NULL;
4690 sp->otherSettings.jpegquality = 75; /* Default IJG quality */
4695
4696 tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
4697 tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
4698 tif->tif_tagmethods.printdir = JPEGPrintDir; /* hook for codec tags */
4699
4700 /*
4701 * Install codec methods.
4702 */
4717
4720 tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */
4722}
4723
4724int TIFFInitJPEG(TIFF *tif, int scheme)
4725{
4726 JPEGState *sp;
4727
4728 (void)scheme;
4729 assert(scheme == COMPRESSION_JPEG);
4730
4731 /*
4732 * Merge codec-specific tag information.
4733 */
4735 {
4736 TIFFErrorExtR(tif, "TIFFInitJPEG",
4737 "Merging JPEG codec-specific tags failed");
4738 return 0;
4739 }
4740
4741 /*
4742 * Allocate state block so tag methods have storage to record values.
4743 */
4744 tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(JPEGState));
4745
4746 if (tif->tif_data == NULL)
4747 {
4748 TIFFErrorExtR(tif, "TIFFInitJPEG", "No space for JPEG state block");
4749 return 0;
4750 }
4751 _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState));
4752
4753 sp = JState(tif);
4754 /*
4755 * Override parent get/set field methods.
4756 */
4760
4763
4765
4766 /*
4767 ** Create a JPEGTables field if no directory has yet been created.
4768 ** We do this just to ensure that sufficient space is reserved for
4769 ** the JPEGTables field. It will be properly created the right
4770 ** size later.
4771 */
4772 if (tif->tif_diroff == 0)
4773 {
4774#define SIZE_OF_JPEGTABLES 2000
4775 /*
4776 The following line assumes incorrectly that all JPEG-in-TIFF files will
4777 have a JPEGTABLES tag generated and causes null-filled JPEGTABLES tags
4778 to be written when the JPEG data is placed with TIFFWriteRawStrip. The
4779 field bit should be set, anyway, later when actual JPEGTABLES header is
4780 generated, so removing it here hopefully is harmless.
4781 TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
4782 */
4786 if (sp->otherSettings.jpegtables)
4787 {
4789 }
4790 else
4791 {
4792 TIFFErrorExtR(tif, "TIFFInitJPEG",
4793 "Failed to allocate memory for JPEG tables");
4794 return 0;
4795 }
4796#undef SIZE_OF_JPEGTABLES
4797 }
4798 return 1;
4799}
4800#endif /* JPEG_SUPPORT */
4801
uint32_t _TIFFParseUInt32(const char *str, uint32_t default_value, uint32_t max_value)
Parse a decimal unsigned integer with a fallback value.
Definition tif_aux.c:58
uint64_t _TIFFAdd64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Check and add two unsigned operands.
Definition tif_aux.c:85
tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module)
Convert an unsigned 64-bit value after checking the destination range.
Definition tif_aux.c:140
uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Multiply two unsigned values with an overflow check.
Definition tif_aux.c:46
void _TIFFSetDefaultCompressionState(TIFF *tif)
Install fallback codec callbacks and reset raw-access flags.
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
Merge borrowed field definitions into the lookup table.
#define TIFFFieldSet(tif, field)
Test a directory presence bit.
Definition tif_dir.h:662
#define FIELD_PSEUDO
Share bit zero with FIELD_IGNORE for pseudo-tags.
Definition tif_dir.h:620
#define TIFFClrFieldBit(tif, field)
Clear a directory presence bit.
Definition tif_dir.h:688
#define TIFFSetFieldBit(tif, field)
Set a directory presence bit.
Definition tif_dir.h:675
@ TIFF_SETGET_INT
Promoted int scalar representation.
Definition tif_dir.h:762
@ TIFF_SETGET_C32_UINT8
Unsigned 8-bit array with 32-bit explicit count.
Definition tif_dir.h:816
void(* TIFFTileMethod)(TIFF *tif, uint32_t *tw, uint32_t *th)
Adjust tile width and height for the current codec.
Definition tiffiop.h:315
uint32_t(* TIFFStripMethod)(TIFF *tif, uint32_t request)
Recommend rows per strip for the current geometry and codec.
Definition tiffiop.h:297
void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
Leave decoded bytes unchanged.
Definition tif_read.c:2209
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
Definition tif_write.c:1213
int TIFFFillTile(TIFF *tif, uint32_t tile)
Load raw tile bytes and initialise codec decoding.
Definition tif_read.c:1614
int TIFFFillStrip(TIFF *tif, uint32_t strip)
Load raw strip bytes and initialise codec decoding.
Definition tif_read.c:1055
static void JPEGResetUpsampled(TIFF *tif)
Update upsampling flags and cached layout sizes after colour-mode changes.
Definition tif_jpeg.c:4270
#define JPEG_MARKER_SOS
Recognise the JPEG start of scan marker.
Definition tif_jpeg.c:1876
#define TIFF_JPEG_MAX_MEMORY_TO_USE
Preferred libjpeg compressor memory limit.
static int TIFFjpeg_create_decompress(JPEGState *sp)
Create a libjpeg decompressor and install TIFF error routing.
Definition tif_jpeg.c:750
static int TIFFjpeg_start_decompress(JPEGState *sp)
Start the dependency decompressor and progress monitor.
Definition tif_jpeg.c:1045
static int JPEGPreEncode(TIFF *tif, uint16_t s)
Select per-strile JPEG sampling table emission and row encoding.
Definition tif_jpeg.c:3770
static int TIFFjpeg_read_header(JPEGState *sp, boolean require_image)
Parse a dependency JPEG header or tables-only stream.
Definition tif_jpeg.c:1000
static boolean std_fill_input_buffer(j_decompress_ptr cinfo)
Supply a synthetic JPEG end marker when cached input is exhausted.
Definition tif_jpeg.c:1608
static void std_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
Skip cached JPEG input or substitute synthetic end-of-image.
Definition tif_jpeg.c:1658
static void TIFFjpeg_tables_src(JPEGState *sp)
Install the JPEG source manager for tables-only input.
Definition tif_jpeg.c:1750
static int JPEGPreDecode(TIFF *tif, uint16_t s)
Validate one JPEG strile header and start its chosen decoder.
Definition tif_jpeg.c:2532
#define LONGJMP(jbuf, code)
Jump back to the frame saved by SETJMP (plain longjmp); never returns.
Definition tif_jpeg.c:372
static int JPEGFixupTags(TIFF *tif)
Attempt JPEG-stream YCbCr subsampling correction before ordinary access.
Definition tif_jpeg.c:1967
static void std_init_source(j_decompress_ptr cinfo)
Attach libjpeg input to cached raw strile bytes.
Definition tif_jpeg.c:1585
static int TIFFjpeg_read_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines, int max_lines)
Decode dependency scanline rows.
Definition tif_jpeg.c:1081
struct jpeg_source_mgr tiff_jpeg_source_mgr
Name the embedded dependency jpeg source mgr type.
Definition tif_jpeg.c:397
#define JPEG_MARKER_SOF10
Recognise the JPEG progressive arithmetic frame marker.
Definition tif_jpeg.c:1852
static int TIFFjpeg_set_colorspace(JPEGState *sp, J_COLOR_SPACE colorspace)
Select the dependency compression colour space.
Definition tif_jpeg.c:799
static void unsuppress_huff_table(JPEGState *sp, int tblno)
Enable emission of an existing JPEG huff table.
Definition tif_jpeg.c:3366
static int JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData *data, uint8_t *result)
Read one byte through the bounded JPEG marker window.
Definition tif_jpeg.c:2222
#define TIFF_JSAMPIMAGE
Select the dependency component-plane sample type.
Definition tif_jpeg.c:286
#define TIFF_JSAMPLE
Select the dependency scalar sample type.
Definition tif_jpeg.c:270
static const TIFFField jpegFields[]
Define JPEG table and quality/colour/table-mode tag metadata.
Definition tif_jpeg.c:545
static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
Gather packed sampled clumps into dependency raw JPEG component rows.
Definition tif_jpeg.c:4043
#define JPEG_MARKER_DQT
Recognise the JPEG quantisation table marker.
Definition tif_jpeg.c:1884
static int JPEGVGetField(TIFF *tif, uint32_t tag, va_list ap)
Retrieve JPEG tables/options or delegate related tag queries.
Definition tif_jpeg.c:4412
#define JPEG_MARKER_APP0
Recognise the JPEG first application (APP0) marker.
Definition tif_jpeg.c:1901
static void JPEGDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
Round parent tile recommendations to JPEG MCU dimensions.
Definition tif_jpeg.c:4516
static void JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData *data, uint16_t skiplength)
Skip bounded marker bytes without immediately reading them.
Definition tif_jpeg.c:2301
#define JPEG_MARKER_SOF1
Recognise the JPEG extended sequential Huffman frame marker.
Definition tif_jpeg.c:1828
int TIFFJPEGIsFullStripRequired(TIFF *tif)
Probe whether cached JPEG input needs whole-strile decoding.
Definition tif_jpeg.c:2410
static void TIFF_std_huff_tables(j_compress_ptr cinfo)
Populate missing standard JPEG Huffman tables for relevant dependency versions.
Definition tif_jpeg.c:3467
struct jpeg_error_mgr tiff_jpeg_error_mgr
Name the embedded dependency jpeg error mgr type.
Definition tif_jpeg.c:405
static int TIFFjpeg_has_multiple_scans(JPEGState *sp)
Query whether dependency input uses multiple scans.
Definition tif_jpeg.c:1020
static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
Reject scanline access to raw downsampled JPEG clumps.
Definition tif_jpeg.c:3054
#define JPEG_MARKER_SOF9
Recognise the JPEG extended sequential arithmetic frame marker.
Definition tif_jpeg.c:1844
static int TIFFjpeg_finish_decompress(JPEGState *sp)
Finish dependency decompression after output rows.
Definition tif_jpeg.c:1263
static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
Encode complete normal JPEG rows from caller bytes.
Definition tif_jpeg.c:3938
#define FIELD_JPEGTABLES
Reserve the codec field bit for JPEGTables.
Definition tif_jpeg.c:536
#define JState(tif)
Borrow the handle-owned JPEG state.
Definition tif_jpeg.c:519
#define TIFF_JSAMPARRAY
Select the dependency row-array sample type.
Definition tif_jpeg.c:278
static void JPEGFixupTagsSubsampling(TIFF *tif)
Probe first-strile markers to recover JPEG sampling factors.
Definition tif_jpeg.c:1997
#define JPEG_MARKER_SOF2
Recognise the JPEG progressive Huffman frame marker.
Definition tif_jpeg.c:1836
#define JPEG_MARKER_SOI
Recognise the JPEG start of image marker.
Definition tif_jpeg.c:1868
int TIFFReInitJPEG_12(TIFF *tif, const JPEGOtherSettings *otherSettings, int scheme, int is_encode)
Replace JPEG state with the twelve-bit ABI and restore saved options.
static void TIFFjpeg_progress_monitor(j_common_ptr cinfo)
Abort JPEG decompression when its scan count reaches the configured limit.
Definition tif_jpeg.c:636
static int TIFFjpeg_write_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines, int num_lines)
Encode dependency scanline rows.
Definition tif_jpeg.c:894
static JSAMPARRAY TIFFjpeg_alloc_sarray(JPEGState *sp, int pool_id, JDIMENSION samplesperrow, JDIMENSION numrows)
Allocate sample-row storage in a dependency memory pool.
Definition tif_jpeg.c:1330
static int JPEGSetupEncode(TIFF *tif)
Initialise JPEG compression parameters tables and output manager.
Definition tif_jpeg.c:3508
static uint64_t JPEGGetMaxCompressionRatio(TIFF *tif)
Estimate JPEG expansion from precision and sampled layout.
Definition tif_jpeg.c:4638
static TIFF_JSAMPLE JPEGReadPacked12Sample(const uint8_t *buf, tmsize_t sample_index)
Read one sample from TIFF packed twelve-bit bytes.
Definition tif_jpeg.c:1206
static tmsize_t JPEGRawBytesPerClumpLine(TIFF *tif, uint32_t width, uint16_t h_sampling, uint16_t v_sampling, int data_precision, const char *module)
Calculate packed bytes for one horizontally sampled JPEG clump row.
Definition tif_jpeg.c:1152
static void tables_init_source(j_decompress_ptr cinfo)
Attach libjpeg input to codec-owned JPEG table bytes.
Definition tif_jpeg.c:1731
static int TIFFjpeg_set_quality(JPEGState *sp, int quality, boolean force_baseline)
Set dependency quantisation quality and baseline policy.
Definition tif_jpeg.c:823
static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
Decode a whole JPEG strile into packed sampled clump rows.
Definition tif_jpeg.c:3104
static int JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData *data)
Scan marker segments for TIFF-compatible component sampling.
Definition tif_jpeg.c:2071
static int TIFFjpeg_destroy(JPEGState *sp)
Destroy dependency resources while retaining outer JPEGState.
Definition tif_jpeg.c:1303
static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
Decode whole JPEG output rows into caller bytes.
Definition tif_jpeg.c:2807
static int JPEGSetupDecode(TIFF *tif)
Initialise JPEG decompression tables sampling and source managers.
Definition tif_jpeg.c:2346
static int TIFFjpeg_create_compress(JPEGState *sp)
Create a libjpeg compressor and install TIFF error routing.
Definition tif_jpeg.c:720
int TIFFInitJPEG(TIFF *tif, int scheme)
Allocate JPEG-in-TIFF codec state and install method chains.
Definition tif_jpeg.c:4724
static void JPEGPrintDir(TIFF *tif, FILE *fd, long flags)
Print JPEG table extent then invoke the saved directory printer.
Definition tif_jpeg.c:4453
#define JMP_BUF
Select jump-buffer storage for dependency failures.
Definition tif_jpeg.c:380
static void tables_init_destination(j_compress_ptr cinfo)
Begin writing JPEG tables into the codec-owned table allocation.
Definition tif_jpeg.c:1465
static void std_term_source(j_decompress_ptr cinfo)
Complete JPEG source management without additional work.
Definition tif_jpeg.c:1686
static void std_term_destination(j_compress_ptr cinfo)
Commit dependency output cursor and pending bytes to the TIFF cache.
Definition tif_jpeg.c:1418
static void unsuppress_quant_table(JPEGState *sp, int tblno)
Enable emission of an existing JPEG quant table.
Definition tif_jpeg.c:3325
static int TIFFjpeg_abort(JPEGState *sp)
Abort the current dependency operation and release image-pool storage.
Definition tif_jpeg.c:1283
static void TIFFjpeg_output_message(j_common_ptr cinfo)
Route a dependency warning through the TIFF handle.
Definition tif_jpeg.c:610
#define SIZE_OF_JPEGTABLES
Size of the zero-filled placeholder JPEGTables buffer.
static int JPEGVSetField(TIFF *tif, uint32_t tag, va_list ap)
Set JPEG tables or pseudo-options and chain related tag changes.
Definition tif_jpeg.c:4334
#define JPEG_MARKER_DRI
Recognise the JPEG restart interval marker.
Definition tif_jpeg.c:1892
static void JPEGCleanup(TIFF *tif)
Restore JPEG tag hooks and release dependency tables and outer state.
Definition tif_jpeg.c:4238
int TIFFJPEGIsFullStripRequired_12(TIFF *tif)
Probe cached twelve-bit JPEG input for whole-strile requirements.
#define JPEG_MARKER_SOF0
Recognise the JPEG baseline sequential Huffman frame marker.
Definition tif_jpeg.c:1820
#define TIFF_JSAMPROW
Select the dependency row-pointer sample type.
Definition tif_jpeg.c:294
static int TIFFjpeg_finish_compress(JPEGState *sp)
Finish dependency compression through the attached destination.
Definition tif_jpeg.c:958
static void TIFFjpeg_data_dest(JPEGState *sp, TIFF *tif)
Install raw-buffer JPEG output manager callbacks.
Definition tif_jpeg.c:1441
static int TIFFjpeg_write_tables(JPEGState *sp)
Emit a tables-only JPEG stream through the destination.
Definition tif_jpeg.c:978
#define CALLVJPEG(sp, op)
Evaluate a void libjpeg call under CALLJPEG, yielding 1 on completion or 0 after a fatal libjpeg erro...
Definition tif_jpeg.c:701
static void TIFFjpeg_error_exit(j_common_ptr cinfo)
Route a fatal JPEG error abort the operation and jump to its wrapper.
Definition tif_jpeg.c:582
static boolean tables_empty_output_buffer(j_compress_ptr cinfo)
Grow the codec-owned JPEG table allocation by one thousand bytes.
Definition tif_jpeg.c:1490
static void std_init_destination(j_compress_ptr cinfo)
Attach libjpeg output to the handle raw buffer.
Definition tif_jpeg.c:1356
static void tables_term_destination(j_compress_ptr cinfo)
Replace JPEG table capacity with the emitted table-stream length.
Definition tif_jpeg.c:1520
static int alloc_downsampled_buffers(TIFF *tif, jpeg_component_info *comp_info, int num_components)
Allocate dependency-owned component rows for raw sampled JPEG transfer.
Definition tif_jpeg.c:1784
#define JPEG_MARKER_DHT
Recognise the JPEG Huffman table marker.
Definition tif_jpeg.c:1860
static void JPEGWritePacked12Sample(uint8_t *buf, tmsize_t sample_index, TIFF_JSAMPLE sample)
Write one sample to TIFF packed twelve-bit bytes.
Definition tif_jpeg.c:1232
static void TIFFInitJPEGCommon(TIFF *tif)
Install common JPEG callbacks defaults and handle backlink.
Definition tif_jpeg.c:4680
static boolean std_empty_output_buffer(j_compress_ptr cinfo)
Flush produced JPEG payload and reset the dependency output window.
Definition tif_jpeg.c:1378
static int TIFFjpeg_start_compress(JPEGState *sp, boolean write_all_tables)
Start the dependency compressor for a strile.
Definition tif_jpeg.c:868
struct jpeg_destination_mgr tiff_jpeg_destination_mgr
Name the embedded dependency jpeg destination mgr type.
Definition tif_jpeg.c:389
static int JPEGInitializeLibJPEG(TIFF *tif, int decompress)
Create or switch the embedded dependency compressor/decompressor.
Definition tif_jpeg.c:4566
static uint32_t JPEGDefaultStripSize(TIFF *tif, uint32_t s)
Round the parent strip recommendation to JPEG MCU height.
Definition tif_jpeg.c:4490
static int JPEGPostEncode(TIFF *tif)
Pad pending raw component rows and finish JPEG compression.
Definition tif_jpeg.c:4192
static int TIFFjpeg_read_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data, int max_lines)
Decode dependency downsampled component rows.
Definition tif_jpeg.c:1116
static int TIFFjpeg_set_defaults(JPEGState *sp)
Apply dependency default compression parameters.
Definition tif_jpeg.c:778
#define JPEG_MARKER_COM
Recognise the JPEG comment marker.
Definition tif_jpeg.c:1909
static int TIFFjpeg_tables_dest(JPEGState *sp, TIFF *tif)
Replace table storage and install tables-only output callbacks.
Definition tif_jpeg.c:1543
static void TIFFjpeg_data_src(JPEGState *sp)
Install cached-strile JPEG source manager callbacks.
Definition tif_jpeg.c:1703
static int TIFFjpeg_suppress_tables(JPEGState *sp, boolean suppress)
Set dependency table-emission suppression.
Definition tif_jpeg.c:846
static int JPEGComputeStrileWidthHeightBytesPerLine(TIFF *tif, uint16_t s)
Cache JPEG strile geometry and decoded row extent.
Definition tif_jpeg.c:2458
static void suppress_quant_table(JPEGState *sp, int tblno)
Suppress emission of an existing JPEG quant table.
Definition tif_jpeg.c:3345
static int TIFFjpeg_write_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data, int num_lines)
Encode dependency downsampled component rows.
Definition tif_jpeg.c:929
static void suppress_huff_table(JPEGState *sp, int tblno)
Suppress emission of an existing JPEG huff table.
Definition tif_jpeg.c:3389
static int prepare_JPEGTables(TIFF *tif)
Generate selected JPEG quantisation and Huffman tables.
Definition tif_jpeg.c:3413
#define CALLJPEG(sp, fail, op)
Evaluate a libjpeg call, yielding a failure value if libjpeg raises a fatal error.
Definition tif_jpeg.c:687
static int JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData *data, uint16_t *result)
Read a big-endian sixteen-bit marker value.
Definition tif_jpeg.c:2272
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void _TIFFsetByteArrayExt(TIFF *tif, void **vpp, const void *vp, uint32_t n)
Replace an owned byte array with a copied array.
Definition tif_dir.c:107
void * _TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
Resize a handle-aware block or release it for a nonpositive size.
Definition tif_open.c:257
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
void * _TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Allocate a checked array with an allocation-failure diagnostic.
Definition tif_aux.c:274
#define TIFFReadFile(tif, buf, size)
Invoke the handle's client read callback.
Definition tiffiop.h:885
#define isTiled(tif)
Test whether a handle uses tiled storage.
Definition tiffiop.h:833
#define TIFFhowmany_64(x, y)
Compute a 64-bit ceiling quotient using an unchecked addition.
Definition tiffiop.h:1115
#define TIFF_UPSAMPLED
Mark codec output that has already been upsampled.
Definition tiffiop.h:481
#define TIFFroundup_32(x, y)
Round a 32-bit value up to a multiple using the guarded quotient.
Definition tiffiop.h:1102
#define TRUE
Represent the legacy internal true value.
Definition tiffiop.h:131
#define FALSE
Represent the legacy internal false value.
Definition tiffiop.h:136
#define TIFFSeekFile(tif, off, whence)
Invoke the handle's client seek callback.
Definition tiffiop.h:920
#define TIFF_DIRTYDIRECT
Mark a current directory that needs writing.
Definition tiffiop.h:421
#define TIFFhowmany_32(x, y)
Compute a guarded 32-bit ceiling quotient.
Definition tiffiop.h:1056
#define TIFF_NOBITREV
Suppress automatic bit reversal.
Definition tiffiop.h:446
#define TIFFhowmany8_64(x)
Compute the byte count needed to hold a 64-bit bit count.
Definition tiffiop.h:1127
#define TIFFArrayCount(a)
Compute the element count of an actual array.
Definition tiffiop.h:1239
#define TIFF_ATTRIBUTE(x)
Expand a compiler attribute in a public declaration when supported.
Definition tiffio.h:764
#define TIFF_ANY
Match any data type when searching for a field descriptor.
Definition tiffio.h:1289
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a warning diagnostic to the configured handlers.
Definition tif_warning.c:85
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
const TIFFField * TIFFFieldWithTag(TIFF *tif, uint32_t tag)
Find a registered field by tag number.
tmsize_t TIFFTileSize(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:307
tmsize_t TIFFScanlineSize(TIFF *tif)
Compute the current scanline I/O byte estimate.
Definition tif_strip.c:383
tmsize_t TIFFTileRowSize(TIFF *tif)
Compute the packed byte count of one tile row.
Definition tif_tile.c:276
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
Definition tif_unix.c:495
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
Definition tif_unix.c:497
#define JPEGTABLESMODE_HUFF
Include Huffman tbls.
Definition tiff.h:1557
#define JPEGCOLORMODE_RAW
No conversion (default).
Definition tiff.h:1549
#define TIFFTAG_JPEGTABLESMODE
Pseudo-tag: What to put in JPEGTables.
Definition tiff.h:1553
#define JPEGTABLESMODE_QUANT
Include quantization tbls.
Definition tiff.h:1555
#define TIFFTAG_JPEGQUALITY
Pseudo-tag: Compression quality level.
Definition tiff.h:1544
#define TIFFTAG_JPEGCOLORMODE
Pseudo-tag: Auto RGB<=>YCbCr convert?
Definition tiff.h:1547
#define JPEGCOLORMODE_RGB
Do auto conversion.
Definition tiff.h:1551
uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
Query one logical strile file offset.
uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
Query one logical strile byte count.
int(* TIFFVGetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag getter.
Definition tiffio.h:1540
int TIFFSetField(TIFF *tif, uint32_t tag,...)
Set a tag value in the current in-memory directory.
Definition tif_dir.c:1627
void(* TIFFPrintMethod)(TIFF *tif, FILE *fd, long flags)
Append extension-specific directory information to a stream.
Definition tiffio.h:1561
int(* TIFFVSetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag setter.
Definition tiffio.h:1521
int TIFFGetField(TIFF *tif, uint32_t tag,...)
Query a present tag in the current directory.
Definition tif_dir.c:2099
#define PLANARCONFIG_SEPARATE
Separate planes of data.
Definition tiff.h:474
#define PHOTOMETRIC_MASK
Transparency mask (obsoleted by TIFF 6.0).
Definition tiff.h:392
#define TIFFTAG_PHOTOMETRIC
TIFF tag: Photometric interpretation.
Definition tiff.h:382
#define PHOTOMETRIC_PALETTE
Color map indexed.
Definition tiff.h:390
#define PHOTOMETRIC_SEPARATED
Color separations (introduced in TIFF 6.0).
Definition tiff.h:394
#define PHOTOMETRIC_YCBCR
CCIR 601 (introduced in TIFF 6.0).
Definition tiff.h:396
#define PHOTOMETRIC_MINISWHITE
Min value is white.
Definition tiff.h:384
#define TIFFTAG_YCBCRSUBSAMPLING
TIFF tag: YCbCr subsampling factors (introduced in TIFF 6.0).
Definition tiff.h:729
#define TIFFTAG_REFERENCEBLACKWHITE
TIFF tag: Colorimetry info (introduced in TIFF 6.0).
Definition tiff.h:737
#define PHOTOMETRIC_MINISBLACK
Min value is black.
Definition tiff.h:386
#define PHOTOMETRIC_RGB
RGB color model.
Definition tiff.h:388
#define TIFFTAG_JPEGTABLES
TIFF tag: JPEG table stream (introduced after TIFF 6.0).
Definition tiff.h:645
#define COMPRESSION_JPEG
JPEG DCT compression (introduced after TIFF 6.0).
Definition tiff.h:318
#define PLANARCONFIG_CONTIG
Single image plane.
Definition tiff.h:472
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
uint64_t toff_t
Represent an unsigned file byte offset independently of off_t.
Definition tiffio.h:94
@ TIFF_UNDEFINED
Untyped 8-bit data.
Definition tiff.h:245
Track a bounded first-strile window for JPEG sampling inspection.
Definition tif_jpeg.c:1919
TIFF * tif
Borrowed handle used by marker reads and sampling correction.
Definition tif_jpeg.c:1921
uint64_t fileoffset
Next unread window offset in the first strile.
Definition tif_jpeg.c:1931
uint8_t filepositioned
Nonzero when client position matches the refill offset.
Definition tif_jpeg.c:1935
uint64_t filebytesleft
Remaining bounded file extent.
Definition tif_jpeg.c:1933
void * buffer
Temporary owned Ext window, freed by the outer probe.
Definition tif_jpeg.c:1923
uint8_t * buffercurrentbyte
Borrowed next-byte cursor within buffer.
Definition tif_jpeg.c:1927
uint32_t bufferbytesleft
Unread buffered bytes.
Definition tif_jpeg.c:1929
uint32_t buffersize
Allocated window bytes, normally 2048.
Definition tif_jpeg.c:1925
Preserve ABI-independent JPEG options and saved parent hooks.
Definition tif_jpeg.c:61
TIFFVSetMethod vsetparent
Borrowed parent setter restored during cleanup.
Definition tif_jpeg.c:65
uint32_t jpegtables_length
Table byte extent, temporarily allocation capacity during generation.
Definition tif_jpeg.c:82
TIFFStripMethod defsparent
Borrowed parent strip-size recommender.
Definition tif_jpeg.c:69
TIFFPrintMethod printdir
Borrowed optional parent printer.
Definition tif_jpeg.c:67
int has_warned_about_progressive_mode
Latch suppressing repeated progressive-mode warnings.
Definition tif_jpeg.c:98
void * jpegtables
Owned Ext JPEG tables or placeholder; replaced by table building/tag assignment and freed on cleanup.
Definition tif_jpeg.c:77
TIFFTileMethod deftparent
Borrowed parent tile-size recommender.
Definition tif_jpeg.c:71
int jpegtablesmode
Cached quantisation/Huffman table emission mask.
Definition tif_jpeg.c:88
int jpegquality
Cached dependency compression quality, default 75.
Definition tif_jpeg.c:84
TIFFVGetMethod vgetparent
Borrowed parent getter restored during cleanup.
Definition tif_jpeg.c:63
int max_allowed_scan_number
Progressive scan threshold set at decompression start.
Definition tif_jpeg.c:96
int jpegcolormode
Cached raw/RGB colour-conversion policy.
Definition tif_jpeg.c:86
int ycbcrsampling_fetched
Set when YCbCrSubsampling is assigned through the tag setter (even if the parent rejects it); suppres...
Definition tif_jpeg.c:94
Own the leading libjpeg ABI context managers and TIFF transfer state.
Definition tif_jpeg.c:434
TIFF * tif
Borrowed handle backlink, live for all callbacks.
Definition tif_jpeg.c:473
JPEGOtherSettings otherSettings
Owned settings/table pointers and borrowed parent hooks retained across ABI reinitialisation.
Definition tif_jpeg.c:501
union JPEGState::@1 cinfo
Overlay the leading compression decompression and common dependency contexts.
tiff_jpeg_destination_mgr dest
Embedded output manager borrowing raw or table storage.
Definition tif_jpeg.c:466
int scancount
Count of accumulated downsampled row groups.
Definition tif_jpeg.c:493
TIFF_JSAMPARRAY ds_buffer[MAX_COMPONENTS]
Borrowed dependency image-pool component-row arrays, invalidated by image-pool release.
Definition tif_jpeg.c:491
struct jpeg_compress_struct c
Compression view of the leading dependency context union.
Definition tif_jpeg.c:440
int encode_raw_error
Latched failure preventing unsafe raw-encoder retry.
Definition tif_jpeg.c:504
struct jpeg_progress_mgr progress
Embedded dependency scan-progress monitor.
Definition tif_jpeg.c:463
JMP_BUF exit_jmpbuf
Jump buffer valid only during a synchronous wrapper invocation.
Definition tif_jpeg.c:460
tmsize_t bytesperline
Decoded line or raw clump-row byte extent for transfer.
Definition tif_jpeg.c:481
struct jpeg_common_struct comm
Common dependency view; leading layout supports callback casts.
Definition tif_jpeg.c:449
uint16_t photometric
Photometric copied at codec setup.
Definition tif_jpeg.c:475
int cinfo_initialized
Whether dependency creation succeeded and destroy is required.
Definition tif_jpeg.c:453
tiff_jpeg_error_mgr err
Embedded dependency error manager routing to TIFF diagnostics.
Definition tif_jpeg.c:456
uint16_t h_sampling
Cached positive horizontal luminance sampling.
Definition tif_jpeg.c:477
struct jpeg_decompress_struct d
Decompression view of the leading dependency context union.
Definition tif_jpeg.c:444
uint32_t strile_width
Current tile/truncated-strip pixel width.
Definition tif_jpeg.c:483
uint16_t v_sampling
Cached positive vertical luminance sampling.
Definition tif_jpeg.c:479
tiff_jpeg_source_mgr src
Embedded input manager borrowing raw/table or static EOI bytes.
Definition tif_jpeg.c:470
uint32_t strile_height
Current tile/truncated-strip row count.
Definition tif_jpeg.c:485
int samplesperclump
Samples per raw sampled clump.
Definition tif_jpeg.c:495
Store the current directory values and image traversal state.
Definition tif_dir.h:143
tmsize_t td_tilesize
Cached decoded tile size in bytes; minus one when invalidated.
Definition tif_dir.h:259
uint32_t td_rowsperstrip
Rows per strip; UINT32_MAX represents one whole-image strip.
Definition tif_dir.h:184
uint32_t td_imagewidth
Image width in pixels.
Definition tif_dir.h:154
uint16_t td_photometric
PHOTOMETRIC_* interpretation.
Definition tif_dir.h:174
uint32_t td_tilewidth
Tile width in pixels.
Definition tif_dir.h:160
tmsize_t td_scanlinesize
Cached decoded scanline size in bytes; zero when not computed.
Definition tif_dir.h:245
uint16_t td_bitspersample
Bits in each sample.
Definition tif_dir.h:168
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
Definition tif_dir.h:240
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
Definition tif_dir.h:200
uint16_t td_ycbcrsubsampling[2]
Horizontal and vertical YCbCr subsampling factors.
Definition tif_dir.h:312
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
Definition tif_dir.h:182
uint32_t td_imagelength
Image length in rows.
Definition tif_dir.h:156
uint32_t td_tilelength
Tile length in rows.
Definition tif_dir.h:162
TIFFVGetMethod vgetfield
Variadic tag-query implementation for the handle.
Definition tiffio.h:1577
TIFFVSetMethod vsetfield
Variadic tag-setting implementation for the handle.
Definition tiffio.h:1575
TIFFPrintMethod printdir
Optional codec/extension directory-print callback.
Definition tiffio.h:1579
Describe a field's file type and internal argument contract.
Definition tif_dir.h:993
unsigned short field_bit
FIELD_* presence index; FIELD_CUSTOM shares storage through the custom list.
Definition tif_dir.h:1023
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
TIFFTileMethod tif_deftilesize
Codec callback computing or constraining default tile dimensions.
Definition tiffiop.h:667
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
Definition tiffiop.h:649
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
Definition tiffiop.h:653
TIFFTagMethods tif_tagmethods
Inline table of directory field and print callbacks.
Definition tiffiop.h:742
TIFFPreMethod tif_preencode
Codec callback preparing a row, strip, or tile encode.
Definition tiffiop.h:639
TIFFBoolMethod tif_fixuptags
Codec callback for directory-dependent tag corrections.
Definition tiffiop.h:626
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
Definition tiffiop.h:696
TIFFPreMethod tif_predecode
Codec callback preparing a row, strip, or tile decode.
Definition tiffiop.h:630
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
Definition tiffiop.h:647
TIFFGetMaxCompressionRatioMethod tif_getmaxcompressionratio
Codec callback returning its maximum compression ratio.
Definition tiffiop.h:670
TIFFPostMethod tif_postdecode
Callback transforming decoded data, for example by byte swapping.
Definition tiffiop.h:727
uint64_t tif_diroff
Current directory byte offset, or zero for an unlinked in-memory IFD.
Definition tiffiop.h:558
TIFFStripMethod tif_defstripsize
Codec callback computing or constraining a default strip size.
Definition tiffiop.h:663
char * tif_name
Current filename pointer; may refer to inline or client-supplied storage.
Definition tiffiop.h:394
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
Definition tiffiop.h:643
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
Definition tiffiop.h:632
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
Definition tiffiop.h:641
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
Definition tiffiop.h:645
TIFFVoidMethod tif_cleanup
Codec callback releasing codec-private state.
Definition tiffiop.h:661
uint8_t * tif_data
Codec-private storage; allocation and cleanup follow the selected codec.
Definition tiffiop.h:675
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
Definition tiffiop.h:692
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
Definition tiffiop.h:628
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
Definition tiffiop.h:681
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
Definition tiffiop.h:686
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
Definition tiffiop.h:404
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
Definition tiffiop.h:651
Declare private library state, callbacks, and implementation helpers.