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