LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_ojpeg.c
Go to the documentation of this file.
1/* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
2 specification is now totally obsolete and deprecated for new applications and
3 images. This file was was created solely in order to read unconverted images
4 still present on some users' computer systems. It will never be extended
5 to write such files. Writing new-style JPEG compressed TIFFs is implemented
6 in tif_jpeg.c.
7
8 The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
9 testfiles, and anticipate as much as possible all other... But still, it may
10 fail on some. If you encounter problems, please report them on the TIFF
11 mailing list and/or to Joris Van Damme <info@awaresystems.be>.
12
13 Please read the file called "TIFF Technical Note #2" if you need to be
14 convinced this compression scheme is bad and breaks TIFF. That document
15 is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
16 and from AWare Systems' TIFF section
17 <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
18 in Adobe's specification supplements, marked "draft" up to this day, but
19 supported by the TIFF community.
20
21 This file interfaces with Release 6B of the JPEG Library written by the
22 Independent JPEG Group. Previous versions of this file required a hack inside
23 the LibJpeg library. This version no longer requires that. Remember to
24 remove the hack if you update from the old version.
25
26 Copyright (c) Joris Van Damme <info@awaresystems.be>
27 Copyright (c) AWare Systems <http://www.awaresystems.be/>
28
29 The licence agreement for this file is the same as the rest of the LibTiff
30 library.
31
32 IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
33 ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
34 OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
35 WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
36 LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 OF THIS SOFTWARE.
38
39 Joris Van Damme and/or AWare Systems may be available for custom
40 development. If you like what you see, and need anything similar or related,
41 contact <info@awaresystems.be>.
42*/
43
44/* What is what, and what is not?
45
46 This decoder starts with an input stream, that is essentially the
47 JpegInterchangeFormat stream, if any, followed by the strile data, if any.
48 This stream is read in OJPEGReadByte and related functions.
49
50 It analyzes the start of this stream, until it encounters non-marker data,
51 i.e. compressed image data. Some of the header markers it sees have no actual
52 content, like the SOI marker, and APP/COM markers that really shouldn't even
53 be there. Some other markers do have content, and the valuable bits and
54 pieces of information in these markers are saved, checking all to verify that
55 the stream is more or less within expected bounds. This happens inside the
56 OJPEGReadHeaderInfoSecStreamXxx functions.
57
58 Some OJPEG imagery contains no valid JPEG header markers. This situation is
59 picked up on if we've seen no SOF marker when we're at the start of the
60 compressed image data. In this case, the tables are read from JpegXxxTables
61 tags, and the other bits and pieces of information is initialized to its most
62 basic value. This is implemented in the OJPEGReadHeaderInfoSecTablesXxx
63 functions.
64
65 When this is complete, a good and valid JPEG header can be assembled, and
66 this is passed through to LibJpeg. When that's done, the remainder of the
67 input stream, i.e. the compressed image data, can be passed through
68 unchanged. This is done in OJPEGWriteStream functions.
69
70 LibTiff rightly expects to know the subsampling values before decompression.
71 Just like in new-style JPEG-in-TIFF, though, or even more so, actually, the
72 YCbCrsubsampling tag is notoriously unreliable. To correct these tag values
73 with the ones inside the JPEG stream, the first part of the input stream is
74 pre-scanned in OJPEGSubsamplingCorrect, making no note of any other data,
75 reporting no warnings or errors, up to the point where either these values
76 are read, or it's clear they aren't there. This means that some of the data
77 is read twice, but we feel speed in correcting these values is important
78 enough to warrant this sacrifice. Although there is currently no define or
79 other configuration mechanism to disable this behavior, the actual header
80 scanning is build to robustly respond with error report if it should
81 encounter an uncorrected mismatch of subsampling values. See
82 OJPEGReadHeaderInfoSecStreamSof.
83
84 The restart interval and restart markers are the most tricky part... The
85 restart interval can be specified in a tag. It can also be set inside the
86 input JPEG stream. It can be used inside the input JPEG stream. If reading
87 from strile data, we've consistently discovered the need to insert restart
88 markers in between the different striles, as is also probably the most likely
89 interpretation of the original TIFF 6.0 specification. With all this setting
90 of interval, and actual use of markers that is not predictable at the time of
91 valid JPEG header assembly, the restart thing may turn out the Achilles heel
92 of this implementation. Fortunately, most OJPEG writer vendors succeed in
93 reading back what they write, which may be the reason why we've been able to
94 discover ways that seem to work.
95
96 Some special provision is made for planarconfig separate OJPEG files. These
97 seem to consistently contain header info, a SOS marker, a plane, SOS marker,
98 plane, SOS, and plane. This may or may not be a valid JPEG configuration, we
99 don't know and don't care. We want LibTiff to be able to access the planes
100 individually, without huge buffering inside LibJpeg, anyway. So we compose
101 headers to feed to LibJpeg, in this case, that allow us to pass a single
102 plane such that LibJpeg sees a valid single-channel JPEG stream. Locating
103 subsequent SOS markers, and thus subsequent planes, is done inside
104 OJPEGReadSecondarySos.
105
106 The benefit of the scheme is... that it works, basically. We know of no other
107 that does. It works without checking software tag, or otherwise going about
108 things in an OJPEG flavor specific manner. Instead, it is a single scheme,
109 that covers the cases with and without JpegInterchangeFormat, with and
110 without striles, with part of the header in JpegInterchangeFormat and
111 remainder in first strile, etc. It is forgiving and robust, may likely work
112 with OJPEG flavors we've not seen yet, and makes most out of the data.
113
114 Another nice side-effect is that a complete JPEG single valid stream is build
115 if planarconfig is not separate (vast majority). We may one day use that to
116 build converters to JPEG, and/or to new-style JPEG compression inside TIFF.
117
118 A disadvantage is the lack of random access to the individual striles. This
119 is the reason for much of the complicated restart-and-position stuff inside
120 OJPEGPreDecode. Applications would do well accessing all striles in order, as
121 this will result in a single sequential scan of the input stream, and no
122 restarting of LibJpeg decoding session.
123*/
124
130#include "tiffiop.h"
131#ifdef OJPEG_SUPPORT
132
133/* Configuration defines here are:
134 * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some
135 * environments, like eg LibTiffDelphi, this is not possible. For this reason,
136 * the actual calls to libjpeg, with longjump stuff, are encapsulated in
137 * dedicated functions. When JPEG_ENCAP_EXTERNAL is defined, these encapsulating
138 * functions are declared external to this unit, and can be defined elsewhere to
139 * use stuff other then longjump. The default mode, without JPEG_ENCAP_EXTERNAL,
140 * implements the call encapsulators here, internally, with normal longjump.
141 * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent
142 * is conveniently available, but still it may be worthwhile to use _setjmp or
143 * sigsetjmp in place of plain setjmp. These macros will make it easier. It is
144 * useless to fiddle with these if you define JPEG_ENCAP_EXTERNAL. OJPEG_BUFFER:
145 * Define the size of the desired buffer here. Should be small enough so as to
146 * guarantee instant processing, optimal streaming and optimal use of processor
147 * cache, but also big enough so as to not result in significant call overhead.
148 * It should be at least a few bytes to accommodate some structures (this is
149 * verified in asserts), but it would not be sensible to make it this small
150 * anyway, and it should be at most 64K since it is indexed with uint16_t. We
151 * recommend 2K. EGYPTIANWALK: You could also define EGYPTIANWALK here, but it
152 * is not used anywhere and has absolutely no effect. That is why most people
153 * insist the EGYPTIANWALK is a bit silly.
154 */
155
156/* define LIBJPEG_ENCAP_EXTERNAL */
169#define SETJMP(jbuf) setjmp(jbuf)
184#define LONGJMP(jbuf, code) longjmp(jbuf, code)
192#define JMP_BUF jmp_buf
201#define OJPEG_BUFFER 2048
202/* define EGYPTIANWALK */
203
212#define JPEG_MARKER_SOF0 0xC0
220#define JPEG_MARKER_SOF1 0xC1
228#define JPEG_MARKER_SOF3 0xC3
237#define JPEG_MARKER_DHT 0xC4
245#define JPEG_MARKER_RST0 0XD0
253#define JPEG_MARKER_SOI 0xD8
260#define JPEG_MARKER_EOI 0xD9
268#define JPEG_MARKER_SOS 0xDA
277#define JPEG_MARKER_DQT 0xDB
285#define JPEG_MARKER_DRI 0xDD
293#define JPEG_MARKER_APP0 0xE0
301#define JPEG_MARKER_COM 0xFE
302
310#define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC + 0)
319#define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC + 1)
327#define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC + 2)
335#define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC + 3)
343#define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC + 4)
351#define FIELD_OJPEG_JPEGPROC (FIELD_CODEC + 5)
359#define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC + 6)
360
390
391#ifndef LIBJPEG_ENCAP_EXTERNAL
392#include <setjmp.h>
393#endif
394
395#include "jerror.h"
396#include "jpeglib.h"
397
405typedef struct jpeg_source_mgr tiff_ojpeg_source_mgr;
406
413typedef struct jpeg_error_mgr tiff_ojpeg_error_mgr;
414
422typedef struct jpeg_common_struct tiff_ojpeg_common_struct;
430typedef struct jpeg_decompress_struct tiff_ojpeg_decompress_struct;
431
450
499
510typedef struct
511{
520#ifndef LIBJPEG_ENCAP_EXTERNAL
523#endif
531 uint64_t file_size;
533 uint32_t image_width;
535 uint32_t image_length;
537 uint32_t strile_width;
553 uint8_t jpeg_proc;
573 uint64_t qtable_offset[3];
575 uint64_t dctable_offset[3];
577 uint64_t actable_offset[3];
579 uint8_t *qtable[4];
581 uint8_t *dctable[4];
583 uint8_t *actable[4];
589 uint8_t sof_log;
593 uint32_t sof_x;
595 uint32_t sof_y;
597 uint8_t sof_c[3];
599 uint8_t sof_hv[3];
601 uint8_t sof_tq[3];
603 uint8_t sos_cs[3];
605 uint8_t sos_tda[3];
607 struct
608 {
610 uint8_t log;
619 } sos_end[3];
681 OJPEGStateInBufferSource in_buffer_source;
683 uint32_t in_buffer_next_strile;
687 uint64_t in_buffer_file_pos;
691 uint64_t in_buffer_file_togo;
697 uint8_t in_buffer[OJPEG_BUFFER];
701 uint8_t out_buffer[OJPEG_BUFFER];
703 uint8_t *skip_buffer;
704} OJPEGState;
705
706static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap);
707static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap);
708static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags);
709
710static int OJPEGFixupTags(TIFF *tif);
711static int OJPEGSetupDecode(TIFF *tif);
712static int OJPEGPreDecode(TIFF *tif, uint16_t s);
713static int OJPEGPreDecodeSkipRaw(TIFF *tif);
714static int OJPEGPreDecodeSkipScanlines(TIFF *tif);
715static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
716static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc);
717static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc);
718static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc);
719static int OJPEGSetupEncode(TIFF *tif);
720static int OJPEGPreEncode(TIFF *tif, uint16_t s);
721static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
722static int OJPEGPostEncode(TIFF *tif);
723static void OJPEGCleanup(TIFF *tif);
724
725static void OJPEGSubsamplingCorrect(TIFF *tif);
726static int OJPEGReadHeaderInfo(TIFF *tif);
727static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s);
728static int OJPEGWriteHeaderInfo(TIFF *tif);
729static void OJPEGLibjpegSessionAbort(TIFF *tif);
730
731static int OJPEGReadHeaderInfoSec(TIFF *tif);
735static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id);
740
741static int OJPEGReadBufferFill(OJPEGState *sp);
742static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte);
743static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte);
744static void OJPEGReadByteAdvance(OJPEGState *sp);
745static int OJPEGReadWord(OJPEGState *sp, uint16_t *word);
746static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem);
747static void OJPEGReadSkip(OJPEGState *sp, uint16_t len);
748
749static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len);
750static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len);
751static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem,
752 uint32_t *len);
753static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem,
754 uint32_t *len);
755static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem,
756 uint32_t *len);
757static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len);
758static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len);
759static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len);
760static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len);
761static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len);
762static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len);
763
764#ifdef LIBJPEG_ENCAP_EXTERNAL
804 uint8_t require_image);
845 void *scanlines, uint32_t max_lines);
869 void *data, uint32_t max_lines);
882extern void jpeg_encap_unwind(TIFF *tif);
883#else
923 uint8_t require_image);
964 void *scanlines, uint32_t max_lines);
988 void *data, uint32_t max_lines);
1001static void jpeg_encap_unwind(TIFF *tif);
1002#endif
1003
1004static void
1007static void
1009static boolean
1011static void
1013 long num_bytes);
1014static boolean
1016 int desired);
1017static void
1019
1020int TIFFInitOJPEG(TIFF *tif, int scheme)
1021{
1022 static const char module[] = "TIFFInitOJPEG";
1023 OJPEGState *sp;
1024
1025 (void)scheme;
1026 assert(scheme == COMPRESSION_OJPEG);
1027
1028 /*
1029 * Merge codec-specific tag information.
1030 */
1032 {
1033 TIFFErrorExtR(tif, module,
1034 "Merging Old JPEG codec-specific tags failed");
1035 return 0;
1036 }
1037
1038 /* state block */
1039 sp = (OJPEGState *)_TIFFmallocExt(tif, sizeof(OJPEGState));
1040 if (sp == NULL)
1041 {
1042 TIFFErrorExtR(tif, module, "No space for OJPEG state block");
1043 return (0);
1044 }
1045 _TIFFmemset(sp, 0, sizeof(OJPEGState));
1046 sp->tif = tif;
1047 sp->jpeg_proc = 1;
1048 sp->subsampling_hor = 2;
1049 sp->subsampling_ver = 2;
1051 /* tif codec methods */
1066 tif->tif_data = (uint8_t *)sp;
1067 /* tif tag methods */
1068 sp->vgetparent = tif->tif_tagmethods.vgetfield;
1070 sp->vsetparent = tif->tif_tagmethods.vsetfield;
1072 sp->printdir = tif->tif_tagmethods.printdir;
1074 /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
1075 Some others do, but have totally meaningless or corrupt values
1076 in these tags. In these cases, the JpegInterchangeFormat stream is
1077 reliable. In any case, this decoder reads the compressed data itself,
1078 from the most reliable locations, and we need to notify encapsulating
1079 LibTiff not to read raw strips or tiles for us. */
1080 tif->tif_flags |= TIFF_NOREADRAW;
1081 return (1);
1082}
1083
1105static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap)
1106{
1107 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1108 switch (tag)
1109 {
1111 *va_arg(ap, uint64_t *) = (uint64_t)sp->jpeg_interchange_format;
1112 break;
1114 *va_arg(ap, uint64_t *) =
1115 (uint64_t)sp->jpeg_interchange_format_length;
1116 break;
1118 if (sp->subsamplingcorrect_done == 0)
1120 *va_arg(ap, uint16_t *) = (uint16_t)sp->subsampling_hor;
1121 *va_arg(ap, uint16_t *) = (uint16_t)sp->subsampling_ver;
1122 break;
1124 *va_arg(ap, uint32_t *) = (uint32_t)sp->qtable_offset_count;
1125 *va_arg(ap, const void **) = (const void *)sp->qtable_offset;
1126 break;
1128 *va_arg(ap, uint32_t *) = (uint32_t)sp->dctable_offset_count;
1129 *va_arg(ap, const void **) = (const void *)sp->dctable_offset;
1130 break;
1132 *va_arg(ap, uint32_t *) = (uint32_t)sp->actable_offset_count;
1133 *va_arg(ap, const void **) = (const void *)sp->actable_offset;
1134 break;
1135 case TIFFTAG_JPEGPROC:
1136 *va_arg(ap, uint16_t *) = (uint16_t)sp->jpeg_proc;
1137 break;
1139 *va_arg(ap, uint16_t *) = sp->restart_interval;
1140 break;
1141 default:
1142 return (*sp->vgetparent)(tif, tag, ap);
1143 }
1144 return (1);
1145}
1146
1169static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap)
1170{
1171 static const char module[] = "OJPEGVSetField";
1172 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1173 uint32_t ma;
1174 uint64_t *mb;
1175 uint32_t n;
1176 const TIFFField *fip;
1177
1178 switch (tag)
1179 {
1181 sp->jpeg_interchange_format = (uint64_t)va_arg(ap, uint64_t);
1182 break;
1184 sp->jpeg_interchange_format_length = (uint64_t)va_arg(ap, uint64_t);
1185 break;
1187 sp->subsampling_tag = 1;
1188 sp->subsampling_hor = (uint8_t)va_arg(ap, uint16_vap);
1189 sp->subsampling_ver = (uint8_t)va_arg(ap, uint16_vap);
1190 tif->tif_dir.td_ycbcrsubsampling[0] = sp->subsampling_hor;
1191 tif->tif_dir.td_ycbcrsubsampling[1] = sp->subsampling_ver;
1192 break;
1194 ma = (uint32_t)va_arg(ap, uint32_t);
1195 if (ma != 0)
1196 {
1197 if (ma > 3)
1198 {
1199 TIFFErrorExtR(tif, module,
1200 "JpegQTables tag has incorrect count");
1201 return (0);
1202 }
1203 sp->qtable_offset_count = (uint8_t)ma;
1204 mb = (uint64_t *)va_arg(ap, uint64_t *);
1205 for (n = 0; n < ma; n++)
1206 sp->qtable_offset[n] = mb[n];
1207 }
1208 break;
1210 ma = (uint32_t)va_arg(ap, uint32_t);
1211 if (ma != 0)
1212 {
1213 if (ma > 3)
1214 {
1215 TIFFErrorExtR(tif, module,
1216 "JpegDcTables tag has incorrect count");
1217 return (0);
1218 }
1219 sp->dctable_offset_count = (uint8_t)ma;
1220 mb = (uint64_t *)va_arg(ap, uint64_t *);
1221 for (n = 0; n < ma; n++)
1222 sp->dctable_offset[n] = mb[n];
1223 }
1224 break;
1226 ma = (uint32_t)va_arg(ap, uint32_t);
1227 if (ma != 0)
1228 {
1229 if (ma > 3)
1230 {
1231 TIFFErrorExtR(tif, module,
1232 "JpegAcTables tag has incorrect count");
1233 return (0);
1234 }
1235 sp->actable_offset_count = (uint8_t)ma;
1236 mb = (uint64_t *)va_arg(ap, uint64_t *);
1237 for (n = 0; n < ma; n++)
1238 sp->actable_offset[n] = mb[n];
1239 }
1240 break;
1241 case TIFFTAG_JPEGPROC:
1242 sp->jpeg_proc = (uint8_t)va_arg(ap, uint16_vap);
1243 break;
1245 sp->restart_interval = (uint16_t)va_arg(ap, uint16_vap);
1246 break;
1247 default:
1248 return (*sp->vsetparent)(tif, tag, ap);
1249 }
1250 fip = TIFFFieldWithTag(tif, tag);
1251 if (fip == NULL) /* shouldn't happen */
1252 return (0);
1253 TIFFSetFieldBit(tif, fip->field_bit);
1255 return (1);
1256}
1257
1270static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags)
1271{
1272 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1273 uint8_t m;
1274 (void)flags;
1275 assert(sp != NULL);
1277 fprintf(fd, " JpegInterchangeFormat: %" PRIu64 "\n",
1278 (uint64_t)sp->jpeg_interchange_format);
1280 fprintf(fd, " JpegInterchangeFormatLength: %" PRIu64 "\n",
1281 (uint64_t)sp->jpeg_interchange_format_length);
1283 {
1284 fprintf(fd, " JpegQTables:");
1285 for (m = 0; m < sp->qtable_offset_count; m++)
1286 fprintf(fd, " %" PRIu64, (uint64_t)sp->qtable_offset[m]);
1287 fprintf(fd, "\n");
1288 }
1290 {
1291 fprintf(fd, " JpegDcTables:");
1292 for (m = 0; m < sp->dctable_offset_count; m++)
1293 fprintf(fd, " %" PRIu64, (uint64_t)sp->dctable_offset[m]);
1294 fprintf(fd, "\n");
1295 }
1297 {
1298 fprintf(fd, " JpegAcTables:");
1299 for (m = 0; m < sp->actable_offset_count; m++)
1300 fprintf(fd, " %" PRIu64, (uint64_t)sp->actable_offset[m]);
1301 fprintf(fd, "\n");
1302 }
1304 fprintf(fd, " JpegProc: %" PRIu8 "\n", sp->jpeg_proc);
1306 fprintf(fd, " JpegRestartInterval: %" PRIu16 "\n",
1307 sp->restart_interval);
1308 if (sp->printdir)
1309 (*sp->printdir)(tif, fd, flags);
1310}
1311
1322static int OJPEGFixupTags(TIFF *tif)
1323{
1324 (void)tif;
1325 return (1);
1326}
1327
1338static int OJPEGSetupDecode(TIFF *tif)
1339{
1340 static const char module[] = "OJPEGSetupDecode";
1341 TIFFWarningExtR(tif, module,
1342 "Deprecated and troublesome old-style JPEG compression "
1343 "mode, please convert to new-style JPEG compression and "
1344 "notify vendor of writing software");
1345 return (1);
1346}
1347
1368static int OJPEGPreDecode(TIFF *tif, uint16_t s)
1369{
1370 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1371 uint32_t m;
1372 if (sp->subsamplingcorrect_done == 0)
1374 if (sp->readheader_done == 0)
1375 {
1376 if (OJPEGReadHeaderInfo(tif) == 0)
1377 return (0);
1378 }
1379 if (sp->sos_end[s].log == 0)
1380 {
1381 if (OJPEGReadSecondarySos(tif, s) == 0)
1382 return (0);
1383 }
1384 if (isTiled(tif))
1385 m = tif->tif_dir.td_curtile;
1386 else
1387 m = tif->tif_dir.td_curstrip;
1388 if ((sp->writeheader_done != 0) &&
1389 ((sp->write_cursample != s) || (sp->write_curstrile > m)))
1390 {
1391 if (sp->libjpeg_session_active != 0)
1393 sp->writeheader_done = 0;
1394 }
1395 if (sp->writeheader_done == 0)
1396 {
1397 sp->plane_sample_offset = (uint8_t)s;
1398 sp->write_cursample = s;
1400 if ((sp->in_buffer_file_pos_log == 0) ||
1403 {
1407 sp->in_buffer_file_pos_log = 0;
1409 sp->in_buffer_togo = 0;
1410 sp->in_buffer_cur = 0;
1411 }
1412 if (OJPEGWriteHeaderInfo(tif) == 0)
1413 return (0);
1414 }
1415
1417
1418 while (sp->write_curstrile < m)
1419 {
1420 if (sp->libjpeg_jpeg_query_style == 0)
1421 {
1422 if (OJPEGPreDecodeSkipRaw(tif) == 0)
1423 return (0);
1424 }
1425 else
1426 {
1427 if (OJPEGPreDecodeSkipScanlines(tif) == 0)
1428 return (0);
1429 }
1430 sp->write_curstrile++;
1431 }
1432 sp->decoder_ok = 1;
1433 return (1);
1434}
1435
1452{
1453 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1454 uint32_t m;
1455 m = sp->lines_per_strile;
1456 if (sp->subsampling_convert_state != 0)
1457 {
1459 {
1463 return (1);
1464 }
1468 }
1469 while (m >= sp->subsampling_convert_clines)
1470 {
1473 (uint32_t)sp->subsampling_ver * 8) == 0)
1474 return (0);
1476 }
1477 if (m > 0)
1478 {
1481 (uint32_t)sp->subsampling_ver * 8) == 0)
1482 return (0);
1484 }
1485 return (1);
1486}
1487
1503{
1504 static const char module[] = "OJPEGPreDecodeSkipScanlines";
1505 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1506 uint32_t m;
1507 if (sp->skip_buffer == NULL)
1508 {
1509 sp->skip_buffer = (uint8_t *)_TIFFmallocExt(tif, sp->bytes_per_line);
1510 if (sp->skip_buffer == NULL)
1511 {
1512 TIFFErrorExtR(tif, module, "Out of memory");
1513 return (0);
1514 }
1515 }
1516 for (m = 0; m < sp->lines_per_strile; m++)
1517 {
1518 if (jpeg_read_scanlines_encap(sp, &(sp->libjpeg_jpeg_decompress_struct),
1519 &sp->skip_buffer, 1) == 0)
1520 return (0);
1521 }
1522 return (1);
1523}
1524
1546static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
1547{
1548 static const char module[] = "OJPEGDecode";
1549 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1550 (void)s;
1551 if (!sp->decoder_ok)
1552 {
1553 memset(buf, 0, (size_t)cc);
1554 TIFFErrorExtR(tif, module,
1555 "Cannot decode: decoder not correctly initialized");
1556 return 0;
1557 }
1558 if (sp->libjpeg_session_active == 0)
1559 {
1560 memset(buf, 0, (size_t)cc);
1561 /* This should normally not happen, except that it does when */
1562 /* using TIFFReadScanline() which calls OJPEGPostDecode() for */
1563 /* each scanline, which assumes that a whole strile was read */
1564 /* and may thus incorrectly consider it has read the whole image,
1565 * causing */
1566 /* OJPEGLibjpegSessionAbort() to be called prematurely. */
1567 /* Triggered by https://gitlab.com/libtiff/libtiff/-/issues/337 */
1568 TIFFErrorExtR(tif, module,
1569 "Cannot decode: libjpeg_session_active == 0");
1570 return 0;
1571 }
1572 if (sp->error_in_raw_data_decoding)
1573 {
1574 memset(buf, 0, (size_t)cc);
1575 return 0;
1576 }
1577 if (sp->libjpeg_jpeg_query_style == 0)
1578 {
1579 if (OJPEGDecodeRaw(tif, buf, cc) == 0)
1580 {
1581 memset(buf, 0, (size_t)cc);
1582 return (0);
1583 }
1584 }
1585 else
1586 {
1587 if (OJPEGDecodeScanlines(tif, buf, cc) == 0)
1588 {
1589 memset(buf, 0, (size_t)cc);
1590 return (0);
1591 }
1592 }
1593 return (1);
1594}
1595
1616static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc)
1617{
1618 static const char module[] = "OJPEGDecodeRaw";
1619 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1620 uint8_t *m;
1621 tmsize_t n;
1622 uint8_t *oy;
1623 uint8_t *ocb;
1624 uint8_t *ocr;
1625 uint8_t *p;
1626 uint32_t q;
1627 uint8_t *r;
1628 uint8_t sx, sy;
1629 if (cc % sp->bytes_per_line != 0)
1630 {
1631 TIFFErrorExtR(tif, module, "Fractional scanline not read");
1632 return (0);
1633 }
1634 if (cc <= 0)
1635 return (0);
1636 m = buf;
1637 n = cc;
1638 do
1639 {
1640 if (sp->subsampling_convert_state == 0)
1641 {
1643 sp, &(sp->libjpeg_jpeg_decompress_struct),
1644 sp->subsampling_convert_ycbcrimage,
1645 (uint32_t)sp->subsampling_ver * 8) == 0)
1646 {
1647 sp->error_in_raw_data_decoding = 1;
1648 return (0);
1649 }
1650 }
1651 oy = sp->subsampling_convert_ybuf +
1652 sp->subsampling_convert_state * sp->subsampling_ver *
1653 sp->subsampling_convert_ylinelen;
1654 ocb = sp->subsampling_convert_cbbuf +
1655 sp->subsampling_convert_state * sp->subsampling_convert_clinelen;
1656 ocr = sp->subsampling_convert_crbuf +
1657 sp->subsampling_convert_state * sp->subsampling_convert_clinelen;
1658 p = m;
1659 for (q = 0; q < sp->subsampling_convert_clinelenout; q++)
1660 {
1661 r = oy;
1662 for (sy = 0; sy < sp->subsampling_ver; sy++)
1663 {
1664 for (sx = 0; sx < sp->subsampling_hor; sx++)
1665 *p++ = *r++;
1666 r += sp->subsampling_convert_ylinelen - sp->subsampling_hor;
1667 }
1668 oy += sp->subsampling_hor;
1669 *p++ = *ocb++;
1670 *p++ = *ocr++;
1671 }
1672 sp->subsampling_convert_state++;
1673 if (sp->subsampling_convert_state == sp->subsampling_convert_clines)
1674 sp->subsampling_convert_state = 0;
1675 m += sp->bytes_per_line;
1676 n -= sp->bytes_per_line;
1677 } while (n > 0);
1678 return (1);
1679}
1680
1701static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc)
1702{
1703 static const char module[] = "OJPEGDecodeScanlines";
1704 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1705 uint8_t *m;
1706 tmsize_t n;
1707 if (cc % sp->bytes_per_line != 0)
1708 {
1709 TIFFErrorExtR(tif, module, "Fractional scanline not read");
1710 return (0);
1711 }
1712 if (cc <= 0)
1713 return (0);
1714 m = buf;
1715 n = cc;
1716 do
1717 {
1718 if (jpeg_read_scanlines_encap(sp, &(sp->libjpeg_jpeg_decompress_struct),
1719 &m, 1) == 0)
1720 return (0);
1721 m += sp->bytes_per_line;
1722 n -= sp->bytes_per_line;
1723 } while (n > 0);
1724 return (1);
1725}
1726
1743static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
1744{
1745 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1746 (void)buf;
1747 (void)cc;
1748 /* This function somehow incorrectly assumes that a whole strile was read,
1749 */
1750 /* which is not true when TIFFReadScanline() is called, */
1751 /* and may thus incorrectly consider it has read the whole image, causing */
1752 /* OJPEGLibjpegSessionAbort() to be called prematurely. */
1753 /* So this logic should be fixed to take into account cc, or disable */
1754 /* the scan line reading interface. */
1755 /* Triggered by https://gitlab.com/libtiff/libtiff/-/issues/337 */
1756 sp->write_curstrile++;
1757 if (sp->write_curstrile % tif->tif_dir.td_stripsperimage == 0)
1758 {
1759 assert(sp->libjpeg_session_active != 0);
1761 sp->writeheader_done = 0;
1762 }
1763}
1764
1774static int OJPEGSetupEncode(TIFF *tif)
1775{
1776 static const char module[] = "OJPEGSetupEncode";
1778 tif, module,
1779 "OJPEG encoding not supported; use new-style JPEG compression instead");
1780 return (0);
1781}
1782
1793static int OJPEGPreEncode(TIFF *tif, uint16_t s)
1794{
1795 static const char module[] = "OJPEGPreEncode";
1796 (void)s;
1798 tif, module,
1799 "OJPEG encoding not supported; use new-style JPEG compression instead");
1800 return (0);
1801}
1802
1815static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
1816{
1817 static const char module[] = "OJPEGEncode";
1818 (void)buf;
1819 (void)cc;
1820 (void)s;
1822 tif, module,
1823 "OJPEG encoding not supported; use new-style JPEG compression instead");
1824 return (0);
1825}
1826
1836static int OJPEGPostEncode(TIFF *tif)
1837{
1838 static const char module[] = "OJPEGPostEncode";
1840 tif, module,
1841 "OJPEG encoding not supported; use new-style JPEG compression instead");
1842 return (0);
1843}
1844
1859static void OJPEGCleanup(TIFF *tif)
1860{
1861 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1862 if (sp != 0)
1863 {
1866 tif->tif_tagmethods.printdir = sp->printdir;
1867 if (sp->qtable[0] != 0)
1868 _TIFFfreeExt(tif, sp->qtable[0]);
1869 if (sp->qtable[1] != 0)
1870 _TIFFfreeExt(tif, sp->qtable[1]);
1871 if (sp->qtable[2] != 0)
1872 _TIFFfreeExt(tif, sp->qtable[2]);
1873 if (sp->qtable[3] != 0)
1874 _TIFFfreeExt(tif, sp->qtable[3]);
1875 if (sp->dctable[0] != 0)
1876 _TIFFfreeExt(tif, sp->dctable[0]);
1877 if (sp->dctable[1] != 0)
1878 _TIFFfreeExt(tif, sp->dctable[1]);
1879 if (sp->dctable[2] != 0)
1880 _TIFFfreeExt(tif, sp->dctable[2]);
1881 if (sp->dctable[3] != 0)
1882 _TIFFfreeExt(tif, sp->dctable[3]);
1883 if (sp->actable[0] != 0)
1884 _TIFFfreeExt(tif, sp->actable[0]);
1885 if (sp->actable[1] != 0)
1886 _TIFFfreeExt(tif, sp->actable[1]);
1887 if (sp->actable[2] != 0)
1888 _TIFFfreeExt(tif, sp->actable[2]);
1889 if (sp->actable[3] != 0)
1890 _TIFFfreeExt(tif, sp->actable[3]);
1891 if (sp->libjpeg_session_active != 0)
1893 if (sp->subsampling_convert_ycbcrbuf != 0)
1895 if (sp->subsampling_convert_ycbcrimage != 0)
1897 if (sp->skip_buffer != 0)
1898 _TIFFfreeExt(tif, sp->skip_buffer);
1899 _TIFFfreeExt(tif, sp);
1900 tif->tif_data = NULL;
1902 }
1903}
1904
1933{
1934 static const char module[] = "OJPEGSubsamplingCorrect";
1935 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1936 uint8_t mh;
1937 uint8_t mv;
1938
1939 assert(sp->subsamplingcorrect_done == 0);
1940 if ((tif->tif_dir.td_samplesperpixel != 3) ||
1943 {
1944 if (sp->subsampling_tag != 0)
1945 TIFFWarningExtR(tif, module,
1946 "Subsampling tag not appropriate for this "
1947 "Photometric and/or SamplesPerPixel");
1948 sp->subsampling_hor = 1;
1949 sp->subsampling_ver = 1;
1950 sp->subsampling_force_desubsampling_inside_decompression = 0;
1951 }
1952 else
1953 {
1954 sp->subsamplingcorrect_done = 1;
1955 mh = sp->subsampling_hor;
1956 mv = sp->subsampling_ver;
1957 sp->subsamplingcorrect = 1;
1959 if (sp->subsampling_force_desubsampling_inside_decompression != 0)
1960 {
1961 sp->subsampling_hor = 1;
1962 sp->subsampling_ver = 1;
1963 }
1964 sp->subsamplingcorrect = 0;
1965 if (((sp->subsampling_hor != mh) || (sp->subsampling_ver != mv)) &&
1966 (sp->subsampling_force_desubsampling_inside_decompression == 0))
1967 {
1968 if (sp->subsampling_tag == 0)
1970 tif, module,
1971 "Subsampling tag is not set, yet subsampling inside JPEG "
1972 "data [%" PRIu8 ",%" PRIu8
1973 "] does not match default values [2,2]; assuming "
1974 "subsampling inside JPEG data is correct",
1975 sp->subsampling_hor, sp->subsampling_ver);
1976 else
1978 tif, module,
1979 "Subsampling inside JPEG data [%" PRIu8 ",%" PRIu8
1980 "] does not match subsampling tag values [%" PRIu8
1981 ",%" PRIu8
1982 "]; assuming subsampling inside JPEG data is correct",
1983 sp->subsampling_hor, sp->subsampling_ver, mh, mv);
1984 }
1985 if (sp->subsampling_force_desubsampling_inside_decompression != 0)
1986 {
1987 if (sp->subsampling_tag == 0)
1989 tif, module,
1990 "Subsampling tag is not set, yet subsampling inside JPEG "
1991 "data does not match default values [2,2] (nor any other "
1992 "values allowed in TIFF); assuming subsampling inside JPEG "
1993 "data is correct and desubsampling inside JPEG "
1994 "decompression");
1995 else
1997 tif, module,
1998 "Subsampling inside JPEG data does not match subsampling "
1999 "tag values [%" PRIu8 ",%" PRIu8
2000 "] (nor any other values allowed in TIFF); assuming "
2001 "subsampling inside JPEG data is correct and desubsampling "
2002 "inside JPEG decompression",
2003 mh, mv);
2004 }
2005 if (sp->subsampling_force_desubsampling_inside_decompression == 0)
2006 {
2007 if (sp->subsampling_hor < sp->subsampling_ver)
2008 TIFFWarningExtR(tif, module,
2009 "Subsampling values [%" PRIu8 ",%" PRIu8
2010 "] are not allowed in TIFF",
2011 sp->subsampling_hor, sp->subsampling_ver);
2012 }
2013 }
2014 sp->subsamplingcorrect_done = 1;
2015}
2016
2038{
2039 static const char module[] = "OJPEGReadHeaderInfo";
2040 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2041 assert(sp->readheader_done == 0);
2042 sp->image_width = tif->tif_dir.td_imagewidth;
2043 sp->image_length = tif->tif_dir.td_imagelength;
2044 if (isTiled(tif))
2045 {
2046 sp->strile_width = tif->tif_dir.td_tilewidth;
2047 sp->strile_length = tif->tif_dir.td_tilelength;
2048 sp->strile_length_total =
2049 ((sp->image_length + sp->strile_length - 1) / sp->strile_length) *
2050 sp->strile_length;
2051 }
2052 else
2053 {
2054 sp->strile_width = sp->image_width;
2055 sp->strile_length = tif->tif_dir.td_rowsperstrip;
2056 if (sp->strile_length == (uint32_t)-1)
2057 sp->strile_length = sp->image_length;
2058 sp->strile_length_total = sp->image_length;
2059 }
2060 if (tif->tif_dir.td_samplesperpixel == 1)
2061 {
2062 sp->samples_per_pixel = 1;
2063 sp->plane_sample_offset = 0;
2064 sp->samples_per_pixel_per_plane = sp->samples_per_pixel;
2065 sp->subsampling_hor = 1;
2066 sp->subsampling_ver = 1;
2067 }
2068 else
2069 {
2070 if (tif->tif_dir.td_samplesperpixel != 3)
2071 {
2072 TIFFErrorExtR(tif, module,
2073 "SamplesPerPixel %" PRIu8
2074 " not supported for this compression scheme",
2075 sp->samples_per_pixel);
2076 return (0);
2077 }
2078 sp->samples_per_pixel = 3;
2079 sp->plane_sample_offset = 0;
2081 sp->samples_per_pixel_per_plane = 3;
2082 else
2083 sp->samples_per_pixel_per_plane = 1;
2084 }
2085 if (sp->strile_length < sp->image_length)
2086 {
2087 if (((sp->subsampling_hor != 1) && (sp->subsampling_hor != 2) &&
2088 (sp->subsampling_hor != 4)) ||
2089 ((sp->subsampling_ver != 1) && (sp->subsampling_ver != 2) &&
2090 (sp->subsampling_ver != 4)))
2091 {
2092 TIFFErrorExtR(tif, module, "Invalid subsampling values");
2093 return (0);
2094 }
2095 if (sp->strile_length % ((uint32_t)sp->subsampling_ver * 8) != 0)
2096 {
2097 TIFFErrorExtR(tif, module,
2098 "Incompatible vertical subsampling and image "
2099 "strip/tile length");
2100 return (0);
2101 }
2102 {
2103 /* Values were validated above as 1, 2, or 4. */
2104 uint32_t mcu_width = (uint32_t)sp->subsampling_hor * 8;
2105 uint32_t mcu_height = (uint32_t)sp->subsampling_ver * 8;
2106 uint32_t mcu_cols =
2107 TIFFhowmany_32_maxuint_compat(sp->strile_width, mcu_width);
2108 uint32_t mcu_rows = sp->strile_length / mcu_height;
2109 if (mcu_cols == 0 || mcu_rows == 0)
2110 {
2111 sp->restart_interval = 0;
2112 }
2113 else
2114 {
2115 uint32_t restart_interval =
2116 _TIFFMultiply32(tif, mcu_cols, mcu_rows, module);
2117 if (restart_interval == 0 || restart_interval > UINT16_MAX)
2118 {
2119 TIFFErrorExtR(tif, module,
2120 "Integer overflow in restart interval");
2121 return (0);
2122 }
2123 sp->restart_interval = (uint16_t)restart_interval;
2124 }
2125 }
2126 }
2127 if (OJPEGReadHeaderInfoSec(tif) == 0)
2128 return (0);
2129 sp->sos_end[0].log = 1;
2130 sp->sos_end[0].in_buffer_source = sp->in_buffer_source;
2131 sp->sos_end[0].in_buffer_next_strile = sp->in_buffer_next_strile;
2132 sp->sos_end[0].in_buffer_file_pos =
2133 sp->in_buffer_file_pos - sp->in_buffer_togo;
2134 sp->sos_end[0].in_buffer_file_togo =
2135 sp->in_buffer_file_togo + sp->in_buffer_togo;
2136 sp->readheader_done = 1;
2137 return (1);
2138}
2139
2155static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s)
2156{
2157 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2158 uint8_t m;
2159 assert(s > 0);
2160 assert(s < 3);
2161 assert(sp->sos_end[0].log != 0);
2162 assert(sp->sos_end[s].log == 0);
2163 sp->plane_sample_offset = (uint8_t)(s - 1);
2164 while (sp->sos_end[sp->plane_sample_offset].log == 0)
2165 sp->plane_sample_offset--;
2166 sp->in_buffer_source =
2170 sp->in_buffer_file_pos =
2172 sp->in_buffer_file_pos_log = 0;
2175 sp->in_buffer_togo = 0;
2176 sp->in_buffer_cur = 0;
2177 while (sp->plane_sample_offset < s)
2178 {
2179 do
2180 {
2181 if (OJPEGReadByte(sp, &m) == 0)
2182 return (0);
2183 if (m == 255)
2184 {
2185 do
2186 {
2187 if (OJPEGReadByte(sp, &m) == 0)
2188 return (0);
2189 if (m != 255)
2190 break;
2191 } while (1);
2192 if (m == JPEG_MARKER_SOS)
2193 break;
2194 }
2195 } while (1);
2196 sp->plane_sample_offset++;
2197 if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0)
2198 return (0);
2199 sp->sos_end[sp->plane_sample_offset].log = 1;
2201 sp->in_buffer_source;
2208 }
2209 return (1);
2210}
2211
2232{
2233 static const char module[] = "OJPEGWriteHeaderInfo";
2234 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2235 uint8_t **m;
2236 uint32_t n;
2237 /* if a previous attempt failed, don't try again */
2238 if (sp->libjpeg_session_active != 0)
2239 return 0;
2240 sp->out_state = ososSoi;
2241 sp->restart_index = 0;
2242 jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr));
2243 sp->libjpeg_jpeg_error_mgr.output_message =
2245 sp->libjpeg_jpeg_error_mgr.error_exit = OJPEGLibjpegJpegErrorMgrErrorExit;
2246 sp->libjpeg_jpeg_decompress_struct.err = &(sp->libjpeg_jpeg_error_mgr);
2247 sp->libjpeg_jpeg_decompress_struct.client_data = (void *)tif;
2249 sp, &(sp->libjpeg_jpeg_decompress_struct)) == 0)
2250 return (0);
2251 sp->libjpeg_session_active = 1;
2252 sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = 0;
2253 sp->libjpeg_jpeg_source_mgr.init_source =
2255 sp->libjpeg_jpeg_source_mgr.fill_input_buffer =
2257 sp->libjpeg_jpeg_source_mgr.skip_input_data =
2259 sp->libjpeg_jpeg_source_mgr.resync_to_restart =
2261 sp->libjpeg_jpeg_source_mgr.term_source =
2263 sp->libjpeg_jpeg_decompress_struct.src = &(sp->libjpeg_jpeg_source_mgr);
2264 if (jpeg_read_header_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), 1) ==
2265 0)
2266 return (0);
2267 if ((sp->subsampling_force_desubsampling_inside_decompression == 0) &&
2268 (sp->samples_per_pixel_per_plane > 1))
2269 {
2270 sp->libjpeg_jpeg_decompress_struct.raw_data_out = 1;
2271#if JPEG_LIB_VERSION >= 70
2272 sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling = FALSE;
2273#endif
2274 sp->libjpeg_jpeg_query_style = 0;
2275 if (sp->subsampling_convert_log == 0)
2276 {
2277 assert(sp->subsampling_convert_ycbcrbuf == 0);
2278 assert(sp->subsampling_convert_ycbcrimage == 0);
2279 /* Check for division by zero. */
2280 if (sp->subsampling_hor == 0 || sp->subsampling_ver == 0)
2281 return (0);
2282 /* Check for potential overflow in subsampling_convert_ylinelen
2283 * computation.
2284 */
2285 if (sp->strile_width >
2286 UINT32_MAX - ((uint32_t)sp->subsampling_hor * 8 - 1))
2287 return (0);
2288 sp->subsampling_convert_ylinelen =
2289 ((sp->strile_width + (uint32_t)sp->subsampling_hor * 8 - 1) /
2290 ((uint32_t)sp->subsampling_hor * 8) *
2291 ((uint32_t)sp->subsampling_hor * 8));
2292 sp->subsampling_convert_ylines = (uint32_t)sp->subsampling_ver * 8;
2293 sp->subsampling_convert_clinelen =
2294 sp->subsampling_convert_ylinelen / sp->subsampling_hor;
2295 sp->subsampling_convert_clines = 8;
2296 /* Check for potential overflow in buffer length computations.
2297 * Use 64-bit intermediates to detect uint32_t overflow in
2298 * ylinelen * ylines, clinelen * clines, and their sum.
2299 */
2300 {
2301 uint64_t ybuflen64 =
2302 (uint64_t)sp->subsampling_convert_ylinelen *
2303 sp->subsampling_convert_ylines;
2304 uint64_t cbuflen64 =
2305 (uint64_t)sp->subsampling_convert_clinelen *
2306 sp->subsampling_convert_clines;
2307 uint64_t ycbcrbuflen64 = ybuflen64 + 2 * cbuflen64;
2308 if (ybuflen64 > UINT32_MAX || cbuflen64 > UINT32_MAX ||
2309 ycbcrbuflen64 > UINT32_MAX)
2310 {
2311 TIFFErrorExtR(tif, module,
2312 "Integer overflow in OJPEG buffer size");
2313 return (0);
2314 }
2315 sp->subsampling_convert_ybuflen = (uint32_t)ybuflen64;
2316 sp->subsampling_convert_cbuflen = (uint32_t)cbuflen64;
2317 sp->subsampling_convert_ycbcrbuflen = (uint32_t)ycbcrbuflen64;
2318 }
2319 /* The calloc is not normally necessary, except in some edge/broken
2320 * cases */
2321 /* for example for a tiled image of height 1 with a tile height of 1
2322 * and subsampling_hor=subsampling_ver=2 */
2323 /* In that case, libjpeg will only fill the 8 first lines of the 16
2324 * lines */
2325 /* See https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=16844
2326 */
2327 /* Even if this case is allowed (?), its handling is broken because
2328 * OJPEGPreDecode() should also likely */
2329 /* reset subsampling_convert_state to 0 when changing tile. */
2330 sp->subsampling_convert_ycbcrbuf = (uint8_t *)_TIFFcallocExt(
2331 tif, 1, sp->subsampling_convert_ycbcrbuflen);
2332 if (sp->subsampling_convert_ycbcrbuf == 0)
2333 {
2334 TIFFErrorExtR(tif, module, "Out of memory");
2335 return (0);
2336 }
2337 sp->subsampling_convert_ybuf = sp->subsampling_convert_ycbcrbuf;
2338 sp->subsampling_convert_cbbuf =
2339 sp->subsampling_convert_ybuf + sp->subsampling_convert_ybuflen;
2340 sp->subsampling_convert_crbuf =
2341 sp->subsampling_convert_cbbuf + sp->subsampling_convert_cbuflen;
2342 sp->subsampling_convert_ycbcrimagelen =
2343 3 + sp->subsampling_convert_ylines +
2344 2 * sp->subsampling_convert_clines;
2345 sp->subsampling_convert_ycbcrimage = (uint8_t **)_TIFFmallocExt(
2346 tif, (tmsize_t)((size_t)sp->subsampling_convert_ycbcrimagelen *
2347 sizeof(uint8_t *)));
2348 if (sp->subsampling_convert_ycbcrimage == 0)
2349 {
2350 TIFFErrorExtR(tif, module, "Out of memory");
2351 return (0);
2352 }
2353 m = sp->subsampling_convert_ycbcrimage;
2354 *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3);
2355 *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3 +
2356 sp->subsampling_convert_ylines);
2357 *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3 +
2358 sp->subsampling_convert_ylines +
2359 sp->subsampling_convert_clines);
2360 for (n = 0; n < sp->subsampling_convert_ylines; n++)
2361 *m++ = sp->subsampling_convert_ybuf +
2362 n * sp->subsampling_convert_ylinelen;
2363 for (n = 0; n < sp->subsampling_convert_clines; n++)
2364 *m++ = sp->subsampling_convert_cbbuf +
2365 n * sp->subsampling_convert_clinelen;
2366 for (n = 0; n < sp->subsampling_convert_clines; n++)
2367 *m++ = sp->subsampling_convert_crbuf +
2368 n * sp->subsampling_convert_clinelen;
2369 sp->subsampling_convert_clinelenout =
2370 sp->strile_width / sp->subsampling_hor +
2371 ((sp->strile_width % sp->subsampling_hor) != 0 ? 1 : 0);
2372 sp->subsampling_convert_state = 0;
2373 sp->error_in_raw_data_decoding = 0;
2374
2375 const uint64_t bpl =
2376 (uint64_t)sp->subsampling_convert_clinelenout *
2377 ((uint64_t)sp->subsampling_ver * sp->subsampling_hor + 2);
2378 if (bpl > UINT32_MAX)
2379 return (0);
2380 sp->bytes_per_line = (uint32_t)bpl;
2381
2382 sp->lines_per_strile =
2383 sp->strile_length / sp->subsampling_ver +
2384 ((sp->strile_length % sp->subsampling_ver) != 0 ? 1 : 0);
2385 sp->subsampling_convert_log = 1;
2386 }
2387 }
2388 else
2389 {
2390 sp->libjpeg_jpeg_decompress_struct.jpeg_color_space = JCS_UNKNOWN;
2391 sp->libjpeg_jpeg_decompress_struct.out_color_space = JCS_UNKNOWN;
2392 sp->libjpeg_jpeg_query_style = 1;
2393 sp->bytes_per_line =
2394 _TIFFMultiply32(tif, sp->samples_per_pixel_per_plane,
2395 sp->strile_width, "OJPEGWriteHeaderInfo");
2396 if (sp->bytes_per_line == 0)
2397 return (0);
2398 sp->lines_per_strile = sp->strile_length;
2399 }
2401 &(sp->libjpeg_jpeg_decompress_struct)) == 0)
2402 return (0);
2403 if (sp->libjpeg_jpeg_decompress_struct.image_width != sp->strile_width)
2404 {
2405 TIFFErrorExtR(tif, module,
2406 "jpeg_start_decompress() returned image_width = %u, "
2407 "expected %" PRIu32,
2408 sp->libjpeg_jpeg_decompress_struct.image_width,
2409 sp->strile_width);
2410 return 0;
2411 }
2412 if (sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor !=
2413 sp->subsampling_hor ||
2414 sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor !=
2415 sp->subsampling_ver)
2416 {
2417 TIFFErrorExtR(tif, module,
2418 "jpeg_start_decompress() returned max_h_samp_factor = %d "
2419 "and max_v_samp_factor = %d, expected %" PRIu8
2420 " and %" PRIu8,
2421 sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor,
2422 sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor,
2423 sp->subsampling_hor, sp->subsampling_ver);
2424 return 0;
2425 }
2426
2427 sp->writeheader_done = 1;
2428 return (1);
2429}
2430
2443{
2444 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2445 assert(sp->libjpeg_session_active != 0);
2446 jpeg_destroy(
2448 sp->libjpeg_session_active = 0;
2449}
2450
2470{
2471 static const char module[] = "OJPEGReadHeaderInfoSec";
2472 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2473 uint8_t m;
2474 uint16_t n;
2475 uint8_t o;
2476 if (sp->file_size == 0)
2477 sp->file_size = TIFFGetFileSize(tif);
2478 if (sp->jpeg_interchange_format != 0)
2479 {
2480 if (sp->jpeg_interchange_format >= sp->file_size)
2481 {
2482 sp->jpeg_interchange_format = 0;
2483 sp->jpeg_interchange_format_length = 0;
2484 }
2485 else
2486 {
2487 if ((sp->jpeg_interchange_format_length == 0) ||
2488 (sp->jpeg_interchange_format >
2489 UINT64_MAX - sp->jpeg_interchange_format_length) ||
2490 (sp->jpeg_interchange_format +
2491 sp->jpeg_interchange_format_length >
2492 sp->file_size))
2493 sp->jpeg_interchange_format_length =
2494 sp->file_size - sp->jpeg_interchange_format;
2495 }
2496 }
2497 sp->in_buffer_source = osibsNotSetYet;
2498 sp->in_buffer_next_strile = 0;
2499 sp->in_buffer_strile_count = tif->tif_dir.td_nstrips;
2500 sp->in_buffer_file_togo = 0;
2501 sp->in_buffer_togo = 0;
2502 do
2503 {
2504 if (OJPEGReadBytePeek(sp, &m) == 0)
2505 return (0);
2506 if (m != 255)
2507 break;
2509 do
2510 {
2511 if (OJPEGReadByte(sp, &m) == 0)
2512 return (0);
2513 } while (m == 255);
2514 switch (m)
2515 {
2516 case JPEG_MARKER_SOI:
2517 /* this type of marker has no data, and should be skipped */
2518 break;
2519 case JPEG_MARKER_COM:
2520 case JPEG_MARKER_APP0:
2521 case JPEG_MARKER_APP0 + 1:
2522 case JPEG_MARKER_APP0 + 2:
2523 case JPEG_MARKER_APP0 + 3:
2524 case JPEG_MARKER_APP0 + 4:
2525 case JPEG_MARKER_APP0 + 5:
2526 case JPEG_MARKER_APP0 + 6:
2527 case JPEG_MARKER_APP0 + 7:
2528 case JPEG_MARKER_APP0 + 8:
2529 case JPEG_MARKER_APP0 + 9:
2530 case JPEG_MARKER_APP0 + 10:
2531 case JPEG_MARKER_APP0 + 11:
2532 case JPEG_MARKER_APP0 + 12:
2533 case JPEG_MARKER_APP0 + 13:
2534 case JPEG_MARKER_APP0 + 14:
2535 case JPEG_MARKER_APP0 + 15:
2536 /* this type of marker has data, but it has no use to us (and no
2537 * place here) and should be skipped */
2538 if (OJPEGReadWord(sp, &n) == 0)
2539 return (0);
2540 if (n < 2)
2541 {
2542 if (sp->subsamplingcorrect == 0)
2543 TIFFErrorExtR(tif, module, "Corrupt JPEG data");
2544 return (0);
2545 }
2546 if (n > 2)
2547 OJPEGReadSkip(sp, (uint16_t)(n - 2));
2548 break;
2549 case JPEG_MARKER_DRI:
2550 if (OJPEGReadHeaderInfoSecStreamDri(tif) == 0)
2551 return (0);
2552 break;
2553 case JPEG_MARKER_DQT:
2554 if (OJPEGReadHeaderInfoSecStreamDqt(tif) == 0)
2555 return (0);
2556 break;
2557 case JPEG_MARKER_DHT:
2558 if (OJPEGReadHeaderInfoSecStreamDht(tif) == 0)
2559 return (0);
2560 break;
2561 case JPEG_MARKER_SOF0:
2562 case JPEG_MARKER_SOF1:
2563 case JPEG_MARKER_SOF3:
2564 if (OJPEGReadHeaderInfoSecStreamSof(tif, m) == 0)
2565 return (0);
2566 if (sp->subsamplingcorrect != 0)
2567 return (1);
2568 break;
2569 case JPEG_MARKER_SOS:
2570 if (sp->subsamplingcorrect != 0)
2571 return (1);
2572 assert(sp->plane_sample_offset == 0);
2573 if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0)
2574 return (0);
2575 break;
2576 default:
2577 TIFFErrorExtR(tif, module,
2578 "Unknown marker type %" PRIu8 " in JPEG data", m);
2579 return (0);
2580 }
2581 } while (m != JPEG_MARKER_SOS);
2582 if (sp->subsamplingcorrect)
2583 return (1);
2584 if (sp->sof_log == 0)
2585 {
2587 return (0);
2588 sp->sof_marker_id = JPEG_MARKER_SOF0;
2589 for (o = 0; o < sp->samples_per_pixel; o++)
2590 sp->sof_c[o] = (uint8_t)o;
2591 sp->sof_hv[0] =
2592 (uint8_t)((sp->subsampling_hor << 4) | sp->subsampling_ver);
2593 for (o = 1; o < sp->samples_per_pixel; o++)
2594 sp->sof_hv[o] = 17;
2595 sp->sof_x = sp->strile_width;
2596 sp->sof_y = sp->strile_length_total;
2597 sp->sof_log = 1;
2599 return (0);
2601 return (0);
2602 for (o = 1; o < sp->samples_per_pixel; o++)
2603 sp->sos_cs[o] = o;
2604 }
2605 return (1);
2606}
2607
2622{
2623 /* This could easily cause trouble in some cases... but no such cases have
2624 occurred so far */
2625 static const char module[] = "OJPEGReadHeaderInfoSecStreamDri";
2626 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2627 uint16_t m;
2628 if (OJPEGReadWord(sp, &m) == 0)
2629 return (0);
2630 if (m != 4)
2631 {
2632 TIFFErrorExtR(tif, module, "Corrupt DRI marker in JPEG data");
2633 return (0);
2634 }
2635 if (OJPEGReadWord(sp, &m) == 0)
2636 return (0);
2637 sp->restart_interval = m;
2638 return (1);
2639}
2640
2658{
2659 /* this is a table marker, and it is to be saved as a whole for exact
2660 * pushing on the jpeg stream later on */
2661 static const char module[] = "OJPEGReadHeaderInfoSecStreamDqt";
2662 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2663 uint16_t m;
2664 uint32_t na;
2665 uint8_t *nb;
2666 uint8_t o;
2667 if (OJPEGReadWord(sp, &m) == 0)
2668 return (0);
2669 if (m <= 2)
2670 {
2671 if (sp->subsamplingcorrect == 0)
2672 TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data");
2673 return (0);
2674 }
2675 if (sp->subsamplingcorrect != 0)
2676 OJPEGReadSkip(sp, (uint16_t)(m - 2));
2677 else
2678 {
2679 m = (uint16_t)(m - 2);
2680 do
2681 {
2682 if (m < 65)
2683 {
2684 TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data");
2685 return (0);
2686 }
2687 na = sizeof(uint32_t) + 69;
2688 nb = (uint8_t *)_TIFFmallocExt(tif, na);
2689 if (nb == 0)
2690 {
2691 TIFFErrorExtR(tif, module, "Out of memory");
2692 return (0);
2693 }
2694 *(uint32_t *)nb = na;
2695 nb[sizeof(uint32_t)] = 255;
2696 nb[sizeof(uint32_t) + 1] = JPEG_MARKER_DQT;
2697 nb[sizeof(uint32_t) + 2] = 0;
2698 nb[sizeof(uint32_t) + 3] = 67;
2699 if (OJPEGReadBlock(sp, 65, &nb[sizeof(uint32_t) + 4]) == 0)
2700 {
2701 _TIFFfreeExt(tif, nb);
2702 return (0);
2703 }
2704 o = nb[sizeof(uint32_t) + 4] & 15;
2705 if (3 < o)
2706 {
2707 TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data");
2708 _TIFFfreeExt(tif, nb);
2709 return (0);
2710 }
2711 if (sp->qtable[o] != 0)
2712 _TIFFfreeExt(tif, sp->qtable[o]);
2713 sp->qtable[o] = nb;
2714 m = (uint16_t)(m - 65);
2715 } while (m > 0);
2716 }
2717 return (1);
2718}
2719
2737{
2738 /* this is a table marker, and it is to be saved as a whole for exact
2739 * pushing on the jpeg stream later on */
2740 /* TODO: the following assumes there is only one table in this marker... but
2741 * i'm not quite sure that assumption is guaranteed correct */
2742 static const char module[] = "OJPEGReadHeaderInfoSecStreamDht";
2743 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2744 uint16_t m;
2745 uint32_t na;
2746 uint8_t *nb;
2747 uint8_t o;
2748 if (OJPEGReadWord(sp, &m) == 0)
2749 return (0);
2750 if (m <= 2)
2751 {
2752 if (sp->subsamplingcorrect == 0)
2753 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
2754 return (0);
2755 }
2756 if (sp->subsamplingcorrect != 0)
2757 {
2758 OJPEGReadSkip(sp, (uint16_t)(m - 2));
2759 }
2760 else
2761 {
2762 na = (uint32_t)(sizeof(uint32_t) + 2 + m);
2763 nb = (uint8_t *)_TIFFmallocExt(tif, na);
2764 if (nb == 0)
2765 {
2766 TIFFErrorExtR(tif, module, "Out of memory");
2767 return (0);
2768 }
2769 *(uint32_t *)nb = na;
2770 nb[sizeof(uint32_t)] = 255;
2771 nb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT;
2772 nb[sizeof(uint32_t) + 2] = (uint8_t)(m >> 8);
2773 nb[sizeof(uint32_t) + 3] = (uint8_t)(m & 255);
2774 if (OJPEGReadBlock(sp, (uint16_t)(m - 2), &nb[sizeof(uint32_t) + 4]) ==
2775 0)
2776 {
2777 _TIFFfreeExt(tif, nb);
2778 return (0);
2779 }
2780 o = nb[sizeof(uint32_t) + 4];
2781 if ((o & 240) == 0)
2782 {
2783 if (3 < o)
2784 {
2785 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
2786 _TIFFfreeExt(tif, nb);
2787 return (0);
2788 }
2789 if (sp->dctable[o] != 0)
2790 _TIFFfreeExt(tif, sp->dctable[o]);
2791 sp->dctable[o] = nb;
2792 }
2793 else
2794 {
2795 if ((o & 240) != 16)
2796 {
2797 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
2798 _TIFFfreeExt(tif, nb);
2799 return (0);
2800 }
2801 o &= 15;
2802 if (3 < o)
2803 {
2804 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
2805 _TIFFfreeExt(tif, nb);
2806 return (0);
2807 }
2808 if (sp->actable[o] != 0)
2809 _TIFFfreeExt(tif, sp->actable[o]);
2810 sp->actable[o] = nb;
2811 }
2812 }
2813 return (1);
2814}
2815
2833static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id)
2834{
2835 /* this marker needs to be checked, and part of its data needs to be saved
2836 * for regeneration later on */
2837 static const char module[] = "OJPEGReadHeaderInfoSecStreamSof";
2838 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2839 uint16_t m;
2840 uint16_t n;
2841 uint8_t o;
2842 uint16_t p;
2843 uint16_t q;
2844 if (sp->sof_log != 0)
2845 {
2846 TIFFErrorExtR(tif, module, "Corrupt JPEG data");
2847 return (0);
2848 }
2849 if (sp->subsamplingcorrect == 0)
2850 sp->sof_marker_id = marker_id;
2851 /* Lf: data length */
2852 if (OJPEGReadWord(sp, &m) == 0)
2853 return (0);
2854 if (m < 11)
2855 {
2856 if (sp->subsamplingcorrect == 0)
2857 TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data");
2858 return (0);
2859 }
2860 m = (uint16_t)(m - 8);
2861 if (m % 3 != 0)
2862 {
2863 if (sp->subsamplingcorrect == 0)
2864 TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data");
2865 return (0);
2866 }
2867 n = m / 3;
2868 if (sp->subsamplingcorrect == 0)
2869 {
2870 if (n != sp->samples_per_pixel)
2871 {
2873 tif, module,
2874 "JPEG compressed data indicates unexpected number of samples");
2875 return (0);
2876 }
2877 }
2878 /* P: Sample precision */
2879 if (OJPEGReadByte(sp, &o) == 0)
2880 return (0);
2881 if (o != 8)
2882 {
2883 if (sp->subsamplingcorrect == 0)
2884 TIFFErrorExtR(tif, module,
2885 "JPEG compressed data indicates unexpected number of "
2886 "bits per sample");
2887 return (0);
2888 }
2889 /* Y: Number of lines, X: Number of samples per line */
2890 if (sp->subsamplingcorrect)
2891 OJPEGReadSkip(sp, 4);
2892 else
2893 {
2894 /* Y: Number of lines */
2895 if (OJPEGReadWord(sp, &p) == 0)
2896 return (0);
2897 if (((uint32_t)p < sp->image_length) &&
2898 ((uint32_t)p < sp->strile_length_total))
2899 {
2900 TIFFErrorExtR(tif, module,
2901 "JPEG compressed data indicates unexpected height");
2902 return (0);
2903 }
2904 sp->sof_y = p;
2905 /* X: Number of samples per line */
2906 if (OJPEGReadWord(sp, &p) == 0)
2907 return (0);
2908 if (((uint32_t)p < sp->image_width) && ((uint32_t)p < sp->strile_width))
2909 {
2910 TIFFErrorExtR(tif, module,
2911 "JPEG compressed data indicates unexpected width");
2912 return (0);
2913 }
2914 if ((uint32_t)p > sp->strile_width)
2915 {
2916 TIFFErrorExtR(tif, module,
2917 "JPEG compressed data image width exceeds expected "
2918 "image width");
2919 return (0);
2920 }
2921 sp->sof_x = p;
2922 }
2923 /* Nf: Number of image components in frame */
2924 if (OJPEGReadByte(sp, &o) == 0)
2925 return (0);
2926 if (o != n)
2927 {
2928 if (sp->subsamplingcorrect == 0)
2929 TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data");
2930 return (0);
2931 }
2932 /* per component stuff */
2933 /* TODO: double-check that flow implies that n cannot be as big as to make
2934 * us overflow sof_c, sof_hv and sof_tq arrays */
2935 for (q = 0; q < n; q++)
2936 {
2937 /* C: Component identifier */
2938 if (OJPEGReadByte(sp, &o) == 0)
2939 return (0);
2940 if (sp->subsamplingcorrect == 0)
2941 sp->sof_c[q] = o;
2942 /* H: Horizontal sampling factor, and V: Vertical sampling factor */
2943 if (OJPEGReadByte(sp, &o) == 0)
2944 return (0);
2945 if (sp->subsamplingcorrect != 0)
2946 {
2947 if (q == 0)
2948 {
2949 sp->subsampling_hor = (o >> 4);
2950 sp->subsampling_ver = (o & 15);
2951 if (((sp->subsampling_hor != 1) && (sp->subsampling_hor != 2) &&
2952 (sp->subsampling_hor != 4)) ||
2953 ((sp->subsampling_ver != 1) && (sp->subsampling_ver != 2) &&
2954 (sp->subsampling_ver != 4)))
2955 sp->subsampling_force_desubsampling_inside_decompression =
2956 1;
2957 }
2958 else
2959 {
2960 if (o != 17)
2961 sp->subsampling_force_desubsampling_inside_decompression =
2962 1;
2963 }
2964 }
2965 else
2966 {
2967 sp->sof_hv[q] = o;
2968 if (sp->subsampling_force_desubsampling_inside_decompression == 0)
2969 {
2970 if (q == 0)
2971 {
2972 if (o != ((sp->subsampling_hor << 4) | sp->subsampling_ver))
2973 {
2974 TIFFErrorExtR(tif, module,
2975 "JPEG compressed data indicates "
2976 "unexpected subsampling values");
2977 return (0);
2978 }
2979 }
2980 else
2981 {
2982 if (o != 17)
2983 {
2984 TIFFErrorExtR(tif, module,
2985 "JPEG compressed data indicates "
2986 "unexpected subsampling values");
2987 return (0);
2988 }
2989 }
2990 }
2991 }
2992 /* Tq: Quantization table destination selector */
2993 if (OJPEGReadByte(sp, &o) == 0)
2994 return (0);
2995 if (sp->subsamplingcorrect == 0)
2996 sp->sof_tq[q] = o;
2997 }
2998 if (sp->subsamplingcorrect == 0)
2999 sp->sof_log = 1;
3000 return (1);
3001}
3002
3019{
3020 /* this marker needs to be checked, and part of its data needs to be saved
3021 * for regeneration later on */
3022 static const char module[] = "OJPEGReadHeaderInfoSecStreamSos";
3023 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3024 uint16_t m;
3025 uint8_t n;
3026 uint8_t o;
3027 assert(sp->subsamplingcorrect == 0);
3028 if (sp->sof_log == 0)
3029 {
3030 TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data");
3031 return (0);
3032 }
3033 /* Ls */
3034 if (OJPEGReadWord(sp, &m) == 0)
3035 return (0);
3036 if (m != 6 + sp->samples_per_pixel_per_plane * 2)
3037 {
3038 TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data");
3039 return (0);
3040 }
3041 /* Ns */
3042 if (OJPEGReadByte(sp, &n) == 0)
3043 return (0);
3044 if (n != sp->samples_per_pixel_per_plane)
3045 {
3046 TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data");
3047 return (0);
3048 }
3049 /* Cs, Td, and Ta */
3050 for (o = 0; o < sp->samples_per_pixel_per_plane; o++)
3051 {
3052 /* Cs */
3053 if (OJPEGReadByte(sp, &n) == 0)
3054 return (0);
3055 sp->sos_cs[sp->plane_sample_offset + o] = n;
3056 /* Td and Ta */
3057 if (OJPEGReadByte(sp, &n) == 0)
3058 return (0);
3059 sp->sos_tda[sp->plane_sample_offset + o] = n;
3060 }
3061 /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as
3062 * per LibJpeg source */
3063 OJPEGReadSkip(sp, 3);
3064 return (1);
3065}
3066
3085{
3086 static const char module[] = "OJPEGReadHeaderInfoSecTablesQTable";
3087 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3088 uint8_t m;
3089 uint8_t n;
3090 uint32_t oa;
3091 uint8_t *ob;
3092 uint32_t p;
3093 if (sp->qtable_offset[0] == 0)
3094 {
3095 TIFFErrorExtR(tif, module, "Missing JPEG tables");
3096 return (0);
3097 }
3098 sp->in_buffer_file_pos_log = 0;
3099 for (m = 0; m < sp->samples_per_pixel; m++)
3100 {
3101 if ((sp->qtable_offset[m] != 0) &&
3102 ((m == 0) || (sp->qtable_offset[m] != sp->qtable_offset[m - 1])))
3103 {
3104 for (n = 0; n < m - 1; n++)
3105 {
3106 if (sp->qtable_offset[m] == sp->qtable_offset[n])
3107 {
3108 TIFFErrorExtR(tif, module, "Corrupt JpegQTables tag value");
3109 return (0);
3110 }
3111 }
3112 oa = sizeof(uint32_t) + 69;
3113 ob = (uint8_t *)_TIFFmallocExt(tif, oa);
3114 if (ob == 0)
3115 {
3116 TIFFErrorExtR(tif, module, "Out of memory");
3117 return (0);
3118 }
3119 *(uint32_t *)ob = oa;
3120 ob[sizeof(uint32_t)] = 255;
3121 ob[sizeof(uint32_t) + 1] = JPEG_MARKER_DQT;
3122 ob[sizeof(uint32_t) + 2] = 0;
3123 ob[sizeof(uint32_t) + 3] = 67;
3124 ob[sizeof(uint32_t) + 4] = m;
3125 TIFFSeekFile(tif, sp->qtable_offset[m], SEEK_SET);
3126 p = (uint32_t)TIFFReadFile(tif, &ob[sizeof(uint32_t) + 5], 64);
3127 if (p != 64)
3128 {
3129 _TIFFfreeExt(tif, ob);
3130 return (0);
3131 }
3132 if (sp->qtable[m] != 0)
3133 _TIFFfreeExt(tif, sp->qtable[m]);
3134 sp->qtable[m] = ob;
3135 sp->sof_tq[m] = m;
3136 }
3137 else
3138 sp->sof_tq[m] = sp->sof_tq[m - 1];
3139 }
3140 return (1);
3141}
3142
3161{
3162 static const char module[] = "OJPEGReadHeaderInfoSecTablesDcTable";
3163 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3164 uint8_t m;
3165 uint8_t n;
3166 uint8_t o[16];
3167 uint32_t p;
3168 uint32_t q;
3169 uint32_t ra;
3170 uint8_t *rb;
3171 if (sp->dctable_offset[0] == 0)
3172 {
3173 TIFFErrorExtR(tif, module, "Missing JPEG tables");
3174 return (0);
3175 }
3176 sp->in_buffer_file_pos_log = 0;
3177 for (m = 0; m < sp->samples_per_pixel; m++)
3178 {
3179 if ((sp->dctable_offset[m] != 0) &&
3180 ((m == 0) || (sp->dctable_offset[m] != sp->dctable_offset[m - 1])))
3181 {
3182 for (n = 0; n < m - 1; n++)
3183 {
3184 if (sp->dctable_offset[m] == sp->dctable_offset[n])
3185 {
3186 TIFFErrorExtR(tif, module,
3187 "Corrupt JpegDcTables tag value");
3188 return (0);
3189 }
3190 }
3191 TIFFSeekFile(tif, sp->dctable_offset[m], SEEK_SET);
3192 p = (uint32_t)TIFFReadFile(tif, o, 16);
3193 if (p != 16)
3194 return (0);
3195 q = 0;
3196 for (n = 0; n < 16; n++)
3197 q += o[n];
3198 ra = (uint32_t)(sizeof(uint32_t) + 21 + q);
3199 rb = (uint8_t *)_TIFFmallocExt(tif, ra);
3200 if (rb == 0)
3201 {
3202 TIFFErrorExtR(tif, module, "Out of memory");
3203 return (0);
3204 }
3205 *(uint32_t *)rb = ra;
3206 rb[sizeof(uint32_t)] = 255;
3207 rb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT;
3208 rb[sizeof(uint32_t) + 2] = (uint8_t)((19 + q) >> 8);
3209 rb[sizeof(uint32_t) + 3] = ((19 + q) & 255);
3210 rb[sizeof(uint32_t) + 4] = m;
3211 for (n = 0; n < 16; n++)
3212 rb[sizeof(uint32_t) + 5 + n] = o[n];
3213 p = (uint32_t)TIFFReadFile(tif, &(rb[sizeof(uint32_t) + 21]), q);
3214 if (p != q)
3215 {
3216 _TIFFfreeExt(tif, rb);
3217 return (0);
3218 }
3219 if (sp->dctable[m] != 0)
3220 _TIFFfreeExt(tif, sp->dctable[m]);
3221 sp->dctable[m] = rb;
3222 sp->sos_tda[m] = (uint8_t)(m << 4);
3223 }
3224 else
3225 sp->sos_tda[m] = sp->sos_tda[m - 1];
3226 }
3227 return (1);
3228}
3229
3248{
3249 static const char module[] = "OJPEGReadHeaderInfoSecTablesAcTable";
3250 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3251 uint8_t m;
3252 uint8_t n;
3253 uint8_t o[16];
3254 uint32_t p;
3255 uint32_t q;
3256 uint32_t ra;
3257 uint8_t *rb;
3258 if (sp->actable_offset[0] == 0)
3259 {
3260 TIFFErrorExtR(tif, module, "Missing JPEG tables");
3261 return (0);
3262 }
3263 sp->in_buffer_file_pos_log = 0;
3264 for (m = 0; m < sp->samples_per_pixel; m++)
3265 {
3266 if ((sp->actable_offset[m] != 0) &&
3267 ((m == 0) || (sp->actable_offset[m] != sp->actable_offset[m - 1])))
3268 {
3269 for (n = 0; n < m - 1; n++)
3270 {
3271 if (sp->actable_offset[m] == sp->actable_offset[n])
3272 {
3273 TIFFErrorExtR(tif, module,
3274 "Corrupt JpegAcTables tag value");
3275 return (0);
3276 }
3277 }
3278 TIFFSeekFile(tif, sp->actable_offset[m], SEEK_SET);
3279 p = (uint32_t)TIFFReadFile(tif, o, 16);
3280 if (p != 16)
3281 return (0);
3282 q = 0;
3283 for (n = 0; n < 16; n++)
3284 q += o[n];
3285 ra = (uint32_t)(sizeof(uint32_t) + 21 + q);
3286 rb = (uint8_t *)_TIFFmallocExt(tif, ra);
3287 if (rb == 0)
3288 {
3289 TIFFErrorExtR(tif, module, "Out of memory");
3290 return (0);
3291 }
3292 *(uint32_t *)rb = ra;
3293 rb[sizeof(uint32_t)] = 255;
3294 rb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT;
3295 rb[sizeof(uint32_t) + 2] = (uint8_t)((19 + q) >> 8);
3296 rb[sizeof(uint32_t) + 3] = ((19 + q) & 255);
3297 rb[sizeof(uint32_t) + 4] = (16 | m);
3298 for (n = 0; n < 16; n++)
3299 rb[sizeof(uint32_t) + 5 + n] = o[n];
3300 p = (uint32_t)TIFFReadFile(tif, &(rb[sizeof(uint32_t) + 21]), q);
3301 if (p != q)
3302 {
3303 _TIFFfreeExt(tif, rb);
3304 return (0);
3305 }
3306 if (sp->actable[m] != 0)
3307 _TIFFfreeExt(tif, sp->actable[m]);
3308 sp->actable[m] = rb;
3309 sp->sos_tda[m] = (sp->sos_tda[m] | m);
3310 }
3311 else
3312 sp->sos_tda[m] = (sp->sos_tda[m] | (sp->sos_tda[m - 1] & 15));
3313 }
3314 return (1);
3315}
3316
3335{
3336 uint16_t m;
3337 tmsize_t n;
3338 /* TODO: double-check: when subsamplingcorrect is set, no call to
3339 * TIFFErrorExt or TIFFWarningExt should be made in any other case, seek or
3340 * read errors should be passed through */
3341 do
3342 {
3343 if (sp->in_buffer_file_togo != 0)
3344 {
3345 if (sp->in_buffer_file_pos_log == 0)
3346 {
3347 TIFFSeekFile(sp->tif, sp->in_buffer_file_pos, SEEK_SET);
3348 sp->in_buffer_file_pos_log = 1;
3349 }
3350 m = OJPEG_BUFFER;
3351 if ((uint64_t)m > sp->in_buffer_file_togo)
3352 m = (uint16_t)sp->in_buffer_file_togo;
3353 n = TIFFReadFile(sp->tif, sp->in_buffer, (tmsize_t)m);
3354 if (n <= 0)
3355 return (0);
3356 assert(n > 0);
3357 assert(n <= OJPEG_BUFFER);
3358 assert(n < 65536);
3359 assert((uint64_t)n <= sp->in_buffer_file_togo);
3360 m = (uint16_t)n;
3361 sp->in_buffer_togo = m;
3362 sp->in_buffer_cur = sp->in_buffer;
3363 sp->in_buffer_file_togo -= m;
3364 sp->in_buffer_file_pos += m;
3365 break;
3366 }
3367 sp->in_buffer_file_pos_log = 0;
3368 switch (sp->in_buffer_source)
3369 {
3370 case osibsNotSetYet:
3371 if (sp->jpeg_interchange_format != 0)
3372 {
3376 }
3378 break;
3381 break;
3382 case osibsStrile:
3385 else
3386 {
3387 int err = 0;
3389 sp->tif, sp->in_buffer_next_strile, &err);
3390 if (err)
3391 return 0;
3392 if (sp->in_buffer_file_pos != 0)
3393 {
3394 uint64_t bytecount = TIFFGetStrileByteCountWithErr(
3395 sp->tif, sp->in_buffer_next_strile, &err);
3396 if (err)
3397 return 0;
3398 if (sp->in_buffer_file_pos >= sp->file_size)
3399 sp->in_buffer_file_pos = 0;
3400 else if (bytecount == 0)
3402 sp->file_size - sp->in_buffer_file_pos;
3403 else
3404 {
3405 sp->in_buffer_file_togo = bytecount;
3406 if (sp->in_buffer_file_togo == 0)
3407 sp->in_buffer_file_pos = 0;
3408 else if (sp->in_buffer_file_pos >
3409 UINT64_MAX - sp->in_buffer_file_togo ||
3410 sp->in_buffer_file_pos +
3412 sp->file_size)
3414 sp->file_size - sp->in_buffer_file_pos;
3415 }
3416 }
3418 }
3419 break;
3420 case osibsEof:
3421 default:
3422 return (0);
3423 }
3424 } while (1);
3425 return (1);
3426}
3427
3441static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte)
3442{
3443 if (sp->in_buffer_togo == 0)
3444 {
3445 if (OJPEGReadBufferFill(sp) == 0)
3446 return (0);
3447 assert(sp->in_buffer_togo > 0);
3448 }
3449 *byte = *(sp->in_buffer_cur);
3450 sp->in_buffer_cur++;
3451 sp->in_buffer_togo--;
3452 return (1);
3453}
3454
3469static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte)
3470{
3471 if (sp->in_buffer_togo == 0)
3472 {
3473 if (OJPEGReadBufferFill(sp) == 0)
3474 return (0);
3475 assert(sp->in_buffer_togo > 0);
3476 }
3477 *byte = *(sp->in_buffer_cur);
3478 return (1);
3479}
3480
3493{
3494 assert(sp->in_buffer_togo > 0);
3495 sp->in_buffer_cur++;
3496 sp->in_buffer_togo--;
3497}
3498
3514static int OJPEGReadWord(OJPEGState *sp, uint16_t *word)
3515{
3516 uint8_t m;
3517 if (OJPEGReadByte(sp, &m) == 0)
3518 return (0);
3519 *word = (uint16_t)(m << 8);
3520 if (OJPEGReadByte(sp, &m) == 0)
3521 return (0);
3522 *word |= m;
3523 return (1);
3524}
3525
3541static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem)
3542{
3543 uint16_t mlen;
3544 uint8_t *mmem;
3545 uint16_t n;
3546 assert(len > 0);
3547 mlen = len;
3548 mmem = (uint8_t *)mem;
3549 do
3550 {
3551 if (sp->in_buffer_togo == 0)
3552 {
3553 if (OJPEGReadBufferFill(sp) == 0)
3554 return (0);
3555 assert(sp->in_buffer_togo > 0);
3556 }
3557 n = mlen;
3558 if (n > sp->in_buffer_togo)
3559 n = sp->in_buffer_togo;
3560 _TIFFmemcpy(mmem, sp->in_buffer_cur, n);
3561 sp->in_buffer_cur += n;
3562 sp->in_buffer_togo = (uint16_t)(sp->in_buffer_togo - n);
3563 mlen = (uint16_t)(mlen - n);
3564 mmem += n;
3565 } while (mlen > 0);
3566 return (1);
3567}
3568
3582static void OJPEGReadSkip(OJPEGState *sp, uint16_t len)
3583{
3584 uint16_t m;
3585 uint16_t n;
3586 m = len;
3587 n = m;
3588 if (n > sp->in_buffer_togo)
3589 n = sp->in_buffer_togo;
3590 sp->in_buffer_cur += n;
3591 sp->in_buffer_togo = (uint16_t)(sp->in_buffer_togo - n);
3592 m = (uint16_t)(m - n);
3593 if (m > 0)
3594 {
3595 assert(sp->in_buffer_togo == 0);
3596 n = m;
3597 if ((uint64_t)n > sp->in_buffer_file_togo)
3598 n = (uint16_t)sp->in_buffer_file_togo;
3599 sp->in_buffer_file_pos += n;
3600 sp->in_buffer_file_togo -= n;
3601 sp->in_buffer_file_pos_log = 0;
3602 /* we don't skip past jpeginterchangeformat/strile block...
3603 * if that is asked from us, we're dealing with totally bazurk
3604 * data anyway, and we've not seen this happening on any
3605 * testfile, so we might as well likely cause some other
3606 * meaningless error to be passed at some later time
3607 */
3608 }
3609}
3610
3629static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len)
3630{
3631 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3632 *len = 0;
3633 do
3634 {
3635 assert(sp->out_state <= ososEoi);
3636 switch (sp->out_state)
3637 {
3638 case ososSoi:
3639 OJPEGWriteStreamSoi(tif, mem, len);
3640 break;
3641 case ososQTable0:
3642 OJPEGWriteStreamQTable(tif, 0, mem, len);
3643 break;
3644 case ososQTable1:
3645 OJPEGWriteStreamQTable(tif, 1, mem, len);
3646 break;
3647 case ososQTable2:
3648 OJPEGWriteStreamQTable(tif, 2, mem, len);
3649 break;
3650 case ososQTable3:
3651 OJPEGWriteStreamQTable(tif, 3, mem, len);
3652 break;
3653 case ososDcTable0:
3654 OJPEGWriteStreamDcTable(tif, 0, mem, len);
3655 break;
3656 case ososDcTable1:
3657 OJPEGWriteStreamDcTable(tif, 1, mem, len);
3658 break;
3659 case ososDcTable2:
3660 OJPEGWriteStreamDcTable(tif, 2, mem, len);
3661 break;
3662 case ososDcTable3:
3663 OJPEGWriteStreamDcTable(tif, 3, mem, len);
3664 break;
3665 case ososAcTable0:
3666 OJPEGWriteStreamAcTable(tif, 0, mem, len);
3667 break;
3668 case ososAcTable1:
3669 OJPEGWriteStreamAcTable(tif, 1, mem, len);
3670 break;
3671 case ososAcTable2:
3672 OJPEGWriteStreamAcTable(tif, 2, mem, len);
3673 break;
3674 case ososAcTable3:
3675 OJPEGWriteStreamAcTable(tif, 3, mem, len);
3676 break;
3677 case ososDri:
3678 OJPEGWriteStreamDri(tif, mem, len);
3679 break;
3680 case ososSof:
3681 OJPEGWriteStreamSof(tif, mem, len);
3682 break;
3683 case ososSos:
3684 OJPEGWriteStreamSos(tif, mem, len);
3685 break;
3686 case ososCompressed:
3687 if (OJPEGWriteStreamCompressed(tif, mem, len) == 0)
3688 return (0);
3689 break;
3690 case ososRst:
3691 OJPEGWriteStreamRst(tif, mem, len);
3692 break;
3693 case ososEoi:
3694 OJPEGWriteStreamEoi(tif, mem, len);
3695 break;
3696 default:
3697 break;
3698 }
3699 } while (*len == 0);
3700 return (1);
3701}
3702
3715static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len)
3716{
3717 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3718 assert(OJPEG_BUFFER >= 2);
3719 sp->out_buffer[0] = 255;
3720 sp->out_buffer[1] = JPEG_MARKER_SOI;
3721 *len = 2;
3722 *mem = (void *)sp->out_buffer;
3723 sp->out_state = (OJPEGStateOutState)(sp->out_state + 1);
3724}
3725
3743static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem,
3744 uint32_t *len)
3745{
3746 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3747 if (sp->qtable[table_index] != 0)
3748 {
3749 *mem = (void *)(sp->qtable[table_index] + sizeof(uint32_t));
3750 *len = (uint32_t)(*((uint32_t *)sp->qtable[table_index]) -
3751 sizeof(uint32_t));
3752 }
3753 sp->out_state = (OJPEGStateOutState)(sp->out_state + 1);
3754}
3755
3773static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem,
3774 uint32_t *len)
3775{
3776 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3777 if (sp->dctable[table_index] != 0)
3778 {
3779 *mem = (void *)(sp->dctable[table_index] + sizeof(uint32_t));
3780 *len = (uint32_t)(*((uint32_t *)sp->dctable[table_index]) -
3781 sizeof(uint32_t));
3782 }
3783 sp->out_state = (OJPEGStateOutState)(sp->out_state + 1);
3784}
3785
3803static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem,
3804 uint32_t *len)
3805{
3806 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3807 if (sp->actable[table_index] != 0)
3808 {
3809 *mem = (void *)(sp->actable[table_index] + sizeof(uint32_t));
3810 *len = (uint32_t)(*((uint32_t *)sp->actable[table_index]) -
3811 sizeof(uint32_t));
3812 }
3813 sp->out_state = (OJPEGStateOutState)(sp->out_state + 1);
3814}
3815
3831static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len)
3832{
3833 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3834 assert(OJPEG_BUFFER >= 6);
3835 if (sp->restart_interval != 0)
3836 {
3837 sp->out_buffer[0] = 255;
3838 sp->out_buffer[1] = JPEG_MARKER_DRI;
3839 sp->out_buffer[2] = 0;
3840 sp->out_buffer[3] = 4;
3841 sp->out_buffer[4] = (uint8_t)(sp->restart_interval >> 8);
3842 sp->out_buffer[5] = (uint8_t)(sp->restart_interval & 255);
3843 *len = 6;
3844 *mem = (void *)sp->out_buffer;
3845 }
3846 sp->out_state = (OJPEGStateOutState)(sp->out_state + 1);
3847}
3848
3863static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len)
3864{
3865 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3866 uint8_t m;
3867 assert(OJPEG_BUFFER >= 2 + 8 + sp->samples_per_pixel_per_plane * 3);
3868 assert(255 >= 8 + sp->samples_per_pixel_per_plane * 3);
3869 sp->out_buffer[0] = 255;
3870 sp->out_buffer[1] = sp->sof_marker_id;
3871 /* Lf */
3872 sp->out_buffer[2] = 0;
3873 sp->out_buffer[3] = (uint8_t)(8 + sp->samples_per_pixel_per_plane * 3);
3874 /* P */
3875 sp->out_buffer[4] = 8;
3876 /* Y */
3877 sp->out_buffer[5] = (uint8_t)(sp->sof_y >> 8);
3878 sp->out_buffer[6] = (uint8_t)(sp->sof_y & 255);
3879 /* X */
3880 sp->out_buffer[7] = (uint8_t)(sp->sof_x >> 8);
3881 sp->out_buffer[8] = (uint8_t)(sp->sof_x & 255);
3882 /* Nf */
3884 for (m = 0; m < sp->samples_per_pixel_per_plane; m++)
3885 {
3886 /* C */
3887 sp->out_buffer[10 + m * 3] = sp->sof_c[sp->plane_sample_offset + m];
3888 /* H and V */
3889 sp->out_buffer[10 + m * 3 + 1] =
3890 sp->sof_hv[sp->plane_sample_offset + m];
3891 /* Tq */
3892 sp->out_buffer[10 + m * 3 + 2] =
3893 sp->sof_tq[sp->plane_sample_offset + m];
3894 }
3895 *len = 10 + (uint32_t)sp->samples_per_pixel_per_plane * 3;
3896 *mem = (void *)sp->out_buffer;
3897 sp->out_state = (OJPEGStateOutState)(sp->out_state + 1);
3898}
3899
3914static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len)
3915{
3916 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3917 uint8_t m;
3918 assert(OJPEG_BUFFER >= 2 + 6 + sp->samples_per_pixel_per_plane * 2);
3919 assert(255 >= 6 + sp->samples_per_pixel_per_plane * 2);
3920 sp->out_buffer[0] = 255;
3921 sp->out_buffer[1] = JPEG_MARKER_SOS;
3922 /* Ls */
3923 sp->out_buffer[2] = 0;
3924 sp->out_buffer[3] = (uint8_t)(6 + sp->samples_per_pixel_per_plane * 2);
3925 /* Ns */
3927 for (m = 0; m < sp->samples_per_pixel_per_plane; m++)
3928 {
3929 /* Cs */
3930 sp->out_buffer[5 + m * 2] = sp->sos_cs[sp->plane_sample_offset + m];
3931 /* Td and Ta */
3932 sp->out_buffer[5 + m * 2 + 1] =
3933 sp->sos_tda[sp->plane_sample_offset + m];
3934 }
3935 /* Ss */
3936 sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2] = 0;
3937 /* Se */
3938 sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2 + 1] = 63;
3939 /* Ah and Al */
3940 sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2 + 2] = 0;
3941 *len = 8 + (uint32_t)sp->samples_per_pixel_per_plane * 2;
3942 *mem = (void *)sp->out_buffer;
3943 sp->out_state = (OJPEGStateOutState)(sp->out_state + 1);
3944}
3945
3963static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len)
3964{
3965 OJPEGState *sp = (OJPEGState *)tif->tif_data;
3966 if (sp->in_buffer_togo == 0)
3967 {
3968 if (OJPEGReadBufferFill(sp) == 0)
3969 return (0);
3970 assert(sp->in_buffer_togo > 0);
3971 }
3972 *len = sp->in_buffer_togo;
3973 *mem = (void *)sp->in_buffer_cur;
3974 sp->in_buffer_togo = 0;
3975 if (sp->in_buffer_file_togo == 0)
3976 {
3977 switch (sp->in_buffer_source)
3978 {
3979 case osibsStrile:
3981 sp->out_state = ososRst;
3982 else
3983 sp->out_state = ososEoi;
3984 break;
3985 case osibsEof:
3986 sp->out_state = ososEoi;
3987 break;
3988 case osibsNotSetYet:
3990 default:
3991 break;
3992 }
3993 }
3994 return (1);
3995}
3996
4010static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len)
4011{
4012 OJPEGState *sp = (OJPEGState *)tif->tif_data;
4013 assert(OJPEG_BUFFER >= 2);
4014 sp->out_buffer[0] = 255;
4015 sp->out_buffer[1] = (uint8_t)(JPEG_MARKER_RST0 + sp->restart_index);
4016 sp->restart_index++;
4017 if (sp->restart_index == 8)
4018 sp->restart_index = 0;
4019 *len = 2;
4020 *mem = (void *)sp->out_buffer;
4022}
4023
4036static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len)
4037{
4038 OJPEGState *sp = (OJPEGState *)tif->tif_data;
4039 assert(OJPEG_BUFFER >= 2);
4040 sp->out_buffer[0] = 255;
4041 sp->out_buffer[1] = JPEG_MARKER_EOI;
4042 *len = 2;
4043 *mem = (void *)sp->out_buffer;
4044}
4045
4046#ifndef LIBJPEG_ENCAP_EXTERNAL
4049{
4050 if (SETJMP(sp->exit_jmpbuf))
4051 return 0;
4052 else
4053 {
4054 jpeg_create_decompress(cinfo);
4055 return 1;
4056 }
4057}
4058#endif
4059
4060#ifndef LIBJPEG_ENCAP_EXTERNAL
4063 uint8_t require_image)
4064{
4065 if (SETJMP(sp->exit_jmpbuf))
4066 return 0;
4067 else
4068 {
4069 jpeg_read_header(cinfo, require_image);
4070 return 1;
4071 }
4072}
4073#endif
4074
4075#ifndef LIBJPEG_ENCAP_EXTERNAL
4078{
4079 if (SETJMP(sp->exit_jmpbuf))
4080 return 0;
4081 else
4082 {
4083 jpeg_start_decompress(cinfo);
4084 return 1;
4085 }
4086}
4087#endif
4088
4089#ifndef LIBJPEG_ENCAP_EXTERNAL
4092 void *scanlines, uint32_t max_lines)
4093{
4094 if (SETJMP(sp->exit_jmpbuf))
4095 return 0;
4096 else
4097 {
4098 jpeg_read_scanlines(cinfo, (JSAMPARRAY)scanlines, max_lines);
4099 return 1;
4100 }
4101}
4102#endif
4103
4104#ifndef LIBJPEG_ENCAP_EXTERNAL
4107 void *data, uint32_t max_lines)
4108{
4109 if (SETJMP(sp->exit_jmpbuf))
4110 return 0;
4111 else
4112 {
4113 jpeg_read_raw_data(cinfo, (JSAMPIMAGE)data, max_lines);
4114 return 1;
4115 }
4116}
4117#endif
4118
4119#ifndef LIBJPEG_ENCAP_EXTERNAL
4120static void jpeg_encap_unwind(TIFF *tif)
4121{
4122 OJPEGState *sp = (OJPEGState *)tif->tif_data;
4123 LONGJMP(sp->exit_jmpbuf, 1);
4124}
4125#endif
4126
4138static void
4140{
4141 char buffer[JMSG_LENGTH_MAX];
4142 (*cinfo->err->format_message)(cinfo, buffer);
4143 TIFFWarningExtR(((TIFF *)(cinfo->client_data)), "LibJpeg", "%s", buffer);
4144}
4145
4158{
4159 char buffer[JMSG_LENGTH_MAX];
4160 (*cinfo->err->format_message)(cinfo, buffer);
4161 TIFFErrorExtR(((TIFF *)(cinfo->client_data)), "LibJpeg", "%s", buffer);
4162 jpeg_encap_unwind((TIFF *)(cinfo->client_data));
4163}
4164
4174static void
4179
4194static boolean
4196{
4197 TIFF *tif = (TIFF *)cinfo->client_data;
4198 OJPEGState *sp = (OJPEGState *)tif->tif_data;
4199 void *mem = 0;
4200 uint32_t len = 0U;
4201 if (OJPEGWriteStream(tif, &mem, &len) == 0)
4202 {
4203 TIFFErrorExtR(tif, "LibJpeg", "Premature end of JPEG data");
4204 jpeg_encap_unwind(tif);
4205 }
4206 sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = len;
4207 sp->libjpeg_jpeg_source_mgr.next_input_byte = (const JOCTET *)mem;
4208 return (1);
4209}
4210
4222static void
4224 long num_bytes)
4225{
4226 TIFF *tif = (TIFF *)cinfo->client_data;
4227 (void)num_bytes;
4228 TIFFErrorExtR(tif, "LibJpeg", "Unexpected error");
4229 jpeg_encap_unwind(tif);
4230}
4231
4232#ifdef _MSC_VER
4233#pragma warning(push)
4234#pragma warning(disable : 4702) /* unreachable code */
4235#endif
4249static boolean
4251 int desired)
4252{
4253 TIFF *tif = (TIFF *)cinfo->client_data;
4254 (void)desired;
4255 TIFFErrorExtR(tif, "LibJpeg", "Unexpected error");
4256 jpeg_encap_unwind(tif);
4257 return (0);
4258}
4259#ifdef _MSC_VER
4260#pragma warning(pop)
4261#endif
4262
4272static void
4277
4278#endif
uint32_t _TIFFMultiply32(TIFF *tif, uint32_t first, uint32_t second, const char *where)
Multiply two unsigned values with an overflow check.
Definition tif_aux.c:40
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 TIFFSetFieldBit(tif, field)
Set a directory presence bit.
Definition tif_dir.h:675
@ TIFF_SETGET_UINT64
Unsigned 64-bit scalar storage.
Definition tif_dir.h:752
@ TIFF_SETGET_UINT16
Unsigned 16-bit scalar storage.
Definition tif_dir.h:744
@ TIFF_SETGET_C32_UINT64
Unsigned 64-bit array with 32-bit explicit count.
Definition tif_dir.h:828
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
void * _TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
Allocate a zero-filled array under a handle's allocation policy.
Definition tif_open.c:224
static int OJPEGSetupDecode(TIFF *tif)
Warn that old-style JPEG compression is deprecated.
Definition tif_ojpeg.c:1338
#define FIELD_OJPEG_JPEGACTABLES
TIFFField field bit for the codec-private JpegAcTables tag.
Definition tif_ojpeg.c:343
#define JPEG_MARKER_SOS
JPEG SOS (start of scan) marker code.
Definition tif_ojpeg.c:268
static int OJPEGPreDecode(TIFF *tif, uint16_t s)
Position a reconstructed JPEG session at the requested strile and plane.
Definition tif_ojpeg.c:1368
static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc)
Decode complete ordinary sample rows.
Definition tif_ojpeg.c:1701
static boolean OJPEGLibjpegJpegSourceMgrFillInputBuffer(tiff_ojpeg_decompress_struct *cinfo)
Supply the next reconstructed JPEG chunk to libjpeg.
Definition tif_ojpeg.c:4195
struct jpeg_decompress_struct tiff_ojpeg_decompress_struct
Alias for the libjpeg decompressor context structure.
Definition tif_ojpeg.c:430
static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem, uint32_t *len)
Produce a AcTable reconstruction chunk.
Definition tif_ojpeg.c:3803
static void OJPEGLibjpegJpegSourceMgrSkipInputData(tiff_ojpeg_decompress_struct *cinfo, long num_bytes)
Reject an unexpected dependency skip request.
Definition tif_ojpeg.c:4223
static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte)
Peek at the next buffered byte.
Definition tif_ojpeg.c:3469
#define LONGJMP(jbuf, code)
Jump back to the frame saved by SETJMP (plain longjmp); never returns.
Definition tif_ojpeg.c:184
OJPEGStateOutState
Next item to emit in the reconstructed JPEG stream.
Definition tif_ojpeg.c:459
static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len)
Produce a Rst reconstruction chunk.
Definition tif_ojpeg.c:4010
static int OJPEGSetupEncode(TIFF *tif)
Reject unsupported old-style JPEG encoding.
Definition tif_ojpeg.c:1774
static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len)
Produce a Soi reconstruction chunk.
Definition tif_ojpeg.c:3715
static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
Advance the cached strile count and end a completed plane session.
Definition tif_ojpeg.c:1743
static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF *tif)
Build AC Huffman markers from TIFF table-offset tags.
Definition tif_ojpeg.c:3247
static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte)
Consume one buffered byte.
Definition tif_ojpeg.c:3441
static int OJPEGFixupTags(TIFF *tif)
Accept the old-style JPEG directory without eager fixups.
Definition tif_ojpeg.c:1322
static int OJPEGReadBufferFill(OJPEGState *sp)
Refill the old-style JPEG input window across interchange and strile extents.
Definition tif_ojpeg.c:3334
static int OJPEGReadHeaderInfoSec(TIFF *tif)
Parse a bounded interchange/strile JPEG header or synthesize it from tags.
Definition tif_ojpeg.c:2469
#define JPEG_MARKER_DQT
JPEG DQT (define quantization table) marker code.
Definition tif_ojpeg.c:277
static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id)
Validate a JPEG frame and cache its component selectors.
Definition tif_ojpeg.c:2833
static int OJPEGReadHeaderInfoSecTablesQTable(TIFF *tif)
Build quantisation markers from TIFF table-offset tags.
Definition tif_ojpeg.c:3084
static void OJPEGLibjpegJpegErrorMgrErrorExit(tiff_ojpeg_common_struct *cinfo)
Report a fatal dependency error and unwind the active wrapper.
Definition tif_ojpeg.c:4157
static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap)
Retrieve old-style JPEG metadata or delegate to the parent getter.
Definition tif_ojpeg.c:1105
#define JPEG_MARKER_APP0
JPEG APP0 marker code; APP0..APP15 are this value plus 0..15.
Definition tif_ojpeg.c:293
static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem, uint32_t *len)
Produce a QTable reconstruction chunk.
Definition tif_ojpeg.c:3743
#define JPEG_MARKER_SOF1
JPEG SOF1 (extended sequential DCT start-of-frame) marker code.
Definition tif_ojpeg.c:220
static void OJPEGLibjpegJpegSourceMgrInitSource(tiff_ojpeg_decompress_struct *cinfo)
Accept the dependency source initialization.
Definition tif_ojpeg.c:4175
static void OJPEGReadByteAdvance(OJPEGState *sp)
Consume one already buffered byte.
Definition tif_ojpeg.c:3492
struct jpeg_common_struct tiff_ojpeg_common_struct
Alias for the libjpeg common context structure.
Definition tif_ojpeg.c:422
static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap)
Cache old-style JPEG tag values or delegate to the parent setter.
Definition tif_ojpeg.c:1169
struct jpeg_error_mgr tiff_ojpeg_error_mgr
Alias for the libjpeg error-manager structure.
Definition tif_ojpeg.c:413
static int jpeg_start_decompress_encap(OJPEGState *sp, tiff_ojpeg_decompress_struct *cinfo)
Trap dependency fatal errors while attempting to start decompression.
Definition tif_ojpeg.c:4076
static int jpeg_create_decompress_encap(OJPEGState *sp, tiff_ojpeg_decompress_struct *cinfo)
Trap dependency fatal errors while attempting to create a decompressor.
Definition tif_ojpeg.c:4047
static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem)
Copy a positive byte block from the input stream.
Definition tif_ojpeg.c:3541
static int OJPEGReadHeaderInfoSecStreamDri(TIFF *tif)
Parse and cache a JPEG restart interval.
Definition tif_ojpeg.c:2621
static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len)
Produce a Sos reconstruction chunk.
Definition tif_ojpeg.c:3914
#define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH
TIFFField field bit for the codec-private JpegInterchangeFormatLength tag.
Definition tif_ojpeg.c:319
static void OJPEGLibjpegJpegSourceMgrTermSource(tiff_ojpeg_decompress_struct *cinfo)
Accept the dependency source termination.
Definition tif_ojpeg.c:4273
#define OJPEG_BUFFER
Capacity in bytes of the embedded input and synthetic marker buffers.
Definition tif_ojpeg.c:201
static void OJPEGSubsamplingCorrect(TIFF *tif)
Probe and reconcile old-style JPEG subsampling once.
Definition tif_ojpeg.c:1932
#define JPEG_MARKER_SOI
JPEG SOI (start of image) marker code.
Definition tif_ojpeg.c:253
#define FIELD_OJPEG_JPEGPROC
TIFFField field bit for the codec-private JpegProc tag.
Definition tif_ojpeg.c:351
static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len)
Produce a Sof reconstruction chunk.
Definition tif_ojpeg.c:3863
static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len)
Produce a Eoi reconstruction chunk.
Definition tif_ojpeg.c:4036
static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
Reject unsupported old-style JPEG encoding.
Definition tif_ojpeg.c:1815
static int OJPEGReadWord(OJPEGState *sp, uint16_t *word)
Consume a big-endian sixteen-bit word.
Definition tif_ojpeg.c:3514
static void OJPEGLibjpegSessionAbort(TIFF *tif)
Destroy the active old-style JPEG dependency session.
Definition tif_ojpeg.c:2442
static int OJPEGPostEncode(TIFF *tif)
Reject unsupported old-style JPEG encoding.
Definition tif_ojpeg.c:1836
static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags)
Print cached old-style JPEG fields before the optional parent printer.
Definition tif_ojpeg.c:1270
static void jpeg_encap_unwind(TIFF *tif)
Jump to the active old-style JPEG dependency wrapper.
Definition tif_ojpeg.c:4120
#define FIELD_OJPEG_JPEGQTABLES
TIFFField field bit for the codec-private JpegQTables tag.
Definition tif_ojpeg.c:327
static int jpeg_read_scanlines_encap(OJPEGState *sp, tiff_ojpeg_decompress_struct *cinfo, void *scanlines, uint32_t max_lines)
Trap dependency fatal errors while attempting to read scanlines.
Definition tif_ojpeg.c:4090
static const TIFFField ojpegFields[]
Describe legacy JPEG tags merged into the handle.
Definition tif_ojpeg.c:368
static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem, uint32_t *len)
Produce a DcTable reconstruction chunk.
Definition tif_ojpeg.c:3773
#define JMP_BUF
Type used to store the libjpeg error jump frame.
Definition tif_ojpeg.c:192
static void OJPEGLibjpegJpegErrorMgrOutputMessage(tiff_ojpeg_common_struct *cinfo)
Route a dependency message through the handle warning handler.
Definition tif_ojpeg.c:4139
static int OJPEGReadHeaderInfoSecStreamDqt(TIFF *tif)
Capture eight-bit JPEG quantisation table markers.
Definition tif_ojpeg.c:2657
static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc)
Decode complete subsampled clump rows.
Definition tif_ojpeg.c:1616
static int OJPEGWriteHeaderInfo(TIFF *tif)
Start libjpeg with a synthesized old-style JPEG input header.
Definition tif_ojpeg.c:2231
static void OJPEGCleanup(TIFF *tif)
Release old-style JPEG state and restore parent methods.
Definition tif_ojpeg.c:1859
#define JPEG_MARKER_RST0
JPEG RST0 (restart) marker code.
Definition tif_ojpeg.c:245
static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s)
Locate and cache a later separate-plane scan header.
Definition tif_ojpeg.c:2155
int TIFFInitOJPEG(TIFF *tif, int scheme)
Install the decode-only old-style JPEG codec.
Definition tif_ojpeg.c:1020
static void OJPEGReadSkip(OJPEGState *sp, uint16_t len)
Skip up to a bounded input extent without fetching skipped bytes.
Definition tif_ojpeg.c:3582
static boolean OJPEGLibjpegJpegSourceMgrResyncToRestart(tiff_ojpeg_decompress_struct *cinfo, int desired)
Reject an unexpected dependency restart resynchronization request.
Definition tif_ojpeg.c:4250
#define JPEG_MARKER_DRI
JPEG DRI (define restart interval) marker code.
Definition tif_ojpeg.c:285
OJPEGStateInBufferSource
Source of the bytes currently read into the input buffer.
Definition tif_ojpeg.c:440
#define JPEG_MARKER_SOF0
JPEG SOF0 (baseline DCT start-of-frame) marker code.
Definition tif_ojpeg.c:212
static int OJPEGPreEncode(TIFF *tif, uint16_t s)
Reject unsupported old-style JPEG encoding.
Definition tif_ojpeg.c:1793
static int OJPEGReadHeaderInfoSecStreamDht(TIFF *tif)
Capture a JPEG Huffman marker in its selected table slot.
Definition tif_ojpeg.c:2736
#define SETJMP(jbuf)
Save the calling environment for a later LONGJMP (plain setjmp).
Definition tif_ojpeg.c:169
#define FIELD_OJPEG_JPEGDCTABLES
TIFFField field bit for the codec-private JpegDcTables tag.
Definition tif_ojpeg.c:335
static int jpeg_read_raw_data_encap(OJPEGState *sp, tiff_ojpeg_decompress_struct *cinfo, void *data, uint32_t max_lines)
Trap dependency fatal errors while attempting to read component planes.
Definition tif_ojpeg.c:4105
#define JPEG_MARKER_DHT
JPEG DHT (define Huffman table) marker code.
Definition tif_ojpeg.c:237
static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len)
Produce the next borrowed chunk of reconstructed decoder input.
Definition tif_ojpeg.c:3629
static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF *tif)
Build DC Huffman markers from TIFF table-offset tags.
Definition tif_ojpeg.c:3160
static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len)
Produce a Dri reconstruction chunk.
Definition tif_ojpeg.c:3831
#define FIELD_OJPEG_JPEGRESTARTINTERVAL
TIFFField field bit for the codec-private JpegRestartInterval tag.
Definition tif_ojpeg.c:359
static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len)
Produce a Compressed reconstruction chunk.
Definition tif_ojpeg.c:3963
static int OJPEGPreDecodeSkipScanlines(TIFF *tif)
Decode and discard one strile in scanlines mode.
Definition tif_ojpeg.c:1502
static int OJPEGPreDecodeSkipRaw(TIFF *tif)
Decode and discard one strile in raw mode.
Definition tif_ojpeg.c:1451
#define JPEG_MARKER_COM
JPEG COM (comment) marker code.
Definition tif_ojpeg.c:301
#define JPEG_MARKER_SOF3
JPEG SOF3 (lossless start-of-frame) marker code.
Definition tif_ojpeg.c:228
#define FIELD_OJPEG_JPEGINTERCHANGEFORMAT
TIFFField field bit for the codec-private JpegInterchangeFormat tag.
Definition tif_ojpeg.c:310
struct jpeg_source_mgr tiff_ojpeg_source_mgr
Alias for the libjpeg source-manager structure.
Definition tif_ojpeg.c:405
static int jpeg_read_header_encap(OJPEGState *sp, tiff_ojpeg_decompress_struct *cinfo, uint8_t require_image)
Trap dependency fatal errors while attempting to read a JPEG header.
Definition tif_ojpeg.c:4061
static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
Decode complete output rows from the active old-style JPEG session.
Definition tif_ojpeg.c:1546
#define JPEG_MARKER_EOI
JPEG EOI (end of image) marker code.
Definition tif_ojpeg.c:260
static int OJPEGReadHeaderInfo(TIFF *tif)
Validate geometry and cache the first old-style JPEG scan position.
Definition tif_ojpeg.c:2037
static int OJPEGReadHeaderInfoSecStreamSos(TIFF *tif)
Validate and cache a JPEG scan component header.
Definition tif_ojpeg.c:3018
@ ososDcTable3
Emit DC Huffman table slot 3 when present.
Definition tif_ojpeg.c:477
@ ososQTable3
Emit quantisation table slot 3 when present.
Definition tif_ojpeg.c:469
@ ososSoi
Emit the synthetic start-of-image marker.
Definition tif_ojpeg.c:461
@ ososQTable2
Emit quantisation table slot 2 when present.
Definition tif_ojpeg.c:467
@ ososDcTable2
Emit DC Huffman table slot 2 when present.
Definition tif_ojpeg.c:475
@ ososEoi
Emit the synthetic end-of-image marker.
Definition tif_ojpeg.c:497
@ ososCompressed
Borrow the next compressed input chunk.
Definition tif_ojpeg.c:493
@ ososQTable1
Emit quantisation table slot 1 when present.
Definition tif_ojpeg.c:465
@ ososAcTable2
Emit AC Huffman table slot 2 when present.
Definition tif_ojpeg.c:483
@ ososRst
Emit the next cyclic restart marker.
Definition tif_ojpeg.c:495
@ ososAcTable0
Emit AC Huffman table slot 0 when present.
Definition tif_ojpeg.c:479
@ ososSof
Emit the reconstructed frame header.
Definition tif_ojpeg.c:489
@ ososAcTable3
Emit AC Huffman table slot 3 when present.
Definition tif_ojpeg.c:485
@ ososDri
Emit the cached restart interval.
Definition tif_ojpeg.c:487
@ ososAcTable1
Emit AC Huffman table slot 1 when present.
Definition tif_ojpeg.c:481
@ ososQTable0
Emit quantisation table slot 0 when present.
Definition tif_ojpeg.c:463
@ ososDcTable1
Emit DC Huffman table slot 1 when present.
Definition tif_ojpeg.c:473
@ ososDcTable0
Emit DC Huffman table slot 0 when present.
Definition tif_ojpeg.c:471
@ ososSos
Emit the current scan header.
Definition tif_ojpeg.c:491
@ osibsStrile
Read bytes from the current strip or tile extent.
Definition tif_ojpeg.c:446
@ osibsNotSetYet
No input extent selected yet.
Definition tif_ojpeg.c:442
@ osibsEof
No further input extents remain.
Definition tif_ojpeg.c:448
@ osibsJpegInterchangeFormat
Read bytes from the interchange-format extent.
Definition tif_ojpeg.c:444
#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_32_maxuint_compat(x, y)
Compute a 32-bit ceiling quotient without adding x and y first.
Definition tiffiop.h:1074
#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 TIFFGetFileSize(tif)
Invoke the handle's client size callback.
Definition tiffiop.h:946
#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 TIFF_NOREADRAW
Suppress the normal raw-data read path.
Definition tiffiop.h:496
#define TIFFArrayCount(a)
Compute the element count of an actual array.
Definition tiffiop.h:1239
#define TIFF_VARIABLE2
Mark a variable-length field with a 32-bit count convention.
Definition tiffio.h:1310
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.
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
uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
Query one logical strile file offset.
uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
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
#define TIFFTAG_JPEGDCTABLES
TIFF tag: DCT table offsets (introduced in TIFF 6.0).
Definition tiff.h:723
#define COMPRESSION_OJPEG
6.0 JPEG (introduced in TIFF 6.0).
Definition tiff.h:316
#define TIFFTAG_JPEGQTABLES
TIFF tag: Q matrix offsets (introduced in TIFF 6.0).
Definition tiff.h:721
#define PHOTOMETRIC_ITULAB
ITU L*a*b*.
Definition tiff.h:402
#define TIFFTAG_JPEGIFBYTECOUNT
TIFF tag: JFIF stream length (introduced in TIFF 6.0).
Definition tiff.h:713
#define TIFFTAG_JPEGACTABLES
TIFF tag: AC coefficient offsets (introduced in TIFF 6.0).
Definition tiff.h:725
#define TIFFTAG_JPEGRESTARTINTERVAL
TIFF tag: Restart interval length (introduced in TIFF 6.0).
Definition tiff.h:715
#define PHOTOMETRIC_YCBCR
CCIR 601 (introduced in TIFF 6.0).
Definition tiff.h:396
#define TIFFTAG_JPEGIFOFFSET
TIFF tag: Pointer to SOI marker (introduced in TIFF 6.0).
Definition tiff.h:711
#define TIFFTAG_YCBCRSUBSAMPLING
TIFF tag: YCbCr subsampling factors (introduced in TIFF 6.0).
Definition tiff.h:729
#define TIFFTAG_JPEGPROC
TIFF tag: JPEG processing algorithm (introduced in TIFF 6.0).
Definition tiff.h:705
#define PLANARCONFIG_CONTIG
Single image plane.
Definition tiff.h:472
int uint16_vap
Represent a uint16_t value after default variadic argument promotion.
Definition tiff.h:159
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
@ TIFF_SHORT
Unsigned 16-bit integer.
Definition tiff.h:237
@ TIFF_LONG8
BigTIFF unsigned 64-bit integer.
Definition tiff.h:259
Own old-style JPEG reconstruction tables buffers and dependency sessions.
Definition tif_ojpeg.c:511
uint8_t samples_per_pixel
Cached component count, at most three.
Definition tif_ojpeg.c:543
uint32_t subsampling_convert_ycbcrbuflen
Combined sample allocation byte capacity.
Definition tif_ojpeg.c:654
uint32_t sof_y
Cached frame height.
Definition tif_ojpeg.c:595
uint8_t writeheader_done
Nonzero after reconstructed decoder session setup.
Definition tif_ojpeg.c:624
uint8_t subsampling_hor
Cached horizontal Y sampling factor.
Definition tif_ojpeg.c:561
tiff_ojpeg_error_mgr libjpeg_jpeg_error_mgr
Embedded dependency diagnostic callbacks.
Definition tif_ojpeg.c:634
uint32_t subsampling_convert_clinelen
Allocated chroma-plane bytes per row.
Definition tif_ojpeg.c:646
uint8_t subsampling_convert_log
Nonzero after conversion geometry/storage setup.
Definition tif_ojpeg.c:640
TIFFPrintMethod printdir
Borrowed saved parent printer.
Definition tif_ojpeg.c:529
uint32_t in_buffer_next_strile
Next/snapshotted strile input index.
Definition tif_ojpeg.c:614
uint32_t subsampling_convert_ybuflen
Y-plane byte capacity.
Definition tif_ojpeg.c:650
uint64_t jpeg_interchange_format
Cached interchange stream offset.
Definition tif_ojpeg.c:549
uint32_t sof_x
Cached frame width.
Definition tif_ojpeg.c:593
uint8_t * subsampling_convert_cbbuf
Borrowed Cb view into the combined allocation.
Definition tif_ojpeg.c:660
uint8_t * in_buffer_cur
Borrowed cursor into in_buffer.
Definition tif_ojpeg.c:695
uint8_t ** subsampling_convert_ycbcrimage
Owned Ext dependency plane/row pointer allocation, borrowing sample storage.
Definition tif_ojpeg.c:669
uint8_t in_buffer[OJPEG_BUFFER]
Embedded client input window.
Definition tif_ojpeg.c:697
uint8_t * skip_buffer
Owned Ext discarded-scanline scratch allocation.
Definition tif_ojpeg.c:703
uint8_t in_buffer_file_pos_log
Nonzero when client position matches refill offset.
Definition tif_ojpeg.c:689
uint16_t in_buffer_togo
Unread bytes in the embedded input window.
Definition tif_ojpeg.c:693
uint8_t subsampling_ver
Cached vertical Y sampling factor.
Definition tif_ojpeg.c:563
uint16_t write_cursample
Current reconstructed separate-plane sample.
Definition tif_ojpeg.c:626
uint8_t samples_per_pixel_per_plane
Components in the current scan.
Definition tif_ojpeg.c:547
uint32_t subsampling_convert_ylines
Y rows per raw MCU group.
Definition tif_ojpeg.c:644
uint8_t * actable[4]
Owned Ext length-prefixed AC Huffman marker blobs.
Definition tif_ojpeg.c:583
uint8_t out_buffer[OJPEG_BUFFER]
Embedded synthetic marker storage, borrowed by source manager.
Definition tif_ojpeg.c:701
OJPEGStateOutState out_state
Next synthetic output state.
Definition tif_ojpeg.c:699
uint8_t dctable_offset_count
Number of stored DC Huffman offsets.
Definition tif_ojpeg.c:569
JMP_BUF exit_jmpbuf
Active synchronous dependency error frame.
Definition tif_ojpeg.c:522
uint8_t subsampling_tag
Nonzero when sampling was explicitly tagged.
Definition tif_ojpeg.c:559
int error_in_raw_data_decoding
Latched raw decode error until reset.
Definition tif_ojpeg.c:519
uint32_t strile_length_total
Height of reconstructed strile stream.
Definition tif_ojpeg.c:541
uint8_t readheader_done
Nonzero after initial header caching.
Definition tif_ojpeg.c:622
uint32_t lines_per_strile
Output ordinary rows or clump units per strile.
Definition tif_ojpeg.c:679
uint8_t * subsampling_convert_ycbcrbuf
Owned Ext combined Y/Cb/Cr sample allocation.
Definition tif_ojpeg.c:656
uint64_t file_size
Cached client file extent.
Definition tif_ojpeg.c:531
uint32_t subsampling_convert_state
Next chroma row in the current MCU group.
Definition tif_ojpeg.c:673
uint8_t libjpeg_jpeg_query_style
Zero for raw planes, one for scanlines.
Definition tif_ojpeg.c:632
uint8_t * dctable[4]
Owned Ext length-prefixed DC Huffman marker blobs.
Definition tif_ojpeg.c:581
uint64_t actable_offset[3]
Embedded AC Huffman offsets, borrowed by getters.
Definition tif_ojpeg.c:577
uint32_t image_length
Cached image height in rows.
Definition tif_ojpeg.c:535
uint8_t actable_offset_count
Number of stored AC Huffman offsets.
Definition tif_ojpeg.c:571
uint32_t subsampling_convert_clines
Chroma rows per raw MCU group.
Definition tif_ojpeg.c:648
uint64_t jpeg_interchange_format_length
Normalized interchange byte extent.
Definition tif_ojpeg.c:551
uint8_t subsamplingcorrect_done
Nonzero after the one-shot correction attempt.
Definition tif_ojpeg.c:557
uint32_t strile_length
Cached strile height.
Definition tif_ojpeg.c:539
uint8_t qtable_offset_count
Number of stored quantisation offsets.
Definition tif_ojpeg.c:567
uint32_t subsampling_convert_clinelenout
Output chroma samples per clump row.
Definition tif_ojpeg.c:671
uint8_t sos_cs[3]
Scan component selectors.
Definition tif_ojpeg.c:603
uint64_t qtable_offset[3]
Embedded quantisation offsets, borrowed by tag getters.
Definition tif_ojpeg.c:573
uint8_t log
Nonzero when this scan snapshot is valid.
Definition tif_ojpeg.c:610
uint32_t image_width
Cached image width in samples.
Definition tif_ojpeg.c:533
uint8_t sof_log
Nonzero when frame details are cached.
Definition tif_ojpeg.c:589
uint8_t libjpeg_session_active
Nonzero while dependency context needs destruction.
Definition tif_ojpeg.c:630
TIFFVGetMethod vgetparent
Borrowed saved parent getter.
Definition tif_ojpeg.c:525
uint8_t sof_marker_id
Cached SOF marker type.
Definition tif_ojpeg.c:591
uint32_t subsampling_convert_ylinelen
Allocated Y-plane bytes per padded row.
Definition tif_ojpeg.c:642
uint8_t sof_c[3]
Frame component identifiers.
Definition tif_ojpeg.c:597
TIFF * tif
Borrowed owning TIFF handle.
Definition tif_ojpeg.c:513
tiff_ojpeg_source_mgr libjpeg_jpeg_source_mgr
Embedded source manager borrowing reconstruction chunks.
Definition tif_ojpeg.c:638
uint8_t sof_hv[3]
Packed per-component sampling factors.
Definition tif_ojpeg.c:599
uint16_t restart_interval
Cached restart MCU interval.
Definition tif_ojpeg.c:585
uint8_t sos_tda[3]
Packed scan DC/AC table selectors.
Definition tif_ojpeg.c:605
uint64_t in_buffer_file_pos
Current/snapshotted next refill offset.
Definition tif_ojpeg.c:616
uint32_t in_buffer_strile_count
Total input strile count.
Definition tif_ojpeg.c:685
uint8_t plane_sample_offset
First component of the selected scan/plane.
Definition tif_ojpeg.c:545
uint8_t sof_tq[3]
Frame quantisation selectors.
Definition tif_ojpeg.c:601
uint32_t subsampling_convert_cbuflen
Each chroma-plane byte capacity.
Definition tif_ojpeg.c:652
uint8_t subsamplingcorrect
Nonzero during sampling-only header probing.
Definition tif_ojpeg.c:555
uint8_t * qtable[4]
Owned Ext length-prefixed quantisation marker blobs.
Definition tif_ojpeg.c:579
uint8_t jpeg_proc
Cached JPEG process tag.
Definition tif_ojpeg.c:553
uint8_t subsampling_force_desubsampling_inside_decompression
Nonzero requests ordinary dependency upsampling.
Definition tif_ojpeg.c:565
uint32_t write_curstrile
Current reconstructed strile counter.
Definition tif_ojpeg.c:628
uint32_t bytes_per_line
Output bytes per ordinary row or subsampling_ver-row clump unit.
Definition tif_ojpeg.c:677
OJPEGStateInBufferSource in_buffer_source
Current/snapshotted interchange or strile input source.
Definition tif_ojpeg.c:612
uint32_t subsampling_convert_ycbcrimagelen
Owned pointer-array entry count.
Definition tif_ojpeg.c:664
int decoder_ok
Latched nonzero by the first successful predecode; never cleared.
Definition tif_ojpeg.c:517
uint8_t * subsampling_convert_crbuf
Borrowed Cr view into the combined allocation.
Definition tif_ojpeg.c:662
uint64_t dctable_offset[3]
Embedded DC Huffman offsets, borrowed by getters.
Definition tif_ojpeg.c:575
struct OJPEGState::@2 sos_end[3]
Save the input position following one plane scan header.
uint64_t in_buffer_file_togo
Current/snapshotted remaining bounded extent.
Definition tif_ojpeg.c:618
uint32_t strile_width
Cached strile width.
Definition tif_ojpeg.c:537
tiff_ojpeg_decompress_struct libjpeg_jpeg_decompress_struct
Embedded context owning dependency session resources.
Definition tif_ojpeg.c:636
uint8_t restart_index
Next cyclic restart marker index.
Definition tif_ojpeg.c:587
TIFFVSetMethod vsetparent
Borrowed saved parent setter.
Definition tif_ojpeg.c:527
uint8_t * subsampling_convert_ybuf
Borrowed Y view into the combined allocation.
Definition tif_ojpeg.c:658
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
uint32_t td_stripsperimage
Strips or tiles per sample plane, despite the historic name.
Definition tif_dir.h:269
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_nstrips
Total strile count across all planes; logical array length.
Definition tif_dir.h:271
uint32_t td_imagelength
Image length in rows.
Definition tif_dir.h:156
uint32_t td_curtile
Current tile index, or NOTILE.
Definition tif_dir.h:256
uint32_t td_tilelength
Tile length in rows.
Definition tif_dir.h:162
uint32_t td_curstrip
Current strip index, or NOSTRIP.
Definition tif_dir.h:242
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
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
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
TIFFPostMethod tif_postdecode
Callback transforming decoded data, for example by byte swapping.
Definition tiffiop.h:727
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
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
Definition tiffiop.h:628
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.