Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: third_party/tiff_v403/tif_ojpeg.c

Issue 1563103002: XFA: Upgrade to libtiff 4.0.6. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: rename to libtiff Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/tiff_v403/tif_next.c ('k') | third_party/tiff_v403/tif_open.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* $Id: tif_ojpeg.c,v 1.56 2012-05-24 03:15:18 fwarmerdam Exp $ */
2
3 /* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
4 specification is now totally obsolete and deprecated for new applications and
5 images. This file was was created solely in order to read unconverted images
6 still present on some users' computer systems. It will never be extended
7 to write such files. Writing new-style JPEG compressed TIFFs is implemented
8 in tif_jpeg.c.
9
10 The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
11 testfiles, and anticipate as much as possible all other... But still, it may
12 fail on some. If you encounter problems, please report them on the TIFF
13 mailing list and/or to Joris Van Damme <info@awaresystems.be>.
14
15 Please read the file called "TIFF Technical Note #2" if you need to be
16 convinced this compression scheme is bad and breaks TIFF. That document
17 is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
18 and from AWare Systems' TIFF section
19 <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
20 in Adobe's specification supplements, marked "draft" up to this day, but
21 supported by the TIFF community.
22
23 This file interfaces with Release 6B of the JPEG Library written by the
24 Independent JPEG Group. Previous versions of this file required a hack inside
25 the LibJpeg library. This version no longer requires that. Remember to
26 remove the hack if you update from the old version.
27
28 Copyright (c) Joris Van Damme <info@awaresystems.be>
29 Copyright (c) AWare Systems <http://www.awaresystems.be/>
30
31 The licence agreement for this file is the same as the rest of the LibTiff
32 library.
33
34 IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
35 ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
36 OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
37 WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
38 LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
39 OF THIS SOFTWARE.
40
41 Joris Van Damme and/or AWare Systems may be available for custom
42 developement. If you like what you see, and need anything similar or related,
43 contact <info@awaresystems.be>.
44 */
45
46 /* What is what, and what is not?
47
48 This decoder starts with an input stream, that is essentially the JpegInterch angeFormat
49 stream, if any, followed by the strile data, if any. This stream is read in
50 OJPEGReadByte and related functions.
51
52 It analyzes the start of this stream, until it encounters non-marker data, i. e.
53 compressed image data. Some of the header markers it sees have no actual cont ent,
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 informatio n
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 OJPEGReadHeaderInfoSecSt reamXxx
58 functions.
59
60 Some OJPEG imagery contains no valid JPEG header markers. This situation is p icked
61 up on if we've seen no SOF marker when we're at the start of the compressed i mage
62 data. In this case, the tables are read from JpegXxxTables tags, and the othe r
63 bits and pieces of information is initialized to its most basic value. This i s
64 implemented in the OJPEGReadHeaderInfoSecTablesXxx functions.
65
66 When this is complete, a good and valid JPEG header can be assembled, and thi s is
67 passed through to LibJpeg. When that's done, the remainder of the input strea m, i.e.
68 the compressed image data, can be passed through unchanged. This is done in
69 OJPEGWriteStream functions.
70
71 LibTiff rightly expects to know the subsampling values before decompression. Just like
72 in new-style JPEG-in-TIFF, though, or even more so, actually, the YCbCrsubsam pling
73 tag is notoriously unreliable. To correct these tag values with the ones insi de
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 warni ngs
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 spe ed
78 in correcting these values is important enough to warrant this sacrifice. All though
79 there is currently no define or other configuration mechanism to disable this behaviour,
80 the actual header scanning is build to robustly respond with error report if it
81 should encounter an uncorrected mismatch of subsampling values. See
82 OJPEGReadHeaderInfoSecStreamSof.
83
84 The restart interval and restart markers are the most tricky part... The rest art
85 interval can be specified in a tag. It can also be set inside the input JPEG stream.
86 It can be used inside the input JPEG stream. If reading from strile data, we' ve
87 consistenly discovered the need to insert restart markers in between the diff erent
88 striles, as is also probably the most likely interpretation of the original T IFF 6.0
89 specification. With all this setting of interval, and actual use of markers t hat 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 be en able
93 to discover ways that seem to work.
94
95 Some special provision is made for planarconfig separate OJPEG files. These s eem
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 a nd don't
98 care. We want LibTiff to be able to access the planes individually, without h uge
99 buffering inside LibJpeg, anyway. So we compose headers to feed to LibJpeg, i n 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 subsequ ent
102 planes, is done inside OJPEGReadSecondarySos.
103
104 The benefit of the scheme is... that it works, basically. We know of no other that
105 does. It works without checking software tag, or otherwise going about things in an
106 OJPEG flavor specific manner. Instead, it is a single scheme, that covers the cases
107 with and without JpegInterchangeFormat, with and without striles, with part o f
108 the header in JpegInterchangeFormat and remainder in first strile, etc. It is forgiving
109 and robust, may likely work with OJPEG flavors we've not seen yet, and makes most out
110 of the data.
111
112 Another nice side-effect is that a complete JPEG single valid stream is build if
113 planarconfig is not separate (vast majority). We may one day use that to buil d
114 converters to JPEG, and/or to new-style JPEG compression inside TIFF.
115
116 A dissadvantage is the lack of random access to the individual striles. This is the
117 reason for much of the complicated restart-and-position stuff inside OJPEGPre Decode.
118 Applications would do well accessing all striles in order, as this will resul t in
119 a single sequential scan of the input stream, and no restarting of LibJpeg de coding
120 session.
121 */
122
123
124 #define WIN32_LEAN_AND_MEAN
125 #define VC_EXTRALEAN
126
127 #include "tiffiop.h"
128 #ifdef OJPEG_SUPPORT
129
130 /* Configuration defines here are:
131 * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some e nvironments,
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. W hen
134 * JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declar ed external
135 * to this unit, and can be defined elsewhere to use stuff other then longj ump.
136 * The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encap sulators
137 * here, internally, with normal longjump.
138 * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
139 * conviniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
140 * in place of plain setjmp. These macros will make it easier. It is useles s
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 eno ugh so as to guarantee
143 * instant processing, optimal streaming and optimal use of processor cache , but also big
144 * enough so as to not result in significant call overhead. It should be at least a few
145 * bytes to accomodate some structures (this is verified in asserts), but i t would not be
146 * sensible to make it this small anyway, and it should be at most 64K sinc e it is indexed
147 * with uint16. We recommend 2K.
148 * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used any where and has
149 * absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
150 */
151
152 /* define LIBJPEG_ENCAP_EXTERNAL */
153 #define SETJMP(jbuf) setjmp(jbuf)
154 #define LONGJMP(jbuf,code) longjmp(jbuf,code)
155 #define JMP_BUF jmp_buf
156 #define OJPEG_BUFFER 2048
157 /* define EGYPTIANWALK */
158
159 #define JPEG_MARKER_SOF0 0xC0
160 #define JPEG_MARKER_SOF1 0xC1
161 #define JPEG_MARKER_SOF3 0xC3
162 #define JPEG_MARKER_DHT 0xC4
163 #define JPEG_MARKER_RST0 0XD0
164 #define JPEG_MARKER_SOI 0xD8
165 #define JPEG_MARKER_EOI 0xD9
166 #define JPEG_MARKER_SOS 0xDA
167 #define JPEG_MARKER_DQT 0xDB
168 #define JPEG_MARKER_DRI 0xDD
169 #define JPEG_MARKER_APP0 0xE0
170 #define JPEG_MARKER_COM 0xFE
171
172 #define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
173 #define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
174 #define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
175 #define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
176 #define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
177 #define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
178 #define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
179
180 static const TIFFField ojpegFields[] = {
181 {TIFFTAG_JPEGIFOFFSET,1,1,TIFF_LONG8,0,TIFF_SETGET_UINT64,TIFF_SETGET_UN DEFINED,FIELD_OJPEG_JPEGINTERCHANGEFORMAT,TRUE,FALSE,"JpegInterchangeFormat",NUL L},
182 {TIFFTAG_JPEGIFBYTECOUNT,1,1,TIFF_LONG8,0,TIFF_SETGET_UINT64,TIFF_SETGET _UNDEFINED,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH,TRUE,FALSE,"JpegInterchangeFo rmatLength",NULL},
183 {TIFFTAG_JPEGQTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SET GET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGQTABLES,FALSE,TRUE,"JpegQTa bles",NULL},
184 {TIFFTAG_JPEGDCTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SE TGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGDCTABLES,FALSE,TRUE,"JpegD cTables",NULL},
185 {TIFFTAG_JPEGACTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SE TGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGACTABLES,FALSE,TRUE,"JpegA cTables",NULL},
186 {TIFFTAG_JPEGPROC,1,1,TIFF_SHORT,0,TIFF_SETGET_UINT16,TIFF_SETGET_UNDEFI NED,FIELD_OJPEG_JPEGPROC,FALSE,FALSE,"JpegProc",NULL},
187 {TIFFTAG_JPEGRESTARTINTERVAL,1,1,TIFF_SHORT,0,TIFF_SETGET_UINT16,TIFF_SE TGET_UNDEFINED,FIELD_OJPEG_JPEGRESTARTINTERVAL,FALSE,FALSE,"JpegRestartInterval" ,NULL},
188 };
189
190 #ifndef LIBJPEG_ENCAP_EXTERNAL
191 #include <setjmp.h>
192 #endif
193
194 /* We undefine FAR to avoid conflict with JPEG definition */
195
196 #ifdef FAR
197 #undef FAR
198 #endif
199
200 /*
201 Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
202 not defined. Unfortunately, the MinGW and Borland compilers include
203 a typedef for INT32, which causes a conflict. MSVC does not include
204 a conficting typedef given the headers which are included.
205 */
206 #if defined(__BORLANDC__) || defined(__MINGW32__)
207 # define XMD_H 1
208 #endif
209
210 /* Define "boolean" as unsigned char, not int, per Windows custom. */
211 #if defined(__WIN32__) && !defined(__MINGW32__)
212 # ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
213 typedef unsigned char boolean;
214 # endif
215 # define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
216 #endif
217
218 #if defined(USE_SYSTEM_LIBJPEG)
219 #include <jpeglib.h>
220 #elif defined(USE_LIBJPEG_TURBO)
221 #include "third_party/libjpeg_turbo/jpeglib.h"
222 #else
223 #include "third_party/libjpeg/jpeglib.h"
224 #endif
225
226 typedef struct jpeg_error_mgr jpeg_error_mgr;
227 typedef struct jpeg_common_struct jpeg_common_struct;
228 typedef struct jpeg_decompress_struct jpeg_decompress_struct;
229 typedef struct jpeg_source_mgr jpeg_source_mgr;
230
231 typedef enum {
232 osibsNotSetYet,
233 osibsJpegInterchangeFormat,
234 osibsStrile,
235 osibsEof
236 } OJPEGStateInBufferSource;
237
238 typedef enum {
239 ososSoi,
240 ososQTable0,ososQTable1,ososQTable2,ososQTable3,
241 ososDcTable0,ososDcTable1,ososDcTable2,ososDcTable3,
242 ososAcTable0,ososAcTable1,ososAcTable2,ososAcTable3,
243 ososDri,
244 ososSof,
245 ososSos,
246 ososCompressed,
247 ososRst,
248 ososEoi
249 } OJPEGStateOutState;
250
251 typedef struct {
252 TIFF* tif;
253 #ifndef LIBJPEG_ENCAP_EXTERNAL
254 JMP_BUF exit_jmpbuf;
255 #endif
256 TIFFVGetMethod vgetparent;
257 TIFFVSetMethod vsetparent;
258 TIFFPrintMethod printdir;
259 uint64 file_size;
260 uint32 image_width;
261 uint32 image_length;
262 uint32 strile_width;
263 uint32 strile_length;
264 uint32 strile_length_total;
265 uint8 samples_per_pixel;
266 uint8 plane_sample_offset;
267 uint8 samples_per_pixel_per_plane;
268 uint64 jpeg_interchange_format;
269 uint64 jpeg_interchange_format_length;
270 uint8 jpeg_proc;
271 uint8 subsamplingcorrect;
272 uint8 subsamplingcorrect_done;
273 uint8 subsampling_tag;
274 uint8 subsampling_hor;
275 uint8 subsampling_ver;
276 uint8 subsampling_force_desubsampling_inside_decompression;
277 uint8 qtable_offset_count;
278 uint8 dctable_offset_count;
279 uint8 actable_offset_count;
280 uint64 qtable_offset[3];
281 uint64 dctable_offset[3];
282 uint64 actable_offset[3];
283 uint8* qtable[4];
284 uint8* dctable[4];
285 uint8* actable[4];
286 uint16 restart_interval;
287 uint8 restart_index;
288 uint8 sof_log;
289 uint8 sof_marker_id;
290 uint32 sof_x;
291 uint32 sof_y;
292 uint8 sof_c[3];
293 uint8 sof_hv[3];
294 uint8 sof_tq[3];
295 uint8 sos_cs[3];
296 uint8 sos_tda[3];
297 struct {
298 uint8 log;
299 OJPEGStateInBufferSource in_buffer_source;
300 uint32 in_buffer_next_strile;
301 uint64 in_buffer_file_pos;
302 uint64 in_buffer_file_togo;
303 } sos_end[3];
304 uint8 readheader_done;
305 uint8 writeheader_done;
306 uint16 write_cursample;
307 uint32 write_curstrile;
308 uint8 libjpeg_session_active;
309 uint8 libjpeg_jpeg_query_style;
310 jpeg_error_mgr libjpeg_jpeg_error_mgr;
311 jpeg_decompress_struct libjpeg_jpeg_decompress_struct;
312 jpeg_source_mgr libjpeg_jpeg_source_mgr;
313 uint8 subsampling_convert_log;
314 uint32 subsampling_convert_ylinelen;
315 uint32 subsampling_convert_ylines;
316 uint32 subsampling_convert_clinelen;
317 uint32 subsampling_convert_clines;
318 uint32 subsampling_convert_ybuflen;
319 uint32 subsampling_convert_cbuflen;
320 uint32 subsampling_convert_ycbcrbuflen;
321 uint8* subsampling_convert_ycbcrbuf;
322 uint8* subsampling_convert_ybuf;
323 uint8* subsampling_convert_cbbuf;
324 uint8* subsampling_convert_crbuf;
325 uint32 subsampling_convert_ycbcrimagelen;
326 uint8** subsampling_convert_ycbcrimage;
327 uint32 subsampling_convert_clinelenout;
328 uint32 subsampling_convert_state;
329 uint32 bytes_per_line; /* if the codec outputs subsampled data, a 'lin e' in bytes_per_line */
330 uint32 lines_per_strile; /* and lines_per_strile means subsampling_ver d esubsampled rows */
331 OJPEGStateInBufferSource in_buffer_source;
332 uint32 in_buffer_next_strile;
333 uint32 in_buffer_strile_count;
334 uint64 in_buffer_file_pos;
335 uint8 in_buffer_file_pos_log;
336 uint64 in_buffer_file_togo;
337 uint16 in_buffer_togo;
338 uint8* in_buffer_cur;
339 uint8 in_buffer[OJPEG_BUFFER];
340 OJPEGStateOutState out_state;
341 uint8 out_buffer[OJPEG_BUFFER];
342 uint8* skip_buffer;
343 } OJPEGState;
344
345 static int OJPEGVGetField(TIFF* tif, uint32 tag, va_list ap);
346 static int OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap);
347 static void OJPEGPrintDir(TIFF* tif, FILE* fd, long flags);
348
349 static int OJPEGFixupTags(TIFF* tif);
350 static int OJPEGSetupDecode(TIFF* tif);
351 static int OJPEGPreDecode(TIFF* tif, uint16 s);
352 static int OJPEGPreDecodeSkipRaw(TIFF* tif);
353 static int OJPEGPreDecodeSkipScanlines(TIFF* tif);
354 static int OJPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
355 static int OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc);
356 static int OJPEGDecodeScanlines(TIFF* tif, uint8* buf, tmsize_t cc);
357 static void OJPEGPostDecode(TIFF* tif, uint8* buf, tmsize_t cc);
358 static int OJPEGSetupEncode(TIFF* tif);
359 static int OJPEGPreEncode(TIFF* tif, uint16 s);
360 static int OJPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
361 static int OJPEGPostEncode(TIFF* tif);
362 static void OJPEGCleanup(TIFF* tif);
363
364 static void OJPEGSubsamplingCorrect(TIFF* tif);
365 static int OJPEGReadHeaderInfo(TIFF* tif);
366 static int OJPEGReadSecondarySos(TIFF* tif, uint16 s);
367 static int OJPEGWriteHeaderInfo(TIFF* tif);
368 static void OJPEGLibjpegSessionAbort(TIFF* tif);
369
370 static int OJPEGReadHeaderInfoSec(TIFF* tif);
371 static int OJPEGReadHeaderInfoSecStreamDri(TIFF* tif);
372 static int OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif);
373 static int OJPEGReadHeaderInfoSecStreamDht(TIFF* tif);
374 static int OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id);
375 static int OJPEGReadHeaderInfoSecStreamSos(TIFF* tif);
376 static int OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif);
377 static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif);
378 static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif);
379
380 static int OJPEGReadBufferFill(OJPEGState* sp);
381 static int OJPEGReadByte(OJPEGState* sp, uint8* byte);
382 static int OJPEGReadBytePeek(OJPEGState* sp, uint8* byte);
383 static void OJPEGReadByteAdvance(OJPEGState* sp);
384 static int OJPEGReadWord(OJPEGState* sp, uint16* word);
385 static int OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem);
386 static void OJPEGReadSkip(OJPEGState* sp, uint16 len);
387
388 static int OJPEGWriteStream(TIFF* tif, void** mem, uint32* len);
389 static void OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len);
390 static void OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uin t32* len);
391 static void OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, ui nt32* len);
392 static void OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, ui nt32* len);
393 static void OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len);
394 static void OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len);
395 static void OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len);
396 static int OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len);
397 static void OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len);
398 static void OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len);
399
400 #ifdef LIBJPEG_ENCAP_EXTERNAL
401 extern int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
402 extern int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
403 extern int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* c info);
404 extern int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cin fo, void* scanlines, uint32 max_lines);
405 extern int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinf o, void* data, uint32 max_lines);
406 extern void jpeg_encap_unwind(TIFF* tif);
407 #else
408 static int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* j);
409 static int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
410 static int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* c info);
411 static int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cin fo, void* scanlines, uint32 max_lines);
412 static int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinf o, void* data, uint32 max_lines);
413 static void jpeg_encap_unwind(TIFF* tif);
414 #endif
415
416 static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo);
417 static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo);
418 static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo);
419 static boolean OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo);
420 static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo , long num_bytes);
421 static boolean OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired);
422 static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo);
423
424 int
425 TIFFInitOJPEG(TIFF* tif, int scheme)
426 {
427 static const char module[]="TIFFInitOJPEG";
428 OJPEGState* sp;
429
430 assert(scheme==COMPRESSION_OJPEG);
431
432 /*
433 * Merge codec-specific tag information.
434 */
435 if (!_TIFFMergeFields(tif, ojpegFields, TIFFArrayCount(ojpegFields))) {
436 TIFFErrorExt(tif->tif_clientdata, module,
437 "Merging Old JPEG codec-specific tags failed");
438 return 0;
439 }
440
441 /* state block */
442 sp = (OJPEGState*)_TIFFmalloc(sizeof(OJPEGState)); // // ad d (OJPEGState*) cast
443 if (sp == NULL)
444 {
445 TIFFErrorExt(tif->tif_clientdata,module,"No space for OJPEG stat e block");
446 return(0);
447 }
448 _TIFFmemset(sp,0,sizeof(OJPEGState));
449 sp->tif=tif;
450 sp->jpeg_proc=1;
451 sp->subsampling_hor=2;
452 sp->subsampling_ver=2;
453 TIFFSetField(tif,TIFFTAG_YCBCRSUBSAMPLING,2,2);
454 /* tif codec methods */
455 tif->tif_fixuptags=OJPEGFixupTags;
456 tif->tif_setupdecode=OJPEGSetupDecode;
457 tif->tif_predecode=OJPEGPreDecode;
458 tif->tif_postdecode=OJPEGPostDecode;
459 tif->tif_decoderow=OJPEGDecode;
460 tif->tif_decodestrip=OJPEGDecode;
461 tif->tif_decodetile=OJPEGDecode;
462 tif->tif_setupencode=OJPEGSetupEncode;
463 tif->tif_preencode=OJPEGPreEncode;
464 tif->tif_postencode=OJPEGPostEncode;
465 tif->tif_encoderow=OJPEGEncode;
466 tif->tif_encodestrip=OJPEGEncode;
467 tif->tif_encodetile=OJPEGEncode;
468 tif->tif_cleanup=OJPEGCleanup;
469 tif->tif_data=(uint8*)sp;
470 /* tif tag methods */
471 sp->vgetparent=tif->tif_tagmethods.vgetfield;
472 tif->tif_tagmethods.vgetfield=OJPEGVGetField;
473 sp->vsetparent=tif->tif_tagmethods.vsetfield;
474 tif->tif_tagmethods.vsetfield=OJPEGVSetField;
475 sp->printdir=tif->tif_tagmethods.printdir;
476 tif->tif_tagmethods.printdir=OJPEGPrintDir;
477 /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
478 Some others do, but have totally meaningless or corrupt values
479 in these tags. In these cases, the JpegInterchangeFormat stream is
480 reliable. In any case, this decoder reads the compressed data itself,
481 from the most reliable locations, and we need to notify encapsulating
482 LibTiff not to read raw strips or tiles for us. */
483 tif->tif_flags|=TIFF_NOREADRAW;
484 return(1);
485 }
486
487 static int
488 OJPEGVGetField(TIFF* tif, uint32 tag, va_list ap)
489 {
490 OJPEGState* sp=(OJPEGState*)tif->tif_data;
491 switch(tag)
492 {
493 case TIFFTAG_JPEGIFOFFSET:
494 *va_arg(ap,uint64*)=(uint64)sp->jpeg_interchange_format;
495 break;
496 case TIFFTAG_JPEGIFBYTECOUNT:
497 *va_arg(ap,uint64*)=(uint64)sp->jpeg_interchange_format_ length;
498 break;
499 case TIFFTAG_YCBCRSUBSAMPLING:
500 if (sp->subsamplingcorrect_done==0)
501 OJPEGSubsamplingCorrect(tif);
502 *va_arg(ap,uint16*)=(uint16)sp->subsampling_hor;
503 *va_arg(ap,uint16*)=(uint16)sp->subsampling_ver;
504 break;
505 case TIFFTAG_JPEGQTABLES:
506 *va_arg(ap,uint32*)=(uint32)sp->qtable_offset_count;
507 *va_arg(ap,void**)=(void*)sp->qtable_offset;
508 break;
509 case TIFFTAG_JPEGDCTABLES:
510 *va_arg(ap,uint32*)=(uint32)sp->dctable_offset_count;
511 *va_arg(ap,void**)=(void*)sp->dctable_offset;
512 break;
513 case TIFFTAG_JPEGACTABLES:
514 *va_arg(ap,uint32*)=(uint32)sp->actable_offset_count;
515 *va_arg(ap,void**)=(void*)sp->actable_offset;
516 break;
517 case TIFFTAG_JPEGPROC:
518 *va_arg(ap,uint16*)=(uint16)sp->jpeg_proc;
519 break;
520 case TIFFTAG_JPEGRESTARTINTERVAL:
521 *va_arg(ap,uint16*)=sp->restart_interval;
522 break;
523 default:
524 return (*sp->vgetparent)(tif,tag,ap);
525 }
526 return (1);
527 }
528
529 static int
530 OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap)
531 {
532 static const char module[]="OJPEGVSetField";
533 OJPEGState* sp=(OJPEGState*)tif->tif_data;
534 uint32 ma;
535 uint64* mb;
536 uint32 n;
537 switch(tag)
538 {
539 case TIFFTAG_JPEGIFOFFSET:
540 sp->jpeg_interchange_format=(uint64)va_arg(ap,uint64);
541 break;
542 case TIFFTAG_JPEGIFBYTECOUNT:
543 sp->jpeg_interchange_format_length=(uint64)va_arg(ap,uin t64);
544 break;
545 case TIFFTAG_YCBCRSUBSAMPLING:
546 sp->subsampling_tag=1;
547 sp->subsampling_hor=(uint8)va_arg(ap,uint16_vap);
548 sp->subsampling_ver=(uint8)va_arg(ap,uint16_vap);
549 tif->tif_dir.td_ycbcrsubsampling[0]=sp->subsampling_hor;
550 tif->tif_dir.td_ycbcrsubsampling[1]=sp->subsampling_ver;
551 break;
552 case TIFFTAG_JPEGQTABLES:
553 ma=(uint32)va_arg(ap,uint32);
554 if (ma!=0)
555 {
556 if (ma>3)
557 {
558 TIFFErrorExt(tif->tif_clientdata,module, "JpegQTables tag has incorrect count");
559 return(0);
560 }
561 sp->qtable_offset_count=(uint8)ma;
562 mb=(uint64*)va_arg(ap,uint64*);
563 for (n=0; n<ma; n++)
564 sp->qtable_offset[n]=mb[n];
565 }
566 break;
567 case TIFFTAG_JPEGDCTABLES:
568 ma=(uint32)va_arg(ap,uint32);
569 if (ma!=0)
570 {
571 if (ma>3)
572 {
573 TIFFErrorExt(tif->tif_clientdata,module, "JpegDcTables tag has incorrect count");
574 return(0);
575 }
576 sp->dctable_offset_count=(uint8)ma;
577 mb=(uint64*)va_arg(ap,uint64*);
578 for (n=0; n<ma; n++)
579 sp->dctable_offset[n]=mb[n];
580 }
581 break;
582 case TIFFTAG_JPEGACTABLES:
583 ma=(uint32)va_arg(ap,uint32);
584 if (ma!=0)
585 {
586 if (ma>3)
587 {
588 TIFFErrorExt(tif->tif_clientdata,module, "JpegAcTables tag has incorrect count");
589 return(0);
590 }
591 sp->actable_offset_count=(uint8)ma;
592 mb=(uint64*)va_arg(ap,uint64*);
593 for (n=0; n<ma; n++)
594 sp->actable_offset[n]=mb[n];
595 }
596 break;
597 case TIFFTAG_JPEGPROC:
598 sp->jpeg_proc=(uint8)va_arg(ap,uint16_vap);
599 break;
600 case TIFFTAG_JPEGRESTARTINTERVAL:
601 sp->restart_interval=(uint16)va_arg(ap,uint16_vap);
602 break;
603 default:
604 return (*sp->vsetparent)(tif,tag,ap);
605 }
606 TIFFSetFieldBit(tif,TIFFFieldWithTag(tif,tag)->field_bit);
607 tif->tif_flags|=TIFF_DIRTYDIRECT;
608 return(1);
609 }
610
611 static void
612 OJPEGPrintDir(TIFF* tif, FILE* fd, long flags)
613 {
614 OJPEGState* sp=(OJPEGState*)tif->tif_data;
615 uint8 m;
616 (void)flags;
617 assert(sp!=NULL);
618 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMAT))
619 fprintf(fd," JpegInterchangeFormat: " TIFF_UINT64_FORMAT "\n",( TIFF_UINT64_T)sp->jpeg_interchange_format);
620 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH))
621 fprintf(fd," JpegInterchangeFormatLength: " TIFF_UINT64_FORMAT "\n",(TIFF_UINT64_T)sp->jpeg_interchange_format_length);
622 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGQTABLES))
623 {
624 fprintf(fd," JpegQTables:");
625 for (m=0; m<sp->qtable_offset_count; m++)
626 fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->qta ble_offset[m]);
627 fprintf(fd,"\n");
628 }
629 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGDCTABLES))
630 {
631 fprintf(fd," JpegDcTables:");
632 for (m=0; m<sp->dctable_offset_count; m++)
633 fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->dct able_offset[m]);
634 fprintf(fd,"\n");
635 }
636 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGACTABLES))
637 {
638 fprintf(fd," JpegAcTables:");
639 for (m=0; m<sp->actable_offset_count; m++)
640 fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->act able_offset[m]);
641 fprintf(fd,"\n");
642 }
643 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGPROC))
644 fprintf(fd," JpegProc: %u\n",(unsigned int)sp->jpeg_proc);
645 if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGRESTARTINTERVAL))
646 fprintf(fd," JpegRestartInterval: %u\n",(unsigned int)sp->resta rt_interval);
647 if (sp->printdir)
648 (*sp->printdir)(tif, fd, flags);
649 }
650
651 static int
652 OJPEGFixupTags(TIFF* tif)
653 {
654 (void) tif;
655 return(1);
656 }
657
658 static int
659 OJPEGSetupDecode(TIFF* tif)
660 {
661 static const char module[]="OJPEGSetupDecode";
662 TIFFWarningExt(tif->tif_clientdata,module,"Depreciated and troublesome o ld-style JPEG compression mode, please convert to new-style JPEG compression and notify vendor of writing software");
663 return(1);
664 }
665
666 static int
667 OJPEGPreDecode(TIFF* tif, uint16 s)
668 {
669 OJPEGState* sp=(OJPEGState*)tif->tif_data;
670 uint32 m;
671 if (sp->subsamplingcorrect_done==0)
672 OJPEGSubsamplingCorrect(tif);
673 if (sp->readheader_done==0)
674 {
675 if (OJPEGReadHeaderInfo(tif)==0)
676 return(0);
677 }
678 if (sp->sos_end[s].log==0)
679 {
680 if (OJPEGReadSecondarySos(tif,s)==0)
681 return(0);
682 }
683 if isTiled(tif)
684 m=tif->tif_curtile;
685 else
686 m=tif->tif_curstrip;
687 if ((sp->writeheader_done!=0) && ((sp->write_cursample!=s) || (sp->write _curstrile>m)))
688 {
689 if (sp->libjpeg_session_active!=0)
690 OJPEGLibjpegSessionAbort(tif);
691 sp->writeheader_done=0;
692 }
693 if (sp->writeheader_done==0)
694 {
695 sp->plane_sample_offset=(uint8)s;
696 sp->write_cursample=s;
697 sp->write_curstrile=s*tif->tif_dir.td_stripsperimage;
698 if ((sp->in_buffer_file_pos_log==0) ||
699 (sp->in_buffer_file_pos-sp->in_buffer_togo!=sp->sos_end[s].i n_buffer_file_pos))
700 {
701 sp->in_buffer_source=sp->sos_end[s].in_buffer_source;
702 sp->in_buffer_next_strile=sp->sos_end[s].in_buffer_next_ strile;
703 sp->in_buffer_file_pos=sp->sos_end[s].in_buffer_file_pos ;
704 sp->in_buffer_file_pos_log=0;
705 sp->in_buffer_file_togo=sp->sos_end[s].in_buffer_file_to go;
706 sp->in_buffer_togo=0;
707 sp->in_buffer_cur=0;
708 }
709 if (OJPEGWriteHeaderInfo(tif)==0)
710 return(0);
711 }
712 while (sp->write_curstrile<m)
713 {
714 if (sp->libjpeg_jpeg_query_style==0)
715 {
716 if (OJPEGPreDecodeSkipRaw(tif)==0)
717 return(0);
718 }
719 else
720 {
721 if (OJPEGPreDecodeSkipScanlines(tif)==0)
722 return(0);
723 }
724 sp->write_curstrile++;
725 }
726 return(1);
727 }
728
729 static int
730 OJPEGPreDecodeSkipRaw(TIFF* tif)
731 {
732 OJPEGState* sp=(OJPEGState*)tif->tif_data;
733 uint32 m;
734 m=sp->lines_per_strile;
735 if (sp->subsampling_convert_state!=0)
736 {
737 if (sp->subsampling_convert_clines-sp->subsampling_convert_state >=m)
738 {
739 sp->subsampling_convert_state+=m;
740 if (sp->subsampling_convert_state==sp->subsampling_conve rt_clines)
741 sp->subsampling_convert_state=0;
742 return(1);
743 }
744 m-=sp->subsampling_convert_clines-sp->subsampling_convert_state;
745 sp->subsampling_convert_state=0;
746 }
747 while (m>=sp->subsampling_convert_clines)
748 {
749 if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_st ruct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
750 return(0);
751 m-=sp->subsampling_convert_clines;
752 }
753 if (m>0)
754 {
755 if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_st ruct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
756 return(0);
757 sp->subsampling_convert_state=m;
758 }
759 return(1);
760 }
761
762 static int
763 OJPEGPreDecodeSkipScanlines(TIFF* tif)
764 {
765 static const char module[]="OJPEGPreDecodeSkipScanlines";
766 OJPEGState* sp=(OJPEGState*)tif->tif_data;
767 uint32 m;
768 if (sp->skip_buffer==NULL)
769 {
770 // add (uint8*) cast
771 sp->skip_buffer = (uint8*)_TIFFmalloc(sp->bytes_per_line);
772 if (sp->skip_buffer == NULL)
773 {
774 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory") ;
775 return(0);
776 }
777 }
778 for (m=0; m<sp->lines_per_strile; m++)
779 {
780 if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_s truct),&sp->skip_buffer,1)==0)
781 return(0);
782 }
783 return(1);
784 }
785
786 static int
787 OJPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
788 {
789 OJPEGState* sp=(OJPEGState*)tif->tif_data;
790 (void)s;
791 if (sp->libjpeg_jpeg_query_style==0)
792 {
793 if (OJPEGDecodeRaw(tif,buf,cc)==0)
794 return(0);
795 }
796 else
797 {
798 if (OJPEGDecodeScanlines(tif,buf,cc)==0)
799 return(0);
800 }
801 return(1);
802 }
803
804 static int
805 OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc)
806 {
807 static const char module[]="OJPEGDecodeRaw";
808 OJPEGState* sp=(OJPEGState*)tif->tif_data;
809 uint8* m;
810 tmsize_t n;
811 uint8* oy;
812 uint8* ocb;
813 uint8* ocr;
814 uint8* p;
815 uint32 q;
816 uint8* r;
817 uint8 sx,sy;
818 if (cc%sp->bytes_per_line!=0)
819 {
820 TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
821 return(0);
822 }
823 assert(cc>0);
824 m=buf;
825 n=cc;
826 do
827 {
828 if (sp->subsampling_convert_state==0)
829 {
830 if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decom press_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
831 return(0);
832 }
833 oy=sp->subsampling_convert_ybuf+sp->subsampling_convert_state*sp ->subsampling_ver*sp->subsampling_convert_ylinelen;
834 ocb=sp->subsampling_convert_cbbuf+sp->subsampling_convert_state* sp->subsampling_convert_clinelen;
835 ocr=sp->subsampling_convert_crbuf+sp->subsampling_convert_state* sp->subsampling_convert_clinelen;
836 p=m;
837 for (q=0; q<sp->subsampling_convert_clinelenout; q++)
838 {
839 r=oy;
840 for (sy=0; sy<sp->subsampling_ver; sy++)
841 {
842 for (sx=0; sx<sp->subsampling_hor; sx++)
843 *p++=*r++;
844 r+=sp->subsampling_convert_ylinelen-sp->subsampl ing_hor;
845 }
846 oy+=sp->subsampling_hor;
847 *p++=*ocb++;
848 *p++=*ocr++;
849 }
850 sp->subsampling_convert_state++;
851 if (sp->subsampling_convert_state==sp->subsampling_convert_cline s)
852 sp->subsampling_convert_state=0;
853 m+=sp->bytes_per_line;
854 n-=sp->bytes_per_line;
855 } while(n>0);
856 return(1);
857 }
858
859 static int
860 OJPEGDecodeScanlines(TIFF* tif, uint8* buf, tmsize_t cc)
861 {
862 static const char module[]="OJPEGDecodeScanlines";
863 OJPEGState* sp=(OJPEGState*)tif->tif_data;
864 uint8* m;
865 tmsize_t n;
866 if (cc%sp->bytes_per_line!=0)
867 {
868 TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
869 return(0);
870 }
871 assert(cc>0);
872 m=buf;
873 n=cc;
874 do
875 {
876 if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_s truct),&m,1)==0)
877 return(0);
878 m+=sp->bytes_per_line;
879 n-=sp->bytes_per_line;
880 } while(n>0);
881 return(1);
882 }
883
884 static void
885 OJPEGPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
886 {
887 OJPEGState* sp=(OJPEGState*)tif->tif_data;
888 (void)buf;
889 (void)cc;
890 sp->write_curstrile++;
891 if (sp->write_curstrile%tif->tif_dir.td_stripsperimage==0)
892 {
893 assert(sp->libjpeg_session_active!=0);
894 OJPEGLibjpegSessionAbort(tif);
895 sp->writeheader_done=0;
896 }
897 }
898
899 static int
900 OJPEGSetupEncode(TIFF* tif)
901 {
902 static const char module[]="OJPEGSetupEncode";
903 TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; u se new-style JPEG compression instead");
904 return(0);
905 }
906
907 static int
908 OJPEGPreEncode(TIFF* tif, uint16 s)
909 {
910 static const char module[]="OJPEGPreEncode";
911 (void)s;
912 TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; u se new-style JPEG compression instead");
913 return(0);
914 }
915
916 static int
917 OJPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
918 {
919 static const char module[]="OJPEGEncode";
920 (void)buf;
921 (void)cc;
922 (void)s;
923 TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; u se new-style JPEG compression instead");
924 return(0);
925 }
926
927 static int
928 OJPEGPostEncode(TIFF* tif)
929 {
930 static const char module[]="OJPEGPostEncode";
931 TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; u se new-style JPEG compression instead");
932 return(0);
933 }
934
935 static void
936 OJPEGCleanup(TIFF* tif)
937 {
938 OJPEGState* sp=(OJPEGState*)tif->tif_data;
939 if (sp!=0)
940 {
941 tif->tif_tagmethods.vgetfield=sp->vgetparent;
942 tif->tif_tagmethods.vsetfield=sp->vsetparent;
943 tif->tif_tagmethods.printdir=sp->printdir;
944 if (sp->qtable[0]!=0)
945 _TIFFfree(sp->qtable[0]);
946 if (sp->qtable[1]!=0)
947 _TIFFfree(sp->qtable[1]);
948 if (sp->qtable[2]!=0)
949 _TIFFfree(sp->qtable[2]);
950 if (sp->qtable[3]!=0)
951 _TIFFfree(sp->qtable[3]);
952 if (sp->dctable[0]!=0)
953 _TIFFfree(sp->dctable[0]);
954 if (sp->dctable[1]!=0)
955 _TIFFfree(sp->dctable[1]);
956 if (sp->dctable[2]!=0)
957 _TIFFfree(sp->dctable[2]);
958 if (sp->dctable[3]!=0)
959 _TIFFfree(sp->dctable[3]);
960 if (sp->actable[0]!=0)
961 _TIFFfree(sp->actable[0]);
962 if (sp->actable[1]!=0)
963 _TIFFfree(sp->actable[1]);
964 if (sp->actable[2]!=0)
965 _TIFFfree(sp->actable[2]);
966 if (sp->actable[3]!=0)
967 _TIFFfree(sp->actable[3]);
968 if (sp->libjpeg_session_active!=0)
969 OJPEGLibjpegSessionAbort(tif);
970 if (sp->subsampling_convert_ycbcrbuf!=0)
971 _TIFFfree(sp->subsampling_convert_ycbcrbuf);
972 if (sp->subsampling_convert_ycbcrimage!=0)
973 _TIFFfree(sp->subsampling_convert_ycbcrimage);
974 if (sp->skip_buffer!=0)
975 _TIFFfree(sp->skip_buffer);
976 _TIFFfree(sp);
977 tif->tif_data=NULL;
978 _TIFFSetDefaultCompressionState(tif);
979 }
980 }
981
982 static void
983 OJPEGSubsamplingCorrect(TIFF* tif)
984 {
985 static const char module[]="OJPEGSubsamplingCorrect";
986 OJPEGState* sp=(OJPEGState*)tif->tif_data;
987 uint8 mh;
988 uint8 mv;
989 _TIFFFillStriles( tif );
990
991 assert(sp->subsamplingcorrect_done==0);
992 if ((tif->tif_dir.td_samplesperpixel!=3) || ((tif->tif_dir.td_photometri c!=PHOTOMETRIC_YCBCR) &&
993 (tif->tif_dir.td_photometric!=PHOTOMETRIC_ITULAB)))
994 {
995 if (sp->subsampling_tag!=0)
996 TIFFWarningExt(tif->tif_clientdata,module,"Subsampling t ag not appropriate for this Photometric and/or SamplesPerPixel");
997 sp->subsampling_hor=1;
998 sp->subsampling_ver=1;
999 sp->subsampling_force_desubsampling_inside_decompression=0;
1000 }
1001 else
1002 {
1003 sp->subsamplingcorrect_done=1;
1004 mh=sp->subsampling_hor;
1005 mv=sp->subsampling_ver;
1006 sp->subsamplingcorrect=1;
1007 OJPEGReadHeaderInfoSec(tif);
1008 if (sp->subsampling_force_desubsampling_inside_decompression!=0)
1009 {
1010 sp->subsampling_hor=1;
1011 sp->subsampling_ver=1;
1012 }
1013 sp->subsamplingcorrect=0;
1014 if (((sp->subsampling_hor!=mh) || (sp->subsampling_ver!=mv)) && (sp->subsampling_force_desubsampling_inside_decompression==0))
1015 {
1016 if (sp->subsampling_tag==0)
1017 TIFFWarningExt(tif->tif_clientdata,module,"Subsa mpling tag is not set, yet subsampling inside JPEG data [%d,%d] does not match d efault values [2,2]; assuming subsampling inside JPEG data is correct",sp->subsa mpling_hor,sp->subsampling_ver);
1018 else
1019 TIFFWarningExt(tif->tif_clientdata,module,"Subsa mpling inside JPEG data [%d,%d] does not match subsampling tag values [%d,%d]; a ssuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsamp ling_ver,mh,mv);
1020 }
1021 if (sp->subsampling_force_desubsampling_inside_decompression!=0)
1022 {
1023 if (sp->subsampling_tag==0)
1024 TIFFWarningExt(tif->tif_clientdata,module,"Subsa mpling tag is not set, yet subsampling inside JPEG data does not match default v alues [2,2] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression");
1025 else
1026 TIFFWarningExt(tif->tif_clientdata,module,"Subsa mpling inside JPEG data does not match subsampling tag values [%d,%d] (nor any o ther values allowed in TIFF); assuming subsampling inside JPEG data is correct a nd desubsampling inside JPEG decompression",mh,mv);
1027 }
1028 if (sp->subsampling_force_desubsampling_inside_decompression==0)
1029 {
1030 if (sp->subsampling_hor<sp->subsampling_ver)
1031 TIFFWarningExt(tif->tif_clientdata,module,"Subsa mpling values [%d,%d] are not allowed in TIFF",sp->subsampling_hor,sp->subsampli ng_ver);
1032 }
1033 }
1034 sp->subsamplingcorrect_done=1;
1035 }
1036
1037 static int
1038 OJPEGReadHeaderInfo(TIFF* tif)
1039 {
1040 static const char module[]="OJPEGReadHeaderInfo";
1041 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1042 assert(sp->readheader_done==0);
1043 sp->image_width=tif->tif_dir.td_imagewidth;
1044 sp->image_length=tif->tif_dir.td_imagelength;
1045 if isTiled(tif)
1046 {
1047 sp->strile_width=tif->tif_dir.td_tilewidth;
1048 sp->strile_length=tif->tif_dir.td_tilelength;
1049 sp->strile_length_total=((sp->image_length+sp->strile_length-1)/ sp->strile_length)*sp->strile_length;
1050 }
1051 else
1052 {
1053 sp->strile_width=sp->image_width;
1054 sp->strile_length=tif->tif_dir.td_rowsperstrip;
1055 sp->strile_length_total=sp->image_length;
1056 }
1057 if (tif->tif_dir.td_samplesperpixel==1)
1058 {
1059 sp->samples_per_pixel=1;
1060 sp->plane_sample_offset=0;
1061 sp->samples_per_pixel_per_plane=sp->samples_per_pixel;
1062 sp->subsampling_hor=1;
1063 sp->subsampling_ver=1;
1064 }
1065 else
1066 {
1067 if (tif->tif_dir.td_samplesperpixel!=3)
1068 {
1069 TIFFErrorExt(tif->tif_clientdata,module,"SamplesPerPixel %d not supported for this compression scheme",sp->samples_per_pixel);
1070 return(0);
1071 }
1072 sp->samples_per_pixel=3;
1073 sp->plane_sample_offset=0;
1074 if (tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)
1075 sp->samples_per_pixel_per_plane=3;
1076 else
1077 sp->samples_per_pixel_per_plane=1;
1078 }
1079 if (sp->strile_length<sp->image_length)
1080 {
1081 if (sp->strile_length%(sp->subsampling_ver*8)!=0)
1082 {
1083 TIFFErrorExt(tif->tif_clientdata,module,"Incompatible ve rtical subsampling and image strip/tile length");
1084 return(0);
1085 }
1086 sp->restart_interval=((sp->strile_width+sp->subsampling_hor*8-1) /(sp->subsampling_hor*8))*(sp->strile_length/(sp->subsampling_ver*8));
1087 }
1088 if (OJPEGReadHeaderInfoSec(tif)==0)
1089 return(0);
1090 sp->sos_end[0].log=1;
1091 sp->sos_end[0].in_buffer_source=sp->in_buffer_source;
1092 sp->sos_end[0].in_buffer_next_strile=sp->in_buffer_next_strile;
1093 sp->sos_end[0].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_t ogo;
1094 sp->sos_end[0].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer _togo;
1095 sp->readheader_done=1;
1096 return(1);
1097 }
1098
1099 static int
1100 OJPEGReadSecondarySos(TIFF* tif, uint16 s)
1101 {
1102 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1103 uint8 m;
1104 assert(s>0);
1105 assert(s<3);
1106 assert(sp->sos_end[0].log!=0);
1107 assert(sp->sos_end[s].log==0);
1108 sp->plane_sample_offset=s-1;
1109 while(sp->sos_end[sp->plane_sample_offset].log==0)
1110 sp->plane_sample_offset--;
1111 sp->in_buffer_source=sp->sos_end[sp->plane_sample_offset].in_buffer_sour ce;
1112 sp->in_buffer_next_strile=sp->sos_end[sp->plane_sample_offset].in_buffer _next_strile;
1113 sp->in_buffer_file_pos=sp->sos_end[sp->plane_sample_offset].in_buffer_fi le_pos;
1114 sp->in_buffer_file_pos_log=0;
1115 sp->in_buffer_file_togo=sp->sos_end[sp->plane_sample_offset].in_buffer_f ile_togo;
1116 sp->in_buffer_togo=0;
1117 sp->in_buffer_cur=0;
1118 while(sp->plane_sample_offset<s)
1119 {
1120 do
1121 {
1122 if (OJPEGReadByte(sp,&m)==0)
1123 return(0);
1124 if (m==255)
1125 {
1126 do
1127 {
1128 if (OJPEGReadByte(sp,&m)==0)
1129 return(0);
1130 if (m!=255)
1131 break;
1132 } while(1);
1133 if (m==JPEG_MARKER_SOS)
1134 break;
1135 }
1136 } while(1);
1137 sp->plane_sample_offset++;
1138 if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
1139 return(0);
1140 sp->sos_end[sp->plane_sample_offset].log=1;
1141 sp->sos_end[sp->plane_sample_offset].in_buffer_source=sp->in_buf fer_source;
1142 sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile=sp->i n_buffer_next_strile;
1143 sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos=sp->in_b uffer_file_pos-sp->in_buffer_togo;
1144 sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo=sp->in_ buffer_file_togo+sp->in_buffer_togo;
1145 }
1146 return(1);
1147 }
1148
1149 static int
1150 OJPEGWriteHeaderInfo(TIFF* tif)
1151 {
1152 static const char module[]="OJPEGWriteHeaderInfo";
1153 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1154 uint8** m;
1155 uint32 n;
1156 /* if a previous attempt failed, don't try again */
1157 if (sp->libjpeg_session_active != 0)
1158 return 0;
1159 sp->out_state=ososSoi;
1160 sp->restart_index=0;
1161 jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr));
1162 sp->libjpeg_jpeg_error_mgr.output_message=OJPEGLibjpegJpegErrorMgrOutput Message;
1163 sp->libjpeg_jpeg_error_mgr.error_exit=OJPEGLibjpegJpegErrorMgrErrorExit;
1164 sp->libjpeg_jpeg_decompress_struct.err=&(sp->libjpeg_jpeg_error_mgr);
1165 sp->libjpeg_jpeg_decompress_struct.client_data=(void*)tif;
1166 if (jpeg_create_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct ))==0)
1167 return(0);
1168 sp->libjpeg_session_active=1;
1169 sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=0;
1170 sp->libjpeg_jpeg_source_mgr.init_source=OJPEGLibjpegJpegSourceMgrInitSou rce;
1171 sp->libjpeg_jpeg_source_mgr.fill_input_buffer=OJPEGLibjpegJpegSourceMgrF illInputBuffer;
1172 sp->libjpeg_jpeg_source_mgr.skip_input_data=OJPEGLibjpegJpegSourceMgrSki pInputData;
1173 sp->libjpeg_jpeg_source_mgr.resync_to_restart=OJPEGLibjpegJpegSourceMgrR esyncToRestart;
1174 sp->libjpeg_jpeg_source_mgr.term_source=OJPEGLibjpegJpegSourceMgrTermSou rce;
1175 sp->libjpeg_jpeg_decompress_struct.src=&(sp->libjpeg_jpeg_source_mgr);
1176 if (jpeg_read_header_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),1)== 0)
1177 return(0);
1178 if ((sp->subsampling_force_desubsampling_inside_decompression==0) && (sp ->samples_per_pixel_per_plane>1))
1179 {
1180 sp->libjpeg_jpeg_decompress_struct.raw_data_out=1;
1181 #if JPEG_LIB_VERSION >= 70
1182 sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling=FALSE;
1183 #endif
1184 sp->libjpeg_jpeg_query_style=0;
1185 if (sp->subsampling_convert_log==0)
1186 {
1187 assert(sp->subsampling_convert_ycbcrbuf==0);
1188 assert(sp->subsampling_convert_ycbcrimage==0);
1189 sp->subsampling_convert_ylinelen=((sp->strile_width+sp-> subsampling_hor*8-1)/(sp->subsampling_hor*8)*sp->subsampling_hor*8);
1190 sp->subsampling_convert_ylines=sp->subsampling_ver*8;
1191 sp->subsampling_convert_clinelen=sp->subsampling_convert _ylinelen/sp->subsampling_hor;
1192 sp->subsampling_convert_clines=8;
1193 sp->subsampling_convert_ybuflen=sp->subsampling_convert_ ylinelen*sp->subsampling_convert_ylines;
1194 sp->subsampling_convert_cbuflen=sp->subsampling_convert_ clinelen*sp->subsampling_convert_clines;
1195 sp->subsampling_convert_ycbcrbuflen=sp->subsampling_conv ert_ybuflen+2*sp->subsampling_convert_cbuflen;
1196 sp->subsampling_convert_ycbcrbuf = (uint8*)_TIFFmalloc(s p->subsampling_convert_ycbcrbuflen); // add (uint8*) cast
1197 if (sp->subsampling_convert_ycbcrbuf==0)
1198 {
1199 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1200 return(0);
1201 }
1202 sp->subsampling_convert_ybuf=sp->subsampling_convert_ycb crbuf;
1203 sp->subsampling_convert_cbbuf=sp->subsampling_convert_yb uf+sp->subsampling_convert_ybuflen;
1204 sp->subsampling_convert_crbuf=sp->subsampling_convert_cb buf+sp->subsampling_convert_cbuflen;
1205 sp->subsampling_convert_ycbcrimagelen=3+sp->subsampling_ convert_ylines+2*sp->subsampling_convert_clines;
1206 sp->subsampling_convert_ycbcrimage = (uint8**)_TIFFmallo c(sp->subsampling_convert_ycbcrimagelen * sizeof(uint8*));// add (uint8**) cast
1207 if (sp->subsampling_convert_ycbcrimage==0)
1208 {
1209 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1210 return(0);
1211 }
1212 m=sp->subsampling_convert_ycbcrimage;
1213 *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3);
1214 *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->s ubsampling_convert_ylines);
1215 *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->s ubsampling_convert_ylines+sp->subsampling_convert_clines);
1216 for (n=0; n<sp->subsampling_convert_ylines; n++)
1217 *m++=sp->subsampling_convert_ybuf+n*sp->subsampl ing_convert_ylinelen;
1218 for (n=0; n<sp->subsampling_convert_clines; n++)
1219 *m++=sp->subsampling_convert_cbbuf+n*sp->subsamp ling_convert_clinelen;
1220 for (n=0; n<sp->subsampling_convert_clines; n++)
1221 *m++=sp->subsampling_convert_crbuf+n*sp->subsamp ling_convert_clinelen;
1222 sp->subsampling_convert_clinelenout=((sp->strile_width+s p->subsampling_hor-1)/sp->subsampling_hor);
1223 sp->subsampling_convert_state=0;
1224 sp->bytes_per_line=sp->subsampling_convert_clinelenout*( sp->subsampling_ver*sp->subsampling_hor+2);
1225 sp->lines_per_strile=((sp->strile_length+sp->subsampling _ver-1)/sp->subsampling_ver);
1226 sp->subsampling_convert_log=1;
1227 }
1228 }
1229 else
1230 {
1231 sp->libjpeg_jpeg_decompress_struct.jpeg_color_space=JCS_UNKNOWN;
1232 sp->libjpeg_jpeg_decompress_struct.out_color_space=JCS_UNKNOWN;
1233 sp->libjpeg_jpeg_query_style=1;
1234 sp->bytes_per_line=sp->samples_per_pixel_per_plane*sp->strile_wi dth;
1235 sp->lines_per_strile=sp->strile_length;
1236 }
1237 if (jpeg_start_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct) )==0)
1238 return(0);
1239 sp->writeheader_done=1;
1240 return(1);
1241 }
1242
1243 static void
1244 OJPEGLibjpegSessionAbort(TIFF* tif)
1245 {
1246 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1247 assert(sp->libjpeg_session_active!=0);
1248 jpeg_destroy((jpeg_common_struct*)(&(sp->libjpeg_jpeg_decompress_struct) ));
1249 sp->libjpeg_session_active=0;
1250 }
1251
1252 static int
1253 OJPEGReadHeaderInfoSec(TIFF* tif)
1254 {
1255 static const char module[]="OJPEGReadHeaderInfoSec";
1256 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1257 uint8 m;
1258 uint16 n;
1259 uint8 o;
1260 if (sp->file_size==0)
1261 sp->file_size=TIFFGetFileSize(tif);
1262 if (sp->jpeg_interchange_format!=0)
1263 {
1264 if (sp->jpeg_interchange_format>=sp->file_size)
1265 {
1266 sp->jpeg_interchange_format=0;
1267 sp->jpeg_interchange_format_length=0;
1268 }
1269 else
1270 {
1271 if ((sp->jpeg_interchange_format_length==0) || (sp->jpeg _interchange_format+sp->jpeg_interchange_format_length>sp->file_size))
1272 sp->jpeg_interchange_format_length=sp->file_size -sp->jpeg_interchange_format;
1273 }
1274 }
1275 sp->in_buffer_source=osibsNotSetYet;
1276 sp->in_buffer_next_strile=0;
1277 sp->in_buffer_strile_count=tif->tif_dir.td_nstrips;
1278 sp->in_buffer_file_togo=0;
1279 sp->in_buffer_togo=0;
1280 do
1281 {
1282 if (OJPEGReadBytePeek(sp,&m)==0)
1283 return(0);
1284 if (m!=255)
1285 break;
1286 OJPEGReadByteAdvance(sp);
1287 do
1288 {
1289 if (OJPEGReadByte(sp,&m)==0)
1290 return(0);
1291 } while(m==255);
1292 switch(m)
1293 {
1294 case JPEG_MARKER_SOI:
1295 /* this type of marker has no data, and should b e skipped */
1296 break;
1297 case JPEG_MARKER_COM:
1298 case JPEG_MARKER_APP0:
1299 case JPEG_MARKER_APP0+1:
1300 case JPEG_MARKER_APP0+2:
1301 case JPEG_MARKER_APP0+3:
1302 case JPEG_MARKER_APP0+4:
1303 case JPEG_MARKER_APP0+5:
1304 case JPEG_MARKER_APP0+6:
1305 case JPEG_MARKER_APP0+7:
1306 case JPEG_MARKER_APP0+8:
1307 case JPEG_MARKER_APP0+9:
1308 case JPEG_MARKER_APP0+10:
1309 case JPEG_MARKER_APP0+11:
1310 case JPEG_MARKER_APP0+12:
1311 case JPEG_MARKER_APP0+13:
1312 case JPEG_MARKER_APP0+14:
1313 case JPEG_MARKER_APP0+15:
1314 /* this type of marker has data, but it has no u se to us (and no place here) and should be skipped */
1315 if (OJPEGReadWord(sp,&n)==0)
1316 return(0);
1317 if (n<2)
1318 {
1319 if (sp->subsamplingcorrect==0)
1320 TIFFErrorExt(tif->tif_clientdata ,module,"Corrupt JPEG data");
1321 return(0);
1322 }
1323 if (n>2)
1324 OJPEGReadSkip(sp,n-2);
1325 break;
1326 case JPEG_MARKER_DRI:
1327 if (OJPEGReadHeaderInfoSecStreamDri(tif)==0)
1328 return(0);
1329 break;
1330 case JPEG_MARKER_DQT:
1331 if (OJPEGReadHeaderInfoSecStreamDqt(tif)==0)
1332 return(0);
1333 break;
1334 case JPEG_MARKER_DHT:
1335 if (OJPEGReadHeaderInfoSecStreamDht(tif)==0)
1336 return(0);
1337 break;
1338 case JPEG_MARKER_SOF0:
1339 case JPEG_MARKER_SOF1:
1340 case JPEG_MARKER_SOF3:
1341 if (OJPEGReadHeaderInfoSecStreamSof(tif,m)==0)
1342 return(0);
1343 if (sp->subsamplingcorrect!=0)
1344 return(1);
1345 break;
1346 case JPEG_MARKER_SOS:
1347 if (sp->subsamplingcorrect!=0)
1348 return(1);
1349 assert(sp->plane_sample_offset==0);
1350 if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
1351 return(0);
1352 break;
1353 default:
1354 TIFFErrorExt(tif->tif_clientdata,module,"Unknown marker type %d in JPEG data",m);
1355 return(0);
1356 }
1357 } while(m!=JPEG_MARKER_SOS);
1358 if (sp->subsamplingcorrect)
1359 return(1);
1360 if (sp->sof_log==0)
1361 {
1362 if (OJPEGReadHeaderInfoSecTablesQTable(tif)==0)
1363 return(0);
1364 sp->sof_marker_id=JPEG_MARKER_SOF0;
1365 for (o=0; o<sp->samples_per_pixel; o++)
1366 sp->sof_c[o]=o;
1367 sp->sof_hv[0]=((sp->subsampling_hor<<4)|sp->subsampling_ver);
1368 for (o=1; o<sp->samples_per_pixel; o++)
1369 sp->sof_hv[o]=17;
1370 sp->sof_x=sp->strile_width;
1371 sp->sof_y=sp->strile_length_total;
1372 sp->sof_log=1;
1373 if (OJPEGReadHeaderInfoSecTablesDcTable(tif)==0)
1374 return(0);
1375 if (OJPEGReadHeaderInfoSecTablesAcTable(tif)==0)
1376 return(0);
1377 for (o=1; o<sp->samples_per_pixel; o++)
1378 sp->sos_cs[o]=o;
1379 }
1380 return(1);
1381 }
1382
1383 static int
1384 OJPEGReadHeaderInfoSecStreamDri(TIFF* tif)
1385 {
1386 /* this could easilly cause trouble in some cases... but no such cases h ave occured sofar */
1387 static const char module[]="OJPEGReadHeaderInfoSecStreamDri";
1388 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1389 uint16 m;
1390 if (OJPEGReadWord(sp,&m)==0)
1391 return(0);
1392 if (m!=4)
1393 {
1394 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DRI marker in J PEG data");
1395 return(0);
1396 }
1397 if (OJPEGReadWord(sp,&m)==0)
1398 return(0);
1399 sp->restart_interval=m;
1400 return(1);
1401 }
1402
1403 static int
1404 OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif)
1405 {
1406 /* this is a table marker, and it is to be saved as a whole for exact pu shing on the jpeg stream later on */
1407 static const char module[]="OJPEGReadHeaderInfoSecStreamDqt";
1408 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1409 uint16 m;
1410 uint32 na;
1411 uint8* nb;
1412 uint8 o;
1413 if (OJPEGReadWord(sp,&m)==0)
1414 return(0);
1415 if (m<=2)
1416 {
1417 if (sp->subsamplingcorrect==0)
1418 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT mar ker in JPEG data");
1419 return(0);
1420 }
1421 if (sp->subsamplingcorrect!=0)
1422 OJPEGReadSkip(sp,m-2);
1423 else
1424 {
1425 m-=2;
1426 do
1427 {
1428 if (m<65)
1429 {
1430 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
1431 return(0);
1432 }
1433 na = sizeof(uint32) + 69;
1434 nb = (uint8*)_TIFFmalloc(na); // add (uint8*) cast
1435 if (nb == 0)
1436 {
1437 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1438 return(0);
1439 }
1440 *(uint32*)nb=na;
1441 nb[sizeof(uint32)]=255;
1442 nb[sizeof(uint32)+1]=JPEG_MARKER_DQT;
1443 nb[sizeof(uint32)+2]=0;
1444 nb[sizeof(uint32)+3]=67;
1445 if (OJPEGReadBlock(sp,65,&nb[sizeof(uint32)+4])==0) {
1446 _TIFFfree(nb);
1447 return(0);
1448 }
1449 o=nb[sizeof(uint32)+4]&15;
1450 if (3<o)
1451 {
1452 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
1453 _TIFFfree(nb);
1454 return(0);
1455 }
1456 if (sp->qtable[o]!=0)
1457 _TIFFfree(sp->qtable[o]);
1458 sp->qtable[o]=nb;
1459 m-=65;
1460 } while(m>0);
1461 }
1462 return(1);
1463 }
1464
1465 static int
1466 OJPEGReadHeaderInfoSecStreamDht(TIFF* tif)
1467 {
1468 /* this is a table marker, and it is to be saved as a whole for exact pu shing on the jpeg stream later on */
1469 /* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
1470 static const char module[]="OJPEGReadHeaderInfoSecStreamDht";
1471 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1472 uint16 m;
1473 uint32 na;
1474 uint8* nb;
1475 uint8 o;
1476 if (OJPEGReadWord(sp,&m)==0)
1477 return(0);
1478 if (m<=2)
1479 {
1480 if (sp->subsamplingcorrect==0)
1481 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT mar ker in JPEG data");
1482 return(0);
1483 }
1484 if (sp->subsamplingcorrect!=0)
1485 {
1486 OJPEGReadSkip(sp,m-2);
1487 }
1488 else
1489 {
1490 na = sizeof(uint32) + 2 + m;
1491 nb = (uint8*)_TIFFmalloc(na); // add (uint8*) cast
1492 if (nb == 0)
1493 {
1494 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory") ;
1495 return(0);
1496 }
1497 *(uint32*)nb=na;
1498 nb[sizeof(uint32)]=255;
1499 nb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
1500 nb[sizeof(uint32)+2]=(m>>8);
1501 nb[sizeof(uint32)+3]=(m&255);
1502 if (OJPEGReadBlock(sp,m-2,&nb[sizeof(uint32)+4])==0)
1503 return(0);
1504 o=nb[sizeof(uint32)+4];
1505 if ((o&240)==0)
1506 {
1507 if (3<o)
1508 {
1509 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1510 return(0);
1511 }
1512 if (sp->dctable[o]!=0)
1513 _TIFFfree(sp->dctable[o]);
1514 sp->dctable[o]=nb;
1515 }
1516 else
1517 {
1518 if ((o&240)!=16)
1519 {
1520 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1521 return(0);
1522 }
1523 o&=15;
1524 if (3<o)
1525 {
1526 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1527 return(0);
1528 }
1529 if (sp->actable[o]!=0)
1530 _TIFFfree(sp->actable[o]);
1531 sp->actable[o]=nb;
1532 }
1533 }
1534 return(1);
1535 }
1536
1537 static int
1538 OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id)
1539 {
1540 /* this marker needs to be checked, and part of its data needs to be sav ed for regeneration later on */
1541 static const char module[]="OJPEGReadHeaderInfoSecStreamSof";
1542 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1543 uint16 m;
1544 uint16 n;
1545 uint8 o;
1546 uint16 p;
1547 uint16 q;
1548 if (sp->sof_log!=0)
1549 {
1550 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
1551 return(0);
1552 }
1553 if (sp->subsamplingcorrect==0)
1554 sp->sof_marker_id=marker_id;
1555 /* Lf: data length */
1556 if (OJPEGReadWord(sp,&m)==0)
1557 return(0);
1558 if (m<11)
1559 {
1560 if (sp->subsamplingcorrect==0)
1561 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF mar ker in JPEG data");
1562 return(0);
1563 }
1564 m-=8;
1565 if (m%3!=0)
1566 {
1567 if (sp->subsamplingcorrect==0)
1568 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF mar ker in JPEG data");
1569 return(0);
1570 }
1571 n=m/3;
1572 if (sp->subsamplingcorrect==0)
1573 {
1574 if (n!=sp->samples_per_pixel)
1575 {
1576 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of samples");
1577 return(0);
1578 }
1579 }
1580 /* P: Sample precision */
1581 if (OJPEGReadByte(sp,&o)==0)
1582 return(0);
1583 if (o!=8)
1584 {
1585 if (sp->subsamplingcorrect==0)
1586 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of bits per sample");
1587 return(0);
1588 }
1589 /* Y: Number of lines, X: Number of samples per line */
1590 if (sp->subsamplingcorrect)
1591 OJPEGReadSkip(sp,4);
1592 else
1593 {
1594 /* Y: Number of lines */
1595 if (OJPEGReadWord(sp,&p)==0)
1596 return(0);
1597 if (((uint32)p<sp->image_length) && ((uint32)p<sp->strile_length _total))
1598 {
1599 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected height");
1600 return(0);
1601 }
1602 sp->sof_y=p;
1603 /* X: Number of samples per line */
1604 if (OJPEGReadWord(sp,&p)==0)
1605 return(0);
1606 if (((uint32)p<sp->image_width) && ((uint32)p<sp->strile_width))
1607 {
1608 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected width");
1609 return(0);
1610 }
1611 if ((uint32)p>sp->strile_width)
1612 {
1613 TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data image width exceeds expected image width");
1614 return(0);
1615 }
1616 sp->sof_x=p;
1617 }
1618 /* Nf: Number of image components in frame */
1619 if (OJPEGReadByte(sp,&o)==0)
1620 return(0);
1621 if (o!=n)
1622 {
1623 if (sp->subsamplingcorrect==0)
1624 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF mar ker in JPEG data");
1625 return(0);
1626 }
1627 /* per component stuff */
1628 /* TODO: double-check that flow implies that n cannot be as big as to ma ke us overflow sof_c, sof_hv and sof_tq arrays */
1629 for (q=0; q<n; q++)
1630 {
1631 /* C: Component identifier */
1632 if (OJPEGReadByte(sp,&o)==0)
1633 return(0);
1634 if (sp->subsamplingcorrect==0)
1635 sp->sof_c[q]=o;
1636 /* H: Horizontal sampling factor, and V: Vertical sampling facto r */
1637 if (OJPEGReadByte(sp,&o)==0)
1638 return(0);
1639 if (sp->subsamplingcorrect!=0)
1640 {
1641 if (q==0)
1642 {
1643 sp->subsampling_hor=(o>>4);
1644 sp->subsampling_ver=(o&15);
1645 if (((sp->subsampling_hor!=1) && (sp->subsamplin g_hor!=2) && (sp->subsampling_hor!=4)) ||
1646 ((sp->subsampling_ver!=1) && (sp->subsam pling_ver!=2) && (sp->subsampling_ver!=4)))
1647 sp->subsampling_force_desubsampling_insi de_decompression=1;
1648 }
1649 else
1650 {
1651 if (o!=17)
1652 sp->subsampling_force_desubsampling_insi de_decompression=1;
1653 }
1654 }
1655 else
1656 {
1657 sp->sof_hv[q]=o;
1658 if (sp->subsampling_force_desubsampling_inside_decompres sion==0)
1659 {
1660 if (q==0)
1661 {
1662 if (o!=((sp->subsampling_hor<<4)|sp->sub sampling_ver))
1663 {
1664 TIFFErrorExt(tif->tif_clientdata ,module,"JPEG compressed data indicates unexpected subsampling values");
1665 return(0);
1666 }
1667 }
1668 else
1669 {
1670 if (o!=17)
1671 {
1672 TIFFErrorExt(tif->tif_clientdata ,module,"JPEG compressed data indicates unexpected subsampling values");
1673 return(0);
1674 }
1675 }
1676 }
1677 }
1678 /* Tq: Quantization table destination selector */
1679 if (OJPEGReadByte(sp,&o)==0)
1680 return(0);
1681 if (sp->subsamplingcorrect==0)
1682 sp->sof_tq[q]=o;
1683 }
1684 if (sp->subsamplingcorrect==0)
1685 sp->sof_log=1;
1686 return(1);
1687 }
1688
1689 static int
1690 OJPEGReadHeaderInfoSecStreamSos(TIFF* tif)
1691 {
1692 /* this marker needs to be checked, and part of its data needs to be sav ed for regeneration later on */
1693 static const char module[]="OJPEGReadHeaderInfoSecStreamSos";
1694 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1695 uint16 m;
1696 uint8 n;
1697 uint8 o;
1698 assert(sp->subsamplingcorrect==0);
1699 if (sp->sof_log==0)
1700 {
1701 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in J PEG data");
1702 return(0);
1703 }
1704 /* Ls */
1705 if (OJPEGReadWord(sp,&m)==0)
1706 return(0);
1707 if (m!=6+sp->samples_per_pixel_per_plane*2)
1708 {
1709 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in J PEG data");
1710 return(0);
1711 }
1712 /* Ns */
1713 if (OJPEGReadByte(sp,&n)==0)
1714 return(0);
1715 if (n!=sp->samples_per_pixel_per_plane)
1716 {
1717 TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in J PEG data");
1718 return(0);
1719 }
1720 /* Cs, Td, and Ta */
1721 for (o=0; o<sp->samples_per_pixel_per_plane; o++)
1722 {
1723 /* Cs */
1724 if (OJPEGReadByte(sp,&n)==0)
1725 return(0);
1726 sp->sos_cs[sp->plane_sample_offset+o]=n;
1727 /* Td and Ta */
1728 if (OJPEGReadByte(sp,&n)==0)
1729 return(0);
1730 sp->sos_tda[sp->plane_sample_offset+o]=n;
1731 }
1732 /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, a s per LibJpeg source */
1733 OJPEGReadSkip(sp,3);
1734 return(1);
1735 }
1736
1737 static int
1738 OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif)
1739 {
1740 static const char module[]="OJPEGReadHeaderInfoSecTablesQTable";
1741 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1742 uint8 m;
1743 uint8 n;
1744 uint32 oa;
1745 uint8* ob;
1746 uint32 p;
1747 if (sp->qtable_offset[0]==0)
1748 {
1749 TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
1750 return(0);
1751 }
1752 sp->in_buffer_file_pos_log=0;
1753 for (m=0; m<sp->samples_per_pixel; m++)
1754 {
1755 if ((sp->qtable_offset[m]!=0) && ((m==0) || (sp->qtable_offset[m ]!=sp->qtable_offset[m-1])))
1756 {
1757 for (n=0; n<m-1; n++)
1758 {
1759 if (sp->qtable_offset[m]==sp->qtable_offset[n])
1760 {
1761 TIFFErrorExt(tif->tif_clientdata,module, "Corrupt JpegQTables tag value");
1762 return(0);
1763 }
1764 }
1765 oa=sizeof(uint32)+69;
1766 ob = (uint8*)_TIFFmalloc(oa); // add (uint8*) cast
1767 if (ob==0)
1768 {
1769 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1770 return(0);
1771 }
1772 *(uint32*)ob=oa;
1773 ob[sizeof(uint32)]=255;
1774 ob[sizeof(uint32)+1]=JPEG_MARKER_DQT;
1775 ob[sizeof(uint32)+2]=0;
1776 ob[sizeof(uint32)+3]=67;
1777 ob[sizeof(uint32)+4]=m;
1778 TIFFSeekFile(tif,sp->qtable_offset[m],SEEK_SET);
1779 p=(uint32)TIFFReadFile(tif,&ob[sizeof(uint32)+5],64);
1780 if (p!=64)
1781 return(0);
1782 sp->qtable[m]=ob;
1783 sp->sof_tq[m]=m;
1784 }
1785 else
1786 sp->sof_tq[m]=sp->sof_tq[m-1];
1787 }
1788 return(1);
1789 }
1790
1791 static int
1792 OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif)
1793 {
1794 static const char module[]="OJPEGReadHeaderInfoSecTablesDcTable";
1795 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1796 uint8 m;
1797 uint8 n;
1798 uint8 o[16];
1799 uint32 p;
1800 uint32 q;
1801 uint32 ra;
1802 uint8* rb;
1803 if (sp->dctable_offset[0]==0)
1804 {
1805 TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
1806 return(0);
1807 }
1808 sp->in_buffer_file_pos_log=0;
1809 for (m=0; m<sp->samples_per_pixel; m++)
1810 {
1811 if ((sp->dctable_offset[m]!=0) && ((m==0) || (sp->dctable_offset [m]!=sp->dctable_offset[m-1])))
1812 {
1813 for (n=0; n<m-1; n++)
1814 {
1815 if (sp->dctable_offset[m]==sp->dctable_offset[n] )
1816 {
1817 TIFFErrorExt(tif->tif_clientdata,module, "Corrupt JpegDcTables tag value");
1818 return(0);
1819 }
1820 }
1821 TIFFSeekFile(tif,sp->dctable_offset[m],SEEK_SET);
1822 p=(uint32)TIFFReadFile(tif,o,16);
1823 if (p!=16)
1824 return(0);
1825 q=0;
1826 for (n=0; n<16; n++)
1827 q+=o[n];
1828 ra=sizeof(uint32)+21+q;
1829 rb = (uint8*)_TIFFmalloc(ra); // add (uint8*) cast
1830 if (rb==0)
1831 {
1832 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1833 return(0);
1834 }
1835 *(uint32*)rb=ra;
1836 rb[sizeof(uint32)]=255;
1837 rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
1838 rb[sizeof(uint32)+2]=((19+q)>>8);
1839 rb[sizeof(uint32)+3]=((19+q)&255);
1840 rb[sizeof(uint32)+4]=m;
1841 for (n=0; n<16; n++)
1842 rb[sizeof(uint32)+5+n]=o[n];
1843 p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
1844 if (p!=q)
1845 return(0);
1846 sp->dctable[m]=rb;
1847 sp->sos_tda[m]=(m<<4);
1848 }
1849 else
1850 sp->sos_tda[m]=sp->sos_tda[m-1];
1851 }
1852 return(1);
1853 }
1854
1855 static int
1856 OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif)
1857 {
1858 static const char module[]="OJPEGReadHeaderInfoSecTablesAcTable";
1859 OJPEGState* sp=(OJPEGState*)tif->tif_data;
1860 uint8 m;
1861 uint8 n;
1862 uint8 o[16];
1863 uint32 p;
1864 uint32 q;
1865 uint32 ra;
1866 uint8* rb;
1867 if (sp->actable_offset[0]==0)
1868 {
1869 TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
1870 return(0);
1871 }
1872 sp->in_buffer_file_pos_log=0;
1873 for (m=0; m<sp->samples_per_pixel; m++)
1874 {
1875 if ((sp->actable_offset[m]!=0) && ((m==0) || (sp->actable_offset [m]!=sp->actable_offset[m-1])))
1876 {
1877 for (n=0; n<m-1; n++)
1878 {
1879 if (sp->actable_offset[m]==sp->actable_offset[n] )
1880 {
1881 TIFFErrorExt(tif->tif_clientdata,module, "Corrupt JpegAcTables tag value");
1882 return(0);
1883 }
1884 }
1885 TIFFSeekFile(tif,sp->actable_offset[m],SEEK_SET);
1886 p=(uint32)TIFFReadFile(tif,o,16);
1887 if (p!=16)
1888 return(0);
1889 q=0;
1890 for (n=0; n<16; n++)
1891 q+=o[n];
1892 ra=sizeof(uint32)+21+q;
1893 rb = (uint8*)_TIFFmalloc(ra); // add (uint8*) cast
1894 if (rb==0)
1895 {
1896 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1897 return(0);
1898 }
1899 *(uint32*)rb=ra;
1900 rb[sizeof(uint32)]=255;
1901 rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
1902 rb[sizeof(uint32)+2]=((19+q)>>8);
1903 rb[sizeof(uint32)+3]=((19+q)&255);
1904 rb[sizeof(uint32)+4]=(16|m);
1905 for (n=0; n<16; n++)
1906 rb[sizeof(uint32)+5+n]=o[n];
1907 p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
1908 if (p!=q)
1909 return(0);
1910 sp->actable[m]=rb;
1911 sp->sos_tda[m]=(sp->sos_tda[m]|m);
1912 }
1913 else
1914 sp->sos_tda[m]=(sp->sos_tda[m]|(sp->sos_tda[m-1]&15));
1915 }
1916 return(1);
1917 }
1918
1919 static int
1920 OJPEGReadBufferFill(OJPEGState* sp)
1921 {
1922 uint16 m;
1923 tmsize_t n;
1924 /* TODO: double-check: when subsamplingcorrect is set, no call to TIFFEr rorExt or TIFFWarningExt should be made
1925 * in any other case, seek or read errors should be passed through */
1926 do
1927 {
1928 if (sp->in_buffer_file_togo!=0)
1929 {
1930 if (sp->in_buffer_file_pos_log==0)
1931 {
1932 TIFFSeekFile(sp->tif,sp->in_buffer_file_pos,SEEK _SET);
1933 sp->in_buffer_file_pos_log=1;
1934 }
1935 m=OJPEG_BUFFER;
1936 if ((uint64)m>sp->in_buffer_file_togo)
1937 m=(uint16)sp->in_buffer_file_togo;
1938 n=TIFFReadFile(sp->tif,sp->in_buffer,(tmsize_t)m);
1939 if (n==0)
1940 return(0);
1941 assert(n>0);
1942 assert(n<=OJPEG_BUFFER);
1943 assert(n<65536);
1944 assert((uint64)n<=sp->in_buffer_file_togo);
1945 m=(uint16)n;
1946 sp->in_buffer_togo=m;
1947 sp->in_buffer_cur=sp->in_buffer;
1948 sp->in_buffer_file_togo-=m;
1949 sp->in_buffer_file_pos+=m;
1950 break;
1951 }
1952 sp->in_buffer_file_pos_log=0;
1953 switch(sp->in_buffer_source)
1954 {
1955 case osibsNotSetYet:
1956 if (sp->jpeg_interchange_format!=0)
1957 {
1958 sp->in_buffer_file_pos=sp->jpeg_intercha nge_format;
1959 sp->in_buffer_file_togo=sp->jpeg_interch ange_format_length;
1960 }
1961 sp->in_buffer_source=osibsJpegInterchangeFormat;
1962 break;
1963 case osibsJpegInterchangeFormat:
1964 sp->in_buffer_source=osibsStrile;
1965 case osibsStrile:
1966 if (!_TIFFFillStriles( sp->tif )
1967 || sp->tif->tif_dir.td_stripoffset == NULL
1968 || sp->tif->tif_dir.td_stripbytecount == NUL L)
1969 return 0;
1970
1971 if (sp->in_buffer_next_strile==sp->in_buffer_str ile_count)
1972 sp->in_buffer_source=osibsEof;
1973 else
1974 {
1975 sp->in_buffer_file_pos=sp->tif->tif_dir. td_stripoffset[sp->in_buffer_next_strile];
1976 if (sp->in_buffer_file_pos!=0)
1977 {
1978 if (sp->in_buffer_file_pos>=sp-> file_size)
1979 sp->in_buffer_file_pos=0 ;
1980 else if (sp->tif->tif_dir.td_str ipbytecount==NULL)
1981 sp->in_buffer_file_togo= sp->file_size-sp->in_buffer_file_pos;
1982 else
1983 {
1984 if (sp->tif->tif_dir.td_ stripbytecount == 0) {
1985 TIFFErrorExt(sp- >tif->tif_clientdata,sp->tif->tif_name,"Strip byte counts are missing");
1986 return(0);
1987 }
1988 sp->in_buffer_file_togo= sp->tif->tif_dir.td_stripbytecount[sp->in_buffer_next_strile];
1989 if (sp->in_buffer_file_t ogo==0)
1990 sp->in_buffer_fi le_pos=0;
1991 else if (sp->in_buffer_f ile_pos+sp->in_buffer_file_togo>sp->file_size)
1992 sp->in_buffer_fi le_togo=sp->file_size-sp->in_buffer_file_pos;
1993 }
1994 }
1995 sp->in_buffer_next_strile++;
1996 }
1997 break;
1998 default:
1999 return(0);
2000 }
2001 } while (1);
2002 return(1);
2003 }
2004
2005 static int
2006 OJPEGReadByte(OJPEGState* sp, uint8* byte)
2007 {
2008 if (sp->in_buffer_togo==0)
2009 {
2010 if (OJPEGReadBufferFill(sp)==0)
2011 return(0);
2012 assert(sp->in_buffer_togo>0);
2013 }
2014 *byte=*(sp->in_buffer_cur);
2015 sp->in_buffer_cur++;
2016 sp->in_buffer_togo--;
2017 return(1);
2018 }
2019
2020 static int
2021 OJPEGReadBytePeek(OJPEGState* sp, uint8* byte)
2022 {
2023 if (sp->in_buffer_togo==0)
2024 {
2025 if (OJPEGReadBufferFill(sp)==0)
2026 return(0);
2027 assert(sp->in_buffer_togo>0);
2028 }
2029 *byte=*(sp->in_buffer_cur);
2030 return(1);
2031 }
2032
2033 static void
2034 OJPEGReadByteAdvance(OJPEGState* sp)
2035 {
2036 assert(sp->in_buffer_togo>0);
2037 sp->in_buffer_cur++;
2038 sp->in_buffer_togo--;
2039 }
2040
2041 static int
2042 OJPEGReadWord(OJPEGState* sp, uint16* word)
2043 {
2044 uint8 m;
2045 if (OJPEGReadByte(sp,&m)==0)
2046 return(0);
2047 *word=(m<<8);
2048 if (OJPEGReadByte(sp,&m)==0)
2049 return(0);
2050 *word|=m;
2051 return(1);
2052 }
2053
2054 static int
2055 OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem)
2056 {
2057 uint16 mlen;
2058 uint8* mmem;
2059 uint16 n;
2060 assert(len>0);
2061 mlen=len;
2062 mmem = (uint8*)mem; // add (uint8*) cast
2063 do
2064 {
2065 if (sp->in_buffer_togo==0)
2066 {
2067 if (OJPEGReadBufferFill(sp)==0)
2068 return(0);
2069 assert(sp->in_buffer_togo>0);
2070 }
2071 n=mlen;
2072 if (n>sp->in_buffer_togo)
2073 n=sp->in_buffer_togo;
2074 _TIFFmemcpy(mmem,sp->in_buffer_cur,n);
2075 sp->in_buffer_cur+=n;
2076 sp->in_buffer_togo-=n;
2077 mlen-=n;
2078 mmem+=n;
2079 } while(mlen>0);
2080 return(1);
2081 }
2082
2083 static void
2084 OJPEGReadSkip(OJPEGState* sp, uint16 len)
2085 {
2086 uint16 m;
2087 uint16 n;
2088 m=len;
2089 n=m;
2090 if (n>sp->in_buffer_togo)
2091 n=sp->in_buffer_togo;
2092 sp->in_buffer_cur+=n;
2093 sp->in_buffer_togo-=n;
2094 m-=n;
2095 if (m>0)
2096 {
2097 assert(sp->in_buffer_togo==0);
2098 n=m;
2099 if ((uint64)n>sp->in_buffer_file_togo)
2100 n=(uint16)sp->in_buffer_file_togo;
2101 sp->in_buffer_file_pos+=n;
2102 sp->in_buffer_file_togo-=n;
2103 sp->in_buffer_file_pos_log=0;
2104 /* we don't skip past jpeginterchangeformat/strile block...
2105 * if that is asked from us, we're dealing with totally bazurk
2106 * data anyway, and we've not seen this happening on any
2107 * testfile, so we might as well likely cause some other
2108 * meaningless error to be passed at some later time
2109 */
2110 }
2111 }
2112
2113 static int
2114 OJPEGWriteStream(TIFF* tif, void** mem, uint32* len)
2115 {
2116 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2117 *len=0;
2118 do
2119 {
2120 assert(sp->out_state<=ososEoi);
2121 switch(sp->out_state)
2122 {
2123 case ososSoi:
2124 OJPEGWriteStreamSoi(tif,mem,len);
2125 break;
2126 case ososQTable0:
2127 OJPEGWriteStreamQTable(tif,0,mem,len);
2128 break;
2129 case ososQTable1:
2130 OJPEGWriteStreamQTable(tif,1,mem,len);
2131 break;
2132 case ososQTable2:
2133 OJPEGWriteStreamQTable(tif,2,mem,len);
2134 break;
2135 case ososQTable3:
2136 OJPEGWriteStreamQTable(tif,3,mem,len);
2137 break;
2138 case ososDcTable0:
2139 OJPEGWriteStreamDcTable(tif,0,mem,len);
2140 break;
2141 case ososDcTable1:
2142 OJPEGWriteStreamDcTable(tif,1,mem,len);
2143 break;
2144 case ososDcTable2:
2145 OJPEGWriteStreamDcTable(tif,2,mem,len);
2146 break;
2147 case ososDcTable3:
2148 OJPEGWriteStreamDcTable(tif,3,mem,len);
2149 break;
2150 case ososAcTable0:
2151 OJPEGWriteStreamAcTable(tif,0,mem,len);
2152 break;
2153 case ososAcTable1:
2154 OJPEGWriteStreamAcTable(tif,1,mem,len);
2155 break;
2156 case ososAcTable2:
2157 OJPEGWriteStreamAcTable(tif,2,mem,len);
2158 break;
2159 case ososAcTable3:
2160 OJPEGWriteStreamAcTable(tif,3,mem,len);
2161 break;
2162 case ososDri:
2163 OJPEGWriteStreamDri(tif,mem,len);
2164 break;
2165 case ososSof:
2166 OJPEGWriteStreamSof(tif,mem,len);
2167 break;
2168 case ososSos:
2169 OJPEGWriteStreamSos(tif,mem,len);
2170 break;
2171 case ososCompressed:
2172 if (OJPEGWriteStreamCompressed(tif,mem,len)==0)
2173 return(0);
2174 break;
2175 case ososRst:
2176 OJPEGWriteStreamRst(tif,mem,len);
2177 break;
2178 case ososEoi:
2179 OJPEGWriteStreamEoi(tif,mem,len);
2180 break;
2181 }
2182 } while (*len==0);
2183 return(1);
2184 }
2185
2186 static void
2187 OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len)
2188 {
2189 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2190 assert(OJPEG_BUFFER>=2);
2191 sp->out_buffer[0]=255;
2192 sp->out_buffer[1]=JPEG_MARKER_SOI;
2193 *len=2;
2194 *mem=(void*)sp->out_buffer;
2195 sp->out_state++;
2196 }
2197
2198 static void
2199 OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
2200 {
2201 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2202 if (sp->qtable[table_index]!=0)
2203 {
2204 *mem=(void*)(sp->qtable[table_index]+sizeof(uint32));
2205 *len=*((uint32*)sp->qtable[table_index])-sizeof(uint32);
2206 }
2207 sp->out_state++;
2208 }
2209
2210 static void
2211 OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
2212 {
2213 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2214 if (sp->dctable[table_index]!=0)
2215 {
2216 *mem=(void*)(sp->dctable[table_index]+sizeof(uint32));
2217 *len=*((uint32*)sp->dctable[table_index])-sizeof(uint32);
2218 }
2219 sp->out_state++;
2220 }
2221
2222 static void
2223 OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
2224 {
2225 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2226 if (sp->actable[table_index]!=0)
2227 {
2228 *mem=(void*)(sp->actable[table_index]+sizeof(uint32));
2229 *len=*((uint32*)sp->actable[table_index])-sizeof(uint32);
2230 }
2231 sp->out_state++;
2232 }
2233
2234 static void
2235 OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len)
2236 {
2237 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2238 assert(OJPEG_BUFFER>=6);
2239 if (sp->restart_interval!=0)
2240 {
2241 sp->out_buffer[0]=255;
2242 sp->out_buffer[1]=JPEG_MARKER_DRI;
2243 sp->out_buffer[2]=0;
2244 sp->out_buffer[3]=4;
2245 sp->out_buffer[4]=(sp->restart_interval>>8);
2246 sp->out_buffer[5]=(sp->restart_interval&255);
2247 *len=6;
2248 *mem=(void*)sp->out_buffer;
2249 }
2250 sp->out_state++;
2251 }
2252
2253 static void
2254 OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len)
2255 {
2256 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2257 uint8 m;
2258 assert(OJPEG_BUFFER>=2+8+sp->samples_per_pixel_per_plane*3);
2259 assert(255>=8+sp->samples_per_pixel_per_plane*3);
2260 sp->out_buffer[0]=255;
2261 sp->out_buffer[1]=sp->sof_marker_id;
2262 /* Lf */
2263 sp->out_buffer[2]=0;
2264 sp->out_buffer[3]=8+sp->samples_per_pixel_per_plane*3;
2265 /* P */
2266 sp->out_buffer[4]=8;
2267 /* Y */
2268 sp->out_buffer[5]=(sp->sof_y>>8);
2269 sp->out_buffer[6]=(sp->sof_y&255);
2270 /* X */
2271 sp->out_buffer[7]=(sp->sof_x>>8);
2272 sp->out_buffer[8]=(sp->sof_x&255);
2273 /* Nf */
2274 sp->out_buffer[9]=sp->samples_per_pixel_per_plane;
2275 for (m=0; m<sp->samples_per_pixel_per_plane; m++)
2276 {
2277 /* C */
2278 sp->out_buffer[10+m*3]=sp->sof_c[sp->plane_sample_offset+m];
2279 /* H and V */
2280 sp->out_buffer[10+m*3+1]=sp->sof_hv[sp->plane_sample_offset+m];
2281 /* Tq */
2282 sp->out_buffer[10+m*3+2]=sp->sof_tq[sp->plane_sample_offset+m];
2283 }
2284 *len=10+sp->samples_per_pixel_per_plane*3;
2285 *mem=(void*)sp->out_buffer;
2286 sp->out_state++;
2287 }
2288
2289 static void
2290 OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len)
2291 {
2292 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2293 uint8 m;
2294 assert(OJPEG_BUFFER>=2+6+sp->samples_per_pixel_per_plane*2);
2295 assert(255>=6+sp->samples_per_pixel_per_plane*2);
2296 sp->out_buffer[0]=255;
2297 sp->out_buffer[1]=JPEG_MARKER_SOS;
2298 /* Ls */
2299 sp->out_buffer[2]=0;
2300 sp->out_buffer[3]=6+sp->samples_per_pixel_per_plane*2;
2301 /* Ns */
2302 sp->out_buffer[4]=sp->samples_per_pixel_per_plane;
2303 for (m=0; m<sp->samples_per_pixel_per_plane; m++)
2304 {
2305 /* Cs */
2306 sp->out_buffer[5+m*2]=sp->sos_cs[sp->plane_sample_offset+m];
2307 /* Td and Ta */
2308 sp->out_buffer[5+m*2+1]=sp->sos_tda[sp->plane_sample_offset+m];
2309 }
2310 /* Ss */
2311 sp->out_buffer[5+sp->samples_per_pixel_per_plane*2]=0;
2312 /* Se */
2313 sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+1]=63;
2314 /* Ah and Al */
2315 sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+2]=0;
2316 *len=8+sp->samples_per_pixel_per_plane*2;
2317 *mem=(void*)sp->out_buffer;
2318 sp->out_state++;
2319 }
2320
2321 static int
2322 OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len)
2323 {
2324 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2325 if (sp->in_buffer_togo==0)
2326 {
2327 if (OJPEGReadBufferFill(sp)==0)
2328 return(0);
2329 assert(sp->in_buffer_togo>0);
2330 }
2331 *len=sp->in_buffer_togo;
2332 *mem=(void*)sp->in_buffer_cur;
2333 sp->in_buffer_togo=0;
2334 if (sp->in_buffer_file_togo==0)
2335 {
2336 switch(sp->in_buffer_source)
2337 {
2338 case osibsStrile:
2339 if (sp->in_buffer_next_strile<sp->in_buffer_stri le_count)
2340 sp->out_state=ososRst;
2341 else
2342 sp->out_state=ososEoi;
2343 break;
2344 case osibsEof:
2345 sp->out_state=ososEoi;
2346 break;
2347 default:
2348 break;
2349 }
2350 }
2351 return(1);
2352 }
2353
2354 static void
2355 OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len)
2356 {
2357 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2358 assert(OJPEG_BUFFER>=2);
2359 sp->out_buffer[0]=255;
2360 sp->out_buffer[1]=JPEG_MARKER_RST0+sp->restart_index;
2361 sp->restart_index++;
2362 if (sp->restart_index==8)
2363 sp->restart_index=0;
2364 *len=2;
2365 *mem=(void*)sp->out_buffer;
2366 sp->out_state=ososCompressed;
2367 }
2368
2369 static void
2370 OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len)
2371 {
2372 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2373 assert(OJPEG_BUFFER>=2);
2374 sp->out_buffer[0]=255;
2375 sp->out_buffer[1]=JPEG_MARKER_EOI;
2376 *len=2;
2377 *mem=(void*)sp->out_buffer;
2378 }
2379
2380 #ifndef LIBJPEG_ENCAP_EXTERNAL
2381 static int
2382 jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
2383 {
2384 return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_create_decompress(cinfo),1));
2385 }
2386 #endif
2387
2388 #ifndef LIBJPEG_ENCAP_EXTERNAL
2389 static int
2390 jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 requ ire_image)
2391 {
2392 return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_read_header(cinfo,require_image), 1));
2393 }
2394 #endif
2395
2396 #ifndef LIBJPEG_ENCAP_EXTERNAL
2397 static int
2398 jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
2399 {
2400 return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_start_decompress(cinfo),1));
2401 }
2402 #endif
2403
2404 #ifndef LIBJPEG_ENCAP_EXTERNAL
2405 static int
2406 jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* s canlines, uint32 max_lines)
2407 {
2408 return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_read_scanlines(cinfo,scanlines,ma x_lines),1));
2409 }
2410 #endif
2411
2412 #ifndef LIBJPEG_ENCAP_EXTERNAL
2413 static int
2414 jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* da ta, uint32 max_lines)
2415 {
2416 return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_read_raw_data(cinfo,data,max_line s),1));
2417 }
2418 #endif
2419
2420 #ifndef LIBJPEG_ENCAP_EXTERNAL
2421 static void
2422 jpeg_encap_unwind(TIFF* tif)
2423 {
2424 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2425 LONGJMP(sp->exit_jmpbuf,1);
2426 }
2427 #endif
2428
2429 static void
2430 OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo)
2431 {
2432 char buffer[JMSG_LENGTH_MAX];
2433 (*cinfo->err->format_message)(cinfo,buffer);
2434 TIFFWarningExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg"," %s",buffer);
2435 }
2436
2437 static void
2438 OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo)
2439 {
2440 char buffer[JMSG_LENGTH_MAX];
2441 (*cinfo->err->format_message)(cinfo,buffer);
2442 TIFFErrorExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg","%s ",buffer);
2443 jpeg_encap_unwind((TIFF*)(cinfo->client_data));
2444 }
2445
2446 static void
2447 OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo)
2448 {
2449 (void)cinfo;
2450 }
2451
2452 static boolean
2453 OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo)
2454 {
2455 TIFF* tif=(TIFF*)cinfo->client_data;
2456 OJPEGState* sp=(OJPEGState*)tif->tif_data;
2457 void* mem=0;
2458 uint32 len=0U;
2459 if (OJPEGWriteStream(tif,&mem,&len)==0)
2460 {
2461 TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Premature end of JPE G data");
2462 jpeg_encap_unwind(tif);
2463 }
2464 sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = len;
2465 sp->libjpeg_jpeg_source_mgr.next_input_byte = (const JOCTET * )mem; // add (const JOCTET * ) cast
2466 return(1);
2467 }
2468
2469 static void
2470 OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_b ytes)
2471 {
2472 TIFF* tif=(TIFF*)cinfo->client_data;
2473 (void)num_bytes;
2474 TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
2475 jpeg_encap_unwind(tif);
2476 }
2477
2478 static boolean
2479 OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desi red)
2480 {
2481 TIFF* tif=(TIFF*)cinfo->client_data;
2482 (void)desired;
2483 TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
2484 jpeg_encap_unwind(tif);
2485 return(0);
2486 }
2487
2488 static void
2489 OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo)
2490 {
2491 (void)cinfo;
2492 }
2493
2494 #endif
2495
2496
2497 /*
2498 * Local Variables:
2499 * mode: c
2500 * c-basic-offset: 8
2501 * fill-column: 78
2502 * End:
2503 */
2504
OLDNEW
« no previous file with comments | « third_party/tiff_v403/tif_next.c ('k') | third_party/tiff_v403/tif_open.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698