1 /* $Id: tif_ojpeg.c,v 1.69 2017-04-27 17:29:26 erouault Exp $ */
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 development. 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. Although
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 consistently 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 disadvantage 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
130 /* Configuration defines here are:
131 * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some environments,
132 * like eg LibTiffDelphi, this is not possible. For this reason, the actual calls to
133 * libjpeg, with longjump stuff, are encapsulated in dedicated functions. When
134 * JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declared external
135 * to this unit, and can be defined elsewhere to use stuff other then longjump.
136 * The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encapsulators
137 * here, internally, with normal longjump.
138 * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
139 * conveniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
140 * in place of plain setjmp. These macros will make it easier. It is useless
141 * to fiddle with these if you define JPEG_ENCAP_EXTERNAL.
142 * OJPEG_BUFFER: Define the size of the desired buffer here. Should be small enough so as to guarantee
143 * instant processing, optimal streaming and optimal use of processor cache, but also big
144 * enough so as to not result in significant call overhead. It should be at least a few
145 * bytes to accommodate some structures (this is verified in asserts), but it would not be
146 * sensible to make it this small anyway, and it should be at most 64K since it is indexed
147 * with uint16. We recommend 2K.
148 * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used anywhere and has
149 * absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
152 /* define LIBJPEG_ENCAP_EXTERNAL */
153 #define SETJMP(jbuf) setjmp(jbuf)
154 #define LONGJMP(jbuf,code) longjmp(jbuf,code)
155 #define JMP_BUF jmp_buf
156 #define OJPEG_BUFFER 2048
157 /* define EGYPTIANWALK */
159 #define JPEG_MARKER_SOF0 0xC0
160 #define JPEG_MARKER_SOF1 0xC1
161 #define JPEG_MARKER_SOF3 0xC3
162 #define JPEG_MARKER_DHT 0xC4
163 #define JPEG_MARKER_RST0 0XD0
164 #define JPEG_MARKER_SOI 0xD8
165 #define JPEG_MARKER_EOI 0xD9
166 #define JPEG_MARKER_SOS 0xDA
167 #define JPEG_MARKER_DQT 0xDB
168 #define JPEG_MARKER_DRI 0xDD
169 #define JPEG_MARKER_APP0 0xE0
170 #define JPEG_MARKER_COM 0xFE
172 #define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
173 #define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
174 #define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
175 #define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
176 #define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
177 #define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
178 #define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
180 static const TIFFField ojpegFields
[] = {
181 {TIFFTAG_JPEGIFOFFSET
,1,1,TIFF_LONG8
,0,TIFF_SETGET_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGINTERCHANGEFORMAT
,TRUE
,FALSE
,"JpegInterchangeFormat",NULL
},
182 {TIFFTAG_JPEGIFBYTECOUNT
,1,1,TIFF_LONG8
,0,TIFF_SETGET_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH
,TRUE
,FALSE
,"JpegInterchangeFormatLength",NULL
},
183 {TIFFTAG_JPEGQTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGQTABLES
,FALSE
,TRUE
,"JpegQTables",NULL
},
184 {TIFFTAG_JPEGDCTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGDCTABLES
,FALSE
,TRUE
,"JpegDcTables",NULL
},
185 {TIFFTAG_JPEGACTABLES
,TIFF_VARIABLE2
,TIFF_VARIABLE2
,TIFF_LONG8
,0,TIFF_SETGET_C32_UINT64
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGACTABLES
,FALSE
,TRUE
,"JpegAcTables",NULL
},
186 {TIFFTAG_JPEGPROC
,1,1,TIFF_SHORT
,0,TIFF_SETGET_UINT16
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGPROC
,FALSE
,FALSE
,"JpegProc",NULL
},
187 {TIFFTAG_JPEGRESTARTINTERVAL
,1,1,TIFF_SHORT
,0,TIFF_SETGET_UINT16
,TIFF_SETGET_UNDEFINED
,FIELD_OJPEG_JPEGRESTARTINTERVAL
,FALSE
,FALSE
,"JpegRestartInterval",NULL
},
190 #ifndef LIBJPEG_ENCAP_EXTERNAL
194 /* We undefine FAR to avoid conflict with JPEG definition */
201 Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
202 not defined. Unfortunately, the MinGW and Borland compilers include
203 a typedef for INT32, which causes a conflict. MSVC does not include
204 a conflicting typedef given the headers which are included.
206 #if defined(__BORLANDC__) || defined(__MINGW32__)
210 /* Define "boolean" as unsigned char, not int, per Windows custom. */
211 #if defined(__WIN32__) && !defined(__MINGW32__)
212 # ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
213 typedef unsigned char boolean
;
215 # define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
221 typedef struct jpeg_error_mgr jpeg_error_mgr
;
222 typedef struct jpeg_common_struct jpeg_common_struct
;
223 typedef struct jpeg_decompress_struct jpeg_decompress_struct
;
224 typedef struct jpeg_source_mgr jpeg_source_mgr
;
228 osibsJpegInterchangeFormat
,
231 } OJPEGStateInBufferSource
;
235 ososQTable0
,ososQTable1
,ososQTable2
,ososQTable3
,
236 ososDcTable0
,ososDcTable1
,ososDcTable2
,ososDcTable3
,
237 ososAcTable0
,ososAcTable1
,ososAcTable2
,ososAcTable3
,
244 } OJPEGStateOutState
;
249 #ifndef LIBJPEG_ENCAP_EXTERNAL
252 TIFFVGetMethod vgetparent
;
253 TIFFVSetMethod vsetparent
;
254 TIFFPrintMethod printdir
;
259 uint32 strile_length
;
260 uint32 strile_length_total
;
261 uint8 samples_per_pixel
;
262 uint8 plane_sample_offset
;
263 uint8 samples_per_pixel_per_plane
;
264 uint64 jpeg_interchange_format
;
265 uint64 jpeg_interchange_format_length
;
267 uint8 subsamplingcorrect
;
268 uint8 subsamplingcorrect_done
;
269 uint8 subsampling_tag
;
270 uint8 subsampling_hor
;
271 uint8 subsampling_ver
;
272 uint8 subsampling_force_desubsampling_inside_decompression
;
273 uint8 qtable_offset_count
;
274 uint8 dctable_offset_count
;
275 uint8 actable_offset_count
;
276 uint64 qtable_offset
[3];
277 uint64 dctable_offset
[3];
278 uint64 actable_offset
[3];
282 uint16 restart_interval
;
295 OJPEGStateInBufferSource in_buffer_source
;
296 uint32 in_buffer_next_strile
;
297 uint64 in_buffer_file_pos
;
298 uint64 in_buffer_file_togo
;
300 uint8 readheader_done
;
301 uint8 writeheader_done
;
302 uint16 write_cursample
;
303 uint32 write_curstrile
;
304 uint8 libjpeg_session_active
;
305 uint8 libjpeg_jpeg_query_style
;
306 jpeg_error_mgr libjpeg_jpeg_error_mgr
;
307 jpeg_decompress_struct libjpeg_jpeg_decompress_struct
;
308 jpeg_source_mgr libjpeg_jpeg_source_mgr
;
309 uint8 subsampling_convert_log
;
310 uint32 subsampling_convert_ylinelen
;
311 uint32 subsampling_convert_ylines
;
312 uint32 subsampling_convert_clinelen
;
313 uint32 subsampling_convert_clines
;
314 uint32 subsampling_convert_ybuflen
;
315 uint32 subsampling_convert_cbuflen
;
316 uint32 subsampling_convert_ycbcrbuflen
;
317 uint8
* subsampling_convert_ycbcrbuf
;
318 uint8
* subsampling_convert_ybuf
;
319 uint8
* subsampling_convert_cbbuf
;
320 uint8
* subsampling_convert_crbuf
;
321 uint32 subsampling_convert_ycbcrimagelen
;
322 uint8
** subsampling_convert_ycbcrimage
;
323 uint32 subsampling_convert_clinelenout
;
324 uint32 subsampling_convert_state
;
325 uint32 bytes_per_line
; /* if the codec outputs subsampled data, a 'line' in bytes_per_line */
326 uint32 lines_per_strile
; /* and lines_per_strile means subsampling_ver desubsampled rows */
327 OJPEGStateInBufferSource in_buffer_source
;
328 uint32 in_buffer_next_strile
;
329 uint32 in_buffer_strile_count
;
330 uint64 in_buffer_file_pos
;
331 uint8 in_buffer_file_pos_log
;
332 uint64 in_buffer_file_togo
;
333 uint16 in_buffer_togo
;
334 uint8
* in_buffer_cur
;
335 uint8 in_buffer
[OJPEG_BUFFER
];
336 OJPEGStateOutState out_state
;
337 uint8 out_buffer
[OJPEG_BUFFER
];
341 static int OJPEGVGetField(TIFF
* tif
, uint32 tag
, va_list ap
);
342 static int OJPEGVSetField(TIFF
* tif
, uint32 tag
, va_list ap
);
343 static void OJPEGPrintDir(TIFF
* tif
, FILE* fd
, long flags
);
345 static int OJPEGFixupTags(TIFF
* tif
);
346 static int OJPEGSetupDecode(TIFF
* tif
);
347 static int OJPEGPreDecode(TIFF
* tif
, uint16 s
);
348 static int OJPEGPreDecodeSkipRaw(TIFF
* tif
);
349 static int OJPEGPreDecodeSkipScanlines(TIFF
* tif
);
350 static int OJPEGDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
);
351 static int OJPEGDecodeRaw(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
352 static int OJPEGDecodeScanlines(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
353 static void OJPEGPostDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
);
354 static int OJPEGSetupEncode(TIFF
* tif
);
355 static int OJPEGPreEncode(TIFF
* tif
, uint16 s
);
356 static int OJPEGEncode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
);
357 static int OJPEGPostEncode(TIFF
* tif
);
358 static void OJPEGCleanup(TIFF
* tif
);
360 static void OJPEGSubsamplingCorrect(TIFF
* tif
);
361 static int OJPEGReadHeaderInfo(TIFF
* tif
);
362 static int OJPEGReadSecondarySos(TIFF
* tif
, uint16 s
);
363 static int OJPEGWriteHeaderInfo(TIFF
* tif
);
364 static void OJPEGLibjpegSessionAbort(TIFF
* tif
);
366 static int OJPEGReadHeaderInfoSec(TIFF
* tif
);
367 static int OJPEGReadHeaderInfoSecStreamDri(TIFF
* tif
);
368 static int OJPEGReadHeaderInfoSecStreamDqt(TIFF
* tif
);
369 static int OJPEGReadHeaderInfoSecStreamDht(TIFF
* tif
);
370 static int OJPEGReadHeaderInfoSecStreamSof(TIFF
* tif
, uint8 marker_id
);
371 static int OJPEGReadHeaderInfoSecStreamSos(TIFF
* tif
);
372 static int OJPEGReadHeaderInfoSecTablesQTable(TIFF
* tif
);
373 static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF
* tif
);
374 static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF
* tif
);
376 static int OJPEGReadBufferFill(OJPEGState
* sp
);
377 static int OJPEGReadByte(OJPEGState
* sp
, uint8
* byte
);
378 static int OJPEGReadBytePeek(OJPEGState
* sp
, uint8
* byte
);
379 static void OJPEGReadByteAdvance(OJPEGState
* sp
);
380 static int OJPEGReadWord(OJPEGState
* sp
, uint16
* word
);
381 static int OJPEGReadBlock(OJPEGState
* sp
, uint16 len
, void* mem
);
382 static void OJPEGReadSkip(OJPEGState
* sp
, uint16 len
);
384 static int OJPEGWriteStream(TIFF
* tif
, void** mem
, uint32
* len
);
385 static void OJPEGWriteStreamSoi(TIFF
* tif
, void** mem
, uint32
* len
);
386 static void OJPEGWriteStreamQTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
387 static void OJPEGWriteStreamDcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
388 static void OJPEGWriteStreamAcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
);
389 static void OJPEGWriteStreamDri(TIFF
* tif
, void** mem
, uint32
* len
);
390 static void OJPEGWriteStreamSof(TIFF
* tif
, void** mem
, uint32
* len
);
391 static void OJPEGWriteStreamSos(TIFF
* tif
, void** mem
, uint32
* len
);
392 static int OJPEGWriteStreamCompressed(TIFF
* tif
, void** mem
, uint32
* len
);
393 static void OJPEGWriteStreamRst(TIFF
* tif
, void** mem
, uint32
* len
);
394 static void OJPEGWriteStreamEoi(TIFF
* tif
, void** mem
, uint32
* len
);
396 #ifdef LIBJPEG_ENCAP_EXTERNAL
397 extern int jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
398 extern int jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
);
399 extern int jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
400 extern int jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
);
401 extern int jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
);
402 extern void jpeg_encap_unwind(TIFF
* tif
);
404 static int jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* j
);
405 static int jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
);
406 static int jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
);
407 static int jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
);
408 static int jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
);
409 static void jpeg_encap_unwind(TIFF
* tif
);
412 static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct
* cinfo
);
413 static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct
* cinfo
);
414 static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct
* cinfo
);
415 static boolean
OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct
* cinfo
);
416 static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct
* cinfo
, long num_bytes
);
417 static boolean
OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct
* cinfo
, int desired
);
418 static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct
* cinfo
);
421 TIFFInitOJPEG(TIFF
* tif
, int scheme
)
423 static const char module
[]="TIFFInitOJPEG";
426 assert(scheme
==COMPRESSION_OJPEG
);
429 * Merge codec-specific tag information.
431 if (!_TIFFMergeFields(tif
, ojpegFields
, TIFFArrayCount(ojpegFields
))) {
432 TIFFErrorExt(tif
->tif_clientdata
, module
,
433 "Merging Old JPEG codec-specific tags failed");
438 sp
=_TIFFmalloc(sizeof(OJPEGState
));
441 TIFFErrorExt(tif
->tif_clientdata
,module
,"No space for OJPEG state block");
444 _TIFFmemset(sp
,0,sizeof(OJPEGState
));
447 sp
->subsampling_hor
=2;
448 sp
->subsampling_ver
=2;
449 TIFFSetField(tif
,TIFFTAG_YCBCRSUBSAMPLING
,2,2);
450 /* tif codec methods */
451 tif
->tif_fixuptags
=OJPEGFixupTags
;
452 tif
->tif_setupdecode
=OJPEGSetupDecode
;
453 tif
->tif_predecode
=OJPEGPreDecode
;
454 tif
->tif_postdecode
=OJPEGPostDecode
;
455 tif
->tif_decoderow
=OJPEGDecode
;
456 tif
->tif_decodestrip
=OJPEGDecode
;
457 tif
->tif_decodetile
=OJPEGDecode
;
458 tif
->tif_setupencode
=OJPEGSetupEncode
;
459 tif
->tif_preencode
=OJPEGPreEncode
;
460 tif
->tif_postencode
=OJPEGPostEncode
;
461 tif
->tif_encoderow
=OJPEGEncode
;
462 tif
->tif_encodestrip
=OJPEGEncode
;
463 tif
->tif_encodetile
=OJPEGEncode
;
464 tif
->tif_cleanup
=OJPEGCleanup
;
465 tif
->tif_data
=(uint8
*)sp
;
466 /* tif tag methods */
467 sp
->vgetparent
=tif
->tif_tagmethods
.vgetfield
;
468 tif
->tif_tagmethods
.vgetfield
=OJPEGVGetField
;
469 sp
->vsetparent
=tif
->tif_tagmethods
.vsetfield
;
470 tif
->tif_tagmethods
.vsetfield
=OJPEGVSetField
;
471 sp
->printdir
=tif
->tif_tagmethods
.printdir
;
472 tif
->tif_tagmethods
.printdir
=OJPEGPrintDir
;
473 /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
474 Some others do, but have totally meaningless or corrupt values
475 in these tags. In these cases, the JpegInterchangeFormat stream is
476 reliable. In any case, this decoder reads the compressed data itself,
477 from the most reliable locations, and we need to notify encapsulating
478 LibTiff not to read raw strips or tiles for us. */
479 tif
->tif_flags
|=TIFF_NOREADRAW
;
484 OJPEGVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
486 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
489 case TIFFTAG_JPEGIFOFFSET
:
490 *va_arg(ap
,uint64
*)=(uint64
)sp
->jpeg_interchange_format
;
492 case TIFFTAG_JPEGIFBYTECOUNT
:
493 *va_arg(ap
,uint64
*)=(uint64
)sp
->jpeg_interchange_format_length
;
495 case TIFFTAG_YCBCRSUBSAMPLING
:
496 if (sp
->subsamplingcorrect_done
==0)
497 OJPEGSubsamplingCorrect(tif
);
498 *va_arg(ap
,uint16
*)=(uint16
)sp
->subsampling_hor
;
499 *va_arg(ap
,uint16
*)=(uint16
)sp
->subsampling_ver
;
501 case TIFFTAG_JPEGQTABLES
:
502 *va_arg(ap
,uint32
*)=(uint32
)sp
->qtable_offset_count
;
503 *va_arg(ap
,void**)=(void*)sp
->qtable_offset
;
505 case TIFFTAG_JPEGDCTABLES
:
506 *va_arg(ap
,uint32
*)=(uint32
)sp
->dctable_offset_count
;
507 *va_arg(ap
,void**)=(void*)sp
->dctable_offset
;
509 case TIFFTAG_JPEGACTABLES
:
510 *va_arg(ap
,uint32
*)=(uint32
)sp
->actable_offset_count
;
511 *va_arg(ap
,void**)=(void*)sp
->actable_offset
;
513 case TIFFTAG_JPEGPROC
:
514 *va_arg(ap
,uint16
*)=(uint16
)sp
->jpeg_proc
;
516 case TIFFTAG_JPEGRESTARTINTERVAL
:
517 *va_arg(ap
,uint16
*)=sp
->restart_interval
;
520 return (*sp
->vgetparent
)(tif
,tag
,ap
);
526 OJPEGVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
528 static const char module
[]="OJPEGVSetField";
529 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
533 const TIFFField
* fip
;
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 fip
= TIFFFieldWithTag(tif
,tag
);
605 if( fip
== NULL
) /* shouldn't happen */
607 TIFFSetFieldBit(tif
,fip
->field_bit
);
608 tif
->tif_flags
|=TIFF_DIRTYDIRECT
;
613 OJPEGPrintDir(TIFF
* tif
, FILE* fd
, long flags
)
615 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
619 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGINTERCHANGEFORMAT
))
620 fprintf(fd
," JpegInterchangeFormat: " TIFF_UINT64_FORMAT
"\n",(TIFF_UINT64_T
)sp
->jpeg_interchange_format
);
621 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH
))
622 fprintf(fd
," JpegInterchangeFormatLength: " TIFF_UINT64_FORMAT
"\n",(TIFF_UINT64_T
)sp
->jpeg_interchange_format_length
);
623 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGQTABLES
))
625 fprintf(fd
," JpegQTables:");
626 for (m
=0; m
<sp
->qtable_offset_count
; m
++)
627 fprintf(fd
," " TIFF_UINT64_FORMAT
,(TIFF_UINT64_T
)sp
->qtable_offset
[m
]);
630 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGDCTABLES
))
632 fprintf(fd
," JpegDcTables:");
633 for (m
=0; m
<sp
->dctable_offset_count
; m
++)
634 fprintf(fd
," " TIFF_UINT64_FORMAT
,(TIFF_UINT64_T
)sp
->dctable_offset
[m
]);
637 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGACTABLES
))
639 fprintf(fd
," JpegAcTables:");
640 for (m
=0; m
<sp
->actable_offset_count
; m
++)
641 fprintf(fd
," " TIFF_UINT64_FORMAT
,(TIFF_UINT64_T
)sp
->actable_offset
[m
]);
644 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGPROC
))
645 fprintf(fd
," JpegProc: %u\n",(unsigned int)sp
->jpeg_proc
);
646 if (TIFFFieldSet(tif
,FIELD_OJPEG_JPEGRESTARTINTERVAL
))
647 fprintf(fd
," JpegRestartInterval: %u\n",(unsigned int)sp
->restart_interval
);
649 (*sp
->printdir
)(tif
, fd
, flags
);
653 OJPEGFixupTags(TIFF
* tif
)
660 OJPEGSetupDecode(TIFF
* tif
)
662 static const char module
[]="OJPEGSetupDecode";
663 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");
668 OJPEGPreDecode(TIFF
* tif
, uint16 s
)
670 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
672 if (sp
->subsamplingcorrect_done
==0)
673 OJPEGSubsamplingCorrect(tif
);
674 if (sp
->readheader_done
==0)
676 if (OJPEGReadHeaderInfo(tif
)==0)
679 if (sp
->sos_end
[s
].log
==0)
681 if (OJPEGReadSecondarySos(tif
,s
)==0)
688 if ((sp
->writeheader_done
!=0) && ((sp
->write_cursample
!=s
) || (sp
->write_curstrile
>m
)))
690 if (sp
->libjpeg_session_active
!=0)
691 OJPEGLibjpegSessionAbort(tif
);
692 sp
->writeheader_done
=0;
694 if (sp
->writeheader_done
==0)
696 sp
->plane_sample_offset
=(uint8
)s
;
697 sp
->write_cursample
=s
;
698 sp
->write_curstrile
=s
*tif
->tif_dir
.td_stripsperimage
;
699 if ((sp
->in_buffer_file_pos_log
==0) ||
700 (sp
->in_buffer_file_pos
-sp
->in_buffer_togo
!=sp
->sos_end
[s
].in_buffer_file_pos
))
702 sp
->in_buffer_source
=sp
->sos_end
[s
].in_buffer_source
;
703 sp
->in_buffer_next_strile
=sp
->sos_end
[s
].in_buffer_next_strile
;
704 sp
->in_buffer_file_pos
=sp
->sos_end
[s
].in_buffer_file_pos
;
705 sp
->in_buffer_file_pos_log
=0;
706 sp
->in_buffer_file_togo
=sp
->sos_end
[s
].in_buffer_file_togo
;
707 sp
->in_buffer_togo
=0;
710 if (OJPEGWriteHeaderInfo(tif
)==0)
713 while (sp
->write_curstrile
<m
)
715 if (sp
->libjpeg_jpeg_query_style
==0)
717 if (OJPEGPreDecodeSkipRaw(tif
)==0)
722 if (OJPEGPreDecodeSkipScanlines(tif
)==0)
725 sp
->write_curstrile
++;
732 OJPEGPreDecodeSkipRaw(TIFF
* tif
)
734 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
736 m
=sp
->lines_per_strile
;
737 if (sp
->subsampling_convert_state
!=0)
739 if (sp
->subsampling_convert_clines
-sp
->subsampling_convert_state
>=m
)
741 sp
->subsampling_convert_state
+=m
;
742 if (sp
->subsampling_convert_state
==sp
->subsampling_convert_clines
)
743 sp
->subsampling_convert_state
=0;
746 m
-=sp
->subsampling_convert_clines
-sp
->subsampling_convert_state
;
747 sp
->subsampling_convert_state
=0;
749 while (m
>=sp
->subsampling_convert_clines
)
751 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
753 m
-=sp
->subsampling_convert_clines
;
757 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
759 sp
->subsampling_convert_state
=m
;
765 OJPEGPreDecodeSkipScanlines(TIFF
* tif
)
767 static const char module
[]="OJPEGPreDecodeSkipScanlines";
768 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
770 if (sp
->skip_buffer
==NULL
)
772 sp
->skip_buffer
=_TIFFmalloc(sp
->bytes_per_line
);
773 if (sp
->skip_buffer
==NULL
)
775 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
779 for (m
=0; m
<sp
->lines_per_strile
; m
++)
781 if (jpeg_read_scanlines_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),&sp
->skip_buffer
,1)==0)
788 OJPEGDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
)
790 static const char module
[]="OJPEGDecode";
791 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
793 if( !sp
->decoder_ok
)
795 TIFFErrorExt(tif
->tif_clientdata
,module
,"Cannot decode: decoder not correctly initialized");
798 if (sp
->libjpeg_jpeg_query_style
==0)
800 if (OJPEGDecodeRaw(tif
,buf
,cc
)==0)
805 if (OJPEGDecodeScanlines(tif
,buf
,cc
)==0)
812 OJPEGDecodeRaw(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
814 static const char module
[]="OJPEGDecodeRaw";
815 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
825 if (cc
%sp
->bytes_per_line
!=0)
827 TIFFErrorExt(tif
->tif_clientdata
,module
,"Fractional scanline not read");
835 if (sp
->subsampling_convert_state
==0)
837 if (jpeg_read_raw_data_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),sp
->subsampling_convert_ycbcrimage
,sp
->subsampling_ver
*8)==0)
840 oy
=sp
->subsampling_convert_ybuf
+sp
->subsampling_convert_state
*sp
->subsampling_ver
*sp
->subsampling_convert_ylinelen
;
841 ocb
=sp
->subsampling_convert_cbbuf
+sp
->subsampling_convert_state
*sp
->subsampling_convert_clinelen
;
842 ocr
=sp
->subsampling_convert_crbuf
+sp
->subsampling_convert_state
*sp
->subsampling_convert_clinelen
;
844 for (q
=0; q
<sp
->subsampling_convert_clinelenout
; q
++)
847 for (sy
=0; sy
<sp
->subsampling_ver
; sy
++)
849 for (sx
=0; sx
<sp
->subsampling_hor
; sx
++)
851 r
+=sp
->subsampling_convert_ylinelen
-sp
->subsampling_hor
;
853 oy
+=sp
->subsampling_hor
;
857 sp
->subsampling_convert_state
++;
858 if (sp
->subsampling_convert_state
==sp
->subsampling_convert_clines
)
859 sp
->subsampling_convert_state
=0;
860 m
+=sp
->bytes_per_line
;
861 n
-=sp
->bytes_per_line
;
867 OJPEGDecodeScanlines(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
869 static const char module
[]="OJPEGDecodeScanlines";
870 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
873 if (cc
%sp
->bytes_per_line
!=0)
875 TIFFErrorExt(tif
->tif_clientdata
,module
,"Fractional scanline not read");
883 if (jpeg_read_scanlines_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),&m
,1)==0)
885 m
+=sp
->bytes_per_line
;
886 n
-=sp
->bytes_per_line
;
892 OJPEGPostDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
894 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
897 sp
->write_curstrile
++;
898 if (sp
->write_curstrile
%tif
->tif_dir
.td_stripsperimage
==0)
900 assert(sp
->libjpeg_session_active
!=0);
901 OJPEGLibjpegSessionAbort(tif
);
902 sp
->writeheader_done
=0;
907 OJPEGSetupEncode(TIFF
* tif
)
909 static const char module
[]="OJPEGSetupEncode";
910 TIFFErrorExt(tif
->tif_clientdata
,module
,"OJPEG encoding not supported; use new-style JPEG compression instead");
915 OJPEGPreEncode(TIFF
* tif
, uint16 s
)
917 static const char module
[]="OJPEGPreEncode";
919 TIFFErrorExt(tif
->tif_clientdata
,module
,"OJPEG encoding not supported; use new-style JPEG compression instead");
924 OJPEGEncode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
, uint16 s
)
926 static const char module
[]="OJPEGEncode";
930 TIFFErrorExt(tif
->tif_clientdata
,module
,"OJPEG encoding not supported; use new-style JPEG compression instead");
935 OJPEGPostEncode(TIFF
* tif
)
937 static const char module
[]="OJPEGPostEncode";
938 TIFFErrorExt(tif
->tif_clientdata
,module
,"OJPEG encoding not supported; use new-style JPEG compression instead");
943 OJPEGCleanup(TIFF
* tif
)
945 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
948 tif
->tif_tagmethods
.vgetfield
=sp
->vgetparent
;
949 tif
->tif_tagmethods
.vsetfield
=sp
->vsetparent
;
950 tif
->tif_tagmethods
.printdir
=sp
->printdir
;
951 if (sp
->qtable
[0]!=0)
952 _TIFFfree(sp
->qtable
[0]);
953 if (sp
->qtable
[1]!=0)
954 _TIFFfree(sp
->qtable
[1]);
955 if (sp
->qtable
[2]!=0)
956 _TIFFfree(sp
->qtable
[2]);
957 if (sp
->qtable
[3]!=0)
958 _TIFFfree(sp
->qtable
[3]);
959 if (sp
->dctable
[0]!=0)
960 _TIFFfree(sp
->dctable
[0]);
961 if (sp
->dctable
[1]!=0)
962 _TIFFfree(sp
->dctable
[1]);
963 if (sp
->dctable
[2]!=0)
964 _TIFFfree(sp
->dctable
[2]);
965 if (sp
->dctable
[3]!=0)
966 _TIFFfree(sp
->dctable
[3]);
967 if (sp
->actable
[0]!=0)
968 _TIFFfree(sp
->actable
[0]);
969 if (sp
->actable
[1]!=0)
970 _TIFFfree(sp
->actable
[1]);
971 if (sp
->actable
[2]!=0)
972 _TIFFfree(sp
->actable
[2]);
973 if (sp
->actable
[3]!=0)
974 _TIFFfree(sp
->actable
[3]);
975 if (sp
->libjpeg_session_active
!=0)
976 OJPEGLibjpegSessionAbort(tif
);
977 if (sp
->subsampling_convert_ycbcrbuf
!=0)
978 _TIFFfree(sp
->subsampling_convert_ycbcrbuf
);
979 if (sp
->subsampling_convert_ycbcrimage
!=0)
980 _TIFFfree(sp
->subsampling_convert_ycbcrimage
);
981 if (sp
->skip_buffer
!=0)
982 _TIFFfree(sp
->skip_buffer
);
985 _TIFFSetDefaultCompressionState(tif
);
990 OJPEGSubsamplingCorrect(TIFF
* tif
)
992 static const char module
[]="OJPEGSubsamplingCorrect";
993 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
996 _TIFFFillStriles( tif
);
998 assert(sp
->subsamplingcorrect_done
==0);
999 if ((tif
->tif_dir
.td_samplesperpixel
!=3) || ((tif
->tif_dir
.td_photometric
!=PHOTOMETRIC_YCBCR
) &&
1000 (tif
->tif_dir
.td_photometric
!=PHOTOMETRIC_ITULAB
)))
1002 if (sp
->subsampling_tag
!=0)
1003 TIFFWarningExt(tif
->tif_clientdata
,module
,"Subsampling tag not appropriate for this Photometric and/or SamplesPerPixel");
1004 sp
->subsampling_hor
=1;
1005 sp
->subsampling_ver
=1;
1006 sp
->subsampling_force_desubsampling_inside_decompression
=0;
1010 sp
->subsamplingcorrect_done
=1;
1011 mh
=sp
->subsampling_hor
;
1012 mv
=sp
->subsampling_ver
;
1013 sp
->subsamplingcorrect
=1;
1014 OJPEGReadHeaderInfoSec(tif
);
1015 if (sp
->subsampling_force_desubsampling_inside_decompression
!=0)
1017 sp
->subsampling_hor
=1;
1018 sp
->subsampling_ver
=1;
1020 sp
->subsamplingcorrect
=0;
1021 if (((sp
->subsampling_hor
!=mh
) || (sp
->subsampling_ver
!=mv
)) && (sp
->subsampling_force_desubsampling_inside_decompression
==0))
1023 if (sp
->subsampling_tag
==0)
1024 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
);
1026 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
);
1028 if (sp
->subsampling_force_desubsampling_inside_decompression
!=0)
1030 if (sp
->subsampling_tag
==0)
1031 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");
1033 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
);
1035 if (sp
->subsampling_force_desubsampling_inside_decompression
==0)
1037 if (sp
->subsampling_hor
<sp
->subsampling_ver
)
1038 TIFFWarningExt(tif
->tif_clientdata
,module
,"Subsampling values [%d,%d] are not allowed in TIFF",sp
->subsampling_hor
,sp
->subsampling_ver
);
1041 sp
->subsamplingcorrect_done
=1;
1045 OJPEGReadHeaderInfo(TIFF
* tif
)
1047 static const char module
[]="OJPEGReadHeaderInfo";
1048 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1049 assert(sp
->readheader_done
==0);
1050 sp
->image_width
=tif
->tif_dir
.td_imagewidth
;
1051 sp
->image_length
=tif
->tif_dir
.td_imagelength
;
1054 sp
->strile_width
=tif
->tif_dir
.td_tilewidth
;
1055 sp
->strile_length
=tif
->tif_dir
.td_tilelength
;
1056 sp
->strile_length_total
=((sp
->image_length
+sp
->strile_length
-1)/sp
->strile_length
)*sp
->strile_length
;
1060 sp
->strile_width
=sp
->image_width
;
1061 sp
->strile_length
=tif
->tif_dir
.td_rowsperstrip
;
1062 sp
->strile_length_total
=sp
->image_length
;
1064 if (tif
->tif_dir
.td_samplesperpixel
==1)
1066 sp
->samples_per_pixel
=1;
1067 sp
->plane_sample_offset
=0;
1068 sp
->samples_per_pixel_per_plane
=sp
->samples_per_pixel
;
1069 sp
->subsampling_hor
=1;
1070 sp
->subsampling_ver
=1;
1074 if (tif
->tif_dir
.td_samplesperpixel
!=3)
1076 TIFFErrorExt(tif
->tif_clientdata
,module
,"SamplesPerPixel %d not supported for this compression scheme",sp
->samples_per_pixel
);
1079 sp
->samples_per_pixel
=3;
1080 sp
->plane_sample_offset
=0;
1081 if (tif
->tif_dir
.td_planarconfig
==PLANARCONFIG_CONTIG
)
1082 sp
->samples_per_pixel_per_plane
=3;
1084 sp
->samples_per_pixel_per_plane
=1;
1086 if (sp
->strile_length
<sp
->image_length
)
1088 if (sp
->strile_length
%(sp
->subsampling_ver
*8)!=0)
1090 TIFFErrorExt(tif
->tif_clientdata
,module
,"Incompatible vertical subsampling and image strip/tile length");
1093 sp
->restart_interval
=(uint16
)(((sp
->strile_width
+sp
->subsampling_hor
*8-1)/(sp
->subsampling_hor
*8))*(sp
->strile_length
/(sp
->subsampling_ver
*8)));
1095 if (OJPEGReadHeaderInfoSec(tif
)==0)
1097 sp
->sos_end
[0].log
=1;
1098 sp
->sos_end
[0].in_buffer_source
=sp
->in_buffer_source
;
1099 sp
->sos_end
[0].in_buffer_next_strile
=sp
->in_buffer_next_strile
;
1100 sp
->sos_end
[0].in_buffer_file_pos
=sp
->in_buffer_file_pos
-sp
->in_buffer_togo
;
1101 sp
->sos_end
[0].in_buffer_file_togo
=sp
->in_buffer_file_togo
+sp
->in_buffer_togo
;
1102 sp
->readheader_done
=1;
1107 OJPEGReadSecondarySos(TIFF
* tif
, uint16 s
)
1109 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1113 assert(sp
->sos_end
[0].log
!=0);
1114 assert(sp
->sos_end
[s
].log
==0);
1115 sp
->plane_sample_offset
=(uint8
)(s
-1);
1116 while(sp
->sos_end
[sp
->plane_sample_offset
].log
==0)
1117 sp
->plane_sample_offset
--;
1118 sp
->in_buffer_source
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_source
;
1119 sp
->in_buffer_next_strile
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_next_strile
;
1120 sp
->in_buffer_file_pos
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_pos
;
1121 sp
->in_buffer_file_pos_log
=0;
1122 sp
->in_buffer_file_togo
=sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_togo
;
1123 sp
->in_buffer_togo
=0;
1124 sp
->in_buffer_cur
=0;
1125 while(sp
->plane_sample_offset
<s
)
1129 if (OJPEGReadByte(sp
,&m
)==0)
1135 if (OJPEGReadByte(sp
,&m
)==0)
1140 if (m
==JPEG_MARKER_SOS
)
1144 sp
->plane_sample_offset
++;
1145 if (OJPEGReadHeaderInfoSecStreamSos(tif
)==0)
1147 sp
->sos_end
[sp
->plane_sample_offset
].log
=1;
1148 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_source
=sp
->in_buffer_source
;
1149 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_next_strile
=sp
->in_buffer_next_strile
;
1150 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_pos
=sp
->in_buffer_file_pos
-sp
->in_buffer_togo
;
1151 sp
->sos_end
[sp
->plane_sample_offset
].in_buffer_file_togo
=sp
->in_buffer_file_togo
+sp
->in_buffer_togo
;
1157 OJPEGWriteHeaderInfo(TIFF
* tif
)
1159 static const char module
[]="OJPEGWriteHeaderInfo";
1160 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1163 /* if a previous attempt failed, don't try again */
1164 if (sp
->libjpeg_session_active
!= 0)
1166 sp
->out_state
=ososSoi
;
1167 sp
->restart_index
=0;
1168 jpeg_std_error(&(sp
->libjpeg_jpeg_error_mgr
));
1169 sp
->libjpeg_jpeg_error_mgr
.output_message
=OJPEGLibjpegJpegErrorMgrOutputMessage
;
1170 sp
->libjpeg_jpeg_error_mgr
.error_exit
=OJPEGLibjpegJpegErrorMgrErrorExit
;
1171 sp
->libjpeg_jpeg_decompress_struct
.err
=&(sp
->libjpeg_jpeg_error_mgr
);
1172 sp
->libjpeg_jpeg_decompress_struct
.client_data
=(void*)tif
;
1173 if (jpeg_create_decompress_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
))==0)
1175 sp
->libjpeg_session_active
=1;
1176 sp
->libjpeg_jpeg_source_mgr
.bytes_in_buffer
=0;
1177 sp
->libjpeg_jpeg_source_mgr
.init_source
=OJPEGLibjpegJpegSourceMgrInitSource
;
1178 sp
->libjpeg_jpeg_source_mgr
.fill_input_buffer
=OJPEGLibjpegJpegSourceMgrFillInputBuffer
;
1179 sp
->libjpeg_jpeg_source_mgr
.skip_input_data
=OJPEGLibjpegJpegSourceMgrSkipInputData
;
1180 sp
->libjpeg_jpeg_source_mgr
.resync_to_restart
=OJPEGLibjpegJpegSourceMgrResyncToRestart
;
1181 sp
->libjpeg_jpeg_source_mgr
.term_source
=OJPEGLibjpegJpegSourceMgrTermSource
;
1182 sp
->libjpeg_jpeg_decompress_struct
.src
=&(sp
->libjpeg_jpeg_source_mgr
);
1183 if (jpeg_read_header_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
),1)==0)
1185 if ((sp
->subsampling_force_desubsampling_inside_decompression
==0) && (sp
->samples_per_pixel_per_plane
>1))
1187 sp
->libjpeg_jpeg_decompress_struct
.raw_data_out
=1;
1188 #if JPEG_LIB_VERSION >= 70
1189 sp
->libjpeg_jpeg_decompress_struct
.do_fancy_upsampling
=FALSE
;
1191 sp
->libjpeg_jpeg_query_style
=0;
1192 if (sp
->subsampling_convert_log
==0)
1194 assert(sp
->subsampling_convert_ycbcrbuf
==0);
1195 assert(sp
->subsampling_convert_ycbcrimage
==0);
1196 sp
->subsampling_convert_ylinelen
=((sp
->strile_width
+sp
->subsampling_hor
*8-1)/(sp
->subsampling_hor
*8)*sp
->subsampling_hor
*8);
1197 sp
->subsampling_convert_ylines
=sp
->subsampling_ver
*8;
1198 sp
->subsampling_convert_clinelen
=sp
->subsampling_convert_ylinelen
/sp
->subsampling_hor
;
1199 sp
->subsampling_convert_clines
=8;
1200 sp
->subsampling_convert_ybuflen
=sp
->subsampling_convert_ylinelen
*sp
->subsampling_convert_ylines
;
1201 sp
->subsampling_convert_cbuflen
=sp
->subsampling_convert_clinelen
*sp
->subsampling_convert_clines
;
1202 sp
->subsampling_convert_ycbcrbuflen
=sp
->subsampling_convert_ybuflen
+2*sp
->subsampling_convert_cbuflen
;
1203 sp
->subsampling_convert_ycbcrbuf
=_TIFFmalloc(sp
->subsampling_convert_ycbcrbuflen
);
1204 if (sp
->subsampling_convert_ycbcrbuf
==0)
1206 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1209 sp
->subsampling_convert_ybuf
=sp
->subsampling_convert_ycbcrbuf
;
1210 sp
->subsampling_convert_cbbuf
=sp
->subsampling_convert_ybuf
+sp
->subsampling_convert_ybuflen
;
1211 sp
->subsampling_convert_crbuf
=sp
->subsampling_convert_cbbuf
+sp
->subsampling_convert_cbuflen
;
1212 sp
->subsampling_convert_ycbcrimagelen
=3+sp
->subsampling_convert_ylines
+2*sp
->subsampling_convert_clines
;
1213 sp
->subsampling_convert_ycbcrimage
=_TIFFmalloc(sp
->subsampling_convert_ycbcrimagelen
*sizeof(uint8
*));
1214 if (sp
->subsampling_convert_ycbcrimage
==0)
1216 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1219 m
=sp
->subsampling_convert_ycbcrimage
;
1220 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3);
1221 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3+sp
->subsampling_convert_ylines
);
1222 *m
++=(uint8
*)(sp
->subsampling_convert_ycbcrimage
+3+sp
->subsampling_convert_ylines
+sp
->subsampling_convert_clines
);
1223 for (n
=0; n
<sp
->subsampling_convert_ylines
; n
++)
1224 *m
++=sp
->subsampling_convert_ybuf
+n
*sp
->subsampling_convert_ylinelen
;
1225 for (n
=0; n
<sp
->subsampling_convert_clines
; n
++)
1226 *m
++=sp
->subsampling_convert_cbbuf
+n
*sp
->subsampling_convert_clinelen
;
1227 for (n
=0; n
<sp
->subsampling_convert_clines
; n
++)
1228 *m
++=sp
->subsampling_convert_crbuf
+n
*sp
->subsampling_convert_clinelen
;
1229 sp
->subsampling_convert_clinelenout
=((sp
->strile_width
+sp
->subsampling_hor
-1)/sp
->subsampling_hor
);
1230 sp
->subsampling_convert_state
=0;
1231 sp
->bytes_per_line
=sp
->subsampling_convert_clinelenout
*(sp
->subsampling_ver
*sp
->subsampling_hor
+2);
1232 sp
->lines_per_strile
=((sp
->strile_length
+sp
->subsampling_ver
-1)/sp
->subsampling_ver
);
1233 sp
->subsampling_convert_log
=1;
1238 sp
->libjpeg_jpeg_decompress_struct
.jpeg_color_space
=JCS_UNKNOWN
;
1239 sp
->libjpeg_jpeg_decompress_struct
.out_color_space
=JCS_UNKNOWN
;
1240 sp
->libjpeg_jpeg_query_style
=1;
1241 sp
->bytes_per_line
=sp
->samples_per_pixel_per_plane
*sp
->strile_width
;
1242 sp
->lines_per_strile
=sp
->strile_length
;
1244 if (jpeg_start_decompress_encap(sp
,&(sp
->libjpeg_jpeg_decompress_struct
))==0)
1246 sp
->writeheader_done
=1;
1251 OJPEGLibjpegSessionAbort(TIFF
* tif
)
1253 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1254 assert(sp
->libjpeg_session_active
!=0);
1255 jpeg_destroy((jpeg_common_struct
*)(&(sp
->libjpeg_jpeg_decompress_struct
)));
1256 sp
->libjpeg_session_active
=0;
1260 OJPEGReadHeaderInfoSec(TIFF
* tif
)
1262 static const char module
[]="OJPEGReadHeaderInfoSec";
1263 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1267 if (sp
->file_size
==0)
1268 sp
->file_size
=TIFFGetFileSize(tif
);
1269 if (sp
->jpeg_interchange_format
!=0)
1271 if (sp
->jpeg_interchange_format
>=sp
->file_size
)
1273 sp
->jpeg_interchange_format
=0;
1274 sp
->jpeg_interchange_format_length
=0;
1278 if ((sp
->jpeg_interchange_format_length
==0) || (sp
->jpeg_interchange_format
+sp
->jpeg_interchange_format_length
>sp
->file_size
))
1279 sp
->jpeg_interchange_format_length
=sp
->file_size
-sp
->jpeg_interchange_format
;
1282 sp
->in_buffer_source
=osibsNotSetYet
;
1283 sp
->in_buffer_next_strile
=0;
1284 sp
->in_buffer_strile_count
=tif
->tif_dir
.td_nstrips
;
1285 sp
->in_buffer_file_togo
=0;
1286 sp
->in_buffer_togo
=0;
1289 if (OJPEGReadBytePeek(sp
,&m
)==0)
1293 OJPEGReadByteAdvance(sp
);
1296 if (OJPEGReadByte(sp
,&m
)==0)
1301 case JPEG_MARKER_SOI
:
1302 /* this type of marker has no data, and should be skipped */
1304 case JPEG_MARKER_COM
:
1305 case JPEG_MARKER_APP0
:
1306 case JPEG_MARKER_APP0
+1:
1307 case JPEG_MARKER_APP0
+2:
1308 case JPEG_MARKER_APP0
+3:
1309 case JPEG_MARKER_APP0
+4:
1310 case JPEG_MARKER_APP0
+5:
1311 case JPEG_MARKER_APP0
+6:
1312 case JPEG_MARKER_APP0
+7:
1313 case JPEG_MARKER_APP0
+8:
1314 case JPEG_MARKER_APP0
+9:
1315 case JPEG_MARKER_APP0
+10:
1316 case JPEG_MARKER_APP0
+11:
1317 case JPEG_MARKER_APP0
+12:
1318 case JPEG_MARKER_APP0
+13:
1319 case JPEG_MARKER_APP0
+14:
1320 case JPEG_MARKER_APP0
+15:
1321 /* this type of marker has data, but it has no use to us (and no place here) and should be skipped */
1322 if (OJPEGReadWord(sp
,&n
)==0)
1326 if (sp
->subsamplingcorrect
==0)
1327 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt JPEG data");
1331 OJPEGReadSkip(sp
,n
-2);
1333 case JPEG_MARKER_DRI
:
1334 if (OJPEGReadHeaderInfoSecStreamDri(tif
)==0)
1337 case JPEG_MARKER_DQT
:
1338 if (OJPEGReadHeaderInfoSecStreamDqt(tif
)==0)
1341 case JPEG_MARKER_DHT
:
1342 if (OJPEGReadHeaderInfoSecStreamDht(tif
)==0)
1345 case JPEG_MARKER_SOF0
:
1346 case JPEG_MARKER_SOF1
:
1347 case JPEG_MARKER_SOF3
:
1348 if (OJPEGReadHeaderInfoSecStreamSof(tif
,m
)==0)
1350 if (sp
->subsamplingcorrect
!=0)
1353 case JPEG_MARKER_SOS
:
1354 if (sp
->subsamplingcorrect
!=0)
1356 assert(sp
->plane_sample_offset
==0);
1357 if (OJPEGReadHeaderInfoSecStreamSos(tif
)==0)
1361 TIFFErrorExt(tif
->tif_clientdata
,module
,"Unknown marker type %d in JPEG data",m
);
1364 } while(m
!=JPEG_MARKER_SOS
);
1365 if (sp
->subsamplingcorrect
)
1369 if (OJPEGReadHeaderInfoSecTablesQTable(tif
)==0)
1371 sp
->sof_marker_id
=JPEG_MARKER_SOF0
;
1372 for (o
=0; o
<sp
->samples_per_pixel
; o
++)
1374 sp
->sof_hv
[0]=((sp
->subsampling_hor
<<4)|sp
->subsampling_ver
);
1375 for (o
=1; o
<sp
->samples_per_pixel
; o
++)
1377 sp
->sof_x
=sp
->strile_width
;
1378 sp
->sof_y
=sp
->strile_length_total
;
1380 if (OJPEGReadHeaderInfoSecTablesDcTable(tif
)==0)
1382 if (OJPEGReadHeaderInfoSecTablesAcTable(tif
)==0)
1384 for (o
=1; o
<sp
->samples_per_pixel
; o
++)
1391 OJPEGReadHeaderInfoSecStreamDri(TIFF
* tif
)
1393 /* This could easily cause trouble in some cases... but no such cases have
1395 static const char module
[]="OJPEGReadHeaderInfoSecStreamDri";
1396 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1398 if (OJPEGReadWord(sp
,&m
)==0)
1402 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DRI marker in JPEG data");
1405 if (OJPEGReadWord(sp
,&m
)==0)
1407 sp
->restart_interval
=m
;
1412 OJPEGReadHeaderInfoSecStreamDqt(TIFF
* tif
)
1414 /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1415 static const char module
[]="OJPEGReadHeaderInfoSecStreamDqt";
1416 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1421 if (OJPEGReadWord(sp
,&m
)==0)
1425 if (sp
->subsamplingcorrect
==0)
1426 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DQT marker in JPEG data");
1429 if (sp
->subsamplingcorrect
!=0)
1430 OJPEGReadSkip(sp
,m
-2);
1438 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DQT marker in JPEG data");
1441 na
=sizeof(uint32
)+69;
1445 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1449 nb
[sizeof(uint32
)]=255;
1450 nb
[sizeof(uint32
)+1]=JPEG_MARKER_DQT
;
1451 nb
[sizeof(uint32
)+2]=0;
1452 nb
[sizeof(uint32
)+3]=67;
1453 if (OJPEGReadBlock(sp
,65,&nb
[sizeof(uint32
)+4])==0) {
1457 o
=nb
[sizeof(uint32
)+4]&15;
1460 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DQT marker in JPEG data");
1464 if (sp
->qtable
[o
]!=0)
1465 _TIFFfree(sp
->qtable
[o
]);
1474 OJPEGReadHeaderInfoSecStreamDht(TIFF
* tif
)
1476 /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1477 /* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
1478 static const char module
[]="OJPEGReadHeaderInfoSecStreamDht";
1479 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1484 if (OJPEGReadWord(sp
,&m
)==0)
1488 if (sp
->subsamplingcorrect
==0)
1489 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DHT marker in JPEG data");
1492 if (sp
->subsamplingcorrect
!=0)
1494 OJPEGReadSkip(sp
,m
-2);
1498 na
=sizeof(uint32
)+2+m
;
1502 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1506 nb
[sizeof(uint32
)]=255;
1507 nb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1508 nb
[sizeof(uint32
)+2]=(m
>>8);
1509 nb
[sizeof(uint32
)+3]=(m
&255);
1510 if (OJPEGReadBlock(sp
,m
-2,&nb
[sizeof(uint32
)+4])==0) {
1514 o
=nb
[sizeof(uint32
)+4];
1519 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DHT marker in JPEG data");
1523 if (sp
->dctable
[o
]!=0)
1524 _TIFFfree(sp
->dctable
[o
]);
1531 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DHT marker in JPEG data");
1538 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt DHT marker in JPEG data");
1542 if (sp
->actable
[o
]!=0)
1543 _TIFFfree(sp
->actable
[o
]);
1551 OJPEGReadHeaderInfoSecStreamSof(TIFF
* tif
, uint8 marker_id
)
1553 /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1554 static const char module
[]="OJPEGReadHeaderInfoSecStreamSof";
1555 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1563 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt JPEG data");
1566 if (sp
->subsamplingcorrect
==0)
1567 sp
->sof_marker_id
=marker_id
;
1568 /* Lf: data length */
1569 if (OJPEGReadWord(sp
,&m
)==0)
1573 if (sp
->subsamplingcorrect
==0)
1574 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOF marker in JPEG data");
1580 if (sp
->subsamplingcorrect
==0)
1581 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOF marker in JPEG data");
1585 if (sp
->subsamplingcorrect
==0)
1587 if (n
!=sp
->samples_per_pixel
)
1589 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected number of samples");
1593 /* P: Sample precision */
1594 if (OJPEGReadByte(sp
,&o
)==0)
1598 if (sp
->subsamplingcorrect
==0)
1599 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected number of bits per sample");
1602 /* Y: Number of lines, X: Number of samples per line */
1603 if (sp
->subsamplingcorrect
)
1604 OJPEGReadSkip(sp
,4);
1607 /* Y: Number of lines */
1608 if (OJPEGReadWord(sp
,&p
)==0)
1610 if (((uint32
)p
<sp
->image_length
) && ((uint32
)p
<sp
->strile_length_total
))
1612 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected height");
1616 /* X: Number of samples per line */
1617 if (OJPEGReadWord(sp
,&p
)==0)
1619 if (((uint32
)p
<sp
->image_width
) && ((uint32
)p
<sp
->strile_width
))
1621 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected width");
1624 if ((uint32
)p
>sp
->strile_width
)
1626 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data image width exceeds expected image width");
1631 /* Nf: Number of image components in frame */
1632 if (OJPEGReadByte(sp
,&o
)==0)
1636 if (sp
->subsamplingcorrect
==0)
1637 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOF marker in JPEG data");
1640 /* per component stuff */
1641 /* 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 */
1644 /* C: Component identifier */
1645 if (OJPEGReadByte(sp
,&o
)==0)
1647 if (sp
->subsamplingcorrect
==0)
1649 /* H: Horizontal sampling factor, and V: Vertical sampling factor */
1650 if (OJPEGReadByte(sp
,&o
)==0)
1652 if (sp
->subsamplingcorrect
!=0)
1656 sp
->subsampling_hor
=(o
>>4);
1657 sp
->subsampling_ver
=(o
&15);
1658 if (((sp
->subsampling_hor
!=1) && (sp
->subsampling_hor
!=2) && (sp
->subsampling_hor
!=4)) ||
1659 ((sp
->subsampling_ver
!=1) && (sp
->subsampling_ver
!=2) && (sp
->subsampling_ver
!=4)))
1660 sp
->subsampling_force_desubsampling_inside_decompression
=1;
1665 sp
->subsampling_force_desubsampling_inside_decompression
=1;
1671 if (sp
->subsampling_force_desubsampling_inside_decompression
==0)
1675 if (o
!=((sp
->subsampling_hor
<<4)|sp
->subsampling_ver
))
1677 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected subsampling values");
1685 TIFFErrorExt(tif
->tif_clientdata
,module
,"JPEG compressed data indicates unexpected subsampling values");
1691 /* Tq: Quantization table destination selector */
1692 if (OJPEGReadByte(sp
,&o
)==0)
1694 if (sp
->subsamplingcorrect
==0)
1697 if (sp
->subsamplingcorrect
==0)
1703 OJPEGReadHeaderInfoSecStreamSos(TIFF
* tif
)
1705 /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1706 static const char module
[]="OJPEGReadHeaderInfoSecStreamSos";
1707 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1711 assert(sp
->subsamplingcorrect
==0);
1714 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOS marker in JPEG data");
1718 if (OJPEGReadWord(sp
,&m
)==0)
1720 if (m
!=6+sp
->samples_per_pixel_per_plane
*2)
1722 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOS marker in JPEG data");
1726 if (OJPEGReadByte(sp
,&n
)==0)
1728 if (n
!=sp
->samples_per_pixel_per_plane
)
1730 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt SOS marker in JPEG data");
1733 /* Cs, Td, and Ta */
1734 for (o
=0; o
<sp
->samples_per_pixel_per_plane
; o
++)
1737 if (OJPEGReadByte(sp
,&n
)==0)
1739 sp
->sos_cs
[sp
->plane_sample_offset
+o
]=n
;
1741 if (OJPEGReadByte(sp
,&n
)==0)
1743 sp
->sos_tda
[sp
->plane_sample_offset
+o
]=n
;
1745 /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as per LibJpeg source */
1746 OJPEGReadSkip(sp
,3);
1751 OJPEGReadHeaderInfoSecTablesQTable(TIFF
* tif
)
1753 static const char module
[]="OJPEGReadHeaderInfoSecTablesQTable";
1754 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1760 if (sp
->qtable_offset
[0]==0)
1762 TIFFErrorExt(tif
->tif_clientdata
,module
,"Missing JPEG tables");
1765 sp
->in_buffer_file_pos_log
=0;
1766 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1768 if ((sp
->qtable_offset
[m
]!=0) && ((m
==0) || (sp
->qtable_offset
[m
]!=sp
->qtable_offset
[m
-1])))
1770 for (n
=0; n
<m
-1; n
++)
1772 if (sp
->qtable_offset
[m
]==sp
->qtable_offset
[n
])
1774 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt JpegQTables tag value");
1778 oa
=sizeof(uint32
)+69;
1782 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1786 ob
[sizeof(uint32
)]=255;
1787 ob
[sizeof(uint32
)+1]=JPEG_MARKER_DQT
;
1788 ob
[sizeof(uint32
)+2]=0;
1789 ob
[sizeof(uint32
)+3]=67;
1790 ob
[sizeof(uint32
)+4]=m
;
1791 TIFFSeekFile(tif
,sp
->qtable_offset
[m
],SEEK_SET
);
1792 p
=(uint32
)TIFFReadFile(tif
,&ob
[sizeof(uint32
)+5],64);
1798 if (sp
->qtable
[m
]!=0)
1799 _TIFFfree(sp
->qtable
[m
]);
1804 sp
->sof_tq
[m
]=sp
->sof_tq
[m
-1];
1810 OJPEGReadHeaderInfoSecTablesDcTable(TIFF
* tif
)
1812 static const char module
[]="OJPEGReadHeaderInfoSecTablesDcTable";
1813 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1821 if (sp
->dctable_offset
[0]==0)
1823 TIFFErrorExt(tif
->tif_clientdata
,module
,"Missing JPEG tables");
1826 sp
->in_buffer_file_pos_log
=0;
1827 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1829 if ((sp
->dctable_offset
[m
]!=0) && ((m
==0) || (sp
->dctable_offset
[m
]!=sp
->dctable_offset
[m
-1])))
1831 for (n
=0; n
<m
-1; n
++)
1833 if (sp
->dctable_offset
[m
]==sp
->dctable_offset
[n
])
1835 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt JpegDcTables tag value");
1839 TIFFSeekFile(tif
,sp
->dctable_offset
[m
],SEEK_SET
);
1840 p
=(uint32
)TIFFReadFile(tif
,o
,16);
1844 for (n
=0; n
<16; n
++)
1846 ra
=sizeof(uint32
)+21+q
;
1850 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1854 rb
[sizeof(uint32
)]=255;
1855 rb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1856 rb
[sizeof(uint32
)+2]=(uint8
)((19+q
)>>8);
1857 rb
[sizeof(uint32
)+3]=((19+q
)&255);
1858 rb
[sizeof(uint32
)+4]=m
;
1859 for (n
=0; n
<16; n
++)
1860 rb
[sizeof(uint32
)+5+n
]=o
[n
];
1861 p
=(uint32
)TIFFReadFile(tif
,&(rb
[sizeof(uint32
)+21]),q
);
1867 if (sp
->dctable
[m
]!=0)
1868 _TIFFfree(sp
->dctable
[m
]);
1870 sp
->sos_tda
[m
]=(m
<<4);
1873 sp
->sos_tda
[m
]=sp
->sos_tda
[m
-1];
1879 OJPEGReadHeaderInfoSecTablesAcTable(TIFF
* tif
)
1881 static const char module
[]="OJPEGReadHeaderInfoSecTablesAcTable";
1882 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
1890 if (sp
->actable_offset
[0]==0)
1892 TIFFErrorExt(tif
->tif_clientdata
,module
,"Missing JPEG tables");
1895 sp
->in_buffer_file_pos_log
=0;
1896 for (m
=0; m
<sp
->samples_per_pixel
; m
++)
1898 if ((sp
->actable_offset
[m
]!=0) && ((m
==0) || (sp
->actable_offset
[m
]!=sp
->actable_offset
[m
-1])))
1900 for (n
=0; n
<m
-1; n
++)
1902 if (sp
->actable_offset
[m
]==sp
->actable_offset
[n
])
1904 TIFFErrorExt(tif
->tif_clientdata
,module
,"Corrupt JpegAcTables tag value");
1908 TIFFSeekFile(tif
,sp
->actable_offset
[m
],SEEK_SET
);
1909 p
=(uint32
)TIFFReadFile(tif
,o
,16);
1913 for (n
=0; n
<16; n
++)
1915 ra
=sizeof(uint32
)+21+q
;
1919 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1923 rb
[sizeof(uint32
)]=255;
1924 rb
[sizeof(uint32
)+1]=JPEG_MARKER_DHT
;
1925 rb
[sizeof(uint32
)+2]=(uint8
)((19+q
)>>8);
1926 rb
[sizeof(uint32
)+3]=((19+q
)&255);
1927 rb
[sizeof(uint32
)+4]=(16|m
);
1928 for (n
=0; n
<16; n
++)
1929 rb
[sizeof(uint32
)+5+n
]=o
[n
];
1930 p
=(uint32
)TIFFReadFile(tif
,&(rb
[sizeof(uint32
)+21]),q
);
1936 if (sp
->actable
[m
]!=0)
1937 _TIFFfree(sp
->actable
[m
]);
1939 sp
->sos_tda
[m
]=(sp
->sos_tda
[m
]|m
);
1942 sp
->sos_tda
[m
]=(sp
->sos_tda
[m
]|(sp
->sos_tda
[m
-1]&15));
1948 OJPEGReadBufferFill(OJPEGState
* sp
)
1952 /* TODO: double-check: when subsamplingcorrect is set, no call to TIFFErrorExt or TIFFWarningExt should be made
1953 * in any other case, seek or read errors should be passed through */
1956 if (sp
->in_buffer_file_togo
!=0)
1958 if (sp
->in_buffer_file_pos_log
==0)
1960 TIFFSeekFile(sp
->tif
,sp
->in_buffer_file_pos
,SEEK_SET
);
1961 sp
->in_buffer_file_pos_log
=1;
1964 if ((uint64
)m
>sp
->in_buffer_file_togo
)
1965 m
=(uint16
)sp
->in_buffer_file_togo
;
1966 n
=TIFFReadFile(sp
->tif
,sp
->in_buffer
,(tmsize_t
)m
);
1970 assert(n
<=OJPEG_BUFFER
);
1972 assert((uint64
)n
<=sp
->in_buffer_file_togo
);
1974 sp
->in_buffer_togo
=m
;
1975 sp
->in_buffer_cur
=sp
->in_buffer
;
1976 sp
->in_buffer_file_togo
-=m
;
1977 sp
->in_buffer_file_pos
+=m
;
1980 sp
->in_buffer_file_pos_log
=0;
1981 switch(sp
->in_buffer_source
)
1983 case osibsNotSetYet
:
1984 if (sp
->jpeg_interchange_format
!=0)
1986 sp
->in_buffer_file_pos
=sp
->jpeg_interchange_format
;
1987 sp
->in_buffer_file_togo
=sp
->jpeg_interchange_format_length
;
1989 sp
->in_buffer_source
=osibsJpegInterchangeFormat
;
1991 case osibsJpegInterchangeFormat
:
1992 sp
->in_buffer_source
=osibsStrile
;
1995 if (!_TIFFFillStriles( sp
->tif
)
1996 || sp
->tif
->tif_dir
.td_stripoffset
== NULL
1997 || sp
->tif
->tif_dir
.td_stripbytecount
== NULL
)
2000 if (sp
->in_buffer_next_strile
==sp
->in_buffer_strile_count
)
2001 sp
->in_buffer_source
=osibsEof
;
2004 sp
->in_buffer_file_pos
=sp
->tif
->tif_dir
.td_stripoffset
[sp
->in_buffer_next_strile
];
2005 if (sp
->in_buffer_file_pos
!=0)
2007 if (sp
->in_buffer_file_pos
>=sp
->file_size
)
2008 sp
->in_buffer_file_pos
=0;
2009 else if (sp
->tif
->tif_dir
.td_stripbytecount
==NULL
)
2010 sp
->in_buffer_file_togo
=sp
->file_size
-sp
->in_buffer_file_pos
;
2013 if (sp
->tif
->tif_dir
.td_stripbytecount
== 0) {
2014 TIFFErrorExt(sp
->tif
->tif_clientdata
,sp
->tif
->tif_name
,"Strip byte counts are missing");
2017 sp
->in_buffer_file_togo
=sp
->tif
->tif_dir
.td_stripbytecount
[sp
->in_buffer_next_strile
];
2018 if (sp
->in_buffer_file_togo
==0)
2019 sp
->in_buffer_file_pos
=0;
2020 else if (sp
->in_buffer_file_pos
+sp
->in_buffer_file_togo
>sp
->file_size
)
2021 sp
->in_buffer_file_togo
=sp
->file_size
-sp
->in_buffer_file_pos
;
2024 sp
->in_buffer_next_strile
++;
2035 OJPEGReadByte(OJPEGState
* sp
, uint8
* byte
)
2037 if (sp
->in_buffer_togo
==0)
2039 if (OJPEGReadBufferFill(sp
)==0)
2041 assert(sp
->in_buffer_togo
>0);
2043 *byte
=*(sp
->in_buffer_cur
);
2044 sp
->in_buffer_cur
++;
2045 sp
->in_buffer_togo
--;
2050 OJPEGReadBytePeek(OJPEGState
* sp
, uint8
* byte
)
2052 if (sp
->in_buffer_togo
==0)
2054 if (OJPEGReadBufferFill(sp
)==0)
2056 assert(sp
->in_buffer_togo
>0);
2058 *byte
=*(sp
->in_buffer_cur
);
2063 OJPEGReadByteAdvance(OJPEGState
* sp
)
2065 assert(sp
->in_buffer_togo
>0);
2066 sp
->in_buffer_cur
++;
2067 sp
->in_buffer_togo
--;
2071 OJPEGReadWord(OJPEGState
* sp
, uint16
* word
)
2074 if (OJPEGReadByte(sp
,&m
)==0)
2077 if (OJPEGReadByte(sp
,&m
)==0)
2084 OJPEGReadBlock(OJPEGState
* sp
, uint16 len
, void* mem
)
2094 if (sp
->in_buffer_togo
==0)
2096 if (OJPEGReadBufferFill(sp
)==0)
2098 assert(sp
->in_buffer_togo
>0);
2101 if (n
>sp
->in_buffer_togo
)
2102 n
=sp
->in_buffer_togo
;
2103 _TIFFmemcpy(mmem
,sp
->in_buffer_cur
,n
);
2104 sp
->in_buffer_cur
+=n
;
2105 sp
->in_buffer_togo
-=n
;
2113 OJPEGReadSkip(OJPEGState
* sp
, uint16 len
)
2119 if (n
>sp
->in_buffer_togo
)
2120 n
=sp
->in_buffer_togo
;
2121 sp
->in_buffer_cur
+=n
;
2122 sp
->in_buffer_togo
-=n
;
2126 assert(sp
->in_buffer_togo
==0);
2128 if ((uint64
)n
>sp
->in_buffer_file_togo
)
2129 n
=(uint16
)sp
->in_buffer_file_togo
;
2130 sp
->in_buffer_file_pos
+=n
;
2131 sp
->in_buffer_file_togo
-=n
;
2132 sp
->in_buffer_file_pos_log
=0;
2133 /* we don't skip past jpeginterchangeformat/strile block...
2134 * if that is asked from us, we're dealing with totally bazurk
2135 * data anyway, and we've not seen this happening on any
2136 * testfile, so we might as well likely cause some other
2137 * meaningless error to be passed at some later time
2143 OJPEGWriteStream(TIFF
* tif
, void** mem
, uint32
* len
)
2145 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2149 assert(sp
->out_state
<=ososEoi
);
2150 switch(sp
->out_state
)
2153 OJPEGWriteStreamSoi(tif
,mem
,len
);
2156 OJPEGWriteStreamQTable(tif
,0,mem
,len
);
2159 OJPEGWriteStreamQTable(tif
,1,mem
,len
);
2162 OJPEGWriteStreamQTable(tif
,2,mem
,len
);
2165 OJPEGWriteStreamQTable(tif
,3,mem
,len
);
2168 OJPEGWriteStreamDcTable(tif
,0,mem
,len
);
2171 OJPEGWriteStreamDcTable(tif
,1,mem
,len
);
2174 OJPEGWriteStreamDcTable(tif
,2,mem
,len
);
2177 OJPEGWriteStreamDcTable(tif
,3,mem
,len
);
2180 OJPEGWriteStreamAcTable(tif
,0,mem
,len
);
2183 OJPEGWriteStreamAcTable(tif
,1,mem
,len
);
2186 OJPEGWriteStreamAcTable(tif
,2,mem
,len
);
2189 OJPEGWriteStreamAcTable(tif
,3,mem
,len
);
2192 OJPEGWriteStreamDri(tif
,mem
,len
);
2195 OJPEGWriteStreamSof(tif
,mem
,len
);
2198 OJPEGWriteStreamSos(tif
,mem
,len
);
2200 case ososCompressed
:
2201 if (OJPEGWriteStreamCompressed(tif
,mem
,len
)==0)
2205 OJPEGWriteStreamRst(tif
,mem
,len
);
2208 OJPEGWriteStreamEoi(tif
,mem
,len
);
2216 OJPEGWriteStreamSoi(TIFF
* tif
, void** mem
, uint32
* len
)
2218 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2219 assert(OJPEG_BUFFER
>=2);
2220 sp
->out_buffer
[0]=255;
2221 sp
->out_buffer
[1]=JPEG_MARKER_SOI
;
2223 *mem
=(void*)sp
->out_buffer
;
2228 OJPEGWriteStreamQTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2230 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2231 if (sp
->qtable
[table_index
]!=0)
2233 *mem
=(void*)(sp
->qtable
[table_index
]+sizeof(uint32
));
2234 *len
=*((uint32
*)sp
->qtable
[table_index
])-sizeof(uint32
);
2240 OJPEGWriteStreamDcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2242 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2243 if (sp
->dctable
[table_index
]!=0)
2245 *mem
=(void*)(sp
->dctable
[table_index
]+sizeof(uint32
));
2246 *len
=*((uint32
*)sp
->dctable
[table_index
])-sizeof(uint32
);
2252 OJPEGWriteStreamAcTable(TIFF
* tif
, uint8 table_index
, void** mem
, uint32
* len
)
2254 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2255 if (sp
->actable
[table_index
]!=0)
2257 *mem
=(void*)(sp
->actable
[table_index
]+sizeof(uint32
));
2258 *len
=*((uint32
*)sp
->actable
[table_index
])-sizeof(uint32
);
2264 OJPEGWriteStreamDri(TIFF
* tif
, void** mem
, uint32
* len
)
2266 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2267 assert(OJPEG_BUFFER
>=6);
2268 if (sp
->restart_interval
!=0)
2270 sp
->out_buffer
[0]=255;
2271 sp
->out_buffer
[1]=JPEG_MARKER_DRI
;
2272 sp
->out_buffer
[2]=0;
2273 sp
->out_buffer
[3]=4;
2274 sp
->out_buffer
[4]=(sp
->restart_interval
>>8);
2275 sp
->out_buffer
[5]=(sp
->restart_interval
&255);
2277 *mem
=(void*)sp
->out_buffer
;
2283 OJPEGWriteStreamSof(TIFF
* tif
, void** mem
, uint32
* len
)
2285 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2287 assert(OJPEG_BUFFER
>=2+8+sp
->samples_per_pixel_per_plane
*3);
2288 assert(255>=8+sp
->samples_per_pixel_per_plane
*3);
2289 sp
->out_buffer
[0]=255;
2290 sp
->out_buffer
[1]=sp
->sof_marker_id
;
2292 sp
->out_buffer
[2]=0;
2293 sp
->out_buffer
[3]=8+sp
->samples_per_pixel_per_plane
*3;
2295 sp
->out_buffer
[4]=8;
2297 sp
->out_buffer
[5]=(uint8
)(sp
->sof_y
>>8);
2298 sp
->out_buffer
[6]=(sp
->sof_y
&255);
2300 sp
->out_buffer
[7]=(uint8
)(sp
->sof_x
>>8);
2301 sp
->out_buffer
[8]=(sp
->sof_x
&255);
2303 sp
->out_buffer
[9]=sp
->samples_per_pixel_per_plane
;
2304 for (m
=0; m
<sp
->samples_per_pixel_per_plane
; m
++)
2307 sp
->out_buffer
[10+m
*3]=sp
->sof_c
[sp
->plane_sample_offset
+m
];
2309 sp
->out_buffer
[10+m
*3+1]=sp
->sof_hv
[sp
->plane_sample_offset
+m
];
2311 sp
->out_buffer
[10+m
*3+2]=sp
->sof_tq
[sp
->plane_sample_offset
+m
];
2313 *len
=10+sp
->samples_per_pixel_per_plane
*3;
2314 *mem
=(void*)sp
->out_buffer
;
2319 OJPEGWriteStreamSos(TIFF
* tif
, void** mem
, uint32
* len
)
2321 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2323 assert(OJPEG_BUFFER
>=2+6+sp
->samples_per_pixel_per_plane
*2);
2324 assert(255>=6+sp
->samples_per_pixel_per_plane
*2);
2325 sp
->out_buffer
[0]=255;
2326 sp
->out_buffer
[1]=JPEG_MARKER_SOS
;
2328 sp
->out_buffer
[2]=0;
2329 sp
->out_buffer
[3]=6+sp
->samples_per_pixel_per_plane
*2;
2331 sp
->out_buffer
[4]=sp
->samples_per_pixel_per_plane
;
2332 for (m
=0; m
<sp
->samples_per_pixel_per_plane
; m
++)
2335 sp
->out_buffer
[5+m
*2]=sp
->sos_cs
[sp
->plane_sample_offset
+m
];
2337 sp
->out_buffer
[5+m
*2+1]=sp
->sos_tda
[sp
->plane_sample_offset
+m
];
2340 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2]=0;
2342 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2+1]=63;
2344 sp
->out_buffer
[5+sp
->samples_per_pixel_per_plane
*2+2]=0;
2345 *len
=8+sp
->samples_per_pixel_per_plane
*2;
2346 *mem
=(void*)sp
->out_buffer
;
2351 OJPEGWriteStreamCompressed(TIFF
* tif
, void** mem
, uint32
* len
)
2353 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2354 if (sp
->in_buffer_togo
==0)
2356 if (OJPEGReadBufferFill(sp
)==0)
2358 assert(sp
->in_buffer_togo
>0);
2360 *len
=sp
->in_buffer_togo
;
2361 *mem
=(void*)sp
->in_buffer_cur
;
2362 sp
->in_buffer_togo
=0;
2363 if (sp
->in_buffer_file_togo
==0)
2365 switch(sp
->in_buffer_source
)
2368 if (sp
->in_buffer_next_strile
<sp
->in_buffer_strile_count
)
2369 sp
->out_state
=ososRst
;
2371 sp
->out_state
=ososEoi
;
2374 sp
->out_state
=ososEoi
;
2384 OJPEGWriteStreamRst(TIFF
* tif
, void** mem
, uint32
* len
)
2386 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2387 assert(OJPEG_BUFFER
>=2);
2388 sp
->out_buffer
[0]=255;
2389 sp
->out_buffer
[1]=JPEG_MARKER_RST0
+sp
->restart_index
;
2390 sp
->restart_index
++;
2391 if (sp
->restart_index
==8)
2392 sp
->restart_index
=0;
2394 *mem
=(void*)sp
->out_buffer
;
2395 sp
->out_state
=ososCompressed
;
2399 OJPEGWriteStreamEoi(TIFF
* tif
, void** mem
, uint32
* len
)
2401 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2402 assert(OJPEG_BUFFER
>=2);
2403 sp
->out_buffer
[0]=255;
2404 sp
->out_buffer
[1]=JPEG_MARKER_EOI
;
2406 *mem
=(void*)sp
->out_buffer
;
2409 #ifndef LIBJPEG_ENCAP_EXTERNAL
2411 jpeg_create_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
)
2413 if( SETJMP(sp
->exit_jmpbuf
) )
2416 jpeg_create_decompress(cinfo
);
2422 #ifndef LIBJPEG_ENCAP_EXTERNAL
2424 jpeg_read_header_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, uint8 require_image
)
2426 if( SETJMP(sp
->exit_jmpbuf
) )
2429 jpeg_read_header(cinfo
,require_image
);
2435 #ifndef LIBJPEG_ENCAP_EXTERNAL
2437 jpeg_start_decompress_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
)
2439 if( SETJMP(sp
->exit_jmpbuf
) )
2442 jpeg_start_decompress(cinfo
);
2448 #ifndef LIBJPEG_ENCAP_EXTERNAL
2450 jpeg_read_scanlines_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* scanlines
, uint32 max_lines
)
2452 if( SETJMP(sp
->exit_jmpbuf
) )
2455 jpeg_read_scanlines(cinfo
,scanlines
,max_lines
);
2461 #ifndef LIBJPEG_ENCAP_EXTERNAL
2463 jpeg_read_raw_data_encap(OJPEGState
* sp
, jpeg_decompress_struct
* cinfo
, void* data
, uint32 max_lines
)
2465 if( SETJMP(sp
->exit_jmpbuf
) )
2468 jpeg_read_raw_data(cinfo
,data
,max_lines
);
2474 #ifndef LIBJPEG_ENCAP_EXTERNAL
2476 jpeg_encap_unwind(TIFF
* tif
)
2478 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2479 LONGJMP(sp
->exit_jmpbuf
,1);
2484 OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct
* cinfo
)
2486 char buffer
[JMSG_LENGTH_MAX
];
2487 (*cinfo
->err
->format_message
)(cinfo
,buffer
);
2488 TIFFWarningExt(((TIFF
*)(cinfo
->client_data
))->tif_clientdata
,"LibJpeg","%s",buffer
);
2492 OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct
* cinfo
)
2494 char buffer
[JMSG_LENGTH_MAX
];
2495 (*cinfo
->err
->format_message
)(cinfo
,buffer
);
2496 TIFFErrorExt(((TIFF
*)(cinfo
->client_data
))->tif_clientdata
,"LibJpeg","%s",buffer
);
2497 jpeg_encap_unwind((TIFF
*)(cinfo
->client_data
));
2501 OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct
* cinfo
)
2507 OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct
* cinfo
)
2509 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2510 OJPEGState
* sp
=(OJPEGState
*)tif
->tif_data
;
2513 if (OJPEGWriteStream(tif
,&mem
,&len
)==0)
2515 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Premature end of JPEG data");
2516 jpeg_encap_unwind(tif
);
2518 sp
->libjpeg_jpeg_source_mgr
.bytes_in_buffer
=len
;
2519 sp
->libjpeg_jpeg_source_mgr
.next_input_byte
=mem
;
2524 OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct
* cinfo
, long num_bytes
)
2526 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2528 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Unexpected error");
2529 jpeg_encap_unwind(tif
);
2533 #pragma warning( push )
2534 #pragma warning( disable : 4702 ) /* unreachable code */
2537 OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct
* cinfo
, int desired
)
2539 TIFF
* tif
=(TIFF
*)cinfo
->client_data
;
2541 TIFFErrorExt(tif
->tif_clientdata
,"LibJpeg","Unexpected error");
2542 jpeg_encap_unwind(tif
);
2546 #pragma warning( pop )
2550 OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct
* cinfo
)