]>
Commit | Line | Data |
---|---|---|
b47c832e RR |
1 | /* $Header$ */ |
2 | ||
3 | /* | |
4 | * Copyright (c) 1994-1997 Sam Leffler | |
5 | * Copyright (c) 1994-1997 Silicon Graphics, Inc. | |
6 | * | |
7 | * Permission to use, copy, modify, distribute, and sell this software and | |
8 | * its documentation for any purpose is hereby granted without fee, provided | |
9 | * that (i) the above copyright notices and this permission notice appear in | |
10 | * all copies of the software and related documentation, and (ii) the names of | |
11 | * Sam Leffler and Silicon Graphics may not be used in any advertising or | |
12 | * publicity relating to the software without the specific, prior written | |
13 | * permission of Sam Leffler and Silicon Graphics. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, | |
16 | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY | |
17 | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. | |
18 | * | |
19 | * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR | |
20 | * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, | |
21 | * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
22 | * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF | |
23 | * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
24 | * OF THIS SOFTWARE. | |
25 | */ | |
26 | ||
27 | #include "tiffiop.h" | |
28 | #ifdef JPEG_SUPPORT | |
29 | /* | |
30 | * TIFF Library | |
31 | * | |
32 | * JPEG Compression support per TIFF Technical Note #2 | |
33 | * (*not* per the original TIFF 6.0 spec). | |
34 | * | |
35 | * This file is simply an interface to the libjpeg library written by | |
36 | * the Independent JPEG Group. You need release 5 or later of the IJG | |
37 | * code, which you can find on the Internet at ftp.uu.net:/graphics/jpeg/. | |
38 | * | |
39 | * Contributed by Tom Lane <tgl@sss.pgh.pa.us>. | |
40 | */ | |
41 | #include <assert.h> | |
42 | #include <stdio.h> | |
43 | #include <setjmp.h> | |
00cb87b4 VZ |
44 | |
45 | int TIFFFillStrip(TIFF*, tstrip_t); | |
46 | int TIFFFillTile(TIFF*, ttile_t); | |
47 | ||
48 | /* We undefine FAR to avoid conflict with JPEG definition */ | |
49 | ||
50 | #ifdef FAR | |
51 | #undef FAR | |
52 | #endif | |
53 | ||
54 | /* | |
55 | The windows RPCNDR.H file defines boolean, but defines it with the | |
56 | wrong size. So we declare HAVE_BOOLEAN so that the jpeg include file | |
57 | won't try to typedef boolean, but #define it to override the rpcndr.h | |
58 | definition. | |
59 | ||
60 | http://bugzilla.remotesensing.org/show_bug.cgi?id=188 | |
61 | */ | |
62 | #if defined(__RPCNDR_H__) | |
63 | #define HAVE_BOOLEAN | |
64 | #define boolean unsigned int | |
65 | #endif | |
66 | ||
b47c832e RR |
67 | #include "jpeglib.h" |
68 | #include "jerror.h" | |
69 | ||
e26a41ba MW |
70 | #ifndef HAVE_WXJPEG_BOOLEAN |
71 | typedef boolean wxjpeg_boolean; | |
72 | #endif | |
73 | ||
b47c832e RR |
74 | /* |
75 | * On some machines it may be worthwhile to use _setjmp or sigsetjmp | |
76 | * in place of plain setjmp. These macros will make it easier. | |
77 | */ | |
78 | #define SETJMP(jbuf) setjmp(jbuf) | |
79 | #define LONGJMP(jbuf,code) longjmp(jbuf,code) | |
80 | #define JMP_BUF jmp_buf | |
81 | ||
82 | typedef struct jpeg_destination_mgr jpeg_destination_mgr; | |
83 | typedef struct jpeg_source_mgr jpeg_source_mgr; | |
84 | typedef struct jpeg_error_mgr jpeg_error_mgr; | |
85 | ||
86 | /* | |
87 | * State block for each open TIFF file using | |
88 | * libjpeg to do JPEG compression/decompression. | |
89 | * | |
90 | * libjpeg's visible state is either a jpeg_compress_struct | |
91 | * or jpeg_decompress_struct depending on which way we | |
92 | * are going. comm can be used to refer to the fields | |
93 | * which are common to both. | |
94 | * | |
95 | * NB: cinfo is required to be the first member of JPEGState, | |
96 | * so we can safely cast JPEGState* -> jpeg_xxx_struct* | |
97 | * and vice versa! | |
98 | */ | |
99 | typedef struct { | |
100 | union { | |
101 | struct jpeg_compress_struct c; | |
102 | struct jpeg_decompress_struct d; | |
103 | struct jpeg_common_struct comm; | |
104 | } cinfo; /* NB: must be first */ | |
00cb87b4 VZ |
105 | int cinfo_initialized; |
106 | ||
b47c832e RR |
107 | jpeg_error_mgr err; /* libjpeg error manager */ |
108 | JMP_BUF exit_jmpbuf; /* for catching libjpeg failures */ | |
109 | /* | |
110 | * The following two members could be a union, but | |
111 | * they're small enough that it's not worth the effort. | |
112 | */ | |
113 | jpeg_destination_mgr dest; /* data dest for compression */ | |
114 | jpeg_source_mgr src; /* data source for decompression */ | |
115 | /* private state */ | |
116 | TIFF* tif; /* back link needed by some code */ | |
117 | uint16 photometric; /* copy of PhotometricInterpretation */ | |
118 | uint16 h_sampling; /* luminance sampling factors */ | |
119 | uint16 v_sampling; | |
120 | tsize_t bytesperline; /* decompressed bytes per scanline */ | |
121 | /* pointers to intermediate buffers when processing downsampled data */ | |
122 | JSAMPARRAY ds_buffer[MAX_COMPONENTS]; | |
123 | int scancount; /* number of "scanlines" accumulated */ | |
124 | int samplesperclump; | |
125 | ||
126 | TIFFVGetMethod vgetparent; /* super-class method */ | |
127 | TIFFVSetMethod vsetparent; /* super-class method */ | |
128 | TIFFStripMethod defsparent; /* super-class method */ | |
129 | TIFFTileMethod deftparent; /* super-class method */ | |
130 | /* pseudo-tag fields */ | |
131 | void* jpegtables; /* JPEGTables tag value, or NULL */ | |
132 | uint32 jpegtables_length; /* number of bytes in same */ | |
133 | int jpegquality; /* Compression quality level */ | |
134 | int jpegcolormode; /* Auto RGB<=>YCbCr convert? */ | |
135 | int jpegtablesmode; /* What to put in JPEGTables */ | |
00cb87b4 VZ |
136 | |
137 | int ycbcrsampling_fetched; | |
b47c832e RR |
138 | } JPEGState; |
139 | ||
140 | #define JState(tif) ((JPEGState*)(tif)->tif_data) | |
141 | ||
142 | static int JPEGDecode(TIFF*, tidata_t, tsize_t, tsample_t); | |
143 | static int JPEGDecodeRaw(TIFF*, tidata_t, tsize_t, tsample_t); | |
144 | static int JPEGEncode(TIFF*, tidata_t, tsize_t, tsample_t); | |
145 | static int JPEGEncodeRaw(TIFF*, tidata_t, tsize_t, tsample_t); | |
00cb87b4 | 146 | static int JPEGInitializeLibJPEG( TIFF * tif ); |
b47c832e RR |
147 | |
148 | #define FIELD_JPEGTABLES (FIELD_CODEC+0) | |
149 | ||
150 | static const TIFFFieldInfo jpegFieldInfo[] = { | |
151 | { TIFFTAG_JPEGTABLES, -1,-1, TIFF_UNDEFINED, FIELD_JPEGTABLES, | |
152 | FALSE, TRUE, "JPEGTables" }, | |
153 | { TIFFTAG_JPEGQUALITY, 0, 0, TIFF_ANY, FIELD_PSEUDO, | |
154 | TRUE, FALSE, "" }, | |
155 | { TIFFTAG_JPEGCOLORMODE, 0, 0, TIFF_ANY, FIELD_PSEUDO, | |
156 | FALSE, FALSE, "" }, | |
157 | { TIFFTAG_JPEGTABLESMODE, 0, 0, TIFF_ANY, FIELD_PSEUDO, | |
158 | FALSE, FALSE, "" }, | |
159 | }; | |
160 | #define N(a) (sizeof (a) / sizeof (a[0])) | |
161 | ||
162 | /* | |
163 | * libjpeg interface layer. | |
164 | * | |
165 | * We use setjmp/longjmp to return control to libtiff | |
166 | * when a fatal error is encountered within the JPEG | |
167 | * library. We also direct libjpeg error and warning | |
168 | * messages through the appropriate libtiff handlers. | |
169 | */ | |
170 | ||
171 | /* | |
172 | * Error handling routines (these replace corresponding | |
173 | * IJG routines from jerror.c). These are used for both | |
174 | * compression and decompression. | |
175 | */ | |
176 | static void | |
177 | TIFFjpeg_error_exit(j_common_ptr cinfo) | |
178 | { | |
179 | JPEGState *sp = (JPEGState *) cinfo; /* NB: cinfo assumed first */ | |
180 | char buffer[JMSG_LENGTH_MAX]; | |
181 | ||
182 | (*cinfo->err->format_message) (cinfo, buffer); | |
183 | TIFFError("JPEGLib", buffer); /* display the error message */ | |
184 | jpeg_abort(cinfo); /* clean up libjpeg state */ | |
185 | LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */ | |
186 | } | |
187 | ||
188 | /* | |
189 | * This routine is invoked only for warning messages, | |
190 | * since error_exit does its own thing and trace_level | |
191 | * is never set > 0. | |
192 | */ | |
193 | static void | |
194 | TIFFjpeg_output_message(j_common_ptr cinfo) | |
195 | { | |
196 | char buffer[JMSG_LENGTH_MAX]; | |
197 | ||
198 | (*cinfo->err->format_message) (cinfo, buffer); | |
199 | TIFFWarning("JPEGLib", buffer); | |
200 | } | |
201 | ||
202 | /* | |
203 | * Interface routines. This layer of routines exists | |
204 | * primarily to limit side-effects from using setjmp. | |
205 | * Also, normal/error returns are converted into return | |
206 | * values per libtiff practice. | |
207 | */ | |
208 | #define CALLJPEG(sp, fail, op) (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op)) | |
209 | #define CALLVJPEG(sp, op) CALLJPEG(sp, 0, ((op),1)) | |
210 | ||
211 | static int | |
212 | TIFFjpeg_create_compress(JPEGState* sp) | |
213 | { | |
214 | /* initialize JPEG error handling */ | |
215 | sp->cinfo.c.err = jpeg_std_error(&sp->err); | |
216 | sp->err.error_exit = TIFFjpeg_error_exit; | |
217 | sp->err.output_message = TIFFjpeg_output_message; | |
218 | ||
219 | return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c)); | |
220 | } | |
221 | ||
222 | static int | |
223 | TIFFjpeg_create_decompress(JPEGState* sp) | |
224 | { | |
225 | /* initialize JPEG error handling */ | |
226 | sp->cinfo.d.err = jpeg_std_error(&sp->err); | |
227 | sp->err.error_exit = TIFFjpeg_error_exit; | |
228 | sp->err.output_message = TIFFjpeg_output_message; | |
229 | ||
230 | return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d)); | |
231 | } | |
232 | ||
233 | static int | |
234 | TIFFjpeg_set_defaults(JPEGState* sp) | |
235 | { | |
236 | return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c)); | |
237 | } | |
238 | ||
239 | static int | |
240 | TIFFjpeg_set_colorspace(JPEGState* sp, J_COLOR_SPACE colorspace) | |
241 | { | |
242 | return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace)); | |
243 | } | |
244 | ||
245 | static int | |
e26a41ba | 246 | TIFFjpeg_set_quality(JPEGState* sp, int quality, wxjpeg_boolean force_baseline) |
b47c832e RR |
247 | { |
248 | return CALLVJPEG(sp, | |
249 | jpeg_set_quality(&sp->cinfo.c, quality, force_baseline)); | |
250 | } | |
251 | ||
252 | static int | |
e26a41ba | 253 | TIFFjpeg_suppress_tables(JPEGState* sp, wxjpeg_boolean suppress) |
b47c832e RR |
254 | { |
255 | return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress)); | |
256 | } | |
257 | ||
258 | static int | |
e26a41ba | 259 | TIFFjpeg_start_compress(JPEGState* sp, wxjpeg_boolean write_all_tables) |
b47c832e RR |
260 | { |
261 | return CALLVJPEG(sp, | |
262 | jpeg_start_compress(&sp->cinfo.c, write_all_tables)); | |
263 | } | |
264 | ||
265 | static int | |
266 | TIFFjpeg_write_scanlines(JPEGState* sp, JSAMPARRAY scanlines, int num_lines) | |
267 | { | |
268 | return CALLJPEG(sp, -1, (int) jpeg_write_scanlines(&sp->cinfo.c, | |
269 | scanlines, (JDIMENSION) num_lines)); | |
270 | } | |
271 | ||
272 | static int | |
273 | TIFFjpeg_write_raw_data(JPEGState* sp, JSAMPIMAGE data, int num_lines) | |
274 | { | |
275 | return CALLJPEG(sp, -1, (int) jpeg_write_raw_data(&sp->cinfo.c, | |
276 | data, (JDIMENSION) num_lines)); | |
277 | } | |
278 | ||
279 | static int | |
280 | TIFFjpeg_finish_compress(JPEGState* sp) | |
281 | { | |
282 | return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c)); | |
283 | } | |
284 | ||
285 | static int | |
286 | TIFFjpeg_write_tables(JPEGState* sp) | |
287 | { | |
288 | return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c)); | |
289 | } | |
290 | ||
291 | static int | |
e26a41ba | 292 | TIFFjpeg_read_header(JPEGState* sp, wxjpeg_boolean require_image) |
b47c832e RR |
293 | { |
294 | return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image)); | |
295 | } | |
296 | ||
297 | static int | |
298 | TIFFjpeg_start_decompress(JPEGState* sp) | |
299 | { | |
300 | return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d)); | |
301 | } | |
302 | ||
303 | static int | |
304 | TIFFjpeg_read_scanlines(JPEGState* sp, JSAMPARRAY scanlines, int max_lines) | |
305 | { | |
306 | return CALLJPEG(sp, -1, (int) jpeg_read_scanlines(&sp->cinfo.d, | |
307 | scanlines, (JDIMENSION) max_lines)); | |
308 | } | |
309 | ||
310 | static int | |
311 | TIFFjpeg_read_raw_data(JPEGState* sp, JSAMPIMAGE data, int max_lines) | |
312 | { | |
313 | return CALLJPEG(sp, -1, (int) jpeg_read_raw_data(&sp->cinfo.d, | |
314 | data, (JDIMENSION) max_lines)); | |
315 | } | |
316 | ||
317 | static int | |
318 | TIFFjpeg_finish_decompress(JPEGState* sp) | |
319 | { | |
320 | return CALLJPEG(sp, -1, (int) jpeg_finish_decompress(&sp->cinfo.d)); | |
321 | } | |
322 | ||
323 | static int | |
324 | TIFFjpeg_abort(JPEGState* sp) | |
325 | { | |
326 | return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm)); | |
327 | } | |
328 | ||
329 | static int | |
330 | TIFFjpeg_destroy(JPEGState* sp) | |
331 | { | |
332 | return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm)); | |
333 | } | |
334 | ||
335 | static JSAMPARRAY | |
336 | TIFFjpeg_alloc_sarray(JPEGState* sp, int pool_id, | |
337 | JDIMENSION samplesperrow, JDIMENSION numrows) | |
338 | { | |
339 | return CALLJPEG(sp, (JSAMPARRAY) NULL, | |
340 | (*sp->cinfo.comm.mem->alloc_sarray) | |
341 | (&sp->cinfo.comm, pool_id, samplesperrow, numrows)); | |
342 | } | |
343 | ||
344 | /* | |
345 | * JPEG library destination data manager. | |
346 | * These routines direct compressed data from libjpeg into the | |
347 | * libtiff output buffer. | |
348 | */ | |
349 | ||
350 | static void | |
351 | std_init_destination(j_compress_ptr cinfo) | |
352 | { | |
353 | JPEGState* sp = (JPEGState*) cinfo; | |
354 | TIFF* tif = sp->tif; | |
355 | ||
356 | sp->dest.next_output_byte = (JOCTET*) tif->tif_rawdata; | |
357 | sp->dest.free_in_buffer = (size_t) tif->tif_rawdatasize; | |
358 | } | |
359 | ||
e26a41ba | 360 | static wxjpeg_boolean |
b47c832e RR |
361 | std_empty_output_buffer(j_compress_ptr cinfo) |
362 | { | |
363 | JPEGState* sp = (JPEGState*) cinfo; | |
364 | TIFF* tif = sp->tif; | |
365 | ||
366 | /* the entire buffer has been filled */ | |
367 | tif->tif_rawcc = tif->tif_rawdatasize; | |
368 | TIFFFlushData1(tif); | |
369 | sp->dest.next_output_byte = (JOCTET*) tif->tif_rawdata; | |
370 | sp->dest.free_in_buffer = (size_t) tif->tif_rawdatasize; | |
371 | ||
372 | return (TRUE); | |
373 | } | |
374 | ||
375 | static void | |
376 | std_term_destination(j_compress_ptr cinfo) | |
377 | { | |
378 | JPEGState* sp = (JPEGState*) cinfo; | |
379 | TIFF* tif = sp->tif; | |
380 | ||
381 | tif->tif_rawcp = (tidata_t) sp->dest.next_output_byte; | |
382 | tif->tif_rawcc = | |
383 | tif->tif_rawdatasize - (tsize_t) sp->dest.free_in_buffer; | |
384 | /* NB: libtiff does the final buffer flush */ | |
385 | } | |
386 | ||
387 | static void | |
388 | TIFFjpeg_data_dest(JPEGState* sp, TIFF* tif) | |
389 | { | |
390 | (void) tif; | |
391 | sp->cinfo.c.dest = &sp->dest; | |
392 | sp->dest.init_destination = std_init_destination; | |
393 | sp->dest.empty_output_buffer = std_empty_output_buffer; | |
394 | sp->dest.term_destination = std_term_destination; | |
395 | } | |
396 | ||
397 | /* | |
398 | * Alternate destination manager for outputting to JPEGTables field. | |
399 | */ | |
400 | ||
401 | static void | |
402 | tables_init_destination(j_compress_ptr cinfo) | |
403 | { | |
404 | JPEGState* sp = (JPEGState*) cinfo; | |
405 | ||
406 | /* while building, jpegtables_length is allocated buffer size */ | |
407 | sp->dest.next_output_byte = (JOCTET*) sp->jpegtables; | |
408 | sp->dest.free_in_buffer = (size_t) sp->jpegtables_length; | |
409 | } | |
410 | ||
e26a41ba | 411 | static wxjpeg_boolean |
b47c832e RR |
412 | tables_empty_output_buffer(j_compress_ptr cinfo) |
413 | { | |
414 | JPEGState* sp = (JPEGState*) cinfo; | |
415 | void* newbuf; | |
416 | ||
417 | /* the entire buffer has been filled; enlarge it by 1000 bytes */ | |
418 | newbuf = _TIFFrealloc((tdata_t) sp->jpegtables, | |
419 | (tsize_t) (sp->jpegtables_length + 1000)); | |
420 | if (newbuf == NULL) | |
421 | ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 100); | |
422 | sp->dest.next_output_byte = (JOCTET*) newbuf + sp->jpegtables_length; | |
423 | sp->dest.free_in_buffer = (size_t) 1000; | |
424 | sp->jpegtables = newbuf; | |
425 | sp->jpegtables_length += 1000; | |
426 | return (TRUE); | |
427 | } | |
428 | ||
429 | static void | |
430 | tables_term_destination(j_compress_ptr cinfo) | |
431 | { | |
432 | JPEGState* sp = (JPEGState*) cinfo; | |
433 | ||
434 | /* set tables length to number of bytes actually emitted */ | |
435 | sp->jpegtables_length -= sp->dest.free_in_buffer; | |
436 | } | |
437 | ||
438 | static int | |
439 | TIFFjpeg_tables_dest(JPEGState* sp, TIFF* tif) | |
440 | { | |
441 | (void) tif; | |
442 | /* | |
443 | * Allocate a working buffer for building tables. | |
444 | * Initial size is 1000 bytes, which is usually adequate. | |
445 | */ | |
446 | if (sp->jpegtables) | |
447 | _TIFFfree(sp->jpegtables); | |
448 | sp->jpegtables_length = 1000; | |
449 | sp->jpegtables = (void*) _TIFFmalloc((tsize_t) sp->jpegtables_length); | |
450 | if (sp->jpegtables == NULL) { | |
451 | sp->jpegtables_length = 0; | |
452 | TIFFError("TIFFjpeg_tables_dest", "No space for JPEGTables"); | |
453 | return (0); | |
454 | } | |
455 | sp->cinfo.c.dest = &sp->dest; | |
456 | sp->dest.init_destination = tables_init_destination; | |
457 | sp->dest.empty_output_buffer = tables_empty_output_buffer; | |
458 | sp->dest.term_destination = tables_term_destination; | |
459 | return (1); | |
460 | } | |
461 | ||
462 | /* | |
463 | * JPEG library source data manager. | |
464 | * These routines supply compressed data to libjpeg. | |
465 | */ | |
466 | ||
467 | static void | |
468 | std_init_source(j_decompress_ptr cinfo) | |
469 | { | |
470 | JPEGState* sp = (JPEGState*) cinfo; | |
471 | TIFF* tif = sp->tif; | |
472 | ||
473 | sp->src.next_input_byte = (const JOCTET*) tif->tif_rawdata; | |
474 | sp->src.bytes_in_buffer = (size_t) tif->tif_rawcc; | |
475 | } | |
476 | ||
e26a41ba | 477 | static wxjpeg_boolean |
b47c832e RR |
478 | std_fill_input_buffer(j_decompress_ptr cinfo) |
479 | { | |
480 | JPEGState* sp = (JPEGState* ) cinfo; | |
481 | static const JOCTET dummy_EOI[2] = { 0xFF, JPEG_EOI }; | |
482 | ||
483 | /* | |
484 | * Should never get here since entire strip/tile is | |
485 | * read into memory before the decompressor is called, | |
486 | * and thus was supplied by init_source. | |
487 | */ | |
488 | WARNMS(cinfo, JWRN_JPEG_EOF); | |
489 | /* insert a fake EOI marker */ | |
490 | sp->src.next_input_byte = dummy_EOI; | |
491 | sp->src.bytes_in_buffer = 2; | |
492 | return (TRUE); | |
493 | } | |
494 | ||
495 | static void | |
496 | std_skip_input_data(j_decompress_ptr cinfo, long num_bytes) | |
497 | { | |
498 | JPEGState* sp = (JPEGState*) cinfo; | |
499 | ||
500 | if (num_bytes > 0) { | |
501 | if (num_bytes > (long) sp->src.bytes_in_buffer) { | |
502 | /* oops, buffer overrun */ | |
503 | (void) std_fill_input_buffer(cinfo); | |
504 | } else { | |
505 | sp->src.next_input_byte += (size_t) num_bytes; | |
506 | sp->src.bytes_in_buffer -= (size_t) num_bytes; | |
507 | } | |
508 | } | |
509 | } | |
510 | ||
511 | static void | |
512 | std_term_source(j_decompress_ptr cinfo) | |
513 | { | |
514 | /* No work necessary here */ | |
515 | /* Or must we update tif->tif_rawcp, tif->tif_rawcc ??? */ | |
516 | /* (if so, need empty tables_term_source!) */ | |
517 | (void) cinfo; | |
518 | } | |
519 | ||
520 | static void | |
521 | TIFFjpeg_data_src(JPEGState* sp, TIFF* tif) | |
522 | { | |
523 | (void) tif; | |
524 | sp->cinfo.d.src = &sp->src; | |
525 | sp->src.init_source = std_init_source; | |
526 | sp->src.fill_input_buffer = std_fill_input_buffer; | |
527 | sp->src.skip_input_data = std_skip_input_data; | |
528 | sp->src.resync_to_restart = jpeg_resync_to_restart; | |
529 | sp->src.term_source = std_term_source; | |
530 | sp->src.bytes_in_buffer = 0; /* for safety */ | |
531 | sp->src.next_input_byte = NULL; | |
532 | } | |
533 | ||
534 | /* | |
535 | * Alternate source manager for reading from JPEGTables. | |
536 | * We can share all the code except for the init routine. | |
537 | */ | |
538 | ||
539 | static void | |
540 | tables_init_source(j_decompress_ptr cinfo) | |
541 | { | |
542 | JPEGState* sp = (JPEGState*) cinfo; | |
543 | ||
544 | sp->src.next_input_byte = (const JOCTET*) sp->jpegtables; | |
545 | sp->src.bytes_in_buffer = (size_t) sp->jpegtables_length; | |
546 | } | |
547 | ||
548 | static void | |
549 | TIFFjpeg_tables_src(JPEGState* sp, TIFF* tif) | |
550 | { | |
551 | TIFFjpeg_data_src(sp, tif); | |
552 | sp->src.init_source = tables_init_source; | |
553 | } | |
554 | ||
555 | /* | |
556 | * Allocate downsampled-data buffers needed for downsampled I/O. | |
557 | * We use values computed in jpeg_start_compress or jpeg_start_decompress. | |
558 | * We use libjpeg's allocator so that buffers will be released automatically | |
559 | * when done with strip/tile. | |
560 | * This is also a handy place to compute samplesperclump, bytesperline. | |
561 | */ | |
562 | static int | |
563 | alloc_downsampled_buffers(TIFF* tif, jpeg_component_info* comp_info, | |
564 | int num_components) | |
565 | { | |
566 | JPEGState* sp = JState(tif); | |
567 | int ci; | |
568 | jpeg_component_info* compptr; | |
569 | JSAMPARRAY buf; | |
570 | int samples_per_clump = 0; | |
571 | ||
572 | for (ci = 0, compptr = comp_info; ci < num_components; | |
573 | ci++, compptr++) { | |
574 | samples_per_clump += compptr->h_samp_factor * | |
575 | compptr->v_samp_factor; | |
576 | buf = TIFFjpeg_alloc_sarray(sp, JPOOL_IMAGE, | |
577 | compptr->width_in_blocks * DCTSIZE, | |
578 | (JDIMENSION) (compptr->v_samp_factor*DCTSIZE)); | |
579 | if (buf == NULL) | |
580 | return (0); | |
581 | sp->ds_buffer[ci] = buf; | |
582 | } | |
583 | sp->samplesperclump = samples_per_clump; | |
b47c832e RR |
584 | return (1); |
585 | } | |
586 | ||
587 | ||
588 | /* | |
589 | * JPEG Decoding. | |
590 | */ | |
591 | ||
592 | static int | |
593 | JPEGSetupDecode(TIFF* tif) | |
594 | { | |
595 | JPEGState* sp = JState(tif); | |
596 | TIFFDirectory *td = &tif->tif_dir; | |
597 | ||
00cb87b4 VZ |
598 | JPEGInitializeLibJPEG( tif ); |
599 | ||
b47c832e RR |
600 | assert(sp != NULL); |
601 | assert(sp->cinfo.comm.is_decompressor); | |
602 | ||
603 | /* Read JPEGTables if it is present */ | |
604 | if (TIFFFieldSet(tif,FIELD_JPEGTABLES)) { | |
605 | TIFFjpeg_tables_src(sp, tif); | |
606 | if(TIFFjpeg_read_header(sp,FALSE) != JPEG_HEADER_TABLES_ONLY) { | |
607 | TIFFError("JPEGSetupDecode", "Bogus JPEGTables field"); | |
608 | return (0); | |
609 | } | |
610 | } | |
611 | ||
612 | /* Grab parameters that are same for all strips/tiles */ | |
613 | sp->photometric = td->td_photometric; | |
614 | switch (sp->photometric) { | |
615 | case PHOTOMETRIC_YCBCR: | |
616 | sp->h_sampling = td->td_ycbcrsubsampling[0]; | |
617 | sp->v_sampling = td->td_ycbcrsubsampling[1]; | |
618 | break; | |
619 | default: | |
620 | /* TIFF 6.0 forbids subsampling of all other color spaces */ | |
621 | sp->h_sampling = 1; | |
622 | sp->v_sampling = 1; | |
623 | break; | |
624 | } | |
625 | ||
626 | /* Set up for reading normal data */ | |
627 | TIFFjpeg_data_src(sp, tif); | |
628 | tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */ | |
629 | return (1); | |
630 | } | |
631 | ||
632 | /* | |
633 | * Set up for decoding a strip or tile. | |
634 | */ | |
635 | static int | |
636 | JPEGPreDecode(TIFF* tif, tsample_t s) | |
637 | { | |
638 | JPEGState *sp = JState(tif); | |
639 | TIFFDirectory *td = &tif->tif_dir; | |
640 | static const char module[] = "JPEGPreDecode"; | |
641 | uint32 segment_width, segment_height; | |
642 | int downsampled_output; | |
643 | int ci; | |
644 | ||
645 | assert(sp != NULL); | |
646 | assert(sp->cinfo.comm.is_decompressor); | |
647 | /* | |
648 | * Reset decoder state from any previous strip/tile, | |
649 | * in case application didn't read the whole strip. | |
650 | */ | |
651 | if (!TIFFjpeg_abort(sp)) | |
652 | return (0); | |
653 | /* | |
654 | * Read the header for this strip/tile. | |
655 | */ | |
656 | if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK) | |
657 | return (0); | |
658 | /* | |
659 | * Check image parameters and set decompression parameters. | |
660 | */ | |
00cb87b4 VZ |
661 | segment_width = td->td_imagewidth; |
662 | segment_height = td->td_imagelength - tif->tif_row; | |
b47c832e | 663 | if (isTiled(tif)) { |
00cb87b4 VZ |
664 | segment_width = td->td_tilewidth; |
665 | segment_height = td->td_tilelength; | |
b47c832e RR |
666 | sp->bytesperline = TIFFTileRowSize(tif); |
667 | } else { | |
b47c832e RR |
668 | if (segment_height > td->td_rowsperstrip) |
669 | segment_height = td->td_rowsperstrip; | |
670 | sp->bytesperline = TIFFScanlineSize(tif); | |
671 | } | |
672 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) { | |
673 | /* | |
674 | * For PC 2, scale down the expected strip/tile size | |
675 | * to match a downsampled component | |
676 | */ | |
677 | segment_width = TIFFhowmany(segment_width, sp->h_sampling); | |
678 | segment_height = TIFFhowmany(segment_height, sp->v_sampling); | |
679 | } | |
680 | if (sp->cinfo.d.image_width != segment_width || | |
681 | sp->cinfo.d.image_height != segment_height) { | |
00cb87b4 VZ |
682 | TIFFWarning(module, |
683 | "Improper JPEG strip/tile size, expected %dx%d, got %dx%d", | |
684 | segment_width, | |
685 | segment_height, | |
686 | sp->cinfo.d.image_width, | |
687 | sp->cinfo.d.image_height); | |
b47c832e RR |
688 | } |
689 | if (sp->cinfo.d.num_components != | |
690 | (td->td_planarconfig == PLANARCONFIG_CONTIG ? | |
691 | td->td_samplesperpixel : 1)) { | |
692 | TIFFError(module, "Improper JPEG component count"); | |
693 | return (0); | |
694 | } | |
695 | if (sp->cinfo.d.data_precision != td->td_bitspersample) { | |
696 | TIFFError(module, "Improper JPEG data precision"); | |
697 | return (0); | |
698 | } | |
699 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) { | |
700 | /* Component 0 should have expected sampling factors */ | |
701 | if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling || | |
702 | sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) { | |
00cb87b4 VZ |
703 | TIFFWarning(module, |
704 | "Improper JPEG sampling factors %d,%d\n" | |
705 | "Apparently should be %d,%d, " | |
706 | "decompressor will try reading with " | |
707 | "sampling %d,%d", | |
708 | sp->cinfo.d.comp_info[0].h_samp_factor, | |
709 | sp->cinfo.d.comp_info[0].v_samp_factor, | |
710 | sp->h_sampling, | |
711 | sp->v_sampling, | |
712 | sp->cinfo.d.comp_info[0].h_samp_factor, | |
713 | sp->cinfo.d.comp_info[0].v_samp_factor ); | |
714 | ||
715 | sp->h_sampling = (uint16) | |
716 | sp->cinfo.d.comp_info[0].h_samp_factor; | |
717 | sp->v_sampling = (uint16) | |
718 | sp->cinfo.d.comp_info[0].v_samp_factor; | |
b47c832e RR |
719 | } |
720 | /* Rest should have sampling factors 1,1 */ | |
721 | for (ci = 1; ci < sp->cinfo.d.num_components; ci++) { | |
722 | if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 || | |
723 | sp->cinfo.d.comp_info[ci].v_samp_factor != 1) { | |
724 | TIFFError(module, "Improper JPEG sampling factors"); | |
725 | return (0); | |
726 | } | |
727 | } | |
728 | } else { | |
729 | /* PC 2's single component should have sampling factors 1,1 */ | |
730 | if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 || | |
731 | sp->cinfo.d.comp_info[0].v_samp_factor != 1) { | |
732 | TIFFError(module, "Improper JPEG sampling factors"); | |
733 | return (0); | |
734 | } | |
735 | } | |
736 | downsampled_output = FALSE; | |
737 | if (td->td_planarconfig == PLANARCONFIG_CONTIG && | |
738 | sp->photometric == PHOTOMETRIC_YCBCR && | |
739 | sp->jpegcolormode == JPEGCOLORMODE_RGB) { | |
00cb87b4 | 740 | /* Convert YCbCr to RGB */ |
b47c832e RR |
741 | sp->cinfo.d.jpeg_color_space = JCS_YCbCr; |
742 | sp->cinfo.d.out_color_space = JCS_RGB; | |
743 | } else { | |
00cb87b4 | 744 | /* Suppress colorspace handling */ |
b47c832e RR |
745 | sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN; |
746 | sp->cinfo.d.out_color_space = JCS_UNKNOWN; | |
747 | if (td->td_planarconfig == PLANARCONFIG_CONTIG && | |
748 | (sp->h_sampling != 1 || sp->v_sampling != 1)) | |
749 | downsampled_output = TRUE; | |
750 | /* XXX what about up-sampling? */ | |
751 | } | |
752 | if (downsampled_output) { | |
753 | /* Need to use raw-data interface to libjpeg */ | |
754 | sp->cinfo.d.raw_data_out = TRUE; | |
755 | tif->tif_decoderow = JPEGDecodeRaw; | |
756 | tif->tif_decodestrip = JPEGDecodeRaw; | |
757 | tif->tif_decodetile = JPEGDecodeRaw; | |
758 | } else { | |
759 | /* Use normal interface to libjpeg */ | |
760 | sp->cinfo.d.raw_data_out = FALSE; | |
761 | tif->tif_decoderow = JPEGDecode; | |
762 | tif->tif_decodestrip = JPEGDecode; | |
763 | tif->tif_decodetile = JPEGDecode; | |
764 | } | |
765 | /* Start JPEG decompressor */ | |
766 | if (!TIFFjpeg_start_decompress(sp)) | |
767 | return (0); | |
768 | /* Allocate downsampled-data buffers if needed */ | |
769 | if (downsampled_output) { | |
770 | if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info, | |
771 | sp->cinfo.d.num_components)) | |
772 | return (0); | |
773 | sp->scancount = DCTSIZE; /* mark buffer empty */ | |
774 | } | |
775 | return (1); | |
776 | } | |
777 | ||
778 | /* | |
779 | * Decode a chunk of pixels. | |
780 | * "Standard" case: returned data is not downsampled. | |
781 | */ | |
00cb87b4 | 782 | /*ARGSUSED*/ static int |
b47c832e RR |
783 | JPEGDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s) |
784 | { | |
00cb87b4 VZ |
785 | JPEGState *sp = JState(tif); |
786 | tsize_t nrows; | |
787 | ||
788 | nrows = cc / sp->bytesperline; | |
789 | if (cc % sp->bytesperline) | |
790 | TIFFWarning(tif->tif_name, "fractional scanline not read"); | |
791 | ||
792 | if( nrows > (int) sp->cinfo.d.image_height ) | |
793 | nrows = sp->cinfo.d.image_height; | |
794 | ||
795 | /* data is expected to be read in multiples of a scanline */ | |
796 | if (nrows) | |
797 | { | |
798 | do { | |
799 | JSAMPROW bufptr = (JSAMPROW)buf; | |
800 | ||
801 | if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1) | |
802 | return (0); | |
803 | ++tif->tif_row; | |
804 | buf += sp->bytesperline; | |
805 | cc -= sp->bytesperline; | |
806 | } while (--nrows > 0); | |
807 | } | |
808 | /* Close down the decompressor if we've finished the strip or tile. */ | |
809 | return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height | |
810 | || TIFFjpeg_finish_decompress(sp); | |
b47c832e RR |
811 | } |
812 | ||
813 | /* | |
814 | * Decode a chunk of pixels. | |
815 | * Returned data is downsampled per sampling factors. | |
816 | */ | |
00cb87b4 | 817 | /*ARGSUSED*/ static int |
b47c832e RR |
818 | JPEGDecodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s) |
819 | { | |
820 | JPEGState *sp = JState(tif); | |
b47c832e | 821 | tsize_t nrows; |
b47c832e | 822 | |
b47c832e | 823 | /* data is expected to be read in multiples of a scanline */ |
00cb87b4 VZ |
824 | if ( (nrows = sp->cinfo.d.image_height) ) { |
825 | /* Cb,Cr both have sampling factors 1, so this is correct */ | |
826 | JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width; | |
827 | int samples_per_clump = sp->samplesperclump; | |
828 | ||
829 | do { | |
830 | jpeg_component_info *compptr; | |
831 | int ci, clumpoffset; | |
b47c832e | 832 | |
00cb87b4 VZ |
833 | /* Reload downsampled-data buffer if needed */ |
834 | if (sp->scancount >= DCTSIZE) { | |
835 | int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE; | |
b47c832e | 836 | |
00cb87b4 VZ |
837 | if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n) |
838 | != n) | |
b47c832e | 839 | return (0); |
00cb87b4 | 840 | sp->scancount = 0; |
b47c832e | 841 | } |
00cb87b4 VZ |
842 | /* |
843 | * Fastest way to unseparate data is to make one pass | |
844 | * over the scanline for each row of each component. | |
845 | */ | |
846 | clumpoffset = 0; /* first sample in clump */ | |
847 | for (ci = 0, compptr = sp->cinfo.d.comp_info; | |
848 | ci < sp->cinfo.d.num_components; | |
849 | ci++, compptr++) { | |
850 | int hsamp = compptr->h_samp_factor; | |
851 | int vsamp = compptr->v_samp_factor; | |
852 | int ypos; | |
853 | ||
854 | for (ypos = 0; ypos < vsamp; ypos++) { | |
855 | JSAMPLE *inptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos]; | |
856 | JSAMPLE *outptr = (JSAMPLE*)buf + clumpoffset; | |
857 | JDIMENSION nclump; | |
858 | ||
859 | if (hsamp == 1) { | |
860 | /* fast path for at least Cb and Cr */ | |
861 | for (nclump = clumps_per_line; nclump-- > 0; ) { | |
862 | outptr[0] = *inptr++; | |
863 | outptr += samples_per_clump; | |
864 | } | |
865 | } else { | |
866 | int xpos; | |
867 | ||
868 | /* general case */ | |
869 | for (nclump = clumps_per_line; nclump-- > 0; ) { | |
870 | for (xpos = 0; xpos < hsamp; xpos++) | |
871 | outptr[xpos] = *inptr++; | |
872 | outptr += samples_per_clump; | |
873 | } | |
874 | } | |
875 | clumpoffset += hsamp; | |
b47c832e RR |
876 | } |
877 | } | |
00cb87b4 VZ |
878 | ++sp->scancount; |
879 | ++tif->tif_row; | |
880 | buf += sp->bytesperline; | |
881 | cc -= sp->bytesperline; | |
882 | } while (--nrows > 0); | |
b47c832e | 883 | } |
00cb87b4 VZ |
884 | |
885 | /* Close down the decompressor if done. */ | |
886 | return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height | |
887 | || TIFFjpeg_finish_decompress(sp); | |
b47c832e RR |
888 | } |
889 | ||
890 | ||
891 | /* | |
892 | * JPEG Encoding. | |
893 | */ | |
894 | ||
895 | static void | |
896 | unsuppress_quant_table (JPEGState* sp, int tblno) | |
897 | { | |
898 | JQUANT_TBL* qtbl; | |
899 | ||
900 | if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL) | |
901 | qtbl->sent_table = FALSE; | |
902 | } | |
903 | ||
904 | static void | |
905 | unsuppress_huff_table (JPEGState* sp, int tblno) | |
906 | { | |
907 | JHUFF_TBL* htbl; | |
908 | ||
909 | if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL) | |
910 | htbl->sent_table = FALSE; | |
911 | if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL) | |
912 | htbl->sent_table = FALSE; | |
913 | } | |
914 | ||
915 | static int | |
916 | prepare_JPEGTables(TIFF* tif) | |
917 | { | |
918 | JPEGState* sp = JState(tif); | |
919 | ||
00cb87b4 VZ |
920 | JPEGInitializeLibJPEG( tif ); |
921 | ||
b47c832e RR |
922 | /* Initialize quant tables for current quality setting */ |
923 | if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE)) | |
924 | return (0); | |
925 | /* Mark only the tables we want for output */ | |
926 | /* NB: chrominance tables are currently used only with YCbCr */ | |
927 | if (!TIFFjpeg_suppress_tables(sp, TRUE)) | |
928 | return (0); | |
929 | if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) { | |
930 | unsuppress_quant_table(sp, 0); | |
931 | if (sp->photometric == PHOTOMETRIC_YCBCR) | |
932 | unsuppress_quant_table(sp, 1); | |
933 | } | |
934 | if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) { | |
935 | unsuppress_huff_table(sp, 0); | |
936 | if (sp->photometric == PHOTOMETRIC_YCBCR) | |
937 | unsuppress_huff_table(sp, 1); | |
938 | } | |
939 | /* Direct libjpeg output into jpegtables */ | |
940 | if (!TIFFjpeg_tables_dest(sp, tif)) | |
941 | return (0); | |
942 | /* Emit tables-only datastream */ | |
943 | if (!TIFFjpeg_write_tables(sp)) | |
944 | return (0); | |
945 | ||
946 | return (1); | |
947 | } | |
948 | ||
949 | static int | |
950 | JPEGSetupEncode(TIFF* tif) | |
951 | { | |
952 | JPEGState* sp = JState(tif); | |
953 | TIFFDirectory *td = &tif->tif_dir; | |
954 | static const char module[] = "JPEGSetupEncode"; | |
955 | ||
00cb87b4 VZ |
956 | JPEGInitializeLibJPEG( tif ); |
957 | ||
b47c832e RR |
958 | assert(sp != NULL); |
959 | assert(!sp->cinfo.comm.is_decompressor); | |
960 | ||
961 | /* | |
962 | * Initialize all JPEG parameters to default values. | |
963 | * Note that jpeg_set_defaults needs legal values for | |
964 | * in_color_space and input_components. | |
965 | */ | |
966 | sp->cinfo.c.in_color_space = JCS_UNKNOWN; | |
967 | sp->cinfo.c.input_components = 1; | |
968 | if (!TIFFjpeg_set_defaults(sp)) | |
969 | return (0); | |
970 | /* Set per-file parameters */ | |
971 | sp->photometric = td->td_photometric; | |
972 | switch (sp->photometric) { | |
973 | case PHOTOMETRIC_YCBCR: | |
974 | sp->h_sampling = td->td_ycbcrsubsampling[0]; | |
975 | sp->v_sampling = td->td_ycbcrsubsampling[1]; | |
976 | /* | |
977 | * A ReferenceBlackWhite field *must* be present since the | |
978 | * default value is inappropriate for YCbCr. Fill in the | |
979 | * proper value if application didn't set it. | |
980 | */ | |
b47c832e RR |
981 | if (!TIFFFieldSet(tif, FIELD_REFBLACKWHITE)) { |
982 | float refbw[6]; | |
983 | long top = 1L << td->td_bitspersample; | |
984 | refbw[0] = 0; | |
985 | refbw[1] = (float)(top-1L); | |
986 | refbw[2] = (float)(top>>1); | |
987 | refbw[3] = refbw[1]; | |
988 | refbw[4] = refbw[2]; | |
989 | refbw[5] = refbw[1]; | |
990 | TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw); | |
991 | } | |
b47c832e RR |
992 | break; |
993 | case PHOTOMETRIC_PALETTE: /* disallowed by Tech Note */ | |
994 | case PHOTOMETRIC_MASK: | |
995 | TIFFError(module, | |
996 | "PhotometricInterpretation %d not allowed for JPEG", | |
997 | (int) sp->photometric); | |
998 | return (0); | |
999 | default: | |
1000 | /* TIFF 6.0 forbids subsampling of all other color spaces */ | |
1001 | sp->h_sampling = 1; | |
1002 | sp->v_sampling = 1; | |
1003 | break; | |
1004 | } | |
1005 | ||
1006 | /* Verify miscellaneous parameters */ | |
1007 | ||
1008 | /* | |
1009 | * This would need work if libtiff ever supports different | |
1010 | * depths for different components, or if libjpeg ever supports | |
1011 | * run-time selection of depth. Neither is imminent. | |
1012 | */ | |
1013 | if (td->td_bitspersample != BITS_IN_JSAMPLE) { | |
1014 | TIFFError(module, "BitsPerSample %d not allowed for JPEG", | |
1015 | (int) td->td_bitspersample); | |
1016 | return (0); | |
1017 | } | |
1018 | sp->cinfo.c.data_precision = td->td_bitspersample; | |
1019 | if (isTiled(tif)) { | |
1020 | if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0) { | |
1021 | TIFFError(module, | |
1022 | "JPEG tile height must be multiple of %d", | |
1023 | sp->v_sampling * DCTSIZE); | |
1024 | return (0); | |
1025 | } | |
1026 | if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0) { | |
1027 | TIFFError(module, | |
1028 | "JPEG tile width must be multiple of %d", | |
1029 | sp->h_sampling * DCTSIZE); | |
1030 | return (0); | |
1031 | } | |
1032 | } else { | |
1033 | if (td->td_rowsperstrip < td->td_imagelength && | |
1034 | (td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0) { | |
1035 | TIFFError(module, | |
1036 | "RowsPerStrip must be multiple of %d for JPEG", | |
1037 | sp->v_sampling * DCTSIZE); | |
1038 | return (0); | |
1039 | } | |
1040 | } | |
1041 | ||
1042 | /* Create a JPEGTables field if appropriate */ | |
1043 | if (sp->jpegtablesmode & (JPEGTABLESMODE_QUANT|JPEGTABLESMODE_HUFF)) { | |
1044 | if (!prepare_JPEGTables(tif)) | |
1045 | return (0); | |
1046 | /* Mark the field present */ | |
1047 | /* Can't use TIFFSetField since BEENWRITING is already set! */ | |
1048 | TIFFSetFieldBit(tif, FIELD_JPEGTABLES); | |
1049 | tif->tif_flags |= TIFF_DIRTYDIRECT; | |
1050 | } else { | |
1051 | /* We do not support application-supplied JPEGTables, */ | |
1052 | /* so mark the field not present */ | |
1053 | TIFFClrFieldBit(tif, FIELD_JPEGTABLES); | |
1054 | } | |
1055 | ||
1056 | /* Direct libjpeg output to libtiff's output buffer */ | |
1057 | TIFFjpeg_data_dest(sp, tif); | |
1058 | ||
1059 | return (1); | |
1060 | } | |
1061 | ||
1062 | /* | |
1063 | * Set encoding state at the start of a strip or tile. | |
1064 | */ | |
1065 | static int | |
1066 | JPEGPreEncode(TIFF* tif, tsample_t s) | |
1067 | { | |
1068 | JPEGState *sp = JState(tif); | |
1069 | TIFFDirectory *td = &tif->tif_dir; | |
1070 | static const char module[] = "JPEGPreEncode"; | |
1071 | uint32 segment_width, segment_height; | |
1072 | int downsampled_input; | |
1073 | ||
1074 | assert(sp != NULL); | |
1075 | assert(!sp->cinfo.comm.is_decompressor); | |
1076 | /* | |
1077 | * Set encoding parameters for this strip/tile. | |
1078 | */ | |
1079 | if (isTiled(tif)) { | |
1080 | segment_width = td->td_tilewidth; | |
1081 | segment_height = td->td_tilelength; | |
1082 | sp->bytesperline = TIFFTileRowSize(tif); | |
1083 | } else { | |
1084 | segment_width = td->td_imagewidth; | |
1085 | segment_height = td->td_imagelength - tif->tif_row; | |
1086 | if (segment_height > td->td_rowsperstrip) | |
1087 | segment_height = td->td_rowsperstrip; | |
1088 | sp->bytesperline = TIFFScanlineSize(tif); | |
1089 | } | |
1090 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) { | |
1091 | /* for PC 2, scale down the strip/tile size | |
1092 | * to match a downsampled component | |
1093 | */ | |
1094 | segment_width = TIFFhowmany(segment_width, sp->h_sampling); | |
1095 | segment_height = TIFFhowmany(segment_height, sp->v_sampling); | |
1096 | } | |
1097 | if (segment_width > 65535 || segment_height > 65535) { | |
1098 | TIFFError(module, "Strip/tile too large for JPEG"); | |
1099 | return (0); | |
1100 | } | |
1101 | sp->cinfo.c.image_width = segment_width; | |
1102 | sp->cinfo.c.image_height = segment_height; | |
1103 | downsampled_input = FALSE; | |
1104 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) { | |
1105 | sp->cinfo.c.input_components = td->td_samplesperpixel; | |
1106 | if (sp->photometric == PHOTOMETRIC_YCBCR) { | |
1107 | if (sp->jpegcolormode == JPEGCOLORMODE_RGB) { | |
1108 | sp->cinfo.c.in_color_space = JCS_RGB; | |
1109 | } else { | |
1110 | sp->cinfo.c.in_color_space = JCS_YCbCr; | |
1111 | if (sp->h_sampling != 1 || sp->v_sampling != 1) | |
1112 | downsampled_input = TRUE; | |
1113 | } | |
1114 | if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr)) | |
1115 | return (0); | |
1116 | /* | |
1117 | * Set Y sampling factors; | |
1118 | * we assume jpeg_set_colorspace() set the rest to 1 | |
1119 | */ | |
1120 | sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling; | |
1121 | sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling; | |
1122 | } else { | |
1123 | sp->cinfo.c.in_color_space = JCS_UNKNOWN; | |
1124 | if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN)) | |
1125 | return (0); | |
1126 | /* jpeg_set_colorspace set all sampling factors to 1 */ | |
1127 | } | |
1128 | } else { | |
1129 | sp->cinfo.c.input_components = 1; | |
1130 | sp->cinfo.c.in_color_space = JCS_UNKNOWN; | |
1131 | if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN)) | |
1132 | return (0); | |
1133 | sp->cinfo.c.comp_info[0].component_id = s; | |
1134 | /* jpeg_set_colorspace() set sampling factors to 1 */ | |
1135 | if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0) { | |
1136 | sp->cinfo.c.comp_info[0].quant_tbl_no = 1; | |
1137 | sp->cinfo.c.comp_info[0].dc_tbl_no = 1; | |
1138 | sp->cinfo.c.comp_info[0].ac_tbl_no = 1; | |
1139 | } | |
1140 | } | |
1141 | /* ensure libjpeg won't write any extraneous markers */ | |
1142 | sp->cinfo.c.write_JFIF_header = FALSE; | |
1143 | sp->cinfo.c.write_Adobe_marker = FALSE; | |
1144 | /* set up table handling correctly */ | |
1145 | if (! (sp->jpegtablesmode & JPEGTABLESMODE_QUANT)) { | |
1146 | if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE)) | |
1147 | return (0); | |
1148 | unsuppress_quant_table(sp, 0); | |
1149 | unsuppress_quant_table(sp, 1); | |
1150 | } | |
1151 | if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) | |
1152 | sp->cinfo.c.optimize_coding = FALSE; | |
1153 | else | |
1154 | sp->cinfo.c.optimize_coding = TRUE; | |
1155 | if (downsampled_input) { | |
1156 | /* Need to use raw-data interface to libjpeg */ | |
1157 | sp->cinfo.c.raw_data_in = TRUE; | |
1158 | tif->tif_encoderow = JPEGEncodeRaw; | |
1159 | tif->tif_encodestrip = JPEGEncodeRaw; | |
1160 | tif->tif_encodetile = JPEGEncodeRaw; | |
1161 | } else { | |
1162 | /* Use normal interface to libjpeg */ | |
1163 | sp->cinfo.c.raw_data_in = FALSE; | |
1164 | tif->tif_encoderow = JPEGEncode; | |
1165 | tif->tif_encodestrip = JPEGEncode; | |
1166 | tif->tif_encodetile = JPEGEncode; | |
1167 | } | |
1168 | /* Start JPEG compressor */ | |
1169 | if (!TIFFjpeg_start_compress(sp, FALSE)) | |
1170 | return (0); | |
1171 | /* Allocate downsampled-data buffers if needed */ | |
1172 | if (downsampled_input) { | |
1173 | if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info, | |
1174 | sp->cinfo.c.num_components)) | |
1175 | return (0); | |
1176 | } | |
1177 | sp->scancount = 0; | |
1178 | ||
1179 | return (1); | |
1180 | } | |
1181 | ||
1182 | /* | |
1183 | * Encode a chunk of pixels. | |
1184 | * "Standard" case: incoming data is not downsampled. | |
1185 | */ | |
1186 | static int | |
1187 | JPEGEncode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s) | |
1188 | { | |
1189 | JPEGState *sp = JState(tif); | |
1190 | tsize_t nrows; | |
1191 | JSAMPROW bufptr[1]; | |
1192 | ||
1193 | (void) s; | |
1194 | assert(sp != NULL); | |
1195 | /* data is expected to be supplied in multiples of a scanline */ | |
1196 | nrows = cc / sp->bytesperline; | |
1197 | if (cc % sp->bytesperline) | |
1198 | TIFFWarning(tif->tif_name, "fractional scanline discarded"); | |
1199 | ||
1200 | while (nrows-- > 0) { | |
1201 | bufptr[0] = (JSAMPROW) buf; | |
1202 | if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1) | |
1203 | return (0); | |
1204 | if (nrows > 0) | |
1205 | tif->tif_row++; | |
1206 | buf += sp->bytesperline; | |
1207 | } | |
1208 | return (1); | |
1209 | } | |
1210 | ||
1211 | /* | |
1212 | * Encode a chunk of pixels. | |
1213 | * Incoming data is expected to be downsampled per sampling factors. | |
1214 | */ | |
1215 | static int | |
1216 | JPEGEncodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s) | |
1217 | { | |
1218 | JPEGState *sp = JState(tif); | |
1219 | JSAMPLE* inptr; | |
1220 | JSAMPLE* outptr; | |
1221 | tsize_t nrows; | |
1222 | JDIMENSION clumps_per_line, nclump; | |
1223 | int clumpoffset, ci, xpos, ypos; | |
1224 | jpeg_component_info* compptr; | |
1225 | int samples_per_clump = sp->samplesperclump; | |
1226 | ||
1227 | (void) s; | |
1228 | assert(sp != NULL); | |
1229 | /* data is expected to be supplied in multiples of a scanline */ | |
1230 | nrows = cc / sp->bytesperline; | |
1231 | if (cc % sp->bytesperline) | |
1232 | TIFFWarning(tif->tif_name, "fractional scanline discarded"); | |
1233 | ||
1234 | /* Cb,Cr both have sampling factors 1, so this is correct */ | |
1235 | clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width; | |
1236 | ||
1237 | while (nrows-- > 0) { | |
1238 | /* | |
1239 | * Fastest way to separate the data is to make one pass | |
1240 | * over the scanline for each row of each component. | |
1241 | */ | |
1242 | clumpoffset = 0; /* first sample in clump */ | |
1243 | for (ci = 0, compptr = sp->cinfo.c.comp_info; | |
1244 | ci < sp->cinfo.c.num_components; | |
1245 | ci++, compptr++) { | |
1246 | int hsamp = compptr->h_samp_factor; | |
1247 | int vsamp = compptr->v_samp_factor; | |
1248 | int padding = (int) (compptr->width_in_blocks * DCTSIZE - | |
1249 | clumps_per_line * hsamp); | |
1250 | for (ypos = 0; ypos < vsamp; ypos++) { | |
1251 | inptr = ((JSAMPLE*) buf) + clumpoffset; | |
1252 | outptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos]; | |
1253 | if (hsamp == 1) { | |
1254 | /* fast path for at least Cb and Cr */ | |
1255 | for (nclump = clumps_per_line; nclump-- > 0; ) { | |
1256 | *outptr++ = inptr[0]; | |
1257 | inptr += samples_per_clump; | |
1258 | } | |
1259 | } else { | |
1260 | /* general case */ | |
1261 | for (nclump = clumps_per_line; nclump-- > 0; ) { | |
1262 | for (xpos = 0; xpos < hsamp; xpos++) | |
1263 | *outptr++ = inptr[xpos]; | |
1264 | inptr += samples_per_clump; | |
1265 | } | |
1266 | } | |
1267 | /* pad each scanline as needed */ | |
1268 | for (xpos = 0; xpos < padding; xpos++) { | |
1269 | *outptr = outptr[-1]; | |
1270 | outptr++; | |
1271 | } | |
1272 | clumpoffset += hsamp; | |
1273 | } | |
1274 | } | |
1275 | sp->scancount++; | |
1276 | if (sp->scancount >= DCTSIZE) { | |
1277 | int n = sp->cinfo.c.max_v_samp_factor * DCTSIZE; | |
1278 | if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n) | |
1279 | return (0); | |
1280 | sp->scancount = 0; | |
1281 | } | |
1282 | if (nrows > 0) | |
1283 | tif->tif_row++; | |
1284 | buf += sp->bytesperline; | |
1285 | } | |
1286 | return (1); | |
1287 | } | |
1288 | ||
1289 | /* | |
1290 | * Finish up at the end of a strip or tile. | |
1291 | */ | |
1292 | static int | |
1293 | JPEGPostEncode(TIFF* tif) | |
1294 | { | |
1295 | JPEGState *sp = JState(tif); | |
1296 | ||
1297 | if (sp->scancount > 0) { | |
1298 | /* | |
1299 | * Need to emit a partial bufferload of downsampled data. | |
1300 | * Pad the data vertically. | |
1301 | */ | |
1302 | int ci, ypos, n; | |
1303 | jpeg_component_info* compptr; | |
1304 | ||
1305 | for (ci = 0, compptr = sp->cinfo.c.comp_info; | |
1306 | ci < sp->cinfo.c.num_components; | |
1307 | ci++, compptr++) { | |
1308 | int vsamp = compptr->v_samp_factor; | |
1309 | tsize_t row_width = compptr->width_in_blocks * DCTSIZE | |
1310 | * sizeof(JSAMPLE); | |
1311 | for (ypos = sp->scancount * vsamp; | |
1312 | ypos < DCTSIZE * vsamp; ypos++) { | |
1313 | _TIFFmemcpy((tdata_t)sp->ds_buffer[ci][ypos], | |
1314 | (tdata_t)sp->ds_buffer[ci][ypos-1], | |
1315 | row_width); | |
1316 | ||
1317 | } | |
1318 | } | |
1319 | n = sp->cinfo.c.max_v_samp_factor * DCTSIZE; | |
1320 | if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n) | |
1321 | return (0); | |
1322 | } | |
1323 | ||
1324 | return (TIFFjpeg_finish_compress(JState(tif))); | |
1325 | } | |
1326 | ||
1327 | static void | |
1328 | JPEGCleanup(TIFF* tif) | |
1329 | { | |
1330 | if (tif->tif_data) { | |
1331 | JPEGState *sp = JState(tif); | |
00cb87b4 VZ |
1332 | if( sp->cinfo_initialized ) |
1333 | TIFFjpeg_destroy(sp); /* release libjpeg resources */ | |
b47c832e RR |
1334 | if (sp->jpegtables) /* tag value */ |
1335 | _TIFFfree(sp->jpegtables); | |
1336 | _TIFFfree(tif->tif_data); /* release local state */ | |
1337 | tif->tif_data = NULL; | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | static int | |
1342 | JPEGVSetField(TIFF* tif, ttag_t tag, va_list ap) | |
1343 | { | |
1344 | JPEGState* sp = JState(tif); | |
1345 | TIFFDirectory* td = &tif->tif_dir; | |
1346 | uint32 v32; | |
1347 | ||
1348 | switch (tag) { | |
1349 | case TIFFTAG_JPEGTABLES: | |
1350 | v32 = va_arg(ap, uint32); | |
1351 | if (v32 == 0) { | |
1352 | /* XXX */ | |
1353 | return (0); | |
1354 | } | |
1355 | _TIFFsetByteArray(&sp->jpegtables, va_arg(ap, void*), | |
1356 | (long) v32); | |
1357 | sp->jpegtables_length = v32; | |
1358 | TIFFSetFieldBit(tif, FIELD_JPEGTABLES); | |
1359 | break; | |
1360 | case TIFFTAG_JPEGQUALITY: | |
1361 | sp->jpegquality = va_arg(ap, int); | |
1362 | return (1); /* pseudo tag */ | |
1363 | case TIFFTAG_JPEGCOLORMODE: | |
1364 | sp->jpegcolormode = va_arg(ap, int); | |
1365 | /* | |
1366 | * Mark whether returned data is up-sampled or not | |
1367 | * so TIFFStripSize and TIFFTileSize return values | |
1368 | * that reflect the true amount of data. | |
1369 | */ | |
1370 | tif->tif_flags &= ~TIFF_UPSAMPLED; | |
1371 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) { | |
1372 | if (td->td_photometric == PHOTOMETRIC_YCBCR && | |
1373 | sp->jpegcolormode == JPEGCOLORMODE_RGB) { | |
1374 | tif->tif_flags |= TIFF_UPSAMPLED; | |
1375 | } else { | |
1376 | if (td->td_ycbcrsubsampling[0] != 1 || | |
1377 | td->td_ycbcrsubsampling[1] != 1) | |
1378 | ; /* XXX what about up-sampling? */ | |
1379 | } | |
1380 | } | |
1381 | /* | |
1382 | * Must recalculate cached tile size | |
1383 | * in case sampling state changed. | |
1384 | */ | |
1385 | tif->tif_tilesize = TIFFTileSize(tif); | |
1386 | return (1); /* pseudo tag */ | |
1387 | case TIFFTAG_JPEGTABLESMODE: | |
1388 | sp->jpegtablesmode = va_arg(ap, int); | |
1389 | return (1); /* pseudo tag */ | |
00cb87b4 VZ |
1390 | case TIFFTAG_YCBCRSUBSAMPLING: |
1391 | /* mark the fact that we have a real ycbcrsubsampling! */ | |
1392 | sp->ycbcrsampling_fetched = 1; | |
1393 | return (*sp->vsetparent)(tif, tag, ap); | |
b47c832e RR |
1394 | default: |
1395 | return (*sp->vsetparent)(tif, tag, ap); | |
1396 | } | |
1397 | tif->tif_flags |= TIFF_DIRTYDIRECT; | |
1398 | return (1); | |
1399 | } | |
1400 | ||
00cb87b4 VZ |
1401 | /* |
1402 | * Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in | |
1403 | * the TIFF tags, but still use non-default (2,2) values within the jpeg | |
1404 | * data stream itself. In order for TIFF applications to work properly | |
1405 | * - for instance to get the strip buffer size right - it is imperative | |
1406 | * that the subsampling be available before we start reading the image | |
1407 | * data normally. This function will attempt to load the first strip in | |
1408 | * order to get the sampling values from the jpeg data stream. Various | |
1409 | * hacks are various places are done to ensure this function gets called | |
1410 | * before the td_ycbcrsubsampling values are used from the directory structure, | |
1411 | * including calling TIFFGetField() for the YCBCRSUBSAMPLING field from | |
1412 | * TIFFStripSize(), and the printing code in tif_print.c. | |
1413 | * | |
1414 | * Note that JPEGPreDeocode() will produce a fairly loud warning when the | |
1415 | * discovered sampling does not match the default sampling (2,2) or whatever | |
1416 | * was actually in the tiff tags. | |
1417 | * | |
1418 | * Problems: | |
1419 | * o This code will cause one whole strip/tile of compressed data to be | |
1420 | * loaded just to get the tags right, even if the imagery is never read. | |
1421 | * It would be more efficient to just load a bit of the header, and | |
1422 | * initialize things from that. | |
1423 | * | |
1424 | * See the bug in bugzilla for details: | |
1425 | * | |
1426 | * http://bugzilla.remotesensing.org/show_bug.cgi?id=168 | |
1427 | * | |
1428 | * Frank Warmerdam, July 2002 | |
1429 | */ | |
1430 | ||
1431 | static void | |
1432 | JPEGFixupTestSubsampling( TIFF * tif ) | |
1433 | { | |
1434 | #if CHECK_JPEG_YCBCR_SUBSAMPLING == 1 | |
1435 | JPEGState *sp = JState(tif); | |
1436 | TIFFDirectory *td = &tif->tif_dir; | |
1437 | ||
1438 | JPEGInitializeLibJPEG( tif ); | |
1439 | ||
1440 | /* | |
1441 | * Some JPEG-in-TIFF files don't provide the ycbcrsampling tags, | |
1442 | * and use a sampling schema other than the default 2,2. To handle | |
1443 | * this we actually have to scan the header of a strip or tile of | |
1444 | * jpeg data to get the sampling. | |
1445 | */ | |
1446 | if( !sp->cinfo.comm.is_decompressor | |
1447 | || sp->ycbcrsampling_fetched | |
1448 | || td->td_photometric != PHOTOMETRIC_YCBCR ) | |
1449 | return; | |
1450 | ||
1451 | sp->ycbcrsampling_fetched = 1; | |
1452 | if( TIFFIsTiled( tif ) ) | |
1453 | { | |
1454 | if( !TIFFFillTile( tif, 0 ) ) | |
1455 | return; | |
1456 | } | |
1457 | else | |
1458 | { | |
1459 | if( !TIFFFillStrip( tif, 0 ) ) | |
1460 | return; | |
1461 | } | |
1462 | ||
1463 | TIFFSetField( tif, TIFFTAG_YCBCRSUBSAMPLING, | |
1464 | (uint16) sp->h_sampling, (uint16) sp->v_sampling ); | |
1465 | #endif /* CHECK_JPEG_YCBCR_SUBSAMPLING == 1 */ | |
1466 | } | |
1467 | ||
b47c832e RR |
1468 | static int |
1469 | JPEGVGetField(TIFF* tif, ttag_t tag, va_list ap) | |
1470 | { | |
1471 | JPEGState* sp = JState(tif); | |
1472 | ||
1473 | switch (tag) { | |
1474 | case TIFFTAG_JPEGTABLES: | |
1475 | /* u_short is bogus --- should be uint32 ??? */ | |
1476 | /* TIFFWriteNormalTag needs fixed XXX */ | |
1477 | *va_arg(ap, u_short*) = (u_short) sp->jpegtables_length; | |
1478 | *va_arg(ap, void**) = sp->jpegtables; | |
1479 | break; | |
1480 | case TIFFTAG_JPEGQUALITY: | |
1481 | *va_arg(ap, int*) = sp->jpegquality; | |
1482 | break; | |
1483 | case TIFFTAG_JPEGCOLORMODE: | |
1484 | *va_arg(ap, int*) = sp->jpegcolormode; | |
1485 | break; | |
1486 | case TIFFTAG_JPEGTABLESMODE: | |
1487 | *va_arg(ap, int*) = sp->jpegtablesmode; | |
1488 | break; | |
00cb87b4 VZ |
1489 | case TIFFTAG_YCBCRSUBSAMPLING: |
1490 | JPEGFixupTestSubsampling( tif ); | |
1491 | return (*sp->vgetparent)(tif, tag, ap); | |
1492 | break; | |
b47c832e RR |
1493 | default: |
1494 | return (*sp->vgetparent)(tif, tag, ap); | |
1495 | } | |
1496 | return (1); | |
1497 | } | |
1498 | ||
1499 | static void | |
1500 | JPEGPrintDir(TIFF* tif, FILE* fd, long flags) | |
1501 | { | |
1502 | JPEGState* sp = JState(tif); | |
1503 | ||
1504 | (void) flags; | |
1505 | if (TIFFFieldSet(tif,FIELD_JPEGTABLES)) | |
1506 | fprintf(fd, " JPEG Tables: (%lu bytes)\n", | |
1507 | (u_long) sp->jpegtables_length); | |
1508 | } | |
1509 | ||
1510 | static uint32 | |
1511 | JPEGDefaultStripSize(TIFF* tif, uint32 s) | |
1512 | { | |
1513 | JPEGState* sp = JState(tif); | |
1514 | TIFFDirectory *td = &tif->tif_dir; | |
1515 | ||
1516 | s = (*sp->defsparent)(tif, s); | |
1517 | if (s < td->td_imagelength) | |
1518 | s = TIFFroundup(s, td->td_ycbcrsubsampling[1] * DCTSIZE); | |
1519 | return (s); | |
1520 | } | |
1521 | ||
1522 | static void | |
1523 | JPEGDefaultTileSize(TIFF* tif, uint32* tw, uint32* th) | |
1524 | { | |
1525 | JPEGState* sp = JState(tif); | |
1526 | TIFFDirectory *td = &tif->tif_dir; | |
1527 | ||
1528 | (*sp->deftparent)(tif, tw, th); | |
1529 | *tw = TIFFroundup(*tw, td->td_ycbcrsubsampling[0] * DCTSIZE); | |
1530 | *th = TIFFroundup(*th, td->td_ycbcrsubsampling[1] * DCTSIZE); | |
1531 | } | |
1532 | ||
00cb87b4 VZ |
1533 | /* |
1534 | * The JPEG library initialized used to be done in TIFFInitJPEG(), but | |
1535 | * now that we allow a TIFF file to be opened in update mode it is necessary | |
1536 | * to have some way of deciding whether compression or decompression is | |
1537 | * desired other than looking at tif->tif_mode. We accomplish this by | |
1538 | * examining {TILE/STRIP}BYTECOUNTS to see if there is a non-zero entry. | |
1539 | * If so, we assume decompression is desired. | |
1540 | * | |
1541 | * This is tricky, because TIFFInitJPEG() is called while the directory is | |
1542 | * being read, and generally speaking the BYTECOUNTS tag won't have been read | |
1543 | * at that point. So we try to defer jpeg library initialization till we | |
1544 | * do have that tag ... basically any access that might require the compressor | |
1545 | * or decompressor that occurs after the reading of the directory. | |
1546 | * | |
1547 | * In an ideal world compressors or decompressors would be setup | |
1548 | * at the point where a single tile or strip was accessed (for read or write) | |
1549 | * so that stuff like update of missing tiles, or replacement of tiles could | |
1550 | * be done. However, we aren't trying to crack that nut just yet ... | |
1551 | * | |
1552 | * NFW, Feb 3rd, 2003. | |
1553 | */ | |
1554 | ||
1555 | static int JPEGInitializeLibJPEG( TIFF * tif ) | |
1556 | { | |
1557 | JPEGState* sp = JState(tif); | |
1558 | uint32 *byte_counts = NULL; | |
1559 | int data_is_empty = TRUE; | |
1560 | ||
1561 | if( sp->cinfo_initialized ) | |
1562 | return 1; | |
1563 | ||
1564 | /* | |
1565 | * Do we have tile data already? Make sure we initialize the | |
1566 | * the state in decompressor mode if we have tile data, even if we | |
1567 | * are not in read-only file access mode. | |
1568 | */ | |
1569 | if( TIFFIsTiled( tif ) | |
1570 | && TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &byte_counts ) | |
1571 | && byte_counts != NULL ) | |
1572 | { | |
1573 | data_is_empty = byte_counts[0] == 0; | |
1574 | } | |
1575 | if( !TIFFIsTiled( tif ) | |
1576 | && TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &byte_counts) | |
1577 | && byte_counts != NULL ) | |
1578 | { | |
1579 | data_is_empty = byte_counts[0] == 0; | |
1580 | } | |
1581 | ||
1582 | /* | |
1583 | * Initialize libjpeg. | |
1584 | */ | |
1585 | if (tif->tif_mode == O_RDONLY || !data_is_empty ) { | |
1586 | if (!TIFFjpeg_create_decompress(sp)) | |
1587 | return (0); | |
1588 | ||
1589 | } else { | |
1590 | if (!TIFFjpeg_create_compress(sp)) | |
1591 | return (0); | |
1592 | } | |
1593 | ||
1594 | sp->cinfo_initialized = TRUE; | |
1595 | ||
1596 | return 1; | |
1597 | } | |
1598 | ||
b47c832e RR |
1599 | int |
1600 | TIFFInitJPEG(TIFF* tif, int scheme) | |
1601 | { | |
1602 | JPEGState* sp; | |
1603 | ||
1604 | assert(scheme == COMPRESSION_JPEG); | |
1605 | ||
1606 | /* | |
1607 | * Allocate state block so tag methods have storage to record values. | |
1608 | */ | |
1609 | tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (JPEGState)); | |
00cb87b4 | 1610 | |
b47c832e RR |
1611 | if (tif->tif_data == NULL) { |
1612 | TIFFError("TIFFInitJPEG", "No space for JPEG state block"); | |
1613 | return (0); | |
1614 | } | |
00cb87b4 VZ |
1615 | memset( tif->tif_data, 0, sizeof(JPEGState)); |
1616 | ||
b47c832e RR |
1617 | sp = JState(tif); |
1618 | sp->tif = tif; /* back link */ | |
1619 | ||
1620 | /* | |
1621 | * Merge codec-specific tag information and | |
1622 | * override parent get/set field methods. | |
1623 | */ | |
1624 | _TIFFMergeFieldInfo(tif, jpegFieldInfo, N(jpegFieldInfo)); | |
00cb87b4 VZ |
1625 | sp->vgetparent = tif->tif_tagmethods.vgetfield; |
1626 | tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */ | |
1627 | sp->vsetparent = tif->tif_tagmethods.vsetfield; | |
1628 | tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */ | |
1629 | tif->tif_tagmethods.printdir = JPEGPrintDir; /* hook for codec tags */ | |
b47c832e RR |
1630 | |
1631 | /* Default values for codec-specific fields */ | |
1632 | sp->jpegtables = NULL; | |
1633 | sp->jpegtables_length = 0; | |
1634 | sp->jpegquality = 75; /* Default IJG quality */ | |
1635 | sp->jpegcolormode = JPEGCOLORMODE_RAW; | |
1636 | sp->jpegtablesmode = JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF; | |
1637 | ||
00cb87b4 VZ |
1638 | sp->ycbcrsampling_fetched = 0; |
1639 | ||
b47c832e RR |
1640 | /* |
1641 | * Install codec methods. | |
1642 | */ | |
1643 | tif->tif_setupdecode = JPEGSetupDecode; | |
1644 | tif->tif_predecode = JPEGPreDecode; | |
1645 | tif->tif_decoderow = JPEGDecode; | |
1646 | tif->tif_decodestrip = JPEGDecode; | |
1647 | tif->tif_decodetile = JPEGDecode; | |
1648 | tif->tif_setupencode = JPEGSetupEncode; | |
1649 | tif->tif_preencode = JPEGPreEncode; | |
1650 | tif->tif_postencode = JPEGPostEncode; | |
1651 | tif->tif_encoderow = JPEGEncode; | |
1652 | tif->tif_encodestrip = JPEGEncode; | |
1653 | tif->tif_encodetile = JPEGEncode; | |
1654 | tif->tif_cleanup = JPEGCleanup; | |
1655 | sp->defsparent = tif->tif_defstripsize; | |
1656 | tif->tif_defstripsize = JPEGDefaultStripSize; | |
1657 | sp->deftparent = tif->tif_deftilesize; | |
1658 | tif->tif_deftilesize = JPEGDefaultTileSize; | |
1659 | tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */ | |
1660 | ||
00cb87b4 VZ |
1661 | sp->cinfo_initialized = FALSE; |
1662 | ||
1663 | /* | |
1664 | * Mark the TIFFTAG_YCBCRSAMPLES as present even if it is not | |
1665 | * see: JPEGFixupTestSubsampling(). | |
1666 | */ | |
1667 | TIFFSetFieldBit( tif, FIELD_YCBCRSUBSAMPLING ); | |
b47c832e RR |
1668 | |
1669 | return (1); | |
1670 | } | |
1671 | #endif /* JPEG_SUPPORT */ |