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