1 | #include "tiffiop.h" |
---|
2 | #ifdef OJPEG_SUPPORT |
---|
3 | |
---|
4 | /* JPEG Compression support, as per the original TIFF 6.0 specification. |
---|
5 | |
---|
6 | WARNING: KLUDGE ALERT! The type of JPEG encapsulation defined by the TIFF |
---|
7 | Version 6.0 specification is now totally obsolete and |
---|
8 | deprecated for new applications and images. This file is an unsupported hack |
---|
9 | that was created solely in order to read (but NOT write!) a few old, |
---|
10 | unconverted images still present on some users' computer systems. The code |
---|
11 | isn't pretty or robust, and it won't read every "old format" JPEG-in-TIFF |
---|
12 | file (see Samuel Leffler's draft "TIFF Technical Note No. 2" for a long and |
---|
13 | incomplete list of known problems), but it seems to work well enough in the |
---|
14 | few cases of practical interest to the author; so, "caveat emptor"! This |
---|
15 | file should NEVER be enhanced to write new images using anything other than |
---|
16 | the latest approved JPEG-in-TIFF encapsulation method, implemented by the |
---|
17 | "tif_jpeg.c" file elsewhere in this library. |
---|
18 | |
---|
19 | This file interfaces with Release 6B of the JPEG Library written by theu |
---|
20 | Independent JPEG Group, which you can find on the Internet at: |
---|
21 | ftp.uu.net:/graphics/jpeg/. |
---|
22 | |
---|
23 | Contributed by Scott Marovich <marovich@hpl.hp.com> with considerable help |
---|
24 | from Charles Auer <Bumble731@msn.com> to unravel the mysteries of image files |
---|
25 | created by Microsoft's Wang Imaging application. |
---|
26 | */ |
---|
27 | #include <setjmp.h> |
---|
28 | #include <stdio.h> |
---|
29 | #ifdef FAR |
---|
30 | #undef FAR /* Undefine FAR to avoid conflict with JPEG definition */ |
---|
31 | #endif |
---|
32 | #define JPEG_INTERNALS /* Include "jpegint.h" for "DSTATE_*" symbols */ |
---|
33 | #undef INLINE |
---|
34 | #include "jpeglib.h" |
---|
35 | #undef JPEG_INTERNALS |
---|
36 | |
---|
37 | /* Hack for Microsoft's Wang Imaging for Windows output files */ |
---|
38 | extern void jpeg_reset_huff_decode(j_decompress_ptr,float *); |
---|
39 | |
---|
40 | /* On some machines, it may be worthwhile to use "_setjmp()" or "sigsetjmp()" |
---|
41 | instead of "setjmp()". These macros make it easier: |
---|
42 | */ |
---|
43 | #define SETJMP(jbuf)setjmp(jbuf) |
---|
44 | #define LONGJMP(jbuf,code)longjmp(jbuf,code) |
---|
45 | #define JMP_BUF jmp_buf |
---|
46 | |
---|
47 | #define TIFFTAG_WANG_PAGECONTROL 32934 |
---|
48 | |
---|
49 | /* Bit-vector offsets for keeping track of TIFF records that we've parsed. */ |
---|
50 | |
---|
51 | #define FIELD_JPEGPROC FIELD_CODEC |
---|
52 | #define FIELD_JPEGIFOFFSET (FIELD_CODEC+1) |
---|
53 | #define FIELD_JPEGIFBYTECOUNT (FIELD_CODEC+2) |
---|
54 | #define FIELD_JPEGRESTARTINTERVAL (FIELD_CODEC+3) |
---|
55 | #define FIELD_JPEGTABLES (FIELD_CODEC+4) /* New, post-6.0 JPEG-in-TIFF tag! */ |
---|
56 | #define FIELD_JPEGLOSSLESSPREDICTORS (FIELD_CODEC+5) |
---|
57 | #define FIELD_JPEGPOINTTRANSFORM (FIELD_CODEC+6) |
---|
58 | #define FIELD_JPEGQTABLES (FIELD_CODEC+7) |
---|
59 | #define FIELD_JPEGDCTABLES (FIELD_CODEC+8) |
---|
60 | #define FIELD_JPEGACTABLES (FIELD_CODEC+9) |
---|
61 | #define FIELD_WANG_PAGECONTROL (FIELD_CODEC+10) |
---|
62 | #define FIELD_JPEGCOLORMODE (FIELD_CODEC+11) |
---|
63 | |
---|
64 | typedef struct jpeg_destination_mgr jpeg_destination_mgr; |
---|
65 | typedef struct jpeg_source_mgr jpeg_source_mgr; |
---|
66 | typedef struct jpeg_error_mgr jpeg_error_mgr; |
---|
67 | |
---|
68 | /* State variable for each open TIFF file that uses "libjpeg" for JPEG |
---|
69 | decompression. (Note: This file should NEVER perform JPEG compression |
---|
70 | except in the manner implemented by the "tif_jpeg.c" file, elsewhere in this |
---|
71 | library; see comments above.) JPEG Library internal state is recorded in a |
---|
72 | "jpeg_{de}compress_struct", while a "jpeg_common_struct" records a few items |
---|
73 | common to both compression and expansion. The "cinfo" field containing JPEG |
---|
74 | Library state MUST be the 1st member of our own state variable, so that we |
---|
75 | can safely "cast" pointers back and forth. |
---|
76 | */ |
---|
77 | typedef struct /* This module's private, per-image state variable */ |
---|
78 | { |
---|
79 | union /* JPEG Library state variable; this MUST be our 1st field! */ |
---|
80 | { |
---|
81 | /* struct jpeg_compress_struct c; */ |
---|
82 | struct jpeg_decompress_struct d; |
---|
83 | struct jpeg_common_struct comm; |
---|
84 | } cinfo; |
---|
85 | jpeg_error_mgr err; /* JPEG Library error manager */ |
---|
86 | JMP_BUF exit_jmpbuf; /* ...for catching JPEG Library failures */ |
---|
87 | # ifdef never |
---|
88 | |
---|
89 | /* (The following two fields could be a "union", but they're small enough that |
---|
90 | it's not worth the effort.) |
---|
91 | */ |
---|
92 | jpeg_destination_mgr dest; /* Destination for compressed data */ |
---|
93 | # endif |
---|
94 | jpeg_source_mgr src; /* Source of expanded data */ |
---|
95 | JSAMPARRAY ds_buffer[MAX_COMPONENTS]; /* ->Temporary downsampling buffers */ |
---|
96 | TIFF *tif; /* Reverse pointer, needed by some code */ |
---|
97 | TIFFVGetMethod vgetparent; /* "Super class" methods... */ |
---|
98 | TIFFVSetMethod vsetparent; |
---|
99 | TIFFStripMethod defsparent; |
---|
100 | TIFFTileMethod deftparent; |
---|
101 | void *jpegtables; /* ->"New" JPEG tables, if we synthesized any */ |
---|
102 | uint32 is_WANG, /* <=> Microsoft Wang Imaging for Windows output file? */ |
---|
103 | jpegtables_length; /* Length of "new" JPEG tables, if they exist */ |
---|
104 | tsize_t bytesperline; /* No. of decompressed Bytes per scan line */ |
---|
105 | int jpegquality, /* Compression quality level */ |
---|
106 | jpegtablesmode, /* What to put in JPEGTables */ |
---|
107 | samplesperclump, |
---|
108 | scancount; /* No. of scan lines accumulated */ |
---|
109 | uint16 h_sampling, /* Luminance sampling factors */ |
---|
110 | v_sampling, |
---|
111 | photometric; /* Copy of "PhotometricInterpretation" tag value */ |
---|
112 | u_char jpegcolormode; /* Who performs RGB <-> YCbCr conversion? */ |
---|
113 | /* JPEGCOLORMODE_RAW <=> TIFF Library does conversion */ |
---|
114 | /* JPEGCOLORMODE_RGB <=> JPEG Library does conversion */ |
---|
115 | } OJPEGState; |
---|
116 | #define OJState(tif)((OJPEGState*)(tif)->tif_data) |
---|
117 | |
---|
118 | static const TIFFFieldInfo ojpegFieldInfo[]=/* JPEG-specific TIFF-record tags */ |
---|
119 | { |
---|
120 | |
---|
121 | /* This is the current JPEG-in-TIFF metadata-encapsulation tag, and its |
---|
122 | treatment in this file is idiosyncratic. It should never appear in a |
---|
123 | "source" image conforming to the TIFF Version 6.0 specification, so we |
---|
124 | arrange to report an error if it appears. But in order to support possible |
---|
125 | future conversion of "old" JPEG-in-TIFF encapsulations to "new" ones, we |
---|
126 | might wish to synthesize an equivalent value to be returned by the TIFF |
---|
127 | Library's "getfield" method. So, this table tells the TIFF Library to pass |
---|
128 | these records to us in order to filter them below. |
---|
129 | */ |
---|
130 | { |
---|
131 | TIFFTAG_JPEGTABLES ,TIFF_VARIABLE,TIFF_VARIABLE, |
---|
132 | TIFF_UNDEFINED,FIELD_JPEGTABLES ,FALSE,TRUE ,"JPEGTables" |
---|
133 | }, |
---|
134 | |
---|
135 | /* These tags are defined by the TIFF Version 6.0 specification and are now |
---|
136 | obsolete. This module reads them from an old "source" image, but it never |
---|
137 | writes them to a new "destination" image. |
---|
138 | */ |
---|
139 | { |
---|
140 | TIFFTAG_JPEGPROC ,1 ,1 , |
---|
141 | TIFF_SHORT ,FIELD_JPEGPROC ,FALSE,FALSE,"JPEGProc" |
---|
142 | }, |
---|
143 | { |
---|
144 | TIFFTAG_JPEGIFOFFSET ,1 ,1 , |
---|
145 | TIFF_LONG ,FIELD_JPEGIFOFFSET ,FALSE,FALSE,"JPEGInterchangeFormat" |
---|
146 | }, |
---|
147 | { |
---|
148 | TIFFTAG_JPEGIFBYTECOUNT ,1 ,1 , |
---|
149 | TIFF_LONG ,FIELD_JPEGIFBYTECOUNT ,FALSE,FALSE,"JPEGInterchangeFormatLength" |
---|
150 | }, |
---|
151 | { |
---|
152 | TIFFTAG_JPEGRESTARTINTERVAL ,1 ,1 , |
---|
153 | TIFF_SHORT ,FIELD_JPEGRESTARTINTERVAL ,FALSE,FALSE,"JPEGRestartInterval" |
---|
154 | }, |
---|
155 | { |
---|
156 | TIFFTAG_JPEGLOSSLESSPREDICTORS,TIFF_VARIABLE,TIFF_VARIABLE, |
---|
157 | TIFF_SHORT ,FIELD_JPEGLOSSLESSPREDICTORS,FALSE,TRUE ,"JPEGLosslessPredictors" |
---|
158 | }, |
---|
159 | { |
---|
160 | TIFFTAG_JPEGPOINTTRANSFORM ,TIFF_VARIABLE,TIFF_VARIABLE, |
---|
161 | TIFF_SHORT ,FIELD_JPEGPOINTTRANSFORM ,FALSE,TRUE ,"JPEGPointTransforms" |
---|
162 | }, |
---|
163 | { |
---|
164 | TIFFTAG_JPEGQTABLES ,TIFF_VARIABLE,TIFF_VARIABLE, |
---|
165 | TIFF_LONG ,FIELD_JPEGQTABLES ,FALSE,TRUE ,"JPEGQTables" |
---|
166 | }, |
---|
167 | { |
---|
168 | TIFFTAG_JPEGDCTABLES ,TIFF_VARIABLE,TIFF_VARIABLE, |
---|
169 | TIFF_LONG ,FIELD_JPEGDCTABLES ,FALSE,TRUE ,"JPEGDCTables" |
---|
170 | }, |
---|
171 | { |
---|
172 | TIFFTAG_JPEGACTABLES ,TIFF_VARIABLE,TIFF_VARIABLE, |
---|
173 | TIFF_LONG ,FIELD_JPEGACTABLES ,FALSE,TRUE ,"JPEGACTables" |
---|
174 | }, |
---|
175 | { |
---|
176 | TIFFTAG_WANG_PAGECONTROL ,TIFF_VARIABLE,1 , |
---|
177 | TIFF_LONG ,FIELD_WANG_PAGECONTROL ,FALSE,FALSE,"WANG PageControl" |
---|
178 | }, |
---|
179 | |
---|
180 | /* This is a pseudo tag intended for internal use only by the TIFF Library and |
---|
181 | its clients, which should never appear in an input/output image file. It |
---|
182 | specifies whether the TIFF Library will perform YCbCr<->RGB color-space |
---|
183 | conversion (JPEGCOLORMODE_RAW <=> 0) or ask the JPEG Library to do it |
---|
184 | (JPEGCOLORMODE_RGB <=> 1). |
---|
185 | */ |
---|
186 | { |
---|
187 | TIFFTAG_JPEGCOLORMODE ,0 ,0 , |
---|
188 | TIFF_ANY ,FIELD_PSEUDO ,FALSE,FALSE,"JPEGColorMode" |
---|
189 | } |
---|
190 | }; |
---|
191 | static const char JPEGLib_name[]={"JPEG Library"}, |
---|
192 | bad_bps[]={"%u BitsPerSample not allowed for JPEG"}, |
---|
193 | # ifdef never |
---|
194 | no_write_frac[]={"fractional scan line discarded"}, |
---|
195 | # endif |
---|
196 | no_read_frac[]={"fractional scan line not read"}, |
---|
197 | no_jtable_space[]={"No space for JPEGTables"}; |
---|
198 | |
---|
199 | /* The following diagnostic subroutines interface with and replace default |
---|
200 | subroutines in the JPEG Library. Our basic strategy is to use "setjmp()"/ |
---|
201 | "longjmp()" in order to return control to the TIFF Library when the JPEG |
---|
202 | library detects an error, and to use TIFF Library subroutines for displaying |
---|
203 | diagnostic messages to a client application. |
---|
204 | */ |
---|
205 | static void |
---|
206 | TIFFojpeg_error_exit(register j_common_ptr cinfo) |
---|
207 | { char buffer[JMSG_LENGTH_MAX]; |
---|
208 | |
---|
209 | (*cinfo->err->format_message)(cinfo,buffer); |
---|
210 | TIFFError(JPEGLib_name,buffer); /* Display error message */ |
---|
211 | jpeg_abort(cinfo); /* Clean up JPEG Library state */ |
---|
212 | LONGJMP(((OJPEGState *)cinfo)->exit_jmpbuf,1); /* Return to TIFF client */ |
---|
213 | } |
---|
214 | |
---|
215 | static void |
---|
216 | TIFFojpeg_output_message(register j_common_ptr cinfo) |
---|
217 | { char buffer[JMSG_LENGTH_MAX]; |
---|
218 | |
---|
219 | /* This subroutine is invoked only for warning messages, since the JPEG |
---|
220 | Library's "error_exit" method does its own thing and "trace_level" is never |
---|
221 | set > 0. |
---|
222 | */ |
---|
223 | (*cinfo->err->format_message)(cinfo,buffer); |
---|
224 | TIFFWarning(JPEGLib_name,buffer); |
---|
225 | } |
---|
226 | |
---|
227 | /* The following subroutines, which also interface with the JPEG Library, exist |
---|
228 | mainly in limit the side effects of "setjmp()" and convert JPEG normal/error |
---|
229 | conditions into TIFF Library return codes. |
---|
230 | */ |
---|
231 | #define CALLJPEG(sp,fail,op)(SETJMP((sp)->exit_jmpbuf)?(fail):(op)) |
---|
232 | #define CALLVJPEG(sp,op)CALLJPEG(sp,0,((op),1)) |
---|
233 | #ifdef never |
---|
234 | |
---|
235 | static int |
---|
236 | TIFFojpeg_create_compress(register OJPEGState *sp) |
---|
237 | { |
---|
238 | sp->cinfo.c.err = jpeg_std_error(&sp->err); /* Initialize error handling */ |
---|
239 | sp->err.error_exit = TIFFojpeg_error_exit; |
---|
240 | sp->err.output_message = TIFFojpeg_output_message; |
---|
241 | return CALLVJPEG(sp,jpeg_create_compress(&sp->cinfo.c)); |
---|
242 | } |
---|
243 | |
---|
244 | static int |
---|
245 | TIFFojpeg_finish_compress(register OJPEGState *sp) |
---|
246 | {return CALLVJPEG(sp,jpeg_finish_compress(&sp->cinfo.c));} |
---|
247 | |
---|
248 | static int |
---|
249 | TIFFojpeg_set_colorspace(register OJPEGState *sp,J_COLOR_SPACE colorspace) |
---|
250 | {return CALLVJPEG(sp,jpeg_set_colorspace(&sp->cinfo.c,colorspace));} |
---|
251 | |
---|
252 | static int |
---|
253 | TIFFojpeg_set_defaults(register OJPEGState *sp) |
---|
254 | {return CALLVJPEG(sp,jpeg_set_defaults(&sp->cinfo.c));} |
---|
255 | |
---|
256 | static int |
---|
257 | TIFFojpeg_set_quality(register OJPEGState *sp,int quality,boolean force_baseline) |
---|
258 | {return CALLVJPEG(sp,jpeg_set_quality(&sp->cinfo.c,quality,force_baseline));} |
---|
259 | |
---|
260 | static int |
---|
261 | TIFFojpeg_start_compress(register OJPEGState *sp,boolean write_all_tables) |
---|
262 | {return CALLVJPEG(sp,jpeg_start_compress(&sp->cinfo.c,write_all_tables));} |
---|
263 | |
---|
264 | static int |
---|
265 | TIFFojpeg_suppress_tables(register OJPEGState *sp,boolean suppress) |
---|
266 | {return CALLVJPEG(sp,jpeg_suppress_tables(&sp->cinfo.c,suppress));} |
---|
267 | |
---|
268 | static int |
---|
269 | TIFFojpeg_write_raw_data(register OJPEGState *sp,JSAMPIMAGE data,int num_lines) |
---|
270 | { return |
---|
271 | CALLJPEG(sp,-1,(int)jpeg_write_raw_data(&sp->cinfo.c,data,(JDIMENSION)num_lines)); |
---|
272 | } |
---|
273 | |
---|
274 | static int |
---|
275 | TIFFojpeg_write_scanlines(register OJPEGState *sp,JSAMPARRAY scanlines, |
---|
276 | int num_lines) |
---|
277 | { return |
---|
278 | CALLJPEG(sp,-1,(int)jpeg_write_scanlines(&sp->cinfo.c,scanlines,(JDIMENSION)num_lines)); |
---|
279 | } |
---|
280 | |
---|
281 | static int |
---|
282 | TIFFojpeg_write_tables(register OJPEGState *sp) |
---|
283 | {return CALLVJPEG(sp,jpeg_write_tables(&sp->cinfo.c));} |
---|
284 | #else /* well, hardly ever */ |
---|
285 | |
---|
286 | static int |
---|
287 | _notSupported(register TIFF *tif) |
---|
288 | { const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression); |
---|
289 | |
---|
290 | TIFFError(tif->tif_name,"%s compression is not supported",c->name); |
---|
291 | return 0; |
---|
292 | } |
---|
293 | #endif /* never */ |
---|
294 | |
---|
295 | static int |
---|
296 | TIFFojpeg_abort(register OJPEGState *sp) |
---|
297 | {return CALLVJPEG(sp,jpeg_abort(&sp->cinfo.comm));} |
---|
298 | |
---|
299 | static JSAMPARRAY |
---|
300 | TIFFojpeg_alloc_sarray(register OJPEGState *sp,int pool_id, |
---|
301 | JDIMENSION samplesperrow,JDIMENSION numrows) |
---|
302 | { return |
---|
303 | CALLJPEG(sp,0,(*sp->cinfo.comm.mem->alloc_sarray)(&sp->cinfo.comm,pool_id,samplesperrow, numrows)); |
---|
304 | } |
---|
305 | |
---|
306 | static int |
---|
307 | TIFFojpeg_create_decompress(register OJPEGState *sp) |
---|
308 | { |
---|
309 | sp->cinfo.d.err = jpeg_std_error(&sp->err); /* Initialize error handling */ |
---|
310 | sp->err.error_exit = TIFFojpeg_error_exit; |
---|
311 | sp->err.output_message = TIFFojpeg_output_message; |
---|
312 | return CALLVJPEG(sp,jpeg_create_decompress(&sp->cinfo.d)); |
---|
313 | } |
---|
314 | |
---|
315 | static int |
---|
316 | TIFFojpeg_destroy(register OJPEGState *sp) |
---|
317 | {return CALLVJPEG(sp,jpeg_destroy(&sp->cinfo.comm));} |
---|
318 | |
---|
319 | static int |
---|
320 | TIFFojpeg_finish_decompress(register OJPEGState *sp) |
---|
321 | {return CALLJPEG(sp,-1,(int)jpeg_finish_decompress(&sp->cinfo.d));} |
---|
322 | |
---|
323 | static int |
---|
324 | TIFFojpeg_read_header(register OJPEGState *sp,boolean require_image) |
---|
325 | {return CALLJPEG(sp,-1,jpeg_read_header(&sp->cinfo.d,require_image));} |
---|
326 | |
---|
327 | static int |
---|
328 | TIFFojpeg_read_raw_data(register OJPEGState *sp,JSAMPIMAGE data,int max_lines) |
---|
329 | { |
---|
330 | return |
---|
331 | CALLJPEG(sp,-1,(int)jpeg_read_raw_data(&sp->cinfo.d,data,(JDIMENSION)max_lines)); |
---|
332 | } |
---|
333 | |
---|
334 | static int |
---|
335 | TIFFojpeg_read_scanlines(register OJPEGState *sp,JSAMPARRAY scanlines, |
---|
336 | int max_lines) |
---|
337 | { return |
---|
338 | CALLJPEG(sp,-1,(int)jpeg_read_scanlines(&sp->cinfo.d,scanlines,(JDIMENSION)max_lines)); |
---|
339 | } |
---|
340 | |
---|
341 | static int |
---|
342 | TIFFojpeg_start_decompress(register OJPEGState *sp) |
---|
343 | {return CALLVJPEG(sp,jpeg_start_decompress(&sp->cinfo.d));} |
---|
344 | #ifdef never |
---|
345 | |
---|
346 | /* The following subroutines comprise a JPEG Library "destination" data manager |
---|
347 | by directing compressed data from the JPEG Library to a TIFF Library output |
---|
348 | buffer. |
---|
349 | */ |
---|
350 | static void |
---|
351 | std_init_destination(register j_compress_ptr cinfo){} /* "Dummy" stub */ |
---|
352 | |
---|
353 | static boolean |
---|
354 | std_empty_output_buffer(register j_compress_ptr cinfo) |
---|
355 | { |
---|
356 | # define sp ((OJPEGState *)cinfo) |
---|
357 | register TIFF *tif = sp->tif; |
---|
358 | |
---|
359 | tif->tif_rawcc = tif->tif_rawdatasize; /* Entire buffer has been filled */ |
---|
360 | TIFFFlushData1(tif); |
---|
361 | sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata; |
---|
362 | sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize; |
---|
363 | return TRUE; |
---|
364 | # undef sp |
---|
365 | } |
---|
366 | |
---|
367 | static void |
---|
368 | std_term_destination(register j_compress_ptr cinfo) |
---|
369 | { |
---|
370 | # define sp ((OJPEGState *)cinfo) |
---|
371 | register TIFF *tif = sp->tif; |
---|
372 | |
---|
373 | /* NB: The TIFF Library does the final buffer flush. */ |
---|
374 | tif->tif_rawcp = (tidata_t)sp->dest.next_output_byte; |
---|
375 | tif->tif_rawcc = tif->tif_rawdatasize - (tsize_t)sp->dest.free_in_buffer; |
---|
376 | # undef sp |
---|
377 | } |
---|
378 | |
---|
379 | /*ARGSUSED*/ static void |
---|
380 | TIFFojpeg_data_dest(register OJPEGState *sp,TIFF *tif) |
---|
381 | { |
---|
382 | sp->cinfo.c.dest = &sp->dest; |
---|
383 | sp->dest.init_destination = std_init_destination; |
---|
384 | sp->dest.empty_output_buffer = std_empty_output_buffer; |
---|
385 | sp->dest.term_destination = std_term_destination; |
---|
386 | } |
---|
387 | |
---|
388 | |
---|
389 | /* Alternate destination manager to output JPEGTables field: */ |
---|
390 | |
---|
391 | static void |
---|
392 | tables_init_destination(register j_compress_ptr cinfo) |
---|
393 | { |
---|
394 | # define sp ((OJPEGState *)cinfo) |
---|
395 | /* The "jpegtables_length" field is the allocated buffer size while building */ |
---|
396 | sp->dest.next_output_byte = (JOCTET *)sp->jpegtables; |
---|
397 | sp->dest.free_in_buffer = (size_t)sp->jpegtables_length; |
---|
398 | # undef sp |
---|
399 | } |
---|
400 | |
---|
401 | static boolean |
---|
402 | tables_empty_output_buffer(register j_compress_ptr cinfo) |
---|
403 | { void *newbuf; |
---|
404 | # define sp ((OJPEGState *)cinfo) |
---|
405 | |
---|
406 | /* The entire buffer has been filled, so enlarge it by 1000 bytes. */ |
---|
407 | if (!( newbuf = _TIFFrealloc( (tdata_t)sp->jpegtables |
---|
408 | , (tsize_t)(sp->jpegtables_length + 1000) |
---|
409 | ) |
---|
410 | ) |
---|
411 | ) ERREXIT1(cinfo,JERR_OUT_OF_MEMORY,100); |
---|
412 | sp->dest.next_output_byte = (JOCTET *)newbuf + sp->jpegtables_length; |
---|
413 | sp->dest.free_in_buffer = (size_t)1000; |
---|
414 | sp->jpegtables = newbuf; |
---|
415 | sp->jpegtables_length += 1000; |
---|
416 | return TRUE; |
---|
417 | # undef sp |
---|
418 | } |
---|
419 | |
---|
420 | static void |
---|
421 | tables_term_destination(register j_compress_ptr cinfo) |
---|
422 | { |
---|
423 | # define sp ((OJPEGState *)cinfo) |
---|
424 | /* Set tables length to no. of Bytes actually emitted. */ |
---|
425 | sp->jpegtables_length -= sp->dest.free_in_buffer; |
---|
426 | # undef sp |
---|
427 | } |
---|
428 | |
---|
429 | /*ARGSUSED*/ static int |
---|
430 | TIFFojpeg_tables_dest(register OJPEGState *sp, TIFF *tif) |
---|
431 | { |
---|
432 | |
---|
433 | /* Allocate a working buffer for building tables. The initial size is 1000 |
---|
434 | Bytes, which is usually adequate. |
---|
435 | */ |
---|
436 | if (sp->jpegtables) _TIFFfree(sp->jpegtables); |
---|
437 | if (!(sp->jpegtables = (void*) |
---|
438 | _TIFFmalloc((tsize_t)(sp->jpegtables_length = 1000)) |
---|
439 | ) |
---|
440 | ) |
---|
441 | { |
---|
442 | sp->jpegtables_length = 0; |
---|
443 | TIFFError("TIFFojpeg_tables_dest",no_jtable_space); |
---|
444 | return 0; |
---|
445 | }; |
---|
446 | sp->cinfo.c.dest = &sp->dest; |
---|
447 | sp->dest.init_destination = tables_init_destination; |
---|
448 | sp->dest.empty_output_buffer = tables_empty_output_buffer; |
---|
449 | sp->dest.term_destination = tables_term_destination; |
---|
450 | return 1; |
---|
451 | } |
---|
452 | #endif /* never */ |
---|
453 | |
---|
454 | /* The following subroutines comprise a JPEG Library "source" data manager by |
---|
455 | by directing compressed data to the JPEG Library from a TIFF Library input |
---|
456 | buffer. |
---|
457 | */ |
---|
458 | static void |
---|
459 | std_init_source(register j_decompress_ptr cinfo) |
---|
460 | { |
---|
461 | # define sp ((OJPEGState *)cinfo) |
---|
462 | register TIFF *tif = sp->tif; |
---|
463 | |
---|
464 | if (sp->src.bytes_in_buffer == 0) |
---|
465 | { |
---|
466 | sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata; |
---|
467 | sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc; |
---|
468 | }; |
---|
469 | # undef sp |
---|
470 | } |
---|
471 | |
---|
472 | static boolean |
---|
473 | std_fill_input_buffer(register j_decompress_ptr cinfo) |
---|
474 | { static const JOCTET dummy_EOI[2]={0xFF,JPEG_EOI}; |
---|
475 | # define sp ((OJPEGState *)cinfo) |
---|
476 | |
---|
477 | /* Control should never get here, since an entire strip/tile is read into |
---|
478 | memory before the decompressor is called; thus, data should have been |
---|
479 | supplied by the "init_source" method. ...But, sometimes things fail. |
---|
480 | */ |
---|
481 | WARNMS(cinfo,JWRN_JPEG_EOF); |
---|
482 | sp->src.next_input_byte = dummy_EOI; /* Insert a fake EOI marker */ |
---|
483 | sp->src.bytes_in_buffer = sizeof dummy_EOI; |
---|
484 | return TRUE; |
---|
485 | # undef sp |
---|
486 | } |
---|
487 | |
---|
488 | static void |
---|
489 | std_skip_input_data(register j_decompress_ptr cinfo,long num_bytes) |
---|
490 | { |
---|
491 | # define sp ((OJPEGState *)cinfo) |
---|
492 | |
---|
493 | if (num_bytes > 0) |
---|
494 | if (num_bytes > (long)sp->src.bytes_in_buffer) /* oops: buffer overrun */ |
---|
495 | (void)std_fill_input_buffer(cinfo); |
---|
496 | else |
---|
497 | { |
---|
498 | sp->src.next_input_byte += (size_t)num_bytes; |
---|
499 | sp->src.bytes_in_buffer -= (size_t)num_bytes; |
---|
500 | } |
---|
501 | # undef sp |
---|
502 | } |
---|
503 | |
---|
504 | /*ARGSUSED*/ static void |
---|
505 | std_term_source(register j_decompress_ptr cinfo){} /* "Dummy" stub */ |
---|
506 | |
---|
507 | /* Allocate temporary I/O buffers for downsampled data, using values computed in |
---|
508 | "jpeg_start_{de}compress()". We use the JPEG Library's allocator so that |
---|
509 | buffers will be released automatically when done with a strip/tile. This is |
---|
510 | also a handy place to compute samplesperclump, bytesperline, etc. |
---|
511 | */ |
---|
512 | static int |
---|
513 | alloc_downsampled_buffers(TIFF *tif,jpeg_component_info *comp_info, |
---|
514 | int num_components) |
---|
515 | { register OJPEGState *sp = OJState(tif); |
---|
516 | |
---|
517 | sp->samplesperclump = 0; |
---|
518 | if (num_components > 0) |
---|
519 | { int ci = 0; |
---|
520 | register jpeg_component_info *compptr = comp_info; |
---|
521 | |
---|
522 | do |
---|
523 | { JSAMPARRAY buf; |
---|
524 | |
---|
525 | sp->samplesperclump += |
---|
526 | compptr->h_samp_factor * compptr->v_samp_factor; |
---|
527 | if (!(buf = TIFFojpeg_alloc_sarray( sp |
---|
528 | , JPOOL_IMAGE |
---|
529 | , compptr->width_in_blocks*DCTSIZE |
---|
530 | , compptr->v_samp_factor *DCTSIZE |
---|
531 | ) |
---|
532 | ) |
---|
533 | ) return 0; |
---|
534 | sp->ds_buffer[ci] = buf; |
---|
535 | } |
---|
536 | while (++compptr,++ci < num_components); |
---|
537 | }; |
---|
538 | return 1; |
---|
539 | } |
---|
540 | #ifdef never |
---|
541 | |
---|
542 | /* JPEG Encoding begins here. */ |
---|
543 | |
---|
544 | static void |
---|
545 | unsuppress_quant_table(register OJPEGState *sp,int tblno) |
---|
546 | { register JQUANT_TBL *qtbl; |
---|
547 | |
---|
548 | if (qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) qtbl->sent_table = FALSE; |
---|
549 | } |
---|
550 | |
---|
551 | static void |
---|
552 | unsuppress_huff_table(register OJPEGState *sp,register int tblno) |
---|
553 | { register JHUFF_TBL *htbl; |
---|
554 | |
---|
555 | if ( (htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) |
---|
556 | || (htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) |
---|
557 | ) htbl->sent_table = FALSE; |
---|
558 | } |
---|
559 | |
---|
560 | static int |
---|
561 | prepare_JPEGTables(register TIFF *tif) |
---|
562 | { register OJPEGState *sp = OJState(tif); |
---|
563 | |
---|
564 | /* Initialize quantization tables for the current quality setting, and mark for |
---|
565 | output only the tables that we want. Note that chrominance tables are |
---|
566 | currently used only with YCbCr. |
---|
567 | */ |
---|
568 | if ( !TIFFojpeg_set_quality(sp,sp->jpegquality,FALSE); |
---|
569 | || !TIFFojpeg_suppress_tables(sp,TRUE) |
---|
570 | ) return 0; |
---|
571 | if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) |
---|
572 | { |
---|
573 | unsuppress_quant_table(sp,0); |
---|
574 | if (sp->photometric == PHOTOMETRIC_YCBCR) unsuppress_quant_table(sp,1); |
---|
575 | } |
---|
576 | if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) |
---|
577 | { |
---|
578 | unsuppress_huff_table(sp,0); |
---|
579 | if (sp->photometric == PHOTOMETRIC_YCBCR) unsuppress_huff_table(sp,1); |
---|
580 | }; |
---|
581 | return TIFFojpeg_tables_dest(sp,tif) && TIFFojpeg_write_tables(sp); |
---|
582 | } |
---|
583 | |
---|
584 | static int |
---|
585 | OJPEGSetupEncode(register TIFF *tif) |
---|
586 | { static const char module[]={"OJPEGSetupEncode"}; |
---|
587 | register OJPEGState *sp = OJState(tif); |
---|
588 | # define td (&tif->tif_dir) |
---|
589 | |
---|
590 | /* Verify miscellaneous parameters. This will need work if the TIFF Library |
---|
591 | ever supports different depths for different components, or if the JPEG |
---|
592 | Library ever supports run-time depth selection. Neither seems imminent. |
---|
593 | */ |
---|
594 | if (td->td_bitspersample != BITS_IN_JSAMPLE) |
---|
595 | { |
---|
596 | TIFFError(module,bad_bps,td->td_bitspersample); |
---|
597 | return 0; |
---|
598 | }; |
---|
599 | |
---|
600 | /* Initialize all JPEG parameters to default values. Note that the JPEG |
---|
601 | Library's "jpeg_set_defaults()" method needs legal values for the |
---|
602 | "in_color_space" and "input_components" fields. |
---|
603 | */ |
---|
604 | sp->cinfo.c.in_color_space = JCS_UNKNOWN; |
---|
605 | sp->cinfo.c.input_components = 1; |
---|
606 | if (!TIFFojpeg_set_defaults(sp)) return 0; |
---|
607 | switch (sp->photometric = td->td_photometric) /* set per-file parameters */ |
---|
608 | { |
---|
609 | case PHOTOMETRIC_YCBCR: |
---|
610 | sp->h_sampling = td->td_ycbcrsubsampling[0]; |
---|
611 | sp->v_sampling = td->td_ycbcrsubsampling[1]; |
---|
612 | # ifdef COLORIMETRY_SUPPORT |
---|
613 | |
---|
614 | /* A ReferenceBlackWhite field MUST be present, since the default value |
---|
615 | is inapproriate for YCbCr. Fill in the proper value if application |
---|
616 | didn't set it. |
---|
617 | */ |
---|
618 | if (!TIFFFieldSet(tif,FIELD_REFBLACKWHITE)) |
---|
619 | { float refbw[6]; |
---|
620 | long top = 1L << td->td_bitspersample; |
---|
621 | |
---|
622 | refbw[0] = 0; |
---|
623 | refbw[1] = (float)(top-1L); |
---|
624 | refbw[2] = (float)(top>>1); |
---|
625 | refbw[3] = refbw[1]; |
---|
626 | refbw[4] = refbw[2]; |
---|
627 | refbw[5] = refbw[1]; |
---|
628 | TIFFSetField(tif,TIFFTAG_REFERENCEBLACKWHITE,refbw); |
---|
629 | }; |
---|
630 | # endif /* COLORIMETRY_SUPPORT */ |
---|
631 | break; |
---|
632 | case PHOTOMETRIC_PALETTE: /* disallowed by Tech Note */ |
---|
633 | case PHOTOMETRIC_MASK: |
---|
634 | TIFFError(module,"PhotometricInterpretation %d not allowed for JPEG", |
---|
635 | (int)sp->photometric); |
---|
636 | return 0; |
---|
637 | |
---|
638 | /* TIFF 6.0 forbids subsampling of all other color spaces */ |
---|
639 | |
---|
640 | default: sp->h_sampling = sp->v_sampling = 1; |
---|
641 | }; |
---|
642 | sp->cinfo.c.data_precision = td->td_bitspersample; |
---|
643 | if (isTiled(tif)) |
---|
644 | { |
---|
645 | if (td->td_tilelength % (sp->v_sampling*DCTSIZE)) |
---|
646 | { |
---|
647 | TIFFError(module,"JPEG tile height must be multiple of %d", |
---|
648 | sp->v_sampling*DCTSIZE); |
---|
649 | return 0; |
---|
650 | }; |
---|
651 | if (td->td_tilewidth % (sp->h_sampling*DCTSIZE)) |
---|
652 | { |
---|
653 | TIFFError(module,"JPEG tile width must be multiple of %d", |
---|
654 | sp->h_sampling*DCTSIZE); |
---|
655 | return 0; |
---|
656 | } |
---|
657 | } |
---|
658 | else |
---|
659 | if ( td->td_rowsperstrip < td->td_imagelength |
---|
660 | && (td->td_rowsperstrip % (sp->v_sampling*DCTSIZE)) |
---|
661 | ) |
---|
662 | { |
---|
663 | TIFFError(module,"RowsPerStrip must be multiple of %d for JPEG", |
---|
664 | sp->v_sampling*DCTSIZE); |
---|
665 | return 0; |
---|
666 | }; |
---|
667 | if (sp->jpegtablesmode & (JPEGTABLESMODE_QUANT|JPEGTABLESMODE_HUFF)) |
---|
668 | { /* create a JPEGTables field */ |
---|
669 | |
---|
670 | if (!prepare_JPEGTables(tif)) return 0; |
---|
671 | |
---|
672 | /* Mark the field "present". We can't use "TIFFSetField()" because |
---|
673 | "BEENWRITING" is already set! |
---|
674 | */ |
---|
675 | TIFFSetFieldBit(tif,FIELD_JPEGTABLES); |
---|
676 | tif->tif_flags |= TIFF_DIRTYDIRECT; |
---|
677 | } |
---|
678 | else |
---|
679 | /* We do not support application-supplied JPEG tables, so mark the field |
---|
680 | "not present". |
---|
681 | */ |
---|
682 | TIFFClrFieldBit(tif,FIELD_JPEGTABLES); |
---|
683 | TIFFojpeg_data_dest(sp,tif); /* send JPEG output to TIFF Library's buffer */ |
---|
684 | return 1; |
---|
685 | # undef td |
---|
686 | } |
---|
687 | |
---|
688 | /*ARGSUSED*/ static int |
---|
689 | OJPEGEncode(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s) |
---|
690 | { register OJPEGState *sp = OJState(tif); |
---|
691 | |
---|
692 | /* Encode a chunk of pixels, where returned data is NOT down-sampled (the |
---|
693 | standard case). The data is expected to be written in scan-line multiples. |
---|
694 | */ |
---|
695 | if (cc % sp->bytesperline) TIFFWarning(tif->tif_name,no_write_frac); |
---|
696 | cc /= sp->bytesperline; |
---|
697 | while (--cc >= 0) |
---|
698 | { JSAMPROW bufptr = (JSAMPROW)buf; |
---|
699 | |
---|
700 | if (TIFFojpeg_write_scanlines(sp,&bufptr,1) != 1) return 0; |
---|
701 | ++tif->tif_row; |
---|
702 | buf += sp->bytesperline; |
---|
703 | }; |
---|
704 | return 1; |
---|
705 | } |
---|
706 | |
---|
707 | /*ARGSUSED*/ static int |
---|
708 | OJPEGEncodeRaw(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s) |
---|
709 | { register OJPEGState *sp = OJState(tif); |
---|
710 | |
---|
711 | /* Encode a chunk of pixels, where returned data is down-sampled as per the |
---|
712 | sampling factors. The data is expected to be written in scan-line |
---|
713 | multiples. |
---|
714 | */ |
---|
715 | if (cc % sp->bytesperline) TIFFWarning(tif->tif_name,no_write_frac); |
---|
716 | cc /= sp->bytesperline; |
---|
717 | while (--cc >= 0) |
---|
718 | { |
---|
719 | if (sp->cinfo.c.num_components > 0) |
---|
720 | { int ci = 0, clumpoffset = 0; |
---|
721 | register jpeg_component_info *compptr = sp->cinfo.c.comp_info; |
---|
722 | |
---|
723 | /* The fastest way to separate the data is to make 1 pass over the scan |
---|
724 | line for each row of each component. |
---|
725 | */ |
---|
726 | do |
---|
727 | { int ypos = 0; |
---|
728 | |
---|
729 | do |
---|
730 | { int padding; |
---|
731 | register JSAMPLE *inptr = (JSAMPLE*)buf + clumpoffset, |
---|
732 | *outptr = |
---|
733 | sp->ds_buffer[ci][sp->scancount*compptr->v_samp_factor+ypos]; |
---|
734 | /* Cb,Cr both have sampling factors 1, so this is correct */ |
---|
735 | register int clumps_per_line = |
---|
736 | sp->cinfo.c.comp_info[1].downsampled_width, |
---|
737 | xpos; |
---|
738 | |
---|
739 | padding = (int) |
---|
740 | ( compptr->width_in_blocks * DCTSIZE |
---|
741 | - clumps_per_line * compptr->h_samp_factor |
---|
742 | ); |
---|
743 | if (compptr->h_samp_factor == 1) /* Cb & Cr fast path */ |
---|
744 | do |
---|
745 | { |
---|
746 | *outptr++ = inptr[0]; |
---|
747 | inptr += sp->samplesperclump; |
---|
748 | } |
---|
749 | while (--clumps_per_line > 0); |
---|
750 | else /* general case */ |
---|
751 | do |
---|
752 | { |
---|
753 | xpos = 0; |
---|
754 | do *outptr++ = inptr[xpos]; |
---|
755 | while (++xpos < compptr->h_samp_factor); |
---|
756 | inptr += sp->samplesperclump; |
---|
757 | } |
---|
758 | while (--clumps_per_line > 0); |
---|
759 | xpos = 0; /* Pad each scan line as needed */ |
---|
760 | do outptr[0]=outptr[-1]; while (++outptr,++xpos < padding); |
---|
761 | clumpoffset += compptr->h_samp_factor; |
---|
762 | } |
---|
763 | while (++ypos < compptr->v_samp_factor); |
---|
764 | } |
---|
765 | while (++compptr,++ci < sp->cinfo.c.num_components); |
---|
766 | }; |
---|
767 | if (++sp->scancount >= DCTSIZE) |
---|
768 | { int n = sp->cinfo.c.max_v_samp_factor*DCTSIZE; |
---|
769 | |
---|
770 | if (TIFFojpeg_write_raw_data(sp,sp->ds_buffer,n) != n) return 0; |
---|
771 | sp->scancount = 0; |
---|
772 | }; |
---|
773 | ++tif->tif_row++ |
---|
774 | buf += sp->bytesperline; |
---|
775 | }; |
---|
776 | return 1; |
---|
777 | } |
---|
778 | |
---|
779 | static int |
---|
780 | OJPEGPreEncode(register TIFF *tif,tsample_t s) |
---|
781 | { static const char module[]={"OJPEGPreEncode"}; |
---|
782 | uint32 segment_width, segment_height; |
---|
783 | int downsampled_input = FALSE; |
---|
784 | register OJPEGState *sp = OJState(tif); |
---|
785 | # define td (&tif->tif_dir) |
---|
786 | |
---|
787 | /* Set encoding state at the start of a strip or tile. */ |
---|
788 | |
---|
789 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
---|
790 | { |
---|
791 | sp->cinfo.c.input_components = td->td_samplesperpixel; |
---|
792 | if (sp->photometric == PHOTOMETRIC_YCBCR) |
---|
793 | { |
---|
794 | if (sp->jpegcolormode == JPEGCOLORMODE_RGB) |
---|
795 | sp->cinfo.c.in_color_space = JCS_RGB; |
---|
796 | else |
---|
797 | { |
---|
798 | sp->cinfo.c.in_color_space = JCS_YCbCr; |
---|
799 | if (sp->h_sampling != 1 || sp->v_sampling != 1) |
---|
800 | downsampled_input = TRUE; |
---|
801 | }; |
---|
802 | if (!TIFFojpeg_set_colorspace(sp,JCS_YCbCr)) return 0; |
---|
803 | |
---|
804 | /* Set Y sampling factors; we assume "jpeg_set_colorspace()" set the |
---|
805 | rest to 1. |
---|
806 | */ |
---|
807 | sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling; |
---|
808 | sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling; |
---|
809 | } |
---|
810 | else |
---|
811 | { |
---|
812 | sp->cinfo.c.in_color_space = JCS_UNKNOWN; |
---|
813 | if (!TIFFojpeg_set_colorspace(sp,JCS_UNKNOWN)) return 0; |
---|
814 | /* "jpeg_set_colorspace()" set all sampling factors to 1. */ |
---|
815 | } |
---|
816 | } |
---|
817 | else |
---|
818 | { |
---|
819 | sp->cinfo.c.input_components = 1; |
---|
820 | sp->cinfo.c.in_color_space = JCS_UNKNOWN; |
---|
821 | if (!TIFFojpeg_set_colorspace(sp,JCS_UNKNOWN)) return 0; |
---|
822 | sp->cinfo.c.comp_info[0].component_id = s; |
---|
823 | /* "jpeg_set_colorspace()" set all sampling factors to 1. */ |
---|
824 | if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0) |
---|
825 | sp->cinfo.c.comp_info[0].quant_tbl_no = |
---|
826 | sp->cinfo.c.comp_info[0].dc_tbl_no = |
---|
827 | sp->cinfo.c.comp_info[0].ac_tbl_no = 1; |
---|
828 | }; |
---|
829 | if (isTiled(tif)) |
---|
830 | { |
---|
831 | segment_width = td->td_tilewidth; |
---|
832 | segment_height = td->td_tilelength; |
---|
833 | sp->bytesperline = TIFFTileRowSize(tif); |
---|
834 | } |
---|
835 | else |
---|
836 | { |
---|
837 | segment_width = td->td_imagewidth; |
---|
838 | segment_height = td->td_imagelength - tif->tif_row; |
---|
839 | if (segment_height > td->td_rowsperstrip) |
---|
840 | segment_height = td->td_rowsperstrip; |
---|
841 | sp->bytesperline = TIFFScanlineSize(tif); |
---|
842 | }; |
---|
843 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) |
---|
844 | { |
---|
845 | |
---|
846 | /* Scale the expected strip/tile size to match a downsampled component. */ |
---|
847 | |
---|
848 | segment_width = TIFFhowmany(segment_width,sp->h_sampling); |
---|
849 | segment_height = TIFFhowmany(segment_height,sp->v_sampling); |
---|
850 | }; |
---|
851 | if (segment_width > 65535 || segment_height > 65535) |
---|
852 | { |
---|
853 | TIFFError(module,"Strip/tile too large for JPEG"); |
---|
854 | return 0; |
---|
855 | }; |
---|
856 | sp->cinfo.c.image_width = segment_width; |
---|
857 | sp->cinfo.c.image_height = segment_height; |
---|
858 | sp->cinfo.c.write_JFIF_header = /* Don't write extraneous markers */ |
---|
859 | sp->cinfo.c.write_Adobe_marker = FALSE; |
---|
860 | if (!(sp->jpegtablesmode & JPEGTABLESMODE_QUANT)) /* setup table handling */ |
---|
861 | { |
---|
862 | if (!TIFFojpeg_set_quality(sp,sp->jpegquality,FALSE)) return 0; |
---|
863 | unsuppress_quant_table(sp,0); |
---|
864 | unsuppress_quant_table(sp,1); |
---|
865 | }; |
---|
866 | sp->cinfo.c.optimize_coding = !(sp->jpegtablesmode & JPEGTABLESMODE_HUFF); |
---|
867 | tif->tif_encoderow = tif->tif_encodestrip = tif->tif_encodetile = |
---|
868 | (sp->cinfo.c.raw_data_in = downsampled_input) |
---|
869 | ? OJPEGEncodeRaw : OJPEGEncode; |
---|
870 | if ( !TIFFojpeg_start_compress(sp,FALSE) /* start JPEG compressor */ |
---|
871 | || downsampled_input /* allocate downsampled-data buffers */ |
---|
872 | && !alloc_downsampled_buffers(tif,sp->cinfo.c.comp_info, |
---|
873 | sp->cinfo.c.num_components) |
---|
874 | ) return 0; |
---|
875 | sp->scancount = 0; |
---|
876 | return 1; |
---|
877 | # undef td |
---|
878 | } |
---|
879 | |
---|
880 | static int |
---|
881 | OJPEGPostEncode(register TIFF *tif) |
---|
882 | { register OJPEGState *sp = OJState(tif); |
---|
883 | |
---|
884 | /* Finish up at the end of a strip or tile. */ |
---|
885 | |
---|
886 | if (sp->scancount > 0) /* emit partial buffer of down-sampled data */ |
---|
887 | { |
---|
888 | if (sp->scancount < DCTSIZE && sp->cinfo.c.num_components > 0) |
---|
889 | { int ci = 0, n; /* Pad the data vertically */ |
---|
890 | register jpeg_component_info *compptr = sp->cinfo.c.comp_info; |
---|
891 | |
---|
892 | do |
---|
893 | { tsize_t row_width = |
---|
894 | compptr->width_in_blocks*DCTSIZE*sizeof(JSAMPLE); |
---|
895 | int ypos = sp->scancount*compptr->v_samp_factor; |
---|
896 | |
---|
897 | do _TIFFmemcpy( (tdata_t)sp->ds_buffer[ci][ypos] |
---|
898 | , (tdata_t)sp->ds_buffer[ci][ypos-1] |
---|
899 | , row_width |
---|
900 | ); |
---|
901 | while (++ypos < compptr->v_samp_factor*DCTSIZE); |
---|
902 | } |
---|
903 | while (++compptr,++ci < sp->cinfo.c.num_components); |
---|
904 | }; |
---|
905 | n = sp->cinfo.c.max_v_samp_factor*DCTSIZE; |
---|
906 | if (TIFFojpeg_write_raw_data(sp,sp->ds_buffer,n) != n) return 0; |
---|
907 | }; |
---|
908 | return TIFFojpeg_finish_compress(sp); |
---|
909 | } |
---|
910 | #endif /* never */ |
---|
911 | |
---|
912 | /* JPEG Decoding begins here. */ |
---|
913 | |
---|
914 | static int |
---|
915 | OJPEGSetupDecode(register TIFF *tif) |
---|
916 | { static const char module[]={"OJPEGSetupDecode"}; |
---|
917 | register OJPEGState *sp = OJState(tif); |
---|
918 | # define td (&tif->tif_dir) |
---|
919 | |
---|
920 | /* Verify miscellaneous parameters. This will need work if the TIFF Library |
---|
921 | ever supports different depths for different components, or if the JPEG |
---|
922 | Library ever supports run-time depth selection. Neither seems imminent. |
---|
923 | */ |
---|
924 | if (td->td_bitspersample != BITS_IN_JSAMPLE) |
---|
925 | { |
---|
926 | TIFFError(module,bad_bps,td->td_bitspersample); |
---|
927 | return 0; |
---|
928 | }; |
---|
929 | |
---|
930 | /* Almost all old JPEG-in-TIFF encapsulations use 8 bits per sample, but the |
---|
931 | following is just a "sanity check", since "OJPEGPreDecode()" actually |
---|
932 | depends upon this assumption in certain cases. |
---|
933 | */ |
---|
934 | if (td->td_bitspersample != 8) |
---|
935 | { |
---|
936 | TIFFError(module,"Cannot decompress %u bits per sample"); |
---|
937 | return 0; |
---|
938 | }; |
---|
939 | |
---|
940 | /* Grab parameters that are same for all strips/tiles. */ |
---|
941 | |
---|
942 | if ((sp->photometric = td->td_photometric) == PHOTOMETRIC_YCBCR) |
---|
943 | { |
---|
944 | sp->h_sampling = td->td_ycbcrsubsampling[0]; |
---|
945 | sp->v_sampling = td->td_ycbcrsubsampling[1]; |
---|
946 | } |
---|
947 | else /* TIFF 6.0 forbids subsampling of all other color spaces */ |
---|
948 | sp->h_sampling = sp->v_sampling = 1; |
---|
949 | sp->cinfo.d.src = &sp->src; |
---|
950 | sp->src.init_source = std_init_source; |
---|
951 | sp->src.fill_input_buffer = std_fill_input_buffer; |
---|
952 | sp->src.skip_input_data = std_skip_input_data; |
---|
953 | sp->src.resync_to_restart = jpeg_resync_to_restart; |
---|
954 | sp->src.term_source = std_term_source; |
---|
955 | tif->tif_postdecode = _TIFFNoPostDecode; /* Override Byte-swapping */ |
---|
956 | return 1; |
---|
957 | # undef td |
---|
958 | } |
---|
959 | |
---|
960 | /*ARGSUSED*/ static int |
---|
961 | OJPEGDecode(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s) |
---|
962 | { static float zeroes[6]; |
---|
963 | tsize_t nrows; |
---|
964 | register OJPEGState *sp = OJState(tif); |
---|
965 | |
---|
966 | /* BEWARE OF KLUDGE: If our input file was produced by Microsoft's Wang |
---|
967 | Imaging for Windows application, the DC coefficients of |
---|
968 | each JPEG image component (Y,Cb,Cr) must be reset at the beginning of each |
---|
969 | TIFF "strip", and any JPEG data bits remaining in the decoder's input buffer |
---|
970 | must be discarded, up to the next input-Byte storage boundary. To do so, we |
---|
971 | create an "ad hoc" interface in the "jdhuff.c" module of IJG JPEG Library |
---|
972 | Version 6, and we invoke that interface here before decoding each "strip". |
---|
973 | */ |
---|
974 | if (sp->is_WANG) jpeg_reset_huff_decode(&sp->cinfo.d,zeroes); |
---|
975 | |
---|
976 | /* Decode a chunk of pixels, where returned data is NOT down-sampled (the |
---|
977 | standard case). The data is expected to be read in scan-line multiples. |
---|
978 | */ |
---|
979 | if (nrows = sp->cinfo.d.image_height) |
---|
980 | { unsigned int bytesperline = isTiled(tif) |
---|
981 | ? TIFFTileRowSize(tif) |
---|
982 | : TIFFScanlineSize(tif); |
---|
983 | |
---|
984 | /* WARNING: Unlike "OJPEGDecodeRaw()", below, the no. of Bytes in each |
---|
985 | decoded row is calculated here as "bytesperline" instead of |
---|
986 | using "sp->bytesperline", which might be a little smaller. This can |
---|
987 | occur for an old tiled image whose width isn't a multiple of 8 pixels. |
---|
988 | That's illegal according to the TIFF Version 6 specification, but some |
---|
989 | test files, like "zackthecat.tif", were built that way. In those cases, |
---|
990 | we want to embed the image's true width in our caller's buffer (which is |
---|
991 | presumably allocated according to the expected tile width) by |
---|
992 | effectively "padding" it with unused Bytes at the end of each row. |
---|
993 | */ |
---|
994 | do |
---|
995 | { JSAMPROW bufptr = (JSAMPROW)buf; |
---|
996 | |
---|
997 | if (TIFFojpeg_read_scanlines(sp,&bufptr,1) != 1) return 0; |
---|
998 | buf += bytesperline; |
---|
999 | ++tif->tif_row; |
---|
1000 | } |
---|
1001 | while ((cc -= bytesperline) > 0 && --nrows > 0); |
---|
1002 | }; |
---|
1003 | return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height |
---|
1004 | || TIFFojpeg_finish_decompress(sp); |
---|
1005 | } |
---|
1006 | |
---|
1007 | /*ARGSUSED*/ static int |
---|
1008 | OJPEGDecodeRaw(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s) |
---|
1009 | { static float zeroes[6]; |
---|
1010 | tsize_t nrows; |
---|
1011 | register OJPEGState *sp = OJState(tif); |
---|
1012 | |
---|
1013 | /* BEWARE OF KLUDGE: If our input file was produced by Microsoft's Wang |
---|
1014 | Imaging for Windows application, the DC coefficients of |
---|
1015 | each JPEG image component (Y,Cb,Cr) must be reset at the beginning of each |
---|
1016 | TIFF "strip", and any JPEG data bits remaining in the decoder's input buffer |
---|
1017 | must be discarded, up to the next input-Byte storage boundary. To do so, we |
---|
1018 | create an "ad hoc" interface in the "jdhuff.c" module of IJG JPEG Library |
---|
1019 | Version 6, and we invoke that interface here before decoding each "strip". |
---|
1020 | */ |
---|
1021 | if (sp->is_WANG) jpeg_reset_huff_decode(&sp->cinfo.d,zeroes); |
---|
1022 | |
---|
1023 | /* Decode a chunk of pixels, where returned data is down-sampled as per the |
---|
1024 | sampling factors. The data is expected to be read in scan-line multiples. |
---|
1025 | */ |
---|
1026 | if (nrows = sp->cinfo.d.image_height) |
---|
1027 | do |
---|
1028 | { |
---|
1029 | if (sp->scancount >= DCTSIZE) /* reload downsampled-data buffer */ |
---|
1030 | { int n = sp->cinfo.d.max_v_samp_factor*DCTSIZE; |
---|
1031 | |
---|
1032 | if (TIFFojpeg_read_raw_data(sp,sp->ds_buffer,n) != n) return 0; |
---|
1033 | sp->scancount = 0; |
---|
1034 | }; |
---|
1035 | if (sp->cinfo.d.num_components > 0) |
---|
1036 | { int ci = 0, clumpoffset = 0; |
---|
1037 | register jpeg_component_info *compptr = sp->cinfo.d.comp_info; |
---|
1038 | |
---|
1039 | /* The fastest way to separate the data is: make 1 pass over the scan |
---|
1040 | line for each row of each component. |
---|
1041 | */ |
---|
1042 | do |
---|
1043 | { int ypos = 0; |
---|
1044 | |
---|
1045 | if (compptr->h_samp_factor == 1) /* Cb & Cr fast path */ |
---|
1046 | do |
---|
1047 | { register JSAMPLE *inptr = |
---|
1048 | sp->ds_buffer[ci][sp->scancount*compptr->v_samp_factor+ypos], |
---|
1049 | *outptr = (JSAMPLE *)buf + clumpoffset; |
---|
1050 | /* Cb & Cr have sampling factors = 1, so this is correct */ |
---|
1051 | register int clumps_per_line = |
---|
1052 | sp->cinfo.d.comp_info[1].downsampled_width; |
---|
1053 | |
---|
1054 | do *outptr = *inptr++; |
---|
1055 | while ( (outptr += sp->samplesperclump) |
---|
1056 | , --clumps_per_line > 0 |
---|
1057 | ); |
---|
1058 | } |
---|
1059 | while ( (clumpoffset += compptr->h_samp_factor) |
---|
1060 | , ++ypos < compptr->v_samp_factor |
---|
1061 | ); |
---|
1062 | else /* general case */ |
---|
1063 | do |
---|
1064 | { register JSAMPLE *inptr = |
---|
1065 | sp->ds_buffer[ci][sp->scancount*compptr->v_samp_factor+ypos], |
---|
1066 | *outptr = (JSAMPLE *)buf + clumpoffset; |
---|
1067 | /* Cb & Cr have sampling factors = 1, so this is correct */ |
---|
1068 | register int clumps_per_line = |
---|
1069 | sp->cinfo.d.comp_info[1].downsampled_width; |
---|
1070 | |
---|
1071 | do |
---|
1072 | { register int xpos = 0; |
---|
1073 | |
---|
1074 | do outptr[xpos] = *inptr++; |
---|
1075 | while (++xpos < compptr->h_samp_factor); |
---|
1076 | } |
---|
1077 | while ( (outptr += sp->samplesperclump) |
---|
1078 | , --clumps_per_line > 0 |
---|
1079 | ); |
---|
1080 | } |
---|
1081 | while ( (clumpoffset += compptr->h_samp_factor) |
---|
1082 | , ++ypos < compptr->v_samp_factor |
---|
1083 | ); |
---|
1084 | } |
---|
1085 | while (++compptr,++ci < sp->cinfo.d.num_components); |
---|
1086 | }; |
---|
1087 | ++sp->scancount; |
---|
1088 | buf += sp->bytesperline; |
---|
1089 | ++tif->tif_row; |
---|
1090 | } |
---|
1091 | while ((cc -= sp->bytesperline) > 0 && --nrows > 0); |
---|
1092 | return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height |
---|
1093 | || TIFFojpeg_finish_decompress(sp); |
---|
1094 | } |
---|
1095 | |
---|
1096 | /* "OJPEGPreDecode()" temporarily forces the JPEG Library to use the following |
---|
1097 | subroutine as a "dummy" input reader, to fool it into thinking that it has |
---|
1098 | read the image's 1st "Start of Scan" (SOS) marker and initialize accordingly. |
---|
1099 | */ |
---|
1100 | /*ARGSUSED*/ METHODDEF(int) |
---|
1101 | fake_SOS_marker(j_decompress_ptr cinfo){return JPEG_REACHED_SOS;} |
---|
1102 | |
---|
1103 | /*ARGSUSED*/ METHODDEF(int) |
---|
1104 | suspend(j_decompress_ptr cinfo){return JPEG_SUSPENDED;} |
---|
1105 | |
---|
1106 | /*ARGSUSED*/ static int |
---|
1107 | OJPEGPreDecode(register TIFF *tif,tsample_t s) |
---|
1108 | { static const char bad_factors[]={"Improper JPEG sampling factors"}, |
---|
1109 | module[]={"OJPEGPreDecode"}; |
---|
1110 | uint32 segment_width, segment_height; |
---|
1111 | int downsampled_output = FALSE, |
---|
1112 | is_JFIF; /* <=> JFIF image? */ |
---|
1113 | J_COLOR_SPACE in_color_space = JCS_UNKNOWN; /* Image's input color space */ |
---|
1114 | register OJPEGState *sp = OJState(tif); |
---|
1115 | # define td (&tif->tif_dir) |
---|
1116 | |
---|
1117 | tif->tif_predecode = _TIFFNoPreCode; /* Don't call us again */ |
---|
1118 | |
---|
1119 | /* BOGOSITY ALERT! MicroSoft's Wang Imaging for Windows application produces |
---|
1120 | images containing "JPEGInterchangeFormat[Length]" TIFF |
---|
1121 | records that resemble JFIF-in-TIFF encapsulations but, in fact, violate the |
---|
1122 | TIFF Version 6 specification in several ways; nevertheless, we try to handle |
---|
1123 | them gracefully because there are apparently a lot of them around. The |
---|
1124 | purported "JFIF" data stream in one of these files vaguely resembles a JPEG |
---|
1125 | "tables only" data stream, except that there's no trailing EOI marker. The |
---|
1126 | rest of the JPEG data stream lies in a discontiguous file region, identified |
---|
1127 | by the 0th Strip offset (which is *also* illegal!), where it begins with an |
---|
1128 | SOS marker and apparently continues to the end of the file. There is no |
---|
1129 | trailing EOI marker here, either. |
---|
1130 | */ |
---|
1131 | is_JFIF = !sp->is_WANG && TIFFFieldSet(tif,FIELD_JPEGIFOFFSET); |
---|
1132 | |
---|
1133 | /* Set up to decode a strip or tile. Start by resetting decoder state left |
---|
1134 | over from any previous strip/tile, in case our client application didn't |
---|
1135 | read all of that data. Then read the JPEG header data. |
---|
1136 | */ |
---|
1137 | if (!TIFFojpeg_abort(sp)) return 0; |
---|
1138 | |
---|
1139 | /* Do a preliminary translation of the image's (input) color space from its |
---|
1140 | TIFF representation to JPEG Library representation. We might have to fix |
---|
1141 | this up after calling "TIFFojpeg_read_header()", which tries to establish |
---|
1142 | its own JPEG Library defaults. While we're here, initialize some other |
---|
1143 | decompression parameters that won't be overridden. |
---|
1144 | */ |
---|
1145 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
---|
1146 | { |
---|
1147 | if (sp->h_sampling != 1 || sp->v_sampling != 1) |
---|
1148 | downsampled_output = TRUE; /* Tentative default */ |
---|
1149 | switch (sp->photometric) /* default color-space translation */ |
---|
1150 | { |
---|
1151 | case PHOTOMETRIC_MINISBLACK: in_color_space = JCS_GRAYSCALE; |
---|
1152 | break; |
---|
1153 | case PHOTOMETRIC_RGB : in_color_space = JCS_RGB; |
---|
1154 | break; |
---|
1155 | case PHOTOMETRIC_SEPARATED : in_color_space = JCS_CMYK; |
---|
1156 | break; |
---|
1157 | case PHOTOMETRIC_YCBCR : in_color_space = JCS_YCbCr; |
---|
1158 | /* JPEG Library converts YCbCr to RGB? */ |
---|
1159 | if ( sp->jpegcolormode |
---|
1160 | == JPEGCOLORMODE_RGB |
---|
1161 | ) downsampled_output = FALSE; |
---|
1162 | } |
---|
1163 | }; |
---|
1164 | segment_width = td->td_imagewidth; |
---|
1165 | segment_height = td->td_imagelength - tif->tif_row; |
---|
1166 | if (isTiled(tif)) |
---|
1167 | { |
---|
1168 | if (sp->is_WANG) /* we don't know how to handle it */ |
---|
1169 | { |
---|
1170 | TIFFError(module,"Tiled Wang image not supported"); |
---|
1171 | return 0; |
---|
1172 | }; |
---|
1173 | |
---|
1174 | /* BOGOSITY ALERT! "TIFFTileRowSize()" seems to work fine for modern JPEG- |
---|
1175 | in-TIFF encapsulations where the image width--like the |
---|
1176 | tile width--is a multiple of 8 or 16 pixels. But image widths and |
---|
1177 | heights are aren't restricted to 8- or 16-bit multiples, and we need |
---|
1178 | the exact Byte count of decompressed scan lines when we call the JPEG |
---|
1179 | Library. At least one old file ("zackthecat.tif") in the TIFF Library |
---|
1180 | test suite has widths and heights slightly less than the tile sizes, and |
---|
1181 | it apparently used the bogus computation below to determine the number |
---|
1182 | of Bytes per scan line (was this due to an old, broken version of |
---|
1183 | "TIFFhowmany()"?). Before we get here, "OJPEGSetupDecode()" verified |
---|
1184 | that our image uses 8-bit samples, so the following check appears to |
---|
1185 | return the correct answer in all known cases tested to date. |
---|
1186 | */ |
---|
1187 | if (is_JFIF || (segment_width & 7) == 0) |
---|
1188 | sp->bytesperline = TIFFTileRowSize(tif); /* Normal case */ |
---|
1189 | else |
---|
1190 | { |
---|
1191 | /* Was the file-encoder's segment-width calculation bogus? */ |
---|
1192 | segment_width = (segment_width/sp->h_sampling + 1) * sp->h_sampling; |
---|
1193 | sp->bytesperline = segment_width * td->td_samplesperpixel; |
---|
1194 | } |
---|
1195 | } |
---|
1196 | else sp->bytesperline = TIFFVStripSize(tif,1); |
---|
1197 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) |
---|
1198 | { |
---|
1199 | |
---|
1200 | /* Scale the expected strip/tile size to match a downsampled component. */ |
---|
1201 | |
---|
1202 | segment_width = TIFFhowmany(segment_width,sp->h_sampling); |
---|
1203 | segment_height = TIFFhowmany(segment_height,sp->v_sampling); |
---|
1204 | }; |
---|
1205 | |
---|
1206 | /* BEWARE OF KLUDGE: If we have JPEG Interchange File Format (JFIF) image, |
---|
1207 | then we want to read "metadata" in the bit-stream's |
---|
1208 | header and validate it against corresponding information in TIFF records. |
---|
1209 | But if we have a *really old* JPEG file that's not JFIF, then we simply |
---|
1210 | assign TIFF-record values to JPEG Library variables without checking. |
---|
1211 | */ |
---|
1212 | if (is_JFIF) /* JFIF image */ |
---|
1213 | { unsigned char *end_of_data; |
---|
1214 | register unsigned char *p; |
---|
1215 | |
---|
1216 | /* WARNING: Although the image file contains a JFIF bit stream, it might |
---|
1217 | also contain some old TIFF records causing "OJPEGVSetField()" |
---|
1218 | to have allocated quantization or Huffman decoding tables. But when the |
---|
1219 | JPEG Library reads and parses the JFIF header below, it reallocate these |
---|
1220 | tables anew without checking for "dangling" pointers, thereby causing a |
---|
1221 | memory "leak". We have enough information to potentially deallocate the |
---|
1222 | old tables here, but unfortunately JPEG Library Version 6B uses a "pool" |
---|
1223 | allocator for small objects, with no deallocation procedure; instead, it |
---|
1224 | reclaims a whole pool when an image is closed/destroyed, so well-behaved |
---|
1225 | TIFF client applications (i.e., those which close their JPEG images as |
---|
1226 | soon as they're no longer needed) will waste memory for a short time but |
---|
1227 | recover it eventually. But ill-behaved TIFF clients (i.e., those which |
---|
1228 | keep many JPEG images open gratuitously) can exhaust memory prematurely. |
---|
1229 | If the JPEG Library ever implements a deallocation procedure, insert |
---|
1230 | this clean-up code: |
---|
1231 | */ |
---|
1232 | # ifdef someday |
---|
1233 | if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) /* free quant. tables */ |
---|
1234 | { register int i = 0; |
---|
1235 | |
---|
1236 | do |
---|
1237 | { register JQUANT_TBL *q; |
---|
1238 | |
---|
1239 | if (q = sp->cinfo.d.quant_tbl_ptrs[i]) |
---|
1240 | { |
---|
1241 | jpeg_free_small(&sp->cinfo.comm,q,sizeof *q); |
---|
1242 | sp->cinfo.d.quant_tbl_ptrs[i] = 0; |
---|
1243 | } |
---|
1244 | } |
---|
1245 | while (++i < NUM_QUANT_TBLS); |
---|
1246 | }; |
---|
1247 | if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) /* free Huffman tables */ |
---|
1248 | { register int i = 0; |
---|
1249 | |
---|
1250 | do |
---|
1251 | { register JHUFF_TBL *h; |
---|
1252 | |
---|
1253 | if (h = sp->cinfo.d.dc_huff_tbl_ptrs[i]) |
---|
1254 | { |
---|
1255 | jpeg_free_small(&sp->cinfo.comm,h,sizeof *h); |
---|
1256 | sp->cinfo.d.dc_huff_tbl_ptrs[i] = 0; |
---|
1257 | }; |
---|
1258 | if (h = sp->cinfo.d.ac_huff_tbl_ptrs[i]) |
---|
1259 | { |
---|
1260 | jpeg_free_small(&sp->cinfo.comm,h,sizeof *h); |
---|
1261 | sp->cinfo.d.ac_huff_tbl_ptrs[i] = 0; |
---|
1262 | } |
---|
1263 | } |
---|
1264 | while (++i < NUM_HUFF_TBLS); |
---|
1265 | }; |
---|
1266 | # endif /* someday */ |
---|
1267 | |
---|
1268 | /* Since we might someday wish to try rewriting "old format" JPEG-in-TIFF |
---|
1269 | encapsulations in "new format" files, try to synthesize the value of a |
---|
1270 | modern "JPEGTables" TIFF record by scanning the JPEG data from just past |
---|
1271 | the "Start of Information" (SOI) marker until something other than a |
---|
1272 | legitimate "table" marker is found, as defined in ISO DIS 10918-1 |
---|
1273 | Appending B.2.4; namely: |
---|
1274 | |
---|
1275 | -- Define Quantization Table (DQT) |
---|
1276 | -- Define Huffman Table (DHT) |
---|
1277 | -- Define Arithmetic Coding table (DAC) |
---|
1278 | -- Define Restart Interval (DRI) |
---|
1279 | -- Comment (COM) |
---|
1280 | -- Application data (APPn) |
---|
1281 | |
---|
1282 | For convenience, we also accept "Expansion" (EXP) markers, although they |
---|
1283 | are apparently not a part of normal "table" data. |
---|
1284 | */ |
---|
1285 | sp->jpegtables = p = (unsigned char *)sp->src.next_input_byte; |
---|
1286 | end_of_data = p + sp->src.bytes_in_buffer; |
---|
1287 | p += 2; |
---|
1288 | while (p < end_of_data && p[0] == 0xFF) |
---|
1289 | switch (p[1]) |
---|
1290 | { |
---|
1291 | default : goto L; |
---|
1292 | case 0xC0: /* SOF0 */ |
---|
1293 | case 0xC1: /* SOF1 */ |
---|
1294 | case 0xC2: /* SOF2 */ |
---|
1295 | case 0xC3: /* SOF3 */ |
---|
1296 | case 0xC4: /* DHT */ |
---|
1297 | case 0xC5: /* SOF5 */ |
---|
1298 | case 0xC6: /* SOF6 */ |
---|
1299 | case 0xC7: /* SOF7 */ |
---|
1300 | case 0xC9: /* SOF9 */ |
---|
1301 | case 0xCA: /* SOF10 */ |
---|
1302 | case 0xCB: /* SOF11 */ |
---|
1303 | case 0xCC: /* DAC */ |
---|
1304 | case 0xCD: /* SOF13 */ |
---|
1305 | case 0xCE: /* SOF14 */ |
---|
1306 | case 0xCF: /* SOF15 */ |
---|
1307 | case 0xDB: /* DQT */ |
---|
1308 | case 0xDD: /* DRI */ |
---|
1309 | case 0xDF: /* EXP */ |
---|
1310 | case 0xE0: /* APP0 */ |
---|
1311 | case 0xE1: /* APP1 */ |
---|
1312 | case 0xE2: /* APP2 */ |
---|
1313 | case 0xE3: /* APP3 */ |
---|
1314 | case 0xE4: /* APP4 */ |
---|
1315 | case 0xE5: /* APP5 */ |
---|
1316 | case 0xE6: /* APP6 */ |
---|
1317 | case 0xE7: /* APP7 */ |
---|
1318 | case 0xE8: /* APP8 */ |
---|
1319 | case 0xE9: /* APP9 */ |
---|
1320 | case 0xEA: /* APP10 */ |
---|
1321 | case 0xEB: /* APP11 */ |
---|
1322 | case 0xEC: /* APP12 */ |
---|
1323 | case 0xED: /* APP13 */ |
---|
1324 | case 0xEE: /* APP14 */ |
---|
1325 | case 0xEF: /* APP15 */ |
---|
1326 | case 0xFE: /* COM */ |
---|
1327 | p += (p[2] << 8 | p[3]) + 2; |
---|
1328 | }; |
---|
1329 | L: if (p - (unsigned char *)sp->jpegtables > 2) /* fake "JPEGTables" */ |
---|
1330 | { |
---|
1331 | |
---|
1332 | /* In case our client application asks, pretend that this image file |
---|
1333 | contains a modern "JPEGTables" TIFF record by copying to a buffer |
---|
1334 | the initial part of the JFIF bit-stream that we just scanned, from |
---|
1335 | the SOI marker through the "metadata" tables, then append an EOI |
---|
1336 | marker and flag the "JPEGTables" TIFF record as "present". |
---|
1337 | */ |
---|
1338 | sp->jpegtables_length = p - (unsigned char*)sp->jpegtables + 2; |
---|
1339 | p = sp->jpegtables; |
---|
1340 | if (!(sp->jpegtables = _TIFFmalloc(sp->jpegtables_length))) |
---|
1341 | { |
---|
1342 | TIFFError(module,no_jtable_space); |
---|
1343 | return 0; |
---|
1344 | }; |
---|
1345 | _TIFFmemcpy(sp->jpegtables,p,sp->jpegtables_length-2); |
---|
1346 | p = (unsigned char *)sp->jpegtables + sp->jpegtables_length; |
---|
1347 | p[-2] = 0xFF; p[-1] = JPEG_EOI; /* Append EOI marker */ |
---|
1348 | TIFFSetFieldBit(tif,FIELD_JPEGTABLES); |
---|
1349 | tif->tif_flags |= TIFF_DIRTYDIRECT; |
---|
1350 | } |
---|
1351 | else sp->jpegtables = 0; /* Don't simulate "JPEGTables" */ |
---|
1352 | if (TIFFojpeg_read_header(sp,TRUE) != JPEG_HEADER_OK) return 0; |
---|
1353 | if ( sp->cinfo.d.image_width != segment_width |
---|
1354 | || sp->cinfo.d.image_height != segment_height |
---|
1355 | ) |
---|
1356 | { |
---|
1357 | TIFFError(module,"Improper JPEG strip/tile size"); |
---|
1358 | return 0; |
---|
1359 | }; |
---|
1360 | if ( ( td->td_planarconfig == PLANARCONFIG_CONTIG |
---|
1361 | ? td->td_samplesperpixel |
---|
1362 | : 1 |
---|
1363 | ) != sp->cinfo.d.num_components |
---|
1364 | ) |
---|
1365 | { |
---|
1366 | TIFFError(module,"Improper JPEG component count"); |
---|
1367 | return 0; |
---|
1368 | }; |
---|
1369 | if (sp->cinfo.d.data_precision != td->td_bitspersample) |
---|
1370 | { |
---|
1371 | TIFFError(module,"Improper JPEG data precision"); |
---|
1372 | return 0; |
---|
1373 | }; |
---|
1374 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
---|
1375 | { int ci; |
---|
1376 | |
---|
1377 | /* Component 0 should have expected sampling factors. */ |
---|
1378 | |
---|
1379 | if ( sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling |
---|
1380 | || sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling |
---|
1381 | ) |
---|
1382 | { |
---|
1383 | TIFFError(module,bad_factors); |
---|
1384 | return 0; |
---|
1385 | }; |
---|
1386 | ci = 1; /* The rest should have sampling factors 1,1 */ |
---|
1387 | do if ( sp->cinfo.d.comp_info[ci].h_samp_factor != 1 |
---|
1388 | || sp->cinfo.d.comp_info[ci].v_samp_factor != 1 |
---|
1389 | ) |
---|
1390 | { |
---|
1391 | TIFFError(module,bad_factors); |
---|
1392 | return 0; |
---|
1393 | } |
---|
1394 | while (++ci < sp->cinfo.d.num_components); |
---|
1395 | } |
---|
1396 | else |
---|
1397 | |
---|
1398 | /* PLANARCONFIG_SEPARATE's single component should have sampling factors |
---|
1399 | 1,1. |
---|
1400 | */ |
---|
1401 | if ( sp->cinfo.d.comp_info[0].h_samp_factor != 1 |
---|
1402 | || sp->cinfo.d.comp_info[0].v_samp_factor != 1 |
---|
1403 | ) |
---|
1404 | { |
---|
1405 | TIFFError(module,bad_factors); |
---|
1406 | return 0; |
---|
1407 | } |
---|
1408 | } |
---|
1409 | else /* not JFIF image */ |
---|
1410 | { int (*save)(j_decompress_ptr cinfo) = sp->cinfo.d.marker->read_markers; |
---|
1411 | register int i; |
---|
1412 | |
---|
1413 | /* We're not assuming that this file's JPEG bit stream has any header |
---|
1414 | "metadata", so fool the JPEG Library into thinking that we read a |
---|
1415 | "Start of Input" (SOI) marker and a "Start of Frame" (SOFx) marker, then |
---|
1416 | force it to read a simulated "Start of Scan" (SOS) marker when we call |
---|
1417 | "TIFFojpeg_read_header()" below. This should cause the JPEG Library to |
---|
1418 | establish reasonable defaults. |
---|
1419 | */ |
---|
1420 | sp->cinfo.d.marker->saw_SOI = /* Pretend we saw SOI marker */ |
---|
1421 | sp->cinfo.d.marker->saw_SOF = TRUE; /* Pretend we saw SOF marker */ |
---|
1422 | sp->cinfo.d.marker->read_markers = |
---|
1423 | sp->is_WANG ? suspend : fake_SOS_marker; |
---|
1424 | sp->cinfo.d.global_state = DSTATE_INHEADER; |
---|
1425 | sp->cinfo.d.Se = DCTSIZE2-1; /* Suppress JPEG Library warning */ |
---|
1426 | sp->cinfo.d.image_width = segment_width; |
---|
1427 | sp->cinfo.d.image_height = segment_height; |
---|
1428 | sp->cinfo.d.data_precision = td->td_bitspersample; |
---|
1429 | |
---|
1430 | /* The following color-space initialization, including the complicated |
---|
1431 | "switch"-statement below, essentially duplicates the logic used by the |
---|
1432 | JPEG Library's "jpeg_init_colorspace()" subroutine during compression. |
---|
1433 | */ |
---|
1434 | sp->cinfo.d.num_components = td->td_planarconfig == PLANARCONFIG_CONTIG |
---|
1435 | ? td->td_samplesperpixel |
---|
1436 | : 1; |
---|
1437 | sp->cinfo.d.comp_info = (jpeg_component_info *) |
---|
1438 | (*sp->cinfo.d.mem->alloc_small) |
---|
1439 | ( &sp->cinfo.comm |
---|
1440 | , JPOOL_IMAGE |
---|
1441 | , sp->cinfo.d.num_components * sizeof *sp->cinfo.d.comp_info |
---|
1442 | ); |
---|
1443 | i = 0; |
---|
1444 | do |
---|
1445 | { |
---|
1446 | sp->cinfo.d.comp_info[i].component_index = i; |
---|
1447 | sp->cinfo.d.comp_info[i].component_needed = TRUE; |
---|
1448 | sp->cinfo.d.cur_comp_info[i] = &sp->cinfo.d.comp_info[i]; |
---|
1449 | } |
---|
1450 | while (++i < sp->cinfo.d.num_components); |
---|
1451 | switch (in_color_space) |
---|
1452 | { |
---|
1453 | case JCS_UNKNOWN : |
---|
1454 | i = 0; |
---|
1455 | do |
---|
1456 | { |
---|
1457 | sp->cinfo.d.comp_info[i].component_id = i; |
---|
1458 | sp->cinfo.d.comp_info[i].h_samp_factor = |
---|
1459 | sp->cinfo.d.comp_info[i].v_samp_factor = 1; |
---|
1460 | } |
---|
1461 | while (++i < sp->cinfo.d.num_components); |
---|
1462 | break; |
---|
1463 | case JCS_GRAYSCALE: |
---|
1464 | sp->cinfo.d.comp_info[0].component_id = |
---|
1465 | sp->cinfo.d.comp_info[0].h_samp_factor = |
---|
1466 | sp->cinfo.d.comp_info[0].v_samp_factor = 1; |
---|
1467 | break; |
---|
1468 | case JCS_RGB : |
---|
1469 | sp->cinfo.d.comp_info[0].component_id = 'R'; |
---|
1470 | sp->cinfo.d.comp_info[1].component_id = 'G'; |
---|
1471 | sp->cinfo.d.comp_info[2].component_id = 'B'; |
---|
1472 | i = 0; |
---|
1473 | do sp->cinfo.d.comp_info[i].h_samp_factor = |
---|
1474 | sp->cinfo.d.comp_info[i].v_samp_factor = 1; |
---|
1475 | while (++i < sp->cinfo.d.num_components); |
---|
1476 | break; |
---|
1477 | case JCS_CMYK : |
---|
1478 | sp->cinfo.d.comp_info[0].component_id = 'C'; |
---|
1479 | sp->cinfo.d.comp_info[1].component_id = 'Y'; |
---|
1480 | sp->cinfo.d.comp_info[2].component_id = 'M'; |
---|
1481 | sp->cinfo.d.comp_info[3].component_id = 'K'; |
---|
1482 | i = 0; |
---|
1483 | do sp->cinfo.d.comp_info[i].h_samp_factor = |
---|
1484 | sp->cinfo.d.comp_info[i].v_samp_factor = 1; |
---|
1485 | while (++i < sp->cinfo.d.num_components); |
---|
1486 | break; |
---|
1487 | case JCS_YCbCr : |
---|
1488 | i = 0; |
---|
1489 | do |
---|
1490 | { |
---|
1491 | sp->cinfo.d.comp_info[i].component_id = i+1; |
---|
1492 | sp->cinfo.d.comp_info[i].h_samp_factor = |
---|
1493 | sp->cinfo.d.comp_info[i].v_samp_factor = 1; |
---|
1494 | sp->cinfo.d.comp_info[i].quant_tbl_no = |
---|
1495 | sp->cinfo.d.comp_info[i].dc_tbl_no = |
---|
1496 | sp->cinfo.d.comp_info[i].ac_tbl_no = i > 0; |
---|
1497 | } |
---|
1498 | while (++i < sp->cinfo.d.num_components); |
---|
1499 | sp->cinfo.d.comp_info[0].h_samp_factor = sp->h_sampling; |
---|
1500 | sp->cinfo.d.comp_info[0].v_samp_factor = sp->v_sampling; |
---|
1501 | }; |
---|
1502 | sp->cinfo.d.comps_in_scan = sp->cinfo.d.num_components; |
---|
1503 | i = TIFFojpeg_read_header(sp,!sp->is_WANG); |
---|
1504 | sp->cinfo.d.marker->read_markers = save; /* Restore input method */ |
---|
1505 | if (sp->is_WANG) /* Microsoft Wang Imaging for Windows file */ |
---|
1506 | { |
---|
1507 | if (i != JPEG_SUSPENDED) return 0; |
---|
1508 | |
---|
1509 | /* BOGOSITY ALERT! Files generated by Microsoft's Wang Imaging |
---|
1510 | application are a special--and, technically |
---|
1511 | illegal--case. A JPEG SOS marker and rest of the data stream should |
---|
1512 | be located at the end of the file, in a position identified by the |
---|
1513 | 0th Strip offset. |
---|
1514 | */ |
---|
1515 | i = td->td_nstrips - 1; |
---|
1516 | sp->src.next_input_byte = tif->tif_base + td->td_stripoffset[0]; |
---|
1517 | sp->src.bytes_in_buffer = td->td_stripoffset[i] - |
---|
1518 | td->td_stripoffset[0] + td->td_stripbytecount[i]; |
---|
1519 | i = TIFFojpeg_read_header(sp,TRUE); |
---|
1520 | }; |
---|
1521 | if (i != JPEG_HEADER_OK) return 0; |
---|
1522 | }; |
---|
1523 | |
---|
1524 | /* The JPEG Library doesn't seem to be as smart as we are about choosing |
---|
1525 | suitable default input- and output color spaces for decompression, so fix |
---|
1526 | things up here. |
---|
1527 | */ |
---|
1528 | sp->cinfo.d.out_color_space = |
---|
1529 | ((sp->cinfo.d.jpeg_color_space = in_color_space) == JCS_YCbCr) |
---|
1530 | ? (sp->jpegcolormode == JPEGCOLORMODE_RGB ? JCS_RGB : JCS_YCbCr) |
---|
1531 | : JCS_UNKNOWN; /* Suppress color-space handling */ |
---|
1532 | tif->tif_decoderow = tif->tif_decodestrip = tif->tif_decodetile = |
---|
1533 | (sp->cinfo.d.raw_data_out = downsampled_output) |
---|
1534 | ? OJPEGDecodeRaw : OJPEGDecode; |
---|
1535 | if (!TIFFojpeg_start_decompress(sp)) return 0; /* Start JPEG decompressor */ |
---|
1536 | if (downsampled_output) /* allocate downsampled-data buffers */ |
---|
1537 | { |
---|
1538 | if (!alloc_downsampled_buffers(tif,sp->cinfo.d.comp_info, |
---|
1539 | sp->cinfo.d.num_components) |
---|
1540 | ) return 0; |
---|
1541 | sp->scancount = DCTSIZE; /* mark buffer empty */ |
---|
1542 | }; |
---|
1543 | return 1; |
---|
1544 | # undef td |
---|
1545 | } |
---|
1546 | |
---|
1547 | static int |
---|
1548 | OJPEGVSetField(register TIFF *tif,ttag_t tag,va_list ap) |
---|
1549 | { uint32 v32; |
---|
1550 | register OJPEGState *sp = OJState(tif); |
---|
1551 | # define td (&tif->tif_dir) |
---|
1552 | |
---|
1553 | switch (tag) |
---|
1554 | { |
---|
1555 | # ifdef COLORIMETRY_SUPPORT |
---|
1556 | |
---|
1557 | /* If a "ReferenceBlackWhite" TIFF tag appears in the file explicitly, undo |
---|
1558 | any modified default definition that we might have installed below, then |
---|
1559 | install the real one. |
---|
1560 | */ |
---|
1561 | case TIFFTAG_REFERENCEBLACKWHITE : if (td->td_refblackwhite) |
---|
1562 | { |
---|
1563 | _TIFFfree(td->td_refblackwhite); |
---|
1564 | td->td_refblackwhite = 0; |
---|
1565 | }; |
---|
1566 | # endif /* COLORIMETRY_SUPPORT */ |
---|
1567 | default : return |
---|
1568 | (*sp->vsetparent)(tif,tag,ap); |
---|
1569 | # ifdef COLORIMETRY_SUPPORT |
---|
1570 | |
---|
1571 | /* BEWARE OF KLUDGE: Some old-format JPEG-in-TIFF files, including those |
---|
1572 | created by Microsoft's Wang Imaging application, |
---|
1573 | illegally omit a "ReferenceBlackWhite" TIFF tag, even though the TIFF |
---|
1574 | specification's default is intended for the RGB color space and is in- |
---|
1575 | appropriate for the YCbCr color space ordinarily used for JPEG images. |
---|
1576 | Since many TIFF client applications request the value of this tag |
---|
1577 | immediately after a TIFF image directory is parsed, and before any other |
---|
1578 | code in this module receives control, we are forced to fix this problem |
---|
1579 | very early in image-file processing. Fortunately, legal TIFF files are |
---|
1580 | supposed to store their tags in numeric order, so a mandatory |
---|
1581 | "PhotometricInterpretation" tag should always appear before an optional |
---|
1582 | "ReferenceBlackWhite" tag. So, we slyly peek ahead when we discover the |
---|
1583 | desired photometry, by installing modified black and white reference |
---|
1584 | levels. |
---|
1585 | */ |
---|
1586 | case TIFFTAG_PHOTOMETRIC : |
---|
1587 | if ( (v32 = (*sp->vsetparent)(tif,tag,ap)) |
---|
1588 | && td->td_photometric == PHOTOMETRIC_YCBCR |
---|
1589 | ) |
---|
1590 | if (td->td_refblackwhite = _TIFFmalloc(6*sizeof(float))) |
---|
1591 | { register long top = 1 << td->td_bitspersample; |
---|
1592 | |
---|
1593 | td->td_refblackwhite[0] = 0; |
---|
1594 | td->td_refblackwhite[1] = td->td_refblackwhite[3] = |
---|
1595 | td->td_refblackwhite[5] = top - 1; |
---|
1596 | td->td_refblackwhite[2] = td->td_refblackwhite[4] = top >> 1; |
---|
1597 | } |
---|
1598 | else |
---|
1599 | { |
---|
1600 | TIFFError(tif->tif_name,"Cannot define default reference black and white levels"); |
---|
1601 | v32 = 0; |
---|
1602 | }; |
---|
1603 | return v32; |
---|
1604 | # endif /* COLORIMETRY_SUPPORT */ |
---|
1605 | |
---|
1606 | /* BEWARE OF KLUDGE: According to Charles Auer <Bumble731@msn.com>, if our |
---|
1607 | input is a multi-image (multi-directory) JPEG-in-TIFF |
---|
1608 | file created by Microsoft's Wang Imaging application, for some reason |
---|
1609 | the first directory excludes the vendor-specific "WANG PageControl" tag |
---|
1610 | (32934) that we check below, so the only other way to identify these |
---|
1611 | directories is apparently to look for a software-identification tag with |
---|
1612 | the substring, "Wang Labs". Single-image files can apparently pass both |
---|
1613 | tests, which causes no harm here, but what a mess this is! |
---|
1614 | */ |
---|
1615 | case TIFFTAG_SOFTWARE : if ( (v32 = (*sp->vsetparent) |
---|
1616 | (tif,tag,ap) |
---|
1617 | ) |
---|
1618 | && strstr( td->td_software |
---|
1619 | , "Wang Labs" |
---|
1620 | ) |
---|
1621 | ) sp->is_WANG = 1; |
---|
1622 | return v32; |
---|
1623 | case TIFFTAG_JPEGPROC : |
---|
1624 | case TIFFTAG_JPEGIFOFFSET : |
---|
1625 | case TIFFTAG_JPEGIFBYTECOUNT : |
---|
1626 | case TIFFTAG_JPEGRESTARTINTERVAL : |
---|
1627 | case TIFFTAG_JPEGLOSSLESSPREDICTORS: |
---|
1628 | case TIFFTAG_JPEGPOINTTRANSFORM : |
---|
1629 | case TIFFTAG_JPEGQTABLES : |
---|
1630 | case TIFFTAG_JPEGDCTABLES : |
---|
1631 | case TIFFTAG_JPEGACTABLES : |
---|
1632 | case TIFFTAG_WANG_PAGECONTROL : |
---|
1633 | case TIFFTAG_JPEGCOLORMODE : ; |
---|
1634 | }; |
---|
1635 | v32 = va_arg(ap,uint32); /* No. of values in this TIFF record */ |
---|
1636 | |
---|
1637 | /* BEWARE: The following actions apply only if we are reading a "source" TIFF |
---|
1638 | image to be decompressed for a client application program. If we |
---|
1639 | ever enhance this file's CODEC to write "destination" JPEG-in-TIFF images, |
---|
1640 | we'll need an "if"- and another "switch"-statement below, because we'll |
---|
1641 | probably want to store these records' values in some different places. Most |
---|
1642 | of these need not be parsed here in order to decode JPEG bit stream, so we |
---|
1643 | set boolean flags to note that they have been seen, but we otherwise ignore |
---|
1644 | them. |
---|
1645 | */ |
---|
1646 | switch (tag) |
---|
1647 | { JHUFF_TBL **h; |
---|
1648 | float *refbw; |
---|
1649 | |
---|
1650 | /* Validate the JPEG-process code. */ |
---|
1651 | |
---|
1652 | case TIFFTAG_JPEGPROC : |
---|
1653 | switch (v32) |
---|
1654 | { |
---|
1655 | default: TIFFError(tif->tif_name,"Unknown JPEG process"); |
---|
1656 | return 0; |
---|
1657 | case 14: TIFFError(JPEGLib_name, |
---|
1658 | "Does not support lossless Huffman coding"); |
---|
1659 | return 0; |
---|
1660 | case 1: ; |
---|
1661 | }; |
---|
1662 | break; |
---|
1663 | |
---|
1664 | /* The TIFF Version 6.0 specification says that if the value of a TIFF |
---|
1665 | "JPEGInterchangeFormat" record is 0, then we are to behave as if this |
---|
1666 | record were absent; i.e., the data does *not* represent a JPEG Inter- |
---|
1667 | change Format File (JFIF), so don't even set the boolean "I've been |
---|
1668 | here" flag below. Otherwise, the field's value represents the file |
---|
1669 | offset of the JPEG SOI marker. |
---|
1670 | */ |
---|
1671 | case TIFFTAG_JPEGIFOFFSET : |
---|
1672 | if (v32) |
---|
1673 | { |
---|
1674 | sp->src.next_input_byte = tif->tif_base + v32; |
---|
1675 | break; |
---|
1676 | }; |
---|
1677 | return 1; |
---|
1678 | case TIFFTAG_JPEGIFBYTECOUNT : |
---|
1679 | sp->src.bytes_in_buffer = v32; |
---|
1680 | break; |
---|
1681 | |
---|
1682 | /* The TIFF Version 6.0 specification says that if the JPEG "Restart" |
---|
1683 | marker interval is 0, then the data has no "Restart" markers; i.e., we |
---|
1684 | must behave as if this TIFF record were absent. So, don't even set the |
---|
1685 | boolean "I've been here" flag below. |
---|
1686 | */ |
---|
1687 | case TIFFTAG_JPEGRESTARTINTERVAL : |
---|
1688 | if (v32) |
---|
1689 | { |
---|
1690 | sp->cinfo.d.restart_interval = v32; |
---|
1691 | break; |
---|
1692 | }; |
---|
1693 | return 1; |
---|
1694 | |
---|
1695 | /* We have a vector of offsets to quantization tables, so load 'em! */ |
---|
1696 | |
---|
1697 | case TIFFTAG_JPEGQTABLES : |
---|
1698 | if (v32) |
---|
1699 | { uint32 *v; |
---|
1700 | int i; |
---|
1701 | |
---|
1702 | if (v32 > NUM_QUANT_TBLS) |
---|
1703 | { |
---|
1704 | TIFFError(tif->tif_name,"Too many quantization tables"); |
---|
1705 | return 0; |
---|
1706 | }; |
---|
1707 | i = 0; |
---|
1708 | v = va_arg(ap,uint32 *); |
---|
1709 | do /* read quantization table */ |
---|
1710 | { register UINT8 *from = tif->tif_base + *v++; |
---|
1711 | register UINT16 *to = (sp->cinfo.d.quant_tbl_ptrs[i] = |
---|
1712 | jpeg_alloc_quant_table(&sp->cinfo.comm) |
---|
1713 | )->quantval; |
---|
1714 | register int j = DCTSIZE2; |
---|
1715 | |
---|
1716 | do *to++ = *from++; while (--j > 0); |
---|
1717 | } |
---|
1718 | while (++i < v32); |
---|
1719 | sp->jpegtablesmode |= JPEGTABLESMODE_QUANT; |
---|
1720 | }; |
---|
1721 | break; |
---|
1722 | |
---|
1723 | /* We have a vector of offsets to DC Huffman tables, so load 'em! */ |
---|
1724 | |
---|
1725 | case TIFFTAG_JPEGDCTABLES : |
---|
1726 | h = sp->cinfo.d.dc_huff_tbl_ptrs; |
---|
1727 | goto L; |
---|
1728 | |
---|
1729 | /* We have a vector of offsets to AC Huffman tables, so load 'em! */ |
---|
1730 | |
---|
1731 | case TIFFTAG_JPEGACTABLES : |
---|
1732 | h = sp->cinfo.d.ac_huff_tbl_ptrs; |
---|
1733 | L: if (v32) |
---|
1734 | { uint32 *v; |
---|
1735 | int i; |
---|
1736 | |
---|
1737 | if (v32 > NUM_HUFF_TBLS) |
---|
1738 | { |
---|
1739 | TIFFError(tif->tif_name,"Too many Huffman tables"); |
---|
1740 | return 0; |
---|
1741 | }; |
---|
1742 | v = va_arg(ap,uint32 *); |
---|
1743 | i = 0; |
---|
1744 | do /* copy each Huffman table */ |
---|
1745 | { int size = 0; |
---|
1746 | register UINT8 *from = tif->tif_base + *v++, |
---|
1747 | *to = (*h++ = |
---|
1748 | jpeg_alloc_huff_table(&sp->cinfo.comm) |
---|
1749 | )->bits; |
---|
1750 | register int j = sizeof (*h)->bits; |
---|
1751 | |
---|
1752 | /* WARNING: This code relies on the fact that an image file not |
---|
1753 | "memory mapped" was read entirely into a single |
---|
1754 | buffer by "TIFFInitOJPEG()", so we can do a fast memory-to- |
---|
1755 | memory copy here. Each table consists of 16 Bytes, which are |
---|
1756 | suffixed to a 0 Byte when copied, followed by a variable |
---|
1757 | number of Bytes whose length is the sum of the first 16. |
---|
1758 | */ |
---|
1759 | *to++ = 0; |
---|
1760 | while (--j > 0) size += *to++ = *from++; /* Copy 16 Bytes */ |
---|
1761 | if (size > sizeof (*h)->huffval/sizeof *(*h)->huffval) |
---|
1762 | { |
---|
1763 | TIFFError(tif->tif_name,"Huffman table too big"); |
---|
1764 | return 0; |
---|
1765 | }; |
---|
1766 | if ((j = size) > 0) do *to++ = *from++; while (--j > 0); |
---|
1767 | while (++size <= sizeof (*h)->huffval/sizeof *(*h)->huffval) |
---|
1768 | *to++ = 0; /* Zero the rest of the table for cleanliness */ |
---|
1769 | } |
---|
1770 | while (++i < v32); |
---|
1771 | sp->jpegtablesmode |= JPEGTABLESMODE_HUFF; |
---|
1772 | }; |
---|
1773 | break; |
---|
1774 | |
---|
1775 | /* The following vendor-specific TIFF tag occurs in (highly illegal) files |
---|
1776 | generated by MicroSoft's Wang Imaging for Windows application. These |
---|
1777 | can apparently have several "pages", in which case this tag specifies |
---|
1778 | the offset of a "page control" structure, which we don't currently know |
---|
1779 | how to handle. 0 indicates a 1-page image with no "page control", which |
---|
1780 | we make a feeble effort to handle. |
---|
1781 | */ |
---|
1782 | case TIFFTAG_WANG_PAGECONTROL : |
---|
1783 | if (v32 == 0) v32 = -1; |
---|
1784 | sp->is_WANG = v32; |
---|
1785 | tag = TIFFTAG_JPEGPROC+FIELD_WANG_PAGECONTROL-FIELD_JPEGPROC; |
---|
1786 | break; |
---|
1787 | |
---|
1788 | /* This pseudo tag indicates whether we think that our caller is supposed |
---|
1789 | to do YCbCr<->RGB color-space conversion (JPEGCOLORMODE_RAW <=> 0) or |
---|
1790 | whether we must ask the JPEG Library to do it (JPEGCOLORMODE_RGB <=> 1). |
---|
1791 | */ |
---|
1792 | case TIFFTAG_JPEGCOLORMODE : |
---|
1793 | sp->jpegcolormode = v32; |
---|
1794 | |
---|
1795 | /* Mark the image to indicate whether returned data is up-sampled, so |
---|
1796 | that "TIFF{Strip,Tile}Size()" reflect the true amount of data present. |
---|
1797 | */ |
---|
1798 | v32 = tif->tif_flags; /* Save flags temporarily */ |
---|
1799 | tif->tif_flags &= ~TIFF_UPSAMPLED; |
---|
1800 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
---|
1801 | if ( td->td_photometric == PHOTOMETRIC_YCBCR |
---|
1802 | && sp->jpegcolormode == JPEGCOLORMODE_RGB |
---|
1803 | ) tif->tif_flags |= TIFF_UPSAMPLED; |
---|
1804 | else |
---|
1805 | if ( (td->td_ycbcrsubsampling[1]<<16|td->td_ycbcrsubsampling[0]) |
---|
1806 | != (1 << 16 | 1) |
---|
1807 | ) /* XXX what about up-sampling? */; |
---|
1808 | |
---|
1809 | /* If the up-sampling state changed, re-calculate tile size. */ |
---|
1810 | |
---|
1811 | if ((tif->tif_flags ^ v32) & TIFF_UPSAMPLED) |
---|
1812 | { |
---|
1813 | tif->tif_tilesize = TIFFTileSize(tif); |
---|
1814 | tif->tif_flags |= TIFF_DIRTYDIRECT; |
---|
1815 | }; |
---|
1816 | return 1; |
---|
1817 | }; |
---|
1818 | TIFFSetFieldBit(tif,tag-TIFFTAG_JPEGPROC+FIELD_JPEGPROC); |
---|
1819 | return 1; |
---|
1820 | # undef td |
---|
1821 | } |
---|
1822 | |
---|
1823 | static int |
---|
1824 | OJPEGVGetField(register TIFF *tif,ttag_t tag,va_list ap) |
---|
1825 | { register OJPEGState *sp = OJState(tif); |
---|
1826 | |
---|
1827 | switch (tag) |
---|
1828 | { |
---|
1829 | |
---|
1830 | /* If this file has managed to synthesize a set of consolidated "metadata" |
---|
1831 | tables for the current (post-TIFF Version 6.0 specification) JPEG-in- |
---|
1832 | TIFF encapsulation strategy, then tell our caller about them; otherwise, |
---|
1833 | keep mum. |
---|
1834 | */ |
---|
1835 | case TIFFTAG_JPEGTABLES : |
---|
1836 | if (sp->jpegtables_length) /* we have "new"-style JPEG tables */ |
---|
1837 | { |
---|
1838 | *va_arg(ap,uint32 *) = sp->jpegtables_length; |
---|
1839 | *va_arg(ap,char **) = sp->jpegtables; |
---|
1840 | return 1; |
---|
1841 | }; |
---|
1842 | |
---|
1843 | /* This pseudo tag indicates whether we think that our caller is supposed |
---|
1844 | to do YCbCr<->RGB color-space conversion (JPEGCOLORMODE_RAW <=> 0) or |
---|
1845 | whether we must ask the JPEG Library to do it (JPEGCOLORMODE_RGB <=> 1). |
---|
1846 | */ |
---|
1847 | case TIFFTAG_JPEGCOLORMODE : |
---|
1848 | *va_arg(ap,uint32 *) = sp->jpegcolormode; |
---|
1849 | return 1; |
---|
1850 | |
---|
1851 | /* The following tags are defined by the TIFF Version 6.0 specification |
---|
1852 | and are obsolete. If our caller asks for information about them, do not |
---|
1853 | return anything, even if we parsed them in an old-format "source" image. |
---|
1854 | */ |
---|
1855 | case TIFFTAG_JPEGPROC : |
---|
1856 | case TIFFTAG_JPEGIFOFFSET : |
---|
1857 | case TIFFTAG_JPEGIFBYTECOUNT : |
---|
1858 | case TIFFTAG_JPEGRESTARTINTERVAL : |
---|
1859 | case TIFFTAG_JPEGLOSSLESSPREDICTORS: |
---|
1860 | case TIFFTAG_JPEGPOINTTRANSFORM : |
---|
1861 | case TIFFTAG_JPEGQTABLES : |
---|
1862 | case TIFFTAG_JPEGDCTABLES : |
---|
1863 | case TIFFTAG_JPEGACTABLES : return 0; |
---|
1864 | }; |
---|
1865 | return (*sp->vgetparent)(tif,tag,ap); |
---|
1866 | } |
---|
1867 | |
---|
1868 | /*ARGSUSED*/ static void |
---|
1869 | OJPEGPrintDir(register TIFF *tif,FILE *fd,long flags) |
---|
1870 | { register OJPEGState *sp = OJState(tif); |
---|
1871 | |
---|
1872 | if (sp->jpegtables_length) |
---|
1873 | fprintf(fd," JPEG Table Data: <present>, %lu bytes\n", |
---|
1874 | sp->jpegtables_length); |
---|
1875 | } |
---|
1876 | |
---|
1877 | static uint32 |
---|
1878 | OJPEGDefaultStripSize(register TIFF *tif,register uint32 s) |
---|
1879 | { register OJPEGState *sp = OJState(tif); |
---|
1880 | # define td (&tif->tif_dir) |
---|
1881 | |
---|
1882 | if ((s = (*sp->defsparent)(tif,s)) < td->td_imagelength) |
---|
1883 | s = TIFFroundup(s,td->td_ycbcrsubsampling[1]*DCTSIZE); |
---|
1884 | return s; |
---|
1885 | # undef td |
---|
1886 | } |
---|
1887 | |
---|
1888 | static void |
---|
1889 | OJPEGDefaultTileSize(register TIFF *tif,register uint32 *tw,register uint32 *th) |
---|
1890 | { register OJPEGState *sp = OJState(tif); |
---|
1891 | # define td (&tif->tif_dir) |
---|
1892 | |
---|
1893 | (*sp->deftparent)(tif,tw,th); |
---|
1894 | *tw = TIFFroundup(*tw,td->td_ycbcrsubsampling[0]*DCTSIZE); |
---|
1895 | *th = TIFFroundup(*th,td->td_ycbcrsubsampling[1]*DCTSIZE); |
---|
1896 | # undef td |
---|
1897 | } |
---|
1898 | |
---|
1899 | static void |
---|
1900 | OJPEGCleanUp(register TIFF *tif) |
---|
1901 | { register OJPEGState *sp; |
---|
1902 | |
---|
1903 | if (sp = OJState(tif)) |
---|
1904 | { |
---|
1905 | TIFFojpeg_destroy(sp); /* Release JPEG Library variables */ |
---|
1906 | if (sp->jpegtables) _TIFFfree(sp->jpegtables); |
---|
1907 | |
---|
1908 | /* If the image file isn't "memory mapped" and we read it all into a |
---|
1909 | single, large memory buffer, free the buffer now. |
---|
1910 | */ |
---|
1911 | if (!isMapped(tif) && tif->tif_base) /* free whole-file buffer */ |
---|
1912 | { |
---|
1913 | _TIFFfree(tif->tif_base); |
---|
1914 | tif->tif_base = 0; |
---|
1915 | tif->tif_size = 0; |
---|
1916 | }; |
---|
1917 | _TIFFfree(sp); /* Release local variables */ |
---|
1918 | tif->tif_data = 0; |
---|
1919 | } |
---|
1920 | } |
---|
1921 | |
---|
1922 | int |
---|
1923 | TIFFInitOJPEG(register TIFF *tif,int scheme) |
---|
1924 | { register OJPEGState *sp; |
---|
1925 | # define td (&tif->tif_dir) |
---|
1926 | # ifndef never |
---|
1927 | |
---|
1928 | /* This module supports a decompression-only CODEC, which is intended strictly |
---|
1929 | for viewing old image files using the obsolete JPEG-in-TIFF encapsulation |
---|
1930 | specified by the TIFF Version 6.0 specification. It does not, and never |
---|
1931 | should, support compression for new images. If a client application asks us |
---|
1932 | to, refuse and complain loudly! |
---|
1933 | */ |
---|
1934 | if (tif->tif_mode != O_RDONLY) return _notSupported(tif); |
---|
1935 | # endif /* never */ |
---|
1936 | if (!isMapped(tif)) |
---|
1937 | { |
---|
1938 | |
---|
1939 | /* BEWARE OF KLUDGE: If our host operating-system doesn't let an image |
---|
1940 | file be "memory mapped", then we want to read the |
---|
1941 | entire file into a single (possibly large) memory buffer as if it had |
---|
1942 | been "memory mapped". Although this is likely to waste space, because |
---|
1943 | analysis of the file's content might cause parts of it to be read into |
---|
1944 | smaller buffers duplicatively, it appears to be the lesser of several |
---|
1945 | evils. Very old JPEG-in-TIFF encapsulations aren't guaranteed to be |
---|
1946 | JFIF bit streams, or to have a TIFF "JPEGTables" record or much other |
---|
1947 | "metadata" to help us locate the decoding tables and entropy-coded data, |
---|
1948 | so we're likely do a lot of random-access grokking around, and we must |
---|
1949 | ultimately tell the JPEG Library to sequentially scan much of the file |
---|
1950 | anyway. This is all likely to be easier if we use "brute force" to |
---|
1951 | read the entire file, once, and don't use incremental disc I/O. If our |
---|
1952 | client application tries to process a file so big that we can't buffer |
---|
1953 | it entirely, then tough shit: we'll give up and exit! |
---|
1954 | */ |
---|
1955 | if (!(tif->tif_base = _TIFFmalloc(tif->tif_size=TIFFGetFileSize(tif)))) |
---|
1956 | { |
---|
1957 | TIFFError(tif->tif_name,"Cannot allocate file buffer"); |
---|
1958 | return 0; |
---|
1959 | }; |
---|
1960 | if (!SeekOK(tif,0) || !ReadOK(tif,tif->tif_base,tif->tif_size)) |
---|
1961 | { |
---|
1962 | TIFFError(tif->tif_name,"Cannot read file"); |
---|
1963 | return 0; |
---|
1964 | } |
---|
1965 | }; |
---|
1966 | |
---|
1967 | /* Allocate storage for this module's per-file variables. */ |
---|
1968 | |
---|
1969 | if (!(tif->tif_data = (tidata_t)_TIFFmalloc(sizeof *sp))) |
---|
1970 | { |
---|
1971 | TIFFError("TIFFInitOJPEG","No space for JPEG state block"); |
---|
1972 | return 0; |
---|
1973 | }; |
---|
1974 | (sp = OJState(tif))->tif = tif; /* Initialize reverse pointer */ |
---|
1975 | if (!TIFFojpeg_create_decompress(sp)) return 0; /* Init. JPEG Library */ |
---|
1976 | |
---|
1977 | /* If the image file doesn't have "JPEGInterchangeFormat[Length]" TIFF records |
---|
1978 | to guide us, we have few clues about where its encapsulated JPEG bit stream |
---|
1979 | is located, so establish intelligent defaults: If the Image File Directory |
---|
1980 | doesn't immediately follow the TIFF header, assume that the JPEG data lies |
---|
1981 | in between; otherwise, assume that it follows the Image File Directory. |
---|
1982 | */ |
---|
1983 | sp->src.next_input_byte = tif->tif_base + tif->tif_diroff; /* Default */ |
---|
1984 | if (tif->tif_header.tiff_diroff > sizeof tif->tif_header) |
---|
1985 | { |
---|
1986 | sp->src.bytes_in_buffer = tif->tif_header.tiff_diroff |
---|
1987 | - sizeof tif->tif_header; |
---|
1988 | sp->src.next_input_byte = sp->src.next_input_byte |
---|
1989 | - sp->src.bytes_in_buffer; |
---|
1990 | } |
---|
1991 | else /* this case is ugly! */ |
---|
1992 | { uint16 dircount; |
---|
1993 | |
---|
1994 | dircount = *(uint16 *)sp->src.next_input_byte; |
---|
1995 | if (tif->tif_flags & TIFF_SWAB) TIFFSwabShort(&dircount); |
---|
1996 | sp->src.next_input_byte += dircount * sizeof(TIFFDirEntry) |
---|
1997 | + sizeof dircount; |
---|
1998 | sp->src.bytes_in_buffer = tif->tif_base + tif->tif_nextdiroff |
---|
1999 | - sp->src.next_input_byte; |
---|
2000 | }; |
---|
2001 | |
---|
2002 | /* Install CODEC-specific tag information and override default TIFF Library |
---|
2003 | "method" subroutines with our own, CODEC-specific methods. Like all good |
---|
2004 | members of an object-class, we save some of these subroutine pointers for |
---|
2005 | "fall back" in case our own methods fail. |
---|
2006 | */ |
---|
2007 | _TIFFMergeFieldInfo(tif,ojpegFieldInfo, |
---|
2008 | sizeof ojpegFieldInfo/sizeof *ojpegFieldInfo); |
---|
2009 | sp->defsparent = tif->tif_defstripsize; |
---|
2010 | sp->deftparent = tif->tif_deftilesize; |
---|
2011 | sp->vgetparent = tif->tif_vgetfield; |
---|
2012 | sp->vsetparent = tif->tif_vsetfield; |
---|
2013 | tif->tif_defstripsize = OJPEGDefaultStripSize; |
---|
2014 | tif->tif_deftilesize = OJPEGDefaultTileSize; |
---|
2015 | tif->tif_vgetfield = OJPEGVGetField; |
---|
2016 | tif->tif_vsetfield = OJPEGVSetField; |
---|
2017 | tif->tif_printdir = OJPEGPrintDir; |
---|
2018 | # ifdef never |
---|
2019 | tif->tif_setupencode = OJPEGSetupEncode; |
---|
2020 | tif->tif_preencode = OJPEGPreEncode; |
---|
2021 | tif->tif_postencode = OJPEGPostEncode; |
---|
2022 | # else /* well, hardly ever */ |
---|
2023 | tif->tif_setupencode = tif->tif_postencode = _notSupported; |
---|
2024 | tif->tif_preencode = (TIFFPreMethod)_notSupported; |
---|
2025 | # endif /* never */ |
---|
2026 | tif->tif_setupdecode = OJPEGSetupDecode; |
---|
2027 | tif->tif_predecode = OJPEGPreDecode; |
---|
2028 | tif->tif_cleanup = OJPEGCleanUp; |
---|
2029 | |
---|
2030 | /* Initialize other CODEC-specific variables requiring default values. */ |
---|
2031 | |
---|
2032 | tif->tif_flags |= TIFF_NOBITREV; /* No bit-reversal within data bytes */ |
---|
2033 | sp->is_WANG = 0; /* Assume not a Microsoft Wang Imaging file by default */ |
---|
2034 | sp->jpegtables = 0; /* No "new"-style JPEG tables synthesized yet */ |
---|
2035 | sp->jpegtables_length = 0; |
---|
2036 | sp->jpegquality = 75; /* Default IJG quality */ |
---|
2037 | sp->jpegcolormode = JPEGCOLORMODE_RAW; |
---|
2038 | sp->jpegtablesmode = 0; /* No tables found yet */ |
---|
2039 | return 1; |
---|
2040 | # undef td |
---|
2041 | } |
---|
2042 | #endif /* OJPEG_SUPPORT */ |
---|