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