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