ReactOS 0.4.16-dev-2574-g474348f
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
125#define WIN32_LEAN_AND_MEAN
126#define VC_EXTRALEAN
127
128#include "tiffiop.h"
129#ifdef OJPEG_SUPPORT
130
131/* Configuration defines here are:
132 * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some
133 * environments, like eg LibTiffDelphi, this is not possible. For this reason,
134 * the actual calls to libjpeg, with longjump stuff, are encapsulated in
135 * dedicated functions. When JPEG_ENCAP_EXTERNAL is defined, these encapsulating
136 * functions are declared external to this unit, and can be defined elsewhere to
137 * use stuff other then longjump. The default mode, without JPEG_ENCAP_EXTERNAL,
138 * implements the call encapsulators here, internally, with normal longjump.
139 * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent
140 * is conveniently available, but still it may be worthwhile to use _setjmp or
141 * sigsetjmp in place of plain setjmp. These macros will make it easier. It is
142 * useless to fiddle with these if you define JPEG_ENCAP_EXTERNAL. OJPEG_BUFFER:
143 * Define the size of the desired buffer here. Should be small enough so as to
144 * guarantee instant processing, optimal streaming and optimal use of processor
145 * cache, but also big enough so as to not result in significant call overhead.
146 * It should be at least a few bytes to accommodate some structures (this is
147 * verified in asserts), but it would not be sensible to make it this small
148 * anyway, and it should be at most 64K since it is indexed with uint16_t. We
149 * recommend 2K. EGYPTIANWALK: You could also define EGYPTIANWALK here, but it
150 * is not used anywhere and has absolutely no effect. That is why most people
151 * insist the EGYPTIANWALK is a bit silly.
152 */
153
154/* define LIBJPEG_ENCAP_EXTERNAL */
155#define SETJMP(jbuf) setjmp(jbuf)
156#define LONGJMP(jbuf, code) longjmp(jbuf, code)
157#define JMP_BUF jmp_buf
158#define OJPEG_BUFFER 2048
159/* define EGYPTIANWALK */
160
161#define JPEG_MARKER_SOF0 0xC0
162#define JPEG_MARKER_SOF1 0xC1
163#define JPEG_MARKER_SOF3 0xC3
164#define JPEG_MARKER_DHT 0xC4
165#define JPEG_MARKER_RST0 0XD0
166#define JPEG_MARKER_SOI 0xD8
167#define JPEG_MARKER_EOI 0xD9
168#define JPEG_MARKER_SOS 0xDA
169#define JPEG_MARKER_DQT 0xDB
170#define JPEG_MARKER_DRI 0xDD
171#define JPEG_MARKER_APP0 0xE0
172#define JPEG_MARKER_COM 0xFE
173
174#define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC + 0)
175#define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC + 1)
176#define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC + 2)
177#define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC + 3)
178#define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC + 4)
179#define FIELD_OJPEG_JPEGPROC (FIELD_CODEC + 5)
180#define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC + 6)
181
182static const TIFFField ojpegFields[] = {
184 FIELD_OJPEG_JPEGINTERCHANGEFORMAT, TRUE, FALSE, "JpegInterchangeFormat",
185 NULL},
187 FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH, TRUE, FALSE,
188 "JpegInterchangeFormatLength", NULL},
190 TIFF_SETGET_C32_UINT64, FIELD_OJPEG_JPEGQTABLES, FALSE, TRUE,
191 "JpegQTables", NULL},
193 TIFF_SETGET_C32_UINT64, FIELD_OJPEG_JPEGDCTABLES, FALSE, TRUE,
194 "JpegDcTables", NULL},
196 TIFF_SETGET_C32_UINT64, FIELD_OJPEG_JPEGACTABLES, FALSE, TRUE,
197 "JpegAcTables", NULL},
199 FIELD_OJPEG_JPEGPROC, FALSE, FALSE, "JpegProc", NULL},
201 FIELD_OJPEG_JPEGRESTARTINTERVAL, FALSE, FALSE, "JpegRestartInterval",
202 NULL},
203};
204
205#ifndef LIBJPEG_ENCAP_EXTERNAL
206#include <setjmp.h>
207#endif
208
209#include "jerror.h"
210#include "jpeglib.h"
211
212#ifndef TIFF_jpeg_source_mgr_defined
213#define TIFF_jpeg_source_mgr_defined
214typedef struct jpeg_source_mgr jpeg_source_mgr;
215#endif
216
217#ifndef TIFF_jpeg_error_mgr_defined
218#define TIFF_jpeg_error_mgr_defined
219typedef struct jpeg_error_mgr jpeg_error_mgr;
220#endif
221
224
225typedef enum
226{
227 osibsNotSetYet,
228 osibsJpegInterchangeFormat,
229 osibsStrile,
230 osibsEof
231} OJPEGStateInBufferSource;
232
233typedef enum
234{
235 ososSoi,
236 ososQTable0,
237 ososQTable1,
238 ososQTable2,
239 ososQTable3,
240 ososDcTable0,
241 ososDcTable1,
242 ososDcTable2,
243 ososDcTable3,
244 ososAcTable0,
245 ososAcTable1,
246 ososAcTable2,
247 ososAcTable3,
248 ososDri,
249 ososSof,
250 ososSos,
251 ososCompressed,
252 ososRst,
253 ososEoi
254} OJPEGStateOutState;
255
256typedef struct
257{
258 TIFF *tif;
259 int decoder_ok;
260 int error_in_raw_data_decoding;
261#ifndef LIBJPEG_ENCAP_EXTERNAL
262 JMP_BUF exit_jmpbuf;
263#endif
264 TIFFVGetMethod vgetparent;
265 TIFFVSetMethod vsetparent;
266 TIFFPrintMethod printdir;
269 uint32_t image_length;
270 uint32_t strile_width;
271 uint32_t strile_length;
272 uint32_t strile_length_total;
273 uint8_t samples_per_pixel;
274 uint8_t plane_sample_offset;
275 uint8_t samples_per_pixel_per_plane;
276 uint64_t jpeg_interchange_format;
277 uint64_t jpeg_interchange_format_length;
278 uint8_t jpeg_proc;
279 uint8_t subsamplingcorrect;
280 uint8_t subsamplingcorrect_done;
281 uint8_t subsampling_tag;
282 uint8_t subsampling_hor;
283 uint8_t subsampling_ver;
284 uint8_t subsampling_force_desubsampling_inside_decompression;
285 uint8_t qtable_offset_count;
286 uint8_t dctable_offset_count;
287 uint8_t actable_offset_count;
288 uint64_t qtable_offset[3];
289 uint64_t dctable_offset[3];
290 uint64_t actable_offset[3];
291 uint8_t *qtable[4];
292 uint8_t *dctable[4];
293 uint8_t *actable[4];
295 uint8_t restart_index;
296 uint8_t sof_log;
297 uint8_t sof_marker_id;
298 uint32_t sof_x;
299 uint32_t sof_y;
300 uint8_t sof_c[3];
301 uint8_t sof_hv[3];
302 uint8_t sof_tq[3];
303 uint8_t sos_cs[3];
304 uint8_t sos_tda[3];
305 struct
306 {
307 uint8_t log;
308 OJPEGStateInBufferSource in_buffer_source;
309 uint32_t in_buffer_next_strile;
310 uint64_t in_buffer_file_pos;
311 uint64_t in_buffer_file_togo;
312 } sos_end[3];
313 uint8_t readheader_done;
314 uint8_t writeheader_done;
315 uint16_t write_cursample;
316 uint32_t write_curstrile;
317 uint8_t libjpeg_session_active;
318 uint8_t libjpeg_jpeg_query_style;
319 jpeg_error_mgr libjpeg_jpeg_error_mgr;
320 jpeg_decompress_struct libjpeg_jpeg_decompress_struct;
321 jpeg_source_mgr libjpeg_jpeg_source_mgr;
322 uint8_t subsampling_convert_log;
323 uint32_t subsampling_convert_ylinelen;
324 uint32_t subsampling_convert_ylines;
325 uint32_t subsampling_convert_clinelen;
326 uint32_t subsampling_convert_clines;
327 uint32_t subsampling_convert_ybuflen;
328 uint32_t subsampling_convert_cbuflen;
329 uint32_t subsampling_convert_ycbcrbuflen;
330 uint8_t *subsampling_convert_ycbcrbuf;
331 uint8_t *subsampling_convert_ybuf;
332 uint8_t *subsampling_convert_cbbuf;
333 uint8_t *subsampling_convert_crbuf;
334 uint32_t subsampling_convert_ycbcrimagelen;
335 uint8_t **subsampling_convert_ycbcrimage;
336 uint32_t subsampling_convert_clinelenout;
337 uint32_t subsampling_convert_state;
338 uint32_t bytes_per_line; /* if the codec outputs subsampled data, a 'line'
339 in bytes_per_line */
340 uint32_t lines_per_strile; /* and lines_per_strile means subsampling_ver
341 desubsampled rows */
342 OJPEGStateInBufferSource in_buffer_source;
343 uint32_t in_buffer_next_strile;
344 uint32_t in_buffer_strile_count;
345 uint64_t in_buffer_file_pos;
346 uint8_t in_buffer_file_pos_log;
347 uint64_t in_buffer_file_togo;
348 uint16_t in_buffer_togo;
349 uint8_t *in_buffer_cur;
350 uint8_t in_buffer[OJPEG_BUFFER];
351 OJPEGStateOutState out_state;
352 uint8_t out_buffer[OJPEG_BUFFER];
353 uint8_t *skip_buffer;
354} OJPEGState;
355
356static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap);
357static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap);
358static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags);
359
360static int OJPEGFixupTags(TIFF *tif);
361static int OJPEGSetupDecode(TIFF *tif);
362static int OJPEGPreDecode(TIFF *tif, uint16_t s);
363static int OJPEGPreDecodeSkipRaw(TIFF *tif);
364static int OJPEGPreDecodeSkipScanlines(TIFF *tif);
365static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
366static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc);
367static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc);
368static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc);
369static int OJPEGSetupEncode(TIFF *tif);
370static int OJPEGPreEncode(TIFF *tif, uint16_t s);
371static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
372static int OJPEGPostEncode(TIFF *tif);
373static void OJPEGCleanup(TIFF *tif);
374
375static void OJPEGSubsamplingCorrect(TIFF *tif);
376static int OJPEGReadHeaderInfo(TIFF *tif);
377static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s);
378static int OJPEGWriteHeaderInfo(TIFF *tif);
379static void OJPEGLibjpegSessionAbort(TIFF *tif);
380
381static int OJPEGReadHeaderInfoSec(TIFF *tif);
382static int OJPEGReadHeaderInfoSecStreamDri(TIFF *tif);
383static int OJPEGReadHeaderInfoSecStreamDqt(TIFF *tif);
384static int OJPEGReadHeaderInfoSecStreamDht(TIFF *tif);
385static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id);
386static int OJPEGReadHeaderInfoSecStreamSos(TIFF *tif);
387static int OJPEGReadHeaderInfoSecTablesQTable(TIFF *tif);
388static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF *tif);
389static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF *tif);
390
391static int OJPEGReadBufferFill(OJPEGState *sp);
392static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte);
393static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte);
394static void OJPEGReadByteAdvance(OJPEGState *sp);
395static int OJPEGReadWord(OJPEGState *sp, uint16_t *word);
396static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem);
397static void OJPEGReadSkip(OJPEGState *sp, uint16_t len);
398
399static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len);
400static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len);
401static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem,
402 uint32_t *len);
403static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem,
404 uint32_t *len);
405static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem,
406 uint32_t *len);
407static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len);
408static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len);
409static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len);
410static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len);
411static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len);
412static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len);
413
414#ifdef LIBJPEG_ENCAP_EXTERNAL
415extern int jpeg_create_decompress_encap(OJPEGState *sp,
417extern int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo,
419extern int jpeg_start_decompress_encap(OJPEGState *sp,
421extern int jpeg_read_scanlines_encap(OJPEGState *sp,
424extern int jpeg_read_raw_data_encap(OJPEGState *sp,
425 jpeg_decompress_struct *cinfo, void *data,
427extern void jpeg_encap_unwind(TIFF *tif);
428#else
429static int jpeg_create_decompress_encap(OJPEGState *sp,
431static int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo,
433static int jpeg_start_decompress_encap(OJPEGState *sp,
435static int jpeg_read_scanlines_encap(OJPEGState *sp,
438static int jpeg_read_raw_data_encap(OJPEGState *sp,
439 jpeg_decompress_struct *cinfo, void *data,
441static void jpeg_encap_unwind(TIFF *tif);
442#endif
443
444static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct *cinfo);
445static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct *cinfo);
446static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct *cinfo);
447static boolean
448OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct *cinfo);
449static void
450OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct *cinfo,
451 long num_bytes);
452static boolean
453OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct *cinfo,
454 int desired);
455static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct *cinfo);
456
457int TIFFInitOJPEG(TIFF *tif, int scheme)
458{
459 static const char module[] = "TIFFInitOJPEG";
460 OJPEGState *sp;
461
462 (void)scheme;
464
465 /*
466 * Merge codec-specific tag information.
467 */
468 if (!_TIFFMergeFields(tif, ojpegFields, TIFFArrayCount(ojpegFields)))
469 {
471 "Merging Old JPEG codec-specific tags failed");
472 return 0;
473 }
474
475 /* state block */
476 sp = _TIFFmallocExt(tif, sizeof(OJPEGState));
477 if (sp == NULL)
478 {
479 TIFFErrorExtR(tif, module, "No space for OJPEG state block");
480 return (0);
481 }
482 _TIFFmemset(sp, 0, sizeof(OJPEGState));
483 sp->tif = tif;
484 sp->jpeg_proc = 1;
485 sp->subsampling_hor = 2;
486 sp->subsampling_ver = 2;
488 /* tif codec methods */
489 tif->tif_fixuptags = OJPEGFixupTags;
490 tif->tif_setupdecode = OJPEGSetupDecode;
491 tif->tif_predecode = OJPEGPreDecode;
492 tif->tif_postdecode = OJPEGPostDecode;
493 tif->tif_decoderow = OJPEGDecode;
494 tif->tif_decodestrip = OJPEGDecode;
495 tif->tif_decodetile = OJPEGDecode;
496 tif->tif_setupencode = OJPEGSetupEncode;
497 tif->tif_preencode = OJPEGPreEncode;
498 tif->tif_postencode = OJPEGPostEncode;
499 tif->tif_encoderow = OJPEGEncode;
500 tif->tif_encodestrip = OJPEGEncode;
501 tif->tif_encodetile = OJPEGEncode;
502 tif->tif_cleanup = OJPEGCleanup;
503 tif->tif_data = (uint8_t *)sp;
504 /* tif tag methods */
505 sp->vgetparent = tif->tif_tagmethods.vgetfield;
506 tif->tif_tagmethods.vgetfield = OJPEGVGetField;
507 sp->vsetparent = tif->tif_tagmethods.vsetfield;
508 tif->tif_tagmethods.vsetfield = OJPEGVSetField;
509 sp->printdir = tif->tif_tagmethods.printdir;
510 tif->tif_tagmethods.printdir = OJPEGPrintDir;
511 /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
512 Some others do, but have totally meaningless or corrupt values
513 in these tags. In these cases, the JpegInterchangeFormat stream is
514 reliable. In any case, this decoder reads the compressed data itself,
515 from the most reliable locations, and we need to notify encapsulating
516 LibTiff not to read raw strips or tiles for us. */
518 return (1);
519}
520
521static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap)
522{
523 OJPEGState *sp = (OJPEGState *)tif->tif_data;
524 switch (tag)
525 {
527 *va_arg(ap, uint64_t *) = (uint64_t)sp->jpeg_interchange_format;
528 break;
530 *va_arg(ap, uint64_t *) =
531 (uint64_t)sp->jpeg_interchange_format_length;
532 break;
534 if (sp->subsamplingcorrect_done == 0)
535 OJPEGSubsamplingCorrect(tif);
536 *va_arg(ap, uint16_t *) = (uint16_t)sp->subsampling_hor;
537 *va_arg(ap, uint16_t *) = (uint16_t)sp->subsampling_ver;
538 break;
540 *va_arg(ap, uint32_t *) = (uint32_t)sp->qtable_offset_count;
541 *va_arg(ap, const void **) = (const void *)sp->qtable_offset;
542 break;
544 *va_arg(ap, uint32_t *) = (uint32_t)sp->dctable_offset_count;
545 *va_arg(ap, const void **) = (const void *)sp->dctable_offset;
546 break;
548 *va_arg(ap, uint32_t *) = (uint32_t)sp->actable_offset_count;
549 *va_arg(ap, const void **) = (const void *)sp->actable_offset;
550 break;
551 case TIFFTAG_JPEGPROC:
552 *va_arg(ap, uint16_t *) = (uint16_t)sp->jpeg_proc;
553 break;
555 *va_arg(ap, uint16_t *) = sp->restart_interval;
556 break;
557 default:
558 return (*sp->vgetparent)(tif, tag, ap);
559 }
560 return (1);
561}
562
563static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap)
564{
565 static const char module[] = "OJPEGVSetField";
566 OJPEGState *sp = (OJPEGState *)tif->tif_data;
567 uint32_t ma;
568 uint64_t *mb;
569 uint32_t n;
570 const TIFFField *fip;
571
572 switch (tag)
573 {
575 sp->jpeg_interchange_format = (uint64_t)va_arg(ap, uint64_t);
576 break;
578 sp->jpeg_interchange_format_length = (uint64_t)va_arg(ap, uint64_t);
579 break;
581 sp->subsampling_tag = 1;
582 sp->subsampling_hor = (uint8_t)va_arg(ap, uint16_vap);
583 sp->subsampling_ver = (uint8_t)va_arg(ap, uint16_vap);
584 tif->tif_dir.td_ycbcrsubsampling[0] = sp->subsampling_hor;
585 tif->tif_dir.td_ycbcrsubsampling[1] = sp->subsampling_ver;
586 break;
588 ma = (uint32_t)va_arg(ap, uint32_t);
589 if (ma != 0)
590 {
591 if (ma > 3)
592 {
594 "JpegQTables tag has incorrect count");
595 return (0);
596 }
597 sp->qtable_offset_count = (uint8_t)ma;
598 mb = (uint64_t *)va_arg(ap, uint64_t *);
599 for (n = 0; n < ma; n++)
600 sp->qtable_offset[n] = mb[n];
601 }
602 break;
604 ma = (uint32_t)va_arg(ap, uint32_t);
605 if (ma != 0)
606 {
607 if (ma > 3)
608 {
610 "JpegDcTables tag has incorrect count");
611 return (0);
612 }
613 sp->dctable_offset_count = (uint8_t)ma;
614 mb = (uint64_t *)va_arg(ap, uint64_t *);
615 for (n = 0; n < ma; n++)
616 sp->dctable_offset[n] = mb[n];
617 }
618 break;
620 ma = (uint32_t)va_arg(ap, uint32_t);
621 if (ma != 0)
622 {
623 if (ma > 3)
624 {
626 "JpegAcTables tag has incorrect count");
627 return (0);
628 }
629 sp->actable_offset_count = (uint8_t)ma;
630 mb = (uint64_t *)va_arg(ap, uint64_t *);
631 for (n = 0; n < ma; n++)
632 sp->actable_offset[n] = mb[n];
633 }
634 break;
635 case TIFFTAG_JPEGPROC:
636 sp->jpeg_proc = (uint8_t)va_arg(ap, uint16_vap);
637 break;
639 sp->restart_interval = (uint16_t)va_arg(ap, uint16_vap);
640 break;
641 default:
642 return (*sp->vsetparent)(tif, tag, ap);
643 }
644 fip = TIFFFieldWithTag(tif, tag);
645 if (fip == NULL) /* shouldn't happen */
646 return (0);
647 TIFFSetFieldBit(tif, fip->field_bit);
649 return (1);
650}
651
652static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags)
653{
654 OJPEGState *sp = (OJPEGState *)tif->tif_data;
655 uint8_t m;
656 (void)flags;
657 assert(sp != NULL);
658 if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGINTERCHANGEFORMAT))
659 fprintf(fd, " JpegInterchangeFormat: %" PRIu64 "\n",
660 (uint64_t)sp->jpeg_interchange_format);
661 if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH))
662 fprintf(fd, " JpegInterchangeFormatLength: %" PRIu64 "\n",
663 (uint64_t)sp->jpeg_interchange_format_length);
664 if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGQTABLES))
665 {
666 fprintf(fd, " JpegQTables:");
667 for (m = 0; m < sp->qtable_offset_count; m++)
668 fprintf(fd, " %" PRIu64, (uint64_t)sp->qtable_offset[m]);
669 fprintf(fd, "\n");
670 }
671 if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGDCTABLES))
672 {
673 fprintf(fd, " JpegDcTables:");
674 for (m = 0; m < sp->dctable_offset_count; m++)
675 fprintf(fd, " %" PRIu64, (uint64_t)sp->dctable_offset[m]);
676 fprintf(fd, "\n");
677 }
678 if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGACTABLES))
679 {
680 fprintf(fd, " JpegAcTables:");
681 for (m = 0; m < sp->actable_offset_count; m++)
682 fprintf(fd, " %" PRIu64, (uint64_t)sp->actable_offset[m]);
683 fprintf(fd, "\n");
684 }
685 if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGPROC))
686 fprintf(fd, " JpegProc: %" PRIu8 "\n", sp->jpeg_proc);
687 if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGRESTARTINTERVAL))
688 fprintf(fd, " JpegRestartInterval: %" PRIu16 "\n",
689 sp->restart_interval);
690 if (sp->printdir)
691 (*sp->printdir)(tif, fd, flags);
692}
693
694static int OJPEGFixupTags(TIFF *tif)
695{
696 (void)tif;
697 return (1);
698}
699
700static int OJPEGSetupDecode(TIFF *tif)
701{
702 static const char module[] = "OJPEGSetupDecode";
704 "Deprecated and troublesome old-style JPEG compression "
705 "mode, please convert to new-style JPEG compression and "
706 "notify vendor of writing software");
707 return (1);
708}
709
710static int OJPEGPreDecode(TIFF *tif, uint16_t s)
711{
712 OJPEGState *sp = (OJPEGState *)tif->tif_data;
713 uint32_t m;
714 if (sp->subsamplingcorrect_done == 0)
715 OJPEGSubsamplingCorrect(tif);
716 if (sp->readheader_done == 0)
717 {
718 if (OJPEGReadHeaderInfo(tif) == 0)
719 return (0);
720 }
721 if (sp->sos_end[s].log == 0)
722 {
723 if (OJPEGReadSecondarySos(tif, s) == 0)
724 return (0);
725 }
726 if (isTiled(tif))
727 m = tif->tif_curtile;
728 else
729 m = tif->tif_curstrip;
730 if ((sp->writeheader_done != 0) &&
731 ((sp->write_cursample != s) || (sp->write_curstrile > m)))
732 {
733 if (sp->libjpeg_session_active != 0)
734 OJPEGLibjpegSessionAbort(tif);
735 sp->writeheader_done = 0;
736 }
737 if (sp->writeheader_done == 0)
738 {
739 sp->plane_sample_offset = (uint8_t)s;
740 sp->write_cursample = s;
741 sp->write_curstrile = s * tif->tif_dir.td_stripsperimage;
742 if ((sp->in_buffer_file_pos_log == 0) ||
743 (sp->in_buffer_file_pos - sp->in_buffer_togo !=
744 sp->sos_end[s].in_buffer_file_pos))
745 {
746 sp->in_buffer_source = sp->sos_end[s].in_buffer_source;
747 sp->in_buffer_next_strile = sp->sos_end[s].in_buffer_next_strile;
748 sp->in_buffer_file_pos = sp->sos_end[s].in_buffer_file_pos;
749 sp->in_buffer_file_pos_log = 0;
750 sp->in_buffer_file_togo = sp->sos_end[s].in_buffer_file_togo;
751 sp->in_buffer_togo = 0;
752 sp->in_buffer_cur = 0;
753 }
754 if (OJPEGWriteHeaderInfo(tif) == 0)
755 return (0);
756 }
757
758 sp->subsampling_convert_state = 0;
759
760 while (sp->write_curstrile < m)
761 {
762 if (sp->libjpeg_jpeg_query_style == 0)
763 {
764 if (OJPEGPreDecodeSkipRaw(tif) == 0)
765 return (0);
766 }
767 else
768 {
769 if (OJPEGPreDecodeSkipScanlines(tif) == 0)
770 return (0);
771 }
772 sp->write_curstrile++;
773 }
774 sp->decoder_ok = 1;
775 return (1);
776}
777
778static int OJPEGPreDecodeSkipRaw(TIFF *tif)
779{
780 OJPEGState *sp = (OJPEGState *)tif->tif_data;
781 uint32_t m;
782 m = sp->lines_per_strile;
783 if (sp->subsampling_convert_state != 0)
784 {
785 if (sp->subsampling_convert_clines - sp->subsampling_convert_state >= m)
786 {
787 sp->subsampling_convert_state += m;
788 if (sp->subsampling_convert_state == sp->subsampling_convert_clines)
789 sp->subsampling_convert_state = 0;
790 return (1);
791 }
792 m -= sp->subsampling_convert_clines - sp->subsampling_convert_state;
793 sp->subsampling_convert_state = 0;
794 sp->error_in_raw_data_decoding = 0;
795 }
796 while (m >= sp->subsampling_convert_clines)
797 {
798 if (jpeg_read_raw_data_encap(sp, &(sp->libjpeg_jpeg_decompress_struct),
799 sp->subsampling_convert_ycbcrimage,
800 sp->subsampling_ver * 8) == 0)
801 return (0);
802 m -= sp->subsampling_convert_clines;
803 }
804 if (m > 0)
805 {
806 if (jpeg_read_raw_data_encap(sp, &(sp->libjpeg_jpeg_decompress_struct),
807 sp->subsampling_convert_ycbcrimage,
808 sp->subsampling_ver * 8) == 0)
809 return (0);
810 sp->subsampling_convert_state = m;
811 }
812 return (1);
813}
814
815static int OJPEGPreDecodeSkipScanlines(TIFF *tif)
816{
817 static const char module[] = "OJPEGPreDecodeSkipScanlines";
818 OJPEGState *sp = (OJPEGState *)tif->tif_data;
819 uint32_t m;
820 if (sp->skip_buffer == NULL)
821 {
822 sp->skip_buffer = _TIFFmallocExt(tif, sp->bytes_per_line);
823 if (sp->skip_buffer == NULL)
824 {
825 TIFFErrorExtR(tif, module, "Out of memory");
826 return (0);
827 }
828 }
829 for (m = 0; m < sp->lines_per_strile; m++)
830 {
831 if (jpeg_read_scanlines_encap(sp, &(sp->libjpeg_jpeg_decompress_struct),
832 &sp->skip_buffer, 1) == 0)
833 return (0);
834 }
835 return (1);
836}
837
838static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
839{
840 static const char module[] = "OJPEGDecode";
841 OJPEGState *sp = (OJPEGState *)tif->tif_data;
842 (void)s;
843 if (!sp->decoder_ok)
844 {
845 memset(buf, 0, (size_t)cc);
847 "Cannot decode: decoder not correctly initialized");
848 return 0;
849 }
850 if (sp->libjpeg_session_active == 0)
851 {
852 memset(buf, 0, (size_t)cc);
853 /* This should normally not happen, except that it does when */
854 /* using TIFFReadScanline() which calls OJPEGPostDecode() for */
855 /* each scanline, which assumes that a whole strile was read */
856 /* and may thus incorrectly consider it has read the whole image,
857 * causing */
858 /* OJPEGLibjpegSessionAbort() to be called prematurely. */
859 /* Triggered by https://gitlab.com/libtiff/libtiff/-/issues/337 */
861 "Cannot decode: libjpeg_session_active == 0");
862 return 0;
863 }
864 if (sp->error_in_raw_data_decoding)
865 {
866 memset(buf, 0, (size_t)cc);
867 return 0;
868 }
869 if (sp->libjpeg_jpeg_query_style == 0)
870 {
871 if (OJPEGDecodeRaw(tif, buf, cc) == 0)
872 {
873 memset(buf, 0, (size_t)cc);
874 return (0);
875 }
876 }
877 else
878 {
879 if (OJPEGDecodeScanlines(tif, buf, cc) == 0)
880 {
881 memset(buf, 0, (size_t)cc);
882 return (0);
883 }
884 }
885 return (1);
886}
887
888static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc)
889{
890 static const char module[] = "OJPEGDecodeRaw";
891 OJPEGState *sp = (OJPEGState *)tif->tif_data;
892 uint8_t *m;
893 tmsize_t n;
894 uint8_t *oy;
895 uint8_t *ocb;
896 uint8_t *ocr;
897 uint8_t *p;
898 uint32_t q;
899 uint8_t *r;
900 uint8_t sx, sy;
901 if (cc % sp->bytes_per_line != 0)
902 {
903 TIFFErrorExtR(tif, module, "Fractional scanline not read");
904 return (0);
905 }
906 assert(cc > 0);
907 m = buf;
908 n = cc;
909 do
910 {
911 if (sp->subsampling_convert_state == 0)
912 {
913 if (jpeg_read_raw_data_encap(sp,
914 &(sp->libjpeg_jpeg_decompress_struct),
915 sp->subsampling_convert_ycbcrimage,
916 sp->subsampling_ver * 8) == 0)
917 {
918 sp->error_in_raw_data_decoding = 1;
919 return (0);
920 }
921 }
922 oy = sp->subsampling_convert_ybuf +
923 sp->subsampling_convert_state * sp->subsampling_ver *
924 sp->subsampling_convert_ylinelen;
925 ocb = sp->subsampling_convert_cbbuf +
926 sp->subsampling_convert_state * sp->subsampling_convert_clinelen;
927 ocr = sp->subsampling_convert_crbuf +
928 sp->subsampling_convert_state * sp->subsampling_convert_clinelen;
929 p = m;
930 for (q = 0; q < sp->subsampling_convert_clinelenout; q++)
931 {
932 r = oy;
933 for (sy = 0; sy < sp->subsampling_ver; sy++)
934 {
935 for (sx = 0; sx < sp->subsampling_hor; sx++)
936 *p++ = *r++;
937 r += sp->subsampling_convert_ylinelen - sp->subsampling_hor;
938 }
939 oy += sp->subsampling_hor;
940 *p++ = *ocb++;
941 *p++ = *ocr++;
942 }
943 sp->subsampling_convert_state++;
944 if (sp->subsampling_convert_state == sp->subsampling_convert_clines)
945 sp->subsampling_convert_state = 0;
946 m += sp->bytes_per_line;
947 n -= sp->bytes_per_line;
948 } while (n > 0);
949 return (1);
950}
951
952static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc)
953{
954 static const char module[] = "OJPEGDecodeScanlines";
955 OJPEGState *sp = (OJPEGState *)tif->tif_data;
956 uint8_t *m;
957 tmsize_t n;
958 if (cc % sp->bytes_per_line != 0)
959 {
960 TIFFErrorExtR(tif, module, "Fractional scanline not read");
961 return (0);
962 }
963 assert(cc > 0);
964 m = buf;
965 n = cc;
966 do
967 {
968 if (jpeg_read_scanlines_encap(sp, &(sp->libjpeg_jpeg_decompress_struct),
969 &m, 1) == 0)
970 return (0);
971 m += sp->bytes_per_line;
972 n -= sp->bytes_per_line;
973 } while (n > 0);
974 return (1);
975}
976
977static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
978{
979 OJPEGState *sp = (OJPEGState *)tif->tif_data;
980 (void)buf;
981 (void)cc;
982 /* This function somehow incorrectly assumes that a whole strile was read,
983 */
984 /* which is not true when TIFFReadScanline() is called, */
985 /* and may thus incorrectly consider it has read the whole image, causing */
986 /* OJPEGLibjpegSessionAbort() to be called prematurely. */
987 /* So this logic should be fixed to take into account cc, or disable */
988 /* the scan line reading interface. */
989 /* Triggered by https://gitlab.com/libtiff/libtiff/-/issues/337 */
990 sp->write_curstrile++;
991 if (sp->write_curstrile % tif->tif_dir.td_stripsperimage == 0)
992 {
993 assert(sp->libjpeg_session_active != 0);
994 OJPEGLibjpegSessionAbort(tif);
995 sp->writeheader_done = 0;
996 }
997}
998
999static int OJPEGSetupEncode(TIFF *tif)
1000{
1001 static const char module[] = "OJPEGSetupEncode";
1003 tif, module,
1004 "OJPEG encoding not supported; use new-style JPEG compression instead");
1005 return (0);
1006}
1007
1008static int OJPEGPreEncode(TIFF *tif, uint16_t s)
1009{
1010 static const char module[] = "OJPEGPreEncode";
1011 (void)s;
1013 tif, module,
1014 "OJPEG encoding not supported; use new-style JPEG compression instead");
1015 return (0);
1016}
1017
1018static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
1019{
1020 static const char module[] = "OJPEGEncode";
1021 (void)buf;
1022 (void)cc;
1023 (void)s;
1025 tif, module,
1026 "OJPEG encoding not supported; use new-style JPEG compression instead");
1027 return (0);
1028}
1029
1030static int OJPEGPostEncode(TIFF *tif)
1031{
1032 static const char module[] = "OJPEGPostEncode";
1034 tif, module,
1035 "OJPEG encoding not supported; use new-style JPEG compression instead");
1036 return (0);
1037}
1038
1039static void OJPEGCleanup(TIFF *tif)
1040{
1041 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1042 if (sp != 0)
1043 {
1044 tif->tif_tagmethods.vgetfield = sp->vgetparent;
1045 tif->tif_tagmethods.vsetfield = sp->vsetparent;
1046 tif->tif_tagmethods.printdir = sp->printdir;
1047 if (sp->qtable[0] != 0)
1048 _TIFFfreeExt(tif, sp->qtable[0]);
1049 if (sp->qtable[1] != 0)
1050 _TIFFfreeExt(tif, sp->qtable[1]);
1051 if (sp->qtable[2] != 0)
1052 _TIFFfreeExt(tif, sp->qtable[2]);
1053 if (sp->qtable[3] != 0)
1054 _TIFFfreeExt(tif, sp->qtable[3]);
1055 if (sp->dctable[0] != 0)
1056 _TIFFfreeExt(tif, sp->dctable[0]);
1057 if (sp->dctable[1] != 0)
1058 _TIFFfreeExt(tif, sp->dctable[1]);
1059 if (sp->dctable[2] != 0)
1060 _TIFFfreeExt(tif, sp->dctable[2]);
1061 if (sp->dctable[3] != 0)
1062 _TIFFfreeExt(tif, sp->dctable[3]);
1063 if (sp->actable[0] != 0)
1064 _TIFFfreeExt(tif, sp->actable[0]);
1065 if (sp->actable[1] != 0)
1066 _TIFFfreeExt(tif, sp->actable[1]);
1067 if (sp->actable[2] != 0)
1068 _TIFFfreeExt(tif, sp->actable[2]);
1069 if (sp->actable[3] != 0)
1070 _TIFFfreeExt(tif, sp->actable[3]);
1071 if (sp->libjpeg_session_active != 0)
1072 OJPEGLibjpegSessionAbort(tif);
1073 if (sp->subsampling_convert_ycbcrbuf != 0)
1074 _TIFFfreeExt(tif, sp->subsampling_convert_ycbcrbuf);
1075 if (sp->subsampling_convert_ycbcrimage != 0)
1076 _TIFFfreeExt(tif, sp->subsampling_convert_ycbcrimage);
1077 if (sp->skip_buffer != 0)
1078 _TIFFfreeExt(tif, sp->skip_buffer);
1079 _TIFFfreeExt(tif, sp);
1080 tif->tif_data = NULL;
1082 }
1083}
1084
1085static void OJPEGSubsamplingCorrect(TIFF *tif)
1086{
1087 static const char module[] = "OJPEGSubsamplingCorrect";
1088 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1089 uint8_t mh;
1090 uint8_t mv;
1091
1092 assert(sp->subsamplingcorrect_done == 0);
1093 if ((tif->tif_dir.td_samplesperpixel != 3) ||
1096 {
1097 if (sp->subsampling_tag != 0)
1099 "Subsampling tag not appropriate for this "
1100 "Photometric and/or SamplesPerPixel");
1101 sp->subsampling_hor = 1;
1102 sp->subsampling_ver = 1;
1103 sp->subsampling_force_desubsampling_inside_decompression = 0;
1104 }
1105 else
1106 {
1107 sp->subsamplingcorrect_done = 1;
1108 mh = sp->subsampling_hor;
1109 mv = sp->subsampling_ver;
1110 sp->subsamplingcorrect = 1;
1111 OJPEGReadHeaderInfoSec(tif);
1112 if (sp->subsampling_force_desubsampling_inside_decompression != 0)
1113 {
1114 sp->subsampling_hor = 1;
1115 sp->subsampling_ver = 1;
1116 }
1117 sp->subsamplingcorrect = 0;
1118 if (((sp->subsampling_hor != mh) || (sp->subsampling_ver != mv)) &&
1119 (sp->subsampling_force_desubsampling_inside_decompression == 0))
1120 {
1121 if (sp->subsampling_tag == 0)
1123 tif, module,
1124 "Subsampling tag is not set, yet subsampling inside JPEG "
1125 "data [%" PRIu8 ",%" PRIu8
1126 "] does not match default values [2,2]; assuming "
1127 "subsampling inside JPEG data is correct",
1128 sp->subsampling_hor, sp->subsampling_ver);
1129 else
1131 tif, module,
1132 "Subsampling inside JPEG data [%" PRIu8 ",%" PRIu8
1133 "] does not match subsampling tag values [%" PRIu8
1134 ",%" PRIu8
1135 "]; assuming subsampling inside JPEG data is correct",
1136 sp->subsampling_hor, sp->subsampling_ver, mh, mv);
1137 }
1138 if (sp->subsampling_force_desubsampling_inside_decompression != 0)
1139 {
1140 if (sp->subsampling_tag == 0)
1142 tif, module,
1143 "Subsampling tag is not set, yet subsampling inside JPEG "
1144 "data does not match default values [2,2] (nor any other "
1145 "values allowed in TIFF); assuming subsampling inside JPEG "
1146 "data is correct and desubsampling inside JPEG "
1147 "decompression");
1148 else
1150 tif, module,
1151 "Subsampling inside JPEG data does not match subsampling "
1152 "tag values [%" PRIu8 ",%" PRIu8
1153 "] (nor any other values allowed in TIFF); assuming "
1154 "subsampling inside JPEG data is correct and desubsampling "
1155 "inside JPEG decompression",
1156 mh, mv);
1157 }
1158 if (sp->subsampling_force_desubsampling_inside_decompression == 0)
1159 {
1160 if (sp->subsampling_hor < sp->subsampling_ver)
1162 "Subsampling values [%" PRIu8 ",%" PRIu8
1163 "] are not allowed in TIFF",
1164 sp->subsampling_hor, sp->subsampling_ver);
1165 }
1166 }
1167 sp->subsamplingcorrect_done = 1;
1168}
1169
1170static int OJPEGReadHeaderInfo(TIFF *tif)
1171{
1172 static const char module[] = "OJPEGReadHeaderInfo";
1173 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1174 assert(sp->readheader_done == 0);
1175 sp->image_width = tif->tif_dir.td_imagewidth;
1176 sp->image_length = tif->tif_dir.td_imagelength;
1177 if (isTiled(tif))
1178 {
1179 sp->strile_width = tif->tif_dir.td_tilewidth;
1180 sp->strile_length = tif->tif_dir.td_tilelength;
1181 sp->strile_length_total =
1182 ((sp->image_length + sp->strile_length - 1) / sp->strile_length) *
1183 sp->strile_length;
1184 }
1185 else
1186 {
1187 sp->strile_width = sp->image_width;
1188 sp->strile_length = tif->tif_dir.td_rowsperstrip;
1189 if (sp->strile_length == (uint32_t)-1)
1190 sp->strile_length = sp->image_length;
1191 sp->strile_length_total = sp->image_length;
1192 }
1193 if (tif->tif_dir.td_samplesperpixel == 1)
1194 {
1195 sp->samples_per_pixel = 1;
1196 sp->plane_sample_offset = 0;
1197 sp->samples_per_pixel_per_plane = sp->samples_per_pixel;
1198 sp->subsampling_hor = 1;
1199 sp->subsampling_ver = 1;
1200 }
1201 else
1202 {
1203 if (tif->tif_dir.td_samplesperpixel != 3)
1204 {
1205 TIFFErrorExtR(tif, module,
1206 "SamplesPerPixel %" PRIu8
1207 " not supported for this compression scheme",
1208 sp->samples_per_pixel);
1209 return (0);
1210 }
1211 sp->samples_per_pixel = 3;
1212 sp->plane_sample_offset = 0;
1214 sp->samples_per_pixel_per_plane = 3;
1215 else
1216 sp->samples_per_pixel_per_plane = 1;
1217 }
1218 if (sp->strile_length < sp->image_length)
1219 {
1220 if (((sp->subsampling_hor != 1) && (sp->subsampling_hor != 2) &&
1221 (sp->subsampling_hor != 4)) ||
1222 ((sp->subsampling_ver != 1) && (sp->subsampling_ver != 2) &&
1223 (sp->subsampling_ver != 4)))
1224 {
1225 TIFFErrorExtR(tif, module, "Invalid subsampling values");
1226 return (0);
1227 }
1228 if (sp->strile_length % (sp->subsampling_ver * 8) != 0)
1229 {
1230 TIFFErrorExtR(tif, module,
1231 "Incompatible vertical subsampling and image "
1232 "strip/tile length");
1233 return (0);
1234 }
1235 sp->restart_interval =
1236 (uint16_t)(((sp->strile_width + sp->subsampling_hor * 8 - 1) /
1237 (sp->subsampling_hor * 8)) *
1238 (sp->strile_length / (sp->subsampling_ver * 8)));
1239 }
1240 if (OJPEGReadHeaderInfoSec(tif) == 0)
1241 return (0);
1242 sp->sos_end[0].log = 1;
1243 sp->sos_end[0].in_buffer_source = sp->in_buffer_source;
1244 sp->sos_end[0].in_buffer_next_strile = sp->in_buffer_next_strile;
1245 sp->sos_end[0].in_buffer_file_pos =
1246 sp->in_buffer_file_pos - sp->in_buffer_togo;
1247 sp->sos_end[0].in_buffer_file_togo =
1248 sp->in_buffer_file_togo + sp->in_buffer_togo;
1249 sp->readheader_done = 1;
1250 return (1);
1251}
1252
1253static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s)
1254{
1255 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1256 uint8_t m;
1257 assert(s > 0);
1258 assert(s < 3);
1259 assert(sp->sos_end[0].log != 0);
1260 assert(sp->sos_end[s].log == 0);
1261 sp->plane_sample_offset = (uint8_t)(s - 1);
1262 while (sp->sos_end[sp->plane_sample_offset].log == 0)
1263 sp->plane_sample_offset--;
1264 sp->in_buffer_source =
1265 sp->sos_end[sp->plane_sample_offset].in_buffer_source;
1266 sp->in_buffer_next_strile =
1267 sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile;
1268 sp->in_buffer_file_pos =
1269 sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos;
1270 sp->in_buffer_file_pos_log = 0;
1271 sp->in_buffer_file_togo =
1272 sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo;
1273 sp->in_buffer_togo = 0;
1274 sp->in_buffer_cur = 0;
1275 while (sp->plane_sample_offset < s)
1276 {
1277 do
1278 {
1279 if (OJPEGReadByte(sp, &m) == 0)
1280 return (0);
1281 if (m == 255)
1282 {
1283 do
1284 {
1285 if (OJPEGReadByte(sp, &m) == 0)
1286 return (0);
1287 if (m != 255)
1288 break;
1289 } while (1);
1290 if (m == JPEG_MARKER_SOS)
1291 break;
1292 }
1293 } while (1);
1294 sp->plane_sample_offset++;
1295 if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0)
1296 return (0);
1297 sp->sos_end[sp->plane_sample_offset].log = 1;
1298 sp->sos_end[sp->plane_sample_offset].in_buffer_source =
1299 sp->in_buffer_source;
1300 sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile =
1301 sp->in_buffer_next_strile;
1302 sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos =
1303 sp->in_buffer_file_pos - sp->in_buffer_togo;
1304 sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo =
1305 sp->in_buffer_file_togo + sp->in_buffer_togo;
1306 }
1307 return (1);
1308}
1309
1310static int OJPEGWriteHeaderInfo(TIFF *tif)
1311{
1312 static const char module[] = "OJPEGWriteHeaderInfo";
1313 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1314 uint8_t **m;
1315 uint32_t n;
1316 /* if a previous attempt failed, don't try again */
1317 if (sp->libjpeg_session_active != 0)
1318 return 0;
1319 sp->out_state = ososSoi;
1320 sp->restart_index = 0;
1321 jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr));
1322 sp->libjpeg_jpeg_error_mgr.output_message =
1323 OJPEGLibjpegJpegErrorMgrOutputMessage;
1324 sp->libjpeg_jpeg_error_mgr.error_exit = OJPEGLibjpegJpegErrorMgrErrorExit;
1325 sp->libjpeg_jpeg_decompress_struct.err = &(sp->libjpeg_jpeg_error_mgr);
1326 sp->libjpeg_jpeg_decompress_struct.client_data = (void *)tif;
1327 if (jpeg_create_decompress_encap(
1328 sp, &(sp->libjpeg_jpeg_decompress_struct)) == 0)
1329 return (0);
1330 sp->libjpeg_session_active = 1;
1331 sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = 0;
1332 sp->libjpeg_jpeg_source_mgr.init_source =
1333 OJPEGLibjpegJpegSourceMgrInitSource;
1334 sp->libjpeg_jpeg_source_mgr.fill_input_buffer =
1335 OJPEGLibjpegJpegSourceMgrFillInputBuffer;
1336 sp->libjpeg_jpeg_source_mgr.skip_input_data =
1337 OJPEGLibjpegJpegSourceMgrSkipInputData;
1338 sp->libjpeg_jpeg_source_mgr.resync_to_restart =
1339 OJPEGLibjpegJpegSourceMgrResyncToRestart;
1340 sp->libjpeg_jpeg_source_mgr.term_source =
1341 OJPEGLibjpegJpegSourceMgrTermSource;
1342 sp->libjpeg_jpeg_decompress_struct.src = &(sp->libjpeg_jpeg_source_mgr);
1343 if (jpeg_read_header_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), 1) ==
1344 0)
1345 return (0);
1346 if ((sp->subsampling_force_desubsampling_inside_decompression == 0) &&
1347 (sp->samples_per_pixel_per_plane > 1))
1348 {
1349 sp->libjpeg_jpeg_decompress_struct.raw_data_out = 1;
1350#if JPEG_LIB_VERSION >= 70
1351 sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling = FALSE;
1352#endif
1353 sp->libjpeg_jpeg_query_style = 0;
1354 if (sp->subsampling_convert_log == 0)
1355 {
1356 assert(sp->subsampling_convert_ycbcrbuf == 0);
1357 assert(sp->subsampling_convert_ycbcrimage == 0);
1358 /* Check for division by zero. */
1359 if (sp->subsampling_hor == 0 || sp->subsampling_ver == 0)
1360 return (0);
1361 sp->subsampling_convert_ylinelen =
1362 ((sp->strile_width + sp->subsampling_hor * 8 - 1) /
1363 (sp->subsampling_hor * 8) * sp->subsampling_hor * 8);
1364 sp->subsampling_convert_ylines = sp->subsampling_ver * 8;
1365 sp->subsampling_convert_clinelen =
1366 sp->subsampling_convert_ylinelen / sp->subsampling_hor;
1367 sp->subsampling_convert_clines = 8;
1368 sp->subsampling_convert_ybuflen = sp->subsampling_convert_ylinelen *
1369 sp->subsampling_convert_ylines;
1370 sp->subsampling_convert_cbuflen = sp->subsampling_convert_clinelen *
1371 sp->subsampling_convert_clines;
1372 sp->subsampling_convert_ycbcrbuflen =
1373 sp->subsampling_convert_ybuflen +
1374 2 * sp->subsampling_convert_cbuflen;
1375 /* The calloc is not normally necessary, except in some edge/broken
1376 * cases */
1377 /* for example for a tiled image of height 1 with a tile height of 1
1378 * and subsampling_hor=subsampling_ver=2 */
1379 /* In that case, libjpeg will only fill the 8 first lines of the 16
1380 * lines */
1381 /* See https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=16844
1382 */
1383 /* Even if this case is allowed (?), its handling is broken because
1384 * OJPEGPreDecode() should also likely */
1385 /* reset subsampling_convert_state to 0 when changing tile. */
1386 sp->subsampling_convert_ycbcrbuf =
1387 _TIFFcallocExt(tif, 1, sp->subsampling_convert_ycbcrbuflen);
1388 if (sp->subsampling_convert_ycbcrbuf == 0)
1389 {
1390 TIFFErrorExtR(tif, module, "Out of memory");
1391 return (0);
1392 }
1393 sp->subsampling_convert_ybuf = sp->subsampling_convert_ycbcrbuf;
1394 sp->subsampling_convert_cbbuf =
1395 sp->subsampling_convert_ybuf + sp->subsampling_convert_ybuflen;
1396 sp->subsampling_convert_crbuf =
1397 sp->subsampling_convert_cbbuf + sp->subsampling_convert_cbuflen;
1398 sp->subsampling_convert_ycbcrimagelen =
1399 3 + sp->subsampling_convert_ylines +
1400 2 * sp->subsampling_convert_clines;
1401 sp->subsampling_convert_ycbcrimage = _TIFFmallocExt(
1402 tif, sp->subsampling_convert_ycbcrimagelen * sizeof(uint8_t *));
1403 if (sp->subsampling_convert_ycbcrimage == 0)
1404 {
1405 TIFFErrorExtR(tif, module, "Out of memory");
1406 return (0);
1407 }
1408 m = sp->subsampling_convert_ycbcrimage;
1409 *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3);
1410 *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3 +
1411 sp->subsampling_convert_ylines);
1412 *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3 +
1413 sp->subsampling_convert_ylines +
1414 sp->subsampling_convert_clines);
1415 for (n = 0; n < sp->subsampling_convert_ylines; n++)
1416 *m++ = sp->subsampling_convert_ybuf +
1417 n * sp->subsampling_convert_ylinelen;
1418 for (n = 0; n < sp->subsampling_convert_clines; n++)
1419 *m++ = sp->subsampling_convert_cbbuf +
1420 n * sp->subsampling_convert_clinelen;
1421 for (n = 0; n < sp->subsampling_convert_clines; n++)
1422 *m++ = sp->subsampling_convert_crbuf +
1423 n * sp->subsampling_convert_clinelen;
1424 sp->subsampling_convert_clinelenout =
1425 sp->strile_width / sp->subsampling_hor +
1426 ((sp->strile_width % sp->subsampling_hor) != 0 ? 1 : 0);
1427 sp->subsampling_convert_state = 0;
1428 sp->error_in_raw_data_decoding = 0;
1429 sp->bytes_per_line =
1430 sp->subsampling_convert_clinelenout *
1431 (sp->subsampling_ver * sp->subsampling_hor + 2);
1432 sp->lines_per_strile =
1433 sp->strile_length / sp->subsampling_ver +
1434 ((sp->strile_length % sp->subsampling_ver) != 0 ? 1 : 0);
1435 sp->subsampling_convert_log = 1;
1436 }
1437 }
1438 else
1439 {
1440 sp->libjpeg_jpeg_decompress_struct.jpeg_color_space = JCS_UNKNOWN;
1441 sp->libjpeg_jpeg_decompress_struct.out_color_space = JCS_UNKNOWN;
1442 sp->libjpeg_jpeg_query_style = 1;
1443 sp->bytes_per_line = sp->samples_per_pixel_per_plane * sp->strile_width;
1444 sp->lines_per_strile = sp->strile_length;
1445 }
1446 if (jpeg_start_decompress_encap(sp,
1447 &(sp->libjpeg_jpeg_decompress_struct)) == 0)
1448 return (0);
1449 if (sp->libjpeg_jpeg_decompress_struct.image_width != sp->strile_width)
1450 {
1451 TIFFErrorExtR(tif, module,
1452 "jpeg_start_decompress() returned image_width = %u, "
1453 "expected %" PRIu32,
1454 sp->libjpeg_jpeg_decompress_struct.image_width,
1455 sp->strile_width);
1456 return 0;
1457 }
1458 if (sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor !=
1459 sp->subsampling_hor ||
1460 sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor !=
1461 sp->subsampling_ver)
1462 {
1463 TIFFErrorExtR(tif, module,
1464 "jpeg_start_decompress() returned max_h_samp_factor = %d "
1465 "and max_v_samp_factor = %d, expected %" PRIu8
1466 " and %" PRIu8,
1467 sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor,
1468 sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor,
1469 sp->subsampling_hor, sp->subsampling_ver);
1470 return 0;
1471 }
1472
1473 sp->writeheader_done = 1;
1474 return (1);
1475}
1476
1477static void OJPEGLibjpegSessionAbort(TIFF *tif)
1478{
1479 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1480 assert(sp->libjpeg_session_active != 0);
1481 jpeg_destroy((jpeg_common_struct *)(&(sp->libjpeg_jpeg_decompress_struct)));
1482 sp->libjpeg_session_active = 0;
1483}
1484
1485static int OJPEGReadHeaderInfoSec(TIFF *tif)
1486{
1487 static const char module[] = "OJPEGReadHeaderInfoSec";
1488 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1489 uint8_t m;
1490 uint16_t n;
1491 uint8_t o;
1492 if (sp->file_size == 0)
1493 sp->file_size = TIFFGetFileSize(tif);
1494 if (sp->jpeg_interchange_format != 0)
1495 {
1496 if (sp->jpeg_interchange_format >= sp->file_size)
1497 {
1498 sp->jpeg_interchange_format = 0;
1499 sp->jpeg_interchange_format_length = 0;
1500 }
1501 else
1502 {
1503 if ((sp->jpeg_interchange_format_length == 0) ||
1504 (sp->jpeg_interchange_format >
1505 UINT64_MAX - sp->jpeg_interchange_format_length) ||
1506 (sp->jpeg_interchange_format +
1507 sp->jpeg_interchange_format_length >
1508 sp->file_size))
1509 sp->jpeg_interchange_format_length =
1510 sp->file_size - sp->jpeg_interchange_format;
1511 }
1512 }
1513 sp->in_buffer_source = osibsNotSetYet;
1514 sp->in_buffer_next_strile = 0;
1515 sp->in_buffer_strile_count = tif->tif_dir.td_nstrips;
1516 sp->in_buffer_file_togo = 0;
1517 sp->in_buffer_togo = 0;
1518 do
1519 {
1520 if (OJPEGReadBytePeek(sp, &m) == 0)
1521 return (0);
1522 if (m != 255)
1523 break;
1524 OJPEGReadByteAdvance(sp);
1525 do
1526 {
1527 if (OJPEGReadByte(sp, &m) == 0)
1528 return (0);
1529 } while (m == 255);
1530 switch (m)
1531 {
1532 case JPEG_MARKER_SOI:
1533 /* this type of marker has no data, and should be skipped */
1534 break;
1535 case JPEG_MARKER_COM:
1536 case JPEG_MARKER_APP0:
1537 case JPEG_MARKER_APP0 + 1:
1538 case JPEG_MARKER_APP0 + 2:
1539 case JPEG_MARKER_APP0 + 3:
1540 case JPEG_MARKER_APP0 + 4:
1541 case JPEG_MARKER_APP0 + 5:
1542 case JPEG_MARKER_APP0 + 6:
1543 case JPEG_MARKER_APP0 + 7:
1544 case JPEG_MARKER_APP0 + 8:
1545 case JPEG_MARKER_APP0 + 9:
1546 case JPEG_MARKER_APP0 + 10:
1547 case JPEG_MARKER_APP0 + 11:
1548 case JPEG_MARKER_APP0 + 12:
1549 case JPEG_MARKER_APP0 + 13:
1550 case JPEG_MARKER_APP0 + 14:
1551 case JPEG_MARKER_APP0 + 15:
1552 /* this type of marker has data, but it has no use to us (and no
1553 * place here) and should be skipped */
1554 if (OJPEGReadWord(sp, &n) == 0)
1555 return (0);
1556 if (n < 2)
1557 {
1558 if (sp->subsamplingcorrect == 0)
1559 TIFFErrorExtR(tif, module, "Corrupt JPEG data");
1560 return (0);
1561 }
1562 if (n > 2)
1563 OJPEGReadSkip(sp, n - 2);
1564 break;
1565 case JPEG_MARKER_DRI:
1566 if (OJPEGReadHeaderInfoSecStreamDri(tif) == 0)
1567 return (0);
1568 break;
1569 case JPEG_MARKER_DQT:
1570 if (OJPEGReadHeaderInfoSecStreamDqt(tif) == 0)
1571 return (0);
1572 break;
1573 case JPEG_MARKER_DHT:
1574 if (OJPEGReadHeaderInfoSecStreamDht(tif) == 0)
1575 return (0);
1576 break;
1577 case JPEG_MARKER_SOF0:
1578 case JPEG_MARKER_SOF1:
1579 case JPEG_MARKER_SOF3:
1580 if (OJPEGReadHeaderInfoSecStreamSof(tif, m) == 0)
1581 return (0);
1582 if (sp->subsamplingcorrect != 0)
1583 return (1);
1584 break;
1585 case JPEG_MARKER_SOS:
1586 if (sp->subsamplingcorrect != 0)
1587 return (1);
1588 assert(sp->plane_sample_offset == 0);
1589 if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0)
1590 return (0);
1591 break;
1592 default:
1593 TIFFErrorExtR(tif, module,
1594 "Unknown marker type %" PRIu8 " in JPEG data", m);
1595 return (0);
1596 }
1597 } while (m != JPEG_MARKER_SOS);
1598 if (sp->subsamplingcorrect)
1599 return (1);
1600 if (sp->sof_log == 0)
1601 {
1602 if (OJPEGReadHeaderInfoSecTablesQTable(tif) == 0)
1603 return (0);
1604 sp->sof_marker_id = JPEG_MARKER_SOF0;
1605 for (o = 0; o < sp->samples_per_pixel; o++)
1606 sp->sof_c[o] = o;
1607 sp->sof_hv[0] = ((sp->subsampling_hor << 4) | sp->subsampling_ver);
1608 for (o = 1; o < sp->samples_per_pixel; o++)
1609 sp->sof_hv[o] = 17;
1610 sp->sof_x = sp->strile_width;
1611 sp->sof_y = sp->strile_length_total;
1612 sp->sof_log = 1;
1613 if (OJPEGReadHeaderInfoSecTablesDcTable(tif) == 0)
1614 return (0);
1615 if (OJPEGReadHeaderInfoSecTablesAcTable(tif) == 0)
1616 return (0);
1617 for (o = 1; o < sp->samples_per_pixel; o++)
1618 sp->sos_cs[o] = o;
1619 }
1620 return (1);
1621}
1622
1623static int OJPEGReadHeaderInfoSecStreamDri(TIFF *tif)
1624{
1625 /* This could easily cause trouble in some cases... but no such cases have
1626 occurred so far */
1627 static const char module[] = "OJPEGReadHeaderInfoSecStreamDri";
1628 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1629 uint16_t m;
1630 if (OJPEGReadWord(sp, &m) == 0)
1631 return (0);
1632 if (m != 4)
1633 {
1634 TIFFErrorExtR(tif, module, "Corrupt DRI marker in JPEG data");
1635 return (0);
1636 }
1637 if (OJPEGReadWord(sp, &m) == 0)
1638 return (0);
1639 sp->restart_interval = m;
1640 return (1);
1641}
1642
1643static int OJPEGReadHeaderInfoSecStreamDqt(TIFF *tif)
1644{
1645 /* this is a table marker, and it is to be saved as a whole for exact
1646 * pushing on the jpeg stream later on */
1647 static const char module[] = "OJPEGReadHeaderInfoSecStreamDqt";
1648 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1649 uint16_t m;
1650 uint32_t na;
1651 uint8_t *nb;
1652 uint8_t o;
1653 if (OJPEGReadWord(sp, &m) == 0)
1654 return (0);
1655 if (m <= 2)
1656 {
1657 if (sp->subsamplingcorrect == 0)
1658 TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data");
1659 return (0);
1660 }
1661 if (sp->subsamplingcorrect != 0)
1662 OJPEGReadSkip(sp, m - 2);
1663 else
1664 {
1665 m -= 2;
1666 do
1667 {
1668 if (m < 65)
1669 {
1670 TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data");
1671 return (0);
1672 }
1673 na = sizeof(uint32_t) + 69;
1674 nb = _TIFFmallocExt(tif, na);
1675 if (nb == 0)
1676 {
1677 TIFFErrorExtR(tif, module, "Out of memory");
1678 return (0);
1679 }
1680 *(uint32_t *)nb = na;
1681 nb[sizeof(uint32_t)] = 255;
1682 nb[sizeof(uint32_t) + 1] = JPEG_MARKER_DQT;
1683 nb[sizeof(uint32_t) + 2] = 0;
1684 nb[sizeof(uint32_t) + 3] = 67;
1685 if (OJPEGReadBlock(sp, 65, &nb[sizeof(uint32_t) + 4]) == 0)
1686 {
1687 _TIFFfreeExt(tif, nb);
1688 return (0);
1689 }
1690 o = nb[sizeof(uint32_t) + 4] & 15;
1691 if (3 < o)
1692 {
1693 TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data");
1694 _TIFFfreeExt(tif, nb);
1695 return (0);
1696 }
1697 if (sp->qtable[o] != 0)
1698 _TIFFfreeExt(tif, sp->qtable[o]);
1699 sp->qtable[o] = nb;
1700 m -= 65;
1701 } while (m > 0);
1702 }
1703 return (1);
1704}
1705
1706static int OJPEGReadHeaderInfoSecStreamDht(TIFF *tif)
1707{
1708 /* this is a table marker, and it is to be saved as a whole for exact
1709 * pushing on the jpeg stream later on */
1710 /* TODO: the following assumes there is only one table in this marker... but
1711 * i'm not quite sure that assumption is guaranteed correct */
1712 static const char module[] = "OJPEGReadHeaderInfoSecStreamDht";
1713 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1714 uint16_t m;
1715 uint32_t na;
1716 uint8_t *nb;
1717 uint8_t o;
1718 if (OJPEGReadWord(sp, &m) == 0)
1719 return (0);
1720 if (m <= 2)
1721 {
1722 if (sp->subsamplingcorrect == 0)
1723 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
1724 return (0);
1725 }
1726 if (sp->subsamplingcorrect != 0)
1727 {
1728 OJPEGReadSkip(sp, m - 2);
1729 }
1730 else
1731 {
1732 na = sizeof(uint32_t) + 2 + m;
1733 nb = _TIFFmallocExt(tif, na);
1734 if (nb == 0)
1735 {
1736 TIFFErrorExtR(tif, module, "Out of memory");
1737 return (0);
1738 }
1739 *(uint32_t *)nb = na;
1740 nb[sizeof(uint32_t)] = 255;
1741 nb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT;
1742 nb[sizeof(uint32_t) + 2] = (m >> 8);
1743 nb[sizeof(uint32_t) + 3] = (m & 255);
1744 if (OJPEGReadBlock(sp, m - 2, &nb[sizeof(uint32_t) + 4]) == 0)
1745 {
1746 _TIFFfreeExt(tif, nb);
1747 return (0);
1748 }
1749 o = nb[sizeof(uint32_t) + 4];
1750 if ((o & 240) == 0)
1751 {
1752 if (3 < o)
1753 {
1754 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
1755 _TIFFfreeExt(tif, nb);
1756 return (0);
1757 }
1758 if (sp->dctable[o] != 0)
1759 _TIFFfreeExt(tif, sp->dctable[o]);
1760 sp->dctable[o] = nb;
1761 }
1762 else
1763 {
1764 if ((o & 240) != 16)
1765 {
1766 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
1767 _TIFFfreeExt(tif, nb);
1768 return (0);
1769 }
1770 o &= 15;
1771 if (3 < o)
1772 {
1773 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
1774 _TIFFfreeExt(tif, nb);
1775 return (0);
1776 }
1777 if (sp->actable[o] != 0)
1778 _TIFFfreeExt(tif, sp->actable[o]);
1779 sp->actable[o] = nb;
1780 }
1781 }
1782 return (1);
1783}
1784
1785static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id)
1786{
1787 /* this marker needs to be checked, and part of its data needs to be saved
1788 * for regeneration later on */
1789 static const char module[] = "OJPEGReadHeaderInfoSecStreamSof";
1790 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1791 uint16_t m;
1792 uint16_t n;
1793 uint8_t o;
1794 uint16_t p;
1795 uint16_t q;
1796 if (sp->sof_log != 0)
1797 {
1798 TIFFErrorExtR(tif, module, "Corrupt JPEG data");
1799 return (0);
1800 }
1801 if (sp->subsamplingcorrect == 0)
1802 sp->sof_marker_id = marker_id;
1803 /* Lf: data length */
1804 if (OJPEGReadWord(sp, &m) == 0)
1805 return (0);
1806 if (m < 11)
1807 {
1808 if (sp->subsamplingcorrect == 0)
1809 TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data");
1810 return (0);
1811 }
1812 m -= 8;
1813 if (m % 3 != 0)
1814 {
1815 if (sp->subsamplingcorrect == 0)
1816 TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data");
1817 return (0);
1818 }
1819 n = m / 3;
1820 if (sp->subsamplingcorrect == 0)
1821 {
1822 if (n != sp->samples_per_pixel)
1823 {
1825 tif, module,
1826 "JPEG compressed data indicates unexpected number of samples");
1827 return (0);
1828 }
1829 }
1830 /* P: Sample precision */
1831 if (OJPEGReadByte(sp, &o) == 0)
1832 return (0);
1833 if (o != 8)
1834 {
1835 if (sp->subsamplingcorrect == 0)
1836 TIFFErrorExtR(tif, module,
1837 "JPEG compressed data indicates unexpected number of "
1838 "bits per sample");
1839 return (0);
1840 }
1841 /* Y: Number of lines, X: Number of samples per line */
1842 if (sp->subsamplingcorrect)
1843 OJPEGReadSkip(sp, 4);
1844 else
1845 {
1846 /* Y: Number of lines */
1847 if (OJPEGReadWord(sp, &p) == 0)
1848 return (0);
1849 if (((uint32_t)p < sp->image_length) &&
1850 ((uint32_t)p < sp->strile_length_total))
1851 {
1852 TIFFErrorExtR(tif, module,
1853 "JPEG compressed data indicates unexpected height");
1854 return (0);
1855 }
1856 sp->sof_y = p;
1857 /* X: Number of samples per line */
1858 if (OJPEGReadWord(sp, &p) == 0)
1859 return (0);
1860 if (((uint32_t)p < sp->image_width) && ((uint32_t)p < sp->strile_width))
1861 {
1862 TIFFErrorExtR(tif, module,
1863 "JPEG compressed data indicates unexpected width");
1864 return (0);
1865 }
1866 if ((uint32_t)p > sp->strile_width)
1867 {
1868 TIFFErrorExtR(tif, module,
1869 "JPEG compressed data image width exceeds expected "
1870 "image width");
1871 return (0);
1872 }
1873 sp->sof_x = p;
1874 }
1875 /* Nf: Number of image components in frame */
1876 if (OJPEGReadByte(sp, &o) == 0)
1877 return (0);
1878 if (o != n)
1879 {
1880 if (sp->subsamplingcorrect == 0)
1881 TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data");
1882 return (0);
1883 }
1884 /* per component stuff */
1885 /* TODO: double-check that flow implies that n cannot be as big as to make
1886 * us overflow sof_c, sof_hv and sof_tq arrays */
1887 for (q = 0; q < n; q++)
1888 {
1889 /* C: Component identifier */
1890 if (OJPEGReadByte(sp, &o) == 0)
1891 return (0);
1892 if (sp->subsamplingcorrect == 0)
1893 sp->sof_c[q] = o;
1894 /* H: Horizontal sampling factor, and V: Vertical sampling factor */
1895 if (OJPEGReadByte(sp, &o) == 0)
1896 return (0);
1897 if (sp->subsamplingcorrect != 0)
1898 {
1899 if (q == 0)
1900 {
1901 sp->subsampling_hor = (o >> 4);
1902 sp->subsampling_ver = (o & 15);
1903 if (((sp->subsampling_hor != 1) && (sp->subsampling_hor != 2) &&
1904 (sp->subsampling_hor != 4)) ||
1905 ((sp->subsampling_ver != 1) && (sp->subsampling_ver != 2) &&
1906 (sp->subsampling_ver != 4)))
1907 sp->subsampling_force_desubsampling_inside_decompression =
1908 1;
1909 }
1910 else
1911 {
1912 if (o != 17)
1913 sp->subsampling_force_desubsampling_inside_decompression =
1914 1;
1915 }
1916 }
1917 else
1918 {
1919 sp->sof_hv[q] = o;
1920 if (sp->subsampling_force_desubsampling_inside_decompression == 0)
1921 {
1922 if (q == 0)
1923 {
1924 if (o != ((sp->subsampling_hor << 4) | sp->subsampling_ver))
1925 {
1926 TIFFErrorExtR(tif, module,
1927 "JPEG compressed data indicates "
1928 "unexpected subsampling values");
1929 return (0);
1930 }
1931 }
1932 else
1933 {
1934 if (o != 17)
1935 {
1936 TIFFErrorExtR(tif, module,
1937 "JPEG compressed data indicates "
1938 "unexpected subsampling values");
1939 return (0);
1940 }
1941 }
1942 }
1943 }
1944 /* Tq: Quantization table destination selector */
1945 if (OJPEGReadByte(sp, &o) == 0)
1946 return (0);
1947 if (sp->subsamplingcorrect == 0)
1948 sp->sof_tq[q] = o;
1949 }
1950 if (sp->subsamplingcorrect == 0)
1951 sp->sof_log = 1;
1952 return (1);
1953}
1954
1955static int OJPEGReadHeaderInfoSecStreamSos(TIFF *tif)
1956{
1957 /* this marker needs to be checked, and part of its data needs to be saved
1958 * for regeneration later on */
1959 static const char module[] = "OJPEGReadHeaderInfoSecStreamSos";
1960 OJPEGState *sp = (OJPEGState *)tif->tif_data;
1961 uint16_t m;
1962 uint8_t n;
1963 uint8_t o;
1964 assert(sp->subsamplingcorrect == 0);
1965 if (sp->sof_log == 0)
1966 {
1967 TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data");
1968 return (0);
1969 }
1970 /* Ls */
1971 if (OJPEGReadWord(sp, &m) == 0)
1972 return (0);
1973 if (m != 6 + sp->samples_per_pixel_per_plane * 2)
1974 {
1975 TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data");
1976 return (0);
1977 }
1978 /* Ns */
1979 if (OJPEGReadByte(sp, &n) == 0)
1980 return (0);
1981 if (n != sp->samples_per_pixel_per_plane)
1982 {
1983 TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data");
1984 return (0);
1985 }
1986 /* Cs, Td, and Ta */
1987 for (o = 0; o < sp->samples_per_pixel_per_plane; o++)
1988 {
1989 /* Cs */
1990 if (OJPEGReadByte(sp, &n) == 0)
1991 return (0);
1992 sp->sos_cs[sp->plane_sample_offset + o] = n;
1993 /* Td and Ta */
1994 if (OJPEGReadByte(sp, &n) == 0)
1995 return (0);
1996 sp->sos_tda[sp->plane_sample_offset + o] = n;
1997 }
1998 /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as
1999 * per LibJpeg source */
2000 OJPEGReadSkip(sp, 3);
2001 return (1);
2002}
2003
2004static int OJPEGReadHeaderInfoSecTablesQTable(TIFF *tif)
2005{
2006 static const char module[] = "OJPEGReadHeaderInfoSecTablesQTable";
2007 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2008 uint8_t m;
2009 uint8_t n;
2010 uint32_t oa;
2011 uint8_t *ob;
2012 uint32_t p;
2013 if (sp->qtable_offset[0] == 0)
2014 {
2015 TIFFErrorExtR(tif, module, "Missing JPEG tables");
2016 return (0);
2017 }
2018 sp->in_buffer_file_pos_log = 0;
2019 for (m = 0; m < sp->samples_per_pixel; m++)
2020 {
2021 if ((sp->qtable_offset[m] != 0) &&
2022 ((m == 0) || (sp->qtable_offset[m] != sp->qtable_offset[m - 1])))
2023 {
2024 for (n = 0; n < m - 1; n++)
2025 {
2026 if (sp->qtable_offset[m] == sp->qtable_offset[n])
2027 {
2028 TIFFErrorExtR(tif, module, "Corrupt JpegQTables tag value");
2029 return (0);
2030 }
2031 }
2032 oa = sizeof(uint32_t) + 69;
2033 ob = _TIFFmallocExt(tif, oa);
2034 if (ob == 0)
2035 {
2036 TIFFErrorExtR(tif, module, "Out of memory");
2037 return (0);
2038 }
2039 *(uint32_t *)ob = oa;
2040 ob[sizeof(uint32_t)] = 255;
2041 ob[sizeof(uint32_t) + 1] = JPEG_MARKER_DQT;
2042 ob[sizeof(uint32_t) + 2] = 0;
2043 ob[sizeof(uint32_t) + 3] = 67;
2044 ob[sizeof(uint32_t) + 4] = m;
2045 TIFFSeekFile(tif, sp->qtable_offset[m], SEEK_SET);
2046 p = (uint32_t)TIFFReadFile(tif, &ob[sizeof(uint32_t) + 5], 64);
2047 if (p != 64)
2048 {
2049 _TIFFfreeExt(tif, ob);
2050 return (0);
2051 }
2052 if (sp->qtable[m] != 0)
2053 _TIFFfreeExt(tif, sp->qtable[m]);
2054 sp->qtable[m] = ob;
2055 sp->sof_tq[m] = m;
2056 }
2057 else
2058 sp->sof_tq[m] = sp->sof_tq[m - 1];
2059 }
2060 return (1);
2061}
2062
2063static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF *tif)
2064{
2065 static const char module[] = "OJPEGReadHeaderInfoSecTablesDcTable";
2066 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2067 uint8_t m;
2068 uint8_t n;
2069 uint8_t o[16];
2070 uint32_t p;
2071 uint32_t q;
2072 uint32_t ra;
2073 uint8_t *rb;
2074 if (sp->dctable_offset[0] == 0)
2075 {
2076 TIFFErrorExtR(tif, module, "Missing JPEG tables");
2077 return (0);
2078 }
2079 sp->in_buffer_file_pos_log = 0;
2080 for (m = 0; m < sp->samples_per_pixel; m++)
2081 {
2082 if ((sp->dctable_offset[m] != 0) &&
2083 ((m == 0) || (sp->dctable_offset[m] != sp->dctable_offset[m - 1])))
2084 {
2085 for (n = 0; n < m - 1; n++)
2086 {
2087 if (sp->dctable_offset[m] == sp->dctable_offset[n])
2088 {
2089 TIFFErrorExtR(tif, module,
2090 "Corrupt JpegDcTables tag value");
2091 return (0);
2092 }
2093 }
2094 TIFFSeekFile(tif, sp->dctable_offset[m], SEEK_SET);
2095 p = (uint32_t)TIFFReadFile(tif, o, 16);
2096 if (p != 16)
2097 return (0);
2098 q = 0;
2099 for (n = 0; n < 16; n++)
2100 q += o[n];
2101 ra = sizeof(uint32_t) + 21 + q;
2102 rb = _TIFFmallocExt(tif, ra);
2103 if (rb == 0)
2104 {
2105 TIFFErrorExtR(tif, module, "Out of memory");
2106 return (0);
2107 }
2108 *(uint32_t *)rb = ra;
2109 rb[sizeof(uint32_t)] = 255;
2110 rb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT;
2111 rb[sizeof(uint32_t) + 2] = (uint8_t)((19 + q) >> 8);
2112 rb[sizeof(uint32_t) + 3] = ((19 + q) & 255);
2113 rb[sizeof(uint32_t) + 4] = m;
2114 for (n = 0; n < 16; n++)
2115 rb[sizeof(uint32_t) + 5 + n] = o[n];
2116 p = (uint32_t)TIFFReadFile(tif, &(rb[sizeof(uint32_t) + 21]), q);
2117 if (p != q)
2118 {
2119 _TIFFfreeExt(tif, rb);
2120 return (0);
2121 }
2122 if (sp->dctable[m] != 0)
2123 _TIFFfreeExt(tif, sp->dctable[m]);
2124 sp->dctable[m] = rb;
2125 sp->sos_tda[m] = (m << 4);
2126 }
2127 else
2128 sp->sos_tda[m] = sp->sos_tda[m - 1];
2129 }
2130 return (1);
2131}
2132
2133static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF *tif)
2134{
2135 static const char module[] = "OJPEGReadHeaderInfoSecTablesAcTable";
2136 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2137 uint8_t m;
2138 uint8_t n;
2139 uint8_t o[16];
2140 uint32_t p;
2141 uint32_t q;
2142 uint32_t ra;
2143 uint8_t *rb;
2144 if (sp->actable_offset[0] == 0)
2145 {
2146 TIFFErrorExtR(tif, module, "Missing JPEG tables");
2147 return (0);
2148 }
2149 sp->in_buffer_file_pos_log = 0;
2150 for (m = 0; m < sp->samples_per_pixel; m++)
2151 {
2152 if ((sp->actable_offset[m] != 0) &&
2153 ((m == 0) || (sp->actable_offset[m] != sp->actable_offset[m - 1])))
2154 {
2155 for (n = 0; n < m - 1; n++)
2156 {
2157 if (sp->actable_offset[m] == sp->actable_offset[n])
2158 {
2159 TIFFErrorExtR(tif, module,
2160 "Corrupt JpegAcTables tag value");
2161 return (0);
2162 }
2163 }
2164 TIFFSeekFile(tif, sp->actable_offset[m], SEEK_SET);
2165 p = (uint32_t)TIFFReadFile(tif, o, 16);
2166 if (p != 16)
2167 return (0);
2168 q = 0;
2169 for (n = 0; n < 16; n++)
2170 q += o[n];
2171 ra = sizeof(uint32_t) + 21 + q;
2172 rb = _TIFFmallocExt(tif, ra);
2173 if (rb == 0)
2174 {
2175 TIFFErrorExtR(tif, module, "Out of memory");
2176 return (0);
2177 }
2178 *(uint32_t *)rb = ra;
2179 rb[sizeof(uint32_t)] = 255;
2180 rb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT;
2181 rb[sizeof(uint32_t) + 2] = (uint8_t)((19 + q) >> 8);
2182 rb[sizeof(uint32_t) + 3] = ((19 + q) & 255);
2183 rb[sizeof(uint32_t) + 4] = (16 | m);
2184 for (n = 0; n < 16; n++)
2185 rb[sizeof(uint32_t) + 5 + n] = o[n];
2186 p = (uint32_t)TIFFReadFile(tif, &(rb[sizeof(uint32_t) + 21]), q);
2187 if (p != q)
2188 {
2189 _TIFFfreeExt(tif, rb);
2190 return (0);
2191 }
2192 if (sp->actable[m] != 0)
2193 _TIFFfreeExt(tif, sp->actable[m]);
2194 sp->actable[m] = rb;
2195 sp->sos_tda[m] = (sp->sos_tda[m] | m);
2196 }
2197 else
2198 sp->sos_tda[m] = (sp->sos_tda[m] | (sp->sos_tda[m - 1] & 15));
2199 }
2200 return (1);
2201}
2202
2203static int OJPEGReadBufferFill(OJPEGState *sp)
2204{
2205 uint16_t m;
2206 tmsize_t n;
2207 /* TODO: double-check: when subsamplingcorrect is set, no call to
2208 * TIFFErrorExt or TIFFWarningExt should be made in any other case, seek or
2209 * read errors should be passed through */
2210 do
2211 {
2212 if (sp->in_buffer_file_togo != 0)
2213 {
2214 if (sp->in_buffer_file_pos_log == 0)
2215 {
2216 TIFFSeekFile(sp->tif, sp->in_buffer_file_pos, SEEK_SET);
2217 sp->in_buffer_file_pos_log = 1;
2218 }
2219 m = OJPEG_BUFFER;
2220 if ((uint64_t)m > sp->in_buffer_file_togo)
2221 m = (uint16_t)sp->in_buffer_file_togo;
2222 n = TIFFReadFile(sp->tif, sp->in_buffer, (tmsize_t)m);
2223 if (n == 0)
2224 return (0);
2225 assert(n > 0);
2226 assert(n <= OJPEG_BUFFER);
2227 assert(n < 65536);
2228 assert((uint64_t)n <= sp->in_buffer_file_togo);
2229 m = (uint16_t)n;
2230 sp->in_buffer_togo = m;
2231 sp->in_buffer_cur = sp->in_buffer;
2232 sp->in_buffer_file_togo -= m;
2233 sp->in_buffer_file_pos += m;
2234 break;
2235 }
2236 sp->in_buffer_file_pos_log = 0;
2237 switch (sp->in_buffer_source)
2238 {
2239 case osibsNotSetYet:
2240 if (sp->jpeg_interchange_format != 0)
2241 {
2242 sp->in_buffer_file_pos = sp->jpeg_interchange_format;
2243 sp->in_buffer_file_togo =
2244 sp->jpeg_interchange_format_length;
2245 }
2246 sp->in_buffer_source = osibsJpegInterchangeFormat;
2247 break;
2248 case osibsJpegInterchangeFormat:
2249 sp->in_buffer_source = osibsStrile;
2250 break;
2251 case osibsStrile:
2252 if (sp->in_buffer_next_strile == sp->in_buffer_strile_count)
2253 sp->in_buffer_source = osibsEof;
2254 else
2255 {
2256 int err = 0;
2257 sp->in_buffer_file_pos = TIFFGetStrileOffsetWithErr(
2258 sp->tif, sp->in_buffer_next_strile, &err);
2259 if (err)
2260 return 0;
2261 if (sp->in_buffer_file_pos != 0)
2262 {
2264 sp->tif, sp->in_buffer_next_strile, &err);
2265 if (err)
2266 return 0;
2267 if (sp->in_buffer_file_pos >= sp->file_size)
2268 sp->in_buffer_file_pos = 0;
2269 else if (bytecount == 0)
2270 sp->in_buffer_file_togo =
2271 sp->file_size - sp->in_buffer_file_pos;
2272 else
2273 {
2274 sp->in_buffer_file_togo = bytecount;
2275 if (sp->in_buffer_file_togo == 0)
2276 sp->in_buffer_file_pos = 0;
2277 else if (sp->in_buffer_file_pos >
2278 UINT64_MAX - sp->in_buffer_file_togo ||
2279 sp->in_buffer_file_pos +
2280 sp->in_buffer_file_togo >
2281 sp->file_size)
2282 sp->in_buffer_file_togo =
2283 sp->file_size - sp->in_buffer_file_pos;
2284 }
2285 }
2286 sp->in_buffer_next_strile++;
2287 }
2288 break;
2289 default:
2290 return (0);
2291 }
2292 } while (1);
2293 return (1);
2294}
2295
2296static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte)
2297{
2298 if (sp->in_buffer_togo == 0)
2299 {
2300 if (OJPEGReadBufferFill(sp) == 0)
2301 return (0);
2302 assert(sp->in_buffer_togo > 0);
2303 }
2304 *byte = *(sp->in_buffer_cur);
2305 sp->in_buffer_cur++;
2306 sp->in_buffer_togo--;
2307 return (1);
2308}
2309
2310static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte)
2311{
2312 if (sp->in_buffer_togo == 0)
2313 {
2314 if (OJPEGReadBufferFill(sp) == 0)
2315 return (0);
2316 assert(sp->in_buffer_togo > 0);
2317 }
2318 *byte = *(sp->in_buffer_cur);
2319 return (1);
2320}
2321
2322static void OJPEGReadByteAdvance(OJPEGState *sp)
2323{
2324 assert(sp->in_buffer_togo > 0);
2325 sp->in_buffer_cur++;
2326 sp->in_buffer_togo--;
2327}
2328
2329static int OJPEGReadWord(OJPEGState *sp, uint16_t *word)
2330{
2331 uint8_t m;
2332 if (OJPEGReadByte(sp, &m) == 0)
2333 return (0);
2334 *word = (m << 8);
2335 if (OJPEGReadByte(sp, &m) == 0)
2336 return (0);
2337 *word |= m;
2338 return (1);
2339}
2340
2341static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem)
2342{
2343 uint16_t mlen;
2344 uint8_t *mmem;
2345 uint16_t n;
2346 assert(len > 0);
2347 mlen = len;
2348 mmem = mem;
2349 do
2350 {
2351 if (sp->in_buffer_togo == 0)
2352 {
2353 if (OJPEGReadBufferFill(sp) == 0)
2354 return (0);
2355 assert(sp->in_buffer_togo > 0);
2356 }
2357 n = mlen;
2358 if (n > sp->in_buffer_togo)
2359 n = sp->in_buffer_togo;
2360 _TIFFmemcpy(mmem, sp->in_buffer_cur, n);
2361 sp->in_buffer_cur += n;
2362 sp->in_buffer_togo -= n;
2363 mlen -= n;
2364 mmem += n;
2365 } while (mlen > 0);
2366 return (1);
2367}
2368
2369static void OJPEGReadSkip(OJPEGState *sp, uint16_t len)
2370{
2371 uint16_t m;
2372 uint16_t n;
2373 m = len;
2374 n = m;
2375 if (n > sp->in_buffer_togo)
2376 n = sp->in_buffer_togo;
2377 sp->in_buffer_cur += n;
2378 sp->in_buffer_togo -= n;
2379 m -= n;
2380 if (m > 0)
2381 {
2382 assert(sp->in_buffer_togo == 0);
2383 n = m;
2384 if ((uint64_t)n > sp->in_buffer_file_togo)
2385 n = (uint16_t)sp->in_buffer_file_togo;
2386 sp->in_buffer_file_pos += n;
2387 sp->in_buffer_file_togo -= n;
2388 sp->in_buffer_file_pos_log = 0;
2389 /* we don't skip past jpeginterchangeformat/strile block...
2390 * if that is asked from us, we're dealing with totally bazurk
2391 * data anyway, and we've not seen this happening on any
2392 * testfile, so we might as well likely cause some other
2393 * meaningless error to be passed at some later time
2394 */
2395 }
2396}
2397
2398static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len)
2399{
2400 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2401 *len = 0;
2402 do
2403 {
2404 assert(sp->out_state <= ososEoi);
2405 switch (sp->out_state)
2406 {
2407 case ososSoi:
2408 OJPEGWriteStreamSoi(tif, mem, len);
2409 break;
2410 case ososQTable0:
2411 OJPEGWriteStreamQTable(tif, 0, mem, len);
2412 break;
2413 case ososQTable1:
2414 OJPEGWriteStreamQTable(tif, 1, mem, len);
2415 break;
2416 case ososQTable2:
2417 OJPEGWriteStreamQTable(tif, 2, mem, len);
2418 break;
2419 case ososQTable3:
2420 OJPEGWriteStreamQTable(tif, 3, mem, len);
2421 break;
2422 case ososDcTable0:
2423 OJPEGWriteStreamDcTable(tif, 0, mem, len);
2424 break;
2425 case ososDcTable1:
2426 OJPEGWriteStreamDcTable(tif, 1, mem, len);
2427 break;
2428 case ososDcTable2:
2429 OJPEGWriteStreamDcTable(tif, 2, mem, len);
2430 break;
2431 case ososDcTable3:
2432 OJPEGWriteStreamDcTable(tif, 3, mem, len);
2433 break;
2434 case ososAcTable0:
2435 OJPEGWriteStreamAcTable(tif, 0, mem, len);
2436 break;
2437 case ososAcTable1:
2438 OJPEGWriteStreamAcTable(tif, 1, mem, len);
2439 break;
2440 case ososAcTable2:
2441 OJPEGWriteStreamAcTable(tif, 2, mem, len);
2442 break;
2443 case ososAcTable3:
2444 OJPEGWriteStreamAcTable(tif, 3, mem, len);
2445 break;
2446 case ososDri:
2447 OJPEGWriteStreamDri(tif, mem, len);
2448 break;
2449 case ososSof:
2450 OJPEGWriteStreamSof(tif, mem, len);
2451 break;
2452 case ososSos:
2453 OJPEGWriteStreamSos(tif, mem, len);
2454 break;
2455 case ososCompressed:
2456 if (OJPEGWriteStreamCompressed(tif, mem, len) == 0)
2457 return (0);
2458 break;
2459 case ososRst:
2460 OJPEGWriteStreamRst(tif, mem, len);
2461 break;
2462 case ososEoi:
2463 OJPEGWriteStreamEoi(tif, mem, len);
2464 break;
2465 }
2466 } while (*len == 0);
2467 return (1);
2468}
2469
2470static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len)
2471{
2472 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2473 assert(OJPEG_BUFFER >= 2);
2474 sp->out_buffer[0] = 255;
2475 sp->out_buffer[1] = JPEG_MARKER_SOI;
2476 *len = 2;
2477 *mem = (void *)sp->out_buffer;
2478 sp->out_state++;
2479}
2480
2481static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem,
2482 uint32_t *len)
2483{
2484 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2485 if (sp->qtable[table_index] != 0)
2486 {
2487 *mem = (void *)(sp->qtable[table_index] + sizeof(uint32_t));
2488 *len = *((uint32_t *)sp->qtable[table_index]) - sizeof(uint32_t);
2489 }
2490 sp->out_state++;
2491}
2492
2493static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem,
2494 uint32_t *len)
2495{
2496 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2497 if (sp->dctable[table_index] != 0)
2498 {
2499 *mem = (void *)(sp->dctable[table_index] + sizeof(uint32_t));
2500 *len = *((uint32_t *)sp->dctable[table_index]) - sizeof(uint32_t);
2501 }
2502 sp->out_state++;
2503}
2504
2505static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem,
2506 uint32_t *len)
2507{
2508 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2509 if (sp->actable[table_index] != 0)
2510 {
2511 *mem = (void *)(sp->actable[table_index] + sizeof(uint32_t));
2512 *len = *((uint32_t *)sp->actable[table_index]) - sizeof(uint32_t);
2513 }
2514 sp->out_state++;
2515}
2516
2517static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len)
2518{
2519 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2520 assert(OJPEG_BUFFER >= 6);
2521 if (sp->restart_interval != 0)
2522 {
2523 sp->out_buffer[0] = 255;
2524 sp->out_buffer[1] = JPEG_MARKER_DRI;
2525 sp->out_buffer[2] = 0;
2526 sp->out_buffer[3] = 4;
2527 sp->out_buffer[4] = (sp->restart_interval >> 8);
2528 sp->out_buffer[5] = (sp->restart_interval & 255);
2529 *len = 6;
2530 *mem = (void *)sp->out_buffer;
2531 }
2532 sp->out_state++;
2533}
2534
2535static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len)
2536{
2537 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2538 uint8_t m;
2539 assert(OJPEG_BUFFER >= 2 + 8 + sp->samples_per_pixel_per_plane * 3);
2540 assert(255 >= 8 + sp->samples_per_pixel_per_plane * 3);
2541 sp->out_buffer[0] = 255;
2542 sp->out_buffer[1] = sp->sof_marker_id;
2543 /* Lf */
2544 sp->out_buffer[2] = 0;
2545 sp->out_buffer[3] = 8 + sp->samples_per_pixel_per_plane * 3;
2546 /* P */
2547 sp->out_buffer[4] = 8;
2548 /* Y */
2549 sp->out_buffer[5] = (uint8_t)(sp->sof_y >> 8);
2550 sp->out_buffer[6] = (sp->sof_y & 255);
2551 /* X */
2552 sp->out_buffer[7] = (uint8_t)(sp->sof_x >> 8);
2553 sp->out_buffer[8] = (sp->sof_x & 255);
2554 /* Nf */
2555 sp->out_buffer[9] = sp->samples_per_pixel_per_plane;
2556 for (m = 0; m < sp->samples_per_pixel_per_plane; m++)
2557 {
2558 /* C */
2559 sp->out_buffer[10 + m * 3] = sp->sof_c[sp->plane_sample_offset + m];
2560 /* H and V */
2561 sp->out_buffer[10 + m * 3 + 1] =
2562 sp->sof_hv[sp->plane_sample_offset + m];
2563 /* Tq */
2564 sp->out_buffer[10 + m * 3 + 2] =
2565 sp->sof_tq[sp->plane_sample_offset + m];
2566 }
2567 *len = 10 + sp->samples_per_pixel_per_plane * 3;
2568 *mem = (void *)sp->out_buffer;
2569 sp->out_state++;
2570}
2571
2572static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len)
2573{
2574 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2575 uint8_t m;
2576 assert(OJPEG_BUFFER >= 2 + 6 + sp->samples_per_pixel_per_plane * 2);
2577 assert(255 >= 6 + sp->samples_per_pixel_per_plane * 2);
2578 sp->out_buffer[0] = 255;
2579 sp->out_buffer[1] = JPEG_MARKER_SOS;
2580 /* Ls */
2581 sp->out_buffer[2] = 0;
2582 sp->out_buffer[3] = 6 + sp->samples_per_pixel_per_plane * 2;
2583 /* Ns */
2584 sp->out_buffer[4] = sp->samples_per_pixel_per_plane;
2585 for (m = 0; m < sp->samples_per_pixel_per_plane; m++)
2586 {
2587 /* Cs */
2588 sp->out_buffer[5 + m * 2] = sp->sos_cs[sp->plane_sample_offset + m];
2589 /* Td and Ta */
2590 sp->out_buffer[5 + m * 2 + 1] =
2591 sp->sos_tda[sp->plane_sample_offset + m];
2592 }
2593 /* Ss */
2594 sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2] = 0;
2595 /* Se */
2596 sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2 + 1] = 63;
2597 /* Ah and Al */
2598 sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2 + 2] = 0;
2599 *len = 8 + sp->samples_per_pixel_per_plane * 2;
2600 *mem = (void *)sp->out_buffer;
2601 sp->out_state++;
2602}
2603
2604static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len)
2605{
2606 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2607 if (sp->in_buffer_togo == 0)
2608 {
2609 if (OJPEGReadBufferFill(sp) == 0)
2610 return (0);
2611 assert(sp->in_buffer_togo > 0);
2612 }
2613 *len = sp->in_buffer_togo;
2614 *mem = (void *)sp->in_buffer_cur;
2615 sp->in_buffer_togo = 0;
2616 if (sp->in_buffer_file_togo == 0)
2617 {
2618 switch (sp->in_buffer_source)
2619 {
2620 case osibsStrile:
2621 if (sp->in_buffer_next_strile < sp->in_buffer_strile_count)
2622 sp->out_state = ososRst;
2623 else
2624 sp->out_state = ososEoi;
2625 break;
2626 case osibsEof:
2627 sp->out_state = ososEoi;
2628 break;
2629 default:
2630 break;
2631 }
2632 }
2633 return (1);
2634}
2635
2636static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len)
2637{
2638 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2639 assert(OJPEG_BUFFER >= 2);
2640 sp->out_buffer[0] = 255;
2641 sp->out_buffer[1] = JPEG_MARKER_RST0 + sp->restart_index;
2642 sp->restart_index++;
2643 if (sp->restart_index == 8)
2644 sp->restart_index = 0;
2645 *len = 2;
2646 *mem = (void *)sp->out_buffer;
2647 sp->out_state = ososCompressed;
2648}
2649
2650static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len)
2651{
2652 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2653 assert(OJPEG_BUFFER >= 2);
2654 sp->out_buffer[0] = 255;
2655 sp->out_buffer[1] = JPEG_MARKER_EOI;
2656 *len = 2;
2657 *mem = (void *)sp->out_buffer;
2658}
2659
2660#ifndef LIBJPEG_ENCAP_EXTERNAL
2661static int jpeg_create_decompress_encap(OJPEGState *sp,
2663{
2664 if (SETJMP(sp->exit_jmpbuf))
2665 return 0;
2666 else
2667 {
2669 return 1;
2670 }
2671}
2672#endif
2673
2674#ifndef LIBJPEG_ENCAP_EXTERNAL
2675static int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo,
2677{
2678 if (SETJMP(sp->exit_jmpbuf))
2679 return 0;
2680 else
2681 {
2683 return 1;
2684 }
2685}
2686#endif
2687
2688#ifndef LIBJPEG_ENCAP_EXTERNAL
2689static int jpeg_start_decompress_encap(OJPEGState *sp,
2691{
2692 if (SETJMP(sp->exit_jmpbuf))
2693 return 0;
2694 else
2695 {
2696 jpeg_start_decompress(cinfo);
2697 return 1;
2698 }
2699}
2700#endif
2701
2702#ifndef LIBJPEG_ENCAP_EXTERNAL
2703static int jpeg_read_scanlines_encap(OJPEGState *sp,
2706{
2707 if (SETJMP(sp->exit_jmpbuf))
2708 return 0;
2709 else
2710 {
2712 return 1;
2713 }
2714}
2715#endif
2716
2717#ifndef LIBJPEG_ENCAP_EXTERNAL
2718static int jpeg_read_raw_data_encap(OJPEGState *sp,
2719 jpeg_decompress_struct *cinfo, void *data,
2721{
2722 if (SETJMP(sp->exit_jmpbuf))
2723 return 0;
2724 else
2725 {
2727 return 1;
2728 }
2729}
2730#endif
2731
2732#ifndef LIBJPEG_ENCAP_EXTERNAL
2733static void jpeg_encap_unwind(TIFF *tif)
2734{
2735 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2736 LONGJMP(sp->exit_jmpbuf, 1);
2737}
2738#endif
2739
2740static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct *cinfo)
2741{
2742 char buffer[JMSG_LENGTH_MAX];
2743 (*cinfo->err->format_message)(cinfo, buffer);
2744 TIFFWarningExtR(((TIFF *)(cinfo->client_data)), "LibJpeg", "%s", buffer);
2745}
2746
2747static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct *cinfo)
2748{
2749 char buffer[JMSG_LENGTH_MAX];
2750 (*cinfo->err->format_message)(cinfo, buffer);
2751 TIFFErrorExtR(((TIFF *)(cinfo->client_data)), "LibJpeg", "%s", buffer);
2752 jpeg_encap_unwind((TIFF *)(cinfo->client_data));
2753}
2754
2755static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct *cinfo)
2756{
2757 (void)cinfo;
2758}
2759
2760static boolean
2761OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct *cinfo)
2762{
2763 TIFF *tif = (TIFF *)cinfo->client_data;
2764 OJPEGState *sp = (OJPEGState *)tif->tif_data;
2765 void *mem = 0;
2766 uint32_t len = 0U;
2767 if (OJPEGWriteStream(tif, &mem, &len) == 0)
2768 {
2769 TIFFErrorExtR(tif, "LibJpeg", "Premature end of JPEG data");
2770 jpeg_encap_unwind(tif);
2771 }
2772 sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = len;
2773 sp->libjpeg_jpeg_source_mgr.next_input_byte = mem;
2774 return (1);
2775}
2776
2777static void
2778OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct *cinfo,
2779 long num_bytes)
2780{
2781 TIFF *tif = (TIFF *)cinfo->client_data;
2782 (void)num_bytes;
2783 TIFFErrorExtR(tif, "LibJpeg", "Unexpected error");
2784 jpeg_encap_unwind(tif);
2785}
2786
2787#ifdef _MSC_VER
2788#pragma warning(push)
2789#pragma warning(disable : 4702) /* unreachable code */
2790#endif
2791static boolean
2792OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct *cinfo,
2793 int desired)
2794{
2795 TIFF *tif = (TIFF *)cinfo->client_data;
2796 (void)desired;
2797 TIFFErrorExtR(tif, "LibJpeg", "Unexpected error");
2798 jpeg_encap_unwind(tif);
2799 return (0);
2800}
2801#ifdef _MSC_VER
2802#pragma warning(pop)
2803#endif
2804
2805static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct *cinfo)
2806{
2807 (void)cinfo;
2808}
2809
2810#endif
#define U(x)
Definition: wordpad.c:45
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
int WINAPIV fprintf(FILE *file, const char *format,...)
Definition: file.c:5549
#define assert(_expr)
Definition: assert.h:32
#define PRIu8
Definition: inttypes.h:82
#define PRIu16
Definition: inttypes.h:83
#define PRIu32
Definition: inttypes.h:84
#define PRIu64
Definition: inttypes.h:28
#define va_arg(v, l)
Definition: stdarg.h:27
unsigned short uint16_t
Definition: stdint.h:35
#define UINT64_MAX
Definition: stdint.h:86
unsigned char uint8_t
Definition: stdint.h:33
char * va_list
Definition: vadefs.h:50
switch(r->id)
Definition: btrfs.c:3046
for(i=0;i< ARRAY_SIZE(offsets);i++)
GLdouble s
Definition: gl.h:2039
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLdouble n
Definition: glext.h:7729
GLuint buffer
Definition: glext.h:5915
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLbitfield flags
Definition: glext.h:7161
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
const GLfloat * m
Definition: glext.h:10848
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
static unsigned char * in_buffer
Definition: iccvid.c:87
uint32_t cc
Definition: isohybrid.c:75
jpeg_destroy(j_common_ptr cinfo)
Definition: jcomapi.c:70
jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
Definition: jdapimin.c:258
jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines)
Definition: jdapistd.c:153
jpeg_read_raw_data(j_decompress_ptr cinfo, JSAMPIMAGE data, JDIMENSION max_lines)
Definition: jdapistd.c:186
jpeg_std_error(struct jpeg_error_mgr *err)
Definition: jerror.c:232
#define SEEK_SET
Definition: jmemansi.c:26
JSAMPARRAY scanlines
Definition: jpeglib.h:1018
int desired
Definition: jpeglib.h:1121
@ JCS_UNKNOWN
Definition: jpeglib.h:221
#define jpeg_create_decompress(cinfo)
Definition: jpeglib.h:964
boolean require_image
Definition: jpeglib.h:1045
JSAMPARRAY JDIMENSION max_lines
Definition: jpeglib.h:1060
#define JMSG_LENGTH_MAX
Definition: jpeglib.h:713
const WCHAR * word
Definition: lex.c:36
int image_width
if(dx< 0)
Definition: linetemp.h:194
static const WCHAR sp[]
Definition: suminfo.c:287
static HANDLE PIO_APC_ROUTINE PVOID PIO_STATUS_BLOCK ULONG PVOID ULONG PVOID out_buffer
Definition: file.c:100
#define uint32_t
Definition: nsiface.idl:61
#define uint64_t
Definition: nsiface.idl:62
#define uint16_t
Definition: nsiface.idl:60
#define uint8_t
Definition: nsiface.idl:59
#define err(...)
static unsigned int file_size
Definition: regtests2xml.c:47
DWORD scheme
static int fd
Definition: io.c:51
#define memset(x, y, z)
Definition: compat.h:39
#define log(outFile, fmt,...)
Definition: util.h:15
uint32_t td_rowsperstrip
Definition: tif_dir.h:94
uint32_t td_imagewidth
Definition: tif_dir.h:83
uint16_t td_photometric
Definition: tif_dir.h:89
uint32_t td_tilewidth
Definition: tif_dir.h:84
uint32_t td_stripsperimage
Definition: tif_dir.h:110
uint16_t td_planarconfig
Definition: tif_dir.h:100
uint16_t td_ycbcrsubsampling[2]
Definition: tif_dir.h:132
uint16_t td_samplesperpixel
Definition: tif_dir.h:93
uint32_t td_nstrips
Definition: tif_dir.h:111
uint32_t td_imagelength
Definition: tif_dir.h:83
uint32_t td_tilelength
Definition: tif_dir.h:84
TIFFVGetMethod vgetfield
Definition: tiffio.h:376
TIFFVSetMethod vsetfield
Definition: tiffio.h:375
TIFFPrintMethod printdir
Definition: tiffio.h:377
unsigned int restart_interval
Definition: jpeglib.h:595
Definition: mem.c:349
Definition: ecma_167.h:138
Definition: tiffiop.h:113
uint32_t tif_curtile
Definition: tiffiop.h:194
TIFFCodeMethod tif_encodestrip
Definition: tiffiop.h:208
TIFFCodeMethod tif_encodetile
Definition: tiffiop.h:210
TIFFTagMethods tif_tagmethods
Definition: tiffiop.h:244
TIFFPreMethod tif_preencode
Definition: tiffiop.h:203
TIFFBoolMethod tif_fixuptags
Definition: tiffiop.h:198
TIFFPreMethod tif_predecode
Definition: tiffiop.h:200
TIFFCodeMethod tif_decodestrip
Definition: tiffiop.h:207
TIFFPostMethod tif_postdecode
Definition: tiffiop.h:239
TIFFCodeMethod tif_decoderow
Definition: tiffiop.h:205
TIFFBoolMethod tif_setupencode
Definition: tiffiop.h:201
TIFFDirectory tif_dir
Definition: tiffiop.h:157
TIFFBoolMethod tif_postencode
Definition: tiffiop.h:204
TIFFCodeMethod tif_encoderow
Definition: tiffiop.h:206
TIFFVoidMethod tif_cleanup
Definition: tiffiop.h:213
uint8_t * tif_data
Definition: tiffiop.h:216
TIFFBoolMethod tif_setupdecode
Definition: tiffiop.h:199
uint32_t tif_flags
Definition: tiffiop.h:117
uint32_t tif_curstrip
Definition: tiffiop.h:184
TIFFCodeMethod tif_decodetile
Definition: tiffiop.h:209
#define TIFFInitOJPEG
Definition: tif_codec.c:50
void _TIFFSetDefaultCompressionState(TIFF *tif)
Definition: tif_compress.c:142
int TIFFSetField(TIFF *tif, uint32_t tag,...)
Definition: tif_dir.c:1146
#define TIFFFieldSet(tif, field)
Definition: tif_dir.h:236
@ TIFF_SETGET_UINT64
Definition: tif_dir.h:253
@ TIFF_SETGET_UINT16
Definition: tif_dir.h:249
@ TIFF_SETGET_C32_UINT64
Definition: tif_dir.h:291
#define TIFFSetFieldBit(tif, field)
Definition: tif_dir.h:237
const TIFFField * TIFFFieldWithTag(TIFF *tif, uint32_t tag)
Definition: tif_dirinfo.c:845
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
Definition: tif_dirinfo.c:573
uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
Definition: tif_dirread.c:8356
uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
Definition: tif_dirread.c:8373
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Definition: tif_error.c:107
void _TIFFfreeExt(TIFF *tif, void *p)
Definition: tif_open.c:275
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Definition: tif_open.c:173
void * _TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
Definition: tif_open.c:201
void _TIFFmemset(void *p, int v, tmsize_t c)
Definition: tif_unix.c:353
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Definition: tif_unix.c:355
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Definition: tif_warning.c:80
int uint16_vap
Definition: tiff.h:100
#define TIFFTAG_JPEGDCTABLES
Definition: tiff.h:385
#define COMPRESSION_OJPEG
Definition: tiff.h:189
#define TIFFTAG_JPEGQTABLES
Definition: tiff.h:384
#define PHOTOMETRIC_ITULAB
Definition: tiff.h:230
#define TIFFTAG_JPEGIFBYTECOUNT
Definition: tiff.h:380
@ TIFF_SHORT
Definition: tiff.h:150
@ TIFF_LONG8
Definition: tiff.h:161
#define TIFFTAG_JPEGACTABLES
Definition: tiff.h:386
#define TIFFTAG_JPEGRESTARTINTERVAL
Definition: tiff.h:381
#define PHOTOMETRIC_YCBCR
Definition: tiff.h:227
#define TIFFTAG_JPEGIFOFFSET
Definition: tiff.h:379
#define TIFFTAG_YCBCRSUBSAMPLING
Definition: tiff.h:388
#define TIFFTAG_JPEGPROC
Definition: tiff.h:376
#define PLANARCONFIG_CONTIG
Definition: tiff.h:265
void(* TIFFPrintMethod)(TIFF *, FILE *, long)
Definition: tiffio.h:371
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:67
int(* TIFFVSetMethod)(TIFF *, uint32_t, va_list)
Definition: tiffio.h:369
int(* TIFFVGetMethod)(TIFF *, uint32_t, va_list)
Definition: tiffio.h:370
#define TIFF_VARIABLE2
Definition: tiffio.h:344
#define TIFFReadFile(tif, buf, size)
Definition: tiffiop.h:278
#define isTiled(tif)
Definition: tiffiop.h:274
#define TIFFGetFileSize(tif)
Definition: tiffiop.h:285
#define TIFFSeekFile(tif, off, whence)
Definition: tiffiop.h:282
#define TIFF_DIRTYDIRECT
Definition: tiffiop.h:120
#define TIFF_NOREADRAW
Definition: tiffiop.h:135
#define TIFFArrayCount(a)
Definition: tiffiop.h:333
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36