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