2 /* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
3 specification is now totally obsolete and deprecated for new applications and
4 images. This file was was created solely in order to read unconverted images
5 still present on some users' computer systems. It will never be extended
6 to write such files. Writing new-style JPEG compressed TIFFs is implemented
9 The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
10 testfiles, and anticipate as much as possible all other... But still, it may
11 fail on some. If you encounter problems, please report them on the TIFF
12 mailing list and/or to Joris Van Damme <info@awaresystems.be>.
14 Please read the file called "TIFF Technical Note #2" if you need to be
15 convinced this compression scheme is bad and breaks TIFF. That document
16 is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
17 and from AWare Systems' TIFF section
18 <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
19 in Adobe's specification supplements, marked "draft" up to this day, but
20 supported by the TIFF community.
22 This file interfaces with Release 6B of the JPEG Library written by the
23 Independent JPEG Group. Previous versions of this file required a hack inside
24 the LibJpeg library. This version no longer requires that. Remember to
25 remove the hack if you update from the old version.
27 Copyright (c) Joris Van Damme <info@awaresystems.be>
28 Copyright (c) AWare Systems <http://www.awaresystems.be/>
30 The licence agreement for this file is the same as the rest of the LibTiff
33 IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
34 ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
35 OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
36 WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
37 LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
40 Joris Van Damme and/or AWare Systems may be available for custom
41 developement. If you like what you see, and need anything similar or related,
42 contact <info@awaresystems.be>.
45 /* What is what, and what is not?
47 This decoder starts with an input stream, that is essentially the JpegInterchangeFormat
48 stream, if any, followed by the strile data, if any. This stream is read in
49 OJPEGReadByte and related functions.
51 It analyzes the start of this stream, until it encounters non-marker data, i.e.
52 compressed image data. Some of the header markers it sees have no actual content,
53 like the SOI marker, and APP/COM markers that really shouldn't even be there. Some
54 other markers do have content, and the valuable bits and pieces of information
55 in these markers are saved, checking all to verify that the stream is more or
56 less within expected bounds. This happens inside the OJPEGReadHeaderInfoSecStreamXxx
59 Some OJPEG imagery contains no valid JPEG header markers. This situation is picked
60 up on if we've seen no SOF marker when we're at the start of the compressed image
61 data. In this case, the tables are read from JpegXxxTables tags, and the other
62 bits and pieces of information is initialized to its most basic value. This is
63 implemented in the OJPEGReadHeaderInfoSecTablesXxx functions.
65 When this is complete, a good and valid JPEG header can be assembled, and this is
66 passed through to LibJpeg. When that's done, the remainder of the input stream, i.e.
67 the compressed image data, can be passed through unchanged. This is done in
68 OJPEGWriteStream functions.
70 LibTiff rightly expects to know the subsampling values before decompression. Just like
71 in new-style JPEG-in-TIFF, though, or even more so, actually, the YCbCrsubsampling
72 tag is notoriously unreliable. To correct these tag values with the ones inside
73 the JPEG stream, the first part of the input stream is pre-scanned in
74 OJPEGSubsamplingCorrect, making no note of any other data, reporting no warnings
75 or errors, up to the point where either these values are read, or it's clear they
76 aren't there. This means that some of the data is read twice, but we feel speed
77 in correcting these values is important enough to warrant this sacrifice. Allthough
78 there is currently no define or other configuration mechanism to disable this behaviour,
79 the actual header scanning is build to robustly respond with error report if it
80 should encounter an uncorrected mismatch of subsampling values. See
81 OJPEGReadHeaderInfoSecStreamSof.
83 The restart interval and restart markers are the most tricky part... The restart
84 interval can be specified in a tag. It can also be set inside the input JPEG stream.
85 It can be used inside the input JPEG stream. If reading from strile data, we've
86 consistenly discovered the need to insert restart markers in between the different
87 striles, as is also probably the most likely interpretation of the original TIFF 6.0
88 specification. With all this setting of interval, and actual use of markers that is not
89 predictable at the time of valid JPEG header assembly, the restart thing may turn
90 out the Achilles heel of this implementation. Fortunately, most OJPEG writer vendors
91 succeed in reading back what they write, which may be the reason why we've been able
92 to discover ways that seem to work.
94 Some special provision is made for planarconfig separate OJPEG files. These seem
95 to consistently contain header info, a SOS marker, a plane, SOS marker, plane, SOS,
96 and plane. This may or may not be a valid JPEG configuration, we don't know and don't
97 care. We want LibTiff to be able to access the planes individually, without huge
98 buffering inside LibJpeg, anyway. So we compose headers to feed to LibJpeg, in this
99 case, that allow us to pass a single plane such that LibJpeg sees a valid
100 single-channel JPEG stream. Locating subsequent SOS markers, and thus subsequent
101 planes, is done inside OJPEGReadSecondarySos.
103 The benefit of the scheme is... that it works, basically. We know of no other that
104 does. It works without checking software tag, or otherwise going about things in an
105 OJPEG flavor specific manner. Instead, it is a single scheme, that covers the cases
106 with and without JpegInterchangeFormat, with and without striles, with part of
107 the header in JpegInterchangeFormat and remainder in first strile, etc. It is forgiving
108 and robust, may likely work with OJPEG flavors we've not seen yet, and makes most out
111 Another nice side-effect is that a complete JPEG single valid stream is build if
112 planarconfig is not separate (vast majority). We may one day use that to build
113 converters to JPEG, and/or to new-style JPEG compression inside TIFF.
115 A dissadvantage is the lack of random access to the individual striles. This is the
116 reason for much of the complicated restart-and-position stuff inside OJPEGPreDecode.
117 Applications would do well accessing all striles in order, as this will result in
118 a single sequential scan of the input stream, and no restarting of LibJpeg decoding
122 #define WIN32_LEAN_AND_MEAN
128 /* Configuration defines here are:
129 * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some environments,
130 * like eg LibTiffDelphi, this is not possible. For this reason, the actual calls to
131 * libjpeg, with longjump stuff, are encapsulated in dedicated functions. When
132 * JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declared external
133 * to this unit, and can be defined elsewhere to use stuff other then longjump.
134 * The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encapsulators
135 * here, internally, with normal longjump.
136 * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
137 * conviniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
138 * in place of plain setjmp. These macros will make it easier. It is useless
139 * to fiddle with these if you define JPEG_ENCAP_EXTERNAL.
140 * OJPEG_BUFFER: Define the size of the desired buffer here. Should be small enough so as to guarantee
141 * instant processing, optimal streaming and optimal use of processor cache, but also big
142 * enough so as to not result in significant call overhead. It should be at least a few
143 * bytes to accomodate some structures (this is verified in asserts), but it would not be
144 * sensible to make it this small anyway, and it should be at most 64K since it is indexed
145 * with uint16. We recommend 2K.
146 * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used anywhere and has
147 * absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
150 /* define LIBJPEG_ENCAP_EXTERNAL */
151 #define SETJMP(jbuf) setjmp(jbuf)
152 #define LONGJMP(jbuf,code) longjmp(jbuf,code)
153 #define JMP_BUF jmp_buf
154 #define OJPEG_BUFFER 2048
155 /* define EGYPTIANWALK */
157 #define JPEG_MARKER_SOF0 0xC0
158 #define JPEG_MARKER_SOF1 0xC1
159 #define JPEG_MARKER_SOF3 0xC3
160 #define JPEG_MARKER_DHT 0xC4
161 #define JPEG_MARKER_RST0 0XD0
162 #define JPEG_MARKER_SOI 0xD8
163 #define JPEG_MARKER_EOI 0xD9
164 #define JPEG_MARKER_SOS 0xDA
165 #define JPEG_MARKER_DQT 0xDB
166 #define JPEG_MARKER_DRI 0xDD
167 #define JPEG_MARKER_APP0 0xE0
168 #define JPEG_MARKER_COM 0xFE
170 #define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
171 #define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
172 #define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
173 #define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
174 #define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
175 #define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
176 #define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
178 static const TIFFField ojpegFields
[] = {
179 {TIFFTAG_JPEGIFOFFSET
,1,1,TIFF_LONG8
,0,TIFF_SETGET_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGINTERCHANGEFORMAT
,TRUE
,FALSE
,"JpegInterchangeFormat",NULL
},
180 {TIFFTAG_JPEGIFBYTECOUNT
,1,1,TIFF_LONG8
,0,TIFF_SETGET_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH
,TRUE
,FALSE
,"JpegInterchangeFormatLength",NULL
},
181 {TIFFTAG_JPEGQTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGQTABLES
,FALSE
,TRUE
,"JpegQTables",NULL
},
182 {TIFFTAG_JPEGDCTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGDCTABLES
,FALSE
,TRUE
,"JpegDcTables",NULL
},
183 {TIFFTAG_JPEGACTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGACTABLES
,FALSE
,TRUE
,"JpegAcTables",NULL
},
184 {TIFFTAG_JPEGPROC
,1,1,TIFF_SHORT
,0,TIFF_SETGET_UINT16
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGPROC
,FALSE
,FALSE
,"JpegProc",NULL
},
185 {TIFFTAG_JPEGRESTARTINTERVAL
,1,1,TIFF_SHORT
,0,TIFF_SETGET_UINT16
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGRESTARTINTERVAL
,FALSE
,FALSE
,"JpegRestartInterval",NULL
},
188 #ifndef LIBJPEG_ENCAP_EXTERNAL
192 /* We undefine FAR to avoid conflict with JPEG definition */
199 Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
200 not defined. Unfortunately, the MinGW and Borland compilers include
201 a typedef for INT32, which causes a conflict. MSVC does not include
202 a conficting typedef given the headers which are included.
204 #if defined(__BORLANDC__) || defined(__MINGW32__)
208 /* Define "boolean" as unsigned char, not int, per Windows custom. */
209 #if defined(__WIN32__) && !defined(__MINGW32__)
210 # ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
211 typedef unsigned char boolean
;
213 # define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
219 #ifndef HAVE_WXJPEG_BOOLEAN
220 typedef boolean wxjpeg_boolean
;
223 typedef struct jpeg_error_mgr jpeg_error_mgr
;
224 typedef struct jpeg_common_struct jpeg_common_struct
;
225 typedef struct jpeg_decompress_struct jpeg_decompress_struct
;
226 typedef struct jpeg_source_mgr jpeg_source_mgr
;
230 osibsJpegInterchangeFormat
,
233 } OJPEGStateInBufferSource
;
237 ososQTable0
,ososQTable1
,ososQTable2
,ososQTable3
,
238 ososDcTable0
,ososDcTable1
,ososDcTable2
,ososDcTable3
,
239 ososAcTable0
,ososAcTable1
,ososAcTable2
,ososAcTable3
,
246 } OJPEGStateOutState
;
250 #ifndef LIBJPEG_ENCAP_EXTERNAL
253 TIFFVGetMethod vgetparent
;
254 TIFFVSetMethod vsetparent
;
255 TIFFPrintMethod printdir
;
260 uint32 strile_length
;
261 uint32 strile_length_total
;
262 uint8 samples_per_pixel
;
263 uint8 plane_sample_offset
;
264 uint8 samples_per_pixel_per_plane
;
265 uint64 jpeg_interchange_format
;
266 uint64 jpeg_interchange_format_length
;
268 uint8 subsamplingcorrect
;
269 uint8 subsamplingcorrect_done
;
270 uint8 subsampling_tag
;
271 uint8 subsampling_hor
;
272 uint8 subsampling_ver
;
273 uint8 subsampling_force_desubsampling_inside_decompression
;
274 uint8 qtable_offset_count
;
275 uint8 dctable_offset_count
;
276 uint8 actable_offset_count
;
277 uint64 qtable_offset
[3];
278 uint64 dctable_offset
[3];
279 uint64 actable_offset
[3];
283 uint16 restart_interval
;
296 OJPEGStateInBufferSource in_buffer_source
;
297 uint32 in_buffer_next_strile
;
298 uint64 in_buffer_file_pos
;
299 uint64 in_buffer_file_togo
;
301 uint8 readheader_done
;
302 uint8 writeheader_done
;
303 uint16 write_cursample
;
304 uint32 write_curstrile
;
305 uint8 libjpeg_session_active
;
306 uint8 libjpeg_jpeg_query_style
;
307 jpeg_error_mgr libjpeg_jpeg_error_mgr
;
308 jpeg_decompress_struct libjpeg_jpeg_decompress_struct
;
309 jpeg_source_mgr libjpeg_jpeg_source_mgr
;
310 uint8 subsampling_convert_log
;
311 uint32 subsampling_convert_ylinelen
;
312 uint32 subsampling_convert_ylines
;
313 uint32 subsampling_convert_clinelen
;
314 uint32 subsampling_convert_clines
;
315 uint32 subsampling_convert_ybuflen
;
316 uint32 subsampling_convert_cbuflen
;
317 uint32 subsampling_convert_ycbcrbuflen
;
318 uint8
* subsampling_convert_ycbcrbuf
;
319 uint8
* subsampling_convert_ybuf
;
320 uint8
* subsampling_convert_cbbuf
;
321 uint8
* subsampling_convert_crbuf
;
322 uint32 subsampling_convert_ycbcrimagelen
;
323 uint8
** subsampling_convert_ycbcrimage
;
324 uint32 subsampling_convert_clinelenout
;
325 uint32 subsampling_convert_state
;
326 uint32 bytes_per_line
; /* if the codec outputs subsampled data, a 'line' in bytes_per_line */
327 uint32 lines_per_strile
; /* and lines_per_strile means subsampling_ver desubsampled rows */
328 OJPEGStateInBufferSource in_buffer_source
;
329 uint32 in_buffer_next_strile
;
330 uint32 in_buffer_strile_count
;
331 uint64 in_buffer_file_pos
;
332 uint8 in_buffer_file_pos_log
;
333 uint64 in_buffer_file_togo
;
334 uint16 in_buffer_togo
;
335 uint8
* in_buffer_cur
;
336 uint8 in_buffer
[OJPEG_BUFFER
];
337 OJPEGStateOutState out_state
;
338 uint8 out_buffer
[OJPEG_BUFFER
];
342 static int OJPEGVGetField(TIFF
* tif
, uint32 tag
, va_list ap
);
343 static int OJPEGVSetField(TIFF
* tif
, uint32 tag
, va_list ap
);
344 static void OJPEGPrintDir(TIFF
* tif
, FILE* fd
, long flags
);
346 static int OJPEGFixupTags(TIFF
* tif
);
347 static int OJPEGSetupDecode(TIFF
* tif
);
348 static int OJPEGPreDecode(TIFF
* tif
, uint16 s
);
349 static int OJPEGPreDecodeSkipRaw(TIFF
* tif
);
350 static int OJPEGPreDecodeSkipScanlines(TIFF
* tif
);
351 static int OJPEGDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
);
352 static int OJPEGDecodeRaw(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
353 static int OJPEGDecodeScanlines(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
354 static void OJPEGPostDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
355 static int OJPEGSetupEncode(TIFF
* tif
);
356 static int OJPEGPreEncode(TIFF
* tif
, uint16 s
);
357 static int OJPEGEncode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
);
358 static int OJPEGPostEncode(TIFF
* tif
);
359 static void OJPEGCleanup(TIFF
* tif
);
361 static void OJPEGSubsamplingCorrect(TIFF
* tif
);
362 static int OJPEGReadHeaderInfo(TIFF
* tif
);
363 static int OJPEGReadSecondarySos(TIFF
* tif
, uint16 s
);
364 static int OJPEGWriteHeaderInfo(TIFF
* tif
);
365 static void OJPEGLibjpegSessionAbort(TIFF
* tif
);
367 static int OJPEGReadHeaderInfoSec(TIFF
* tif
);
368 static int OJPEGReadHeaderInfoSecStreamDri(TIFF
* tif
);
369 static int OJPEGReadHeaderInfoSecStreamDqt(TIFF
* tif
);
370 static int OJPEGReadHeaderInfoSecStreamDht(TIFF
* tif
);
371 static int OJPEGReadHeaderInfoSecStreamSof(TIFF
* tif
, uint8 marker_id
);
372 static int OJPEGReadHeaderInfoSecStreamSos(TIFF
* tif
);
373 static int OJPEGReadHeaderInfoSecTablesQTable(TIFF
* tif
);
374 static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF
* tif
);
375 static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF
* tif
);
377 static int OJPEGReadBufferFill(OJPEGState
* sp
);
378 static int OJPEGReadByte(OJPEGState
* sp
, uint8
* byte
);
379 static int OJPEGReadBytePeek(OJPEGState
* sp
, uint8
* byte
);
380 static void OJPEGReadByteAdvance(OJPEGState
* sp
);
381 static int OJPEGReadWord(OJPEGState
* sp
, uint16
* word
);
382 static int OJPEGReadBlock(OJPEGState
* sp
, uint16 len
, void* mem
);
383 static void OJPEGReadSkip(OJPEGState
* sp
, uint16 len
);
385 static int OJPEGWriteStream(TIFF
* tif
, void** mem
, uint32
* len
);
386 static void OJPEGWriteStreamSoi(TIFF
* tif
, void** mem
, uint32
* len
);
387 static void OJPEGWriteStreamQTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
388 static void OJPEGWriteStreamDcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
389 static void OJPEGWriteStreamAcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
390 static void OJPEGWriteStreamDri(TIFF
* tif
, void** mem
, uint32
* len
);
391 static void OJPEGWriteStreamSof(TIFF
* tif
, void** mem
, uint32
* len
);
392 static void OJPEGWriteStreamSos(TIFF
* tif
, void** mem
, uint32
* len
);
393 static int OJPEGWriteStreamCompressed(TIFF
* tif
, void** mem
, uint32
* len
);
394 static void OJPEGWriteStreamRst(TIFF
* tif
, void** mem
, uint32
* len
);
395 static void OJPEGWriteStreamEoi(TIFF
* tif
, void** mem
, uint32
* len
);
397 #ifdef LIBJPEG_ENCAP_EXTERNAL
398 extern int jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
399 extern int jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
);
400 extern int jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
401 extern int jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
);
402 extern int jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
);
403 extern void jpeg_encap_unwind(TIFF
* tif
);
405 static int jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* j
);
406 static int jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
);
407 static int jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
408 static int jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
);
409 static int jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
);
410 static void jpeg_encap_unwind(TIFF
* tif
);
413 static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct
* cinfo
);
414 static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct
* cinfo
);
415 static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct
* cinfo
);
416 static wxjpeg_boolean
OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct
* cinfo
);
417 static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct
* cinfo
, long num_bytes
);
418 static wxjpeg_boolean
OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct
* cinfo
, int desired
);
419 static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct
* cinfo
);
422 TIFFInitOJPEG(TIFF
* tif
, int scheme
)
424 static const char module[]="TIFFInitOJPEG";
427 assert(scheme
==COMPRESSION_OJPEG
);
430 * Merge codec-specific tag information.
432 if (!_TIFFMergeFields(tif
, ojpegFields
, TIFFArrayCount(ojpegFields
))) {
433 TIFFErrorExt(tif
->tif_clientdata
, module,
434 "Merging Old JPEG codec-specific tags failed");
439 sp
=_TIFFmalloc(sizeof(OJPEGState
));
442 TIFFErrorExt(tif
->tif_clientdata
,module,"No space for OJPEG state block");
445 _TIFFmemset(sp
,0,sizeof(OJPEGState
));
448 sp
->subsampling_hor
=2;
449 sp
->subsampling_ver
=2;
450 TIFFSetField(tif
,TIFFTAG_YCBCRSUBSAMPLING
,2,2);
451 /* tif codec methods */
452 tif
->tif_fixuptags
=OJPEGFixupTags
;
453 tif
->tif_setupdecode
=OJPEGSetupDecode
;
454 tif
->tif_predecode
=OJPEGPreDecode
;
455 tif
->tif_postdecode
=OJPEGPostDecode
;
456 tif
->tif_decoderow
=OJPEGDecode
;
457 tif
->tif_decodestrip
=OJPEGDecode
;
458 tif
->tif_decodetile
=OJPEGDecode
;
459 tif
->tif_setupencode
=OJPEGSetupEncode
;
460 tif
->tif_preencode
=OJPEGPreEncode
;
461 tif
->tif_postencode
=OJPEGPostEncode
;
462 tif
->tif_encoderow
=OJPEGEncode
;
463 tif
->tif_encodestrip
=OJPEGEncode
;
464 tif
->tif_encodetile
=OJPEGEncode
;
465 tif
->tif_cleanup
=OJPEGCleanup
;
466 tif
->tif_data
=(uint8
*)sp
;
467 /* tif tag methods */
468 sp
->vgetparent
=tif
->tif_tagmethods
.vgetfield
;
469 tif
->tif_tagmethods
.vgetfield
=OJPEGVGetField
;
470 sp
->vsetparent
=tif
->tif_tagmethods
.vsetfield
;
471 tif
->tif_tagmethods
.vsetfield
=OJPEGVSetField
;
472 sp
->printdir
=tif
->tif_tagmethods
.printdir
;
473 tif
->tif_tagmethods
.printdir
=OJPEGPrintDir
;
474 /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
475 Some others do, but have totally meaningless or corrupt values
476 in these tags. In these cases, the JpegInterchangeFormat stream is
477 reliable. In any case, this decoder reads the compressed data itself,
478 from the most reliable locations, and we need to notify encapsulating
479 LibTiff not to read raw strips or tiles for us. */
480 tif
->tif_flags
|=TIFF_NOREADRAW
;
485 OJPEGVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
487 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
490 case TIFFTAG_JPEGIFOFFSET
:
491 *va_arg(ap
,uint64
*)=(uint64
)sp
->jpeg_interchange_format
;
493 case TIFFTAG_JPEGIFBYTECOUNT
:
494 *va_arg(ap
,uint64
*)=(uint64
)sp
->jpeg_interchange_format_length
;
496 case TIFFTAG_YCBCRSUBSAMPLING
:
497 if (sp
->subsamplingcorrect_done
==0)
498 OJPEGSubsamplingCorrect(tif
);
499 *va_arg(ap
,uint16
*)=(uint16
)sp
->subsampling_hor
;
500 *va_arg(ap
,uint16
*)=(uint16
)sp
->subsampling_ver
;
502 case TIFFTAG_JPEGQTABLES
:
503 *va_arg(ap
,uint32
*)=(uint32
)sp
->qtable_offset_count
;
504 *va_arg(ap
,void**)=(void*)sp
->qtable_offset
;
506 case TIFFTAG_JPEGDCTABLES
:
507 *va_arg(ap
,uint32
*)=(uint32
)sp
->dctable_offset_count
;
508 *va_arg(ap
,void**)=(void*)sp
->dctable_offset
;
510 case TIFFTAG_JPEGACTABLES
:
511 *va_arg(ap
,uint32
*)=(uint32
)sp
->actable_offset_count
;
512 *va_arg(ap
,void**)=(void*)sp
->actable_offset
;
514 case TIFFTAG_JPEGPROC
:
515 *va_arg(ap
,uint16
*)=(uint16
)sp
->jpeg_proc
;
517 case TIFFTAG_JPEGRESTARTINTERVAL
:
518 *va_arg(ap
,uint16
*)=sp
->restart_interval
;
521 return (*sp
->vgetparent
)(tif
,tag
,ap
);
527 OJPEGVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
529 static const char module[]="OJPEGVSetField";
530 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
536 case TIFFTAG_JPEGIFOFFSET
:
537 sp
->jpeg_interchange_format
=(uint64
)va_arg(ap
,uint64
);
539 case TIFFTAG_JPEGIFBYTECOUNT
:
540 sp
->jpeg_interchange_format_length
=(uint64
)va_arg(ap
,uint64
);
542 case TIFFTAG_YCBCRSUBSAMPLING
:
543 sp
->subsampling_tag
=1;
544 sp
->subsampling_hor
=(uint8
)va_arg(ap
,uint16_vap
);
545 sp
->subsampling_ver
=(uint8
)va_arg(ap
,uint16_vap
);
546 tif
->tif_dir
.td_ycbcrsubsampling
[0]=sp
->subsampling_hor
;
547 tif
->tif_dir
.td_ycbcrsubsampling
[1]=sp
->subsampling_ver
;
549 case TIFFTAG_JPEGQTABLES
:
550 ma
=(uint32
)va_arg(ap
,uint32
);
555 TIFFErrorExt(tif
->tif_clientdata
,module,"JpegQTables tag has incorrect count");
558 sp
->qtable_offset_count
=(uint8
)ma
;
559 mb
=(uint64
*)va_arg(ap
,uint64
*);
561 sp
->qtable_offset
[n
]=mb
[n
];
564 case TIFFTAG_JPEGDCTABLES
:
565 ma
=(uint32
)va_arg(ap
,uint32
);
570 TIFFErrorExt(tif
->tif_clientdata
,module,"JpegDcTables tag has incorrect count");
573 sp
->dctable_offset_count
=(uint8
)ma
;
574 mb
=(uint64
*)va_arg(ap
,uint64
*);
576 sp
->dctable_offset
[n
]=mb
[n
];
579 case TIFFTAG_JPEGACTABLES
:
580 ma
=(uint32
)va_arg(ap
,uint32
);
585 TIFFErrorExt(tif
->tif_clientdata
,module,"JpegAcTables tag has incorrect count");
588 sp
->actable_offset_count
=(uint8
)ma
;
589 mb
=(uint64
*)va_arg(ap
,uint64
*);
591 sp
->actable_offset
[n
]=mb
[n
];
594 case TIFFTAG_JPEGPROC
:
595 sp
->jpeg_proc
=(uint8
)va_arg(ap
,uint16_vap
);
597 case TIFFTAG_JPEGRESTARTINTERVAL
:
598 sp
->restart_interval
=(uint16
)va_arg(ap
,uint16_vap
);
601 return (*sp
->vsetparent
)(tif
,tag
,ap
);
603 TIFFSetFieldBit(tif
,TIFFFieldWithTag(tif
,tag
)->field_bit
);
604 tif
->tif_flags
|=TIFF_DIRTYDIRECT
;
609 OJPEGPrintDir(TIFF
* tif
, FILE* fd
, long flags
)
611 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
615 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGINTERCHANGEFORMAT
))
616 fprintf(fd
," JpegInterchangeFormat: " TIFF_UINT64_FORMAT
"\n",(TIFF_UINT64_T
)sp
->jpeg_interchange_format
);
617 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH
))
618 fprintf(fd
," JpegInterchangeFormatLength: " TIFF_UINT64_FORMAT
"\n",(TIFF_UINT64_T
)sp
->jpeg_interchange_format_length
);
619 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGQTABLES
))
621 fprintf(fd
," JpegQTables:");
622 for (m
=0; m
<sp
->qtable_offset_count
; m
++)
623 fprintf(fd
," " TIFF_UINT64_FORMAT
,(TIFF_UINT64_T
)sp
->qtable_offset
[m
]);
626 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGDCTABLES
))
628 fprintf(fd
," JpegDcTables:");
629 for (m
=0; m
<sp
->dctable_offset_count
; m
++)
630 fprintf(fd
," " TIFF_UINT64_FORMAT
,(TIFF_UINT64_T
)sp
->dctable_offset
[m
]);
633 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGACTABLES
))
635 fprintf(fd
," JpegAcTables:");
636 for (m
=0; m
<sp
->actable_offset_count
; m
++)
637 fprintf(fd
," " TIFF_UINT64_FORMAT
,(TIFF_UINT64_T
)sp
->actable_offset
[m
]);
640 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGPROC
))
641 fprintf(fd
," JpegProc: %u\n",(unsigned int)sp
->jpeg_proc
);
642 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGRESTARTINTERVAL
))
643 fprintf(fd
," JpegRestartInterval: %u\n",(unsigned int)sp
->restart_interval
);
645 (*sp
->printdir
)(tif
, fd
, flags
);
649 OJPEGFixupTags(TIFF
* tif
)
656 OJPEGSetupDecode(TIFF
* tif
)
658 static const char module[]="OJPEGSetupDecode";
659 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");
664 OJPEGPreDecode(TIFF
* tif
, uint16 s
)
666 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
668 if (sp
->subsamplingcorrect_done
==0)
669 OJPEGSubsamplingCorrect(tif
);
670 if (sp
->readheader_done
==0)
672 if (OJPEGReadHeaderInfo(tif
)==0)
675 if (sp
->sos_end
[s
].log
==0)
677 if (OJPEGReadSecondarySos(tif
,s
)==0)
684 if ((sp
->writeheader_done
!=0) && ((sp
->write_cursample
!=s
) || (sp
->write_curstrile
>m
)))
686 if (sp
->libjpeg_session_active
!=0)
687 OJPEGLibjpegSessionAbort(tif
);
688 sp
->writeheader_done
=0;
690 if (sp
->writeheader_done
==0)
692 sp
->plane_sample_offset
=(uint8
)s
;
693 sp
->write_cursample
=s
;
694 sp
->write_curstrile
=s
*tif
->tif_dir
.td_stripsperimage
;
695 if ((sp
->in_buffer_file_pos_log
==0) ||
696 (sp
->in_buffer_file_pos
-sp
->in_buffer_togo
!=sp
->sos_end
[s
].in_buffer_file_pos
))
698 sp
->in_buffer_source
=sp
->sos_end
[s
].in_buffer_source
;
699 sp
->in_buffer_next_strile
=sp
->sos_end
[s
].in_buffer_next_strile
;
700 sp
->in_buffer_file_pos
=sp
->sos_end
[s
].in_buffer_file_pos
;
701 sp
->in_buffer_file_pos_log
=0;
702 sp
->in_buffer_file_togo
=sp
->sos_end
[s
].in_buffer_file_togo
;
703 sp
->in_buffer_togo
=0;
706 if (OJPEGWriteHeaderInfo(tif
)==0)
709 while (sp
->write_curstrile
<m
)
711 if (sp
->libjpeg_jpeg_query_style
==0)
713 if (OJPEGPreDecodeSkipRaw(tif
)==0)
718 if (OJPEGPreDecodeSkipScanlines(tif
)==0)
721 sp
->write_curstrile
++;
727 OJPEGPreDecodeSkipRaw(TIFF
* tif
)
729 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
731 m
=sp
->lines_per_strile
;
732 if (sp
->subsampling_convert_state
!=0)
734 if (sp
->subsampling_convert_clines
-sp
->subsampling_convert_state
>=m
)
736 sp
->subsampling_convert_state
+=m
;
737 if (sp
->subsampling_convert_state
==sp
->subsampling_convert_clines
)
738 sp
->subsampling_convert_state
=0;
741 m
-=sp
->subsampling_convert_clines
-sp
->subsampling_convert_state
;
742 sp
->subsampling_convert_state
=0;
744 while (m
>=sp
->subsampling_convert_clines
)
746 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
748 m
-=sp
->subsampling_convert_clines
;
752 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
754 sp
->subsampling_convert_state
=m
;
760 OJPEGPreDecodeSkipScanlines(TIFF
* tif
)
762 static const char module[]="OJPEGPreDecodeSkipScanlines";
763 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
765 if (sp
->skip_buffer
==NULL
)
767 sp
->skip_buffer
=_TIFFmalloc(sp
->bytes_per_line
);
768 if (sp
->skip_buffer
==NULL
)
770 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
774 for (m
=0; m
<sp
->lines_per_strile
; m
++)
776 if (jpeg_read_scanlines_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),&sp
->skip_buffer
,1)==0)
783 OJPEGDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
)
785 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
787 if (sp
->libjpeg_jpeg_query_style
==0)
789 if (OJPEGDecodeRaw(tif
,buf
,cc
)==0)
794 if (OJPEGDecodeScanlines(tif
,buf
,cc
)==0)
801 OJPEGDecodeRaw(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
803 static const char module[]="OJPEGDecodeRaw";
804 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
814 if (cc%sp
->bytes_per_line
!=0)
816 TIFFErrorExt(tif
->tif_clientdata
,module,"Fractional scanline not read");
824 if (sp
->subsampling_convert_state
==0)
826 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
829 oy
=sp
->subsampling_convert_ybuf
+sp
->subsampling_convert_state
*sp
->subsampling_ver
*sp
->subsampling_convert_ylinelen
;
830 ocb
=sp
->subsampling_convert_cbbuf
+sp
->subsampling_convert_state
*sp
->subsampling_convert_clinelen
;
831 ocr
=sp
->subsampling_convert_crbuf
+sp
->subsampling_convert_state
*sp
->subsampling_convert_clinelen
;
833 for (q
=0; q
<sp
->subsampling_convert_clinelenout
; q
++)
836 for (sy
=0; sy
<sp
->subsampling_ver
; sy
++)
838 for (sx
=0; sx
<sp
->subsampling_hor
; sx
++)
840 r
+=sp
->subsampling_convert_ylinelen
-sp
->subsampling_hor
;
842 oy
+=sp
->subsampling_hor
;
846 sp
->subsampling_convert_state
++;
847 if (sp
->subsampling_convert_state
==sp
->subsampling_convert_clines
)
848 sp
->subsampling_convert_state
=0;
849 m
+=sp
->bytes_per_line
;
850 n
-=sp
->bytes_per_line
;
856 OJPEGDecodeScanlines(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
858 static const char module[]="OJPEGDecodeScanlines";
859 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
862 if (cc%sp
->bytes_per_line
!=0)
864 TIFFErrorExt(tif
->tif_clientdata
,module,"Fractional scanline not read");
872 if (jpeg_read_scanlines_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),&m
,1)==0)
874 m
+=sp
->bytes_per_line
;
875 n
-=sp
->bytes_per_line
;
881 OJPEGPostDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
883 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
886 sp
->write_curstrile
++;
887 if (sp
->write_curstrile%tif
->tif_dir
.td_stripsperimage
==0)
889 assert(sp
->libjpeg_session_active
!=0);
890 OJPEGLibjpegSessionAbort(tif
);
891 sp
->writeheader_done
=0;
896 OJPEGSetupEncode(TIFF
* tif
)
898 static const char module[]="OJPEGSetupEncode";
899 TIFFErrorExt(tif
->tif_clientdata
,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
904 OJPEGPreEncode(TIFF
* tif
, uint16 s
)
906 static const char module[]="OJPEGPreEncode";
908 TIFFErrorExt(tif
->tif_clientdata
,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
913 OJPEGEncode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
)
915 static const char module[]="OJPEGEncode";
919 TIFFErrorExt(tif
->tif_clientdata
,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
924 OJPEGPostEncode(TIFF
* tif
)
926 static const char module[]="OJPEGPostEncode";
927 TIFFErrorExt(tif
->tif_clientdata
,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
932 OJPEGCleanup(TIFF
* tif
)
934 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
937 tif
->tif_tagmethods
.vgetfield
=sp
->vgetparent
;
938 tif
->tif_tagmethods
.vsetfield
=sp
->vsetparent
;
939 tif
->tif_tagmethods
.printdir
=sp
->printdir
;
940 if (sp
->qtable
[0]!=0)
941 _TIFFfree(sp
->qtable
[0]);
942 if (sp
->qtable
[1]!=0)
943 _TIFFfree(sp
->qtable
[1]);
944 if (sp
->qtable
[2]!=0)
945 _TIFFfree(sp
->qtable
[2]);
946 if (sp
->qtable
[3]!=0)
947 _TIFFfree(sp
->qtable
[3]);
948 if (sp
->dctable
[0]!=0)
949 _TIFFfree(sp
->dctable
[0]);
950 if (sp
->dctable
[1]!=0)
951 _TIFFfree(sp
->dctable
[1]);
952 if (sp
->dctable
[2]!=0)
953 _TIFFfree(sp
->dctable
[2]);
954 if (sp
->dctable
[3]!=0)
955 _TIFFfree(sp
->dctable
[3]);
956 if (sp
->actable
[0]!=0)
957 _TIFFfree(sp
->actable
[0]);
958 if (sp
->actable
[1]!=0)
959 _TIFFfree(sp
->actable
[1]);
960 if (sp
->actable
[2]!=0)
961 _TIFFfree(sp
->actable
[2]);
962 if (sp
->actable
[3]!=0)
963 _TIFFfree(sp
->actable
[3]);
964 if (sp
->libjpeg_session_active
!=0)
965 OJPEGLibjpegSessionAbort(tif
);
966 if (sp
->subsampling_convert_ycbcrbuf
!=0)
967 _TIFFfree(sp
->subsampling_convert_ycbcrbuf
);
968 if (sp
->subsampling_convert_ycbcrimage
!=0)
969 _TIFFfree(sp
->subsampling_convert_ycbcrimage
);
970 if (sp
->skip_buffer
!=0)
971 _TIFFfree(sp
->skip_buffer
);
974 _TIFFSetDefaultCompressionState(tif
);
979 OJPEGSubsamplingCorrect(TIFF
* tif
)
981 static const char module[]="OJPEGSubsamplingCorrect";
982 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
985 _TIFFFillStriles( tif
);
987 assert(sp
->subsamplingcorrect_done
==0);
988 if ((tif
->tif_dir
.td_samplesperpixel
!=3) || ((tif
->tif_dir
.td_photometric
!=PHOTOMETRIC_YCBCR
) &&
989 (tif
->tif_dir
.td_photometric
!=PHOTOMETRIC_ITULAB
)))
991 if (sp
->subsampling_tag
!=0)
992 TIFFWarningExt(tif
->tif_clientdata
,module,"Subsampling tag not appropriate for this Photometric and/or SamplesPerPixel");
993 sp
->subsampling_hor
=1;
994 sp
->subsampling_ver
=1;
995 sp
->subsampling_force_desubsampling_inside_decompression
=0;
999 sp
->subsamplingcorrect_done
=1;
1000 mh
=sp
->subsampling_hor
;
1001 mv
=sp
->subsampling_ver
;
1002 sp
->subsamplingcorrect
=1;
1003 OJPEGReadHeaderInfoSec(tif
);
1004 if (sp
->subsampling_force_desubsampling_inside_decompression
!=0)
1006 sp
->subsampling_hor
=1;
1007 sp
->subsampling_ver
=1;
1009 sp
->subsamplingcorrect
=0;
1010 if (((sp
->subsampling_hor
!=mh
) || (sp
->subsampling_ver
!=mv
)) && (sp
->subsampling_force_desubsampling_inside_decompression
==0))
1012 if (sp
->subsampling_tag
==0)
1013 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 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 if (sp
->subsampling_force_desubsampling_inside_decompression
!=0)
1019 if (sp
->subsampling_tag
==0)
1020 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 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 if (sp
->subsampling_force_desubsampling_inside_decompression
==0)
1026 if (sp
->subsampling_hor
<sp
->subsampling_ver
)
1027 TIFFWarningExt(tif
->tif_clientdata
,module,"Subsampling values [%d,%d] are not allowed in TIFF",sp
->subsampling_hor
,sp
->subsampling_ver
);
1030 sp
->subsamplingcorrect_done
=1;
1034 OJPEGReadHeaderInfo(TIFF
* tif
)
1036 static const char module[]="OJPEGReadHeaderInfo";
1037 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1038 assert(sp
->readheader_done
==0);
1039 sp
->image_width
=tif
->tif_dir
.td_imagewidth
;
1040 sp
->image_length
=tif
->tif_dir
.td_imagelength
;
1043 sp
->strile_width
=tif
->tif_dir
.td_tilewidth
;
1044 sp
->strile_length
=tif
->tif_dir
.td_tilelength
;
1045 sp
->strile_length_total
=((sp
->image_length
+sp
->strile_length
-1)/sp
->strile_length
)*sp
->strile_length
;
1049 sp
->strile_width
=sp
->image_width
;
1050 sp
->strile_length
=tif
->tif_dir
.td_rowsperstrip
;
1051 sp
->strile_length_total
=sp
->image_length
;
1053 if (tif
->tif_dir
.td_samplesperpixel
==1)
1055 sp
->samples_per_pixel
=1;
1056 sp
->plane_sample_offset
=0;
1057 sp
->samples_per_pixel_per_plane
=sp
->samples_per_pixel
;
1058 sp
->subsampling_hor
=1;
1059 sp
->subsampling_ver
=1;
1063 if (tif
->tif_dir
.td_samplesperpixel
!=3)
1065 TIFFErrorExt(tif
->tif_clientdata
,module,"SamplesPerPixel %d not supported for this compression scheme",sp
->samples_per_pixel
);
1068 sp
->samples_per_pixel
=3;
1069 sp
->plane_sample_offset
=0;
1070 if (tif
->tif_dir
.td_planarconfig
==PLANARCONFIG_CONTIG
)
1071 sp
->samples_per_pixel_per_plane
=3;
1073 sp
->samples_per_pixel_per_plane
=1;
1075 if (sp
->strile_length
<sp
->image_length
)
1077 if (sp
->strile_length
%(sp
->subsampling_ver
*8)!=0)
1079 TIFFErrorExt(tif
->tif_clientdata
,module,"Incompatible vertical subsampling and image strip/tile length");
1082 sp
->restart_interval
=((sp
->strile_width
+sp
->subsampling_hor
*8-1)/(sp
->subsampling_hor
*8))*(sp
->strile_length
/(sp
->subsampling_ver
*8));
1084 if (OJPEGReadHeaderInfoSec(tif
)==0)
1086 sp
->sos_end
[0].log
=1;
1087 sp
->sos_end
[0].in_buffer_source
=sp
->in_buffer_source
;
1088 sp
->sos_end
[0].in_buffer_next_strile
=sp
->in_buffer_next_strile
;
1089 sp
->sos_end
[0].in_buffer_file_pos
=sp
->in_buffer_file_pos
-sp
->in_buffer_togo
;
1090 sp
->sos_end
[0].in_buffer_file_togo
=sp
->in_buffer_file_togo
+sp
->in_buffer_togo
;
1091 sp
->readheader_done
=1;
1096 OJPEGReadSecondarySos(TIFF
* tif
, uint16 s
)
1098 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1102 assert(sp
->sos_end
[0].log
!=0);
1103 assert(sp
->sos_end
[s
].log
==0);
1104 sp
->plane_sample_offset
=s
-1;
1105 while(sp
->sos_end
[sp
->plane_sample_offset
].log
==0)
1106 sp
->plane_sample_offset
--;
1107 sp
->in_buffer_source
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_source
;
1108 sp
->in_buffer_next_strile
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_next_strile
;
1109 sp
->in_buffer_file_pos
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_pos
;
1110 sp
->in_buffer_file_pos_log
=0;
1111 sp
->in_buffer_file_togo
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_togo
;
1112 sp
->in_buffer_togo
=0;
1113 sp
->in_buffer_cur
=0;
1114 while(sp
->plane_sample_offset
<s
)
1118 if (OJPEGReadByte(sp
,&m
)==0)
1124 if (OJPEGReadByte(sp
,&m
)==0)
1129 if (m
==JPEG_MARKER_SOS
)
1133 sp
->plane_sample_offset
++;
1134 if (OJPEGReadHeaderInfoSecStreamSos(tif
)==0)
1136 sp
->sos_end
[sp
->plane_sample_offset
].log
=1;
1137 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_source
=sp
->in_buffer_source
;
1138 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_next_strile
=sp
->in_buffer_next_strile
;
1139 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_pos
=sp
->in_buffer_file_pos
-sp
->in_buffer_togo
;
1140 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_togo
=sp
->in_buffer_file_togo
+sp
->in_buffer_togo
;
1146 OJPEGWriteHeaderInfo(TIFF
* tif
)
1148 static const char module[]="OJPEGWriteHeaderInfo";
1149 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1152 /* if a previous attempt failed, don't try again */
1153 if (sp
->libjpeg_session_active
!= 0)
1155 sp
->out_state
=ososSoi
;
1156 sp
->restart_index
=0;
1157 jpeg_std_error(&(sp
->libjpeg_jpeg_error_mgr
));
1158 sp
->libjpeg_jpeg_error_mgr
.output_message
=OJPEGLibjpegJpegErrorMgrOutputMessage
;
1159 sp
->libjpeg_jpeg_error_mgr
.error_exit
=OJPEGLibjpegJpegErrorMgrErrorExit
;
1160 sp
->libjpeg_jpeg_decompress_struct
.err
=&(sp
->libjpeg_jpeg_error_mgr
);
1161 sp
->libjpeg_jpeg_decompress_struct
.client_data
=(void*)tif
;
1162 if (jpeg_create_decompress_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
))==0)
1164 sp
->libjpeg_session_active
=1;
1165 sp
->libjpeg_jpeg_source_mgr
.bytes_in_buffer
=0;
1166 sp
->libjpeg_jpeg_source_mgr
.init_source
=OJPEGLibjpegJpegSourceMgrInitSource
;
1167 sp
->libjpeg_jpeg_source_mgr
.fill_input_buffer
=OJPEGLibjpegJpegSourceMgrFillInputBuffer
;
1168 sp
->libjpeg_jpeg_source_mgr
.skip_input_data
=OJPEGLibjpegJpegSourceMgrSkipInputData
;
1169 sp
->libjpeg_jpeg_source_mgr
.resync_to_restart
=OJPEGLibjpegJpegSourceMgrResyncToRestart
;
1170 sp
->libjpeg_jpeg_source_mgr
.term_source
=OJPEGLibjpegJpegSourceMgrTermSource
;
1171 sp
->libjpeg_jpeg_decompress_struct
.src
=&(sp
->libjpeg_jpeg_source_mgr
);
1172 if (jpeg_read_header_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),1)==0)
1174 if ((sp
->subsampling_force_desubsampling_inside_decompression
==0) && (sp
->samples_per_pixel_per_plane
>1))
1176 sp
->libjpeg_jpeg_decompress_struct
.raw_data_out
=1;
1177 #if JPEG_LIB_VERSION >= 70
1178 sp
->libjpeg_jpeg_decompress_struct
.do_fancy_upsampling
=FALSE
;
1180 sp
->libjpeg_jpeg_query_style
=0;
1181 if (sp
->subsampling_convert_log
==0)
1183 assert(sp
->subsampling_convert_ycbcrbuf
==0);
1184 assert(sp
->subsampling_convert_ycbcrimage
==0);
1185 sp
->subsampling_convert_ylinelen
=((sp
->strile_width
+sp
->subsampling_hor
*8-1)/(sp
->subsampling_hor
*8)*sp
->subsampling_hor
*8);
1186 sp
->subsampling_convert_ylines
=sp
->subsampling_ver
*8;
1187 sp
->subsampling_convert_clinelen
=sp
->subsampling_convert_ylinelen
/sp
->subsampling_hor
;
1188 sp
->subsampling_convert_clines
=8;
1189 sp
->subsampling_convert_ybuflen
=sp
->subsampling_convert_ylinelen
*sp
->subsampling_convert_ylines
;
1190 sp
->subsampling_convert_cbuflen
=sp
->subsampling_convert_clinelen
*sp
->subsampling_convert_clines
;
1191 sp
->subsampling_convert_ycbcrbuflen
=sp
->subsampling_convert_ybuflen
+2*sp
->subsampling_convert_cbuflen
;
1192 sp
->subsampling_convert_ycbcrbuf
=_TIFFmalloc(sp
->subsampling_convert_ycbcrbuflen
);
1193 if (sp
->subsampling_convert_ycbcrbuf
==0)
1195 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1198 sp
->subsampling_convert_ybuf
=sp
->subsampling_convert_ycbcrbuf
;
1199 sp
->subsampling_convert_cbbuf
=sp
->subsampling_convert_ybuf
+sp
->subsampling_convert_ybuflen
;
1200 sp
->subsampling_convert_crbuf
=sp
->subsampling_convert_cbbuf
+sp
->subsampling_convert_cbuflen
;
1201 sp
->subsampling_convert_ycbcrimagelen
=3+sp
->subsampling_convert_ylines
+2*sp
->subsampling_convert_clines
;
1202 sp
->subsampling_convert_ycbcrimage
=_TIFFmalloc(sp
->subsampling_convert_ycbcrimagelen
*sizeof(uint8
*));
1203 if (sp
->subsampling_convert_ycbcrimage
==0)
1205 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1208 m
=sp
->subsampling_convert_ycbcrimage
;
1209 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3);
1210 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3+sp
->subsampling_convert_ylines
);
1211 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3+sp
->subsampling_convert_ylines
+sp
->subsampling_convert_clines
);
1212 for (n
=0; n
<sp
->subsampling_convert_ylines
; n
++)
1213 *m
++=sp
->subsampling_convert_ybuf
+n
*sp
->subsampling_convert_ylinelen
;
1214 for (n
=0; n
<sp
->subsampling_convert_clines
; n
++)
1215 *m
++=sp
->subsampling_convert_cbbuf
+n
*sp
->subsampling_convert_clinelen
;
1216 for (n
=0; n
<sp
->subsampling_convert_clines
; n
++)
1217 *m
++=sp
->subsampling_convert_crbuf
+n
*sp
->subsampling_convert_clinelen
;
1218 sp
->subsampling_convert_clinelenout
=((sp
->strile_width
+sp
->subsampling_hor
-1)/sp
->subsampling_hor
);
1219 sp
->subsampling_convert_state
=0;
1220 sp
->bytes_per_line
=sp
->subsampling_convert_clinelenout
*(sp
->subsampling_ver
*sp
->subsampling_hor
+2);
1221 sp
->lines_per_strile
=((sp
->strile_length
+sp
->subsampling_ver
-1)/sp
->subsampling_ver
);
1222 sp
->subsampling_convert_log
=1;
1227 sp
->libjpeg_jpeg_decompress_struct
.jpeg_color_space
=JCS_UNKNOWN
;
1228 sp
->libjpeg_jpeg_decompress_struct
.out_color_space
=JCS_UNKNOWN
;
1229 sp
->libjpeg_jpeg_query_style
=1;
1230 sp
->bytes_per_line
=sp
->samples_per_pixel_per_plane
*sp
->strile_width
;
1231 sp
->lines_per_strile
=sp
->strile_length
;
1233 if (jpeg_start_decompress_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
))==0)
1235 sp
->writeheader_done
=1;
1240 OJPEGLibjpegSessionAbort(TIFF
* tif
)
1242 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1243 assert(sp
->libjpeg_session_active
!=0);
1244 jpeg_destroy((jpeg_common_struct
*)(&(sp
->libjpeg_jpeg_decompress_struct
)));
1245 sp
->libjpeg_session_active
=0;
1249 OJPEGReadHeaderInfoSec(TIFF
* tif
)
1251 static const char module[]="OJPEGReadHeaderInfoSec";
1252 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1256 if (sp
->file_size
==0)
1257 sp
->file_size
=TIFFGetFileSize(tif
);
1258 if (sp
->jpeg_interchange_format
!=0)
1260 if (sp
->jpeg_interchange_format
>=sp
->file_size
)
1262 sp
->jpeg_interchange_format
=0;
1263 sp
->jpeg_interchange_format_length
=0;
1267 if ((sp
->jpeg_interchange_format_length
==0) || (sp
->jpeg_interchange_format
+sp
->jpeg_interchange_format_length
>sp
->file_size
))
1268 sp
->jpeg_interchange_format_length
=sp
->file_size
-sp
->jpeg_interchange_format
;
1271 sp
->in_buffer_source
=osibsNotSetYet
;
1272 sp
->in_buffer_next_strile
=0;
1273 sp
->in_buffer_strile_count
=tif
->tif_dir
.td_nstrips
;
1274 sp
->in_buffer_file_togo
=0;
1275 sp
->in_buffer_togo
=0;
1278 if (OJPEGReadBytePeek(sp
,&m
)==0)
1282 OJPEGReadByteAdvance(sp
);
1285 if (OJPEGReadByte(sp
,&m
)==0)
1290 case JPEG_MARKER_SOI
:
1291 /* this type of marker has no data, and should be skipped */
1293 case JPEG_MARKER_COM
:
1294 case JPEG_MARKER_APP0
:
1295 case JPEG_MARKER_APP0
+1:
1296 case JPEG_MARKER_APP0
+2:
1297 case JPEG_MARKER_APP0
+3:
1298 case JPEG_MARKER_APP0
+4:
1299 case JPEG_MARKER_APP0
+5:
1300 case JPEG_MARKER_APP0
+6:
1301 case JPEG_MARKER_APP0
+7:
1302 case JPEG_MARKER_APP0
+8:
1303 case JPEG_MARKER_APP0
+9:
1304 case JPEG_MARKER_APP0
+10:
1305 case JPEG_MARKER_APP0
+11:
1306 case JPEG_MARKER_APP0
+12:
1307 case JPEG_MARKER_APP0
+13:
1308 case JPEG_MARKER_APP0
+14:
1309 case JPEG_MARKER_APP0
+15:
1310 /* this type of marker has data, but it has no use to us (and no place here) and should be skipped */
1311 if (OJPEGReadWord(sp
,&n
)==0)
1315 if (sp
->subsamplingcorrect
==0)
1316 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt JPEG data");
1320 OJPEGReadSkip(sp
,n
-2);
1322 case JPEG_MARKER_DRI
:
1323 if (OJPEGReadHeaderInfoSecStreamDri(tif
)==0)
1326 case JPEG_MARKER_DQT
:
1327 if (OJPEGReadHeaderInfoSecStreamDqt(tif
)==0)
1330 case JPEG_MARKER_DHT
:
1331 if (OJPEGReadHeaderInfoSecStreamDht(tif
)==0)
1334 case JPEG_MARKER_SOF0
:
1335 case JPEG_MARKER_SOF1
:
1336 case JPEG_MARKER_SOF3
:
1337 if (OJPEGReadHeaderInfoSecStreamSof(tif
,m
)==0)
1339 if (sp
->subsamplingcorrect
!=0)
1342 case JPEG_MARKER_SOS
:
1343 if (sp
->subsamplingcorrect
!=0)
1345 assert(sp
->plane_sample_offset
==0);
1346 if (OJPEGReadHeaderInfoSecStreamSos(tif
)==0)
1350 TIFFErrorExt(tif
->tif_clientdata
,module,"Unknown marker type %d in JPEG data",m
);
1353 } while(m
!=JPEG_MARKER_SOS
);
1354 if (sp
->subsamplingcorrect
)
1358 if (OJPEGReadHeaderInfoSecTablesQTable(tif
)==0)
1360 sp
->sof_marker_id
=JPEG_MARKER_SOF0
;
1361 for (o
=0; o
<sp
->samples_per_pixel
; o
++)
1363 sp
->sof_hv
[0]=((sp
->subsampling_hor
<<4)|sp
->subsampling_ver
);
1364 for (o
=1; o
<sp
->samples_per_pixel
; o
++)
1366 sp
->sof_x
=sp
->strile_width
;
1367 sp
->sof_y
=sp
->strile_length_total
;
1369 if (OJPEGReadHeaderInfoSecTablesDcTable(tif
)==0)
1371 if (OJPEGReadHeaderInfoSecTablesAcTable(tif
)==0)
1373 for (o
=1; o
<sp
->samples_per_pixel
; o
++)
1380 OJPEGReadHeaderInfoSecStreamDri(TIFF
* tif
)
1382 /* this could easilly cause trouble in some cases... but no such cases have occured sofar */
1383 static const char module[]="OJPEGReadHeaderInfoSecStreamDri";
1384 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1386 if (OJPEGReadWord(sp
,&m
)==0)
1390 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DRI marker in JPEG data");
1393 if (OJPEGReadWord(sp
,&m
)==0)
1395 sp
->restart_interval
=m
;
1400 OJPEGReadHeaderInfoSecStreamDqt(TIFF
* tif
)
1402 /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1403 static const char module[]="OJPEGReadHeaderInfoSecStreamDqt";
1404 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1409 if (OJPEGReadWord(sp
,&m
)==0)
1413 if (sp
->subsamplingcorrect
==0)
1414 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DQT marker in JPEG data");
1417 if (sp
->subsamplingcorrect
!=0)
1418 OJPEGReadSkip(sp
,m
-2);
1426 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DQT marker in JPEG data");
1429 na
=sizeof(uint32
)+69;
1433 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1437 nb
[sizeof(uint32
)]=255;
1438 nb
[sizeof(uint32
)+1]=JPEG_MARKER_DQT
;
1439 nb
[sizeof(uint32
)+2]=0;
1440 nb
[sizeof(uint32
)+3]=67;
1441 if (OJPEGReadBlock(sp
,65,&nb
[sizeof(uint32
)+4])==0) {
1445 o
=nb
[sizeof(uint32
)+4]&15;
1448 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DQT marker in JPEG data");
1452 if (sp
->qtable
[o
]!=0)
1453 _TIFFfree(sp
->qtable
[o
]);
1462 OJPEGReadHeaderInfoSecStreamDht(TIFF
* tif
)
1464 /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1465 /* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
1466 static const char module[]="OJPEGReadHeaderInfoSecStreamDht";
1467 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1472 if (OJPEGReadWord(sp
,&m
)==0)
1476 if (sp
->subsamplingcorrect
==0)
1477 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DHT marker in JPEG data");
1480 if (sp
->subsamplingcorrect
!=0)
1482 OJPEGReadSkip(sp
,m
-2);
1486 na
=sizeof(uint32
)+2+m
;
1490 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1494 nb
[sizeof(uint32
)]=255;
1495 nb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1496 nb
[sizeof(uint32
)+2]=(m
>>8);
1497 nb
[sizeof(uint32
)+3]=(m
&255);
1498 if (OJPEGReadBlock(sp
,m
-2,&nb
[sizeof(uint32
)+4])==0)
1500 o
=nb
[sizeof(uint32
)+4];
1505 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DHT marker in JPEG data");
1508 if (sp
->dctable
[o
]!=0)
1509 _TIFFfree(sp
->dctable
[o
]);
1516 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DHT marker in JPEG data");
1522 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DHT marker in JPEG data");
1525 if (sp
->actable
[o
]!=0)
1526 _TIFFfree(sp
->actable
[o
]);
1534 OJPEGReadHeaderInfoSecStreamSof(TIFF
* tif
, uint8 marker_id
)
1536 /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1537 static const char module[]="OJPEGReadHeaderInfoSecStreamSof";
1538 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1546 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt JPEG data");
1549 if (sp
->subsamplingcorrect
==0)
1550 sp
->sof_marker_id
=marker_id
;
1551 /* Lf: data length */
1552 if (OJPEGReadWord(sp
,&m
)==0)
1556 if (sp
->subsamplingcorrect
==0)
1557 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOF marker in JPEG data");
1563 if (sp
->subsamplingcorrect
==0)
1564 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOF marker in JPEG data");
1568 if (sp
->subsamplingcorrect
==0)
1570 if (n
!=sp
->samples_per_pixel
)
1572 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected number of samples");
1576 /* P: Sample precision */
1577 if (OJPEGReadByte(sp
,&o
)==0)
1581 if (sp
->subsamplingcorrect
==0)
1582 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected number of bits per sample");
1585 /* Y: Number of lines, X: Number of samples per line */
1586 if (sp
->subsamplingcorrect
)
1587 OJPEGReadSkip(sp
,4);
1590 /* Y: Number of lines */
1591 if (OJPEGReadWord(sp
,&p
)==0)
1593 if (((uint32
)p
<sp
->image_length
) && ((uint32
)p
<sp
->strile_length_total
))
1595 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected height");
1599 /* X: Number of samples per line */
1600 if (OJPEGReadWord(sp
,&p
)==0)
1602 if (((uint32
)p
<sp
->image_width
) && ((uint32
)p
<sp
->strile_width
))
1604 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected width");
1607 if ((uint32
)p
>sp
->strile_width
)
1609 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data image width exceeds expected image width");
1614 /* Nf: Number of image components in frame */
1615 if (OJPEGReadByte(sp
,&o
)==0)
1619 if (sp
->subsamplingcorrect
==0)
1620 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOF marker in JPEG data");
1623 /* per component stuff */
1624 /* 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 */
1627 /* C: Component identifier */
1628 if (OJPEGReadByte(sp
,&o
)==0)
1630 if (sp
->subsamplingcorrect
==0)
1632 /* H: Horizontal sampling factor, and V: Vertical sampling factor */
1633 if (OJPEGReadByte(sp
,&o
)==0)
1635 if (sp
->subsamplingcorrect
!=0)
1639 sp
->subsampling_hor
=(o
>>4);
1640 sp
->subsampling_ver
=(o
&15);
1641 if (((sp
->subsampling_hor
!=1) && (sp
->subsampling_hor
!=2) && (sp
->subsampling_hor
!=4)) ||
1642 ((sp
->subsampling_ver
!=1) && (sp
->subsampling_ver
!=2) && (sp
->subsampling_ver
!=4)))
1643 sp
->subsampling_force_desubsampling_inside_decompression
=1;
1648 sp
->subsampling_force_desubsampling_inside_decompression
=1;
1654 if (sp
->subsampling_force_desubsampling_inside_decompression
==0)
1658 if (o
!=((sp
->subsampling_hor
<<4)|sp
->subsampling_ver
))
1660 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected subsampling values");
1668 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected subsampling values");
1674 /* Tq: Quantization table destination selector */
1675 if (OJPEGReadByte(sp
,&o
)==0)
1677 if (sp
->subsamplingcorrect
==0)
1680 if (sp
->subsamplingcorrect
==0)
1686 OJPEGReadHeaderInfoSecStreamSos(TIFF
* tif
)
1688 /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1689 static const char module[]="OJPEGReadHeaderInfoSecStreamSos";
1690 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1694 assert(sp
->subsamplingcorrect
==0);
1697 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOS marker in JPEG data");
1701 if (OJPEGReadWord(sp
,&m
)==0)
1703 if (m
!=6+sp
->samples_per_pixel_per_plane
*2)
1705 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOS marker in JPEG data");
1709 if (OJPEGReadByte(sp
,&n
)==0)
1711 if (n
!=sp
->samples_per_pixel_per_plane
)
1713 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOS marker in JPEG data");
1716 /* Cs, Td, and Ta */
1717 for (o
=0; o
<sp
->samples_per_pixel_per_plane
; o
++)
1720 if (OJPEGReadByte(sp
,&n
)==0)
1722 sp
->sos_cs
[sp
->plane_sample_offset
+o
]=n
;
1724 if (OJPEGReadByte(sp
,&n
)==0)
1726 sp
->sos_tda
[sp
->plane_sample_offset
+o
]=n
;
1728 /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as per LibJpeg source */
1729 OJPEGReadSkip(sp
,3);
1734 OJPEGReadHeaderInfoSecTablesQTable(TIFF
* tif
)
1736 static const char module[]="OJPEGReadHeaderInfoSecTablesQTable";
1737 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1743 if (sp
->qtable_offset
[0]==0)
1745 TIFFErrorExt(tif
->tif_clientdata
,module,"Missing JPEG tables");
1748 sp
->in_buffer_file_pos_log
=0;
1749 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1751 if ((sp
->qtable_offset
[m
]!=0) && ((m
==0) || (sp
->qtable_offset
[m
]!=sp
->qtable_offset
[m
-1])))
1753 for (n
=0; n
<m
-1; n
++)
1755 if (sp
->qtable_offset
[m
]==sp
->qtable_offset
[n
])
1757 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt JpegQTables tag value");
1761 oa
=sizeof(uint32
)+69;
1765 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1769 ob
[sizeof(uint32
)]=255;
1770 ob
[sizeof(uint32
)+1]=JPEG_MARKER_DQT
;
1771 ob
[sizeof(uint32
)+2]=0;
1772 ob
[sizeof(uint32
)+3]=67;
1773 ob
[sizeof(uint32
)+4]=m
;
1774 TIFFSeekFile(tif
,sp
->qtable_offset
[m
],SEEK_SET
);
1775 p
=TIFFReadFile(tif
,&ob
[sizeof(uint32
)+5],64);
1782 sp
->sof_tq
[m
]=sp
->sof_tq
[m
-1];
1788 OJPEGReadHeaderInfoSecTablesDcTable(TIFF
* tif
)
1790 static const char module[]="OJPEGReadHeaderInfoSecTablesDcTable";
1791 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1799 if (sp
->dctable_offset
[0]==0)
1801 TIFFErrorExt(tif
->tif_clientdata
,module,"Missing JPEG tables");
1804 sp
->in_buffer_file_pos_log
=0;
1805 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1807 if ((sp
->dctable_offset
[m
]!=0) && ((m
==0) || (sp
->dctable_offset
[m
]!=sp
->dctable_offset
[m
-1])))
1809 for (n
=0; n
<m
-1; n
++)
1811 if (sp
->dctable_offset
[m
]==sp
->dctable_offset
[n
])
1813 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt JpegDcTables tag value");
1817 TIFFSeekFile(tif
,sp
->dctable_offset
[m
],SEEK_SET
);
1818 p
=TIFFReadFile(tif
,o
,16);
1822 for (n
=0; n
<16; n
++)
1824 ra
=sizeof(uint32
)+21+q
;
1828 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1832 rb
[sizeof(uint32
)]=255;
1833 rb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1834 rb
[sizeof(uint32
)+2]=((19+q
)>>8);
1835 rb
[sizeof(uint32
)+3]=((19+q
)&255);
1836 rb
[sizeof(uint32
)+4]=m
;
1837 for (n
=0; n
<16; n
++)
1838 rb
[sizeof(uint32
)+5+n
]=o
[n
];
1839 p
=TIFFReadFile(tif
,&(rb
[sizeof(uint32
)+21]),q
);
1843 sp
->sos_tda
[m
]=(m
<<4);
1846 sp
->sos_tda
[m
]=sp
->sos_tda
[m
-1];
1852 OJPEGReadHeaderInfoSecTablesAcTable(TIFF
* tif
)
1854 static const char module[]="OJPEGReadHeaderInfoSecTablesAcTable";
1855 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1863 if (sp
->actable_offset
[0]==0)
1865 TIFFErrorExt(tif
->tif_clientdata
,module,"Missing JPEG tables");
1868 sp
->in_buffer_file_pos_log
=0;
1869 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1871 if ((sp
->actable_offset
[m
]!=0) && ((m
==0) || (sp
->actable_offset
[m
]!=sp
->actable_offset
[m
-1])))
1873 for (n
=0; n
<m
-1; n
++)
1875 if (sp
->actable_offset
[m
]==sp
->actable_offset
[n
])
1877 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt JpegAcTables tag value");
1881 TIFFSeekFile(tif
,sp
->actable_offset
[m
],SEEK_SET
);
1882 p
=TIFFReadFile(tif
,o
,16);
1886 for (n
=0; n
<16; n
++)
1888 ra
=sizeof(uint32
)+21+q
;
1892 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1896 rb
[sizeof(uint32
)]=255;
1897 rb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1898 rb
[sizeof(uint32
)+2]=((19+q
)>>8);
1899 rb
[sizeof(uint32
)+3]=((19+q
)&255);
1900 rb
[sizeof(uint32
)+4]=(16|m
);
1901 for (n
=0; n
<16; n
++)
1902 rb
[sizeof(uint32
)+5+n
]=o
[n
];
1903 p
=TIFFReadFile(tif
,&(rb
[sizeof(uint32
)+21]),q
);
1907 sp
->sos_tda
[m
]=(sp
->sos_tda
[m
]|m
);
1910 sp
->sos_tda
[m
]=(sp
->sos_tda
[m
]|(sp
->sos_tda
[m
-1]&15));
1916 OJPEGReadBufferFill(OJPEGState
* sp
)
1920 /* TODO: double-check: when subsamplingcorrect is set, no call to TIFFErrorExt or TIFFWarningExt should be made
1921 * in any other case, seek or read errors should be passed through */
1924 if (sp
->in_buffer_file_togo
!=0)
1926 if (sp
->in_buffer_file_pos_log
==0)
1928 TIFFSeekFile(sp
->tif
,sp
->in_buffer_file_pos
,SEEK_SET
);
1929 sp
->in_buffer_file_pos_log
=1;
1932 if ((uint64
)m
>sp
->in_buffer_file_togo
)
1933 m
=(uint16
)sp
->in_buffer_file_togo
;
1934 n
=TIFFReadFile(sp
->tif
,sp
->in_buffer
,(tmsize_t
)m
);
1938 assert(n
<=OJPEG_BUFFER
);
1940 assert((uint64
)n
<=sp
->in_buffer_file_togo
);
1942 sp
->in_buffer_togo
=m
;
1943 sp
->in_buffer_cur
=sp
->in_buffer
;
1944 sp
->in_buffer_file_togo
-=m
;
1945 sp
->in_buffer_file_pos
+=m
;
1948 sp
->in_buffer_file_pos_log
=0;
1949 switch(sp
->in_buffer_source
)
1951 case osibsNotSetYet
:
1952 if (sp
->jpeg_interchange_format
!=0)
1954 sp
->in_buffer_file_pos
=sp
->jpeg_interchange_format
;
1955 sp
->in_buffer_file_togo
=sp
->jpeg_interchange_format_length
;
1957 sp
->in_buffer_source
=osibsJpegInterchangeFormat
;
1959 case osibsJpegInterchangeFormat
:
1960 sp
->in_buffer_source
=osibsStrile
;
1962 if (!_TIFFFillStriles( sp
->tif
)
1963 || sp
->tif
->tif_dir
.td_stripoffset
== NULL
1964 || sp
->tif
->tif_dir
.td_stripbytecount
== NULL
)
1967 if (sp
->in_buffer_next_strile
==sp
->in_buffer_strile_count
)
1968 sp
->in_buffer_source
=osibsEof
;
1971 sp
->in_buffer_file_pos
=sp
->tif
->tif_dir
.td_stripoffset
[sp
->in_buffer_next_strile
];
1972 if (sp
->in_buffer_file_pos
!=0)
1974 if (sp
->in_buffer_file_pos
>=sp
->file_size
)
1975 sp
->in_buffer_file_pos
=0;
1976 else if (sp
->tif
->tif_dir
.td_stripbytecount
==NULL
)
1977 sp
->in_buffer_file_togo
=sp
->file_size
-sp
->in_buffer_file_pos
;
1980 if (sp
->tif
->tif_dir
.td_stripbytecount
== 0) {
1981 TIFFErrorExt(sp
->tif
->tif_clientdata
,sp
->tif
->tif_name
,"Strip byte counts are missing");
1984 sp
->in_buffer_file_togo
=sp
->tif
->tif_dir
.td_stripbytecount
[sp
->in_buffer_next_strile
];
1985 if (sp
->in_buffer_file_togo
==0)
1986 sp
->in_buffer_file_pos
=0;
1987 else if (sp
->in_buffer_file_pos
+sp
->in_buffer_file_togo
>sp
->file_size
)
1988 sp
->in_buffer_file_togo
=sp
->file_size
-sp
->in_buffer_file_pos
;
1991 sp
->in_buffer_next_strile
++;
2002 OJPEGReadByte(OJPEGState
* sp
, uint8
* byte
)
2004 if (sp
->in_buffer_togo
==0)
2006 if (OJPEGReadBufferFill(sp
)==0)
2008 assert(sp
->in_buffer_togo
>0);
2010 *byte
=*(sp
->in_buffer_cur
);
2011 sp
->in_buffer_cur
++;
2012 sp
->in_buffer_togo
--;
2017 OJPEGReadBytePeek(OJPEGState
* sp
, uint8
* byte
)
2019 if (sp
->in_buffer_togo
==0)
2021 if (OJPEGReadBufferFill(sp
)==0)
2023 assert(sp
->in_buffer_togo
>0);
2025 *byte
=*(sp
->in_buffer_cur
);
2030 OJPEGReadByteAdvance(OJPEGState
* sp
)
2032 assert(sp
->in_buffer_togo
>0);
2033 sp
->in_buffer_cur
++;
2034 sp
->in_buffer_togo
--;
2038 OJPEGReadWord(OJPEGState
* sp
, uint16
* word
)
2041 if (OJPEGReadByte(sp
,&m
)==0)
2044 if (OJPEGReadByte(sp
,&m
)==0)
2051 OJPEGReadBlock(OJPEGState
* sp
, uint16 len
, void* mem
)
2061 if (sp
->in_buffer_togo
==0)
2063 if (OJPEGReadBufferFill(sp
)==0)
2065 assert(sp
->in_buffer_togo
>0);
2068 if (n
>sp
->in_buffer_togo
)
2069 n
=sp
->in_buffer_togo
;
2070 _TIFFmemcpy(mmem
,sp
->in_buffer_cur
,n
);
2071 sp
->in_buffer_cur
+=n
;
2072 sp
->in_buffer_togo
-=n
;
2080 OJPEGReadSkip(OJPEGState
* sp
, uint16 len
)
2086 if (n
>sp
->in_buffer_togo
)
2087 n
=sp
->in_buffer_togo
;
2088 sp
->in_buffer_cur
+=n
;
2089 sp
->in_buffer_togo
-=n
;
2093 assert(sp
->in_buffer_togo
==0);
2095 if ((uint64
)n
>sp
->in_buffer_file_togo
)
2096 n
=(uint16
)sp
->in_buffer_file_togo
;
2097 sp
->in_buffer_file_pos
+=n
;
2098 sp
->in_buffer_file_togo
-=n
;
2099 sp
->in_buffer_file_pos_log
=0;
2100 /* we don't skip past jpeginterchangeformat/strile block...
2101 * if that is asked from us, we're dealing with totally bazurk
2102 * data anyway, and we've not seen this happening on any
2103 * testfile, so we might as well likely cause some other
2104 * meaningless error to be passed at some later time
2110 OJPEGWriteStream(TIFF
* tif
, void** mem
, uint32
* len
)
2112 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2116 assert(sp
->out_state
<=ososEoi
);
2117 switch(sp
->out_state
)
2120 OJPEGWriteStreamSoi(tif
,mem
,len
);
2123 OJPEGWriteStreamQTable(tif
,0,mem
,len
);
2126 OJPEGWriteStreamQTable(tif
,1,mem
,len
);
2129 OJPEGWriteStreamQTable(tif
,2,mem
,len
);
2132 OJPEGWriteStreamQTable(tif
,3,mem
,len
);
2135 OJPEGWriteStreamDcTable(tif
,0,mem
,len
);
2138 OJPEGWriteStreamDcTable(tif
,1,mem
,len
);
2141 OJPEGWriteStreamDcTable(tif
,2,mem
,len
);
2144 OJPEGWriteStreamDcTable(tif
,3,mem
,len
);
2147 OJPEGWriteStreamAcTable(tif
,0,mem
,len
);
2150 OJPEGWriteStreamAcTable(tif
,1,mem
,len
);
2153 OJPEGWriteStreamAcTable(tif
,2,mem
,len
);
2156 OJPEGWriteStreamAcTable(tif
,3,mem
,len
);
2159 OJPEGWriteStreamDri(tif
,mem
,len
);
2162 OJPEGWriteStreamSof(tif
,mem
,len
);
2165 OJPEGWriteStreamSos(tif
,mem
,len
);
2167 case ososCompressed
:
2168 if (OJPEGWriteStreamCompressed(tif
,mem
,len
)==0)
2172 OJPEGWriteStreamRst(tif
,mem
,len
);
2175 OJPEGWriteStreamEoi(tif
,mem
,len
);
2183 OJPEGWriteStreamSoi(TIFF
* tif
, void** mem
, uint32
* len
)
2185 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2186 assert(OJPEG_BUFFER
>=2);
2187 sp
->out_buffer
[0]=255;
2188 sp
->out_buffer
[1]=JPEG_MARKER_SOI
;
2190 *mem
=(void*)sp
->out_buffer
;
2195 OJPEGWriteStreamQTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2197 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2198 if (sp
->qtable
[table_index
]!=0)
2200 *mem
=(void*)(sp
->qtable
[table_index
]+sizeof(uint32
));
2201 *len
=*((uint32
*)sp
->qtable
[table_index
])-sizeof(uint32
);
2207 OJPEGWriteStreamDcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2209 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2210 if (sp
->dctable
[table_index
]!=0)
2212 *mem
=(void*)(sp
->dctable
[table_index
]+sizeof(uint32
));
2213 *len
=*((uint32
*)sp
->dctable
[table_index
])-sizeof(uint32
);
2219 OJPEGWriteStreamAcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2221 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2222 if (sp
->actable
[table_index
]!=0)
2224 *mem
=(void*)(sp
->actable
[table_index
]+sizeof(uint32
));
2225 *len
=*((uint32
*)sp
->actable
[table_index
])-sizeof(uint32
);
2231 OJPEGWriteStreamDri(TIFF
* tif
, void** mem
, uint32
* len
)
2233 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2234 assert(OJPEG_BUFFER
>=6);
2235 if (sp
->restart_interval
!=0)
2237 sp
->out_buffer
[0]=255;
2238 sp
->out_buffer
[1]=JPEG_MARKER_DRI
;
2239 sp
->out_buffer
[2]=0;
2240 sp
->out_buffer
[3]=4;
2241 sp
->out_buffer
[4]=(sp
->restart_interval
>>8);
2242 sp
->out_buffer
[5]=(sp
->restart_interval
&255);
2244 *mem
=(void*)sp
->out_buffer
;
2250 OJPEGWriteStreamSof(TIFF
* tif
, void** mem
, uint32
* len
)
2252 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2254 assert(OJPEG_BUFFER
>=2+8+sp
->samples_per_pixel_per_plane
*3);
2255 assert(255>=8+sp
->samples_per_pixel_per_plane
*3);
2256 sp
->out_buffer
[0]=255;
2257 sp
->out_buffer
[1]=sp
->sof_marker_id
;
2259 sp
->out_buffer
[2]=0;
2260 sp
->out_buffer
[3]=8+sp
->samples_per_pixel_per_plane
*3;
2262 sp
->out_buffer
[4]=8;
2264 sp
->out_buffer
[5]=(sp
->sof_y
>>8);
2265 sp
->out_buffer
[6]=(sp
->sof_y
&255);
2267 sp
->out_buffer
[7]=(sp
->sof_x
>>8);
2268 sp
->out_buffer
[8]=(sp
->sof_x
&255);
2270 sp
->out_buffer
[9]=sp
->samples_per_pixel_per_plane
;
2271 for (m
=0; m
<sp
->samples_per_pixel_per_plane
; m
++)
2274 sp
->out_buffer
[10+m
*3]=sp
->sof_c
[sp
->plane_sample_offset
+m
];
2276 sp
->out_buffer
[10+m
*3+1]=sp
->sof_hv
[sp
->plane_sample_offset
+m
];
2278 sp
->out_buffer
[10+m
*3+2]=sp
->sof_tq
[sp
->plane_sample_offset
+m
];
2280 *len
=10+sp
->samples_per_pixel_per_plane
*3;
2281 *mem
=(void*)sp
->out_buffer
;
2286 OJPEGWriteStreamSos(TIFF
* tif
, void** mem
, uint32
* len
)
2288 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2290 assert(OJPEG_BUFFER
>=2+6+sp
->samples_per_pixel_per_plane
*2);
2291 assert(255>=6+sp
->samples_per_pixel_per_plane
*2);
2292 sp
->out_buffer
[0]=255;
2293 sp
->out_buffer
[1]=JPEG_MARKER_SOS
;
2295 sp
->out_buffer
[2]=0;
2296 sp
->out_buffer
[3]=6+sp
->samples_per_pixel_per_plane
*2;
2298 sp
->out_buffer
[4]=sp
->samples_per_pixel_per_plane
;
2299 for (m
=0; m
<sp
->samples_per_pixel_per_plane
; m
++)
2302 sp
->out_buffer
[5+m
*2]=sp
->sos_cs
[sp
->plane_sample_offset
+m
];
2304 sp
->out_buffer
[5+m
*2+1]=sp
->sos_tda
[sp
->plane_sample_offset
+m
];
2307 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2]=0;
2309 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2+1]=63;
2311 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2+2]=0;
2312 *len
=8+sp
->samples_per_pixel_per_plane
*2;
2313 *mem
=(void*)sp
->out_buffer
;
2318 OJPEGWriteStreamCompressed(TIFF
* tif
, void** mem
, uint32
* len
)
2320 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2321 if (sp
->in_buffer_togo
==0)
2323 if (OJPEGReadBufferFill(sp
)==0)
2325 assert(sp
->in_buffer_togo
>0);
2327 *len
=sp
->in_buffer_togo
;
2328 *mem
=(void*)sp
->in_buffer_cur
;
2329 sp
->in_buffer_togo
=0;
2330 if (sp
->in_buffer_file_togo
==0)
2332 switch(sp
->in_buffer_source
)
2335 if (sp
->in_buffer_next_strile
<sp
->in_buffer_strile_count
)
2336 sp
->out_state
=ososRst
;
2338 sp
->out_state
=ososEoi
;
2341 sp
->out_state
=ososEoi
;
2351 OJPEGWriteStreamRst(TIFF
* tif
, void** mem
, uint32
* len
)
2353 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2354 assert(OJPEG_BUFFER
>=2);
2355 sp
->out_buffer
[0]=255;
2356 sp
->out_buffer
[1]=JPEG_MARKER_RST0
+sp
->restart_index
;
2357 sp
->restart_index
++;
2358 if (sp
->restart_index
==8)
2359 sp
->restart_index
=0;
2361 *mem
=(void*)sp
->out_buffer
;
2362 sp
->out_state
=ososCompressed
;
2366 OJPEGWriteStreamEoi(TIFF
* tif
, void** mem
, uint32
* len
)
2368 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2369 assert(OJPEG_BUFFER
>=2);
2370 sp
->out_buffer
[0]=255;
2371 sp
->out_buffer
[1]=JPEG_MARKER_EOI
;
2373 *mem
=(void*)sp
->out_buffer
;
2376 #ifndef LIBJPEG_ENCAP_EXTERNAL
2378 jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
)
2380 return(SETJMP(sp
->exit_jmpbuf
)?0:(jpeg_create_decompress(cinfo
),1));
2384 #ifndef LIBJPEG_ENCAP_EXTERNAL
2386 jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
)
2388 return(SETJMP(sp
->exit_jmpbuf
)?0:(jpeg_read_header(cinfo
,require_image
),1));
2392 #ifndef LIBJPEG_ENCAP_EXTERNAL
2394 jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
)
2396 return(SETJMP(sp
->exit_jmpbuf
)?0:(jpeg_start_decompress(cinfo
),1));
2400 #ifndef LIBJPEG_ENCAP_EXTERNAL
2402 jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
)
2404 return(SETJMP(sp
->exit_jmpbuf
)?0:(jpeg_read_scanlines(cinfo
,scanlines
,max_lines
),1));
2408 #ifndef LIBJPEG_ENCAP_EXTERNAL
2410 jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
)
2412 return(SETJMP(sp
->exit_jmpbuf
)?0:(jpeg_read_raw_data(cinfo
,data
,max_lines
),1));
2416 #ifndef LIBJPEG_ENCAP_EXTERNAL
2418 jpeg_encap_unwind(TIFF
* tif
)
2420 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2421 LONGJMP(sp
->exit_jmpbuf
,1);
2426 OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct
* cinfo
)
2428 char buffer
[JMSG_LENGTH_MAX
];
2429 (*cinfo
->err
->format_message
)(cinfo
,buffer
);
2430 TIFFWarningExt(((TIFF
*)(cinfo
->client_data
))->tif_clientdata
,"LibJpeg","%s",buffer
);
2434 OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct
* cinfo
)
2436 char buffer
[JMSG_LENGTH_MAX
];
2437 (*cinfo
->err
->format_message
)(cinfo
,buffer
);
2438 TIFFErrorExt(((TIFF
*)(cinfo
->client_data
))->tif_clientdata
,"LibJpeg","%s",buffer
);
2439 jpeg_encap_unwind((TIFF
*)(cinfo
->client_data
));
2443 OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct
* cinfo
)
2448 static wxjpeg_boolean
2449 OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct
* cinfo
)
2451 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2452 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2455 if (OJPEGWriteStream(tif
,&mem
,&len
)==0)
2457 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Premature end of JPEG data");
2458 jpeg_encap_unwind(tif
);
2460 sp
->libjpeg_jpeg_source_mgr
.bytes_in_buffer
=len
;
2461 sp
->libjpeg_jpeg_source_mgr
.next_input_byte
=mem
;
2466 OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct
* cinfo
, long num_bytes
)
2468 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2470 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Unexpected error");
2471 jpeg_encap_unwind(tif
);
2474 static wxjpeg_boolean
2475 OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct
* cinfo
, int desired
)
2477 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2479 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Unexpected error");
2480 jpeg_encap_unwind(tif
);
2485 OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct
* cinfo
)