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
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>.
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.
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.
28 Copyright (c) Joris Van Damme <info@awaresystems.be>
29 Copyright (c) AWare Systems <http://www.awaresystems.be/>
31 The licence agreement for this file is the same as the rest of the LibTiff
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
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>.
46 /* What is what, and what is not?
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.
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
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.
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.
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.
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.
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.
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
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.
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
123 #define WIN32_LEAN_AND_MEAN
129 /* Configuration defines here are:
130 * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some environments,
131 * like eg LibTiffDelphi, this is not possible. For this reason, the actual calls to
132 * libjpeg, with longjump stuff, are encapsulated in dedicated functions. When
133 * JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declared external
134 * to this unit, and can be defined elsewhere to use stuff other then longjump.
135 * The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encapsulators
136 * here, internally, with normal longjump.
137 * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
138 * conviniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
139 * in place of plain setjmp. These macros will make it easier. It is useless
140 * to fiddle with these if you define JPEG_ENCAP_EXTERNAL.
141 * OJPEG_BUFFER: Define the size of the desired buffer here. Should be small enough so as to guarantee
142 * instant processing, optimal streaming and optimal use of processor cache, but also big
143 * enough so as to not result in significant call overhead. It should be at least a few
144 * bytes to accomodate some structures (this is verified in asserts), but it would not be
145 * sensible to make it this small anyway, and it should be at most 64K since it is indexed
146 * with uint16. We recommend 2K.
147 * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used anywhere and has
148 * absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
151 /* define LIBJPEG_ENCAP_EXTERNAL */
152 #define SETJMP(jbuf) setjmp(jbuf)
153 #define LONGJMP(jbuf,code) longjmp(jbuf,code)
154 #define JMP_BUF jmp_buf
155 #define OJPEG_BUFFER 2048
156 /* define EGYPTIANWALK */
158 #define JPEG_MARKER_SOF0 0xC0
159 #define JPEG_MARKER_SOF1 0xC1
160 #define JPEG_MARKER_SOF3 0xC3
161 #define JPEG_MARKER_DHT 0xC4
162 #define JPEG_MARKER_RST0 0XD0
163 #define JPEG_MARKER_SOI 0xD8
164 #define JPEG_MARKER_EOI 0xD9
165 #define JPEG_MARKER_SOS 0xDA
166 #define JPEG_MARKER_DQT 0xDB
167 #define JPEG_MARKER_DRI 0xDD
168 #define JPEG_MARKER_APP0 0xE0
169 #define JPEG_MARKER_COM 0xFE
171 #define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
172 #define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
173 #define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
174 #define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
175 #define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
176 #define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
177 #define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
179 static const TIFFField ojpegFields
[] = {
180 {TIFFTAG_JPEGIFOFFSET
,1,1,TIFF_LONG8
,0,TIFF_SETGET_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGINTERCHANGEFORMAT
,TRUE
,FALSE
,"JpegInterchangeFormat",NULL
},
181 {TIFFTAG_JPEGIFBYTECOUNT
,1,1,TIFF_LONG8
,0,TIFF_SETGET_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH
,TRUE
,FALSE
,"JpegInterchangeFormatLength",NULL
},
182 {TIFFTAG_JPEGQTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGQTABLES
,FALSE
,TRUE
,"JpegQTables",NULL
},
183 {TIFFTAG_JPEGDCTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGDCTABLES
,FALSE
,TRUE
,"JpegDcTables",NULL
},
184 {TIFFTAG_JPEGACTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGACTABLES
,FALSE
,TRUE
,"JpegAcTables",NULL
},
185 {TIFFTAG_JPEGPROC
,1,1,TIFF_SHORT
,0,TIFF_SETGET_UINT16
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGPROC
,FALSE
,FALSE
,"JpegProc",NULL
},
186 {TIFFTAG_JPEGRESTARTINTERVAL
,1,1,TIFF_SHORT
,0,TIFF_SETGET_UINT16
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGRESTARTINTERVAL
,FALSE
,FALSE
,"JpegRestartInterval",NULL
},
189 #ifndef LIBJPEG_ENCAP_EXTERNAL
193 /* We undefine FAR to avoid conflict with JPEG definition */
200 Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
201 not defined. Unfortunately, the MinGW and Borland compilers include
202 a typedef for INT32, which causes a conflict. MSVC does not include
203 a conficting typedef given the headers which are included.
205 #if defined(__BORLANDC__) || defined(__MINGW32__)
209 /* Define "boolean" as unsigned char, not int, per Windows custom. */
210 #if defined(__WIN32__) && !defined(__MINGW32__)
211 # ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
212 typedef unsigned char boolean
;
214 # define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
220 #ifndef HAVE_WXJPEG_BOOLEAN
221 typedef boolean wxjpeg_boolean
;
224 typedef struct jpeg_error_mgr jpeg_error_mgr
;
225 typedef struct jpeg_common_struct jpeg_common_struct
;
226 typedef struct jpeg_decompress_struct jpeg_decompress_struct
;
227 typedef struct jpeg_source_mgr jpeg_source_mgr
;
231 osibsJpegInterchangeFormat
,
234 } OJPEGStateInBufferSource
;
238 ososQTable0
,ososQTable1
,ososQTable2
,ososQTable3
,
239 ososDcTable0
,ososDcTable1
,ososDcTable2
,ososDcTable3
,
240 ososAcTable0
,ososAcTable1
,ososAcTable2
,ososAcTable3
,
247 } OJPEGStateOutState
;
251 #ifndef LIBJPEG_ENCAP_EXTERNAL
254 TIFFVGetMethod vgetparent
;
255 TIFFVSetMethod vsetparent
;
256 TIFFPrintMethod printdir
;
261 uint32 strile_length
;
262 uint32 strile_length_total
;
263 uint8 samples_per_pixel
;
264 uint8 plane_sample_offset
;
265 uint8 samples_per_pixel_per_plane
;
266 uint64 jpeg_interchange_format
;
267 uint64 jpeg_interchange_format_length
;
269 uint8 subsamplingcorrect
;
270 uint8 subsamplingcorrect_done
;
271 uint8 subsampling_tag
;
272 uint8 subsampling_hor
;
273 uint8 subsampling_ver
;
274 uint8 subsampling_force_desubsampling_inside_decompression
;
275 uint8 qtable_offset_count
;
276 uint8 dctable_offset_count
;
277 uint8 actable_offset_count
;
278 uint64 qtable_offset
[3];
279 uint64 dctable_offset
[3];
280 uint64 actable_offset
[3];
284 uint16 restart_interval
;
297 OJPEGStateInBufferSource in_buffer_source
;
298 uint32 in_buffer_next_strile
;
299 uint64 in_buffer_file_pos
;
300 uint64 in_buffer_file_togo
;
302 uint8 readheader_done
;
303 uint8 writeheader_done
;
304 uint16 write_cursample
;
305 uint32 write_curstrile
;
306 uint8 libjpeg_session_active
;
307 uint8 libjpeg_jpeg_query_style
;
308 jpeg_error_mgr libjpeg_jpeg_error_mgr
;
309 jpeg_decompress_struct libjpeg_jpeg_decompress_struct
;
310 jpeg_source_mgr libjpeg_jpeg_source_mgr
;
311 uint8 subsampling_convert_log
;
312 uint32 subsampling_convert_ylinelen
;
313 uint32 subsampling_convert_ylines
;
314 uint32 subsampling_convert_clinelen
;
315 uint32 subsampling_convert_clines
;
316 uint32 subsampling_convert_ybuflen
;
317 uint32 subsampling_convert_cbuflen
;
318 uint32 subsampling_convert_ycbcrbuflen
;
319 uint8
* subsampling_convert_ycbcrbuf
;
320 uint8
* subsampling_convert_ybuf
;
321 uint8
* subsampling_convert_cbbuf
;
322 uint8
* subsampling_convert_crbuf
;
323 uint32 subsampling_convert_ycbcrimagelen
;
324 uint8
** subsampling_convert_ycbcrimage
;
325 uint32 subsampling_convert_clinelenout
;
326 uint32 subsampling_convert_state
;
327 uint32 bytes_per_line
; /* if the codec outputs subsampled data, a 'line' in bytes_per_line */
328 uint32 lines_per_strile
; /* and lines_per_strile means subsampling_ver desubsampled rows */
329 OJPEGStateInBufferSource in_buffer_source
;
330 uint32 in_buffer_next_strile
;
331 uint32 in_buffer_strile_count
;
332 uint64 in_buffer_file_pos
;
333 uint8 in_buffer_file_pos_log
;
334 uint64 in_buffer_file_togo
;
335 uint16 in_buffer_togo
;
336 uint8
* in_buffer_cur
;
337 uint8 in_buffer
[OJPEG_BUFFER
];
338 OJPEGStateOutState out_state
;
339 uint8 out_buffer
[OJPEG_BUFFER
];
343 static int OJPEGVGetField(TIFF
* tif
, uint32 tag
, va_list ap
);
344 static int OJPEGVSetField(TIFF
* tif
, uint32 tag
, va_list ap
);
345 static void OJPEGPrintDir(TIFF
* tif
, FILE* fd
, long flags
);
347 static int OJPEGFixupTags(TIFF
* tif
);
348 static int OJPEGSetupDecode(TIFF
* tif
);
349 static int OJPEGPreDecode(TIFF
* tif
, uint16 s
);
350 static int OJPEGPreDecodeSkipRaw(TIFF
* tif
);
351 static int OJPEGPreDecodeSkipScanlines(TIFF
* tif
);
352 static int OJPEGDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
);
353 static int OJPEGDecodeRaw(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
354 static int OJPEGDecodeScanlines(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
355 static void OJPEGPostDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
356 static int OJPEGSetupEncode(TIFF
* tif
);
357 static int OJPEGPreEncode(TIFF
* tif
, uint16 s
);
358 static int OJPEGEncode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
);
359 static int OJPEGPostEncode(TIFF
* tif
);
360 static void OJPEGCleanup(TIFF
* tif
);
362 static void OJPEGSubsamplingCorrect(TIFF
* tif
);
363 static int OJPEGReadHeaderInfo(TIFF
* tif
);
364 static int OJPEGReadSecondarySos(TIFF
* tif
, uint16 s
);
365 static int OJPEGWriteHeaderInfo(TIFF
* tif
);
366 static void OJPEGLibjpegSessionAbort(TIFF
* tif
);
368 static int OJPEGReadHeaderInfoSec(TIFF
* tif
);
369 static int OJPEGReadHeaderInfoSecStreamDri(TIFF
* tif
);
370 static int OJPEGReadHeaderInfoSecStreamDqt(TIFF
* tif
);
371 static int OJPEGReadHeaderInfoSecStreamDht(TIFF
* tif
);
372 static int OJPEGReadHeaderInfoSecStreamSof(TIFF
* tif
, uint8 marker_id
);
373 static int OJPEGReadHeaderInfoSecStreamSos(TIFF
* tif
);
374 static int OJPEGReadHeaderInfoSecTablesQTable(TIFF
* tif
);
375 static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF
* tif
);
376 static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF
* tif
);
378 static int OJPEGReadBufferFill(OJPEGState
* sp
);
379 static int OJPEGReadByte(OJPEGState
* sp
, uint8
* byte
);
380 static int OJPEGReadBytePeek(OJPEGState
* sp
, uint8
* byte
);
381 static void OJPEGReadByteAdvance(OJPEGState
* sp
);
382 static int OJPEGReadWord(OJPEGState
* sp
, uint16
* word
);
383 static int OJPEGReadBlock(OJPEGState
* sp
, uint16 len
, void* mem
);
384 static void OJPEGReadSkip(OJPEGState
* sp
, uint16 len
);
386 static int OJPEGWriteStream(TIFF
* tif
, void** mem
, uint32
* len
);
387 static void OJPEGWriteStreamSoi(TIFF
* tif
, void** mem
, uint32
* len
);
388 static void OJPEGWriteStreamQTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
389 static void OJPEGWriteStreamDcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
390 static void OJPEGWriteStreamAcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
391 static void OJPEGWriteStreamDri(TIFF
* tif
, void** mem
, uint32
* len
);
392 static void OJPEGWriteStreamSof(TIFF
* tif
, void** mem
, uint32
* len
);
393 static void OJPEGWriteStreamSos(TIFF
* tif
, void** mem
, uint32
* len
);
394 static int OJPEGWriteStreamCompressed(TIFF
* tif
, void** mem
, uint32
* len
);
395 static void OJPEGWriteStreamRst(TIFF
* tif
, void** mem
, uint32
* len
);
396 static void OJPEGWriteStreamEoi(TIFF
* tif
, void** mem
, uint32
* len
);
398 #ifdef LIBJPEG_ENCAP_EXTERNAL
399 extern int jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
400 extern int jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
);
401 extern int jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
402 extern int jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
);
403 extern int jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
);
404 extern void jpeg_encap_unwind(TIFF
* tif
);
406 static int jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* j
);
407 static int jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
);
408 static int jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
409 static int jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
);
410 static int jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
);
411 static void jpeg_encap_unwind(TIFF
* tif
);
414 static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct
* cinfo
);
415 static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct
* cinfo
);
416 static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct
* cinfo
);
417 static wxjpeg_boolean
OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct
* cinfo
);
418 static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct
* cinfo
, long num_bytes
);
419 static wxjpeg_boolean
OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct
* cinfo
, int desired
);
420 static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct
* cinfo
);
423 TIFFInitOJPEG(TIFF
* tif
, int scheme
)
425 static const char module[]="TIFFInitOJPEG";
428 assert(scheme
==COMPRESSION_OJPEG
);
431 * Merge codec-specific tag information.
433 if (!_TIFFMergeFields(tif
, ojpegFields
, TIFFArrayCount(ojpegFields
))) {
434 TIFFErrorExt(tif
->tif_clientdata
, module,
435 "Merging Old JPEG codec-specific tags failed");
440 sp
=_TIFFmalloc(sizeof(OJPEGState
));
443 TIFFErrorExt(tif
->tif_clientdata
,module,"No space for OJPEG state block");
446 _TIFFmemset(sp
,0,sizeof(OJPEGState
));
449 sp
->subsampling_hor
=2;
450 sp
->subsampling_ver
=2;
451 TIFFSetField(tif
,TIFFTAG_YCBCRSUBSAMPLING
,2,2);
452 /* tif codec methods */
453 tif
->tif_fixuptags
=OJPEGFixupTags
;
454 tif
->tif_setupdecode
=OJPEGSetupDecode
;
455 tif
->tif_predecode
=OJPEGPreDecode
;
456 tif
->tif_postdecode
=OJPEGPostDecode
;
457 tif
->tif_decoderow
=OJPEGDecode
;
458 tif
->tif_decodestrip
=OJPEGDecode
;
459 tif
->tif_decodetile
=OJPEGDecode
;
460 tif
->tif_setupencode
=OJPEGSetupEncode
;
461 tif
->tif_preencode
=OJPEGPreEncode
;
462 tif
->tif_postencode
=OJPEGPostEncode
;
463 tif
->tif_encoderow
=OJPEGEncode
;
464 tif
->tif_encodestrip
=OJPEGEncode
;
465 tif
->tif_encodetile
=OJPEGEncode
;
466 tif
->tif_cleanup
=OJPEGCleanup
;
467 tif
->tif_data
=(uint8
*)sp
;
468 /* tif tag methods */
469 sp
->vgetparent
=tif
->tif_tagmethods
.vgetfield
;
470 tif
->tif_tagmethods
.vgetfield
=OJPEGVGetField
;
471 sp
->vsetparent
=tif
->tif_tagmethods
.vsetfield
;
472 tif
->tif_tagmethods
.vsetfield
=OJPEGVSetField
;
473 sp
->printdir
=tif
->tif_tagmethods
.printdir
;
474 tif
->tif_tagmethods
.printdir
=OJPEGPrintDir
;
475 /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
476 Some others do, but have totally meaningless or corrupt values
477 in these tags. In these cases, the JpegInterchangeFormat stream is
478 reliable. In any case, this decoder reads the compressed data itself,
479 from the most reliable locations, and we need to notify encapsulating
480 LibTiff not to read raw strips or tiles for us. */
481 tif
->tif_flags
|=TIFF_NOREADRAW
;
486 OJPEGVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
488 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
491 case TIFFTAG_JPEGIFOFFSET
:
492 *va_arg(ap
,uint64
*)=(uint64
)sp
->jpeg_interchange_format
;
494 case TIFFTAG_JPEGIFBYTECOUNT
:
495 *va_arg(ap
,uint64
*)=(uint64
)sp
->jpeg_interchange_format_length
;
497 case TIFFTAG_YCBCRSUBSAMPLING
:
498 if (sp
->subsamplingcorrect_done
==0)
499 OJPEGSubsamplingCorrect(tif
);
500 *va_arg(ap
,uint16
*)=(uint16
)sp
->subsampling_hor
;
501 *va_arg(ap
,uint16
*)=(uint16
)sp
->subsampling_ver
;
503 case TIFFTAG_JPEGQTABLES
:
504 *va_arg(ap
,uint32
*)=(uint32
)sp
->qtable_offset_count
;
505 *va_arg(ap
,void**)=(void*)sp
->qtable_offset
;
507 case TIFFTAG_JPEGDCTABLES
:
508 *va_arg(ap
,uint32
*)=(uint32
)sp
->dctable_offset_count
;
509 *va_arg(ap
,void**)=(void*)sp
->dctable_offset
;
511 case TIFFTAG_JPEGACTABLES
:
512 *va_arg(ap
,uint32
*)=(uint32
)sp
->actable_offset_count
;
513 *va_arg(ap
,void**)=(void*)sp
->actable_offset
;
515 case TIFFTAG_JPEGPROC
:
516 *va_arg(ap
,uint16
*)=(uint16
)sp
->jpeg_proc
;
518 case TIFFTAG_JPEGRESTARTINTERVAL
:
519 *va_arg(ap
,uint16
*)=sp
->restart_interval
;
522 return (*sp
->vgetparent
)(tif
,tag
,ap
);
528 OJPEGVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
530 static const char module[]="OJPEGVSetField";
531 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
537 case TIFFTAG_JPEGIFOFFSET
:
538 sp
->jpeg_interchange_format
=(uint64
)va_arg(ap
,uint64
);
540 case TIFFTAG_JPEGIFBYTECOUNT
:
541 sp
->jpeg_interchange_format_length
=(uint64
)va_arg(ap
,uint64
);
543 case TIFFTAG_YCBCRSUBSAMPLING
:
544 sp
->subsampling_tag
=1;
545 sp
->subsampling_hor
=(uint8
)va_arg(ap
,uint16_vap
);
546 sp
->subsampling_ver
=(uint8
)va_arg(ap
,uint16_vap
);
547 tif
->tif_dir
.td_ycbcrsubsampling
[0]=sp
->subsampling_hor
;
548 tif
->tif_dir
.td_ycbcrsubsampling
[1]=sp
->subsampling_ver
;
550 case TIFFTAG_JPEGQTABLES
:
551 ma
=(uint32
)va_arg(ap
,uint32
);
556 TIFFErrorExt(tif
->tif_clientdata
,module,"JpegQTables tag has incorrect count");
559 sp
->qtable_offset_count
=(uint8
)ma
;
560 mb
=(uint64
*)va_arg(ap
,uint64
*);
562 sp
->qtable_offset
[n
]=mb
[n
];
565 case TIFFTAG_JPEGDCTABLES
:
566 ma
=(uint32
)va_arg(ap
,uint32
);
571 TIFFErrorExt(tif
->tif_clientdata
,module,"JpegDcTables tag has incorrect count");
574 sp
->dctable_offset_count
=(uint8
)ma
;
575 mb
=(uint64
*)va_arg(ap
,uint64
*);
577 sp
->dctable_offset
[n
]=mb
[n
];
580 case TIFFTAG_JPEGACTABLES
:
581 ma
=(uint32
)va_arg(ap
,uint32
);
586 TIFFErrorExt(tif
->tif_clientdata
,module,"JpegAcTables tag has incorrect count");
589 sp
->actable_offset_count
=(uint8
)ma
;
590 mb
=(uint64
*)va_arg(ap
,uint64
*);
592 sp
->actable_offset
[n
]=mb
[n
];
595 case TIFFTAG_JPEGPROC
:
596 sp
->jpeg_proc
=(uint8
)va_arg(ap
,uint16_vap
);
598 case TIFFTAG_JPEGRESTARTINTERVAL
:
599 sp
->restart_interval
=(uint16
)va_arg(ap
,uint16_vap
);
602 return (*sp
->vsetparent
)(tif
,tag
,ap
);
604 TIFFSetFieldBit(tif
,TIFFFieldWithTag(tif
,tag
)->field_bit
);
605 tif
->tif_flags
|=TIFF_DIRTYDIRECT
;
610 OJPEGPrintDir(TIFF
* tif
, FILE* fd
, long flags
)
612 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
616 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGINTERCHANGEFORMAT
))
617 fprintf(fd
," JpegInterchangeFormat: " TIFF_UINT64_FORMAT
"\n",(TIFF_UINT64_T
)sp
->jpeg_interchange_format
);
618 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH
))
619 fprintf(fd
," JpegInterchangeFormatLength: " TIFF_UINT64_FORMAT
"\n",(TIFF_UINT64_T
)sp
->jpeg_interchange_format_length
);
620 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGQTABLES
))
622 fprintf(fd
," JpegQTables:");
623 for (m
=0; m
<sp
->qtable_offset_count
; m
++)
624 fprintf(fd
," " TIFF_UINT64_FORMAT
,(TIFF_UINT64_T
)sp
->qtable_offset
[m
]);
627 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGDCTABLES
))
629 fprintf(fd
," JpegDcTables:");
630 for (m
=0; m
<sp
->dctable_offset_count
; m
++)
631 fprintf(fd
," " TIFF_UINT64_FORMAT
,(TIFF_UINT64_T
)sp
->dctable_offset
[m
]);
634 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGACTABLES
))
636 fprintf(fd
," JpegAcTables:");
637 for (m
=0; m
<sp
->actable_offset_count
; m
++)
638 fprintf(fd
," " TIFF_UINT64_FORMAT
,(TIFF_UINT64_T
)sp
->actable_offset
[m
]);
641 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGPROC
))
642 fprintf(fd
," JpegProc: %u\n",(unsigned int)sp
->jpeg_proc
);
643 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGRESTARTINTERVAL
))
644 fprintf(fd
," JpegRestartInterval: %u\n",(unsigned int)sp
->restart_interval
);
646 (*sp
->printdir
)(tif
, fd
, flags
);
650 OJPEGFixupTags(TIFF
* tif
)
657 OJPEGSetupDecode(TIFF
* tif
)
659 static const char module[]="OJPEGSetupDecode";
660 TIFFWarningExt(tif
->tif_clientdata
,module,"Depreciated and troublesome old-style JPEG compression mode, please convert to new-style JPEG compression and notify vendor of writing software");
665 OJPEGPreDecode(TIFF
* tif
, uint16 s
)
667 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
669 if (sp
->subsamplingcorrect_done
==0)
670 OJPEGSubsamplingCorrect(tif
);
671 if (sp
->readheader_done
==0)
673 if (OJPEGReadHeaderInfo(tif
)==0)
676 if (sp
->sos_end
[s
].log
==0)
678 if (OJPEGReadSecondarySos(tif
,s
)==0)
685 if ((sp
->writeheader_done
!=0) && ((sp
->write_cursample
!=s
) || (sp
->write_curstrile
>m
)))
687 if (sp
->libjpeg_session_active
!=0)
688 OJPEGLibjpegSessionAbort(tif
);
689 sp
->writeheader_done
=0;
691 if (sp
->writeheader_done
==0)
693 sp
->plane_sample_offset
=(uint8
)s
;
694 sp
->write_cursample
=s
;
695 sp
->write_curstrile
=s
*tif
->tif_dir
.td_stripsperimage
;
696 if ((sp
->in_buffer_file_pos_log
==0) ||
697 (sp
->in_buffer_file_pos
-sp
->in_buffer_togo
!=sp
->sos_end
[s
].in_buffer_file_pos
))
699 sp
->in_buffer_source
=sp
->sos_end
[s
].in_buffer_source
;
700 sp
->in_buffer_next_strile
=sp
->sos_end
[s
].in_buffer_next_strile
;
701 sp
->in_buffer_file_pos
=sp
->sos_end
[s
].in_buffer_file_pos
;
702 sp
->in_buffer_file_pos_log
=0;
703 sp
->in_buffer_file_togo
=sp
->sos_end
[s
].in_buffer_file_togo
;
704 sp
->in_buffer_togo
=0;
707 if (OJPEGWriteHeaderInfo(tif
)==0)
710 while (sp
->write_curstrile
<m
)
712 if (sp
->libjpeg_jpeg_query_style
==0)
714 if (OJPEGPreDecodeSkipRaw(tif
)==0)
719 if (OJPEGPreDecodeSkipScanlines(tif
)==0)
722 sp
->write_curstrile
++;
728 OJPEGPreDecodeSkipRaw(TIFF
* tif
)
730 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
732 m
=sp
->lines_per_strile
;
733 if (sp
->subsampling_convert_state
!=0)
735 if (sp
->subsampling_convert_clines
-sp
->subsampling_convert_state
>=m
)
737 sp
->subsampling_convert_state
+=m
;
738 if (sp
->subsampling_convert_state
==sp
->subsampling_convert_clines
)
739 sp
->subsampling_convert_state
=0;
742 m
-=sp
->subsampling_convert_clines
-sp
->subsampling_convert_state
;
743 sp
->subsampling_convert_state
=0;
745 while (m
>=sp
->subsampling_convert_clines
)
747 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
749 m
-=sp
->subsampling_convert_clines
;
753 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
755 sp
->subsampling_convert_state
=m
;
761 OJPEGPreDecodeSkipScanlines(TIFF
* tif
)
763 static const char module[]="OJPEGPreDecodeSkipScanlines";
764 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
766 if (sp
->skip_buffer
==NULL
)
768 sp
->skip_buffer
=_TIFFmalloc(sp
->bytes_per_line
);
769 if (sp
->skip_buffer
==NULL
)
771 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
775 for (m
=0; m
<sp
->lines_per_strile
; m
++)
777 if (jpeg_read_scanlines_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),&sp
->skip_buffer
,1)==0)
784 OJPEGDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
)
786 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
788 if (sp
->libjpeg_jpeg_query_style
==0)
790 if (OJPEGDecodeRaw(tif
,buf
,cc
)==0)
795 if (OJPEGDecodeScanlines(tif
,buf
,cc
)==0)
802 OJPEGDecodeRaw(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
804 static const char module[]="OJPEGDecodeRaw";
805 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
815 if (cc%sp
->bytes_per_line
!=0)
817 TIFFErrorExt(tif
->tif_clientdata
,module,"Fractional scanline not read");
825 if (sp
->subsampling_convert_state
==0)
827 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
830 oy
=sp
->subsampling_convert_ybuf
+sp
->subsampling_convert_state
*sp
->subsampling_ver
*sp
->subsampling_convert_ylinelen
;
831 ocb
=sp
->subsampling_convert_cbbuf
+sp
->subsampling_convert_state
*sp
->subsampling_convert_clinelen
;
832 ocr
=sp
->subsampling_convert_crbuf
+sp
->subsampling_convert_state
*sp
->subsampling_convert_clinelen
;
834 for (q
=0; q
<sp
->subsampling_convert_clinelenout
; q
++)
837 for (sy
=0; sy
<sp
->subsampling_ver
; sy
++)
839 for (sx
=0; sx
<sp
->subsampling_hor
; sx
++)
841 r
+=sp
->subsampling_convert_ylinelen
-sp
->subsampling_hor
;
843 oy
+=sp
->subsampling_hor
;
847 sp
->subsampling_convert_state
++;
848 if (sp
->subsampling_convert_state
==sp
->subsampling_convert_clines
)
849 sp
->subsampling_convert_state
=0;
850 m
+=sp
->bytes_per_line
;
851 n
-=sp
->bytes_per_line
;
857 OJPEGDecodeScanlines(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
859 static const char module[]="OJPEGDecodeScanlines";
860 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
863 if (cc%sp
->bytes_per_line
!=0)
865 TIFFErrorExt(tif
->tif_clientdata
,module,"Fractional scanline not read");
873 if (jpeg_read_scanlines_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),&m
,1)==0)
875 m
+=sp
->bytes_per_line
;
876 n
-=sp
->bytes_per_line
;
882 OJPEGPostDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
884 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
887 sp
->write_curstrile
++;
888 if (sp
->write_curstrile%tif
->tif_dir
.td_stripsperimage
==0)
890 assert(sp
->libjpeg_session_active
!=0);
891 OJPEGLibjpegSessionAbort(tif
);
892 sp
->writeheader_done
=0;
897 OJPEGSetupEncode(TIFF
* tif
)
899 static const char module[]="OJPEGSetupEncode";
900 TIFFErrorExt(tif
->tif_clientdata
,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
905 OJPEGPreEncode(TIFF
* tif
, uint16 s
)
907 static const char module[]="OJPEGPreEncode";
909 TIFFErrorExt(tif
->tif_clientdata
,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
914 OJPEGEncode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
)
916 static const char module[]="OJPEGEncode";
920 TIFFErrorExt(tif
->tif_clientdata
,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
925 OJPEGPostEncode(TIFF
* tif
)
927 static const char module[]="OJPEGPostEncode";
928 TIFFErrorExt(tif
->tif_clientdata
,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
933 OJPEGCleanup(TIFF
* tif
)
935 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
938 tif
->tif_tagmethods
.vgetfield
=sp
->vgetparent
;
939 tif
->tif_tagmethods
.vsetfield
=sp
->vsetparent
;
940 tif
->tif_tagmethods
.printdir
=sp
->printdir
;
941 if (sp
->qtable
[0]!=0)
942 _TIFFfree(sp
->qtable
[0]);
943 if (sp
->qtable
[1]!=0)
944 _TIFFfree(sp
->qtable
[1]);
945 if (sp
->qtable
[2]!=0)
946 _TIFFfree(sp
->qtable
[2]);
947 if (sp
->qtable
[3]!=0)
948 _TIFFfree(sp
->qtable
[3]);
949 if (sp
->dctable
[0]!=0)
950 _TIFFfree(sp
->dctable
[0]);
951 if (sp
->dctable
[1]!=0)
952 _TIFFfree(sp
->dctable
[1]);
953 if (sp
->dctable
[2]!=0)
954 _TIFFfree(sp
->dctable
[2]);
955 if (sp
->dctable
[3]!=0)
956 _TIFFfree(sp
->dctable
[3]);
957 if (sp
->actable
[0]!=0)
958 _TIFFfree(sp
->actable
[0]);
959 if (sp
->actable
[1]!=0)
960 _TIFFfree(sp
->actable
[1]);
961 if (sp
->actable
[2]!=0)
962 _TIFFfree(sp
->actable
[2]);
963 if (sp
->actable
[3]!=0)
964 _TIFFfree(sp
->actable
[3]);
965 if (sp
->libjpeg_session_active
!=0)
966 OJPEGLibjpegSessionAbort(tif
);
967 if (sp
->subsampling_convert_ycbcrbuf
!=0)
968 _TIFFfree(sp
->subsampling_convert_ycbcrbuf
);
969 if (sp
->subsampling_convert_ycbcrimage
!=0)
970 _TIFFfree(sp
->subsampling_convert_ycbcrimage
);
971 if (sp
->skip_buffer
!=0)
972 _TIFFfree(sp
->skip_buffer
);
975 _TIFFSetDefaultCompressionState(tif
);
980 OJPEGSubsamplingCorrect(TIFF
* tif
)
982 static const char module[]="OJPEGSubsamplingCorrect";
983 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
986 _TIFFFillStriles( tif
);
988 assert(sp
->subsamplingcorrect_done
==0);
989 if ((tif
->tif_dir
.td_samplesperpixel
!=3) || ((tif
->tif_dir
.td_photometric
!=PHOTOMETRIC_YCBCR
) &&
990 (tif
->tif_dir
.td_photometric
!=PHOTOMETRIC_ITULAB
)))
992 if (sp
->subsampling_tag
!=0)
993 TIFFWarningExt(tif
->tif_clientdata
,module,"Subsampling tag not appropriate for this Photometric and/or SamplesPerPixel");
994 sp
->subsampling_hor
=1;
995 sp
->subsampling_ver
=1;
996 sp
->subsampling_force_desubsampling_inside_decompression
=0;
1000 sp
->subsamplingcorrect_done
=1;
1001 mh
=sp
->subsampling_hor
;
1002 mv
=sp
->subsampling_ver
;
1003 sp
->subsamplingcorrect
=1;
1004 OJPEGReadHeaderInfoSec(tif
);
1005 if (sp
->subsampling_force_desubsampling_inside_decompression
!=0)
1007 sp
->subsampling_hor
=1;
1008 sp
->subsampling_ver
=1;
1010 sp
->subsamplingcorrect
=0;
1011 if (((sp
->subsampling_hor
!=mh
) || (sp
->subsampling_ver
!=mv
)) && (sp
->subsampling_force_desubsampling_inside_decompression
==0))
1013 if (sp
->subsampling_tag
==0)
1014 TIFFWarningExt(tif
->tif_clientdata
,module,"Subsampling tag is not set, yet subsampling inside JPEG data [%d,%d] does not match default values [2,2]; assuming subsampling inside JPEG data is correct",sp
->subsampling_hor
,sp
->subsampling_ver
);
1016 TIFFWarningExt(tif
->tif_clientdata
,module,"Subsampling inside JPEG data [%d,%d] does not match subsampling tag values [%d,%d]; assuming subsampling inside JPEG data is correct",sp
->subsampling_hor
,sp
->subsampling_ver
,mh
,mv
);
1018 if (sp
->subsampling_force_desubsampling_inside_decompression
!=0)
1020 if (sp
->subsampling_tag
==0)
1021 TIFFWarningExt(tif
->tif_clientdata
,module,"Subsampling tag is not set, yet subsampling inside JPEG data does not match default values [2,2] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression");
1023 TIFFWarningExt(tif
->tif_clientdata
,module,"Subsampling inside JPEG data does not match subsampling tag values [%d,%d] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression",mh
,mv
);
1025 if (sp
->subsampling_force_desubsampling_inside_decompression
==0)
1027 if (sp
->subsampling_hor
<sp
->subsampling_ver
)
1028 TIFFWarningExt(tif
->tif_clientdata
,module,"Subsampling values [%d,%d] are not allowed in TIFF",sp
->subsampling_hor
,sp
->subsampling_ver
);
1031 sp
->subsamplingcorrect_done
=1;
1035 OJPEGReadHeaderInfo(TIFF
* tif
)
1037 static const char module[]="OJPEGReadHeaderInfo";
1038 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1039 assert(sp
->readheader_done
==0);
1040 sp
->image_width
=tif
->tif_dir
.td_imagewidth
;
1041 sp
->image_length
=tif
->tif_dir
.td_imagelength
;
1044 sp
->strile_width
=tif
->tif_dir
.td_tilewidth
;
1045 sp
->strile_length
=tif
->tif_dir
.td_tilelength
;
1046 sp
->strile_length_total
=((sp
->image_length
+sp
->strile_length
-1)/sp
->strile_length
)*sp
->strile_length
;
1050 sp
->strile_width
=sp
->image_width
;
1051 sp
->strile_length
=tif
->tif_dir
.td_rowsperstrip
;
1052 sp
->strile_length_total
=sp
->image_length
;
1054 if (tif
->tif_dir
.td_samplesperpixel
==1)
1056 sp
->samples_per_pixel
=1;
1057 sp
->plane_sample_offset
=0;
1058 sp
->samples_per_pixel_per_plane
=sp
->samples_per_pixel
;
1059 sp
->subsampling_hor
=1;
1060 sp
->subsampling_ver
=1;
1064 if (tif
->tif_dir
.td_samplesperpixel
!=3)
1066 TIFFErrorExt(tif
->tif_clientdata
,module,"SamplesPerPixel %d not supported for this compression scheme",sp
->samples_per_pixel
);
1069 sp
->samples_per_pixel
=3;
1070 sp
->plane_sample_offset
=0;
1071 if (tif
->tif_dir
.td_planarconfig
==PLANARCONFIG_CONTIG
)
1072 sp
->samples_per_pixel_per_plane
=3;
1074 sp
->samples_per_pixel_per_plane
=1;
1076 if (sp
->strile_length
<sp
->image_length
)
1078 if (sp
->strile_length
%(sp
->subsampling_ver
*8)!=0)
1080 TIFFErrorExt(tif
->tif_clientdata
,module,"Incompatible vertical subsampling and image strip/tile length");
1083 sp
->restart_interval
=((sp
->strile_width
+sp
->subsampling_hor
*8-1)/(sp
->subsampling_hor
*8))*(sp
->strile_length
/(sp
->subsampling_ver
*8));
1085 if (OJPEGReadHeaderInfoSec(tif
)==0)
1087 sp
->sos_end
[0].log
=1;
1088 sp
->sos_end
[0].in_buffer_source
=sp
->in_buffer_source
;
1089 sp
->sos_end
[0].in_buffer_next_strile
=sp
->in_buffer_next_strile
;
1090 sp
->sos_end
[0].in_buffer_file_pos
=sp
->in_buffer_file_pos
-sp
->in_buffer_togo
;
1091 sp
->sos_end
[0].in_buffer_file_togo
=sp
->in_buffer_file_togo
+sp
->in_buffer_togo
;
1092 sp
->readheader_done
=1;
1097 OJPEGReadSecondarySos(TIFF
* tif
, uint16 s
)
1099 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1103 assert(sp
->sos_end
[0].log
!=0);
1104 assert(sp
->sos_end
[s
].log
==0);
1105 sp
->plane_sample_offset
=s
-1;
1106 while(sp
->sos_end
[sp
->plane_sample_offset
].log
==0)
1107 sp
->plane_sample_offset
--;
1108 sp
->in_buffer_source
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_source
;
1109 sp
->in_buffer_next_strile
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_next_strile
;
1110 sp
->in_buffer_file_pos
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_pos
;
1111 sp
->in_buffer_file_pos_log
=0;
1112 sp
->in_buffer_file_togo
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_togo
;
1113 sp
->in_buffer_togo
=0;
1114 sp
->in_buffer_cur
=0;
1115 while(sp
->plane_sample_offset
<s
)
1119 if (OJPEGReadByte(sp
,&m
)==0)
1125 if (OJPEGReadByte(sp
,&m
)==0)
1130 if (m
==JPEG_MARKER_SOS
)
1134 sp
->plane_sample_offset
++;
1135 if (OJPEGReadHeaderInfoSecStreamSos(tif
)==0)
1137 sp
->sos_end
[sp
->plane_sample_offset
].log
=1;
1138 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_source
=sp
->in_buffer_source
;
1139 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_next_strile
=sp
->in_buffer_next_strile
;
1140 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_pos
=sp
->in_buffer_file_pos
-sp
->in_buffer_togo
;
1141 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_togo
=sp
->in_buffer_file_togo
+sp
->in_buffer_togo
;
1147 OJPEGWriteHeaderInfo(TIFF
* tif
)
1149 static const char module[]="OJPEGWriteHeaderInfo";
1150 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1153 /* if a previous attempt failed, don't try again */
1154 if (sp
->libjpeg_session_active
!= 0)
1156 sp
->out_state
=ososSoi
;
1157 sp
->restart_index
=0;
1158 jpeg_std_error(&(sp
->libjpeg_jpeg_error_mgr
));
1159 sp
->libjpeg_jpeg_error_mgr
.output_message
=OJPEGLibjpegJpegErrorMgrOutputMessage
;
1160 sp
->libjpeg_jpeg_error_mgr
.error_exit
=OJPEGLibjpegJpegErrorMgrErrorExit
;
1161 sp
->libjpeg_jpeg_decompress_struct
.err
=&(sp
->libjpeg_jpeg_error_mgr
);
1162 sp
->libjpeg_jpeg_decompress_struct
.client_data
=(void*)tif
;
1163 if (jpeg_create_decompress_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
))==0)
1165 sp
->libjpeg_session_active
=1;
1166 sp
->libjpeg_jpeg_source_mgr
.bytes_in_buffer
=0;
1167 sp
->libjpeg_jpeg_source_mgr
.init_source
=OJPEGLibjpegJpegSourceMgrInitSource
;
1168 sp
->libjpeg_jpeg_source_mgr
.fill_input_buffer
=OJPEGLibjpegJpegSourceMgrFillInputBuffer
;
1169 sp
->libjpeg_jpeg_source_mgr
.skip_input_data
=OJPEGLibjpegJpegSourceMgrSkipInputData
;
1170 sp
->libjpeg_jpeg_source_mgr
.resync_to_restart
=OJPEGLibjpegJpegSourceMgrResyncToRestart
;
1171 sp
->libjpeg_jpeg_source_mgr
.term_source
=OJPEGLibjpegJpegSourceMgrTermSource
;
1172 sp
->libjpeg_jpeg_decompress_struct
.src
=&(sp
->libjpeg_jpeg_source_mgr
);
1173 if (jpeg_read_header_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),1)==0)
1175 if ((sp
->subsampling_force_desubsampling_inside_decompression
==0) && (sp
->samples_per_pixel_per_plane
>1))
1177 sp
->libjpeg_jpeg_decompress_struct
.raw_data_out
=1;
1178 #if JPEG_LIB_VERSION >= 70
1179 sp
->libjpeg_jpeg_decompress_struct
.do_fancy_upsampling
=FALSE
;
1181 sp
->libjpeg_jpeg_query_style
=0;
1182 if (sp
->subsampling_convert_log
==0)
1184 assert(sp
->subsampling_convert_ycbcrbuf
==0);
1185 assert(sp
->subsampling_convert_ycbcrimage
==0);
1186 sp
->subsampling_convert_ylinelen
=((sp
->strile_width
+sp
->subsampling_hor
*8-1)/(sp
->subsampling_hor
*8)*sp
->subsampling_hor
*8);
1187 sp
->subsampling_convert_ylines
=sp
->subsampling_ver
*8;
1188 sp
->subsampling_convert_clinelen
=sp
->subsampling_convert_ylinelen
/sp
->subsampling_hor
;
1189 sp
->subsampling_convert_clines
=8;
1190 sp
->subsampling_convert_ybuflen
=sp
->subsampling_convert_ylinelen
*sp
->subsampling_convert_ylines
;
1191 sp
->subsampling_convert_cbuflen
=sp
->subsampling_convert_clinelen
*sp
->subsampling_convert_clines
;
1192 sp
->subsampling_convert_ycbcrbuflen
=sp
->subsampling_convert_ybuflen
+2*sp
->subsampling_convert_cbuflen
;
1193 sp
->subsampling_convert_ycbcrbuf
=_TIFFmalloc(sp
->subsampling_convert_ycbcrbuflen
);
1194 if (sp
->subsampling_convert_ycbcrbuf
==0)
1196 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1199 sp
->subsampling_convert_ybuf
=sp
->subsampling_convert_ycbcrbuf
;
1200 sp
->subsampling_convert_cbbuf
=sp
->subsampling_convert_ybuf
+sp
->subsampling_convert_ybuflen
;
1201 sp
->subsampling_convert_crbuf
=sp
->subsampling_convert_cbbuf
+sp
->subsampling_convert_cbuflen
;
1202 sp
->subsampling_convert_ycbcrimagelen
=3+sp
->subsampling_convert_ylines
+2*sp
->subsampling_convert_clines
;
1203 sp
->subsampling_convert_ycbcrimage
=_TIFFmalloc(sp
->subsampling_convert_ycbcrimagelen
*sizeof(uint8
*));
1204 if (sp
->subsampling_convert_ycbcrimage
==0)
1206 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1209 m
=sp
->subsampling_convert_ycbcrimage
;
1210 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3);
1211 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3+sp
->subsampling_convert_ylines
);
1212 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3+sp
->subsampling_convert_ylines
+sp
->subsampling_convert_clines
);
1213 for (n
=0; n
<sp
->subsampling_convert_ylines
; n
++)
1214 *m
++=sp
->subsampling_convert_ybuf
+n
*sp
->subsampling_convert_ylinelen
;
1215 for (n
=0; n
<sp
->subsampling_convert_clines
; n
++)
1216 *m
++=sp
->subsampling_convert_cbbuf
+n
*sp
->subsampling_convert_clinelen
;
1217 for (n
=0; n
<sp
->subsampling_convert_clines
; n
++)
1218 *m
++=sp
->subsampling_convert_crbuf
+n
*sp
->subsampling_convert_clinelen
;
1219 sp
->subsampling_convert_clinelenout
=((sp
->strile_width
+sp
->subsampling_hor
-1)/sp
->subsampling_hor
);
1220 sp
->subsampling_convert_state
=0;
1221 sp
->bytes_per_line
=sp
->subsampling_convert_clinelenout
*(sp
->subsampling_ver
*sp
->subsampling_hor
+2);
1222 sp
->lines_per_strile
=((sp
->strile_length
+sp
->subsampling_ver
-1)/sp
->subsampling_ver
);
1223 sp
->subsampling_convert_log
=1;
1228 sp
->libjpeg_jpeg_decompress_struct
.jpeg_color_space
=JCS_UNKNOWN
;
1229 sp
->libjpeg_jpeg_decompress_struct
.out_color_space
=JCS_UNKNOWN
;
1230 sp
->libjpeg_jpeg_query_style
=1;
1231 sp
->bytes_per_line
=sp
->samples_per_pixel_per_plane
*sp
->strile_width
;
1232 sp
->lines_per_strile
=sp
->strile_length
;
1234 if (jpeg_start_decompress_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
))==0)
1236 sp
->writeheader_done
=1;
1241 OJPEGLibjpegSessionAbort(TIFF
* tif
)
1243 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1244 assert(sp
->libjpeg_session_active
!=0);
1245 jpeg_destroy((jpeg_common_struct
*)(&(sp
->libjpeg_jpeg_decompress_struct
)));
1246 sp
->libjpeg_session_active
=0;
1250 OJPEGReadHeaderInfoSec(TIFF
* tif
)
1252 static const char module[]="OJPEGReadHeaderInfoSec";
1253 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1257 if (sp
->file_size
==0)
1258 sp
->file_size
=TIFFGetFileSize(tif
);
1259 if (sp
->jpeg_interchange_format
!=0)
1261 if (sp
->jpeg_interchange_format
>=sp
->file_size
)
1263 sp
->jpeg_interchange_format
=0;
1264 sp
->jpeg_interchange_format_length
=0;
1268 if ((sp
->jpeg_interchange_format_length
==0) || (sp
->jpeg_interchange_format
+sp
->jpeg_interchange_format_length
>sp
->file_size
))
1269 sp
->jpeg_interchange_format_length
=sp
->file_size
-sp
->jpeg_interchange_format
;
1272 sp
->in_buffer_source
=osibsNotSetYet
;
1273 sp
->in_buffer_next_strile
=0;
1274 sp
->in_buffer_strile_count
=tif
->tif_dir
.td_nstrips
;
1275 sp
->in_buffer_file_togo
=0;
1276 sp
->in_buffer_togo
=0;
1279 if (OJPEGReadBytePeek(sp
,&m
)==0)
1283 OJPEGReadByteAdvance(sp
);
1286 if (OJPEGReadByte(sp
,&m
)==0)
1291 case JPEG_MARKER_SOI
:
1292 /* this type of marker has no data, and should be skipped */
1294 case JPEG_MARKER_COM
:
1295 case JPEG_MARKER_APP0
:
1296 case JPEG_MARKER_APP0
+1:
1297 case JPEG_MARKER_APP0
+2:
1298 case JPEG_MARKER_APP0
+3:
1299 case JPEG_MARKER_APP0
+4:
1300 case JPEG_MARKER_APP0
+5:
1301 case JPEG_MARKER_APP0
+6:
1302 case JPEG_MARKER_APP0
+7:
1303 case JPEG_MARKER_APP0
+8:
1304 case JPEG_MARKER_APP0
+9:
1305 case JPEG_MARKER_APP0
+10:
1306 case JPEG_MARKER_APP0
+11:
1307 case JPEG_MARKER_APP0
+12:
1308 case JPEG_MARKER_APP0
+13:
1309 case JPEG_MARKER_APP0
+14:
1310 case JPEG_MARKER_APP0
+15:
1311 /* this type of marker has data, but it has no use to us (and no place here) and should be skipped */
1312 if (OJPEGReadWord(sp
,&n
)==0)
1316 if (sp
->subsamplingcorrect
==0)
1317 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt JPEG data");
1321 OJPEGReadSkip(sp
,n
-2);
1323 case JPEG_MARKER_DRI
:
1324 if (OJPEGReadHeaderInfoSecStreamDri(tif
)==0)
1327 case JPEG_MARKER_DQT
:
1328 if (OJPEGReadHeaderInfoSecStreamDqt(tif
)==0)
1331 case JPEG_MARKER_DHT
:
1332 if (OJPEGReadHeaderInfoSecStreamDht(tif
)==0)
1335 case JPEG_MARKER_SOF0
:
1336 case JPEG_MARKER_SOF1
:
1337 case JPEG_MARKER_SOF3
:
1338 if (OJPEGReadHeaderInfoSecStreamSof(tif
,m
)==0)
1340 if (sp
->subsamplingcorrect
!=0)
1343 case JPEG_MARKER_SOS
:
1344 if (sp
->subsamplingcorrect
!=0)
1346 assert(sp
->plane_sample_offset
==0);
1347 if (OJPEGReadHeaderInfoSecStreamSos(tif
)==0)
1351 TIFFErrorExt(tif
->tif_clientdata
,module,"Unknown marker type %d in JPEG data",m
);
1354 } while(m
!=JPEG_MARKER_SOS
);
1355 if (sp
->subsamplingcorrect
)
1359 if (OJPEGReadHeaderInfoSecTablesQTable(tif
)==0)
1361 sp
->sof_marker_id
=JPEG_MARKER_SOF0
;
1362 for (o
=0; o
<sp
->samples_per_pixel
; o
++)
1364 sp
->sof_hv
[0]=((sp
->subsampling_hor
<<4)|sp
->subsampling_ver
);
1365 for (o
=1; o
<sp
->samples_per_pixel
; o
++)
1367 sp
->sof_x
=sp
->strile_width
;
1368 sp
->sof_y
=sp
->strile_length_total
;
1370 if (OJPEGReadHeaderInfoSecTablesDcTable(tif
)==0)
1372 if (OJPEGReadHeaderInfoSecTablesAcTable(tif
)==0)
1374 for (o
=1; o
<sp
->samples_per_pixel
; o
++)
1381 OJPEGReadHeaderInfoSecStreamDri(TIFF
* tif
)
1383 /* this could easilly cause trouble in some cases... but no such cases have occured sofar */
1384 static const char module[]="OJPEGReadHeaderInfoSecStreamDri";
1385 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1387 if (OJPEGReadWord(sp
,&m
)==0)
1391 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DRI marker in JPEG data");
1394 if (OJPEGReadWord(sp
,&m
)==0)
1396 sp
->restart_interval
=m
;
1401 OJPEGReadHeaderInfoSecStreamDqt(TIFF
* tif
)
1403 /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1404 static const char module[]="OJPEGReadHeaderInfoSecStreamDqt";
1405 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1410 if (OJPEGReadWord(sp
,&m
)==0)
1414 if (sp
->subsamplingcorrect
==0)
1415 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DQT marker in JPEG data");
1418 if (sp
->subsamplingcorrect
!=0)
1419 OJPEGReadSkip(sp
,m
-2);
1427 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DQT marker in JPEG data");
1430 na
=sizeof(uint32
)+69;
1434 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1438 nb
[sizeof(uint32
)]=255;
1439 nb
[sizeof(uint32
)+1]=JPEG_MARKER_DQT
;
1440 nb
[sizeof(uint32
)+2]=0;
1441 nb
[sizeof(uint32
)+3]=67;
1442 if (OJPEGReadBlock(sp
,65,&nb
[sizeof(uint32
)+4])==0) {
1446 o
=nb
[sizeof(uint32
)+4]&15;
1449 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DQT marker in JPEG data");
1453 if (sp
->qtable
[o
]!=0)
1454 _TIFFfree(sp
->qtable
[o
]);
1463 OJPEGReadHeaderInfoSecStreamDht(TIFF
* tif
)
1465 /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1466 /* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
1467 static const char module[]="OJPEGReadHeaderInfoSecStreamDht";
1468 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1473 if (OJPEGReadWord(sp
,&m
)==0)
1477 if (sp
->subsamplingcorrect
==0)
1478 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DHT marker in JPEG data");
1481 if (sp
->subsamplingcorrect
!=0)
1483 OJPEGReadSkip(sp
,m
-2);
1487 na
=sizeof(uint32
)+2+m
;
1491 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1495 nb
[sizeof(uint32
)]=255;
1496 nb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1497 nb
[sizeof(uint32
)+2]=(m
>>8);
1498 nb
[sizeof(uint32
)+3]=(m
&255);
1499 if (OJPEGReadBlock(sp
,m
-2,&nb
[sizeof(uint32
)+4])==0)
1501 o
=nb
[sizeof(uint32
)+4];
1506 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DHT marker in JPEG data");
1509 if (sp
->dctable
[o
]!=0)
1510 _TIFFfree(sp
->dctable
[o
]);
1517 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DHT marker in JPEG data");
1523 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt DHT marker in JPEG data");
1526 if (sp
->actable
[o
]!=0)
1527 _TIFFfree(sp
->actable
[o
]);
1535 OJPEGReadHeaderInfoSecStreamSof(TIFF
* tif
, uint8 marker_id
)
1537 /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1538 static const char module[]="OJPEGReadHeaderInfoSecStreamSof";
1539 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1547 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt JPEG data");
1550 if (sp
->subsamplingcorrect
==0)
1551 sp
->sof_marker_id
=marker_id
;
1552 /* Lf: data length */
1553 if (OJPEGReadWord(sp
,&m
)==0)
1557 if (sp
->subsamplingcorrect
==0)
1558 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOF marker in JPEG data");
1564 if (sp
->subsamplingcorrect
==0)
1565 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOF marker in JPEG data");
1569 if (sp
->subsamplingcorrect
==0)
1571 if (n
!=sp
->samples_per_pixel
)
1573 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected number of samples");
1577 /* P: Sample precision */
1578 if (OJPEGReadByte(sp
,&o
)==0)
1582 if (sp
->subsamplingcorrect
==0)
1583 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected number of bits per sample");
1586 /* Y: Number of lines, X: Number of samples per line */
1587 if (sp
->subsamplingcorrect
)
1588 OJPEGReadSkip(sp
,4);
1591 /* Y: Number of lines */
1592 if (OJPEGReadWord(sp
,&p
)==0)
1594 if (((uint32
)p
<sp
->image_length
) && ((uint32
)p
<sp
->strile_length_total
))
1596 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected height");
1600 /* X: Number of samples per line */
1601 if (OJPEGReadWord(sp
,&p
)==0)
1603 if (((uint32
)p
<sp
->image_width
) && ((uint32
)p
<sp
->strile_width
))
1605 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected width");
1608 if ((uint32
)p
>sp
->strile_width
)
1610 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data image width exceeds expected image width");
1615 /* Nf: Number of image components in frame */
1616 if (OJPEGReadByte(sp
,&o
)==0)
1620 if (sp
->subsamplingcorrect
==0)
1621 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOF marker in JPEG data");
1624 /* per component stuff */
1625 /* TODO: double-check that flow implies that n cannot be as big as to make us overflow sof_c, sof_hv and sof_tq arrays */
1628 /* C: Component identifier */
1629 if (OJPEGReadByte(sp
,&o
)==0)
1631 if (sp
->subsamplingcorrect
==0)
1633 /* H: Horizontal sampling factor, and V: Vertical sampling factor */
1634 if (OJPEGReadByte(sp
,&o
)==0)
1636 if (sp
->subsamplingcorrect
!=0)
1640 sp
->subsampling_hor
=(o
>>4);
1641 sp
->subsampling_ver
=(o
&15);
1642 if (((sp
->subsampling_hor
!=1) && (sp
->subsampling_hor
!=2) && (sp
->subsampling_hor
!=4)) ||
1643 ((sp
->subsampling_ver
!=1) && (sp
->subsampling_ver
!=2) && (sp
->subsampling_ver
!=4)))
1644 sp
->subsampling_force_desubsampling_inside_decompression
=1;
1649 sp
->subsampling_force_desubsampling_inside_decompression
=1;
1655 if (sp
->subsampling_force_desubsampling_inside_decompression
==0)
1659 if (o
!=((sp
->subsampling_hor
<<4)|sp
->subsampling_ver
))
1661 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected subsampling values");
1669 TIFFErrorExt(tif
->tif_clientdata
,module,"JPEG compressed data indicates unexpected subsampling values");
1675 /* Tq: Quantization table destination selector */
1676 if (OJPEGReadByte(sp
,&o
)==0)
1678 if (sp
->subsamplingcorrect
==0)
1681 if (sp
->subsamplingcorrect
==0)
1687 OJPEGReadHeaderInfoSecStreamSos(TIFF
* tif
)
1689 /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1690 static const char module[]="OJPEGReadHeaderInfoSecStreamSos";
1691 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1695 assert(sp
->subsamplingcorrect
==0);
1698 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOS marker in JPEG data");
1702 if (OJPEGReadWord(sp
,&m
)==0)
1704 if (m
!=6+sp
->samples_per_pixel_per_plane
*2)
1706 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOS marker in JPEG data");
1710 if (OJPEGReadByte(sp
,&n
)==0)
1712 if (n
!=sp
->samples_per_pixel_per_plane
)
1714 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt SOS marker in JPEG data");
1717 /* Cs, Td, and Ta */
1718 for (o
=0; o
<sp
->samples_per_pixel_per_plane
; o
++)
1721 if (OJPEGReadByte(sp
,&n
)==0)
1723 sp
->sos_cs
[sp
->plane_sample_offset
+o
]=n
;
1725 if (OJPEGReadByte(sp
,&n
)==0)
1727 sp
->sos_tda
[sp
->plane_sample_offset
+o
]=n
;
1729 /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as per LibJpeg source */
1730 OJPEGReadSkip(sp
,3);
1735 OJPEGReadHeaderInfoSecTablesQTable(TIFF
* tif
)
1737 static const char module[]="OJPEGReadHeaderInfoSecTablesQTable";
1738 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1744 if (sp
->qtable_offset
[0]==0)
1746 TIFFErrorExt(tif
->tif_clientdata
,module,"Missing JPEG tables");
1749 sp
->in_buffer_file_pos_log
=0;
1750 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1752 if ((sp
->qtable_offset
[m
]!=0) && ((m
==0) || (sp
->qtable_offset
[m
]!=sp
->qtable_offset
[m
-1])))
1754 for (n
=0; n
<m
-1; n
++)
1756 if (sp
->qtable_offset
[m
]==sp
->qtable_offset
[n
])
1758 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt JpegQTables tag value");
1762 oa
=sizeof(uint32
)+69;
1766 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1770 ob
[sizeof(uint32
)]=255;
1771 ob
[sizeof(uint32
)+1]=JPEG_MARKER_DQT
;
1772 ob
[sizeof(uint32
)+2]=0;
1773 ob
[sizeof(uint32
)+3]=67;
1774 ob
[sizeof(uint32
)+4]=m
;
1775 TIFFSeekFile(tif
,sp
->qtable_offset
[m
],SEEK_SET
);
1776 p
=TIFFReadFile(tif
,&ob
[sizeof(uint32
)+5],64);
1783 sp
->sof_tq
[m
]=sp
->sof_tq
[m
-1];
1789 OJPEGReadHeaderInfoSecTablesDcTable(TIFF
* tif
)
1791 static const char module[]="OJPEGReadHeaderInfoSecTablesDcTable";
1792 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1800 if (sp
->dctable_offset
[0]==0)
1802 TIFFErrorExt(tif
->tif_clientdata
,module,"Missing JPEG tables");
1805 sp
->in_buffer_file_pos_log
=0;
1806 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1808 if ((sp
->dctable_offset
[m
]!=0) && ((m
==0) || (sp
->dctable_offset
[m
]!=sp
->dctable_offset
[m
-1])))
1810 for (n
=0; n
<m
-1; n
++)
1812 if (sp
->dctable_offset
[m
]==sp
->dctable_offset
[n
])
1814 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt JpegDcTables tag value");
1818 TIFFSeekFile(tif
,sp
->dctable_offset
[m
],SEEK_SET
);
1819 p
=TIFFReadFile(tif
,o
,16);
1823 for (n
=0; n
<16; n
++)
1825 ra
=sizeof(uint32
)+21+q
;
1829 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1833 rb
[sizeof(uint32
)]=255;
1834 rb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1835 rb
[sizeof(uint32
)+2]=((19+q
)>>8);
1836 rb
[sizeof(uint32
)+3]=((19+q
)&255);
1837 rb
[sizeof(uint32
)+4]=m
;
1838 for (n
=0; n
<16; n
++)
1839 rb
[sizeof(uint32
)+5+n
]=o
[n
];
1840 p
=TIFFReadFile(tif
,&(rb
[sizeof(uint32
)+21]),q
);
1844 sp
->sos_tda
[m
]=(m
<<4);
1847 sp
->sos_tda
[m
]=sp
->sos_tda
[m
-1];
1853 OJPEGReadHeaderInfoSecTablesAcTable(TIFF
* tif
)
1855 static const char module[]="OJPEGReadHeaderInfoSecTablesAcTable";
1856 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1864 if (sp
->actable_offset
[0]==0)
1866 TIFFErrorExt(tif
->tif_clientdata
,module,"Missing JPEG tables");
1869 sp
->in_buffer_file_pos_log
=0;
1870 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1872 if ((sp
->actable_offset
[m
]!=0) && ((m
==0) || (sp
->actable_offset
[m
]!=sp
->actable_offset
[m
-1])))
1874 for (n
=0; n
<m
-1; n
++)
1876 if (sp
->actable_offset
[m
]==sp
->actable_offset
[n
])
1878 TIFFErrorExt(tif
->tif_clientdata
,module,"Corrupt JpegAcTables tag value");
1882 TIFFSeekFile(tif
,sp
->actable_offset
[m
],SEEK_SET
);
1883 p
=TIFFReadFile(tif
,o
,16);
1887 for (n
=0; n
<16; n
++)
1889 ra
=sizeof(uint32
)+21+q
;
1893 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1897 rb
[sizeof(uint32
)]=255;
1898 rb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1899 rb
[sizeof(uint32
)+2]=((19+q
)>>8);
1900 rb
[sizeof(uint32
)+3]=((19+q
)&255);
1901 rb
[sizeof(uint32
)+4]=(16|m
);
1902 for (n
=0; n
<16; n
++)
1903 rb
[sizeof(uint32
)+5+n
]=o
[n
];
1904 p
=TIFFReadFile(tif
,&(rb
[sizeof(uint32
)+21]),q
);
1908 sp
->sos_tda
[m
]=(sp
->sos_tda
[m
]|m
);
1911 sp
->sos_tda
[m
]=(sp
->sos_tda
[m
]|(sp
->sos_tda
[m
-1]&15));
1917 OJPEGReadBufferFill(OJPEGState
* sp
)
1921 /* TODO: double-check: when subsamplingcorrect is set, no call to TIFFErrorExt or TIFFWarningExt should be made
1922 * in any other case, seek or read errors should be passed through */
1925 if (sp
->in_buffer_file_togo
!=0)
1927 if (sp
->in_buffer_file_pos_log
==0)
1929 TIFFSeekFile(sp
->tif
,sp
->in_buffer_file_pos
,SEEK_SET
);
1930 sp
->in_buffer_file_pos_log
=1;
1933 if ((uint64
)m
>sp
->in_buffer_file_togo
)
1934 m
=(uint16
)sp
->in_buffer_file_togo
;
1935 n
=TIFFReadFile(sp
->tif
,sp
->in_buffer
,(tmsize_t
)m
);
1939 assert(n
<=OJPEG_BUFFER
);
1941 assert((uint64
)n
<=sp
->in_buffer_file_togo
);
1943 sp
->in_buffer_togo
=m
;
1944 sp
->in_buffer_cur
=sp
->in_buffer
;
1945 sp
->in_buffer_file_togo
-=m
;
1946 sp
->in_buffer_file_pos
+=m
;
1949 sp
->in_buffer_file_pos_log
=0;
1950 switch(sp
->in_buffer_source
)
1952 case osibsNotSetYet
:
1953 if (sp
->jpeg_interchange_format
!=0)
1955 sp
->in_buffer_file_pos
=sp
->jpeg_interchange_format
;
1956 sp
->in_buffer_file_togo
=sp
->jpeg_interchange_format_length
;
1958 sp
->in_buffer_source
=osibsJpegInterchangeFormat
;
1960 case osibsJpegInterchangeFormat
:
1961 sp
->in_buffer_source
=osibsStrile
;
1963 if (!_TIFFFillStriles( sp
->tif
)
1964 || sp
->tif
->tif_dir
.td_stripoffset
== NULL
1965 || sp
->tif
->tif_dir
.td_stripbytecount
== NULL
)
1968 if (sp
->in_buffer_next_strile
==sp
->in_buffer_strile_count
)
1969 sp
->in_buffer_source
=osibsEof
;
1972 sp
->in_buffer_file_pos
=sp
->tif
->tif_dir
.td_stripoffset
[sp
->in_buffer_next_strile
];
1973 if (sp
->in_buffer_file_pos
!=0)
1975 if (sp
->in_buffer_file_pos
>=sp
->file_size
)
1976 sp
->in_buffer_file_pos
=0;
1977 else if (sp
->tif
->tif_dir
.td_stripbytecount
==NULL
)
1978 sp
->in_buffer_file_togo
=sp
->file_size
-sp
->in_buffer_file_pos
;
1981 if (sp
->tif
->tif_dir
.td_stripbytecount
== 0) {
1982 TIFFErrorExt(sp
->tif
->tif_clientdata
,sp
->tif
->tif_name
,"Strip byte counts are missing");
1985 sp
->in_buffer_file_togo
=sp
->tif
->tif_dir
.td_stripbytecount
[sp
->in_buffer_next_strile
];
1986 if (sp
->in_buffer_file_togo
==0)
1987 sp
->in_buffer_file_pos
=0;
1988 else if (sp
->in_buffer_file_pos
+sp
->in_buffer_file_togo
>sp
->file_size
)
1989 sp
->in_buffer_file_togo
=sp
->file_size
-sp
->in_buffer_file_pos
;
1992 sp
->in_buffer_next_strile
++;
2003 OJPEGReadByte(OJPEGState
* sp
, uint8
* byte
)
2005 if (sp
->in_buffer_togo
==0)
2007 if (OJPEGReadBufferFill(sp
)==0)
2009 assert(sp
->in_buffer_togo
>0);
2011 *byte
=*(sp
->in_buffer_cur
);
2012 sp
->in_buffer_cur
++;
2013 sp
->in_buffer_togo
--;
2018 OJPEGReadBytePeek(OJPEGState
* sp
, uint8
* byte
)
2020 if (sp
->in_buffer_togo
==0)
2022 if (OJPEGReadBufferFill(sp
)==0)
2024 assert(sp
->in_buffer_togo
>0);
2026 *byte
=*(sp
->in_buffer_cur
);
2031 OJPEGReadByteAdvance(OJPEGState
* sp
)
2033 assert(sp
->in_buffer_togo
>0);
2034 sp
->in_buffer_cur
++;
2035 sp
->in_buffer_togo
--;
2039 OJPEGReadWord(OJPEGState
* sp
, uint16
* word
)
2042 if (OJPEGReadByte(sp
,&m
)==0)
2045 if (OJPEGReadByte(sp
,&m
)==0)
2052 OJPEGReadBlock(OJPEGState
* sp
, uint16 len
, void* mem
)
2062 if (sp
->in_buffer_togo
==0)
2064 if (OJPEGReadBufferFill(sp
)==0)
2066 assert(sp
->in_buffer_togo
>0);
2069 if (n
>sp
->in_buffer_togo
)
2070 n
=sp
->in_buffer_togo
;
2071 _TIFFmemcpy(mmem
,sp
->in_buffer_cur
,n
);
2072 sp
->in_buffer_cur
+=n
;
2073 sp
->in_buffer_togo
-=n
;
2081 OJPEGReadSkip(OJPEGState
* sp
, uint16 len
)
2087 if (n
>sp
->in_buffer_togo
)
2088 n
=sp
->in_buffer_togo
;
2089 sp
->in_buffer_cur
+=n
;
2090 sp
->in_buffer_togo
-=n
;
2094 assert(sp
->in_buffer_togo
==0);
2096 if ((uint64
)n
>sp
->in_buffer_file_togo
)
2097 n
=(uint16
)sp
->in_buffer_file_togo
;
2098 sp
->in_buffer_file_pos
+=n
;
2099 sp
->in_buffer_file_togo
-=n
;
2100 sp
->in_buffer_file_pos_log
=0;
2101 /* we don't skip past jpeginterchangeformat/strile block...
2102 * if that is asked from us, we're dealing with totally bazurk
2103 * data anyway, and we've not seen this happening on any
2104 * testfile, so we might as well likely cause some other
2105 * meaningless error to be passed at some later time
2111 OJPEGWriteStream(TIFF
* tif
, void** mem
, uint32
* len
)
2113 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2117 assert(sp
->out_state
<=ososEoi
);
2118 switch(sp
->out_state
)
2121 OJPEGWriteStreamSoi(tif
,mem
,len
);
2124 OJPEGWriteStreamQTable(tif
,0,mem
,len
);
2127 OJPEGWriteStreamQTable(tif
,1,mem
,len
);
2130 OJPEGWriteStreamQTable(tif
,2,mem
,len
);
2133 OJPEGWriteStreamQTable(tif
,3,mem
,len
);
2136 OJPEGWriteStreamDcTable(tif
,0,mem
,len
);
2139 OJPEGWriteStreamDcTable(tif
,1,mem
,len
);
2142 OJPEGWriteStreamDcTable(tif
,2,mem
,len
);
2145 OJPEGWriteStreamDcTable(tif
,3,mem
,len
);
2148 OJPEGWriteStreamAcTable(tif
,0,mem
,len
);
2151 OJPEGWriteStreamAcTable(tif
,1,mem
,len
);
2154 OJPEGWriteStreamAcTable(tif
,2,mem
,len
);
2157 OJPEGWriteStreamAcTable(tif
,3,mem
,len
);
2160 OJPEGWriteStreamDri(tif
,mem
,len
);
2163 OJPEGWriteStreamSof(tif
,mem
,len
);
2166 OJPEGWriteStreamSos(tif
,mem
,len
);
2168 case ososCompressed
:
2169 if (OJPEGWriteStreamCompressed(tif
,mem
,len
)==0)
2173 OJPEGWriteStreamRst(tif
,mem
,len
);
2176 OJPEGWriteStreamEoi(tif
,mem
,len
);
2184 OJPEGWriteStreamSoi(TIFF
* tif
, void** mem
, uint32
* len
)
2186 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2187 assert(OJPEG_BUFFER
>=2);
2188 sp
->out_buffer
[0]=255;
2189 sp
->out_buffer
[1]=JPEG_MARKER_SOI
;
2191 *mem
=(void*)sp
->out_buffer
;
2196 OJPEGWriteStreamQTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2198 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2199 if (sp
->qtable
[table_index
]!=0)
2201 *mem
=(void*)(sp
->qtable
[table_index
]+sizeof(uint32
));
2202 *len
=*((uint32
*)sp
->qtable
[table_index
])-sizeof(uint32
);
2208 OJPEGWriteStreamDcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2210 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2211 if (sp
->dctable
[table_index
]!=0)
2213 *mem
=(void*)(sp
->dctable
[table_index
]+sizeof(uint32
));
2214 *len
=*((uint32
*)sp
->dctable
[table_index
])-sizeof(uint32
);
2220 OJPEGWriteStreamAcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2222 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2223 if (sp
->actable
[table_index
]!=0)
2225 *mem
=(void*)(sp
->actable
[table_index
]+sizeof(uint32
));
2226 *len
=*((uint32
*)sp
->actable
[table_index
])-sizeof(uint32
);
2232 OJPEGWriteStreamDri(TIFF
* tif
, void** mem
, uint32
* len
)
2234 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2235 assert(OJPEG_BUFFER
>=6);
2236 if (sp
->restart_interval
!=0)
2238 sp
->out_buffer
[0]=255;
2239 sp
->out_buffer
[1]=JPEG_MARKER_DRI
;
2240 sp
->out_buffer
[2]=0;
2241 sp
->out_buffer
[3]=4;
2242 sp
->out_buffer
[4]=(sp
->restart_interval
>>8);
2243 sp
->out_buffer
[5]=(sp
->restart_interval
&255);
2245 *mem
=(void*)sp
->out_buffer
;
2251 OJPEGWriteStreamSof(TIFF
* tif
, void** mem
, uint32
* len
)
2253 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2255 assert(OJPEG_BUFFER
>=2+8+sp
->samples_per_pixel_per_plane
*3);
2256 assert(255>=8+sp
->samples_per_pixel_per_plane
*3);
2257 sp
->out_buffer
[0]=255;
2258 sp
->out_buffer
[1]=sp
->sof_marker_id
;
2260 sp
->out_buffer
[2]=0;
2261 sp
->out_buffer
[3]=8+sp
->samples_per_pixel_per_plane
*3;
2263 sp
->out_buffer
[4]=8;
2265 sp
->out_buffer
[5]=(sp
->sof_y
>>8);
2266 sp
->out_buffer
[6]=(sp
->sof_y
&255);
2268 sp
->out_buffer
[7]=(sp
->sof_x
>>8);
2269 sp
->out_buffer
[8]=(sp
->sof_x
&255);
2271 sp
->out_buffer
[9]=sp
->samples_per_pixel_per_plane
;
2272 for (m
=0; m
<sp
->samples_per_pixel_per_plane
; m
++)
2275 sp
->out_buffer
[10+m
*3]=sp
->sof_c
[sp
->plane_sample_offset
+m
];
2277 sp
->out_buffer
[10+m
*3+1]=sp
->sof_hv
[sp
->plane_sample_offset
+m
];
2279 sp
->out_buffer
[10+m
*3+2]=sp
->sof_tq
[sp
->plane_sample_offset
+m
];
2281 *len
=10+sp
->samples_per_pixel_per_plane
*3;
2282 *mem
=(void*)sp
->out_buffer
;
2287 OJPEGWriteStreamSos(TIFF
* tif
, void** mem
, uint32
* len
)
2289 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2291 assert(OJPEG_BUFFER
>=2+6+sp
->samples_per_pixel_per_plane
*2);
2292 assert(255>=6+sp
->samples_per_pixel_per_plane
*2);
2293 sp
->out_buffer
[0]=255;
2294 sp
->out_buffer
[1]=JPEG_MARKER_SOS
;
2296 sp
->out_buffer
[2]=0;
2297 sp
->out_buffer
[3]=6+sp
->samples_per_pixel_per_plane
*2;
2299 sp
->out_buffer
[4]=sp
->samples_per_pixel_per_plane
;
2300 for (m
=0; m
<sp
->samples_per_pixel_per_plane
; m
++)
2303 sp
->out_buffer
[5+m
*2]=sp
->sos_cs
[sp
->plane_sample_offset
+m
];
2305 sp
->out_buffer
[5+m
*2+1]=sp
->sos_tda
[sp
->plane_sample_offset
+m
];
2308 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2]=0;
2310 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2+1]=63;
2312 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2+2]=0;
2313 *len
=8+sp
->samples_per_pixel_per_plane
*2;
2314 *mem
=(void*)sp
->out_buffer
;
2319 OJPEGWriteStreamCompressed(TIFF
* tif
, void** mem
, uint32
* len
)
2321 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2322 if (sp
->in_buffer_togo
==0)
2324 if (OJPEGReadBufferFill(sp
)==0)
2326 assert(sp
->in_buffer_togo
>0);
2328 *len
=sp
->in_buffer_togo
;
2329 *mem
=(void*)sp
->in_buffer_cur
;
2330 sp
->in_buffer_togo
=0;
2331 if (sp
->in_buffer_file_togo
==0)
2333 switch(sp
->in_buffer_source
)
2336 if (sp
->in_buffer_next_strile
<sp
->in_buffer_strile_count
)
2337 sp
->out_state
=ososRst
;
2339 sp
->out_state
=ososEoi
;
2342 sp
->out_state
=ososEoi
;
2352 OJPEGWriteStreamRst(TIFF
* tif
, void** mem
, uint32
* len
)
2354 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2355 assert(OJPEG_BUFFER
>=2);
2356 sp
->out_buffer
[0]=255;
2357 sp
->out_buffer
[1]=JPEG_MARKER_RST0
+sp
->restart_index
;
2358 sp
->restart_index
++;
2359 if (sp
->restart_index
==8)
2360 sp
->restart_index
=0;
2362 *mem
=(void*)sp
->out_buffer
;
2363 sp
->out_state
=ososCompressed
;
2367 OJPEGWriteStreamEoi(TIFF
* tif
, void** mem
, uint32
* len
)
2369 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2370 assert(OJPEG_BUFFER
>=2);
2371 sp
->out_buffer
[0]=255;
2372 sp
->out_buffer
[1]=JPEG_MARKER_EOI
;
2374 *mem
=(void*)sp
->out_buffer
;
2377 #ifndef LIBJPEG_ENCAP_EXTERNAL
2379 jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
)
2381 return(SETJMP(sp
->exit_jmpbuf
)?0:(jpeg_create_decompress(cinfo
),1));
2385 #ifndef LIBJPEG_ENCAP_EXTERNAL
2387 jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
)
2389 return(SETJMP(sp
->exit_jmpbuf
)?0:(jpeg_read_header(cinfo
,require_image
),1));
2393 #ifndef LIBJPEG_ENCAP_EXTERNAL
2395 jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
)
2397 return(SETJMP(sp
->exit_jmpbuf
)?0:(jpeg_start_decompress(cinfo
),1));
2401 #ifndef LIBJPEG_ENCAP_EXTERNAL
2403 jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
)
2405 return(SETJMP(sp
->exit_jmpbuf
)?0:(jpeg_read_scanlines(cinfo
,scanlines
,max_lines
),1));
2409 #ifndef LIBJPEG_ENCAP_EXTERNAL
2411 jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
)
2413 return(SETJMP(sp
->exit_jmpbuf
)?0:(jpeg_read_raw_data(cinfo
,data
,max_lines
),1));
2417 #ifndef LIBJPEG_ENCAP_EXTERNAL
2419 jpeg_encap_unwind(TIFF
* tif
)
2421 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2422 LONGJMP(sp
->exit_jmpbuf
,1);
2427 OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct
* cinfo
)
2429 char buffer
[JMSG_LENGTH_MAX
];
2430 (*cinfo
->err
->format_message
)(cinfo
,buffer
);
2431 TIFFWarningExt(((TIFF
*)(cinfo
->client_data
))->tif_clientdata
,"LibJpeg","%s",buffer
);
2435 OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct
* cinfo
)
2437 char buffer
[JMSG_LENGTH_MAX
];
2438 (*cinfo
->err
->format_message
)(cinfo
,buffer
);
2439 TIFFErrorExt(((TIFF
*)(cinfo
->client_data
))->tif_clientdata
,"LibJpeg","%s",buffer
);
2440 jpeg_encap_unwind((TIFF
*)(cinfo
->client_data
));
2444 OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct
* cinfo
)
2449 static wxjpeg_boolean
2450 OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct
* cinfo
)
2452 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2453 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2456 if (OJPEGWriteStream(tif
,&mem
,&len
)==0)
2458 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Premature end of JPEG data");
2459 jpeg_encap_unwind(tif
);
2461 sp
->libjpeg_jpeg_source_mgr
.bytes_in_buffer
=len
;
2462 sp
->libjpeg_jpeg_source_mgr
.next_input_byte
=mem
;
2467 OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct
* cinfo
, long num_bytes
)
2469 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2471 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Unexpected error");
2472 jpeg_encap_unwind(tif
);
2475 static wxjpeg_boolean
2476 OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct
* cinfo
, int desired
)
2478 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2480 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Unexpected error");
2481 jpeg_encap_unwind(tif
);
2486 OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct
* cinfo
)