1 /* $Id: tif_ojpeg.c,v 1.69 2017-04-27 17:29:26 erouault Exp $ */
2
3 /* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
4 specification is now totally obsolete and deprecated for new applications and
5 images. This file was was created solely in order to read unconverted images
6 still present on some users' computer systems. It will never be extended
7 to write such files. Writing new-style JPEG compressed TIFFs is implemented
8 in tif_jpeg.c.
9
10 The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
11 testfiles, and anticipate as much as possible all other... But still, it may
12 fail on some. If you encounter problems, please report them on the TIFF
13 mailing list and/or to Joris Van Damme <info@awaresystems.be>.
14
15 Please read the file called "TIFF Technical Note #2" if you need to be
16 convinced this compression scheme is bad and breaks TIFF. That document
17 is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
18 and from AWare Systems' TIFF section
19 <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
20 in Adobe's specification supplements, marked "draft" up to this day, but
21 supported by the TIFF community.
22
23 This file interfaces with Release 6B of the JPEG Library written by the
24 Independent JPEG Group. Previous versions of this file required a hack inside
25 the LibJpeg library. This version no longer requires that. Remember to
26 remove the hack if you update from the old version.
27
28 Copyright (c) Joris Van Damme <info@awaresystems.be>
29 Copyright (c) AWare Systems <http://www.awaresystems.be/>
30
31 The licence agreement for this file is the same as the rest of the LibTiff
32 library.
33
34 IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
35 ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
36 OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
37 WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
38 LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
39 OF THIS SOFTWARE.
40
41 Joris Van Damme and/or AWare Systems may be available for custom
42 development. If you like what you see, and need anything similar or related,
43 contact <info@awaresystems.be>.
44 */
45
46 /* What is what, and what is not?
47
48 This decoder starts with an input stream, that is essentially the JpegInterchangeFormat
49 stream, if any, followed by the strile data, if any. This stream is read in
50 OJPEGReadByte and related functions.
51
52 It analyzes the start of this stream, until it encounters non-marker data, i.e.
53 compressed image data. Some of the header markers it sees have no actual content,
54 like the SOI marker, and APP/COM markers that really shouldn't even be there. Some
55 other markers do have content, and the valuable bits and pieces of information
56 in these markers are saved, checking all to verify that the stream is more or
57 less within expected bounds. This happens inside the OJPEGReadHeaderInfoSecStreamXxx
58 functions.
59
60 Some OJPEG imagery contains no valid JPEG header markers. This situation is picked
61 up on if we've seen no SOF marker when we're at the start of the compressed image
62 data. In this case, the tables are read from JpegXxxTables tags, and the other
63 bits and pieces of information is initialized to its most basic value. This is
64 implemented in the OJPEGReadHeaderInfoSecTablesXxx functions.
65
66 When this is complete, a good and valid JPEG header can be assembled, and this is
67 passed through to LibJpeg. When that's done, the remainder of the input stream, i.e.
68 the compressed image data, can be passed through unchanged. This is done in
69 OJPEGWriteStream functions.
70
71 LibTiff rightly expects to know the subsampling values before decompression. Just like
72 in new-style JPEG-in-TIFF, though, or even more so, actually, the YCbCrsubsampling
73 tag is notoriously unreliable. To correct these tag values with the ones inside
74 the JPEG stream, the first part of the input stream is pre-scanned in
75 OJPEGSubsamplingCorrect, making no note of any other data, reporting no warnings
76 or errors, up to the point where either these values are read, or it's clear they
77 aren't there. This means that some of the data is read twice, but we feel speed
78 in correcting these values is important enough to warrant this sacrifice. Although
79 there is currently no define or other configuration mechanism to disable this behaviour,
80 the actual header scanning is build to robustly respond with error report if it
81 should encounter an uncorrected mismatch of subsampling values. See
82 OJPEGReadHeaderInfoSecStreamSof.
83
84 The restart interval and restart markers are the most tricky part... The restart
85 interval can be specified in a tag. It can also be set inside the input JPEG stream.
86 It can be used inside the input JPEG stream. If reading from strile data, we've
87 consistently discovered the need to insert restart markers in between the different
88 striles, as is also probably the most likely interpretation of the original TIFF 6.0
89 specification. With all this setting of interval, and actual use of markers that is not
90 predictable at the time of valid JPEG header assembly, the restart thing may turn
91 out the Achilles heel of this implementation. Fortunately, most OJPEG writer vendors
92 succeed in reading back what they write, which may be the reason why we've been able
93 to discover ways that seem to work.
94
95 Some special provision is made for planarconfig separate OJPEG files. These seem
96 to consistently contain header info, a SOS marker, a plane, SOS marker, plane, SOS,
97 and plane. This may or may not be a valid JPEG configuration, we don't know and don't
98 care. We want LibTiff to be able to access the planes individually, without huge
99 buffering inside LibJpeg, anyway. So we compose headers to feed to LibJpeg, in this
100 case, that allow us to pass a single plane such that LibJpeg sees a valid
101 single-channel JPEG stream. Locating subsequent SOS markers, and thus subsequent
102 planes, is done inside OJPEGReadSecondarySos.
103
104 The benefit of the scheme is... that it works, basically. We know of no other that
105 does. It works without checking software tag, or otherwise going about things in an
106 OJPEG flavor specific manner. Instead, it is a single scheme, that covers the cases
107 with and without JpegInterchangeFormat, with and without striles, with part of
108 the header in JpegInterchangeFormat and remainder in first strile, etc. It is forgiving
109 and robust, may likely work with OJPEG flavors we've not seen yet, and makes most out
110 of the data.
111
112 Another nice side-effect is that a complete JPEG single valid stream is build if
113 planarconfig is not separate (vast majority). We may one day use that to build
114 converters to JPEG, and/or to new-style JPEG compression inside TIFF.
115
116 A disadvantage is the lack of random access to the individual striles. This is the
117 reason for much of the complicated restart-and-position stuff inside OJPEGPreDecode.
118 Applications would do well accessing all striles in order, as this will result in
119 a single sequential scan of the input stream, and no restarting of LibJpeg decoding
120 session.
121 */
122
123 #define WIN32_LEAN_AND_MEAN
124 #define VC_EXTRALEAN
125
126 #include "tiffiop.h"
127 #ifdef OJPEG_SUPPORT
128
129 /* Configuration defines here are:
130 * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some environments,
131 * like eg LibTiffDelphi, this is not possible. For this reason, the actual calls to
132 * libjpeg, with longjump stuff, are encapsulated in dedicated functions. When
133 * JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declared external
134 * to this unit, and can be defined elsewhere to use stuff other then longjump.
135 * The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encapsulators
136 * here, internally, with normal longjump.
137 * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
138 * conveniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
139 * in place of plain setjmp. These macros will make it easier. It is useless
140 * to fiddle with these if you define JPEG_ENCAP_EXTERNAL.
141 * OJPEG_BUFFER: Define the size of the desired buffer here. Should be small enough so as to guarantee
142 * instant processing, optimal streaming and optimal use of processor cache, but also big
143 * enough so as to not result in significant call overhead. It should be at least a few
144 * bytes to accommodate some structures (this is verified in asserts), but it would not be
145 * sensible to make it this small anyway, and it should be at most 64K since it is indexed
146 * with uint16. We recommend 2K.
147 * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used anywhere and has
148 * absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
149 */
150
151 /* define LIBJPEG_ENCAP_EXTERNAL */
152 #define SETJMP(jbuf) setjmp(jbuf)
153 #define LONGJMP(jbuf,code) longjmp(jbuf,code)
154 #define JMP_BUF jmp_buf
155 #define OJPEG_BUFFER 2048
156 /* define EGYPTIANWALK */
157
158 #define JPEG_MARKER_SOF0 0xC0
159 #define JPEG_MARKER_SOF1 0xC1
160 #define JPEG_MARKER_SOF3 0xC3
161 #define JPEG_MARKER_DHT 0xC4
162 #define JPEG_MARKER_RST0 0XD0
163 #define JPEG_MARKER_SOI 0xD8
164 #define JPEG_MARKER_EOI 0xD9
165 #define JPEG_MARKER_SOS 0xDA
166 #define JPEG_MARKER_DQT 0xDB
167 #define JPEG_MARKER_DRI 0xDD
168 #define JPEG_MARKER_APP0 0xE0
169 #define JPEG_MARKER_COM 0xFE
170
171 #define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
172 #define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
173 #define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
174 #define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
175 #define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
176 #define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
177 #define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
178
179 static const TIFFField ojpegFields[] = {
180 {TIFFTAG_JPEGIFOFFSET,1,1,TIFF_LONG8,0,TIFF_SETGET_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGINTERCHANGEFORMAT,TRUE,FALSE,"JpegInterchangeFormat",NULL},
181 {TIFFTAG_JPEGIFBYTECOUNT,1,1,TIFF_LONG8,0,TIFF_SETGET_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH,TRUE,FALSE,"JpegInterchangeFormatLength",NULL},
182 {TIFFTAG_JPEGQTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGQTABLES,FALSE,TRUE,"JpegQTables",NULL},
183 {TIFFTAG_JPEGDCTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGDCTABLES,FALSE,TRUE,"JpegDcTables",NULL},
184 {TIFFTAG_JPEGACTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGACTABLES,FALSE,TRUE,"JpegAcTables",NULL},
185 {TIFFTAG_JPEGPROC,1,1,TIFF_SHORT,0,TIFF_SETGET_UINT16,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGPROC,FALSE,FALSE,"JpegProc",NULL},
186 {TIFFTAG_JPEGRESTARTINTERVAL,1,1,TIFF_SHORT,0,TIFF_SETGET_UINT16,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGRESTARTINTERVAL,FALSE,FALSE,"JpegRestartInterval",NULL},
187 };
188
189 #ifndef LIBJPEG_ENCAP_EXTERNAL
190 #include <setjmp.h>
191 #endif
192
193 /* We undefine FAR to avoid conflict with JPEG definition */
194
195 #ifdef FAR
196 #undef FAR
197 #endif
198
199 /*
200 Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
201 not defined. Unfortunately, the MinGW and Borland compilers include
202 a typedef for INT32, which causes a conflict. MSVC does not include
203 a conflicting typedef given the headers which are included.
204 */
205 #if defined(__BORLANDC__) || defined(__MINGW32__)
206 # define XMD_H 1
207 #endif
208
209 /* Define "boolean" as unsigned char, not int, per Windows custom. */
210 #if defined(__WIN32__) && !defined(__MINGW32__)
211 # ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
212 typedef unsigned char boolean;
213 # endif
214 # define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
215 #endif
216
217 #if defined(USE_SYSTEM_LIBJPEG)
218 #include <jerror.h>
219 #include <jpeglib.h>
220 #elif defined(USE_LIBJPEG_TURBO)
221 #include "third_party/libjpeg_turbo/jerror.h"
222 #include "third_party/libjpeg_turbo/jpeglib.h"
223 #else
224 #include "third_party/libjpeg/jerror.h"
225 #include "third_party/libjpeg/jpeglib.h"
226 #endif
227
228
229 typedef struct jpeg_error_mgr jpeg_error_mgr;
230 typedef struct jpeg_common_struct jpeg_common_struct;
231 typedef struct jpeg_decompress_struct jpeg_decompress_struct;
232 typedef struct jpeg_source_mgr jpeg_source_mgr;
233
234 typedef enum {
235 osibsNotSetYet,
236 osibsJpegInterchangeFormat,
237 osibsStrile,
238 osibsEof
239 } OJPEGStateInBufferSource;
240
241 typedef enum {
242 ososSoi,
243 ososQTable0,ososQTable1,ososQTable2,ososQTable3,
244 ososDcTable0,ososDcTable1,ososDcTable2,ososDcTable3,
245 ososAcTable0,ososAcTable1,ososAcTable2,ososAcTable3,
246 ososDri,
247 ososSof,
248 ososSos,
249 ososCompressed,
250 ososRst,
251 ososEoi
252 } OJPEGStateOutState;
253
254 typedef struct {
255 TIFF* tif;
256 int decoder_ok;
257 #ifndef LIBJPEG_ENCAP_EXTERNAL
258 JMP_BUF exit_jmpbuf;
259 #endif
260 TIFFVGetMethod vgetparent;
261 TIFFVSetMethod vsetparent;
262 TIFFPrintMethod printdir;
263 uint64 file_size;
264 uint32 image_width;
265 uint32 image_length;
266 uint32 strile_width;
267 uint32 strile_length;
268 uint32 strile_length_total;
269 uint8 samples_per_pixel;
270 uint8 plane_sample_offset;
271 uint8 samples_per_pixel_per_plane;
272 uint64 jpeg_interchange_format;
273 uint64 jpeg_interchange_format_length;
274 uint8 jpeg_proc;
275 uint8 subsamplingcorrect;
276 uint8 subsamplingcorrect_done;
277 uint8 subsampling_tag;
278 uint8 subsampling_hor;
279 uint8 subsampling_ver;
280 uint8 subsampling_force_desubsampling_inside_decompression;
281 uint8 qtable_offset_count;
282 uint8 dctable_offset_count;
283 uint8 actable_offset_count;
284 uint64 qtable_offset[3];
285 uint64 dctable_offset[3];
286 uint64 actable_offset[3];
287 uint8* qtable[4];
288 uint8* dctable[4];
289 uint8* actable[4];
290 uint16 restart_interval;
291 uint8 restart_index;
292 uint8 sof_log;
293 uint8 sof_marker_id;
294 uint32 sof_x;
295 uint32 sof_y;
296 uint8 sof_c[3];
297 uint8 sof_hv[3];
298 uint8 sof_tq[3];
299 uint8 sos_cs[3];
300 uint8 sos_tda[3];
301 struct {
302 uint8 log;
303 OJPEGStateInBufferSource in_buffer_source;
304 uint32 in_buffer_next_strile;
305 uint64 in_buffer_file_pos;
306 uint64 in_buffer_file_togo;
307 } sos_end[3];
308 uint8 readheader_done;
309 uint8 writeheader_done;
310 uint16 write_cursample;
311 uint32 write_curstrile;
312 uint8 libjpeg_session_active;
313 uint8 libjpeg_jpeg_query_style;
314 jpeg_error_mgr libjpeg_jpeg_error_mgr;
315 jpeg_decompress_struct libjpeg_jpeg_decompress_struct;
316 jpeg_source_mgr libjpeg_jpeg_source_mgr;
317 uint8 subsampling_convert_log;
318 uint32 subsampling_convert_ylinelen;
319 uint32 subsampling_convert_ylines;
320 uint32 subsampling_convert_clinelen;
321 uint32 subsampling_convert_clines;
322 uint32 subsampling_convert_ybuflen;
323 uint32 subsampling_convert_cbuflen;
324 uint32 subsampling_convert_ycbcrbuflen;
325 uint8* subsampling_convert_ycbcrbuf;
326 uint8* subsampling_convert_ybuf;
327 uint8* subsampling_convert_cbbuf;
328 uint8* subsampling_convert_crbuf;
329 uint32 subsampling_convert_ycbcrimagelen;
330 uint8** subsampling_convert_ycbcrimage;
331 uint32 subsampling_convert_clinelenout;
332 uint32 subsampling_convert_state;
333 uint32 bytes_per_line; /* if the codec outputs subsampled data, a 'line' in bytes_per_line */
334 uint32 lines_per_strile; /* and lines_per_strile means subsampling_ver desubsampled rows */
335 OJPEGStateInBufferSource in_buffer_source;
336 uint32 in_buffer_next_strile;
337 uint32 in_buffer_strile_count;
338 uint64 in_buffer_file_pos;
339 uint8 in_buffer_file_pos_log;
340 uint64 in_buffer_file_togo;
341 uint16 in_buffer_togo;
342 uint8* in_buffer_cur;
343 uint8 in_buffer[OJPEG_BUFFER];
344 OJPEGStateOutState out_state;
345 uint8 out_buffer[OJPEG_BUFFER];
346 uint8* skip_buffer;
347 } OJPEGState;
348
349 static int OJPEGVGetField(TIFF* tif, uint32 tag, va_list ap);
350 static int OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap);
351 static void OJPEGPrintDir(TIFF* tif, FILE* fd, long flags);
352
353 static int OJPEGFixupTags(TIFF* tif);
354 static int OJPEGSetupDecode(TIFF* tif);
355 static int OJPEGPreDecode(TIFF* tif, uint16 s);
356 static int OJPEGPreDecodeSkipRaw(TIFF* tif);
357 static int OJPEGPreDecodeSkipScanlines(TIFF* tif);
358 static int OJPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
359 static int OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc);
360 static int OJPEGDecodeScanlines(TIFF* tif, uint8* buf, tmsize_t cc);
361 static void OJPEGPostDecode(TIFF* tif, uint8* buf, tmsize_t cc);
362 static int OJPEGSetupEncode(TIFF* tif);
363 static int OJPEGPreEncode(TIFF* tif, uint16 s);
364 static int OJPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
365 static int OJPEGPostEncode(TIFF* tif);
366 static void OJPEGCleanup(TIFF* tif);
367
368 static void OJPEGSubsamplingCorrect(TIFF* tif);
369 static int OJPEGReadHeaderInfo(TIFF* tif);
370 static int OJPEGReadSecondarySos(TIFF* tif, uint16 s);
371 static int OJPEGWriteHeaderInfo(TIFF* tif);
372 static void OJPEGLibjpegSessionAbort(TIFF* tif);
373
374 static int OJPEGReadHeaderInfoSec(TIFF* tif);
375 static int OJPEGReadHeaderInfoSecStreamDri(TIFF* tif);
376 static int OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif);
377 static int OJPEGReadHeaderInfoSecStreamDht(TIFF* tif);
378 static int OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id);
379 static int OJPEGReadHeaderInfoSecStreamSos(TIFF* tif);
380 static int OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif);
381 static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif);
382 static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif);
383
384 static int OJPEGReadBufferFill(OJPEGState* sp);
385 static int OJPEGReadByte(OJPEGState* sp, uint8* byte);
386 static int OJPEGReadBytePeek(OJPEGState* sp, uint8* byte);
387 static void OJPEGReadByteAdvance(OJPEGState* sp);
388 static int OJPEGReadWord(OJPEGState* sp, uint16* word);
389 static int OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem);
390 static void OJPEGReadSkip(OJPEGState* sp, uint16 len);
391
392 static int OJPEGWriteStream(TIFF* tif, void** mem, uint32* len);
393 static void OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len);
394 static void OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
395 static void OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
396 static void OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
397 static void OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len);
398 static void OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len);
399 static void OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len);
400 static int OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len);
401 static void OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len);
402 static void OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len);
403
404 #ifdef LIBJPEG_ENCAP_EXTERNAL
405 extern int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
406 extern int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
407 extern int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
408 extern int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
409 extern int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
410 extern void jpeg_encap_unwind(TIFF* tif);
411 #else
412 static int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* j);
413 static int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
414 static int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
415 static int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
416 static int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
417 static void jpeg_encap_unwind(TIFF* tif);
418 #endif
419
420 static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo);
421 static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo);
422 static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo);
423 static boolean OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo);
424 static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes);
425 static boolean OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired);
426 static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo);
427
428 int
TIFFInitOJPEG(TIFF * tif,int scheme)429 TIFFInitOJPEG(TIFF* tif, int scheme)
430 {
431 static const char module[]="TIFFInitOJPEG";
432 OJPEGState* sp;
433
434 assert(scheme==COMPRESSION_OJPEG);
435
436 /*
437 * Merge codec-specific tag information.
438 */
439 if (!_TIFFMergeFields(tif, ojpegFields, TIFFArrayCount(ojpegFields))) {
440 TIFFErrorExt(tif->tif_clientdata, module,
441 "Merging Old JPEG codec-specific tags failed");
442 return 0;
443 }
444
445 /* state block */
446 sp=_TIFFmalloc(sizeof(OJPEGState));
447 if (sp==NULL)
448 {
449 TIFFErrorExt(tif->tif_clientdata,module,"No space for OJPEG state block");
450 return(0);
451 }
452 _TIFFmemset(sp,0,sizeof(OJPEGState));
453 sp->tif=tif;
454 sp->jpeg_proc=1;
455 sp->subsampling_hor=2;
456 sp->subsampling_ver=2;
457 TIFFSetField(tif,TIFFTAG_YCBCRSUBSAMPLING,2,2);
458 /* tif codec methods */
459 tif->tif_fixuptags=OJPEGFixupTags;
460 tif->tif_setupdecode=OJPEGSetupDecode;
461 tif->tif_predecode=OJPEGPreDecode;
462 tif->tif_postdecode=OJPEGPostDecode;
463 tif->tif_decoderow=OJPEGDecode;
464 tif->tif_decodestrip=OJPEGDecode;
465 tif->tif_decodetile=OJPEGDecode;
466 tif->tif_setupencode=OJPEGSetupEncode;
467 tif->tif_preencode=OJPEGPreEncode;
468 tif->tif_postencode=OJPEGPostEncode;
469 tif->tif_encoderow=OJPEGEncode;
470 tif->tif_encodestrip=OJPEGEncode;
471 tif->tif_encodetile=OJPEGEncode;
472 tif->tif_cleanup=OJPEGCleanup;
473 tif->tif_data=(uint8*)sp;
474 /* tif tag methods */
475 sp->vgetparent=tif->tif_tagmethods.vgetfield;
476 tif->tif_tagmethods.vgetfield=OJPEGVGetField;
477 sp->vsetparent=tif->tif_tagmethods.vsetfield;
478 tif->tif_tagmethods.vsetfield=OJPEGVSetField;
479 sp->printdir=tif->tif_tagmethods.printdir;
480 tif->tif_tagmethods.printdir=OJPEGPrintDir;
481 /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
482 Some others do, but have totally meaningless or corrupt values
483 in these tags. In these cases, the JpegInterchangeFormat stream is
484 reliable. In any case, this decoder reads the compressed data itself,
485 from the most reliable locations, and we need to notify encapsulating
486 LibTiff not to read raw strips or tiles for us. */
487 tif->tif_flags|=TIFF_NOREADRAW;
488 return(1);
489 }
490
491 static int
OJPEGVGetField(TIFF * tif,uint32 tag,va_list ap)492 OJPEGVGetField(TIFF* tif, uint32 tag, va_list ap)
493 {
494 OJPEGState* sp=(OJPEGState*)tif->tif_data;
495 switch(tag)
496 {
497 case TIFFTAG_JPEGIFOFFSET:
498 *va_arg(ap,uint64*)=(uint64)sp->jpeg_interchange_format;
499 break;
500 case TIFFTAG_JPEGIFBYTECOUNT:
501 *va_arg(ap,uint64*)=(uint64)sp->jpeg_interchange_format_length;
502 break;
503 case TIFFTAG_YCBCRSUBSAMPLING:
504 if (sp->subsamplingcorrect_done==0)
505 OJPEGSubsamplingCorrect(tif);
506 *va_arg(ap,uint16*)=(uint16)sp->subsampling_hor;
507 *va_arg(ap,uint16*)=(uint16)sp->subsampling_ver;
508 break;
509 case TIFFTAG_JPEGQTABLES:
510 *va_arg(ap,uint32*)=(uint32)sp->qtable_offset_count;
511 *va_arg(ap,void**)=(void*)sp->qtable_offset;
512 break;
513 case TIFFTAG_JPEGDCTABLES:
514 *va_arg(ap,uint32*)=(uint32)sp->dctable_offset_count;
515 *va_arg(ap,void**)=(void*)sp->dctable_offset;
516 break;
517 case TIFFTAG_JPEGACTABLES:
518 *va_arg(ap,uint32*)=(uint32)sp->actable_offset_count;
519 *va_arg(ap,void**)=(void*)sp->actable_offset;
520 break;
521 case TIFFTAG_JPEGPROC:
522 *va_arg(ap,uint16*)=(uint16)sp->jpeg_proc;
523 break;
524 case TIFFTAG_JPEGRESTARTINTERVAL:
525 *va_arg(ap,uint16*)=sp->restart_interval;
526 break;
527 default:
528 return (*sp->vgetparent)(tif,tag,ap);
529 }
530 return (1);
531 }
532
533 static int
OJPEGVSetField(TIFF * tif,uint32 tag,va_list ap)534 OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap)
535 {
536 static const char module[]="OJPEGVSetField";
537 OJPEGState* sp=(OJPEGState*)tif->tif_data;
538 uint32 ma;
539 uint64* mb;
540 uint32 n;
541 const TIFFField* fip;
542
543 switch(tag)
544 {
545 case TIFFTAG_JPEGIFOFFSET:
546 sp->jpeg_interchange_format=(uint64)va_arg(ap,uint64);
547 break;
548 case TIFFTAG_JPEGIFBYTECOUNT:
549 sp->jpeg_interchange_format_length=(uint64)va_arg(ap,uint64);
550 break;
551 case TIFFTAG_YCBCRSUBSAMPLING:
552 sp->subsampling_tag=1;
553 sp->subsampling_hor=(uint8)va_arg(ap,uint16_vap);
554 sp->subsampling_ver=(uint8)va_arg(ap,uint16_vap);
555 tif->tif_dir.td_ycbcrsubsampling[0]=sp->subsampling_hor;
556 tif->tif_dir.td_ycbcrsubsampling[1]=sp->subsampling_ver;
557 break;
558 case TIFFTAG_JPEGQTABLES:
559 ma=(uint32)va_arg(ap,uint32);
560 if (ma!=0)
561 {
562 if (ma>3)
563 {
564 TIFFErrorExt(tif->tif_clientdata,module,"JpegQTables tag has incorrect count");
565 return(0);
566 }
567 sp->qtable_offset_count=(uint8)ma;
568 mb=(uint64*)va_arg(ap,uint64*);
569 for (n=0; n<ma; n++)
570 sp->qtable_offset[n]=mb[n];
571 }
572 break;
573 case TIFFTAG_JPEGDCTABLES:
574 ma=(uint32)va_arg(ap,uint32);
575 if (ma!=0)
576 {
577 if (ma>3)
578 {
579 TIFFErrorExt(tif->tif_clientdata,module,"JpegDcTables tag has incorrect count");
580 return(0);
581 }
582 sp->dctable_offset_count=(uint8)ma;
583 mb=(uint64*)va_arg(ap,uint64*);
584 for (n=0; n<ma; n++)
585 sp->dctable_offset[n]=mb[n];
586 }
587 break;
588 case TIFFTAG_JPEGACTABLES:
589 ma=(uint32)va_arg(ap,uint32);
590 if (ma!=0)
591 {
592 if (ma>3)
593 {
594 TIFFErrorExt(tif->tif_clientdata,module,"JpegAcTables tag has incorrect count");
595 return(0);
596 }
597 sp->actable_offset_count=(uint8)ma;
598 mb=(uint64*)va_arg(ap,uint64*);
599 for (n=0; n<ma; n++)
600 sp->actable_offset[n]=mb[n];
601 }
602 break;
603 case TIFFTAG_JPEGPROC:
604 sp->jpeg_proc=(uint8)va_arg(ap,uint16_vap);
605 break;
606 case TIFFTAG_JPEGRESTARTINTERVAL:
607 sp->restart_interval=(uint16)va_arg(ap,uint16_vap);
608 break;
609 default:
610 return (*sp->vsetparent)(tif,tag,ap);
611 }
612 fip = TIFFFieldWithTag(tif,tag);
613 if( fip == NULL ) /* shouldn't happen */
614 return(0);
615 TIFFSetFieldBit(tif,fip->field_bit);
616 tif->tif_flags|=TIFF_DIRTYDIRECT;
617 return(1);
618 }
619
620 static void
OJPEGPrintDir(TIFF * tif,FILE * fd,long flags)621 OJPEGPrintDir(TIFF* tif, FILE* fd, long flags)
622 {
623 OJPEGState* sp=(OJPEGState*)tif->tif_data;
624 uint8 m;
625 (void)flags;
626 assert(sp!=NULL);
627 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMAT))
628 fprintf(fd," JpegInterchangeFormat: " TIFF_UINT64_FORMAT "\n",(TIFF_UINT64_T)sp->jpeg_interchange_format);
629 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH))
630 fprintf(fd," JpegInterchangeFormatLength: " TIFF_UINT64_FORMAT "\n",(TIFF_UINT64_T)sp->jpeg_interchange_format_length);
631 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGQTABLES))
632 {
633 fprintf(fd," JpegQTables:");
634 for (m=0; m<sp->qtable_offset_count; m++)
635 fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->qtable_offset[m]);
636 fprintf(fd,"\n");
637 }
638 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGDCTABLES))
639 {
640 fprintf(fd," JpegDcTables:");
641 for (m=0; m<sp->dctable_offset_count; m++)
642 fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->dctable_offset[m]);
643 fprintf(fd,"\n");
644 }
645 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGACTABLES))
646 {
647 fprintf(fd," JpegAcTables:");
648 for (m=0; m<sp->actable_offset_count; m++)
649 fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->actable_offset[m]);
650 fprintf(fd,"\n");
651 }
652 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGPROC))
653 fprintf(fd," JpegProc: %u\n",(unsigned int)sp->jpeg_proc);
654 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGRESTARTINTERVAL))
655 fprintf(fd," JpegRestartInterval: %u\n",(unsigned int)sp->restart_interval);
656 if (sp->printdir)
657 (*sp->printdir)(tif, fd, flags);
658 }
659
660 static int
OJPEGFixupTags(TIFF * tif)661 OJPEGFixupTags(TIFF* tif)
662 {
663 (void) tif;
664 return(1);
665 }
666
667 static int
OJPEGSetupDecode(TIFF * tif)668 OJPEGSetupDecode(TIFF* tif)
669 {
670 static const char module[]="OJPEGSetupDecode";
671 TIFFWarningExt(tif->tif_clientdata,module,"Depreciated and troublesome old-style JPEG compression mode, please convert to new-style JPEG compression and notify vendor of writing software");
672 return(1);
673 }
674
675 static int
OJPEGPreDecode(TIFF * tif,uint16 s)676 OJPEGPreDecode(TIFF* tif, uint16 s)
677 {
678 OJPEGState* sp=(OJPEGState*)tif->tif_data;
679 uint32 m;
680 if (sp->subsamplingcorrect_done==0)
681 OJPEGSubsamplingCorrect(tif);
682 if (sp->readheader_done==0)
683 {
684 if (OJPEGReadHeaderInfo(tif)==0)
685 return(0);
686 }
687 if (sp->sos_end[s].log==0)
688 {
689 if (OJPEGReadSecondarySos(tif,s)==0)
690 return(0);
691 }
692 if isTiled(tif)
693 m=tif->tif_curtile;
694 else
695 m=tif->tif_curstrip;
696 if ((sp->writeheader_done!=0) && ((sp->write_cursample!=s) || (sp->write_curstrile>m)))
697 {
698 if (sp->libjpeg_session_active!=0)
699 OJPEGLibjpegSessionAbort(tif);
700 sp->writeheader_done=0;
701 }
702 if (sp->writeheader_done==0)
703 {
704 sp->plane_sample_offset=(uint8)s;
705 sp->write_cursample=s;
706 sp->write_curstrile=s*tif->tif_dir.td_stripsperimage;
707 if ((sp->in_buffer_file_pos_log==0) ||
708 (sp->in_buffer_file_pos-sp->in_buffer_togo!=sp->sos_end[s].in_buffer_file_pos))
709 {
710 sp->in_buffer_source=sp->sos_end[s].in_buffer_source;
711 sp->in_buffer_next_strile=sp->sos_end[s].in_buffer_next_strile;
712 sp->in_buffer_file_pos=sp->sos_end[s].in_buffer_file_pos;
713 sp->in_buffer_file_pos_log=0;
714 sp->in_buffer_file_togo=sp->sos_end[s].in_buffer_file_togo;
715 sp->in_buffer_togo=0;
716 sp->in_buffer_cur=0;
717 }
718 if (OJPEGWriteHeaderInfo(tif)==0)
719 return(0);
720 }
721 while (sp->write_curstrile<m)
722 {
723 if (sp->libjpeg_jpeg_query_style==0)
724 {
725 if (OJPEGPreDecodeSkipRaw(tif)==0)
726 return(0);
727 }
728 else
729 {
730 if (OJPEGPreDecodeSkipScanlines(tif)==0)
731 return(0);
732 }
733 sp->write_curstrile++;
734 }
735 sp->decoder_ok = 1;
736 return(1);
737 }
738
739 static int
OJPEGPreDecodeSkipRaw(TIFF * tif)740 OJPEGPreDecodeSkipRaw(TIFF* tif)
741 {
742 OJPEGState* sp=(OJPEGState*)tif->tif_data;
743 uint32 m;
744 m=sp->lines_per_strile;
745 if (sp->subsampling_convert_state!=0)
746 {
747 if (sp->subsampling_convert_clines-sp->subsampling_convert_state>=m)
748 {
749 sp->subsampling_convert_state+=m;
750 if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
751 sp->subsampling_convert_state=0;
752 return(1);
753 }
754 m-=sp->subsampling_convert_clines-sp->subsampling_convert_state;
755 sp->subsampling_convert_state=0;
756 }
757 while (m>=sp->subsampling_convert_clines)
758 {
759 if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
760 return(0);
761 m-=sp->subsampling_convert_clines;
762 }
763 if (m>0)
764 {
765 if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
766 return(0);
767 sp->subsampling_convert_state=m;
768 }
769 return(1);
770 }
771
772 static int
OJPEGPreDecodeSkipScanlines(TIFF * tif)773 OJPEGPreDecodeSkipScanlines(TIFF* tif)
774 {
775 static const char module[]="OJPEGPreDecodeSkipScanlines";
776 OJPEGState* sp=(OJPEGState*)tif->tif_data;
777 uint32 m;
778 if (sp->skip_buffer==NULL)
779 {
780 sp->skip_buffer=_TIFFmalloc(sp->bytes_per_line);
781 if (sp->skip_buffer==NULL)
782 {
783 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
784 return(0);
785 }
786 }
787 for (m=0; m<sp->lines_per_strile; m++)
788 {
789 if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&sp->skip_buffer,1)==0)
790 return(0);
791 }
792 return(1);
793 }
794
795 static int
OJPEGDecode(TIFF * tif,uint8 * buf,tmsize_t cc,uint16 s)796 OJPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
797 {
798 static const char module[]="OJPEGDecode";
799 OJPEGState* sp=(OJPEGState*)tif->tif_data;
800 (void)s;
801 if( !sp->decoder_ok )
802 {
803 TIFFErrorExt(tif->tif_clientdata,module,"Cannot decode: decoder not correctly initialized");
804 return 0;
805 }
806 if (sp->libjpeg_jpeg_query_style==0)
807 {
808 if (OJPEGDecodeRaw(tif,buf,cc)==0)
809 return(0);
810 }
811 else
812 {
813 if (OJPEGDecodeScanlines(tif,buf,cc)==0)
814 return(0);
815 }
816 return(1);
817 }
818
819 static int
OJPEGDecodeRaw(TIFF * tif,uint8 * buf,tmsize_t cc)820 OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc)
821 {
822 static const char module[]="OJPEGDecodeRaw";
823 OJPEGState* sp=(OJPEGState*)tif->tif_data;
824 uint8* m;
825 tmsize_t n;
826 uint8* oy;
827 uint8* ocb;
828 uint8* ocr;
829 uint8* p;
830 uint32 q;
831 uint8* r;
832 uint8 sx,sy;
833 if (cc%sp->bytes_per_line!=0)
834 {
835 TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
836 return(0);
837 }
838 assert(cc>0);
839 m=buf;
840 n=cc;
841 do
842 {
843 if (sp->subsampling_convert_state==0)
844 {
845 if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
846 return(0);
847 }
848 oy=sp->subsampling_convert_ybuf+sp->subsampling_convert_state*sp->subsampling_ver*sp->subsampling_convert_ylinelen;
849 ocb=sp->subsampling_convert_cbbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
850 ocr=sp->subsampling_convert_crbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
851 p=m;
852 for (q=0; q<sp->subsampling_convert_clinelenout; q++)
853 {
854 r=oy;
855 for (sy=0; sy<sp->subsampling_ver; sy++)
856 {
857 for (sx=0; sx<sp->subsampling_hor; sx++)
858 *p++=*r++;
859 r+=sp->subsampling_convert_ylinelen-sp->subsampling_hor;
860 }
861 oy+=sp->subsampling_hor;
862 *p++=*ocb++;
863 *p++=*ocr++;
864 }
865 sp->subsampling_convert_state++;
866 if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
867 sp->subsampling_convert_state=0;
868 m+=sp->bytes_per_line;
869 n-=sp->bytes_per_line;
870 } while(n>0);
871 return(1);
872 }
873
874 static int
OJPEGDecodeScanlines(TIFF * tif,uint8 * buf,tmsize_t cc)875 OJPEGDecodeScanlines(TIFF* tif, uint8* buf, tmsize_t cc)
876 {
877 static const char module[]="OJPEGDecodeScanlines";
878 OJPEGState* sp=(OJPEGState*)tif->tif_data;
879 uint8* m;
880 tmsize_t n;
881 if (cc%sp->bytes_per_line!=0)
882 {
883 TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
884 return(0);
885 }
886 assert(cc>0);
887 m=buf;
888 n=cc;
889 do
890 {
891 if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&m,1)==0)
892 return(0);
893 m+=sp->bytes_per_line;
894 n-=sp->bytes_per_line;
895 } while(n>0);
896 return(1);
897 }
898
899 static void
OJPEGPostDecode(TIFF * tif,uint8 * buf,tmsize_t cc)900 OJPEGPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
901 {
902 OJPEGState* sp=(OJPEGState*)tif->tif_data;
903 (void)buf;
904 (void)cc;
905 sp->write_curstrile++;
906 if (sp->write_curstrile%tif->tif_dir.td_stripsperimage==0)
907 {
908 assert(sp->libjpeg_session_active!=0);
909 OJPEGLibjpegSessionAbort(tif);
910 sp->writeheader_done=0;
911 }
912 }
913
914 static int
OJPEGSetupEncode(TIFF * tif)915 OJPEGSetupEncode(TIFF* tif)
916 {
917 static const char module[]="OJPEGSetupEncode";
918 TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
919 return(0);
920 }
921
922 static int
OJPEGPreEncode(TIFF * tif,uint16 s)923 OJPEGPreEncode(TIFF* tif, uint16 s)
924 {
925 static const char module[]="OJPEGPreEncode";
926 (void)s;
927 TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
928 return(0);
929 }
930
931 static int
OJPEGEncode(TIFF * tif,uint8 * buf,tmsize_t cc,uint16 s)932 OJPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
933 {
934 static const char module[]="OJPEGEncode";
935 (void)buf;
936 (void)cc;
937 (void)s;
938 TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
939 return(0);
940 }
941
942 static int
OJPEGPostEncode(TIFF * tif)943 OJPEGPostEncode(TIFF* tif)
944 {
945 static const char module[]="OJPEGPostEncode";
946 TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
947 return(0);
948 }
949
950 static void
OJPEGCleanup(TIFF * tif)951 OJPEGCleanup(TIFF* tif)
952 {
953 OJPEGState* sp=(OJPEGState*)tif->tif_data;
954 if (sp!=0)
955 {
956 tif->tif_tagmethods.vgetfield=sp->vgetparent;
957 tif->tif_tagmethods.vsetfield=sp->vsetparent;
958 tif->tif_tagmethods.printdir=sp->printdir;
959 if (sp->qtable[0]!=0)
960 _TIFFfree(sp->qtable[0]);
961 if (sp->qtable[1]!=0)
962 _TIFFfree(sp->qtable[1]);
963 if (sp->qtable[2]!=0)
964 _TIFFfree(sp->qtable[2]);
965 if (sp->qtable[3]!=0)
966 _TIFFfree(sp->qtable[3]);
967 if (sp->dctable[0]!=0)
968 _TIFFfree(sp->dctable[0]);
969 if (sp->dctable[1]!=0)
970 _TIFFfree(sp->dctable[1]);
971 if (sp->dctable[2]!=0)
972 _TIFFfree(sp->dctable[2]);
973 if (sp->dctable[3]!=0)
974 _TIFFfree(sp->dctable[3]);
975 if (sp->actable[0]!=0)
976 _TIFFfree(sp->actable[0]);
977 if (sp->actable[1]!=0)
978 _TIFFfree(sp->actable[1]);
979 if (sp->actable[2]!=0)
980 _TIFFfree(sp->actable[2]);
981 if (sp->actable[3]!=0)
982 _TIFFfree(sp->actable[3]);
983 if (sp->libjpeg_session_active!=0)
984 OJPEGLibjpegSessionAbort(tif);
985 if (sp->subsampling_convert_ycbcrbuf!=0)
986 _TIFFfree(sp->subsampling_convert_ycbcrbuf);
987 if (sp->subsampling_convert_ycbcrimage!=0)
988 _TIFFfree(sp->subsampling_convert_ycbcrimage);
989 if (sp->skip_buffer!=0)
990 _TIFFfree(sp->skip_buffer);
991 _TIFFfree(sp);
992 tif->tif_data=NULL;
993 _TIFFSetDefaultCompressionState(tif);
994 }
995 }
996
997 static void
OJPEGSubsamplingCorrect(TIFF * tif)998 OJPEGSubsamplingCorrect(TIFF* tif)
999 {
1000 static const char module[]="OJPEGSubsamplingCorrect";
1001 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1002 uint8 mh;
1003 uint8 mv;
1004 _TIFFFillStriles( tif );
1005
1006 assert(sp->subsamplingcorrect_done==0);
1007 if ((tif->tif_dir.td_samplesperpixel!=3) || ((tif->tif_dir.td_photometric!=PHOTOMETRIC_YCBCR) &&
1008 (tif->tif_dir.td_photometric!=PHOTOMETRIC_ITULAB)))
1009 {
1010 if (sp->subsampling_tag!=0)
1011 TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag not appropriate for this Photometric and/or SamplesPerPixel");
1012 sp->subsampling_hor=1;
1013 sp->subsampling_ver=1;
1014 sp->subsampling_force_desubsampling_inside_decompression=0;
1015 }
1016 else
1017 {
1018 sp->subsamplingcorrect_done=1;
1019 mh=sp->subsampling_hor;
1020 mv=sp->subsampling_ver;
1021 sp->subsamplingcorrect=1;
1022 OJPEGReadHeaderInfoSec(tif);
1023 if (sp->subsampling_force_desubsampling_inside_decompression!=0)
1024 {
1025 sp->subsampling_hor=1;
1026 sp->subsampling_ver=1;
1027 }
1028 sp->subsamplingcorrect=0;
1029 if (((sp->subsampling_hor!=mh) || (sp->subsampling_ver!=mv)) && (sp->subsampling_force_desubsampling_inside_decompression==0))
1030 {
1031 if (sp->subsampling_tag==0)
1032 TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data [%d,%d] does not match default values [2,2]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver);
1033 else
1034 TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data [%d,%d] does not match subsampling tag values [%d,%d]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver,mh,mv);
1035 }
1036 if (sp->subsampling_force_desubsampling_inside_decompression!=0)
1037 {
1038 if (sp->subsampling_tag==0)
1039 TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data does not match default values [2,2] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression");
1040 else
1041 TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data does not match subsampling tag values [%d,%d] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression",mh,mv);
1042 }
1043 if (sp->subsampling_force_desubsampling_inside_decompression==0)
1044 {
1045 if (sp->subsampling_hor<sp->subsampling_ver)
1046 TIFFWarningExt(tif->tif_clientdata,module,"Subsampling values [%d,%d] are not allowed in TIFF",sp->subsampling_hor,sp->subsampling_ver);
1047 }
1048 }
1049 sp->subsamplingcorrect_done=1;
1050 }
1051
1052 static int
OJPEGReadHeaderInfo(TIFF * tif)1053 OJPEGReadHeaderInfo(TIFF* tif)
1054 {
1055 static const char module[]="OJPEGReadHeaderInfo";
1056 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1057 assert(sp->readheader_done==0);
1058 sp->image_width=tif->tif_dir.td_imagewidth;
1059 sp->image_length=tif->tif_dir.td_imagelength;
1060 if isTiled(tif)
1061 {
1062 sp->strile_width=tif->tif_dir.td_tilewidth;
1063 sp->strile_length=tif->tif_dir.td_tilelength;
1064 sp->strile_length_total=((sp->image_length+sp->strile_length-1)/sp->strile_length)*sp->strile_length;
1065 }
1066 else
1067 {
1068 sp->strile_width=sp->image_width;
1069 sp->strile_length=tif->tif_dir.td_rowsperstrip;
1070 sp->strile_length_total=sp->image_length;
1071 }
1072 if (tif->tif_dir.td_samplesperpixel==1)
1073 {
1074 sp->samples_per_pixel=1;
1075 sp->plane_sample_offset=0;
1076 sp->samples_per_pixel_per_plane=sp->samples_per_pixel;
1077 sp->subsampling_hor=1;
1078 sp->subsampling_ver=1;
1079 }
1080 else
1081 {
1082 if (tif->tif_dir.td_samplesperpixel!=3)
1083 {
1084 TIFFErrorExt(tif->tif_clientdata,module,"SamplesPerPixel %d not supported for this compression scheme",sp->samples_per_pixel);
1085 return(0);
1086 }
1087 sp->samples_per_pixel=3;
1088 sp->plane_sample_offset=0;
1089 if (tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)
1090 sp->samples_per_pixel_per_plane=3;
1091 else
1092 sp->samples_per_pixel_per_plane=1;
1093 }
1094 if (sp->strile_length<sp->image_length)
1095 {
1096 if (sp->strile_length%(sp->subsampling_ver*8)!=0)
1097 {
1098 TIFFErrorExt(tif->tif_clientdata,module,"Incompatible vertical subsampling and image strip/tile length");
1099 return(0);
1100 }
1101 sp->restart_interval=(uint16)(((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8))*(sp->strile_length/(sp->subsampling_ver*8)));
1102 }
1103 if (OJPEGReadHeaderInfoSec(tif)==0)
1104 return(0);
1105 sp->sos_end[0].log=1;
1106 sp->sos_end[0].in_buffer_source=sp->in_buffer_source;
1107 sp->sos_end[0].in_buffer_next_strile=sp->in_buffer_next_strile;
1108 sp->sos_end[0].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
1109 sp->sos_end[0].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
1110 sp->readheader_done=1;
1111 return(1);
1112 }
1113
1114 static int
OJPEGReadSecondarySos(TIFF * tif,uint16 s)1115 OJPEGReadSecondarySos(TIFF* tif, uint16 s)
1116 {
1117 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1118 uint8 m;
1119 assert(s>0);
1120 assert(s<3);
1121 assert(sp->sos_end[0].log!=0);
1122 assert(sp->sos_end[s].log==0);
1123 sp->plane_sample_offset=(uint8)(s-1);
1124 while(sp->sos_end[sp->plane_sample_offset].log==0)
1125 sp->plane_sample_offset--;
1126 sp->in_buffer_source=sp->sos_end[sp->plane_sample_offset].in_buffer_source;
1127 sp->in_buffer_next_strile=sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile;
1128 sp->in_buffer_file_pos=sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos;
1129 sp->in_buffer_file_pos_log=0;
1130 sp->in_buffer_file_togo=sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo;
1131 sp->in_buffer_togo=0;
1132 sp->in_buffer_cur=0;
1133 while(sp->plane_sample_offset<s)
1134 {
1135 do
1136 {
1137 if (OJPEGReadByte(sp,&m)==0)
1138 return(0);
1139 if (m==255)
1140 {
1141 do
1142 {
1143 if (OJPEGReadByte(sp,&m)==0)
1144 return(0);
1145 if (m!=255)
1146 break;
1147 } while(1);
1148 if (m==JPEG_MARKER_SOS)
1149 break;
1150 }
1151 } while(1);
1152 sp->plane_sample_offset++;
1153 if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
1154 return(0);
1155 sp->sos_end[sp->plane_sample_offset].log=1;
1156 sp->sos_end[sp->plane_sample_offset].in_buffer_source=sp->in_buffer_source;
1157 sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile=sp->in_buffer_next_strile;
1158 sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
1159 sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
1160 }
1161 return(1);
1162 }
1163
1164 static int
OJPEGWriteHeaderInfo(TIFF * tif)1165 OJPEGWriteHeaderInfo(TIFF* tif)
1166 {
1167 static const char module[]="OJPEGWriteHeaderInfo";
1168 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1169 uint8** m;
1170 uint32 n;
1171 /* if a previous attempt failed, don't try again */
1172 if (sp->libjpeg_session_active != 0)
1173 return 0;
1174 sp->out_state=ososSoi;
1175 sp->restart_index=0;
1176 jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr));
1177 sp->libjpeg_jpeg_error_mgr.output_message=OJPEGLibjpegJpegErrorMgrOutputMessage;
1178 sp->libjpeg_jpeg_error_mgr.error_exit=OJPEGLibjpegJpegErrorMgrErrorExit;
1179 sp->libjpeg_jpeg_decompress_struct.err=&(sp->libjpeg_jpeg_error_mgr);
1180 sp->libjpeg_jpeg_decompress_struct.client_data=(void*)tif;
1181 if (jpeg_create_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
1182 return(0);
1183 sp->libjpeg_session_active=1;
1184 sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=0;
1185 sp->libjpeg_jpeg_source_mgr.init_source=OJPEGLibjpegJpegSourceMgrInitSource;
1186 sp->libjpeg_jpeg_source_mgr.fill_input_buffer=OJPEGLibjpegJpegSourceMgrFillInputBuffer;
1187 sp->libjpeg_jpeg_source_mgr.skip_input_data=OJPEGLibjpegJpegSourceMgrSkipInputData;
1188 sp->libjpeg_jpeg_source_mgr.resync_to_restart=OJPEGLibjpegJpegSourceMgrResyncToRestart;
1189 sp->libjpeg_jpeg_source_mgr.term_source=OJPEGLibjpegJpegSourceMgrTermSource;
1190 sp->libjpeg_jpeg_decompress_struct.src=&(sp->libjpeg_jpeg_source_mgr);
1191 if (jpeg_read_header_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),1)==0)
1192 return(0);
1193 if ((sp->subsampling_force_desubsampling_inside_decompression==0) && (sp->samples_per_pixel_per_plane>1))
1194 {
1195 sp->libjpeg_jpeg_decompress_struct.raw_data_out=1;
1196 #if JPEG_LIB_VERSION >= 70
1197 sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling=FALSE;
1198 #endif
1199 sp->libjpeg_jpeg_query_style=0;
1200 if (sp->subsampling_convert_log==0)
1201 {
1202 assert(sp->subsampling_convert_ycbcrbuf==0);
1203 assert(sp->subsampling_convert_ycbcrimage==0);
1204 sp->subsampling_convert_ylinelen=((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8)*sp->subsampling_hor*8);
1205 sp->subsampling_convert_ylines=sp->subsampling_ver*8;
1206 sp->subsampling_convert_clinelen=sp->subsampling_convert_ylinelen/sp->subsampling_hor;
1207 sp->subsampling_convert_clines=8;
1208 sp->subsampling_convert_ybuflen=sp->subsampling_convert_ylinelen*sp->subsampling_convert_ylines;
1209 sp->subsampling_convert_cbuflen=sp->subsampling_convert_clinelen*sp->subsampling_convert_clines;
1210 sp->subsampling_convert_ycbcrbuflen=sp->subsampling_convert_ybuflen+2*sp->subsampling_convert_cbuflen;
1211 sp->subsampling_convert_ycbcrbuf=_TIFFmalloc(sp->subsampling_convert_ycbcrbuflen);
1212 if (sp->subsampling_convert_ycbcrbuf==0)
1213 {
1214 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1215 return(0);
1216 }
1217 sp->subsampling_convert_ybuf=sp->subsampling_convert_ycbcrbuf;
1218 sp->subsampling_convert_cbbuf=sp->subsampling_convert_ybuf+sp->subsampling_convert_ybuflen;
1219 sp->subsampling_convert_crbuf=sp->subsampling_convert_cbbuf+sp->subsampling_convert_cbuflen;
1220 sp->subsampling_convert_ycbcrimagelen=3+sp->subsampling_convert_ylines+2*sp->subsampling_convert_clines;
1221 sp->subsampling_convert_ycbcrimage=_TIFFmalloc(sp->subsampling_convert_ycbcrimagelen*sizeof(uint8*));
1222 if (sp->subsampling_convert_ycbcrimage==0)
1223 {
1224 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1225 return(0);
1226 }
1227 m=sp->subsampling_convert_ycbcrimage;
1228 *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3);
1229 *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines);
1230 *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines+sp->subsampling_convert_clines);
1231 for (n=0; n<sp->subsampling_convert_ylines; n++)
1232 *m++=sp->subsampling_convert_ybuf+n*sp->subsampling_convert_ylinelen;
1233 for (n=0; n<sp->subsampling_convert_clines; n++)
1234 *m++=sp->subsampling_convert_cbbuf+n*sp->subsampling_convert_clinelen;
1235 for (n=0; n<sp->subsampling_convert_clines; n++)
1236 *m++=sp->subsampling_convert_crbuf+n*sp->subsampling_convert_clinelen;
1237 sp->subsampling_convert_clinelenout=((sp->strile_width+sp->subsampling_hor-1)/sp->subsampling_hor);
1238 sp->subsampling_convert_state=0;
1239 sp->bytes_per_line=sp->subsampling_convert_clinelenout*(sp->subsampling_ver*sp->subsampling_hor+2);
1240 sp->lines_per_strile=((sp->strile_length+sp->subsampling_ver-1)/sp->subsampling_ver);
1241 sp->subsampling_convert_log=1;
1242 }
1243 }
1244 else
1245 {
1246 sp->libjpeg_jpeg_decompress_struct.jpeg_color_space=JCS_UNKNOWN;
1247 sp->libjpeg_jpeg_decompress_struct.out_color_space=JCS_UNKNOWN;
1248 sp->libjpeg_jpeg_query_style=1;
1249 sp->bytes_per_line=sp->samples_per_pixel_per_plane*sp->strile_width;
1250 sp->lines_per_strile=sp->strile_length;
1251 }
1252 if (jpeg_start_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
1253 return(0);
1254 sp->writeheader_done=1;
1255 return(1);
1256 }
1257
1258 static void
OJPEGLibjpegSessionAbort(TIFF * tif)1259 OJPEGLibjpegSessionAbort(TIFF* tif)
1260 {
1261 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1262 assert(sp->libjpeg_session_active!=0);
1263 jpeg_destroy((jpeg_common_struct*)(&(sp->libjpeg_jpeg_decompress_struct)));
1264 sp->libjpeg_session_active=0;
1265 }
1266
1267 static int
OJPEGReadHeaderInfoSec(TIFF * tif)1268 OJPEGReadHeaderInfoSec(TIFF* tif)
1269 {
1270 static const char module[]="OJPEGReadHeaderInfoSec";
1271 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1272 uint8 m;
1273 uint16 n;
1274 uint8 o;
1275 if (sp->file_size==0)
1276 sp->file_size=TIFFGetFileSize(tif);
1277 if (sp->jpeg_interchange_format!=0)
1278 {
1279 if (sp->jpeg_interchange_format>=sp->file_size)
1280 {
1281 sp->jpeg_interchange_format=0;
1282 sp->jpeg_interchange_format_length=0;
1283 }
1284 else
1285 {
1286 if ((sp->jpeg_interchange_format_length==0) || (sp->jpeg_interchange_format+sp->jpeg_interchange_format_length>sp->file_size))
1287 sp->jpeg_interchange_format_length=sp->file_size-sp->jpeg_interchange_format;
1288 }
1289 }
1290 sp->in_buffer_source=osibsNotSetYet;
1291 sp->in_buffer_next_strile=0;
1292 sp->in_buffer_strile_count=tif->tif_dir.td_nstrips;
1293 sp->in_buffer_file_togo=0;
1294 sp->in_buffer_togo=0;
1295 do
1296 {
1297 if (OJPEGReadBytePeek(sp,&m)==0)
1298 return(0);
1299 if (m!=255)
1300 break;
1301 OJPEGReadByteAdvance(sp);
1302 do
1303 {
1304 if (OJPEGReadByte(sp,&m)==0)
1305 return(0);
1306 } while(m==255);
1307 switch(m)
1308 {
1309 case JPEG_MARKER_SOI:
1310 /* this type of marker has no data, and should be skipped */
1311 break;
1312 case JPEG_MARKER_COM:
1313 case JPEG_MARKER_APP0:
1314 case JPEG_MARKER_APP0+1:
1315 case JPEG_MARKER_APP0+2:
1316 case JPEG_MARKER_APP0+3:
1317 case JPEG_MARKER_APP0+4:
1318 case JPEG_MARKER_APP0+5:
1319 case JPEG_MARKER_APP0+6:
1320 case JPEG_MARKER_APP0+7:
1321 case JPEG_MARKER_APP0+8:
1322 case JPEG_MARKER_APP0+9:
1323 case JPEG_MARKER_APP0+10:
1324 case JPEG_MARKER_APP0+11:
1325 case JPEG_MARKER_APP0+12:
1326 case JPEG_MARKER_APP0+13:
1327 case JPEG_MARKER_APP0+14:
1328 case JPEG_MARKER_APP0+15:
1329 /* this type of marker has data, but it has no use to us (and no place here) and should be skipped */
1330 if (OJPEGReadWord(sp,&n)==0)
1331 return(0);
1332 if (n<2)
1333 {
1334 if (sp->subsamplingcorrect==0)
1335 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
1336 return(0);
1337 }
1338 if (n>2)
1339 OJPEGReadSkip(sp,n-2);
1340 break;
1341 case JPEG_MARKER_DRI:
1342 if (OJPEGReadHeaderInfoSecStreamDri(tif)==0)
1343 return(0);
1344 break;
1345 case JPEG_MARKER_DQT:
1346 if (OJPEGReadHeaderInfoSecStreamDqt(tif)==0)
1347 return(0);
1348 break;
1349 case JPEG_MARKER_DHT:
1350 if (OJPEGReadHeaderInfoSecStreamDht(tif)==0)
1351 return(0);
1352 break;
1353 case JPEG_MARKER_SOF0:
1354 case JPEG_MARKER_SOF1:
1355 case JPEG_MARKER_SOF3:
1356 if (OJPEGReadHeaderInfoSecStreamSof(tif,m)==0)
1357 return(0);
1358 if (sp->subsamplingcorrect!=0)
1359 return(1);
1360 break;
1361 case JPEG_MARKER_SOS:
1362 if (sp->subsamplingcorrect!=0)
1363 return(1);
1364 assert(sp->plane_sample_offset==0);
1365 if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
1366 return(0);
1367 break;
1368 default:
1369 TIFFErrorExt(tif->tif_clientdata,module,"Unknown marker type %d in JPEG data",m);
1370 return(0);
1371 }
1372 } while(m!=JPEG_MARKER_SOS);
1373 if (sp->subsamplingcorrect)
1374 return(1);
1375 if (sp->sof_log==0)
1376 {
1377 if (OJPEGReadHeaderInfoSecTablesQTable(tif)==0)
1378 return(0);
1379 sp->sof_marker_id=JPEG_MARKER_SOF0;
1380 for (o=0; o<sp->samples_per_pixel; o++)
1381 sp->sof_c[o]=o;
1382 sp->sof_hv[0]=((sp->subsampling_hor<<4)|sp->subsampling_ver);
1383 for (o=1; o<sp->samples_per_pixel; o++)
1384 sp->sof_hv[o]=17;
1385 sp->sof_x=sp->strile_width;
1386 sp->sof_y=sp->strile_length_total;
1387 sp->sof_log=1;
1388 if (OJPEGReadHeaderInfoSecTablesDcTable(tif)==0)
1389 return(0);
1390 if (OJPEGReadHeaderInfoSecTablesAcTable(tif)==0)
1391 return(0);
1392 for (o=1; o<sp->samples_per_pixel; o++)
1393 sp->sos_cs[o]=o;
1394 }
1395 return(1);
1396 }
1397
1398 static int
OJPEGReadHeaderInfoSecStreamDri(TIFF * tif)1399 OJPEGReadHeaderInfoSecStreamDri(TIFF* tif)
1400 {
1401 /* This could easily cause trouble in some cases... but no such cases have
1402 occurred so far */
1403 static const char module[]="OJPEGReadHeaderInfoSecStreamDri";
1404 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1405 uint16 m;
1406 if (OJPEGReadWord(sp,&m)==0)
1407 return(0);
1408 if (m!=4)
1409 {
1410 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DRI marker in JPEG data");
1411 return(0);
1412 }
1413 if (OJPEGReadWord(sp,&m)==0)
1414 return(0);
1415 sp->restart_interval=m;
1416 return(1);
1417 }
1418
1419 static int
OJPEGReadHeaderInfoSecStreamDqt(TIFF * tif)1420 OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif)
1421 {
1422 /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1423 static const char module[]="OJPEGReadHeaderInfoSecStreamDqt";
1424 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1425 uint16 m;
1426 uint32 na;
1427 uint8* nb;
1428 uint8 o;
1429 if (OJPEGReadWord(sp,&m)==0)
1430 return(0);
1431 if (m<=2)
1432 {
1433 if (sp->subsamplingcorrect==0)
1434 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
1435 return(0);
1436 }
1437 if (sp->subsamplingcorrect!=0)
1438 OJPEGReadSkip(sp,m-2);
1439 else
1440 {
1441 m-=2;
1442 do
1443 {
1444 if (m<65)
1445 {
1446 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
1447 return(0);
1448 }
1449 na=sizeof(uint32)+69;
1450 nb=_TIFFmalloc(na);
1451 if (nb==0)
1452 {
1453 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1454 return(0);
1455 }
1456 *(uint32*)nb=na;
1457 nb[sizeof(uint32)]=255;
1458 nb[sizeof(uint32)+1]=JPEG_MARKER_DQT;
1459 nb[sizeof(uint32)+2]=0;
1460 nb[sizeof(uint32)+3]=67;
1461 if (OJPEGReadBlock(sp,65,&nb[sizeof(uint32)+4])==0) {
1462 _TIFFfree(nb);
1463 return(0);
1464 }
1465 o=nb[sizeof(uint32)+4]&15;
1466 if (3<o)
1467 {
1468 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
1469 _TIFFfree(nb);
1470 return(0);
1471 }
1472 if (sp->qtable[o]!=0)
1473 _TIFFfree(sp->qtable[o]);
1474 sp->qtable[o]=nb;
1475 m-=65;
1476 } while(m>0);
1477 }
1478 return(1);
1479 }
1480
1481 static int
OJPEGReadHeaderInfoSecStreamDht(TIFF * tif)1482 OJPEGReadHeaderInfoSecStreamDht(TIFF* tif)
1483 {
1484 /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1485 /* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
1486 static const char module[]="OJPEGReadHeaderInfoSecStreamDht";
1487 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1488 uint16 m;
1489 uint32 na;
1490 uint8* nb;
1491 uint8 o;
1492 if (OJPEGReadWord(sp,&m)==0)
1493 return(0);
1494 if (m<=2)
1495 {
1496 if (sp->subsamplingcorrect==0)
1497 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1498 return(0);
1499 }
1500 if (sp->subsamplingcorrect!=0)
1501 {
1502 OJPEGReadSkip(sp,m-2);
1503 }
1504 else
1505 {
1506 na=sizeof(uint32)+2+m;
1507 nb=_TIFFmalloc(na);
1508 if (nb==0)
1509 {
1510 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1511 return(0);
1512 }
1513 *(uint32*)nb=na;
1514 nb[sizeof(uint32)]=255;
1515 nb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
1516 nb[sizeof(uint32)+2]=(m>>8);
1517 nb[sizeof(uint32)+3]=(m&255);
1518 if (OJPEGReadBlock(sp,m-2,&nb[sizeof(uint32)+4])==0) {
1519 _TIFFfree(nb);
1520 return(0);
1521 }
1522 o=nb[sizeof(uint32)+4];
1523 if ((o&240)==0)
1524 {
1525 if (3<o)
1526 {
1527 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1528 _TIFFfree(nb);
1529 return(0);
1530 }
1531 if (sp->dctable[o]!=0)
1532 _TIFFfree(sp->dctable[o]);
1533 sp->dctable[o]=nb;
1534 }
1535 else
1536 {
1537 if ((o&240)!=16)
1538 {
1539 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1540 _TIFFfree(nb);
1541 return(0);
1542 }
1543 o&=15;
1544 if (3<o)
1545 {
1546 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1547 _TIFFfree(nb);
1548 return(0);
1549 }
1550 if (sp->actable[o]!=0)
1551 _TIFFfree(sp->actable[o]);
1552 sp->actable[o]=nb;
1553 }
1554 }
1555 return(1);
1556 }
1557
1558 static int
OJPEGReadHeaderInfoSecStreamSof(TIFF * tif,uint8 marker_id)1559 OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id)
1560 {
1561 /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1562 static const char module[]="OJPEGReadHeaderInfoSecStreamSof";
1563 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1564 uint16 m;
1565 uint16 n;
1566 uint8 o;
1567 uint16 p;
1568 uint16 q;
1569 if (sp->sof_log!=0)
1570 {
1571 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
1572 return(0);
1573 }
1574 if (sp->subsamplingcorrect==0)
1575 sp->sof_marker_id=marker_id;
1576 /* Lf: data length */
1577 if (OJPEGReadWord(sp,&m)==0)
1578 return(0);
1579 if (m<11)
1580 {
1581 if (sp->subsamplingcorrect==0)
1582 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
1583 return(0);
1584 }
1585 m-=8;
1586 if (m%3!=0)
1587 {
1588 if (sp->subsamplingcorrect==0)
1589 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
1590 return(0);
1591 }
1592 n=m/3;
1593 if (sp->subsamplingcorrect==0)
1594 {
1595 if (n!=sp->samples_per_pixel)
1596 {
1597 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of samples");
1598 return(0);
1599 }
1600 }
1601 /* P: Sample precision */
1602 if (OJPEGReadByte(sp,&o)==0)
1603 return(0);
1604 if (o!=8)
1605 {
1606 if (sp->subsamplingcorrect==0)
1607 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of bits per sample");
1608 return(0);
1609 }
1610 /* Y: Number of lines, X: Number of samples per line */
1611 if (sp->subsamplingcorrect)
1612 OJPEGReadSkip(sp,4);
1613 else
1614 {
1615 /* Y: Number of lines */
1616 if (OJPEGReadWord(sp,&p)==0)
1617 return(0);
1618 if (((uint32)p<sp->image_length) && ((uint32)p<sp->strile_length_total))
1619 {
1620 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected height");
1621 return(0);
1622 }
1623 sp->sof_y=p;
1624 /* X: Number of samples per line */
1625 if (OJPEGReadWord(sp,&p)==0)
1626 return(0);
1627 if (((uint32)p<sp->image_width) && ((uint32)p<sp->strile_width))
1628 {
1629 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected width");
1630 return(0);
1631 }
1632 if ((uint32)p>sp->strile_width)
1633 {
1634 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data image width exceeds expected image width");
1635 return(0);
1636 }
1637 sp->sof_x=p;
1638 }
1639 /* Nf: Number of image components in frame */
1640 if (OJPEGReadByte(sp,&o)==0)
1641 return(0);
1642 if (o!=n)
1643 {
1644 if (sp->subsamplingcorrect==0)
1645 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
1646 return(0);
1647 }
1648 /* per component stuff */
1649 /* TODO: double-check that flow implies that n cannot be as big as to make us overflow sof_c, sof_hv and sof_tq arrays */
1650 for (q=0; q<n; q++)
1651 {
1652 /* C: Component identifier */
1653 if (OJPEGReadByte(sp,&o)==0)
1654 return(0);
1655 if (sp->subsamplingcorrect==0)
1656 sp->sof_c[q]=o;
1657 /* H: Horizontal sampling factor, and V: Vertical sampling factor */
1658 if (OJPEGReadByte(sp,&o)==0)
1659 return(0);
1660 if (sp->subsamplingcorrect!=0)
1661 {
1662 if (q==0)
1663 {
1664 sp->subsampling_hor=(o>>4);
1665 sp->subsampling_ver=(o&15);
1666 if (((sp->subsampling_hor!=1) && (sp->subsampling_hor!=2) && (sp->subsampling_hor!=4)) ||
1667 ((sp->subsampling_ver!=1) && (sp->subsampling_ver!=2) && (sp->subsampling_ver!=4)))
1668 sp->subsampling_force_desubsampling_inside_decompression=1;
1669 }
1670 else
1671 {
1672 if (o!=17)
1673 sp->subsampling_force_desubsampling_inside_decompression=1;
1674 }
1675 }
1676 else
1677 {
1678 sp->sof_hv[q]=o;
1679 if (sp->subsampling_force_desubsampling_inside_decompression==0)
1680 {
1681 if (q==0)
1682 {
1683 if (o!=((sp->subsampling_hor<<4)|sp->subsampling_ver))
1684 {
1685 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
1686 return(0);
1687 }
1688 }
1689 else
1690 {
1691 if (o!=17)
1692 {
1693 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
1694 return(0);
1695 }
1696 }
1697 }
1698 }
1699 /* Tq: Quantization table destination selector */
1700 if (OJPEGReadByte(sp,&o)==0)
1701 return(0);
1702 if (sp->subsamplingcorrect==0)
1703 sp->sof_tq[q]=o;
1704 }
1705 if (sp->subsamplingcorrect==0)
1706 sp->sof_log=1;
1707 return(1);
1708 }
1709
1710 static int
OJPEGReadHeaderInfoSecStreamSos(TIFF * tif)1711 OJPEGReadHeaderInfoSecStreamSos(TIFF* tif)
1712 {
1713 /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1714 static const char module[]="OJPEGReadHeaderInfoSecStreamSos";
1715 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1716 uint16 m;
1717 uint8 n;
1718 uint8 o;
1719 assert(sp->subsamplingcorrect==0);
1720 if (sp->sof_log==0)
1721 {
1722 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
1723 return(0);
1724 }
1725 /* Ls */
1726 if (OJPEGReadWord(sp,&m)==0)
1727 return(0);
1728 if (m!=6+sp->samples_per_pixel_per_plane*2)
1729 {
1730 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
1731 return(0);
1732 }
1733 /* Ns */
1734 if (OJPEGReadByte(sp,&n)==0)
1735 return(0);
1736 if (n!=sp->samples_per_pixel_per_plane)
1737 {
1738 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
1739 return(0);
1740 }
1741 /* Cs, Td, and Ta */
1742 for (o=0; o<sp->samples_per_pixel_per_plane; o++)
1743 {
1744 /* Cs */
1745 if (OJPEGReadByte(sp,&n)==0)
1746 return(0);
1747 sp->sos_cs[sp->plane_sample_offset+o]=n;
1748 /* Td and Ta */
1749 if (OJPEGReadByte(sp,&n)==0)
1750 return(0);
1751 sp->sos_tda[sp->plane_sample_offset+o]=n;
1752 }
1753 /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as per LibJpeg source */
1754 OJPEGReadSkip(sp,3);
1755 return(1);
1756 }
1757
1758 static int
OJPEGReadHeaderInfoSecTablesQTable(TIFF * tif)1759 OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif)
1760 {
1761 static const char module[]="OJPEGReadHeaderInfoSecTablesQTable";
1762 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1763 uint8 m;
1764 uint8 n;
1765 uint32 oa;
1766 uint8* ob;
1767 uint32 p;
1768 if (sp->qtable_offset[0]==0)
1769 {
1770 TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
1771 return(0);
1772 }
1773 sp->in_buffer_file_pos_log=0;
1774 for (m=0; m<sp->samples_per_pixel; m++)
1775 {
1776 if ((sp->qtable_offset[m]!=0) && ((m==0) || (sp->qtable_offset[m]!=sp->qtable_offset[m-1])))
1777 {
1778 for (n=0; n<m-1; n++)
1779 {
1780 if (sp->qtable_offset[m]==sp->qtable_offset[n])
1781 {
1782 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegQTables tag value");
1783 return(0);
1784 }
1785 }
1786 oa=sizeof(uint32)+69;
1787 ob=_TIFFmalloc(oa);
1788 if (ob==0)
1789 {
1790 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1791 return(0);
1792 }
1793 *(uint32*)ob=oa;
1794 ob[sizeof(uint32)]=255;
1795 ob[sizeof(uint32)+1]=JPEG_MARKER_DQT;
1796 ob[sizeof(uint32)+2]=0;
1797 ob[sizeof(uint32)+3]=67;
1798 ob[sizeof(uint32)+4]=m;
1799 TIFFSeekFile(tif,sp->qtable_offset[m],SEEK_SET);
1800 p=(uint32)TIFFReadFile(tif,&ob[sizeof(uint32)+5],64);
1801 if (p!=64)
1802 {
1803 _TIFFfree(ob);
1804 return(0);
1805 }
1806 if (sp->qtable[m]!=0)
1807 _TIFFfree(sp->qtable[m]);
1808 sp->qtable[m]=ob;
1809 sp->sof_tq[m]=m;
1810 }
1811 else
1812 sp->sof_tq[m]=sp->sof_tq[m-1];
1813 }
1814 return(1);
1815 }
1816
1817 static int
OJPEGReadHeaderInfoSecTablesDcTable(TIFF * tif)1818 OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif)
1819 {
1820 static const char module[]="OJPEGReadHeaderInfoSecTablesDcTable";
1821 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1822 uint8 m;
1823 uint8 n;
1824 uint8 o[16];
1825 uint32 p;
1826 uint32 q;
1827 uint32 ra;
1828 uint8* rb;
1829 if (sp->dctable_offset[0]==0)
1830 {
1831 TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
1832 return(0);
1833 }
1834 sp->in_buffer_file_pos_log=0;
1835 for (m=0; m<sp->samples_per_pixel; m++)
1836 {
1837 if ((sp->dctable_offset[m]!=0) && ((m==0) || (sp->dctable_offset[m]!=sp->dctable_offset[m-1])))
1838 {
1839 for (n=0; n<m-1; n++)
1840 {
1841 if (sp->dctable_offset[m]==sp->dctable_offset[n])
1842 {
1843 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegDcTables tag value");
1844 return(0);
1845 }
1846 }
1847 TIFFSeekFile(tif,sp->dctable_offset[m],SEEK_SET);
1848 p=(uint32)TIFFReadFile(tif,o,16);
1849 if (p!=16)
1850 return(0);
1851 q=0;
1852 for (n=0; n<16; n++)
1853 q+=o[n];
1854 ra=sizeof(uint32)+21+q;
1855 rb=_TIFFmalloc(ra);
1856 if (rb==0)
1857 {
1858 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1859 return(0);
1860 }
1861 *(uint32*)rb=ra;
1862 rb[sizeof(uint32)]=255;
1863 rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
1864 rb[sizeof(uint32)+2]=(uint8)((19+q)>>8);
1865 rb[sizeof(uint32)+3]=((19+q)&255);
1866 rb[sizeof(uint32)+4]=m;
1867 for (n=0; n<16; n++)
1868 rb[sizeof(uint32)+5+n]=o[n];
1869 p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
1870 if (p!=q)
1871 {
1872 _TIFFfree(rb);
1873 return(0);
1874 }
1875 if (sp->dctable[m]!=0)
1876 _TIFFfree(sp->dctable[m]);
1877 sp->dctable[m]=rb;
1878 sp->sos_tda[m]=(m<<4);
1879 }
1880 else
1881 sp->sos_tda[m]=sp->sos_tda[m-1];
1882 }
1883 return(1);
1884 }
1885
1886 static int
OJPEGReadHeaderInfoSecTablesAcTable(TIFF * tif)1887 OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif)
1888 {
1889 static const char module[]="OJPEGReadHeaderInfoSecTablesAcTable";
1890 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1891 uint8 m;
1892 uint8 n;
1893 uint8 o[16];
1894 uint32 p;
1895 uint32 q;
1896 uint32 ra;
1897 uint8* rb;
1898 if (sp->actable_offset[0]==0)
1899 {
1900 TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
1901 return(0);
1902 }
1903 sp->in_buffer_file_pos_log=0;
1904 for (m=0; m<sp->samples_per_pixel; m++)
1905 {
1906 if ((sp->actable_offset[m]!=0) && ((m==0) || (sp->actable_offset[m]!=sp->actable_offset[m-1])))
1907 {
1908 for (n=0; n<m-1; n++)
1909 {
1910 if (sp->actable_offset[m]==sp->actable_offset[n])
1911 {
1912 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegAcTables tag value");
1913 return(0);
1914 }
1915 }
1916 TIFFSeekFile(tif,sp->actable_offset[m],SEEK_SET);
1917 p=(uint32)TIFFReadFile(tif,o,16);
1918 if (p!=16)
1919 return(0);
1920 q=0;
1921 for (n=0; n<16; n++)
1922 q+=o[n];
1923 ra=sizeof(uint32)+21+q;
1924 rb=_TIFFmalloc(ra);
1925 if (rb==0)
1926 {
1927 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1928 return(0);
1929 }
1930 *(uint32*)rb=ra;
1931 rb[sizeof(uint32)]=255;
1932 rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
1933 rb[sizeof(uint32)+2]=(uint8)((19+q)>>8);
1934 rb[sizeof(uint32)+3]=((19+q)&255);
1935 rb[sizeof(uint32)+4]=(16|m);
1936 for (n=0; n<16; n++)
1937 rb[sizeof(uint32)+5+n]=o[n];
1938 p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
1939 if (p!=q)
1940 {
1941 _TIFFfree(rb);
1942 return(0);
1943 }
1944 if (sp->actable[m]!=0)
1945 _TIFFfree(sp->actable[m]);
1946 sp->actable[m]=rb;
1947 sp->sos_tda[m]=(sp->sos_tda[m]|m);
1948 }
1949 else
1950 sp->sos_tda[m]=(sp->sos_tda[m]|(sp->sos_tda[m-1]&15));
1951 }
1952 return(1);
1953 }
1954
1955 static int
OJPEGReadBufferFill(OJPEGState * sp)1956 OJPEGReadBufferFill(OJPEGState* sp)
1957 {
1958 uint16 m;
1959 tmsize_t n;
1960 /* TODO: double-check: when subsamplingcorrect is set, no call to TIFFErrorExt or TIFFWarningExt should be made
1961 * in any other case, seek or read errors should be passed through */
1962 do
1963 {
1964 if (sp->in_buffer_file_togo!=0)
1965 {
1966 if (sp->in_buffer_file_pos_log==0)
1967 {
1968 TIFFSeekFile(sp->tif,sp->in_buffer_file_pos,SEEK_SET);
1969 sp->in_buffer_file_pos_log=1;
1970 }
1971 m=OJPEG_BUFFER;
1972 if ((uint64)m>sp->in_buffer_file_togo)
1973 m=(uint16)sp->in_buffer_file_togo;
1974 n=TIFFReadFile(sp->tif,sp->in_buffer,(tmsize_t)m);
1975 if (n==0)
1976 return(0);
1977 assert(n>0);
1978 assert(n<=OJPEG_BUFFER);
1979 assert(n<65536);
1980 assert((uint64)n<=sp->in_buffer_file_togo);
1981 m=(uint16)n;
1982 sp->in_buffer_togo=m;
1983 sp->in_buffer_cur=sp->in_buffer;
1984 sp->in_buffer_file_togo-=m;
1985 sp->in_buffer_file_pos+=m;
1986 break;
1987 }
1988 sp->in_buffer_file_pos_log=0;
1989 switch(sp->in_buffer_source)
1990 {
1991 case osibsNotSetYet:
1992 if (sp->jpeg_interchange_format!=0)
1993 {
1994 sp->in_buffer_file_pos=sp->jpeg_interchange_format;
1995 sp->in_buffer_file_togo=sp->jpeg_interchange_format_length;
1996 }
1997 sp->in_buffer_source=osibsJpegInterchangeFormat;
1998 break;
1999 case osibsJpegInterchangeFormat:
2000 sp->in_buffer_source=osibsStrile;
2001 break;
2002 case osibsStrile:
2003 if (!_TIFFFillStriles( sp->tif )
2004 || sp->tif->tif_dir.td_stripoffset == NULL
2005 || sp->tif->tif_dir.td_stripbytecount == NULL)
2006 return 0;
2007
2008 if (sp->in_buffer_next_strile==sp->in_buffer_strile_count)
2009 sp->in_buffer_source=osibsEof;
2010 else
2011 {
2012 sp->in_buffer_file_pos=sp->tif->tif_dir.td_stripoffset[sp->in_buffer_next_strile];
2013 if (sp->in_buffer_file_pos!=0)
2014 {
2015 if (sp->in_buffer_file_pos>=sp->file_size)
2016 sp->in_buffer_file_pos=0;
2017 else if (sp->tif->tif_dir.td_stripbytecount==NULL)
2018 sp->in_buffer_file_togo=sp->file_size-sp->in_buffer_file_pos;
2019 else
2020 {
2021 if (sp->tif->tif_dir.td_stripbytecount == 0) {
2022 TIFFErrorExt(sp->tif->tif_clientdata,sp->tif->tif_name,"Strip byte counts are missing");
2023 return(0);
2024 }
2025 sp->in_buffer_file_togo=sp->tif->tif_dir.td_stripbytecount[sp->in_buffer_next_strile];
2026 if (sp->in_buffer_file_togo==0)
2027 sp->in_buffer_file_pos=0;
2028 else if (sp->in_buffer_file_pos+sp->in_buffer_file_togo>sp->file_size)
2029 sp->in_buffer_file_togo=sp->file_size-sp->in_buffer_file_pos;
2030 }
2031 }
2032 sp->in_buffer_next_strile++;
2033 }
2034 break;
2035 default:
2036 return(0);
2037 }
2038 } while (1);
2039 return(1);
2040 }
2041
2042 static int
OJPEGReadByte(OJPEGState * sp,uint8 * byte)2043 OJPEGReadByte(OJPEGState* sp, uint8* byte)
2044 {
2045 if (sp->in_buffer_togo==0)
2046 {
2047 if (OJPEGReadBufferFill(sp)==0)
2048 return(0);
2049 assert(sp->in_buffer_togo>0);
2050 }
2051 *byte=*(sp->in_buffer_cur);
2052 sp->in_buffer_cur++;
2053 sp->in_buffer_togo--;
2054 return(1);
2055 }
2056
2057 static int
OJPEGReadBytePeek(OJPEGState * sp,uint8 * byte)2058 OJPEGReadBytePeek(OJPEGState* sp, uint8* byte)
2059 {
2060 if (sp->in_buffer_togo==0)
2061 {
2062 if (OJPEGReadBufferFill(sp)==0)
2063 return(0);
2064 assert(sp->in_buffer_togo>0);
2065 }
2066 *byte=*(sp->in_buffer_cur);
2067 return(1);
2068 }
2069
2070 static void
OJPEGReadByteAdvance(OJPEGState * sp)2071 OJPEGReadByteAdvance(OJPEGState* sp)
2072 {
2073 assert(sp->in_buffer_togo>0);
2074 sp->in_buffer_cur++;
2075 sp->in_buffer_togo--;
2076 }
2077
2078 static int
OJPEGReadWord(OJPEGState * sp,uint16 * word)2079 OJPEGReadWord(OJPEGState* sp, uint16* word)
2080 {
2081 uint8 m;
2082 if (OJPEGReadByte(sp,&m)==0)
2083 return(0);
2084 *word=(m<<8);
2085 if (OJPEGReadByte(sp,&m)==0)
2086 return(0);
2087 *word|=m;
2088 return(1);
2089 }
2090
2091 static int
OJPEGReadBlock(OJPEGState * sp,uint16 len,void * mem)2092 OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem)
2093 {
2094 uint16 mlen;
2095 uint8* mmem;
2096 uint16 n;
2097 assert(len>0);
2098 mlen=len;
2099 mmem=mem;
2100 do
2101 {
2102 if (sp->in_buffer_togo==0)
2103 {
2104 if (OJPEGReadBufferFill(sp)==0)
2105 return(0);
2106 assert(sp->in_buffer_togo>0);
2107 }
2108 n=mlen;
2109 if (n>sp->in_buffer_togo)
2110 n=sp->in_buffer_togo;
2111 _TIFFmemcpy(mmem,sp->in_buffer_cur,n);
2112 sp->in_buffer_cur+=n;
2113 sp->in_buffer_togo-=n;
2114 mlen-=n;
2115 mmem+=n;
2116 } while(mlen>0);
2117 return(1);
2118 }
2119
2120 static void
OJPEGReadSkip(OJPEGState * sp,uint16 len)2121 OJPEGReadSkip(OJPEGState* sp, uint16 len)
2122 {
2123 uint16 m;
2124 uint16 n;
2125 m=len;
2126 n=m;
2127 if (n>sp->in_buffer_togo)
2128 n=sp->in_buffer_togo;
2129 sp->in_buffer_cur+=n;
2130 sp->in_buffer_togo-=n;
2131 m-=n;
2132 if (m>0)
2133 {
2134 assert(sp->in_buffer_togo==0);
2135 n=m;
2136 if ((uint64)n>sp->in_buffer_file_togo)
2137 n=(uint16)sp->in_buffer_file_togo;
2138 sp->in_buffer_file_pos+=n;
2139 sp->in_buffer_file_togo-=n;
2140 sp->in_buffer_file_pos_log=0;
2141 /* we don't skip past jpeginterchangeformat/strile block...
2142 * if that is asked from us, we're dealing with totally bazurk
2143 * data anyway, and we've not seen this happening on any
2144 * testfile, so we might as well likely cause some other
2145 * meaningless error to be passed at some later time
2146 */
2147 }
2148 }
2149
2150 static int
OJPEGWriteStream(TIFF * tif,void ** mem,uint32 * len)2151 OJPEGWriteStream(TIFF* tif, void** mem, uint32* len)
2152 {
2153 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2154 *len=0;
2155 do
2156 {
2157 assert(sp->out_state<=ososEoi);
2158 switch(sp->out_state)
2159 {
2160 case ososSoi:
2161 OJPEGWriteStreamSoi(tif,mem,len);
2162 break;
2163 case ososQTable0:
2164 OJPEGWriteStreamQTable(tif,0,mem,len);
2165 break;
2166 case ososQTable1:
2167 OJPEGWriteStreamQTable(tif,1,mem,len);
2168 break;
2169 case ososQTable2:
2170 OJPEGWriteStreamQTable(tif,2,mem,len);
2171 break;
2172 case ososQTable3:
2173 OJPEGWriteStreamQTable(tif,3,mem,len);
2174 break;
2175 case ososDcTable0:
2176 OJPEGWriteStreamDcTable(tif,0,mem,len);
2177 break;
2178 case ososDcTable1:
2179 OJPEGWriteStreamDcTable(tif,1,mem,len);
2180 break;
2181 case ososDcTable2:
2182 OJPEGWriteStreamDcTable(tif,2,mem,len);
2183 break;
2184 case ososDcTable3:
2185 OJPEGWriteStreamDcTable(tif,3,mem,len);
2186 break;
2187 case ososAcTable0:
2188 OJPEGWriteStreamAcTable(tif,0,mem,len);
2189 break;
2190 case ososAcTable1:
2191 OJPEGWriteStreamAcTable(tif,1,mem,len);
2192 break;
2193 case ososAcTable2:
2194 OJPEGWriteStreamAcTable(tif,2,mem,len);
2195 break;
2196 case ososAcTable3:
2197 OJPEGWriteStreamAcTable(tif,3,mem,len);
2198 break;
2199 case ososDri:
2200 OJPEGWriteStreamDri(tif,mem,len);
2201 break;
2202 case ososSof:
2203 OJPEGWriteStreamSof(tif,mem,len);
2204 break;
2205 case ososSos:
2206 OJPEGWriteStreamSos(tif,mem,len);
2207 break;
2208 case ososCompressed:
2209 if (OJPEGWriteStreamCompressed(tif,mem,len)==0)
2210 return(0);
2211 break;
2212 case ososRst:
2213 OJPEGWriteStreamRst(tif,mem,len);
2214 break;
2215 case ososEoi:
2216 OJPEGWriteStreamEoi(tif,mem,len);
2217 break;
2218 }
2219 } while (*len==0);
2220 return(1);
2221 }
2222
2223 static void
OJPEGWriteStreamSoi(TIFF * tif,void ** mem,uint32 * len)2224 OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len)
2225 {
2226 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2227 assert(OJPEG_BUFFER>=2);
2228 sp->out_buffer[0]=255;
2229 sp->out_buffer[1]=JPEG_MARKER_SOI;
2230 *len=2;
2231 *mem=(void*)sp->out_buffer;
2232 sp->out_state++;
2233 }
2234
2235 static void
OJPEGWriteStreamQTable(TIFF * tif,uint8 table_index,void ** mem,uint32 * len)2236 OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
2237 {
2238 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2239 if (sp->qtable[table_index]!=0)
2240 {
2241 *mem=(void*)(sp->qtable[table_index]+sizeof(uint32));
2242 *len=*((uint32*)sp->qtable[table_index])-sizeof(uint32);
2243 }
2244 sp->out_state++;
2245 }
2246
2247 static void
OJPEGWriteStreamDcTable(TIFF * tif,uint8 table_index,void ** mem,uint32 * len)2248 OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
2249 {
2250 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2251 if (sp->dctable[table_index]!=0)
2252 {
2253 *mem=(void*)(sp->dctable[table_index]+sizeof(uint32));
2254 *len=*((uint32*)sp->dctable[table_index])-sizeof(uint32);
2255 }
2256 sp->out_state++;
2257 }
2258
2259 static void
OJPEGWriteStreamAcTable(TIFF * tif,uint8 table_index,void ** mem,uint32 * len)2260 OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
2261 {
2262 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2263 if (sp->actable[table_index]!=0)
2264 {
2265 *mem=(void*)(sp->actable[table_index]+sizeof(uint32));
2266 *len=*((uint32*)sp->actable[table_index])-sizeof(uint32);
2267 }
2268 sp->out_state++;
2269 }
2270
2271 static void
OJPEGWriteStreamDri(TIFF * tif,void ** mem,uint32 * len)2272 OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len)
2273 {
2274 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2275 assert(OJPEG_BUFFER>=6);
2276 if (sp->restart_interval!=0)
2277 {
2278 sp->out_buffer[0]=255;
2279 sp->out_buffer[1]=JPEG_MARKER_DRI;
2280 sp->out_buffer[2]=0;
2281 sp->out_buffer[3]=4;
2282 sp->out_buffer[4]=(sp->restart_interval>>8);
2283 sp->out_buffer[5]=(sp->restart_interval&255);
2284 *len=6;
2285 *mem=(void*)sp->out_buffer;
2286 }
2287 sp->out_state++;
2288 }
2289
2290 static void
OJPEGWriteStreamSof(TIFF * tif,void ** mem,uint32 * len)2291 OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len)
2292 {
2293 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2294 uint8 m;
2295 assert(OJPEG_BUFFER>=2+8+sp->samples_per_pixel_per_plane*3);
2296 assert(255>=8+sp->samples_per_pixel_per_plane*3);
2297 sp->out_buffer[0]=255;
2298 sp->out_buffer[1]=sp->sof_marker_id;
2299 /* Lf */
2300 sp->out_buffer[2]=0;
2301 sp->out_buffer[3]=8+sp->samples_per_pixel_per_plane*3;
2302 /* P */
2303 sp->out_buffer[4]=8;
2304 /* Y */
2305 sp->out_buffer[5]=(uint8)(sp->sof_y>>8);
2306 sp->out_buffer[6]=(sp->sof_y&255);
2307 /* X */
2308 sp->out_buffer[7]=(uint8)(sp->sof_x>>8);
2309 sp->out_buffer[8]=(sp->sof_x&255);
2310 /* Nf */
2311 sp->out_buffer[9]=sp->samples_per_pixel_per_plane;
2312 for (m=0; m<sp->samples_per_pixel_per_plane; m++)
2313 {
2314 /* C */
2315 sp->out_buffer[10+m*3]=sp->sof_c[sp->plane_sample_offset+m];
2316 /* H and V */
2317 sp->out_buffer[10+m*3+1]=sp->sof_hv[sp->plane_sample_offset+m];
2318 /* Tq */
2319 sp->out_buffer[10+m*3+2]=sp->sof_tq[sp->plane_sample_offset+m];
2320 }
2321 *len=10+sp->samples_per_pixel_per_plane*3;
2322 *mem=(void*)sp->out_buffer;
2323 sp->out_state++;
2324 }
2325
2326 static void
OJPEGWriteStreamSos(TIFF * tif,void ** mem,uint32 * len)2327 OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len)
2328 {
2329 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2330 uint8 m;
2331 assert(OJPEG_BUFFER>=2+6+sp->samples_per_pixel_per_plane*2);
2332 assert(255>=6+sp->samples_per_pixel_per_plane*2);
2333 sp->out_buffer[0]=255;
2334 sp->out_buffer[1]=JPEG_MARKER_SOS;
2335 /* Ls */
2336 sp->out_buffer[2]=0;
2337 sp->out_buffer[3]=6+sp->samples_per_pixel_per_plane*2;
2338 /* Ns */
2339 sp->out_buffer[4]=sp->samples_per_pixel_per_plane;
2340 for (m=0; m<sp->samples_per_pixel_per_plane; m++)
2341 {
2342 /* Cs */
2343 sp->out_buffer[5+m*2]=sp->sos_cs[sp->plane_sample_offset+m];
2344 /* Td and Ta */
2345 sp->out_buffer[5+m*2+1]=sp->sos_tda[sp->plane_sample_offset+m];
2346 }
2347 /* Ss */
2348 sp->out_buffer[5+sp->samples_per_pixel_per_plane*2]=0;
2349 /* Se */
2350 sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+1]=63;
2351 /* Ah and Al */
2352 sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+2]=0;
2353 *len=8+sp->samples_per_pixel_per_plane*2;
2354 *mem=(void*)sp->out_buffer;
2355 sp->out_state++;
2356 }
2357
2358 static int
OJPEGWriteStreamCompressed(TIFF * tif,void ** mem,uint32 * len)2359 OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len)
2360 {
2361 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2362 if (sp->in_buffer_togo==0)
2363 {
2364 if (OJPEGReadBufferFill(sp)==0)
2365 return(0);
2366 assert(sp->in_buffer_togo>0);
2367 }
2368 *len=sp->in_buffer_togo;
2369 *mem=(void*)sp->in_buffer_cur;
2370 sp->in_buffer_togo=0;
2371 if (sp->in_buffer_file_togo==0)
2372 {
2373 switch(sp->in_buffer_source)
2374 {
2375 case osibsStrile:
2376 if (sp->in_buffer_next_strile<sp->in_buffer_strile_count)
2377 sp->out_state=ososRst;
2378 else
2379 sp->out_state=ososEoi;
2380 break;
2381 case osibsEof:
2382 sp->out_state=ososEoi;
2383 break;
2384 default:
2385 break;
2386 }
2387 }
2388 return(1);
2389 }
2390
2391 static void
OJPEGWriteStreamRst(TIFF * tif,void ** mem,uint32 * len)2392 OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len)
2393 {
2394 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2395 assert(OJPEG_BUFFER>=2);
2396 sp->out_buffer[0]=255;
2397 sp->out_buffer[1]=JPEG_MARKER_RST0+sp->restart_index;
2398 sp->restart_index++;
2399 if (sp->restart_index==8)
2400 sp->restart_index=0;
2401 *len=2;
2402 *mem=(void*)sp->out_buffer;
2403 sp->out_state=ososCompressed;
2404 }
2405
2406 static void
OJPEGWriteStreamEoi(TIFF * tif,void ** mem,uint32 * len)2407 OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len)
2408 {
2409 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2410 assert(OJPEG_BUFFER>=2);
2411 sp->out_buffer[0]=255;
2412 sp->out_buffer[1]=JPEG_MARKER_EOI;
2413 *len=2;
2414 *mem=(void*)sp->out_buffer;
2415 }
2416
2417 #ifndef LIBJPEG_ENCAP_EXTERNAL
2418 static int
jpeg_create_decompress_encap(OJPEGState * sp,jpeg_decompress_struct * cinfo)2419 jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
2420 {
2421 if( SETJMP(sp->exit_jmpbuf) )
2422 return 0;
2423 else {
2424 jpeg_create_decompress(cinfo);
2425 return 1;
2426 }
2427 }
2428 #endif
2429
2430 #ifndef LIBJPEG_ENCAP_EXTERNAL
2431 static int
jpeg_read_header_encap(OJPEGState * sp,jpeg_decompress_struct * cinfo,uint8 require_image)2432 jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image)
2433 {
2434 if( SETJMP(sp->exit_jmpbuf) )
2435 return 0;
2436 else {
2437 jpeg_read_header(cinfo,require_image);
2438 return 1;
2439 }
2440 }
2441 #endif
2442
2443 #ifndef LIBJPEG_ENCAP_EXTERNAL
2444 static int
jpeg_start_decompress_encap(OJPEGState * sp,jpeg_decompress_struct * cinfo)2445 jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
2446 {
2447 if( SETJMP(sp->exit_jmpbuf) )
2448 return 0;
2449 else {
2450 jpeg_start_decompress(cinfo);
2451 return 1;
2452 }
2453 }
2454 #endif
2455
2456 #ifndef LIBJPEG_ENCAP_EXTERNAL
2457 static int
jpeg_read_scanlines_encap(OJPEGState * sp,jpeg_decompress_struct * cinfo,void * scanlines,uint32 max_lines)2458 jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines)
2459 {
2460 if( SETJMP(sp->exit_jmpbuf) )
2461 return 0;
2462 else {
2463 jpeg_read_scanlines(cinfo,scanlines,max_lines);
2464 return 1;
2465 }
2466 }
2467 #endif
2468
2469 #ifndef LIBJPEG_ENCAP_EXTERNAL
2470 static int
jpeg_read_raw_data_encap(OJPEGState * sp,jpeg_decompress_struct * cinfo,void * data,uint32 max_lines)2471 jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines)
2472 {
2473 if( SETJMP(sp->exit_jmpbuf) )
2474 return 0;
2475 else {
2476 jpeg_read_raw_data(cinfo,data,max_lines);
2477 return 1;
2478 }
2479 }
2480 #endif
2481
2482 #ifndef LIBJPEG_ENCAP_EXTERNAL
2483 static void
jpeg_encap_unwind(TIFF * tif)2484 jpeg_encap_unwind(TIFF* tif)
2485 {
2486 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2487 LONGJMP(sp->exit_jmpbuf,1);
2488 }
2489 #endif
2490
2491 static void
OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct * cinfo)2492 OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo)
2493 {
2494 char buffer[JMSG_LENGTH_MAX];
2495 (*cinfo->err->format_message)(cinfo,buffer);
2496 TIFFWarningExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg","%s",buffer);
2497 }
2498
2499 static void
OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct * cinfo)2500 OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo)
2501 {
2502 char buffer[JMSG_LENGTH_MAX];
2503 (*cinfo->err->format_message)(cinfo,buffer);
2504 TIFFErrorExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg","%s",buffer);
2505 jpeg_encap_unwind((TIFF*)(cinfo->client_data));
2506 }
2507
2508 static void
OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct * cinfo)2509 OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo)
2510 {
2511 (void)cinfo;
2512 }
2513
2514 static boolean
OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct * cinfo)2515 OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo)
2516 {
2517 TIFF* tif=(TIFF*)cinfo->client_data;
2518 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2519 void* mem=0;
2520 uint32 len=0U;
2521 if (OJPEGWriteStream(tif,&mem,&len)==0)
2522 {
2523 TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Premature end of JPEG data");
2524 jpeg_encap_unwind(tif);
2525 }
2526 sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=len;
2527 sp->libjpeg_jpeg_source_mgr.next_input_byte=mem;
2528 return(1);
2529 }
2530
2531 static void
OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct * cinfo,long num_bytes)2532 OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes)
2533 {
2534 TIFF* tif=(TIFF*)cinfo->client_data;
2535 (void)num_bytes;
2536 TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
2537 jpeg_encap_unwind(tif);
2538 }
2539
2540 #ifdef _MSC_VER
2541 #pragma warning( push )
2542 #pragma warning( disable : 4702 ) /* unreachable code */
2543 #endif
2544 static boolean
OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct * cinfo,int desired)2545 OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired)
2546 {
2547 TIFF* tif=(TIFF*)cinfo->client_data;
2548 (void)desired;
2549 TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
2550 jpeg_encap_unwind(tif);
2551 return(0);
2552 }
2553 #ifdef _MSC_VER
2554 #pragma warning( pop )
2555 #endif
2556
2557 static void
OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct * cinfo)2558 OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo)
2559 {
2560 (void)cinfo;
2561 }
2562
2563 #endif
2564
2565
2566 /*
2567 * Local Variables:
2568 * mode: c
2569 * c-basic-offset: 8
2570 * fill-column: 78
2571 * End:
2572 */
2573