1 /* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
2 specification is now totally obsolete and deprecated for new applications and
3 images. This file was was created solely in order to read unconverted images
4 still present on some users' computer systems. It will never be extended
5 to write such files. Writing new-style JPEG compressed TIFFs is implemented
8 The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
9 testfiles, and anticipate as much as possible all other... But still, it may
10 fail on some. If you encounter problems, please report them on the TIFF
11 mailing list and/or to Joris Van Damme <info@awaresystems.be>.
13 Please read the file called "TIFF Technical Note #2" if you need to be
14 convinced this compression scheme is bad and breaks TIFF. That document
15 is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
16 and from AWare Systems' TIFF section
17 <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
18 in Adobe's specification supplements, marked "draft" up to this day, but
19 supported by the TIFF community.
21 This file interfaces with Release 6B of the JPEG Library written by the
22 Independent JPEG Group. Previous versions of this file required a hack inside
23 the LibJpeg library. This version no longer requires that. Remember to
24 remove the hack if you update from the old version.
26 Copyright (c) Joris Van Damme <info@awaresystems.be>
27 Copyright (c) AWare Systems <http://www.awaresystems.be/>
29 The licence agreement for this file is the same as the rest of the LibTiff
32 IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
33 ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
34 OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
35 WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
36 LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
39 Joris Van Damme and/or AWare Systems may be available for custom
40 development. If you like what you see, and need anything similar or related,
41 contact <info@awaresystems.be>.
44 /* What is what, and what is not?
46 This decoder starts with an input stream, that is essentially the JpegInterchangeFormat
47 stream, if any, followed by the strile data, if any. This stream is read in
48 OJPEGReadByte and related functions.
50 It analyzes the start of this stream, until it encounters non-marker data, i.e.
51 compressed image data. Some of the header markers it sees have no actual content,
52 like the SOI marker, and APP/COM markers that really shouldn't even be there. Some
53 other markers do have content, and the valuable bits and pieces of information
54 in these markers are saved, checking all to verify that the stream is more or
55 less within expected bounds. This happens inside the OJPEGReadHeaderInfoSecStreamXxx
58 Some OJPEG imagery contains no valid JPEG header markers. This situation is picked
59 up on if we've seen no SOF marker when we're at the start of the compressed image
60 data. In this case, the tables are read from JpegXxxTables tags, and the other
61 bits and pieces of information is initialized to its most basic value. This is
62 implemented in the OJPEGReadHeaderInfoSecTablesXxx functions.
64 When this is complete, a good and valid JPEG header can be assembled, and this is
65 passed through to LibJpeg. When that's done, the remainder of the input stream, i.e.
66 the compressed image data, can be passed through unchanged. This is done in
67 OJPEGWriteStream functions.
69 LibTiff rightly expects to know the subsampling values before decompression. Just like
70 in new-style JPEG-in-TIFF, though, or even more so, actually, the YCbCrsubsampling
71 tag is notoriously unreliable. To correct these tag values with the ones inside
72 the JPEG stream, the first part of the input stream is pre-scanned in
73 OJPEGSubsamplingCorrect, making no note of any other data, reporting no warnings
74 or errors, up to the point where either these values are read, or it's clear they
75 aren't there. This means that some of the data is read twice, but we feel speed
76 in correcting these values is important enough to warrant this sacrifice. Although
77 there is currently no define or other configuration mechanism to disable this behaviour,
78 the actual header scanning is build to robustly respond with error report if it
79 should encounter an uncorrected mismatch of subsampling values. See
80 OJPEGReadHeaderInfoSecStreamSof.
82 The restart interval and restart markers are the most tricky part... The restart
83 interval can be specified in a tag. It can also be set inside the input JPEG stream.
84 It can be used inside the input JPEG stream. If reading from strile data, we've
85 consistently discovered the need to insert restart markers in between the different
86 striles, as is also probably the most likely interpretation of the original TIFF 6.0
87 specification. With all this setting of interval, and actual use of markers that is not
88 predictable at the time of valid JPEG header assembly, the restart thing may turn
89 out the Achilles heel of this implementation. Fortunately, most OJPEG writer vendors
90 succeed in reading back what they write, which may be the reason why we've been able
91 to discover ways that seem to work.
93 Some special provision is made for planarconfig separate OJPEG files. These seem
94 to consistently contain header info, a SOS marker, a plane, SOS marker, plane, SOS,
95 and plane. This may or may not be a valid JPEG configuration, we don't know and don't
96 care. We want LibTiff to be able to access the planes individually, without huge
97 buffering inside LibJpeg, anyway. So we compose headers to feed to LibJpeg, in this
98 case, that allow us to pass a single plane such that LibJpeg sees a valid
99 single-channel JPEG stream. Locating subsequent SOS markers, and thus subsequent
100 planes, is done inside OJPEGReadSecondarySos.
102 The benefit of the scheme is... that it works, basically. We know of no other that
103 does. It works without checking software tag, or otherwise going about things in an
104 OJPEG flavor specific manner. Instead, it is a single scheme, that covers the cases
105 with and without JpegInterchangeFormat, with and without striles, with part of
106 the header in JpegInterchangeFormat and remainder in first strile, etc. It is forgiving
107 and robust, may likely work with OJPEG flavors we've not seen yet, and makes most out
110 Another nice side-effect is that a complete JPEG single valid stream is build if
111 planarconfig is not separate (vast majority). We may one day use that to build
112 converters to JPEG, and/or to new-style JPEG compression inside TIFF.
114 A disadvantage is the lack of random access to the individual striles. This is the
115 reason for much of the complicated restart-and-position stuff inside OJPEGPreDecode.
116 Applications would do well accessing all striles in order, as this will result in
117 a single sequential scan of the input stream, and no restarting of LibJpeg decoding
121 #define WIN32_LEAN_AND_MEAN
127 /* Configuration defines here are:
128 * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some environments,
129 * like eg LibTiffDelphi, this is not possible. For this reason, the actual calls to
130 * libjpeg, with longjump stuff, are encapsulated in dedicated functions. When
131 * JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declared external
132 * to this unit, and can be defined elsewhere to use stuff other then longjump.
133 * The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encapsulators
134 * here, internally, with normal longjump.
135 * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
136 * conveniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
137 * in place of plain setjmp. These macros will make it easier. It is useless
138 * to fiddle with these if you define JPEG_ENCAP_EXTERNAL.
139 * OJPEG_BUFFER: Define the size of the desired buffer here. Should be small enough so as to guarantee
140 * instant processing, optimal streaming and optimal use of processor cache, but also big
141 * enough so as to not result in significant call overhead. It should be at least a few
142 * bytes to accommodate some structures (this is verified in asserts), but it would not be
143 * sensible to make it this small anyway, and it should be at most 64K since it is indexed
144 * with uint16. We recommend 2K.
145 * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used anywhere and has
146 * absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
149 /* define LIBJPEG_ENCAP_EXTERNAL */
150 #define SETJMP(jbuf) setjmp(jbuf)
151 #define LONGJMP(jbuf,code) longjmp(jbuf,code)
152 #define JMP_BUF jmp_buf
153 #define OJPEG_BUFFER 2048
154 /* define EGYPTIANWALK */
156 #define JPEG_MARKER_SOF0 0xC0
157 #define JPEG_MARKER_SOF1 0xC1
158 #define JPEG_MARKER_SOF3 0xC3
159 #define JPEG_MARKER_DHT 0xC4
160 #define JPEG_MARKER_RST0 0XD0
161 #define JPEG_MARKER_SOI 0xD8
162 #define JPEG_MARKER_EOI 0xD9
163 #define JPEG_MARKER_SOS 0xDA
164 #define JPEG_MARKER_DQT 0xDB
165 #define JPEG_MARKER_DRI 0xDD
166 #define JPEG_MARKER_APP0 0xE0
167 #define JPEG_MARKER_COM 0xFE
169 #define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
170 #define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
171 #define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
172 #define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
173 #define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
174 #define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
175 #define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
177 static const TIFFField ojpegFields
[] = {
178 {TIFFTAG_JPEGIFOFFSET
,1,1,TIFF_LONG8
,0,TIFF_SETGET_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGINTERCHANGEFORMAT
,TRUE
,FALSE
,"JpegInterchangeFormat",NULL
},
179 {TIFFTAG_JPEGIFBYTECOUNT
,1,1,TIFF_LONG8
,0,TIFF_SETGET_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH
,TRUE
,FALSE
,"JpegInterchangeFormatLength",NULL
},
180 {TIFFTAG_JPEGQTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGQTABLES
,FALSE
,TRUE
,"JpegQTables",NULL
},
181 {TIFFTAG_JPEGDCTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGDCTABLES
,FALSE
,TRUE
,"JpegDcTables",NULL
},
182 {TIFFTAG_JPEGACTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGACTABLES
,FALSE
,TRUE
,"JpegAcTables",NULL
},
183 {TIFFTAG_JPEGPROC
,1,1,TIFF_SHORT
,0,TIFF_SETGET_UINT16
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGPROC
,FALSE
,FALSE
,"JpegProc",NULL
},
184 {TIFFTAG_JPEGRESTARTINTERVAL
,1,1,TIFF_SHORT
,0,TIFF_SETGET_UINT16
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGRESTARTINTERVAL
,FALSE
,FALSE
,"JpegRestartInterval",NULL
},
187 #ifndef LIBJPEG_ENCAP_EXTERNAL
191 /* We undefine FAR to avoid conflict with JPEG definition */
198 Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
199 not defined. Unfortunately, the MinGW and Borland compilers include
200 a typedef for INT32, which causes a conflict. MSVC does not include
201 a conflicting typedef given the headers which are included.
203 #if defined(__BORLANDC__) || defined(__MINGW32__)
207 /* Define "boolean" as unsigned char, not int, per Windows custom. */
208 #if defined(__WIN32__) && !defined(__MINGW32__)
209 # ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
210 typedef unsigned char boolean
;
212 # define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
218 typedef struct jpeg_error_mgr jpeg_error_mgr
;
219 typedef struct jpeg_common_struct jpeg_common_struct
;
220 typedef struct jpeg_decompress_struct jpeg_decompress_struct
;
221 typedef struct jpeg_source_mgr jpeg_source_mgr
;
225 osibsJpegInterchangeFormat
,
228 } OJPEGStateInBufferSource
;
232 ososQTable0
,ososQTable1
,ososQTable2
,ososQTable3
,
233 ososDcTable0
,ososDcTable1
,ososDcTable2
,ososDcTable3
,
234 ososAcTable0
,ososAcTable1
,ososAcTable2
,ososAcTable3
,
241 } OJPEGStateOutState
;
246 #ifndef LIBJPEG_ENCAP_EXTERNAL
249 TIFFVGetMethod vgetparent
;
250 TIFFVSetMethod vsetparent
;
251 TIFFPrintMethod printdir
;
256 uint32 strile_length
;
257 uint32 strile_length_total
;
258 uint8 samples_per_pixel
;
259 uint8 plane_sample_offset
;
260 uint8 samples_per_pixel_per_plane
;
261 uint64 jpeg_interchange_format
;
262 uint64 jpeg_interchange_format_length
;
264 uint8 subsamplingcorrect
;
265 uint8 subsamplingcorrect_done
;
266 uint8 subsampling_tag
;
267 uint8 subsampling_hor
;
268 uint8 subsampling_ver
;
269 uint8 subsampling_force_desubsampling_inside_decompression
;
270 uint8 qtable_offset_count
;
271 uint8 dctable_offset_count
;
272 uint8 actable_offset_count
;
273 uint64 qtable_offset
[3];
274 uint64 dctable_offset
[3];
275 uint64 actable_offset
[3];
279 uint16 restart_interval
;
292 OJPEGStateInBufferSource in_buffer_source
;
293 uint32 in_buffer_next_strile
;
294 uint64 in_buffer_file_pos
;
295 uint64 in_buffer_file_togo
;
297 uint8 readheader_done
;
298 uint8 writeheader_done
;
299 uint16 write_cursample
;
300 uint32 write_curstrile
;
301 uint8 libjpeg_session_active
;
302 uint8 libjpeg_jpeg_query_style
;
303 jpeg_error_mgr libjpeg_jpeg_error_mgr
;
304 jpeg_decompress_struct libjpeg_jpeg_decompress_struct
;
305 jpeg_source_mgr libjpeg_jpeg_source_mgr
;
306 uint8 subsampling_convert_log
;
307 uint32 subsampling_convert_ylinelen
;
308 uint32 subsampling_convert_ylines
;
309 uint32 subsampling_convert_clinelen
;
310 uint32 subsampling_convert_clines
;
311 uint32 subsampling_convert_ybuflen
;
312 uint32 subsampling_convert_cbuflen
;
313 uint32 subsampling_convert_ycbcrbuflen
;
314 uint8
* subsampling_convert_ycbcrbuf
;
315 uint8
* subsampling_convert_ybuf
;
316 uint8
* subsampling_convert_cbbuf
;
317 uint8
* subsampling_convert_crbuf
;
318 uint32 subsampling_convert_ycbcrimagelen
;
319 uint8
** subsampling_convert_ycbcrimage
;
320 uint32 subsampling_convert_clinelenout
;
321 uint32 subsampling_convert_state
;
322 uint32 bytes_per_line
; /* if the codec outputs subsampled data, a 'line' in bytes_per_line */
323 uint32 lines_per_strile
; /* and lines_per_strile means subsampling_ver desubsampled rows */
324 OJPEGStateInBufferSource in_buffer_source
;
325 uint32 in_buffer_next_strile
;
326 uint32 in_buffer_strile_count
;
327 uint64 in_buffer_file_pos
;
328 uint8 in_buffer_file_pos_log
;
329 uint64 in_buffer_file_togo
;
330 uint16 in_buffer_togo
;
331 uint8
* in_buffer_cur
;
332 uint8 in_buffer
[OJPEG_BUFFER
];
333 OJPEGStateOutState out_state
;
334 uint8 out_buffer
[OJPEG_BUFFER
];
338 static int OJPEGVGetField(TIFF
* tif
, uint32 tag
, va_list ap
);
339 static int OJPEGVSetField(TIFF
* tif
, uint32 tag
, va_list ap
);
340 static void OJPEGPrintDir(TIFF
* tif
, FILE* fd
, long flags
);
342 static int OJPEGFixupTags(TIFF
* tif
);
343 static int OJPEGSetupDecode(TIFF
* tif
);
344 static int OJPEGPreDecode(TIFF
* tif
, uint16 s
);
345 static int OJPEGPreDecodeSkipRaw(TIFF
* tif
);
346 static int OJPEGPreDecodeSkipScanlines(TIFF
* tif
);
347 static int OJPEGDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
);
348 static int OJPEGDecodeRaw(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
349 static int OJPEGDecodeScanlines(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
350 static void OJPEGPostDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
351 static int OJPEGSetupEncode(TIFF
* tif
);
352 static int OJPEGPreEncode(TIFF
* tif
, uint16 s
);
353 static int OJPEGEncode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
);
354 static int OJPEGPostEncode(TIFF
* tif
);
355 static void OJPEGCleanup(TIFF
* tif
);
357 static void OJPEGSubsamplingCorrect(TIFF
* tif
);
358 static int OJPEGReadHeaderInfo(TIFF
* tif
);
359 static int OJPEGReadSecondarySos(TIFF
* tif
, uint16 s
);
360 static int OJPEGWriteHeaderInfo(TIFF
* tif
);
361 static void OJPEGLibjpegSessionAbort(TIFF
* tif
);
363 static int OJPEGReadHeaderInfoSec(TIFF
* tif
);
364 static int OJPEGReadHeaderInfoSecStreamDri(TIFF
* tif
);
365 static int OJPEGReadHeaderInfoSecStreamDqt(TIFF
* tif
);
366 static int OJPEGReadHeaderInfoSecStreamDht(TIFF
* tif
);
367 static int OJPEGReadHeaderInfoSecStreamSof(TIFF
* tif
, uint8 marker_id
);
368 static int OJPEGReadHeaderInfoSecStreamSos(TIFF
* tif
);
369 static int OJPEGReadHeaderInfoSecTablesQTable(TIFF
* tif
);
370 static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF
* tif
);
371 static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF
* tif
);
373 static int OJPEGReadBufferFill(OJPEGState
* sp
);
374 static int OJPEGReadByte(OJPEGState
* sp
, uint8
* byte
);
375 static int OJPEGReadBytePeek(OJPEGState
* sp
, uint8
* byte
);
376 static void OJPEGReadByteAdvance(OJPEGState
* sp
);
377 static int OJPEGReadWord(OJPEGState
* sp
, uint16
* word
);
378 static int OJPEGReadBlock(OJPEGState
* sp
, uint16 len
, void* mem
);
379 static void OJPEGReadSkip(OJPEGState
* sp
, uint16 len
);
381 static int OJPEGWriteStream(TIFF
* tif
, void** mem
, uint32
* len
);
382 static void OJPEGWriteStreamSoi(TIFF
* tif
, void** mem
, uint32
* len
);
383 static void OJPEGWriteStreamQTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
384 static void OJPEGWriteStreamDcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
385 static void OJPEGWriteStreamAcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
386 static void OJPEGWriteStreamDri(TIFF
* tif
, void** mem
, uint32
* len
);
387 static void OJPEGWriteStreamSof(TIFF
* tif
, void** mem
, uint32
* len
);
388 static void OJPEGWriteStreamSos(TIFF
* tif
, void** mem
, uint32
* len
);
389 static int OJPEGWriteStreamCompressed(TIFF
* tif
, void** mem
, uint32
* len
);
390 static void OJPEGWriteStreamRst(TIFF
* tif
, void** mem
, uint32
* len
);
391 static void OJPEGWriteStreamEoi(TIFF
* tif
, void** mem
, uint32
* len
);
393 #ifdef LIBJPEG_ENCAP_EXTERNAL
394 extern int jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
395 extern int jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
);
396 extern int jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
397 extern int jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
);
398 extern int jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
);
399 extern void jpeg_encap_unwind(TIFF
* tif
);
401 static int jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* j
);
402 static int jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
);
403 static int jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
404 static int jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
);
405 static int jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
);
406 static void jpeg_encap_unwind(TIFF
* tif
);
409 static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct
* cinfo
);
410 static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct
* cinfo
);
411 static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct
* cinfo
);
412 static boolean
OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct
* cinfo
);
413 static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct
* cinfo
, long num_bytes
);
414 static boolean
OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct
* cinfo
, int desired
);
415 static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct
* cinfo
);
418 TIFFInitOJPEG(TIFF
* tif
, int scheme
)
420 static const char module
[]="TIFFInitOJPEG";
423 assert(scheme
==COMPRESSION_OJPEG
);
426 * Merge codec-specific tag information.
428 if (!_TIFFMergeFields(tif
, ojpegFields
, TIFFArrayCount(ojpegFields
))) {
429 TIFFErrorExt(tif
->tif_clientdata
, module
,
430 "Merging Old JPEG codec-specific tags failed");
435 sp
=_TIFFmalloc(sizeof(OJPEGState
));
438 TIFFErrorExt(tif
->tif_clientdata
,module
,"No space for OJPEG state block");
441 _TIFFmemset(sp
,0,sizeof(OJPEGState
));
444 sp
->subsampling_hor
=2;
445 sp
->subsampling_ver
=2;
446 TIFFSetField(tif
,TIFFTAG_YCBCRSUBSAMPLING
,2,2);
447 /* tif codec methods */
448 tif
->tif_fixuptags
=OJPEGFixupTags
;
449 tif
->tif_setupdecode
=OJPEGSetupDecode
;
450 tif
->tif_predecode
=OJPEGPreDecode
;
451 tif
->tif_postdecode
=OJPEGPostDecode
;
452 tif
->tif_decoderow
=OJPEGDecode
;
453 tif
->tif_decodestrip
=OJPEGDecode
;
454 tif
->tif_decodetile
=OJPEGDecode
;
455 tif
->tif_setupencode
=OJPEGSetupEncode
;
456 tif
->tif_preencode
=OJPEGPreEncode
;
457 tif
->tif_postencode
=OJPEGPostEncode
;
458 tif
->tif_encoderow
=OJPEGEncode
;
459 tif
->tif_encodestrip
=OJPEGEncode
;
460 tif
->tif_encodetile
=OJPEGEncode
;
461 tif
->tif_cleanup
=OJPEGCleanup
;
462 tif
->tif_data
=(uint8
*)sp
;
463 /* tif tag methods */
464 sp
->vgetparent
=tif
->tif_tagmethods
.vgetfield
;
465 tif
->tif_tagmethods
.vgetfield
=OJPEGVGetField
;
466 sp
->vsetparent
=tif
->tif_tagmethods
.vsetfield
;
467 tif
->tif_tagmethods
.vsetfield
=OJPEGVSetField
;
468 sp
->printdir
=tif
->tif_tagmethods
.printdir
;
469 tif
->tif_tagmethods
.printdir
=OJPEGPrintDir
;
470 /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
471 Some others do, but have totally meaningless or corrupt values
472 in these tags. In these cases, the JpegInterchangeFormat stream is
473 reliable. In any case, this decoder reads the compressed data itself,
474 from the most reliable locations, and we need to notify encapsulating
475 LibTiff not to read raw strips or tiles for us. */
476 tif
->tif_flags
|=TIFF_NOREADRAW
;
481 OJPEGVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
483 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
486 case TIFFTAG_JPEGIFOFFSET
:
487 *va_arg(ap
,uint64
*)=(uint64
)sp
->jpeg_interchange_format
;
489 case TIFFTAG_JPEGIFBYTECOUNT
:
490 *va_arg(ap
,uint64
*)=(uint64
)sp
->jpeg_interchange_format_length
;
492 case TIFFTAG_YCBCRSUBSAMPLING
:
493 if (sp
->subsamplingcorrect_done
==0)
494 OJPEGSubsamplingCorrect(tif
);
495 *va_arg(ap
,uint16
*)=(uint16
)sp
->subsampling_hor
;
496 *va_arg(ap
,uint16
*)=(uint16
)sp
->subsampling_ver
;
498 case TIFFTAG_JPEGQTABLES
:
499 *va_arg(ap
,uint32
*)=(uint32
)sp
->qtable_offset_count
;
500 *va_arg(ap
,void**)=(void*)sp
->qtable_offset
;
502 case TIFFTAG_JPEGDCTABLES
:
503 *va_arg(ap
,uint32
*)=(uint32
)sp
->dctable_offset_count
;
504 *va_arg(ap
,void**)=(void*)sp
->dctable_offset
;
506 case TIFFTAG_JPEGACTABLES
:
507 *va_arg(ap
,uint32
*)=(uint32
)sp
->actable_offset_count
;
508 *va_arg(ap
,void**)=(void*)sp
->actable_offset
;
510 case TIFFTAG_JPEGPROC
:
511 *va_arg(ap
,uint16
*)=(uint16
)sp
->jpeg_proc
;
513 case TIFFTAG_JPEGRESTARTINTERVAL
:
514 *va_arg(ap
,uint16
*)=sp
->restart_interval
;
517 return (*sp
->vgetparent
)(tif
,tag
,ap
);
523 OJPEGVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
525 static const char module
[]="OJPEGVSetField";
526 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
530 const TIFFField
* fip
;
534 case TIFFTAG_JPEGIFOFFSET
:
535 sp
->jpeg_interchange_format
=(uint64
)va_arg(ap
,uint64
);
537 case TIFFTAG_JPEGIFBYTECOUNT
:
538 sp
->jpeg_interchange_format_length
=(uint64
)va_arg(ap
,uint64
);
540 case TIFFTAG_YCBCRSUBSAMPLING
:
541 sp
->subsampling_tag
=1;
542 sp
->subsampling_hor
=(uint8
)va_arg(ap
,uint16_vap
);
543 sp
->subsampling_ver
=(uint8
)va_arg(ap
,uint16_vap
);
544 tif
->tif_dir
.td_ycbcrsubsampling
[0]=sp
->subsampling_hor
;
545 tif
->tif_dir
.td_ycbcrsubsampling
[1]=sp
->subsampling_ver
;
547 case TIFFTAG_JPEGQTABLES
:
548 ma
=(uint32
)va_arg(ap
,uint32
);
553 TIFFErrorExt(tif
->tif_clientdata
,module
,"JpegQTables tag has incorrect count");
556 sp
->qtable_offset_count
=(uint8
)ma
;
557 mb
=(uint64
*)va_arg(ap
,uint64
*);
559 sp
->qtable_offset
[n
]=mb
[n
];
562 case TIFFTAG_JPEGDCTABLES
:
563 ma
=(uint32
)va_arg(ap
,uint32
);
568 TIFFErrorExt(tif
->tif_clientdata
,module
,"JpegDcTables tag has incorrect count");
571 sp
->dctable_offset_count
=(uint8
)ma
;
572 mb
=(uint64
*)va_arg(ap
,uint64
*);
574 sp
->dctable_offset
[n
]=mb
[n
];
577 case TIFFTAG_JPEGACTABLES
:
578 ma
=(uint32
)va_arg(ap
,uint32
);
583 TIFFErrorExt(tif
->tif_clientdata
,module
,"JpegAcTables tag has incorrect count");
586 sp
->actable_offset_count
=(uint8
)ma
;
587 mb
=(uint64
*)va_arg(ap
,uint64
*);
589 sp
->actable_offset
[n
]=mb
[n
];
592 case TIFFTAG_JPEGPROC
:
593 sp
->jpeg_proc
=(uint8
)va_arg(ap
,uint16_vap
);
595 case TIFFTAG_JPEGRESTARTINTERVAL
:
596 sp
->restart_interval
=(uint16
)va_arg(ap
,uint16_vap
);
599 return (*sp
->vsetparent
)(tif
,tag
,ap
);
601 fip
= TIFFFieldWithTag(tif
,tag
);
602 if( fip
== NULL
) /* shouldn't happen */
604 TIFFSetFieldBit(tif
,fip
->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
++;
729 OJPEGPreDecodeSkipRaw(TIFF
* tif
)
731 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
733 m
=sp
->lines_per_strile
;
734 if (sp
->subsampling_convert_state
!=0)
736 if (sp
->subsampling_convert_clines
-sp
->subsampling_convert_state
>=m
)
738 sp
->subsampling_convert_state
+=m
;
739 if (sp
->subsampling_convert_state
==sp
->subsampling_convert_clines
)
740 sp
->subsampling_convert_state
=0;
743 m
-=sp
->subsampling_convert_clines
-sp
->subsampling_convert_state
;
744 sp
->subsampling_convert_state
=0;
746 while (m
>=sp
->subsampling_convert_clines
)
748 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
750 m
-=sp
->subsampling_convert_clines
;
754 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
756 sp
->subsampling_convert_state
=m
;
762 OJPEGPreDecodeSkipScanlines(TIFF
* tif
)
764 static const char module
[]="OJPEGPreDecodeSkipScanlines";
765 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
767 if (sp
->skip_buffer
==NULL
)
769 sp
->skip_buffer
=_TIFFmalloc(sp
->bytes_per_line
);
770 if (sp
->skip_buffer
==NULL
)
772 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
776 for (m
=0; m
<sp
->lines_per_strile
; m
++)
778 if (jpeg_read_scanlines_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),&sp
->skip_buffer
,1)==0)
785 OJPEGDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
)
787 static const char module
[]="OJPEGDecode";
788 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
790 if( !sp
->decoder_ok
)
792 TIFFErrorExt(tif
->tif_clientdata
,module
,"Cannot decode: decoder not correctly initialized");
795 if (sp
->libjpeg_jpeg_query_style
==0)
797 if (OJPEGDecodeRaw(tif
,buf
,cc
)==0)
802 if (OJPEGDecodeScanlines(tif
,buf
,cc
)==0)
809 OJPEGDecodeRaw(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
811 static const char module
[]="OJPEGDecodeRaw";
812 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
822 if (cc
%sp
->bytes_per_line
!=0)
824 TIFFErrorExt(tif
->tif_clientdata
,module
,"Fractional scanline not read");
832 if (sp
->subsampling_convert_state
==0)
834 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
837 oy
=sp
->subsampling_convert_ybuf
+sp
->subsampling_convert_state
*sp
->subsampling_ver
*sp
->subsampling_convert_ylinelen
;
838 ocb
=sp
->subsampling_convert_cbbuf
+sp
->subsampling_convert_state
*sp
->subsampling_convert_clinelen
;
839 ocr
=sp
->subsampling_convert_crbuf
+sp
->subsampling_convert_state
*sp
->subsampling_convert_clinelen
;
841 for (q
=0; q
<sp
->subsampling_convert_clinelenout
; q
++)
844 for (sy
=0; sy
<sp
->subsampling_ver
; sy
++)
846 for (sx
=0; sx
<sp
->subsampling_hor
; sx
++)
848 r
+=sp
->subsampling_convert_ylinelen
-sp
->subsampling_hor
;
850 oy
+=sp
->subsampling_hor
;
854 sp
->subsampling_convert_state
++;
855 if (sp
->subsampling_convert_state
==sp
->subsampling_convert_clines
)
856 sp
->subsampling_convert_state
=0;
857 m
+=sp
->bytes_per_line
;
858 n
-=sp
->bytes_per_line
;
864 OJPEGDecodeScanlines(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
866 static const char module
[]="OJPEGDecodeScanlines";
867 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
870 if (cc
%sp
->bytes_per_line
!=0)
872 TIFFErrorExt(tif
->tif_clientdata
,module
,"Fractional scanline not read");
880 if (jpeg_read_scanlines_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),&m
,1)==0)
882 m
+=sp
->bytes_per_line
;
883 n
-=sp
->bytes_per_line
;
889 OJPEGPostDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
891 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
894 sp
->write_curstrile
++;
895 if (sp
->write_curstrile
%tif
->tif_dir
.td_stripsperimage
==0)
897 assert(sp
->libjpeg_session_active
!=0);
898 OJPEGLibjpegSessionAbort(tif
);
899 sp
->writeheader_done
=0;
904 OJPEGSetupEncode(TIFF
* tif
)
906 static const char module
[]="OJPEGSetupEncode";
907 TIFFErrorExt(tif
->tif_clientdata
,module
,"OJPEG encoding not supported; use new-style JPEG compression instead");
912 OJPEGPreEncode(TIFF
* tif
, uint16 s
)
914 static const char module
[]="OJPEGPreEncode";
916 TIFFErrorExt(tif
->tif_clientdata
,module
,"OJPEG encoding not supported; use new-style JPEG compression instead");
921 OJPEGEncode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
)
923 static const char module
[]="OJPEGEncode";
927 TIFFErrorExt(tif
->tif_clientdata
,module
,"OJPEG encoding not supported; use new-style JPEG compression instead");
932 OJPEGPostEncode(TIFF
* tif
)
934 static const char module
[]="OJPEGPostEncode";
935 TIFFErrorExt(tif
->tif_clientdata
,module
,"OJPEG encoding not supported; use new-style JPEG compression instead");
940 OJPEGCleanup(TIFF
* tif
)
942 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
945 tif
->tif_tagmethods
.vgetfield
=sp
->vgetparent
;
946 tif
->tif_tagmethods
.vsetfield
=sp
->vsetparent
;
947 tif
->tif_tagmethods
.printdir
=sp
->printdir
;
948 if (sp
->qtable
[0]!=0)
949 _TIFFfree(sp
->qtable
[0]);
950 if (sp
->qtable
[1]!=0)
951 _TIFFfree(sp
->qtable
[1]);
952 if (sp
->qtable
[2]!=0)
953 _TIFFfree(sp
->qtable
[2]);
954 if (sp
->qtable
[3]!=0)
955 _TIFFfree(sp
->qtable
[3]);
956 if (sp
->dctable
[0]!=0)
957 _TIFFfree(sp
->dctable
[0]);
958 if (sp
->dctable
[1]!=0)
959 _TIFFfree(sp
->dctable
[1]);
960 if (sp
->dctable
[2]!=0)
961 _TIFFfree(sp
->dctable
[2]);
962 if (sp
->dctable
[3]!=0)
963 _TIFFfree(sp
->dctable
[3]);
964 if (sp
->actable
[0]!=0)
965 _TIFFfree(sp
->actable
[0]);
966 if (sp
->actable
[1]!=0)
967 _TIFFfree(sp
->actable
[1]);
968 if (sp
->actable
[2]!=0)
969 _TIFFfree(sp
->actable
[2]);
970 if (sp
->actable
[3]!=0)
971 _TIFFfree(sp
->actable
[3]);
972 if (sp
->libjpeg_session_active
!=0)
973 OJPEGLibjpegSessionAbort(tif
);
974 if (sp
->subsampling_convert_ycbcrbuf
!=0)
975 _TIFFfree(sp
->subsampling_convert_ycbcrbuf
);
976 if (sp
->subsampling_convert_ycbcrimage
!=0)
977 _TIFFfree(sp
->subsampling_convert_ycbcrimage
);
978 if (sp
->skip_buffer
!=0)
979 _TIFFfree(sp
->skip_buffer
);
982 _TIFFSetDefaultCompressionState(tif
);
987 OJPEGSubsamplingCorrect(TIFF
* tif
)
989 static const char module
[]="OJPEGSubsamplingCorrect";
990 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
993 _TIFFFillStriles( tif
);
995 assert(sp
->subsamplingcorrect_done
==0);
996 if ((tif
->tif_dir
.td_samplesperpixel
!=3) || ((tif
->tif_dir
.td_photometric
!=PHOTOMETRIC_YCBCR
) &&
997 (tif
->tif_dir
.td_photometric
!=PHOTOMETRIC_ITULAB
)))
999 if (sp
->subsampling_tag
!=0)
1000 TIFFWarningExt(tif
->tif_clientdata
,module
,"Subsampling tag not appropriate for this Photometric and/or SamplesPerPixel");
1001 sp
->subsampling_hor
=1;
1002 sp
->subsampling_ver
=1;
1003 sp
->subsampling_force_desubsampling_inside_decompression
=0;
1007 sp
->subsamplingcorrect_done
=1;
1008 mh
=sp
->subsampling_hor
;
1009 mv
=sp
->subsampling_ver
;
1010 sp
->subsamplingcorrect
=1;
1011 OJPEGReadHeaderInfoSec(tif
);
1012 if (sp
->subsampling_force_desubsampling_inside_decompression
!=0)
1014 sp
->subsampling_hor
=1;
1015 sp
->subsampling_ver
=1;
1017 sp
->subsamplingcorrect
=0;
1018 if (((sp
->subsampling_hor
!=mh
) || (sp
->subsampling_ver
!=mv
)) && (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 [%d,%d] does not match default values [2,2]; assuming subsampling inside JPEG data is correct",sp
->subsampling_hor
,sp
->subsampling_ver
);
1023 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
);
1025 if (sp
->subsampling_force_desubsampling_inside_decompression
!=0)
1027 if (sp
->subsampling_tag
==0)
1028 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");
1030 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
);
1032 if (sp
->subsampling_force_desubsampling_inside_decompression
==0)
1034 if (sp
->subsampling_hor
<sp
->subsampling_ver
)
1035 TIFFWarningExt(tif
->tif_clientdata
,module
,"Subsampling values [%d,%d] are not allowed in TIFF",sp
->subsampling_hor
,sp
->subsampling_ver
);
1038 sp
->subsamplingcorrect_done
=1;
1042 OJPEGReadHeaderInfo(TIFF
* tif
)
1044 static const char module
[]="OJPEGReadHeaderInfo";
1045 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1046 assert(sp
->readheader_done
==0);
1047 sp
->image_width
=tif
->tif_dir
.td_imagewidth
;
1048 sp
->image_length
=tif
->tif_dir
.td_imagelength
;
1051 sp
->strile_width
=tif
->tif_dir
.td_tilewidth
;
1052 sp
->strile_length
=tif
->tif_dir
.td_tilelength
;
1053 sp
->strile_length_total
=((sp
->image_length
+sp
->strile_length
-1)/sp
->strile_length
)*sp
->strile_length
;
1057 sp
->strile_width
=sp
->image_width
;
1058 sp
->strile_length
=tif
->tif_dir
.td_rowsperstrip
;
1059 sp
->strile_length_total
=sp
->image_length
;
1061 if (tif
->tif_dir
.td_samplesperpixel
==1)
1063 sp
->samples_per_pixel
=1;
1064 sp
->plane_sample_offset
=0;
1065 sp
->samples_per_pixel_per_plane
=sp
->samples_per_pixel
;
1066 sp
->subsampling_hor
=1;
1067 sp
->subsampling_ver
=1;
1071 if (tif
->tif_dir
.td_samplesperpixel
!=3)
1073 TIFFErrorExt(tif
->tif_clientdata
,module
,"SamplesPerPixel %d not supported for this compression scheme",sp
->samples_per_pixel
);
1076 sp
->samples_per_pixel
=3;
1077 sp
->plane_sample_offset
=0;
1078 if (tif
->tif_dir
.td_planarconfig
==PLANARCONFIG_CONTIG
)
1079 sp
->samples_per_pixel_per_plane
=3;
1081 sp
->samples_per_pixel_per_plane
=1;
1083 if (sp
->strile_length
<sp
->image_length
)
1085 if (sp
->strile_length
%(sp
->subsampling_ver
*8)!=0)
1087 TIFFErrorExt(tif
->tif_clientdata
,module
,"Incompatible vertical subsampling and image strip/tile length");
1090 sp
->restart_interval
=(uint16
)(((sp
->strile_width
+sp
->subsampling_hor
*8-1)/(sp
->subsampling_hor
*8))*(sp
->strile_length
/(sp
->subsampling_ver
*8)));
1092 if (OJPEGReadHeaderInfoSec(tif
)==0)
1094 sp
->sos_end
[0].log
=1;
1095 sp
->sos_end
[0].in_buffer_source
=sp
->in_buffer_source
;
1096 sp
->sos_end
[0].in_buffer_next_strile
=sp
->in_buffer_next_strile
;
1097 sp
->sos_end
[0].in_buffer_file_pos
=sp
->in_buffer_file_pos
-sp
->in_buffer_togo
;
1098 sp
->sos_end
[0].in_buffer_file_togo
=sp
->in_buffer_file_togo
+sp
->in_buffer_togo
;
1099 sp
->readheader_done
=1;
1104 OJPEGReadSecondarySos(TIFF
* tif
, uint16 s
)
1106 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1110 assert(sp
->sos_end
[0].log
!=0);
1111 assert(sp
->sos_end
[s
].log
==0);
1112 sp
->plane_sample_offset
=(uint8
)(s
-1);
1113 while(sp
->sos_end
[sp
->plane_sample_offset
].log
==0)
1114 sp
->plane_sample_offset
--;
1115 sp
->in_buffer_source
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_source
;
1116 sp
->in_buffer_next_strile
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_next_strile
;
1117 sp
->in_buffer_file_pos
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_pos
;
1118 sp
->in_buffer_file_pos_log
=0;
1119 sp
->in_buffer_file_togo
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_togo
;
1120 sp
->in_buffer_togo
=0;
1121 sp
->in_buffer_cur
=0;
1122 while(sp
->plane_sample_offset
<s
)
1126 if (OJPEGReadByte(sp
,&m
)==0)
1132 if (OJPEGReadByte(sp
,&m
)==0)
1137 if (m
==JPEG_MARKER_SOS
)
1141 sp
->plane_sample_offset
++;
1142 if (OJPEGReadHeaderInfoSecStreamSos(tif
)==0)
1144 sp
->sos_end
[sp
->plane_sample_offset
].log
=1;
1145 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_source
=sp
->in_buffer_source
;
1146 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_next_strile
=sp
->in_buffer_next_strile
;
1147 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_pos
=sp
->in_buffer_file_pos
-sp
->in_buffer_togo
;
1148 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_togo
=sp
->in_buffer_file_togo
+sp
->in_buffer_togo
;
1154 OJPEGWriteHeaderInfo(TIFF
* tif
)
1156 static const char module
[]="OJPEGWriteHeaderInfo";
1157 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1160 /* if a previous attempt failed, don't try again */
1161 if (sp
->libjpeg_session_active
!= 0)
1163 sp
->out_state
=ososSoi
;
1164 sp
->restart_index
=0;
1165 jpeg_std_error(&(sp
->libjpeg_jpeg_error_mgr
));
1166 sp
->libjpeg_jpeg_error_mgr
.output_message
=OJPEGLibjpegJpegErrorMgrOutputMessage
;
1167 sp
->libjpeg_jpeg_error_mgr
.error_exit
=OJPEGLibjpegJpegErrorMgrErrorExit
;
1168 sp
->libjpeg_jpeg_decompress_struct
.err
=&(sp
->libjpeg_jpeg_error_mgr
);
1169 sp
->libjpeg_jpeg_decompress_struct
.client_data
=(void*)tif
;
1170 if (jpeg_create_decompress_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
))==0)
1172 sp
->libjpeg_session_active
=1;
1173 sp
->libjpeg_jpeg_source_mgr
.bytes_in_buffer
=0;
1174 sp
->libjpeg_jpeg_source_mgr
.init_source
=OJPEGLibjpegJpegSourceMgrInitSource
;
1175 sp
->libjpeg_jpeg_source_mgr
.fill_input_buffer
=OJPEGLibjpegJpegSourceMgrFillInputBuffer
;
1176 sp
->libjpeg_jpeg_source_mgr
.skip_input_data
=OJPEGLibjpegJpegSourceMgrSkipInputData
;
1177 sp
->libjpeg_jpeg_source_mgr
.resync_to_restart
=OJPEGLibjpegJpegSourceMgrResyncToRestart
;
1178 sp
->libjpeg_jpeg_source_mgr
.term_source
=OJPEGLibjpegJpegSourceMgrTermSource
;
1179 sp
->libjpeg_jpeg_decompress_struct
.src
=&(sp
->libjpeg_jpeg_source_mgr
);
1180 if (jpeg_read_header_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),1)==0)
1182 if ((sp
->subsampling_force_desubsampling_inside_decompression
==0) && (sp
->samples_per_pixel_per_plane
>1))
1184 sp
->libjpeg_jpeg_decompress_struct
.raw_data_out
=1;
1185 #if JPEG_LIB_VERSION >= 70
1186 sp
->libjpeg_jpeg_decompress_struct
.do_fancy_upsampling
=FALSE
;
1188 sp
->libjpeg_jpeg_query_style
=0;
1189 if (sp
->subsampling_convert_log
==0)
1191 assert(sp
->subsampling_convert_ycbcrbuf
==0);
1192 assert(sp
->subsampling_convert_ycbcrimage
==0);
1193 sp
->subsampling_convert_ylinelen
=((sp
->strile_width
+sp
->subsampling_hor
*8-1)/(sp
->subsampling_hor
*8)*sp
->subsampling_hor
*8);
1194 sp
->subsampling_convert_ylines
=sp
->subsampling_ver
*8;
1195 sp
->subsampling_convert_clinelen
=sp
->subsampling_convert_ylinelen
/sp
->subsampling_hor
;
1196 sp
->subsampling_convert_clines
=8;
1197 sp
->subsampling_convert_ybuflen
=sp
->subsampling_convert_ylinelen
*sp
->subsampling_convert_ylines
;
1198 sp
->subsampling_convert_cbuflen
=sp
->subsampling_convert_clinelen
*sp
->subsampling_convert_clines
;
1199 sp
->subsampling_convert_ycbcrbuflen
=sp
->subsampling_convert_ybuflen
+2*sp
->subsampling_convert_cbuflen
;
1200 sp
->subsampling_convert_ycbcrbuf
=_TIFFmalloc(sp
->subsampling_convert_ycbcrbuflen
);
1201 if (sp
->subsampling_convert_ycbcrbuf
==0)
1203 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1206 sp
->subsampling_convert_ybuf
=sp
->subsampling_convert_ycbcrbuf
;
1207 sp
->subsampling_convert_cbbuf
=sp
->subsampling_convert_ybuf
+sp
->subsampling_convert_ybuflen
;
1208 sp
->subsampling_convert_crbuf
=sp
->subsampling_convert_cbbuf
+sp
->subsampling_convert_cbuflen
;
1209 sp
->subsampling_convert_ycbcrimagelen
=3+sp
->subsampling_convert_ylines
+2*sp
->subsampling_convert_clines
;
1210 sp
->subsampling_convert_ycbcrimage
=_TIFFmalloc(sp
->subsampling_convert_ycbcrimagelen
*sizeof(uint8
*));
1211 if (sp
->subsampling_convert_ycbcrimage
==0)
1213 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1216 m
=sp
->subsampling_convert_ycbcrimage
;
1217 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3);
1218 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3+sp
->subsampling_convert_ylines
);
1219 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3+sp
->subsampling_convert_ylines
+sp
->subsampling_convert_clines
);
1220 for (n
=0; n
<sp
->subsampling_convert_ylines
; n
++)
1221 *m
++=sp
->subsampling_convert_ybuf
+n
*sp
->subsampling_convert_ylinelen
;
1222 for (n
=0; n
<sp
->subsampling_convert_clines
; n
++)
1223 *m
++=sp
->subsampling_convert_cbbuf
+n
*sp
->subsampling_convert_clinelen
;
1224 for (n
=0; n
<sp
->subsampling_convert_clines
; n
++)
1225 *m
++=sp
->subsampling_convert_crbuf
+n
*sp
->subsampling_convert_clinelen
;
1226 sp
->subsampling_convert_clinelenout
=((sp
->strile_width
+sp
->subsampling_hor
-1)/sp
->subsampling_hor
);
1227 sp
->subsampling_convert_state
=0;
1228 sp
->bytes_per_line
=sp
->subsampling_convert_clinelenout
*(sp
->subsampling_ver
*sp
->subsampling_hor
+2);
1229 sp
->lines_per_strile
=((sp
->strile_length
+sp
->subsampling_ver
-1)/sp
->subsampling_ver
);
1230 sp
->subsampling_convert_log
=1;
1235 sp
->libjpeg_jpeg_decompress_struct
.jpeg_color_space
=JCS_UNKNOWN
;
1236 sp
->libjpeg_jpeg_decompress_struct
.out_color_space
=JCS_UNKNOWN
;
1237 sp
->libjpeg_jpeg_query_style
=1;
1238 sp
->bytes_per_line
=sp
->samples_per_pixel_per_plane
*sp
->strile_width
;
1239 sp
->lines_per_strile
=sp
->strile_length
;
1241 if (jpeg_start_decompress_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
))==0)
1243 sp
->writeheader_done
=1;
1248 OJPEGLibjpegSessionAbort(TIFF
* tif
)
1250 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1251 assert(sp
->libjpeg_session_active
!=0);
1252 jpeg_destroy((jpeg_common_struct
*)(&(sp
->libjpeg_jpeg_decompress_struct
)));
1253 sp
->libjpeg_session_active
=0;
1257 OJPEGReadHeaderInfoSec(TIFF
* tif
)
1259 static const char module
[]="OJPEGReadHeaderInfoSec";
1260 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1264 if (sp
->file_size
==0)
1265 sp
->file_size
=TIFFGetFileSize(tif
);
1266 if (sp
->jpeg_interchange_format
!=0)
1268 if (sp
->jpeg_interchange_format
>=sp
->file_size
)
1270 sp
->jpeg_interchange_format
=0;
1271 sp
->jpeg_interchange_format_length
=0;
1275 if ((sp
->jpeg_interchange_format_length
==0) || (sp
->jpeg_interchange_format
+sp
->jpeg_interchange_format_length
>sp
->file_size
))
1276 sp
->jpeg_interchange_format_length
=sp
->file_size
-sp
->jpeg_interchange_format
;
1279 sp
->in_buffer_source
=osibsNotSetYet
;
1280 sp
->in_buffer_next_strile
=0;
1281 sp
->in_buffer_strile_count
=tif
->tif_dir
.td_nstrips
;
1282 sp
->in_buffer_file_togo
=0;
1283 sp
->in_buffer_togo
=0;
1286 if (OJPEGReadBytePeek(sp
,&m
)==0)
1290 OJPEGReadByteAdvance(sp
);
1293 if (OJPEGReadByte(sp
,&m
)==0)
1298 case JPEG_MARKER_SOI
:
1299 /* this type of marker has no data, and should be skipped */
1301 case JPEG_MARKER_COM
:
1302 case JPEG_MARKER_APP0
:
1303 case JPEG_MARKER_APP0
+1:
1304 case JPEG_MARKER_APP0
+2:
1305 case JPEG_MARKER_APP0
+3:
1306 case JPEG_MARKER_APP0
+4:
1307 case JPEG_MARKER_APP0
+5:
1308 case JPEG_MARKER_APP0
+6:
1309 case JPEG_MARKER_APP0
+7:
1310 case JPEG_MARKER_APP0
+8:
1311 case JPEG_MARKER_APP0
+9:
1312 case JPEG_MARKER_APP0
+10:
1313 case JPEG_MARKER_APP0
+11:
1314 case JPEG_MARKER_APP0
+12:
1315 case JPEG_MARKER_APP0
+13:
1316 case JPEG_MARKER_APP0
+14:
1317 case JPEG_MARKER_APP0
+15:
1318 /* this type of marker has data, but it has no use to us (and no place here) and should be skipped */
1319 if (OJPEGReadWord(sp
,&n
)==0)
1323 if (sp
->subsamplingcorrect
==0)
1324 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt JPEG data");
1328 OJPEGReadSkip(sp
,n
-2);
1330 case JPEG_MARKER_DRI
:
1331 if (OJPEGReadHeaderInfoSecStreamDri(tif
)==0)
1334 case JPEG_MARKER_DQT
:
1335 if (OJPEGReadHeaderInfoSecStreamDqt(tif
)==0)
1338 case JPEG_MARKER_DHT
:
1339 if (OJPEGReadHeaderInfoSecStreamDht(tif
)==0)
1342 case JPEG_MARKER_SOF0
:
1343 case JPEG_MARKER_SOF1
:
1344 case JPEG_MARKER_SOF3
:
1345 if (OJPEGReadHeaderInfoSecStreamSof(tif
,m
)==0)
1347 if (sp
->subsamplingcorrect
!=0)
1350 case JPEG_MARKER_SOS
:
1351 if (sp
->subsamplingcorrect
!=0)
1353 assert(sp
->plane_sample_offset
==0);
1354 if (OJPEGReadHeaderInfoSecStreamSos(tif
)==0)
1358 TIFFErrorExt(tif
->tif_clientdata
,module
,"Unknown marker type %d in JPEG data",m
);
1361 } while(m
!=JPEG_MARKER_SOS
);
1362 if (sp
->subsamplingcorrect
)
1366 if (OJPEGReadHeaderInfoSecTablesQTable(tif
)==0)
1368 sp
->sof_marker_id
=JPEG_MARKER_SOF0
;
1369 for (o
=0; o
<sp
->samples_per_pixel
; o
++)
1371 sp
->sof_hv
[0]=((sp
->subsampling_hor
<<4)|sp
->subsampling_ver
);
1372 for (o
=1; o
<sp
->samples_per_pixel
; o
++)
1374 sp
->sof_x
=sp
->strile_width
;
1375 sp
->sof_y
=sp
->strile_length_total
;
1377 if (OJPEGReadHeaderInfoSecTablesDcTable(tif
)==0)
1379 if (OJPEGReadHeaderInfoSecTablesAcTable(tif
)==0)
1381 for (o
=1; o
<sp
->samples_per_pixel
; o
++)
1388 OJPEGReadHeaderInfoSecStreamDri(TIFF
* tif
)
1390 /* This could easily cause trouble in some cases... but no such cases have
1392 static const char module
[]="OJPEGReadHeaderInfoSecStreamDri";
1393 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1395 if (OJPEGReadWord(sp
,&m
)==0)
1399 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DRI marker in JPEG data");
1402 if (OJPEGReadWord(sp
,&m
)==0)
1404 sp
->restart_interval
=m
;
1409 OJPEGReadHeaderInfoSecStreamDqt(TIFF
* tif
)
1411 /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1412 static const char module
[]="OJPEGReadHeaderInfoSecStreamDqt";
1413 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1418 if (OJPEGReadWord(sp
,&m
)==0)
1422 if (sp
->subsamplingcorrect
==0)
1423 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DQT marker in JPEG data");
1426 if (sp
->subsamplingcorrect
!=0)
1427 OJPEGReadSkip(sp
,m
-2);
1435 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DQT marker in JPEG data");
1438 na
=sizeof(uint32
)+69;
1442 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1446 nb
[sizeof(uint32
)]=255;
1447 nb
[sizeof(uint32
)+1]=JPEG_MARKER_DQT
;
1448 nb
[sizeof(uint32
)+2]=0;
1449 nb
[sizeof(uint32
)+3]=67;
1450 if (OJPEGReadBlock(sp
,65,&nb
[sizeof(uint32
)+4])==0) {
1454 o
=nb
[sizeof(uint32
)+4]&15;
1457 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DQT marker in JPEG data");
1461 if (sp
->qtable
[o
]!=0)
1462 _TIFFfree(sp
->qtable
[o
]);
1471 OJPEGReadHeaderInfoSecStreamDht(TIFF
* tif
)
1473 /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1474 /* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
1475 static const char module
[]="OJPEGReadHeaderInfoSecStreamDht";
1476 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1481 if (OJPEGReadWord(sp
,&m
)==0)
1485 if (sp
->subsamplingcorrect
==0)
1486 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DHT marker in JPEG data");
1489 if (sp
->subsamplingcorrect
!=0)
1491 OJPEGReadSkip(sp
,m
-2);
1495 na
=sizeof(uint32
)+2+m
;
1499 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1503 nb
[sizeof(uint32
)]=255;
1504 nb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1505 nb
[sizeof(uint32
)+2]=(m
>>8);
1506 nb
[sizeof(uint32
)+3]=(m
&255);
1507 if (OJPEGReadBlock(sp
,m
-2,&nb
[sizeof(uint32
)+4])==0) {
1511 o
=nb
[sizeof(uint32
)+4];
1516 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DHT marker in JPEG data");
1520 if (sp
->dctable
[o
]!=0)
1521 _TIFFfree(sp
->dctable
[o
]);
1528 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DHT marker in JPEG data");
1535 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DHT marker in JPEG data");
1539 if (sp
->actable
[o
]!=0)
1540 _TIFFfree(sp
->actable
[o
]);
1548 OJPEGReadHeaderInfoSecStreamSof(TIFF
* tif
, uint8 marker_id
)
1550 /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1551 static const char module
[]="OJPEGReadHeaderInfoSecStreamSof";
1552 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1560 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt JPEG data");
1563 if (sp
->subsamplingcorrect
==0)
1564 sp
->sof_marker_id
=marker_id
;
1565 /* Lf: data length */
1566 if (OJPEGReadWord(sp
,&m
)==0)
1570 if (sp
->subsamplingcorrect
==0)
1571 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOF marker in JPEG data");
1577 if (sp
->subsamplingcorrect
==0)
1578 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOF marker in JPEG data");
1582 if (sp
->subsamplingcorrect
==0)
1584 if (n
!=sp
->samples_per_pixel
)
1586 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected number of samples");
1590 /* P: Sample precision */
1591 if (OJPEGReadByte(sp
,&o
)==0)
1595 if (sp
->subsamplingcorrect
==0)
1596 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected number of bits per sample");
1599 /* Y: Number of lines, X: Number of samples per line */
1600 if (sp
->subsamplingcorrect
)
1601 OJPEGReadSkip(sp
,4);
1604 /* Y: Number of lines */
1605 if (OJPEGReadWord(sp
,&p
)==0)
1607 if (((uint32
)p
<sp
->image_length
) && ((uint32
)p
<sp
->strile_length_total
))
1609 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected height");
1613 /* X: Number of samples per line */
1614 if (OJPEGReadWord(sp
,&p
)==0)
1616 if (((uint32
)p
<sp
->image_width
) && ((uint32
)p
<sp
->strile_width
))
1618 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected width");
1621 if ((uint32
)p
>sp
->strile_width
)
1623 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data image width exceeds expected image width");
1628 /* Nf: Number of image components in frame */
1629 if (OJPEGReadByte(sp
,&o
)==0)
1633 if (sp
->subsamplingcorrect
==0)
1634 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOF marker in JPEG data");
1637 /* per component stuff */
1638 /* 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 */
1641 /* C: Component identifier */
1642 if (OJPEGReadByte(sp
,&o
)==0)
1644 if (sp
->subsamplingcorrect
==0)
1646 /* H: Horizontal sampling factor, and V: Vertical sampling factor */
1647 if (OJPEGReadByte(sp
,&o
)==0)
1649 if (sp
->subsamplingcorrect
!=0)
1653 sp
->subsampling_hor
=(o
>>4);
1654 sp
->subsampling_ver
=(o
&15);
1655 if (((sp
->subsampling_hor
!=1) && (sp
->subsampling_hor
!=2) && (sp
->subsampling_hor
!=4)) ||
1656 ((sp
->subsampling_ver
!=1) && (sp
->subsampling_ver
!=2) && (sp
->subsampling_ver
!=4)))
1657 sp
->subsampling_force_desubsampling_inside_decompression
=1;
1662 sp
->subsampling_force_desubsampling_inside_decompression
=1;
1668 if (sp
->subsampling_force_desubsampling_inside_decompression
==0)
1672 if (o
!=((sp
->subsampling_hor
<<4)|sp
->subsampling_ver
))
1674 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected subsampling values");
1682 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected subsampling values");
1688 /* Tq: Quantization table destination selector */
1689 if (OJPEGReadByte(sp
,&o
)==0)
1691 if (sp
->subsamplingcorrect
==0)
1694 if (sp
->subsamplingcorrect
==0)
1700 OJPEGReadHeaderInfoSecStreamSos(TIFF
* tif
)
1702 /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1703 static const char module
[]="OJPEGReadHeaderInfoSecStreamSos";
1704 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1708 assert(sp
->subsamplingcorrect
==0);
1711 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOS marker in JPEG data");
1715 if (OJPEGReadWord(sp
,&m
)==0)
1717 if (m
!=6+sp
->samples_per_pixel_per_plane
*2)
1719 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOS marker in JPEG data");
1723 if (OJPEGReadByte(sp
,&n
)==0)
1725 if (n
!=sp
->samples_per_pixel_per_plane
)
1727 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOS marker in JPEG data");
1730 /* Cs, Td, and Ta */
1731 for (o
=0; o
<sp
->samples_per_pixel_per_plane
; o
++)
1734 if (OJPEGReadByte(sp
,&n
)==0)
1736 sp
->sos_cs
[sp
->plane_sample_offset
+o
]=n
;
1738 if (OJPEGReadByte(sp
,&n
)==0)
1740 sp
->sos_tda
[sp
->plane_sample_offset
+o
]=n
;
1742 /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as per LibJpeg source */
1743 OJPEGReadSkip(sp
,3);
1748 OJPEGReadHeaderInfoSecTablesQTable(TIFF
* tif
)
1750 static const char module
[]="OJPEGReadHeaderInfoSecTablesQTable";
1751 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1757 if (sp
->qtable_offset
[0]==0)
1759 TIFFErrorExt(tif
->tif_clientdata
,module
,"Missing JPEG tables");
1762 sp
->in_buffer_file_pos_log
=0;
1763 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1765 if ((sp
->qtable_offset
[m
]!=0) && ((m
==0) || (sp
->qtable_offset
[m
]!=sp
->qtable_offset
[m
-1])))
1767 for (n
=0; n
<m
-1; n
++)
1769 if (sp
->qtable_offset
[m
]==sp
->qtable_offset
[n
])
1771 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt JpegQTables tag value");
1775 oa
=sizeof(uint32
)+69;
1779 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1783 ob
[sizeof(uint32
)]=255;
1784 ob
[sizeof(uint32
)+1]=JPEG_MARKER_DQT
;
1785 ob
[sizeof(uint32
)+2]=0;
1786 ob
[sizeof(uint32
)+3]=67;
1787 ob
[sizeof(uint32
)+4]=m
;
1788 TIFFSeekFile(tif
,sp
->qtable_offset
[m
],SEEK_SET
);
1789 p
=(uint32
)TIFFReadFile(tif
,&ob
[sizeof(uint32
)+5],64);
1795 if (sp
->qtable
[m
]!=0)
1796 _TIFFfree(sp
->qtable
[m
]);
1801 sp
->sof_tq
[m
]=sp
->sof_tq
[m
-1];
1807 OJPEGReadHeaderInfoSecTablesDcTable(TIFF
* tif
)
1809 static const char module
[]="OJPEGReadHeaderInfoSecTablesDcTable";
1810 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1818 if (sp
->dctable_offset
[0]==0)
1820 TIFFErrorExt(tif
->tif_clientdata
,module
,"Missing JPEG tables");
1823 sp
->in_buffer_file_pos_log
=0;
1824 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1826 if ((sp
->dctable_offset
[m
]!=0) && ((m
==0) || (sp
->dctable_offset
[m
]!=sp
->dctable_offset
[m
-1])))
1828 for (n
=0; n
<m
-1; n
++)
1830 if (sp
->dctable_offset
[m
]==sp
->dctable_offset
[n
])
1832 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt JpegDcTables tag value");
1836 TIFFSeekFile(tif
,sp
->dctable_offset
[m
],SEEK_SET
);
1837 p
=(uint32
)TIFFReadFile(tif
,o
,16);
1841 for (n
=0; n
<16; n
++)
1843 ra
=sizeof(uint32
)+21+q
;
1847 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1851 rb
[sizeof(uint32
)]=255;
1852 rb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1853 rb
[sizeof(uint32
)+2]=(uint8
)((19+q
)>>8);
1854 rb
[sizeof(uint32
)+3]=((19+q
)&255);
1855 rb
[sizeof(uint32
)+4]=m
;
1856 for (n
=0; n
<16; n
++)
1857 rb
[sizeof(uint32
)+5+n
]=o
[n
];
1858 p
=(uint32
)TIFFReadFile(tif
,&(rb
[sizeof(uint32
)+21]),q
);
1864 if (sp
->dctable
[m
]!=0)
1865 _TIFFfree(sp
->dctable
[m
]);
1867 sp
->sos_tda
[m
]=(m
<<4);
1870 sp
->sos_tda
[m
]=sp
->sos_tda
[m
-1];
1876 OJPEGReadHeaderInfoSecTablesAcTable(TIFF
* tif
)
1878 static const char module
[]="OJPEGReadHeaderInfoSecTablesAcTable";
1879 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1887 if (sp
->actable_offset
[0]==0)
1889 TIFFErrorExt(tif
->tif_clientdata
,module
,"Missing JPEG tables");
1892 sp
->in_buffer_file_pos_log
=0;
1893 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1895 if ((sp
->actable_offset
[m
]!=0) && ((m
==0) || (sp
->actable_offset
[m
]!=sp
->actable_offset
[m
-1])))
1897 for (n
=0; n
<m
-1; n
++)
1899 if (sp
->actable_offset
[m
]==sp
->actable_offset
[n
])
1901 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt JpegAcTables tag value");
1905 TIFFSeekFile(tif
,sp
->actable_offset
[m
],SEEK_SET
);
1906 p
=(uint32
)TIFFReadFile(tif
,o
,16);
1910 for (n
=0; n
<16; n
++)
1912 ra
=sizeof(uint32
)+21+q
;
1916 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1920 rb
[sizeof(uint32
)]=255;
1921 rb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1922 rb
[sizeof(uint32
)+2]=(uint8
)((19+q
)>>8);
1923 rb
[sizeof(uint32
)+3]=((19+q
)&255);
1924 rb
[sizeof(uint32
)+4]=(16|m
);
1925 for (n
=0; n
<16; n
++)
1926 rb
[sizeof(uint32
)+5+n
]=o
[n
];
1927 p
=(uint32
)TIFFReadFile(tif
,&(rb
[sizeof(uint32
)+21]),q
);
1933 if (sp
->actable
[m
]!=0)
1934 _TIFFfree(sp
->actable
[m
]);
1936 sp
->sos_tda
[m
]=(sp
->sos_tda
[m
]|m
);
1939 sp
->sos_tda
[m
]=(sp
->sos_tda
[m
]|(sp
->sos_tda
[m
-1]&15));
1945 OJPEGReadBufferFill(OJPEGState
* sp
)
1949 /* TODO: double-check: when subsamplingcorrect is set, no call to TIFFErrorExt or TIFFWarningExt should be made
1950 * in any other case, seek or read errors should be passed through */
1953 if (sp
->in_buffer_file_togo
!=0)
1955 if (sp
->in_buffer_file_pos_log
==0)
1957 TIFFSeekFile(sp
->tif
,sp
->in_buffer_file_pos
,SEEK_SET
);
1958 sp
->in_buffer_file_pos_log
=1;
1961 if ((uint64
)m
>sp
->in_buffer_file_togo
)
1962 m
=(uint16
)sp
->in_buffer_file_togo
;
1963 n
=TIFFReadFile(sp
->tif
,sp
->in_buffer
,(tmsize_t
)m
);
1967 assert(n
<=OJPEG_BUFFER
);
1969 assert((uint64
)n
<=sp
->in_buffer_file_togo
);
1971 sp
->in_buffer_togo
=m
;
1972 sp
->in_buffer_cur
=sp
->in_buffer
;
1973 sp
->in_buffer_file_togo
-=m
;
1974 sp
->in_buffer_file_pos
+=m
;
1977 sp
->in_buffer_file_pos_log
=0;
1978 switch(sp
->in_buffer_source
)
1980 case osibsNotSetYet
:
1981 if (sp
->jpeg_interchange_format
!=0)
1983 sp
->in_buffer_file_pos
=sp
->jpeg_interchange_format
;
1984 sp
->in_buffer_file_togo
=sp
->jpeg_interchange_format_length
;
1986 sp
->in_buffer_source
=osibsJpegInterchangeFormat
;
1988 case osibsJpegInterchangeFormat
:
1989 sp
->in_buffer_source
=osibsStrile
;
1992 if (!_TIFFFillStriles( sp
->tif
)
1993 || sp
->tif
->tif_dir
.td_stripoffset
== NULL
1994 || sp
->tif
->tif_dir
.td_stripbytecount
== NULL
)
1997 if (sp
->in_buffer_next_strile
==sp
->in_buffer_strile_count
)
1998 sp
->in_buffer_source
=osibsEof
;
2001 sp
->in_buffer_file_pos
=sp
->tif
->tif_dir
.td_stripoffset
[sp
->in_buffer_next_strile
];
2002 if (sp
->in_buffer_file_pos
!=0)
2004 if (sp
->in_buffer_file_pos
>=sp
->file_size
)
2005 sp
->in_buffer_file_pos
=0;
2006 else if (sp
->tif
->tif_dir
.td_stripbytecount
==NULL
)
2007 sp
->in_buffer_file_togo
=sp
->file_size
-sp
->in_buffer_file_pos
;
2010 if (sp
->tif
->tif_dir
.td_stripbytecount
== 0) {
2011 TIFFErrorExt(sp
->tif
->tif_clientdata
,sp
->tif
->tif_name
,"Strip byte counts are missing");
2014 sp
->in_buffer_file_togo
=sp
->tif
->tif_dir
.td_stripbytecount
[sp
->in_buffer_next_strile
];
2015 if (sp
->in_buffer_file_togo
==0)
2016 sp
->in_buffer_file_pos
=0;
2017 else if (sp
->in_buffer_file_pos
+sp
->in_buffer_file_togo
>sp
->file_size
)
2018 sp
->in_buffer_file_togo
=sp
->file_size
-sp
->in_buffer_file_pos
;
2021 sp
->in_buffer_next_strile
++;
2032 OJPEGReadByte(OJPEGState
* sp
, uint8
* byte
)
2034 if (sp
->in_buffer_togo
==0)
2036 if (OJPEGReadBufferFill(sp
)==0)
2038 assert(sp
->in_buffer_togo
>0);
2040 *byte
=*(sp
->in_buffer_cur
);
2041 sp
->in_buffer_cur
++;
2042 sp
->in_buffer_togo
--;
2047 OJPEGReadBytePeek(OJPEGState
* sp
, uint8
* byte
)
2049 if (sp
->in_buffer_togo
==0)
2051 if (OJPEGReadBufferFill(sp
)==0)
2053 assert(sp
->in_buffer_togo
>0);
2055 *byte
=*(sp
->in_buffer_cur
);
2060 OJPEGReadByteAdvance(OJPEGState
* sp
)
2062 assert(sp
->in_buffer_togo
>0);
2063 sp
->in_buffer_cur
++;
2064 sp
->in_buffer_togo
--;
2068 OJPEGReadWord(OJPEGState
* sp
, uint16
* word
)
2071 if (OJPEGReadByte(sp
,&m
)==0)
2074 if (OJPEGReadByte(sp
,&m
)==0)
2081 OJPEGReadBlock(OJPEGState
* sp
, uint16 len
, void* mem
)
2091 if (sp
->in_buffer_togo
==0)
2093 if (OJPEGReadBufferFill(sp
)==0)
2095 assert(sp
->in_buffer_togo
>0);
2098 if (n
>sp
->in_buffer_togo
)
2099 n
=sp
->in_buffer_togo
;
2100 _TIFFmemcpy(mmem
,sp
->in_buffer_cur
,n
);
2101 sp
->in_buffer_cur
+=n
;
2102 sp
->in_buffer_togo
-=n
;
2110 OJPEGReadSkip(OJPEGState
* sp
, uint16 len
)
2116 if (n
>sp
->in_buffer_togo
)
2117 n
=sp
->in_buffer_togo
;
2118 sp
->in_buffer_cur
+=n
;
2119 sp
->in_buffer_togo
-=n
;
2123 assert(sp
->in_buffer_togo
==0);
2125 if ((uint64
)n
>sp
->in_buffer_file_togo
)
2126 n
=(uint16
)sp
->in_buffer_file_togo
;
2127 sp
->in_buffer_file_pos
+=n
;
2128 sp
->in_buffer_file_togo
-=n
;
2129 sp
->in_buffer_file_pos_log
=0;
2130 /* we don't skip past jpeginterchangeformat/strile block...
2131 * if that is asked from us, we're dealing with totally bazurk
2132 * data anyway, and we've not seen this happening on any
2133 * testfile, so we might as well likely cause some other
2134 * meaningless error to be passed at some later time
2140 OJPEGWriteStream(TIFF
* tif
, void** mem
, uint32
* len
)
2142 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2146 assert(sp
->out_state
<=ososEoi
);
2147 switch(sp
->out_state
)
2150 OJPEGWriteStreamSoi(tif
,mem
,len
);
2153 OJPEGWriteStreamQTable(tif
,0,mem
,len
);
2156 OJPEGWriteStreamQTable(tif
,1,mem
,len
);
2159 OJPEGWriteStreamQTable(tif
,2,mem
,len
);
2162 OJPEGWriteStreamQTable(tif
,3,mem
,len
);
2165 OJPEGWriteStreamDcTable(tif
,0,mem
,len
);
2168 OJPEGWriteStreamDcTable(tif
,1,mem
,len
);
2171 OJPEGWriteStreamDcTable(tif
,2,mem
,len
);
2174 OJPEGWriteStreamDcTable(tif
,3,mem
,len
);
2177 OJPEGWriteStreamAcTable(tif
,0,mem
,len
);
2180 OJPEGWriteStreamAcTable(tif
,1,mem
,len
);
2183 OJPEGWriteStreamAcTable(tif
,2,mem
,len
);
2186 OJPEGWriteStreamAcTable(tif
,3,mem
,len
);
2189 OJPEGWriteStreamDri(tif
,mem
,len
);
2192 OJPEGWriteStreamSof(tif
,mem
,len
);
2195 OJPEGWriteStreamSos(tif
,mem
,len
);
2197 case ososCompressed
:
2198 if (OJPEGWriteStreamCompressed(tif
,mem
,len
)==0)
2202 OJPEGWriteStreamRst(tif
,mem
,len
);
2205 OJPEGWriteStreamEoi(tif
,mem
,len
);
2213 OJPEGWriteStreamSoi(TIFF
* tif
, void** mem
, uint32
* len
)
2215 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2216 assert(OJPEG_BUFFER
>=2);
2217 sp
->out_buffer
[0]=255;
2218 sp
->out_buffer
[1]=JPEG_MARKER_SOI
;
2220 *mem
=(void*)sp
->out_buffer
;
2225 OJPEGWriteStreamQTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2227 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2228 if (sp
->qtable
[table_index
]!=0)
2230 *mem
=(void*)(sp
->qtable
[table_index
]+sizeof(uint32
));
2231 *len
=*((uint32
*)sp
->qtable
[table_index
])-sizeof(uint32
);
2237 OJPEGWriteStreamDcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2239 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2240 if (sp
->dctable
[table_index
]!=0)
2242 *mem
=(void*)(sp
->dctable
[table_index
]+sizeof(uint32
));
2243 *len
=*((uint32
*)sp
->dctable
[table_index
])-sizeof(uint32
);
2249 OJPEGWriteStreamAcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2251 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2252 if (sp
->actable
[table_index
]!=0)
2254 *mem
=(void*)(sp
->actable
[table_index
]+sizeof(uint32
));
2255 *len
=*((uint32
*)sp
->actable
[table_index
])-sizeof(uint32
);
2261 OJPEGWriteStreamDri(TIFF
* tif
, void** mem
, uint32
* len
)
2263 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2264 assert(OJPEG_BUFFER
>=6);
2265 if (sp
->restart_interval
!=0)
2267 sp
->out_buffer
[0]=255;
2268 sp
->out_buffer
[1]=JPEG_MARKER_DRI
;
2269 sp
->out_buffer
[2]=0;
2270 sp
->out_buffer
[3]=4;
2271 sp
->out_buffer
[4]=(sp
->restart_interval
>>8);
2272 sp
->out_buffer
[5]=(sp
->restart_interval
&255);
2274 *mem
=(void*)sp
->out_buffer
;
2280 OJPEGWriteStreamSof(TIFF
* tif
, void** mem
, uint32
* len
)
2282 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2284 assert(OJPEG_BUFFER
>=2+8+sp
->samples_per_pixel_per_plane
*3);
2285 assert(255>=8+sp
->samples_per_pixel_per_plane
*3);
2286 sp
->out_buffer
[0]=255;
2287 sp
->out_buffer
[1]=sp
->sof_marker_id
;
2289 sp
->out_buffer
[2]=0;
2290 sp
->out_buffer
[3]=8+sp
->samples_per_pixel_per_plane
*3;
2292 sp
->out_buffer
[4]=8;
2294 sp
->out_buffer
[5]=(uint8
)(sp
->sof_y
>>8);
2295 sp
->out_buffer
[6]=(sp
->sof_y
&255);
2297 sp
->out_buffer
[7]=(uint8
)(sp
->sof_x
>>8);
2298 sp
->out_buffer
[8]=(sp
->sof_x
&255);
2300 sp
->out_buffer
[9]=sp
->samples_per_pixel_per_plane
;
2301 for (m
=0; m
<sp
->samples_per_pixel_per_plane
; m
++)
2304 sp
->out_buffer
[10+m
*3]=sp
->sof_c
[sp
->plane_sample_offset
+m
];
2306 sp
->out_buffer
[10+m
*3+1]=sp
->sof_hv
[sp
->plane_sample_offset
+m
];
2308 sp
->out_buffer
[10+m
*3+2]=sp
->sof_tq
[sp
->plane_sample_offset
+m
];
2310 *len
=10+sp
->samples_per_pixel_per_plane
*3;
2311 *mem
=(void*)sp
->out_buffer
;
2316 OJPEGWriteStreamSos(TIFF
* tif
, void** mem
, uint32
* len
)
2318 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2320 assert(OJPEG_BUFFER
>=2+6+sp
->samples_per_pixel_per_plane
*2);
2321 assert(255>=6+sp
->samples_per_pixel_per_plane
*2);
2322 sp
->out_buffer
[0]=255;
2323 sp
->out_buffer
[1]=JPEG_MARKER_SOS
;
2325 sp
->out_buffer
[2]=0;
2326 sp
->out_buffer
[3]=6+sp
->samples_per_pixel_per_plane
*2;
2328 sp
->out_buffer
[4]=sp
->samples_per_pixel_per_plane
;
2329 for (m
=0; m
<sp
->samples_per_pixel_per_plane
; m
++)
2332 sp
->out_buffer
[5+m
*2]=sp
->sos_cs
[sp
->plane_sample_offset
+m
];
2334 sp
->out_buffer
[5+m
*2+1]=sp
->sos_tda
[sp
->plane_sample_offset
+m
];
2337 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2]=0;
2339 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2+1]=63;
2341 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2+2]=0;
2342 *len
=8+sp
->samples_per_pixel_per_plane
*2;
2343 *mem
=(void*)sp
->out_buffer
;
2348 OJPEGWriteStreamCompressed(TIFF
* tif
, void** mem
, uint32
* len
)
2350 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2351 if (sp
->in_buffer_togo
==0)
2353 if (OJPEGReadBufferFill(sp
)==0)
2355 assert(sp
->in_buffer_togo
>0);
2357 *len
=sp
->in_buffer_togo
;
2358 *mem
=(void*)sp
->in_buffer_cur
;
2359 sp
->in_buffer_togo
=0;
2360 if (sp
->in_buffer_file_togo
==0)
2362 switch(sp
->in_buffer_source
)
2365 if (sp
->in_buffer_next_strile
<sp
->in_buffer_strile_count
)
2366 sp
->out_state
=ososRst
;
2368 sp
->out_state
=ososEoi
;
2371 sp
->out_state
=ososEoi
;
2381 OJPEGWriteStreamRst(TIFF
* tif
, void** mem
, uint32
* len
)
2383 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2384 assert(OJPEG_BUFFER
>=2);
2385 sp
->out_buffer
[0]=255;
2386 sp
->out_buffer
[1]=JPEG_MARKER_RST0
+sp
->restart_index
;
2387 sp
->restart_index
++;
2388 if (sp
->restart_index
==8)
2389 sp
->restart_index
=0;
2391 *mem
=(void*)sp
->out_buffer
;
2392 sp
->out_state
=ososCompressed
;
2396 OJPEGWriteStreamEoi(TIFF
* tif
, void** mem
, uint32
* len
)
2398 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2399 assert(OJPEG_BUFFER
>=2);
2400 sp
->out_buffer
[0]=255;
2401 sp
->out_buffer
[1]=JPEG_MARKER_EOI
;
2403 *mem
=(void*)sp
->out_buffer
;
2406 #ifndef LIBJPEG_ENCAP_EXTERNAL
2408 jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
)
2410 if( SETJMP(sp
->exit_jmpbuf
) )
2413 jpeg_create_decompress(cinfo
);
2419 #ifndef LIBJPEG_ENCAP_EXTERNAL
2421 jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
)
2423 if( SETJMP(sp
->exit_jmpbuf
) )
2426 jpeg_read_header(cinfo
,require_image
);
2432 #ifndef LIBJPEG_ENCAP_EXTERNAL
2434 jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
)
2436 if( SETJMP(sp
->exit_jmpbuf
) )
2439 jpeg_start_decompress(cinfo
);
2445 #ifndef LIBJPEG_ENCAP_EXTERNAL
2447 jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
)
2449 if( SETJMP(sp
->exit_jmpbuf
) )
2452 jpeg_read_scanlines(cinfo
,scanlines
,max_lines
);
2458 #ifndef LIBJPEG_ENCAP_EXTERNAL
2460 jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
)
2462 if( SETJMP(sp
->exit_jmpbuf
) )
2465 jpeg_read_raw_data(cinfo
,data
,max_lines
);
2471 #ifndef LIBJPEG_ENCAP_EXTERNAL
2473 jpeg_encap_unwind(TIFF
* tif
)
2475 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2476 LONGJMP(sp
->exit_jmpbuf
,1);
2481 OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct
* cinfo
)
2483 char buffer
[JMSG_LENGTH_MAX
];
2484 (*cinfo
->err
->format_message
)(cinfo
,buffer
);
2485 TIFFWarningExt(((TIFF
*)(cinfo
->client_data
))->tif_clientdata
,"LibJpeg","%s",buffer
);
2489 OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct
* cinfo
)
2491 char buffer
[JMSG_LENGTH_MAX
];
2492 (*cinfo
->err
->format_message
)(cinfo
,buffer
);
2493 TIFFErrorExt(((TIFF
*)(cinfo
->client_data
))->tif_clientdata
,"LibJpeg","%s",buffer
);
2494 jpeg_encap_unwind((TIFF
*)(cinfo
->client_data
));
2498 OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct
* cinfo
)
2504 OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct
* cinfo
)
2506 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2507 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2510 if (OJPEGWriteStream(tif
,&mem
,&len
)==0)
2512 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Premature end of JPEG data");
2513 jpeg_encap_unwind(tif
);
2515 sp
->libjpeg_jpeg_source_mgr
.bytes_in_buffer
=len
;
2516 sp
->libjpeg_jpeg_source_mgr
.next_input_byte
=mem
;
2521 OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct
* cinfo
, long num_bytes
)
2523 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2525 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Unexpected error");
2526 jpeg_encap_unwind(tif
);
2530 #pragma warning( push )
2531 #pragma warning( disable : 4702 ) /* unreachable code */
2534 OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct
* cinfo
, int desired
)
2536 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2538 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Unexpected error");
2539 jpeg_encap_unwind(tif
);
2543 #pragma warning( pop )
2547 OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct
* cinfo
)