Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / core / src / fxcodec / fx_tiff / tiff_v403 / tif_jpeg.c
1 /* $Id: tif_jpeg.c,v 1.111 2012-07-06 18:48:04 bfriesen 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 #if (!defined(_FPDFAPI_MINI_) || defined(_TIFF_DECODER_)) && !defined(_USE_ADDIN_) && !defined _FX_NO_ANSIC_ && !defined(_FX_EMB_NOUSE_DECODER_)
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* tif, uint32 strip);
48 int TIFFFillTile(TIFF* tif, uint32 tile);
49 int TIFFReInitJPEG_12( TIFF *tif, int scheme, int is_encode );
50
51 /* We undefine FAR to avoid conflict with JPEG definition */
52
53 #ifdef FAR
54 #undef FAR
55 #endif
56
57 /*
58   Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
59   not defined.  Unfortunately, the MinGW and Borland compilers include
60   a typedef for INT32, which causes a conflict.  MSVC does not include
61   a conficting typedef given the headers which are included.
62 */
63 #if defined(__BORLANDC__) || defined(__MINGW32__)
64 # define XMD_H 1
65 #endif
66
67 /*
68    The windows RPCNDR.H file defines boolean, but defines it with the
69    unsigned char size.  You should compile JPEG library using appropriate
70    definitions in jconfig.h header, but many users compile library in wrong
71    way. That causes errors of the following type:
72
73    "JPEGLib: JPEG parameter struct mismatch: library thinks size is 432,
74    caller expects 464"
75
76    For such users we wil fix the problem here. See install.doc file from
77    the JPEG library distribution for details.
78 */
79
80 /* Define "boolean" as unsigned char, not int, per Windows custom. */
81 #if defined(WIN32) && !defined(__MINGW32__)
82 # ifndef __RPCNDR_H__            /* don't conflict if rpcndr.h already read */
83    typedef unsigned char boolean;
84 # endif
85 # define HAVE_BOOLEAN            /* prevent jmorecfg.h from redefining it */
86 #endif
87
88 #ifndef _FX_JPEG_TURBO_
89         #include "../../libjpeg/jpeglib.h"
90         #include "../../libjpeg/jerror.h"
91 #else
92         #include "../../libjpeg-turbo/jpeglib.h"
93         #include "../../libjpeg-turbo/jerror.h"
94 #endif//_FX_JPEG_TURBO_
95
96 /* 
97  * Do we want to do special processing suitable for when JSAMPLE is a
98  * 16bit value?  
99  */
100
101 #if defined(JPEG_LIB_MK1)
102 #  define JPEG_LIB_MK1_OR_12BIT 1
103 #elif BITS_IN_JSAMPLE == 12
104 #  define JPEG_LIB_MK1_OR_12BIT 1
105 #endif
106
107 /*
108  * We are using width_in_blocks which is supposed to be private to
109  * libjpeg. Unfortunately, the libjpeg delivered with Cygwin has
110  * renamed this member to width_in_data_units.  Since the header has
111  * also renamed a define, use that unique define name in order to
112  * detect the problem header and adjust to suit.
113  */
114 #if defined(D_MAX_DATA_UNITS_IN_MCU)
115 #define width_in_blocks width_in_data_units
116 #endif
117
118 /*
119  * On some machines it may be worthwhile to use _setjmp or sigsetjmp
120  * in place of plain setjmp.  These macros will make it easier.
121  */
122 #define SETJMP(jbuf)            setjmp(jbuf)
123 #define LONGJMP(jbuf,code)      longjmp(jbuf,code)
124 #define JMP_BUF                 jmp_buf
125
126 typedef struct jpeg_destination_mgr jpeg_destination_mgr;
127 typedef struct jpeg_source_mgr jpeg_source_mgr;
128 typedef struct jpeg_error_mgr jpeg_error_mgr;
129
130 /*
131  * State block for each open TIFF file using
132  * libjpeg to do JPEG compression/decompression.
133  *
134  * libjpeg's visible state is either a jpeg_compress_struct
135  * or jpeg_decompress_struct depending on which way we
136  * are going.  comm can be used to refer to the fields
137  * which are common to both.
138  *
139  * NB: cinfo is required to be the first member of JPEGState,
140  *     so we can safely cast JPEGState* -> jpeg_xxx_struct*
141  *     and vice versa!
142  */
143 typedef struct {
144         union {
145                 struct jpeg_compress_struct c;
146                 struct jpeg_decompress_struct d;
147                 struct jpeg_common_struct comm;
148         } cinfo;                        /* NB: must be first */
149         int             cinfo_initialized;
150
151         jpeg_error_mgr  err;            /* libjpeg error manager */
152         JMP_BUF         exit_jmpbuf;    /* for catching libjpeg failures */
153         /*
154          * The following two members could be a union, but
155          * they're small enough that it's not worth the effort.
156          */
157         jpeg_destination_mgr dest;      /* data dest for compression */
158         jpeg_source_mgr src;            /* data source for decompression */
159                                         /* private state */
160         TIFF*           tif;            /* back link needed by some code */
161         uint16          photometric;    /* copy of PhotometricInterpretation */
162         uint16          h_sampling;     /* luminance sampling factors */
163         uint16          v_sampling;
164         tmsize_t        bytesperline;   /* decompressed bytes per scanline */
165         /* pointers to intermediate buffers when processing downsampled data */
166         JSAMPARRAY      ds_buffer[MAX_COMPONENTS];
167         int             scancount;      /* number of "scanlines" accumulated */
168         int             samplesperclump;
169
170         TIFFVGetMethod  vgetparent;     /* super-class method */
171         TIFFVSetMethod  vsetparent;     /* super-class method */
172         TIFFPrintMethod printdir;       /* super-class method */
173         TIFFStripMethod defsparent;     /* super-class method */
174         TIFFTileMethod  deftparent;     /* super-class method */
175                                         /* pseudo-tag fields */
176         void*           jpegtables;     /* JPEGTables tag value, or NULL */
177         uint32          jpegtables_length; /* number of bytes in same */
178         int             jpegquality;    /* Compression quality level */
179         int             jpegcolormode;  /* Auto RGB<=>YCbCr convert? */
180         int             jpegtablesmode; /* What to put in JPEGTables */
181
182         int             ycbcrsampling_fetched;
183 } JPEGState;
184
185 #define JState(tif)     ((JPEGState*)(tif)->tif_data)
186
187 static int JPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
188 static int JPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
189 static int JPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
190 static int JPEGEncodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
191 static int JPEGInitializeLibJPEG(TIFF * tif, int decode );
192 static int DecodeRowError(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
193
194 #define FIELD_JPEGTABLES        (FIELD_CODEC+0)
195
196 static const TIFFField jpegFields[] = {
197     { TIFFTAG_JPEGTABLES, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, TIFF_SETGET_C32_UINT8, FIELD_JPEGTABLES, FALSE, TRUE, "JPEGTables", NULL },
198     { TIFFTAG_JPEGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
199     { TIFFTAG_JPEGCOLORMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL },
200     { TIFFTAG_JPEGTABLESMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL }
201 };
202
203 /*
204  * libjpeg interface layer.
205  *
206  * We use setjmp/longjmp to return control to libtiff
207  * when a fatal error is encountered within the JPEG
208  * library.  We also direct libjpeg error and warning
209  * messages through the appropriate libtiff handlers.
210  */
211
212 /*
213  * Error handling routines (these replace corresponding
214  * IJG routines from jerror.c).  These are used for both
215  * compression and decompression.
216  */
217 static void
218 TIFFjpeg_error_exit(j_common_ptr cinfo)
219 {
220         JPEGState *sp = (JPEGState *) cinfo;    /* NB: cinfo assumed first */
221         char buffer[JMSG_LENGTH_MAX];
222
223         (*cinfo->err->format_message) (cinfo, buffer);
224         TIFFErrorExt(sp->tif->tif_clientdata, "JPEGLib", "%s", buffer);         /* display the error message */
225         jpeg_abort(cinfo);                      /* clean up libjpeg state */
226         LONGJMP(sp->exit_jmpbuf, 1);            /* return to libtiff caller */
227 }
228
229 /*
230  * This routine is invoked only for warning messages,
231  * since error_exit does its own thing and trace_level
232  * is never set > 0.
233  */
234 static void
235 TIFFjpeg_output_message(j_common_ptr cinfo)
236 {
237         char buffer[JMSG_LENGTH_MAX];
238
239         (*cinfo->err->format_message) (cinfo, buffer);
240         TIFFWarningExt(((JPEGState *) cinfo)->tif->tif_clientdata, "JPEGLib", "%s", buffer);
241 }
242
243 /*
244  * Interface routines.  This layer of routines exists
245  * primarily to limit side-effects from using setjmp.
246  * Also, normal/error returns are converted into return
247  * values per libtiff practice.
248  */
249 #define CALLJPEG(sp, fail, op)  (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op))
250 #define CALLVJPEG(sp, op)       CALLJPEG(sp, 0, ((op),1))
251
252 static int
253 TIFFjpeg_create_compress(JPEGState* sp)
254 {
255         /* initialize JPEG error handling */
256         sp->cinfo.c.err = jpeg_std_error(&sp->err);
257         sp->err.error_exit = TIFFjpeg_error_exit;
258         sp->err.output_message = TIFFjpeg_output_message;
259
260         return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c));
261 }
262
263 static int
264 TIFFjpeg_create_decompress(JPEGState* sp)
265 {
266         /* initialize JPEG error handling */
267         sp->cinfo.d.err = jpeg_std_error(&sp->err);
268         sp->err.error_exit = TIFFjpeg_error_exit;
269         sp->err.output_message = TIFFjpeg_output_message;
270
271         return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d));
272 }
273
274 static int
275 TIFFjpeg_set_defaults(JPEGState* sp)
276 {
277         return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c));
278 }
279
280 static int
281 TIFFjpeg_set_colorspace(JPEGState* sp, J_COLOR_SPACE colorspace)
282 {
283         return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace));
284 }
285
286 static int
287 TIFFjpeg_set_quality(JPEGState* sp, int quality, boolean force_baseline)
288 {
289         return CALLVJPEG(sp,
290             jpeg_set_quality(&sp->cinfo.c, quality, force_baseline));
291 }
292
293 static int
294 TIFFjpeg_suppress_tables(JPEGState* sp, boolean suppress)
295 {
296         return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress));
297 }
298
299 static int
300 TIFFjpeg_start_compress(JPEGState* sp, boolean write_all_tables)
301 {
302         return CALLVJPEG(sp,
303             jpeg_start_compress(&sp->cinfo.c, write_all_tables));
304 }
305
306 static int
307 TIFFjpeg_write_scanlines(JPEGState* sp, JSAMPARRAY scanlines, int num_lines)
308 {
309         return CALLJPEG(sp, -1, (int) jpeg_write_scanlines(&sp->cinfo.c,
310             scanlines, (JDIMENSION) num_lines));
311 }
312
313 static int
314 TIFFjpeg_write_raw_data(JPEGState* sp, JSAMPIMAGE data, int num_lines)
315 {
316         return CALLJPEG(sp, -1, (int) jpeg_write_raw_data(&sp->cinfo.c,
317             data, (JDIMENSION) num_lines));
318 }
319
320 static int
321 TIFFjpeg_finish_compress(JPEGState* sp)
322 {
323         return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c));
324 }
325
326 static int
327 TIFFjpeg_write_tables(JPEGState* sp)
328 {
329         return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c));
330 }
331
332 static int
333 TIFFjpeg_read_header(JPEGState* sp, boolean require_image)
334 {
335         return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image));
336 }
337
338 static int
339 TIFFjpeg_start_decompress(JPEGState* sp)
340 {
341         return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d));
342 }
343
344 static int
345 TIFFjpeg_read_scanlines(JPEGState* sp, JSAMPARRAY scanlines, int max_lines)
346 {
347         return CALLJPEG(sp, -1, (int) jpeg_read_scanlines(&sp->cinfo.d,
348             scanlines, (JDIMENSION) max_lines));
349 }
350
351 static int
352 TIFFjpeg_read_raw_data(JPEGState* sp, JSAMPIMAGE data, int max_lines)
353 {
354         return CALLJPEG(sp, -1, (int) jpeg_read_raw_data(&sp->cinfo.d,
355             data, (JDIMENSION) max_lines));
356 }
357
358 static int
359 TIFFjpeg_finish_decompress(JPEGState* sp)
360 {
361         return CALLJPEG(sp, -1, (int) jpeg_finish_decompress(&sp->cinfo.d));
362 }
363
364 static int
365 TIFFjpeg_abort(JPEGState* sp)
366 {
367         return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm));
368 }
369
370 static int
371 TIFFjpeg_destroy(JPEGState* sp)
372 {
373         return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm));
374 }
375
376 static JSAMPARRAY
377 TIFFjpeg_alloc_sarray(JPEGState* sp, int pool_id,
378                       JDIMENSION samplesperrow, JDIMENSION numrows)
379 {
380         return CALLJPEG(sp, (JSAMPARRAY) NULL,
381             (*sp->cinfo.comm.mem->alloc_sarray)
382                 (&sp->cinfo.comm, pool_id, samplesperrow, numrows));
383 }
384
385 /*
386  * JPEG library destination data manager.
387  * These routines direct compressed data from libjpeg into the
388  * libtiff output buffer.
389  */
390
391 static void
392 std_init_destination(j_compress_ptr cinfo)
393 {
394         JPEGState* sp = (JPEGState*) cinfo;
395         TIFF* tif = sp->tif;
396
397         sp->dest.next_output_byte = (JOCTET*) tif->tif_rawdata;
398         sp->dest.free_in_buffer = (size_t) tif->tif_rawdatasize;
399 }
400
401 static boolean
402 std_empty_output_buffer(j_compress_ptr cinfo)
403 {
404         JPEGState* sp = (JPEGState*) cinfo;
405         TIFF* tif = sp->tif;
406
407         /* the entire buffer has been filled */
408         tif->tif_rawcc = tif->tif_rawdatasize;
409
410 #ifdef IPPJ_HUFF
411        /*
412         * The Intel IPP performance library does not necessarily fill up
413         * the whole output buffer on each pass, so only dump out the parts
414         * that have been filled.
415         *   http://trac.osgeo.org/gdal/wiki/JpegIPP
416         */
417        if ( sp->dest.free_in_buffer >= 0 ) {
418                tif->tif_rawcc = tif->tif_rawdatasize - sp->dest.free_in_buffer;
419        }
420 #endif
421
422         TIFFFlushData1(tif);
423         sp->dest.next_output_byte = (JOCTET*) tif->tif_rawdata;
424         sp->dest.free_in_buffer = (size_t) tif->tif_rawdatasize;
425
426         return (TRUE);
427 }
428
429 static void
430 std_term_destination(j_compress_ptr cinfo)
431 {
432         JPEGState* sp = (JPEGState*) cinfo;
433         TIFF* tif = sp->tif;
434
435         tif->tif_rawcp = (uint8*) sp->dest.next_output_byte;
436         tif->tif_rawcc =
437             tif->tif_rawdatasize - (tmsize_t) sp->dest.free_in_buffer;
438         /* NB: libtiff does the final buffer flush */
439 }
440
441 static void
442 TIFFjpeg_data_dest(JPEGState* sp, TIFF* tif)
443 {
444         (void) tif;
445         sp->cinfo.c.dest = &sp->dest;
446         sp->dest.init_destination = std_init_destination;
447         sp->dest.empty_output_buffer = std_empty_output_buffer;
448         sp->dest.term_destination = std_term_destination;
449 }
450
451 /*
452  * Alternate destination manager for outputting to JPEGTables field.
453  */
454
455 static void
456 tables_init_destination(j_compress_ptr cinfo)
457 {
458         JPEGState* sp = (JPEGState*) cinfo;
459
460         /* while building, jpegtables_length is allocated buffer size */
461         sp->dest.next_output_byte = (JOCTET*) sp->jpegtables;
462         sp->dest.free_in_buffer = (size_t) sp->jpegtables_length;
463 }
464
465 static boolean
466 tables_empty_output_buffer(j_compress_ptr cinfo)
467 {
468         JPEGState* sp = (JPEGState*) cinfo;
469         void* newbuf;
470
471         /* the entire buffer has been filled; enlarge it by 1000 bytes */
472         newbuf = _TIFFrealloc((void*) sp->jpegtables,
473                               (tmsize_t) (sp->jpegtables_length + 1000));
474         if (newbuf == NULL)
475                 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 100);
476         sp->dest.next_output_byte = (JOCTET*) newbuf + sp->jpegtables_length;
477         sp->dest.free_in_buffer = (size_t) 1000;
478         sp->jpegtables = newbuf;
479         sp->jpegtables_length += 1000;
480         return (TRUE);
481 }
482
483 static void
484 tables_term_destination(j_compress_ptr cinfo)
485 {
486         JPEGState* sp = (JPEGState*) cinfo;
487
488         /* set tables length to number of bytes actually emitted */
489         sp->jpegtables_length -= (uint32) sp->dest.free_in_buffer;
490 }
491
492 static int
493 TIFFjpeg_tables_dest(JPEGState* sp, TIFF* tif)
494 {
495         (void) tif;
496         /*
497          * Allocate a working buffer for building tables.
498          * Initial size is 1000 bytes, which is usually adequate.
499          */
500         if (sp->jpegtables)
501                 _TIFFfree(sp->jpegtables);
502         sp->jpegtables_length = 1000;
503         sp->jpegtables = (void*) _TIFFmalloc((tmsize_t) sp->jpegtables_length);
504         if (sp->jpegtables == NULL) {
505                 sp->jpegtables_length = 0;
506                 TIFFErrorExt(sp->tif->tif_clientdata, "TIFFjpeg_tables_dest", "No space for JPEGTables");
507                 return (0);
508         }
509         sp->cinfo.c.dest = &sp->dest;
510         sp->dest.init_destination = tables_init_destination;
511         sp->dest.empty_output_buffer = tables_empty_output_buffer;
512         sp->dest.term_destination = tables_term_destination;
513         return (1);
514 }
515
516 /*
517  * JPEG library source data manager.
518  * These routines supply compressed data to libjpeg.
519  */
520
521 static void
522 std_init_source(j_decompress_ptr cinfo)
523 {
524         JPEGState* sp = (JPEGState*) cinfo;
525         TIFF* tif = sp->tif;
526
527         sp->src.next_input_byte = (const JOCTET*) tif->tif_rawdata;
528         sp->src.bytes_in_buffer = (size_t) tif->tif_rawcc;
529 }
530
531 static boolean
532 std_fill_input_buffer(j_decompress_ptr cinfo)
533 {
534         JPEGState* sp = (JPEGState* ) cinfo;
535         static const JOCTET dummy_EOI[2] = { 0xFF, JPEG_EOI };
536
537 #ifdef IPPJ_HUFF
538         /*
539          * The Intel IPP performance library does not necessarily read the whole
540          * input buffer in one pass, so it is possible to get here with data
541          * yet to read. 
542          * 
543          * We just return without doing anything, until the entire buffer has
544          * been read.  
545          * http://trac.osgeo.org/gdal/wiki/JpegIPP
546          */
547         if( sp->src.bytes_in_buffer > 0 ) {
548             return (TRUE);
549         }
550 #endif
551
552         /*
553          * Normally the whole strip/tile is read and so we don't need to do
554          * a fill.  In the case of CHUNKY_STRIP_READ_SUPPORT we might not have
555          * all the data, but the rawdata is refreshed between scanlines and
556          * we push this into the io machinery in JPEGDecode().   
557          * http://trac.osgeo.org/gdal/ticket/3894
558          */
559         
560         WARNMS(cinfo, JWRN_JPEG_EOF);
561         /* insert a fake EOI marker */
562         sp->src.next_input_byte = dummy_EOI;
563         sp->src.bytes_in_buffer = 2;
564         return (TRUE);
565 }
566
567 static void
568 std_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
569 {
570         JPEGState* sp = (JPEGState*) cinfo;
571
572         if (num_bytes > 0) {
573                 if ((size_t)num_bytes > sp->src.bytes_in_buffer) {
574                         /* oops, buffer overrun */
575                         (void) std_fill_input_buffer(cinfo);
576                 } else {
577                         sp->src.next_input_byte += (size_t) num_bytes;
578                         sp->src.bytes_in_buffer -= (size_t) num_bytes;
579                 }
580         }
581 }
582
583 static void
584 std_term_source(j_decompress_ptr cinfo)
585 {
586         /* No work necessary here */
587         (void) cinfo;
588 }
589
590 static void
591 TIFFjpeg_data_src(JPEGState* sp, TIFF* tif)
592 {
593         (void) tif;
594         sp->cinfo.d.src = &sp->src;
595         sp->src.init_source = std_init_source;
596         sp->src.fill_input_buffer = std_fill_input_buffer;
597         sp->src.skip_input_data = std_skip_input_data;
598         sp->src.resync_to_restart = jpeg_resync_to_restart;
599         sp->src.term_source = std_term_source;
600         sp->src.bytes_in_buffer = 0;            /* for safety */
601         sp->src.next_input_byte = NULL;
602 }
603
604 /*
605  * Alternate source manager for reading from JPEGTables.
606  * We can share all the code except for the init routine.
607  */
608
609 static void
610 tables_init_source(j_decompress_ptr cinfo)
611 {
612         JPEGState* sp = (JPEGState*) cinfo;
613
614         sp->src.next_input_byte = (const JOCTET*) sp->jpegtables;
615         sp->src.bytes_in_buffer = (size_t) sp->jpegtables_length;
616 }
617
618 static void
619 TIFFjpeg_tables_src(JPEGState* sp, TIFF* tif)
620 {
621         TIFFjpeg_data_src(sp, tif);
622         sp->src.init_source = tables_init_source;
623 }
624
625 /*
626  * Allocate downsampled-data buffers needed for downsampled I/O.
627  * We use values computed in jpeg_start_compress or jpeg_start_decompress.
628  * We use libjpeg's allocator so that buffers will be released automatically
629  * when done with strip/tile.
630  * This is also a handy place to compute samplesperclump, bytesperline.
631  */
632 static int
633 alloc_downsampled_buffers(TIFF* tif, jpeg_component_info* comp_info,
634                           int num_components)
635 {
636         JPEGState* sp = JState(tif);
637         int ci;
638         jpeg_component_info* compptr;
639         JSAMPARRAY buf;
640         int samples_per_clump = 0;
641
642         for (ci = 0, compptr = comp_info; ci < num_components;
643              ci++, compptr++) {
644                 samples_per_clump += compptr->h_samp_factor *
645                         compptr->v_samp_factor;
646                 buf = TIFFjpeg_alloc_sarray(sp, JPOOL_IMAGE,
647                                 compptr->width_in_blocks * DCTSIZE,
648                                 (JDIMENSION) (compptr->v_samp_factor*DCTSIZE));
649                 if (buf == NULL)
650                         return (0);
651                 sp->ds_buffer[ci] = buf;
652         }
653         sp->samplesperclump = samples_per_clump;
654         return (1);
655 }
656
657
658 /*
659  * JPEG Decoding.
660  */
661
662 #ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
663
664 #define JPEG_MARKER_SOF0 0xC0
665 #define JPEG_MARKER_SOF1 0xC1
666 #define JPEG_MARKER_SOF3 0xC3
667 #define JPEG_MARKER_DHT 0xC4
668 #define JPEG_MARKER_SOI 0xD8
669 #define JPEG_MARKER_SOS 0xDA
670 #define JPEG_MARKER_DQT 0xDB
671 #define JPEG_MARKER_DRI 0xDD
672 #define JPEG_MARKER_APP0 0xE0
673 #define JPEG_MARKER_COM 0xFE
674 struct JPEGFixupTagsSubsamplingData
675 {
676         TIFF* tif;
677         void* buffer;
678         uint32 buffersize;
679         uint8* buffercurrentbyte;
680         uint32 bufferbytesleft;
681         uint64 fileoffset;
682         uint64 filebytesleft;
683         uint8 filepositioned;
684 };
685 static void JPEGFixupTagsSubsampling(TIFF* tif);
686 static int JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData* data);
687 static int JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData* data, uint8* result);
688 static int JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData* data, uint16* result);
689 static void JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData* data, uint16 skiplength);
690
691 #endif
692
693 static int
694 JPEGFixupTags(TIFF* tif)
695 {
696 #ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
697         if ((tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)&&
698             (tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
699             (tif->tif_dir.td_samplesperpixel==3))
700                 JPEGFixupTagsSubsampling(tif);
701 #endif
702         
703         return(1);
704 }
705
706 #ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
707
708 static void
709 JPEGFixupTagsSubsampling(TIFF* tif)
710 {
711         /*
712          * Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in
713          * the TIFF tags, but still use non-default (2,2) values within the jpeg
714          * data stream itself.  In order for TIFF applications to work properly
715          * - for instance to get the strip buffer size right - it is imperative
716          * that the subsampling be available before we start reading the image
717          * data normally.  This function will attempt to analyze the first strip in
718          * order to get the sampling values from the jpeg data stream.
719          *
720          * Note that JPEGPreDeocode() will produce a fairly loud warning when the
721          * discovered sampling does not match the default sampling (2,2) or whatever
722          * was actually in the tiff tags.
723          *
724          * See the bug in bugzilla for details:
725          *
726          * http://bugzilla.remotesensing.org/show_bug.cgi?id=168
727          *
728          * Frank Warmerdam, July 2002
729          * Joris Van Damme, May 2007
730          */
731         static const char module[] = "JPEGFixupTagsSubsampling";
732         struct JPEGFixupTagsSubsamplingData m;
733
734         _TIFFFillStriles( tif );
735         
736         if( tif->tif_dir.td_stripbytecount == NULL
737             || tif->tif_dir.td_stripbytecount[0] == 0 )
738         {
739             /* Do not even try to check if the first strip/tile does not
740                yet exist, as occurs when GDAL has created a new NULL file
741                for instance. */
742             return;
743         }
744
745         m.tif=tif;
746         m.buffersize=2048;
747         m.buffer=_TIFFmalloc(m.buffersize);
748         if (m.buffer==NULL)
749         {
750                 TIFFWarningExt(tif->tif_clientdata,module,
751                     "Unable to allocate memory for auto-correcting of subsampling values; auto-correcting skipped");
752                 return;
753         }
754         m.buffercurrentbyte=NULL;
755         m.bufferbytesleft=0;
756         m.fileoffset=tif->tif_dir.td_stripoffset[0];
757         m.filepositioned=0;
758         m.filebytesleft=tif->tif_dir.td_stripbytecount[0];
759         if (!JPEGFixupTagsSubsamplingSec(&m))
760                 TIFFWarningExt(tif->tif_clientdata,module,
761                     "Unable to auto-correct subsampling values, likely corrupt JPEG compressed data in first strip/tile; auto-correcting skipped");
762         _TIFFfree(m.buffer);
763 }
764
765 static int
766 JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData* data)
767 {
768         static const char module[] = "JPEGFixupTagsSubsamplingSec";
769         uint8 m;
770         while (1)
771         {
772                 while (1)
773                 {
774                         if (!JPEGFixupTagsSubsamplingReadByte(data,&m))
775                                 return(0);
776                         if (m==255)
777                                 break;
778                 }
779                 while (1)
780                 {
781                         if (!JPEGFixupTagsSubsamplingReadByte(data,&m))
782                                 return(0);
783                         if (m!=255)
784                                 break;
785                 }
786                 switch (m)
787                 {
788                         case JPEG_MARKER_SOI:
789                                 /* this type of marker has no data and should be skipped */
790                                 break;
791                         case JPEG_MARKER_COM:
792                         case JPEG_MARKER_APP0:
793                         case JPEG_MARKER_APP0+1:
794                         case JPEG_MARKER_APP0+2:
795                         case JPEG_MARKER_APP0+3:
796                         case JPEG_MARKER_APP0+4:
797                         case JPEG_MARKER_APP0+5:
798                         case JPEG_MARKER_APP0+6:
799                         case JPEG_MARKER_APP0+7:
800                         case JPEG_MARKER_APP0+8:
801                         case JPEG_MARKER_APP0+9:
802                         case JPEG_MARKER_APP0+10:
803                         case JPEG_MARKER_APP0+11:
804                         case JPEG_MARKER_APP0+12:
805                         case JPEG_MARKER_APP0+13:
806                         case JPEG_MARKER_APP0+14:
807                         case JPEG_MARKER_APP0+15:
808                         case JPEG_MARKER_DQT:
809                         case JPEG_MARKER_SOS:
810                         case JPEG_MARKER_DHT:
811                         case JPEG_MARKER_DRI:
812                                 /* this type of marker has data, but it has no use to us and should be skipped */
813                                 {
814                                         uint16 n;
815                                         if (!JPEGFixupTagsSubsamplingReadWord(data,&n))
816                                                 return(0);
817                                         if (n<2)
818                                                 return(0);
819                                         n-=2;
820                                         if (n>0)
821                                                 JPEGFixupTagsSubsamplingSkip(data,n);
822                                 }
823                                 break;
824                         case JPEG_MARKER_SOF0:
825                         case JPEG_MARKER_SOF1:
826                                 /* this marker contains the subsampling factors we're scanning for */
827                                 {
828                                         uint16 n;
829                                         uint16 o;
830                                         uint8 p;
831                                         uint8 ph,pv;
832                                         if (!JPEGFixupTagsSubsamplingReadWord(data,&n))
833                                                 return(0);
834                                         if (n!=8+data->tif->tif_dir.td_samplesperpixel*3)
835                                                 return(0);
836                                         JPEGFixupTagsSubsamplingSkip(data,7);
837                                         if (!JPEGFixupTagsSubsamplingReadByte(data,&p))
838                                                 return(0);
839                                         ph=(p>>4);
840                                         pv=(p&15);
841                                         JPEGFixupTagsSubsamplingSkip(data,1);
842                                         for (o=1; o<data->tif->tif_dir.td_samplesperpixel; o++)
843                                         {
844                                                 JPEGFixupTagsSubsamplingSkip(data,1);
845                                                 if (!JPEGFixupTagsSubsamplingReadByte(data,&p))
846                                                         return(0);
847                                                 if (p!=0x11)
848                                                 {
849                                                         TIFFWarningExt(data->tif->tif_clientdata,module,
850                                                             "Subsampling values inside JPEG compressed data have no TIFF equivalent, auto-correction of TIFF subsampling values failed");
851                                                         return(1);
852                                                 }
853                                                 JPEGFixupTagsSubsamplingSkip(data,1);
854                                         }
855                                         if (((ph!=1)&&(ph!=2)&&(ph!=4))||((pv!=1)&&(pv!=2)&&(pv!=4)))
856                                         {
857                                                 TIFFWarningExt(data->tif->tif_clientdata,module,
858                                                     "Subsampling values inside JPEG compressed data have no TIFF equivalent, auto-correction of TIFF subsampling values failed");
859                                                 return(1);
860                                         }
861                                         if ((ph!=data->tif->tif_dir.td_ycbcrsubsampling[0])||(pv!=data->tif->tif_dir.td_ycbcrsubsampling[1]))
862                                         {
863                                                 TIFFWarningExt(data->tif->tif_clientdata,module,
864                                                     "Auto-corrected former TIFF subsampling values [%d,%d] to match subsampling values inside JPEG compressed data [%d,%d]",
865                                                     (int)data->tif->tif_dir.td_ycbcrsubsampling[0],
866                                                     (int)data->tif->tif_dir.td_ycbcrsubsampling[1],
867                                                     (int)ph,(int)pv);
868                                                 data->tif->tif_dir.td_ycbcrsubsampling[0]=ph;
869                                                 data->tif->tif_dir.td_ycbcrsubsampling[1]=pv;
870                                         }
871                                 }
872                                 return(1);
873                         default:
874                                 return(0);
875                 }
876         }
877 }
878
879 static int
880 JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData* data, uint8* result)
881 {
882         if (data->bufferbytesleft==0)
883         {
884                 uint32 m;
885                 if (data->filebytesleft==0)
886                         return(0);
887                 if (!data->filepositioned)
888                 {
889                         TIFFSeekFile(data->tif,data->fileoffset,SEEK_SET);
890                         data->filepositioned=1;
891                 }
892                 m=data->buffersize;
893                 if ((uint64)m>data->filebytesleft)
894                         m=(uint32)data->filebytesleft;
895                 assert(m<0x80000000UL);
896                 if (TIFFReadFile(data->tif,data->buffer,(tmsize_t)m)!=(tmsize_t)m)
897                         return(0);
898                 data->buffercurrentbyte=data->buffer;
899                 data->bufferbytesleft=m;
900                 data->fileoffset+=m;
901                 data->filebytesleft-=m;
902         }
903         *result=*data->buffercurrentbyte;
904         data->buffercurrentbyte++;
905         data->bufferbytesleft--;
906         return(1);
907 }
908
909 static int
910 JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData* data, uint16* result)
911 {
912         uint8 ma;
913         uint8 mb;
914         if (!JPEGFixupTagsSubsamplingReadByte(data,&ma))
915                 return(0);
916         if (!JPEGFixupTagsSubsamplingReadByte(data,&mb))
917                 return(0);
918         *result=(ma<<8)|mb;
919         return(1);
920 }
921
922 static void
923 JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData* data, uint16 skiplength)
924 {
925         if ((uint32)skiplength<=data->bufferbytesleft)
926         {
927                 data->buffercurrentbyte+=skiplength;
928                 data->bufferbytesleft-=skiplength;
929         }
930         else
931         {
932                 uint16 m;
933                 m=skiplength-data->bufferbytesleft;
934                 if (m<=data->filebytesleft)
935                 {
936                         data->bufferbytesleft=0;
937                         data->fileoffset+=m;
938                         data->filebytesleft-=m;
939                         data->filepositioned=0;
940                 }
941                 else
942                 {
943                         data->bufferbytesleft=0;
944                         data->filebytesleft=0;
945                 }
946         }
947 }
948
949 #endif
950
951
952 static int
953 JPEGSetupDecode(TIFF* tif)
954 {
955         JPEGState* sp = JState(tif);
956         TIFFDirectory *td = &tif->tif_dir;
957
958 #if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFInitJPEG)
959         if( tif->tif_dir.td_bitspersample == 12 )
960             return TIFFReInitJPEG_12( tif, COMPRESSION_JPEG, 0 );
961 #endif
962
963         JPEGInitializeLibJPEG( tif, TRUE );
964
965         assert(sp != NULL);
966         assert(sp->cinfo.comm.is_decompressor);
967
968         /* Read JPEGTables if it is present */
969         if (TIFFFieldSet(tif,FIELD_JPEGTABLES)) {
970                 TIFFjpeg_tables_src(sp, tif);
971                 if(TIFFjpeg_read_header(sp,FALSE) != JPEG_HEADER_TABLES_ONLY) {
972                         TIFFErrorExt(tif->tif_clientdata, "JPEGSetupDecode", "Bogus JPEGTables field");
973                         return (0);
974                 }
975         }
976
977         /* Grab parameters that are same for all strips/tiles */
978         sp->photometric = td->td_photometric;
979         switch (sp->photometric) {
980         case PHOTOMETRIC_YCBCR:
981                 sp->h_sampling = td->td_ycbcrsubsampling[0];
982                 sp->v_sampling = td->td_ycbcrsubsampling[1];
983                 break;
984         default:
985                 /* TIFF 6.0 forbids subsampling of all other color spaces */
986                 sp->h_sampling = 1;
987                 sp->v_sampling = 1;
988                 break;
989         }
990
991         /* Set up for reading normal data */
992         TIFFjpeg_data_src(sp, tif);
993         tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */
994         return (1);
995 }
996
997 /*
998  * Set up for decoding a strip or tile.
999  */
1000 static int
1001 JPEGPreDecode(TIFF* tif, uint16 s)
1002 {
1003         JPEGState *sp = JState(tif);
1004         TIFFDirectory *td = &tif->tif_dir;
1005         static const char module[] = "JPEGPreDecode";
1006         uint32 segment_width, segment_height;
1007         int downsampled_output;
1008         int ci;
1009
1010         assert(sp != NULL);
1011   
1012         if (sp->cinfo.comm.is_decompressor == 0)
1013         {
1014                 tif->tif_setupdecode( tif );
1015         }
1016   
1017         assert(sp->cinfo.comm.is_decompressor);
1018         /*
1019          * Reset decoder state from any previous strip/tile,
1020          * in case application didn't read the whole strip.
1021          */
1022         if (!TIFFjpeg_abort(sp))
1023                 return (0);
1024         /*
1025          * Read the header for this strip/tile.
1026          */
1027         
1028         if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
1029                 return (0);
1030
1031         tif->tif_rawcp = (uint8*) sp->src.next_input_byte;
1032         tif->tif_rawcc = sp->src.bytes_in_buffer;
1033
1034         /*
1035          * Check image parameters and set decompression parameters.
1036          */
1037         segment_width = td->td_imagewidth;
1038         segment_height = td->td_imagelength - tif->tif_row;
1039         if (isTiled(tif)) {
1040                 segment_width = td->td_tilewidth;
1041                 segment_height = td->td_tilelength;
1042                 sp->bytesperline = TIFFTileRowSize(tif);
1043         } else {
1044                 if (segment_height > td->td_rowsperstrip)
1045                         segment_height = td->td_rowsperstrip;
1046                 sp->bytesperline = TIFFScanlineSize(tif);
1047         }
1048         if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) {
1049                 /*
1050                  * For PC 2, scale down the expected strip/tile size
1051                  * to match a downsampled component
1052                  */
1053                 segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
1054                 segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
1055         }
1056         if (sp->cinfo.d.image_width < segment_width ||
1057             sp->cinfo.d.image_height < segment_height) {
1058                 TIFFWarningExt(tif->tif_clientdata, module,
1059                                "Improper JPEG strip/tile size, "
1060                                "expected %dx%d, got %dx%d",
1061                                segment_width, segment_height,
1062                                sp->cinfo.d.image_width,
1063                                sp->cinfo.d.image_height);
1064         } 
1065         if (sp->cinfo.d.image_width > segment_width ||
1066             sp->cinfo.d.image_height > segment_height) {
1067                 /*
1068                  * This case could be dangerous, if the strip or tile size has
1069                  * been reported as less than the amount of data jpeg will
1070                  * return, some potential security issues arise. Catch this
1071                  * case and error out.
1072                  */
1073                 TIFFErrorExt(tif->tif_clientdata, module,
1074                              "JPEG strip/tile size exceeds expected dimensions,"
1075                              " expected %dx%d, got %dx%d",
1076                              segment_width, segment_height,
1077                              sp->cinfo.d.image_width, sp->cinfo.d.image_height);
1078                 return (0);
1079         }
1080         if (sp->cinfo.d.num_components !=
1081             (td->td_planarconfig == PLANARCONFIG_CONTIG ?
1082              td->td_samplesperpixel : 1)) {
1083                 TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG component count");
1084                 return (0);
1085         }
1086 #ifdef JPEG_LIB_MK1
1087         if (12 != td->td_bitspersample && 8 != td->td_bitspersample) {
1088                 TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
1089                 return (0);
1090         }
1091         sp->cinfo.d.data_precision = td->td_bitspersample;
1092         sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
1093 #else
1094         if (sp->cinfo.d.data_precision != td->td_bitspersample) {
1095                 TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
1096                 return (0);
1097         }
1098 #endif
1099         if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
1100                 /* Component 0 should have expected sampling factors */
1101                 if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
1102                     sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) {
1103                         TIFFErrorExt(tif->tif_clientdata, module,
1104                                        "Improper JPEG sampling factors %d,%d\n"
1105                                        "Apparently should be %d,%d.",
1106                                        sp->cinfo.d.comp_info[0].h_samp_factor,
1107                                        sp->cinfo.d.comp_info[0].v_samp_factor,
1108                                        sp->h_sampling, sp->v_sampling);
1109                         return (0);
1110                 }
1111                 /* Rest should have sampling factors 1,1 */
1112                 for (ci = 1; ci < sp->cinfo.d.num_components; ci++) {
1113                         if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
1114                             sp->cinfo.d.comp_info[ci].v_samp_factor != 1) {
1115                                 TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG sampling factors");
1116                                 return (0);
1117                         }
1118                 }
1119         } else {
1120                 /* PC 2's single component should have sampling factors 1,1 */
1121                 if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
1122                     sp->cinfo.d.comp_info[0].v_samp_factor != 1) {
1123                         TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG sampling factors");
1124                         return (0);
1125                 }
1126         }
1127         downsampled_output = FALSE;
1128         if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1129             sp->photometric == PHOTOMETRIC_YCBCR &&
1130             sp->jpegcolormode == JPEGCOLORMODE_RGB) {
1131                 /* Convert YCbCr to RGB */
1132                 sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
1133                 sp->cinfo.d.out_color_space = JCS_RGB;
1134         } else {
1135                 /* Suppress colorspace handling */
1136                 sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
1137                 sp->cinfo.d.out_color_space = JCS_UNKNOWN;
1138                 if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1139                     (sp->h_sampling != 1 || sp->v_sampling != 1))
1140                         downsampled_output = TRUE;
1141                 /* XXX what about up-sampling? */
1142         }
1143         if (downsampled_output) {
1144                 /* Need to use raw-data interface to libjpeg */
1145                 sp->cinfo.d.raw_data_out = TRUE;
1146 #if JPEG_LIB_VERSION >= 70
1147                 sp->cinfo.d.do_fancy_upsampling = FALSE;
1148 #endif /* JPEG_LIB_VERSION >= 70 */
1149                 tif->tif_decoderow = DecodeRowError;
1150                 tif->tif_decodestrip = JPEGDecodeRaw;
1151                 tif->tif_decodetile = JPEGDecodeRaw;
1152         } else {
1153                 /* Use normal interface to libjpeg */
1154                 sp->cinfo.d.raw_data_out = FALSE;
1155                 tif->tif_decoderow = JPEGDecode;
1156                 tif->tif_decodestrip = JPEGDecode;
1157                 tif->tif_decodetile = JPEGDecode;  
1158         }
1159         /* Start JPEG decompressor */
1160         if (!TIFFjpeg_start_decompress(sp))
1161                 return (0);
1162         /* Allocate downsampled-data buffers if needed */
1163         if (downsampled_output) {
1164                 if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info,
1165                                                sp->cinfo.d.num_components))
1166                         return (0);
1167                 sp->scancount = DCTSIZE;        /* mark buffer empty */
1168         }
1169         return (1);
1170 }
1171
1172 /*
1173  * Decode a chunk of pixels.
1174  * "Standard" case: returned data is not downsampled.
1175  */
1176 /*ARGSUSED*/ static int
1177 JPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
1178 {
1179         JPEGState *sp = JState(tif);
1180         tmsize_t nrows;
1181         (void) s;
1182
1183         /*
1184         ** Update available information, buffer may have been refilled
1185         ** between decode requests
1186         */
1187         sp->src.next_input_byte = (const JOCTET*) tif->tif_rawcp;
1188         sp->src.bytes_in_buffer = (size_t) tif->tif_rawcc;
1189
1190         if( sp->bytesperline == 0 )
1191                 return 0;
1192         
1193         nrows = cc / sp->bytesperline;
1194         if (cc % sp->bytesperline)
1195                 TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline not read");
1196
1197         if( nrows > (tmsize_t) sp->cinfo.d.image_height )
1198                 nrows = sp->cinfo.d.image_height;
1199
1200         /* data is expected to be read in multiples of a scanline */
1201         if (nrows)
1202         {
1203                 JSAMPROW line_work_buf = NULL;
1204
1205                 /*
1206                  * For 6B, only use temporary buffer for 12 bit imagery.
1207                  * For Mk1 always use it.
1208                  */
1209 #if !defined(JPEG_LIB_MK1)
1210                 if( sp->cinfo.d.data_precision == 12 )
1211 #endif
1212                 {
1213                         line_work_buf = (JSAMPROW)
1214                             _TIFFmalloc(sizeof(short) * sp->cinfo.d.output_width
1215                             * sp->cinfo.d.num_components );
1216                 }
1217
1218                 do {
1219                         if( line_work_buf != NULL )
1220                         {
1221                                 /*
1222                                  * In the MK1 case, we aways read into a 16bit buffer, and then
1223                                  * pack down to 12bit or 8bit.  In 6B case we only read into 16
1224                                  * bit buffer for 12bit data, which we need to repack.
1225                                 */
1226                                 if (TIFFjpeg_read_scanlines(sp, &line_work_buf, 1) != 1)
1227                                         return (0);
1228
1229                                 if( sp->cinfo.d.data_precision == 12 )
1230                                 {
1231                                         int value_pairs = (sp->cinfo.d.output_width
1232                                             * sp->cinfo.d.num_components) / 2;
1233                                         int iPair;
1234
1235                                         for( iPair = 0; iPair < value_pairs; iPair++ )
1236                                         {
1237                                                 unsigned char *out_ptr =
1238                                                     ((unsigned char *) buf) + iPair * 3;
1239                                                 JSAMPLE *in_ptr = line_work_buf + iPair * 2;
1240
1241                                                 out_ptr[0] = (in_ptr[0] & 0xff0) >> 4;
1242                                                 out_ptr[1] = ((in_ptr[0] & 0xf) << 4)
1243                                                     | ((in_ptr[1] & 0xf00) >> 8);
1244                                                 out_ptr[2] = ((in_ptr[1] & 0xff) >> 0);
1245                                         }
1246                                 }
1247                                 else if( sp->cinfo.d.data_precision == 8 )
1248                                 {
1249                                         int value_count = (sp->cinfo.d.output_width
1250                                             * sp->cinfo.d.num_components);
1251                                         int iValue;
1252
1253                                         for( iValue = 0; iValue < value_count; iValue++ )
1254                                         {
1255                                                 ((unsigned char *) buf)[iValue] =
1256                                                     line_work_buf[iValue] & 0xff;
1257                                         }
1258                                 }
1259                         }
1260                         else
1261                         {
1262                                 /*
1263                                  * In the libjpeg6b 8bit case.  We read directly into the
1264                                  * TIFF buffer.
1265                                 */
1266                                 JSAMPROW bufptr = (JSAMPROW)buf;
1267
1268                                 if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1)
1269                                         return (0);
1270                         }
1271
1272                         ++tif->tif_row;
1273                         buf += sp->bytesperline;
1274                         cc -= sp->bytesperline;
1275                 } while (--nrows > 0);
1276
1277                 if( line_work_buf != NULL )
1278                         _TIFFfree( line_work_buf );
1279         }
1280
1281         /* Update information on consumed data */
1282         tif->tif_rawcp = (uint8*) sp->src.next_input_byte;
1283         tif->tif_rawcc = sp->src.bytes_in_buffer;
1284                 
1285         /* Close down the decompressor if we've finished the strip or tile. */
1286         return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
1287             || TIFFjpeg_finish_decompress(sp);
1288 }
1289
1290 /*ARGSUSED*/ static int
1291 DecodeRowError(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
1292
1293 {
1294     (void) buf;
1295     (void) cc;
1296     (void) s;
1297
1298     TIFFErrorExt(tif->tif_clientdata, "TIFFReadScanline",
1299                  "scanline oriented access is not supported for downsampled JPEG compressed images, consider enabling TIFF_JPEGCOLORMODE as JPEGCOLORMODE_RGB." );
1300     return 0;
1301 }
1302
1303 /*
1304  * Decode a chunk of pixels.
1305  * Returned data is downsampled per sampling factors.
1306  */
1307 /*ARGSUSED*/ static int
1308 JPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
1309 {
1310         JPEGState *sp = JState(tif);
1311         tmsize_t nrows;
1312         (void) s;
1313
1314         /* data is expected to be read in multiples of a scanline */
1315         if ( (nrows = sp->cinfo.d.image_height) ) {
1316
1317                 /* Cb,Cr both have sampling factors 1, so this is correct */
1318                 JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width;            
1319                 int samples_per_clump = sp->samplesperclump;
1320
1321 #if defined(JPEG_LIB_MK1_OR_12BIT)
1322                 unsigned short* tmpbuf = _TIFFmalloc(sizeof(unsigned short) *
1323                                                      sp->cinfo.d.output_width *
1324                                                      sp->cinfo.d.num_components);
1325                 if(tmpbuf==NULL) {
1326                         TIFFErrorExt(tif->tif_clientdata, "JPEGDecodeRaw",
1327                                      "Out of memory");
1328                         return 0;
1329                 }
1330 #endif
1331
1332                 do {
1333                         jpeg_component_info *compptr;
1334                         int ci, clumpoffset;
1335
1336                         if( cc < sp->bytesperline ) {
1337                                 TIFFErrorExt(tif->tif_clientdata, "JPEGDecodeRaw",
1338                                              "application buffer not large enough for all data.");
1339                                 return 0;
1340                         }
1341
1342                         /* Reload downsampled-data buffer if needed */
1343                         if (sp->scancount >= DCTSIZE) {
1344                                 int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE;
1345                                 if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n) != n)
1346                                         return (0);
1347                                 sp->scancount = 0;
1348                         }
1349                         /*
1350                          * Fastest way to unseparate data is to make one pass
1351                          * over the scanline for each row of each component.
1352                          */
1353                         clumpoffset = 0;    /* first sample in clump */
1354                         for (ci = 0, compptr = sp->cinfo.d.comp_info;
1355                              ci < sp->cinfo.d.num_components;
1356                              ci++, compptr++) {
1357                                 int hsamp = compptr->h_samp_factor;
1358                                 int vsamp = compptr->v_samp_factor;
1359                                 int ypos;
1360
1361                                 for (ypos = 0; ypos < vsamp; ypos++) {
1362                                         JSAMPLE *inptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos];
1363                                         JDIMENSION nclump;
1364 #if defined(JPEG_LIB_MK1_OR_12BIT)
1365                                         JSAMPLE *outptr = (JSAMPLE*)tmpbuf + clumpoffset;
1366 #else
1367                                         JSAMPLE *outptr = (JSAMPLE*)buf + clumpoffset;
1368                                         if (cc < (tmsize_t) (clumpoffset + samples_per_clump*(clumps_per_line-1) + hsamp)) {
1369                                                 TIFFErrorExt(tif->tif_clientdata, "JPEGDecodeRaw",
1370                                                              "application buffer not large enough for all data, possible subsampling issue");
1371                                                 return 0;
1372                                         }
1373 #endif
1374
1375                                         if (hsamp == 1) {
1376                                                 /* fast path for at least Cb and Cr */
1377                                                 for (nclump = clumps_per_line; nclump-- > 0; ) {
1378                                                         outptr[0] = *inptr++;
1379                                                         outptr += samples_per_clump;
1380                                                 }
1381                                         } else {
1382                                                 int xpos;
1383
1384                                                 /* general case */
1385                                                 for (nclump = clumps_per_line; nclump-- > 0; ) {
1386                                                         for (xpos = 0; xpos < hsamp; xpos++)
1387                                                                 outptr[xpos] = *inptr++;
1388                                                         outptr += samples_per_clump;
1389                                                 }
1390                                         }
1391                                         clumpoffset += hsamp;
1392                                 }
1393                         }
1394
1395 #if defined(JPEG_LIB_MK1_OR_12BIT)
1396                         {
1397                                 if (sp->cinfo.d.data_precision == 8)
1398                                 {
1399                                         int i=0;
1400                                         int len = sp->cinfo.d.output_width * sp->cinfo.d.num_components;
1401                                         for (i=0; i<len; i++)
1402                                         {
1403                                                 ((unsigned char*)buf)[i] = tmpbuf[i] & 0xff;
1404                                         }
1405                                 }
1406                                 else
1407                                 {         /* 12-bit */
1408                                         int value_pairs = (sp->cinfo.d.output_width
1409                                                            * sp->cinfo.d.num_components) / 2;
1410                                         int iPair;
1411                                         for( iPair = 0; iPair < value_pairs; iPair++ )
1412                                         {
1413                                                 unsigned char *out_ptr = ((unsigned char *) buf) + iPair * 3;
1414                                                 JSAMPLE *in_ptr = (JSAMPLE *) (tmpbuf + iPair * 2);
1415                                                 out_ptr[0] = (in_ptr[0] & 0xff0) >> 4;
1416                                                 out_ptr[1] = ((in_ptr[0] & 0xf) << 4)
1417                                                         | ((in_ptr[1] & 0xf00) >> 8);
1418                                                 out_ptr[2] = ((in_ptr[1] & 0xff) >> 0);
1419                                         }
1420                                 }
1421                         }
1422 #endif
1423
1424                         sp->scancount ++;
1425                         tif->tif_row += sp->v_sampling;
1426
1427                         buf += sp->bytesperline;
1428                         cc -= sp->bytesperline;
1429
1430                         nrows -= sp->v_sampling;
1431                 } while (nrows > 0);
1432
1433 #if defined(JPEG_LIB_MK1_OR_12BIT)
1434                 _TIFFfree(tmpbuf);
1435 #endif
1436
1437         }
1438
1439         /* Close down the decompressor if done. */
1440         return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
1441                 || TIFFjpeg_finish_decompress(sp);
1442 }
1443
1444
1445 /*
1446  * JPEG Encoding.
1447  */
1448
1449 static void
1450 unsuppress_quant_table (JPEGState* sp, int tblno)
1451 {
1452         JQUANT_TBL* qtbl;
1453
1454         if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1455                 qtbl->sent_table = FALSE;
1456 }
1457
1458 static void
1459 unsuppress_huff_table (JPEGState* sp, int tblno)
1460 {
1461         JHUFF_TBL* htbl;
1462
1463         if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
1464                 htbl->sent_table = FALSE;
1465         if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
1466                 htbl->sent_table = FALSE;
1467 }
1468
1469 static int
1470 prepare_JPEGTables(TIFF* tif)
1471 {
1472         JPEGState* sp = JState(tif);
1473
1474         /* Initialize quant tables for current quality setting */
1475         if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE))
1476                 return (0);
1477         /* Mark only the tables we want for output */
1478         /* NB: chrominance tables are currently used only with YCbCr */
1479         if (!TIFFjpeg_suppress_tables(sp, TRUE))
1480                 return (0);
1481         if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) {
1482                 unsuppress_quant_table(sp, 0);
1483                 if (sp->photometric == PHOTOMETRIC_YCBCR)
1484                         unsuppress_quant_table(sp, 1);
1485         }
1486         if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) {
1487                 unsuppress_huff_table(sp, 0);
1488                 if (sp->photometric == PHOTOMETRIC_YCBCR)
1489                         unsuppress_huff_table(sp, 1);
1490         }
1491         /* Direct libjpeg output into jpegtables */
1492         if (!TIFFjpeg_tables_dest(sp, tif))
1493                 return (0);
1494         /* Emit tables-only datastream */
1495         if (!TIFFjpeg_write_tables(sp))
1496                 return (0);
1497
1498         return (1);
1499 }
1500
1501 static int
1502 JPEGSetupEncode(TIFF* tif)
1503 {
1504         JPEGState* sp = JState(tif);
1505         TIFFDirectory *td = &tif->tif_dir;
1506         static const char module[] = "JPEGSetupEncode";
1507
1508 #if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFInitJPEG)
1509         if( tif->tif_dir.td_bitspersample == 12 )
1510             return TIFFReInitJPEG_12( tif, COMPRESSION_JPEG, 1 );
1511 #endif
1512
1513         JPEGInitializeLibJPEG( tif, FALSE );
1514
1515         assert(sp != NULL);
1516         assert(!sp->cinfo.comm.is_decompressor);
1517
1518         /*
1519          * Initialize all JPEG parameters to default values.
1520          * Note that jpeg_set_defaults needs legal values for
1521          * in_color_space and input_components.
1522          */
1523         sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1524         sp->cinfo.c.input_components = 1;
1525         if (!TIFFjpeg_set_defaults(sp))
1526                 return (0);
1527         /* Set per-file parameters */
1528         sp->photometric = td->td_photometric;
1529         switch (sp->photometric) {
1530         case PHOTOMETRIC_YCBCR:
1531                 sp->h_sampling = td->td_ycbcrsubsampling[0];
1532                 sp->v_sampling = td->td_ycbcrsubsampling[1];
1533                 /*
1534                  * A ReferenceBlackWhite field *must* be present since the
1535                  * default value is inappropriate for YCbCr.  Fill in the
1536                  * proper value if application didn't set it.
1537                  */
1538                 {
1539                         float *ref;
1540                         if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE,
1541                                           &ref)) {
1542                                 float refbw[6];
1543                                 long top = 1L << td->td_bitspersample;
1544                                 refbw[0] = 0;
1545                                 refbw[1] = (float)(top-1L);
1546                                 refbw[2] = (float)(top>>1);
1547                                 refbw[3] = refbw[1];
1548                                 refbw[4] = refbw[2];
1549                                 refbw[5] = refbw[1];
1550                                 TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE,
1551                                              refbw);
1552                         }
1553                 }
1554                 break;
1555         case PHOTOMETRIC_PALETTE:               /* disallowed by Tech Note */
1556         case PHOTOMETRIC_MASK:
1557                 TIFFErrorExt(tif->tif_clientdata, module,
1558                           "PhotometricInterpretation %d not allowed for JPEG",
1559                           (int) sp->photometric);
1560                 return (0);
1561         default:
1562                 /* TIFF 6.0 forbids subsampling of all other color spaces */
1563                 sp->h_sampling = 1;
1564                 sp->v_sampling = 1;
1565                 break;
1566         }
1567
1568         /* Verify miscellaneous parameters */
1569
1570         /*
1571          * This would need work if libtiff ever supports different
1572          * depths for different components, or if libjpeg ever supports
1573          * run-time selection of depth.  Neither is imminent.
1574          */
1575 #ifdef JPEG_LIB_MK1
1576         /* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */
1577         if (td->td_bitspersample != 8 && td->td_bitspersample != 12) 
1578 #else
1579         if (td->td_bitspersample != BITS_IN_JSAMPLE )
1580 #endif
1581         {
1582                 TIFFErrorExt(tif->tif_clientdata, module, "BitsPerSample %d not allowed for JPEG",
1583                           (int) td->td_bitspersample);
1584                 return (0);
1585         }
1586         sp->cinfo.c.data_precision = td->td_bitspersample;
1587 #ifdef JPEG_LIB_MK1
1588         sp->cinfo.c.bits_in_jsample = td->td_bitspersample;
1589 #endif
1590         if (isTiled(tif)) {
1591                 if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0) {
1592                         TIFFErrorExt(tif->tif_clientdata, module,
1593                                   "JPEG tile height must be multiple of %d",
1594                                   sp->v_sampling * DCTSIZE);
1595                         return (0);
1596                 }
1597                 if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0) {
1598                         TIFFErrorExt(tif->tif_clientdata, module,
1599                                   "JPEG tile width must be multiple of %d",
1600                                   sp->h_sampling * DCTSIZE);
1601                         return (0);
1602                 }
1603         } else {
1604                 if (td->td_rowsperstrip < td->td_imagelength &&
1605                     (td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0) {
1606                         TIFFErrorExt(tif->tif_clientdata, module,
1607                                   "RowsPerStrip must be multiple of %d for JPEG",
1608                                   sp->v_sampling * DCTSIZE);
1609                         return (0);
1610                 }
1611         }
1612
1613         /* Create a JPEGTables field if appropriate */
1614         if (sp->jpegtablesmode & (JPEGTABLESMODE_QUANT|JPEGTABLESMODE_HUFF)) {
1615                 if( sp->jpegtables == NULL
1616                     || memcmp(sp->jpegtables,"\0\0\0\0\0\0\0\0\0",8) == 0 )
1617                 {
1618                         if (!prepare_JPEGTables(tif))
1619                                 return (0);
1620                         /* Mark the field present */
1621                         /* Can't use TIFFSetField since BEENWRITING is already set! */
1622                         tif->tif_flags |= TIFF_DIRTYDIRECT;
1623                         TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
1624                 }
1625         } else {
1626                 /* We do not support application-supplied JPEGTables, */
1627                 /* so mark the field not present */
1628                 TIFFClrFieldBit(tif, FIELD_JPEGTABLES);
1629         }
1630
1631         /* Direct libjpeg output to libtiff's output buffer */
1632         TIFFjpeg_data_dest(sp, tif);
1633
1634         return (1);
1635 }
1636
1637 /*
1638  * Set encoding state at the start of a strip or tile.
1639  */
1640 static int
1641 JPEGPreEncode(TIFF* tif, uint16 s)
1642 {
1643         JPEGState *sp = JState(tif);
1644         TIFFDirectory *td = &tif->tif_dir;
1645         static const char module[] = "JPEGPreEncode";
1646         uint32 segment_width, segment_height;
1647         int downsampled_input;
1648
1649         assert(sp != NULL);
1650   
1651         if (sp->cinfo.comm.is_decompressor == 1)
1652         {
1653                 tif->tif_setupencode( tif );
1654         }
1655   
1656         assert(!sp->cinfo.comm.is_decompressor);
1657         /*
1658          * Set encoding parameters for this strip/tile.
1659          */
1660         if (isTiled(tif)) {
1661                 segment_width = td->td_tilewidth;
1662                 segment_height = td->td_tilelength;
1663                 sp->bytesperline = TIFFTileRowSize(tif);
1664         } else {
1665                 segment_width = td->td_imagewidth;
1666                 segment_height = td->td_imagelength - tif->tif_row;
1667                 if (segment_height > td->td_rowsperstrip)
1668                         segment_height = td->td_rowsperstrip;
1669                 sp->bytesperline = TIFFScanlineSize(tif);
1670         }
1671         if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) {
1672                 /* for PC 2, scale down the strip/tile size
1673                  * to match a downsampled component
1674                  */
1675                 segment_width = TIFFhowmany_32(segment_width, sp->h_sampling); 
1676                 segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
1677         }
1678         if (segment_width > 65535 || segment_height > 65535) {
1679                 TIFFErrorExt(tif->tif_clientdata, module, "Strip/tile too large for JPEG");
1680                 return (0);
1681         }
1682         sp->cinfo.c.image_width = segment_width;
1683         sp->cinfo.c.image_height = segment_height;
1684         downsampled_input = FALSE;
1685         if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
1686                 sp->cinfo.c.input_components = td->td_samplesperpixel;
1687                 if (sp->photometric == PHOTOMETRIC_YCBCR) {
1688                         if (sp->jpegcolormode == JPEGCOLORMODE_RGB) {
1689                                 sp->cinfo.c.in_color_space = JCS_RGB;
1690                         } else {
1691                                 sp->cinfo.c.in_color_space = JCS_YCbCr;
1692                                 if (sp->h_sampling != 1 || sp->v_sampling != 1)
1693                                         downsampled_input = TRUE;
1694                         }
1695                         if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr))
1696                                 return (0);
1697                         /*
1698                          * Set Y sampling factors;
1699                          * we assume jpeg_set_colorspace() set the rest to 1
1700                          */
1701                         sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
1702                         sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
1703                 } else {
1704                         if ((td->td_photometric == PHOTOMETRIC_MINISWHITE || td->td_photometric == PHOTOMETRIC_MINISBLACK) && td->td_samplesperpixel == 1)
1705                                 sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
1706                         else if (td->td_photometric == PHOTOMETRIC_RGB && td->td_samplesperpixel == 3)
1707                                 sp->cinfo.c.in_color_space = JCS_RGB;
1708                         else if (td->td_photometric == PHOTOMETRIC_SEPARATED && td->td_samplesperpixel == 4)
1709                                 sp->cinfo.c.in_color_space = JCS_CMYK;
1710                         else
1711                                 sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1712                         if (!TIFFjpeg_set_colorspace(sp, sp->cinfo.c.in_color_space))
1713                                 return (0);
1714                         /* jpeg_set_colorspace set all sampling factors to 1 */
1715                 }
1716         } else {
1717                 sp->cinfo.c.input_components = 1;
1718                 sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1719                 if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
1720                         return (0);
1721                 sp->cinfo.c.comp_info[0].component_id = s;
1722                 /* jpeg_set_colorspace() set sampling factors to 1 */
1723                 if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0) {
1724                         sp->cinfo.c.comp_info[0].quant_tbl_no = 1;
1725                         sp->cinfo.c.comp_info[0].dc_tbl_no = 1;
1726                         sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
1727                 }
1728         }
1729         /* ensure libjpeg won't write any extraneous markers */
1730         sp->cinfo.c.write_JFIF_header = FALSE;
1731         sp->cinfo.c.write_Adobe_marker = FALSE;
1732         /* set up table handling correctly */
1733         if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE))
1734                 return (0);
1735         if (! (sp->jpegtablesmode & JPEGTABLESMODE_QUANT)) {
1736                 unsuppress_quant_table(sp, 0);
1737                 unsuppress_quant_table(sp, 1);
1738         }
1739         if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF)
1740                 sp->cinfo.c.optimize_coding = FALSE;
1741         else
1742                 sp->cinfo.c.optimize_coding = TRUE;
1743         if (downsampled_input) {
1744                 /* Need to use raw-data interface to libjpeg */
1745                 sp->cinfo.c.raw_data_in = TRUE;
1746                 tif->tif_encoderow = JPEGEncodeRaw;
1747                 tif->tif_encodestrip = JPEGEncodeRaw;
1748                 tif->tif_encodetile = JPEGEncodeRaw;
1749         } else {
1750                 /* Use normal interface to libjpeg */
1751                 sp->cinfo.c.raw_data_in = FALSE;
1752                 tif->tif_encoderow = JPEGEncode;
1753                 tif->tif_encodestrip = JPEGEncode;
1754                 tif->tif_encodetile = JPEGEncode;
1755         }
1756         /* Start JPEG compressor */
1757         if (!TIFFjpeg_start_compress(sp, FALSE))
1758                 return (0);
1759         /* Allocate downsampled-data buffers if needed */
1760         if (downsampled_input) {
1761                 if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info,
1762                                                sp->cinfo.c.num_components))
1763                         return (0);
1764         }
1765         sp->scancount = 0;
1766
1767         return (1);
1768 }
1769
1770 /*
1771  * Encode a chunk of pixels.
1772  * "Standard" case: incoming data is not downsampled.
1773  */
1774 static int
1775 JPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
1776 {
1777         JPEGState *sp = JState(tif);
1778         tmsize_t nrows;
1779         JSAMPROW bufptr[1];
1780         short *line16 = NULL;
1781         int    line16_count = 0;
1782
1783         (void) s;
1784         assert(sp != NULL);
1785         /* data is expected to be supplied in multiples of a scanline */
1786         nrows = cc / sp->bytesperline;
1787         if (cc % sp->bytesperline)
1788             TIFFWarningExt(tif->tif_clientdata, tif->tif_name, 
1789                            "fractional scanline discarded");
1790
1791         /* The last strip will be limited to image size */
1792         if( !isTiled(tif) && tif->tif_row+nrows > tif->tif_dir.td_imagelength )
1793             nrows = tif->tif_dir.td_imagelength - tif->tif_row;
1794
1795         if( sp->cinfo.c.data_precision == 12 )
1796         {
1797             line16_count = (int)((sp->bytesperline * 2) / 3);
1798             line16 = (short *) _TIFFmalloc(sizeof(short) * line16_count);
1799             // FIXME: undiagnosed malloc failure
1800         }
1801             
1802         while (nrows-- > 0) {
1803
1804             if( sp->cinfo.c.data_precision == 12 )
1805             {
1806
1807                 int value_pairs = line16_count / 2;
1808                 int iPair;
1809
1810                 bufptr[0] = (JSAMPROW) line16;
1811
1812                 for( iPair = 0; iPair < value_pairs; iPair++ )
1813                 {
1814                     unsigned char *in_ptr =
1815                         ((unsigned char *) buf) + iPair * 3;
1816                     JSAMPLE *out_ptr = (JSAMPLE *) (line16 + iPair * 2);
1817
1818                     out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4);
1819                     out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2];
1820                 }
1821             }
1822             else
1823             {
1824                 bufptr[0] = (JSAMPROW) buf;
1825             }
1826             if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
1827                 return (0);
1828             if (nrows > 0)
1829                 tif->tif_row++;
1830             buf += sp->bytesperline;
1831         }
1832
1833         if( sp->cinfo.c.data_precision == 12 )
1834         {
1835             _TIFFfree( line16 );
1836         }
1837             
1838         return (1);
1839 }
1840
1841 /*
1842  * Encode a chunk of pixels.
1843  * Incoming data is expected to be downsampled per sampling factors.
1844  */
1845 static int
1846 JPEGEncodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
1847 {
1848         JPEGState *sp = JState(tif);
1849         JSAMPLE* inptr;
1850         JSAMPLE* outptr;
1851         tmsize_t nrows;
1852         JDIMENSION clumps_per_line, nclump;
1853         int clumpoffset, ci, xpos, ypos;
1854         jpeg_component_info* compptr;
1855         int samples_per_clump = sp->samplesperclump;
1856         tmsize_t bytesperclumpline;
1857
1858         (void) s;
1859         assert(sp != NULL);
1860         /* data is expected to be supplied in multiples of a clumpline */
1861         /* a clumpline is equivalent to v_sampling desubsampled scanlines */
1862         /* TODO: the following calculation of bytesperclumpline, should substitute calculation of sp->bytesperline, except that it is per v_sampling lines */
1863         bytesperclumpline = (((sp->cinfo.c.image_width+sp->h_sampling-1)/sp->h_sampling)
1864                              *(sp->h_sampling*sp->v_sampling+2)*sp->cinfo.c.data_precision+7)
1865                             /8;
1866
1867         nrows = ( cc / bytesperclumpline ) * sp->v_sampling;
1868         if (cc % bytesperclumpline)
1869                 TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline discarded");
1870
1871         /* Cb,Cr both have sampling factors 1, so this is correct */
1872         clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width;
1873
1874         while (nrows > 0) {
1875                 /*
1876                  * Fastest way to separate the data is to make one pass
1877                  * over the scanline for each row of each component.
1878                  */
1879                 clumpoffset = 0;                /* first sample in clump */
1880                 for (ci = 0, compptr = sp->cinfo.c.comp_info;
1881                      ci < sp->cinfo.c.num_components;
1882                      ci++, compptr++) {
1883                     int hsamp = compptr->h_samp_factor;
1884                     int vsamp = compptr->v_samp_factor;
1885                     int padding = (int) (compptr->width_in_blocks * DCTSIZE -
1886                                          clumps_per_line * hsamp);
1887                     for (ypos = 0; ypos < vsamp; ypos++) {
1888                         inptr = ((JSAMPLE*) buf) + clumpoffset;
1889                         outptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos];
1890                         if (hsamp == 1) {
1891                             /* fast path for at least Cb and Cr */
1892                             for (nclump = clumps_per_line; nclump-- > 0; ) {
1893                                 *outptr++ = inptr[0];
1894                                 inptr += samples_per_clump;
1895                             }
1896                         } else {
1897                             /* general case */
1898                             for (nclump = clumps_per_line; nclump-- > 0; ) {
1899                                 for (xpos = 0; xpos < hsamp; xpos++)
1900                                     *outptr++ = inptr[xpos];
1901                                 inptr += samples_per_clump;
1902                             }
1903                         }
1904                         /* pad each scanline as needed */
1905                         for (xpos = 0; xpos < padding; xpos++) {
1906                             *outptr = outptr[-1];
1907                             outptr++;
1908                         }
1909                         clumpoffset += hsamp;
1910                     }
1911                 }
1912                 sp->scancount++;
1913                 if (sp->scancount >= DCTSIZE) {
1914                         int n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
1915                         if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
1916                                 return (0);
1917                         sp->scancount = 0;
1918                 }
1919                 tif->tif_row += sp->v_sampling;
1920                 buf += bytesperclumpline;
1921                 nrows -= sp->v_sampling;
1922         }
1923         return (1);
1924 }
1925
1926 /*
1927  * Finish up at the end of a strip or tile.
1928  */
1929 static int
1930 JPEGPostEncode(TIFF* tif)
1931 {
1932         JPEGState *sp = JState(tif);
1933
1934         if (sp->scancount > 0) {
1935                 /*
1936                  * Need to emit a partial bufferload of downsampled data.
1937                  * Pad the data vertically.
1938                  */
1939                 int ci, ypos, n;
1940                 jpeg_component_info* compptr;
1941
1942                 for (ci = 0, compptr = sp->cinfo.c.comp_info;
1943                      ci < sp->cinfo.c.num_components;
1944                      ci++, compptr++) {
1945                         int vsamp = compptr->v_samp_factor;
1946                         tmsize_t row_width = compptr->width_in_blocks * DCTSIZE
1947                                 * sizeof(JSAMPLE);
1948                         for (ypos = sp->scancount * vsamp;
1949                              ypos < DCTSIZE * vsamp; ypos++) {
1950                                 _TIFFmemcpy((void*)sp->ds_buffer[ci][ypos],
1951                                             (void*)sp->ds_buffer[ci][ypos-1],
1952                                             row_width);
1953
1954                         }
1955                 }
1956                 n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
1957                 if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
1958                         return (0);
1959         }
1960
1961         return (TIFFjpeg_finish_compress(JState(tif)));
1962 }
1963
1964 static void
1965 JPEGCleanup(TIFF* tif)
1966 {
1967         JPEGState *sp = JState(tif);
1968         
1969         assert(sp != 0);
1970
1971         tif->tif_tagmethods.vgetfield = sp->vgetparent;
1972         tif->tif_tagmethods.vsetfield = sp->vsetparent;
1973         tif->tif_tagmethods.printdir = sp->printdir;
1974
1975         if( sp != NULL ) {
1976                 if( sp->cinfo_initialized )
1977                     TIFFjpeg_destroy(sp);       /* release libjpeg resources */
1978                 if (sp->jpegtables)             /* tag value */
1979                         _TIFFfree(sp->jpegtables);
1980         }
1981         _TIFFfree(tif->tif_data);       /* release local state */
1982         tif->tif_data = NULL;
1983
1984         _TIFFSetDefaultCompressionState(tif);
1985 }
1986
1987 static void 
1988 JPEGResetUpsampled( TIFF* tif )
1989 {
1990         JPEGState* sp = JState(tif);
1991         TIFFDirectory* td = &tif->tif_dir;
1992
1993         /*
1994          * Mark whether returned data is up-sampled or not so TIFFStripSize
1995          * and TIFFTileSize return values that reflect the true amount of
1996          * data.
1997          */
1998         tif->tif_flags &= ~TIFF_UPSAMPLED;
1999         if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
2000                 if (td->td_photometric == PHOTOMETRIC_YCBCR &&
2001                     sp->jpegcolormode == JPEGCOLORMODE_RGB) {
2002                         tif->tif_flags |= TIFF_UPSAMPLED;
2003                 } else {
2004 #ifdef notdef
2005                         if (td->td_ycbcrsubsampling[0] != 1 ||
2006                             td->td_ycbcrsubsampling[1] != 1)
2007                                 ; /* XXX what about up-sampling? */
2008 #endif
2009                 }
2010         }
2011
2012         /*
2013          * Must recalculate cached tile size in case sampling state changed.
2014          * Should we really be doing this now if image size isn't set? 
2015          */
2016         if( tif->tif_tilesize > 0 )
2017             tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);   
2018         if( tif->tif_scanlinesize > 0 )
2019             tif->tif_scanlinesize = TIFFScanlineSize(tif); 
2020 }
2021
2022 static int
2023 JPEGVSetField(TIFF* tif, uint32 tag, va_list ap)
2024 {
2025         JPEGState* sp = JState(tif);
2026         const TIFFField* fip;
2027         uint32 v32;
2028
2029         assert(sp != NULL);
2030
2031         switch (tag) {
2032         case TIFFTAG_JPEGTABLES:
2033                 v32 = (uint32) va_arg(ap, uint32);
2034                 if (v32 == 0) {
2035                         /* XXX */
2036                         return (0);
2037                 }
2038                 _TIFFsetByteArray(&sp->jpegtables, va_arg(ap, void*),
2039                     (long) v32);
2040                 sp->jpegtables_length = v32;
2041                 TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2042                 break;
2043         case TIFFTAG_JPEGQUALITY:
2044                 sp->jpegquality = (int) va_arg(ap, int);
2045                 return (1);                     /* pseudo tag */
2046         case TIFFTAG_JPEGCOLORMODE:
2047                 sp->jpegcolormode = (int) va_arg(ap, int);
2048                 JPEGResetUpsampled( tif );
2049                 return (1);                     /* pseudo tag */
2050         case TIFFTAG_PHOTOMETRIC:
2051         {
2052                 int ret_value = (*sp->vsetparent)(tif, tag, ap);
2053                 JPEGResetUpsampled( tif );
2054                 return ret_value;
2055         }
2056         case TIFFTAG_JPEGTABLESMODE:
2057                 sp->jpegtablesmode = (int) va_arg(ap, int);
2058                 return (1);                     /* pseudo tag */
2059         case TIFFTAG_YCBCRSUBSAMPLING:
2060                 /* mark the fact that we have a real ycbcrsubsampling! */
2061                 sp->ycbcrsampling_fetched = 1;
2062                 /* should we be recomputing upsampling info here? */
2063                 return (*sp->vsetparent)(tif, tag, ap);
2064         default:
2065                 return (*sp->vsetparent)(tif, tag, ap);
2066         }
2067
2068         if ((fip = TIFFFieldWithTag(tif, tag))) {
2069                 TIFFSetFieldBit(tif, fip->field_bit);
2070         } else {
2071                 return (0);
2072         }
2073
2074         tif->tif_flags |= TIFF_DIRTYDIRECT;
2075         return (1);
2076 }
2077
2078 static int
2079 JPEGVGetField(TIFF* tif, uint32 tag, va_list ap)
2080 {
2081         JPEGState* sp = JState(tif);
2082
2083         assert(sp != NULL);
2084
2085         switch (tag) {
2086                 case TIFFTAG_JPEGTABLES:
2087                         *va_arg(ap, uint32*) = sp->jpegtables_length;
2088                         *va_arg(ap, void**) = sp->jpegtables;
2089                         break;
2090                 case TIFFTAG_JPEGQUALITY:
2091                         *va_arg(ap, int*) = sp->jpegquality;
2092                         break;
2093                 case TIFFTAG_JPEGCOLORMODE:
2094                         *va_arg(ap, int*) = sp->jpegcolormode;
2095                         break;
2096                 case TIFFTAG_JPEGTABLESMODE:
2097                         *va_arg(ap, int*) = sp->jpegtablesmode;
2098                         break;
2099                 default:
2100                         return (*sp->vgetparent)(tif, tag, ap);
2101         }
2102         return (1);
2103 }
2104
2105 static void
2106 JPEGPrintDir(TIFF* tif, FILE* fd, long flags)
2107 {
2108         JPEGState* sp = JState(tif);
2109
2110         assert(sp != NULL);
2111         (void) flags;
2112
2113         if( sp != NULL ) {
2114                 if (TIFFFieldSet(tif,FIELD_JPEGTABLES))
2115                         fprintf(fd, "  JPEG Tables: (%lu bytes)\n",
2116                                 (unsigned long) sp->jpegtables_length);
2117                 if (sp->printdir)
2118                         (*sp->printdir)(tif, fd, flags);
2119         }
2120 }
2121
2122 static uint32
2123 JPEGDefaultStripSize(TIFF* tif, uint32 s)
2124 {
2125         JPEGState* sp = JState(tif);
2126         TIFFDirectory *td = &tif->tif_dir;
2127
2128         s = (*sp->defsparent)(tif, s);
2129         if (s < td->td_imagelength)
2130                 s = TIFFroundup_32(s, td->td_ycbcrsubsampling[1] * DCTSIZE);
2131         return (s);
2132 }
2133
2134 static void
2135 JPEGDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
2136 {
2137         JPEGState* sp = JState(tif);
2138         TIFFDirectory *td = &tif->tif_dir;
2139
2140         (*sp->deftparent)(tif, tw, th);
2141         *tw = TIFFroundup_32(*tw, td->td_ycbcrsubsampling[0] * DCTSIZE);
2142         *th = TIFFroundup_32(*th, td->td_ycbcrsubsampling[1] * DCTSIZE);
2143 }
2144
2145 /*
2146  * The JPEG library initialized used to be done in TIFFInitJPEG(), but
2147  * now that we allow a TIFF file to be opened in update mode it is necessary
2148  * to have some way of deciding whether compression or decompression is
2149  * desired other than looking at tif->tif_mode.  We accomplish this by 
2150  * examining {TILE/STRIP}BYTECOUNTS to see if there is a non-zero entry.
2151  * If so, we assume decompression is desired. 
2152  *
2153  * This is tricky, because TIFFInitJPEG() is called while the directory is
2154  * being read, and generally speaking the BYTECOUNTS tag won't have been read
2155  * at that point.  So we try to defer jpeg library initialization till we
2156  * do have that tag ... basically any access that might require the compressor
2157  * or decompressor that occurs after the reading of the directory. 
2158  *
2159  * In an ideal world compressors or decompressors would be setup
2160  * at the point where a single tile or strip was accessed (for read or write)
2161  * so that stuff like update of missing tiles, or replacement of tiles could
2162  * be done. However, we aren't trying to crack that nut just yet ...
2163  *
2164  * NFW, Feb 3rd, 2003.
2165  */
2166
2167 static int JPEGInitializeLibJPEG( TIFF * tif, int decompress )
2168 {
2169     JPEGState* sp = JState(tif);
2170
2171     if(sp->cinfo_initialized)
2172     {
2173         if( !decompress && sp->cinfo.comm.is_decompressor )
2174             TIFFjpeg_destroy( sp );
2175         else if( decompress && !sp->cinfo.comm.is_decompressor )
2176             TIFFjpeg_destroy( sp );
2177         else
2178             return 1;
2179
2180         sp->cinfo_initialized = 0;
2181     }
2182
2183     /*
2184      * Initialize libjpeg.
2185      */
2186     if ( decompress ) {
2187         if (!TIFFjpeg_create_decompress(sp))
2188             return (0);
2189     } else {
2190         if (!TIFFjpeg_create_compress(sp))
2191             return (0);
2192     }
2193
2194     sp->cinfo_initialized = TRUE;
2195
2196     return 1;
2197 }
2198
2199 int
2200 TIFFInitJPEG(TIFF* tif, int scheme)
2201 {
2202         JPEGState* sp;
2203
2204         assert(scheme == COMPRESSION_JPEG);
2205
2206         /*
2207          * Merge codec-specific tag information.
2208          */
2209         if (!_TIFFMergeFields(tif, jpegFields, TIFFArrayCount(jpegFields))) {
2210                 TIFFErrorExt(tif->tif_clientdata,
2211                              "TIFFInitJPEG",
2212                              "Merging JPEG codec-specific tags failed");
2213                 return 0;
2214         }
2215
2216         /*
2217          * Allocate state block so tag methods have storage to record values.
2218          */
2219         tif->tif_data = (uint8*) _TIFFmalloc(sizeof (JPEGState));
2220
2221         if (tif->tif_data == NULL) {
2222                 TIFFErrorExt(tif->tif_clientdata,
2223                              "TIFFInitJPEG", "No space for JPEG state block");
2224                 return 0;
2225         }
2226         _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState));
2227
2228         sp = JState(tif);
2229         sp->tif = tif;                          /* back link */
2230
2231         /*
2232          * Override parent get/set field methods.
2233          */
2234         sp->vgetparent = tif->tif_tagmethods.vgetfield;
2235         tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
2236         sp->vsetparent = tif->tif_tagmethods.vsetfield;
2237         tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
2238         sp->printdir = tif->tif_tagmethods.printdir;
2239         tif->tif_tagmethods.printdir = JPEGPrintDir;   /* hook for codec tags */
2240
2241         /* Default values for codec-specific fields */
2242         sp->jpegtables = NULL;
2243         sp->jpegtables_length = 0;
2244         sp->jpegquality = 75;                   /* Default IJG quality */
2245         sp->jpegcolormode = JPEGCOLORMODE_RAW;
2246         sp->jpegtablesmode = JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF;
2247         sp->ycbcrsampling_fetched = 0;
2248
2249         /*
2250          * Install codec methods.
2251          */
2252         tif->tif_fixuptags = JPEGFixupTags;
2253         tif->tif_setupdecode = JPEGSetupDecode;
2254         tif->tif_predecode = JPEGPreDecode;
2255         tif->tif_decoderow = JPEGDecode;
2256         tif->tif_decodestrip = JPEGDecode;
2257         tif->tif_decodetile = JPEGDecode;
2258         tif->tif_setupencode = JPEGSetupEncode;
2259         tif->tif_preencode = JPEGPreEncode;
2260         tif->tif_postencode = JPEGPostEncode;
2261         tif->tif_encoderow = JPEGEncode;
2262         tif->tif_encodestrip = JPEGEncode;
2263         tif->tif_encodetile = JPEGEncode;  
2264         tif->tif_cleanup = JPEGCleanup;
2265         sp->defsparent = tif->tif_defstripsize;
2266         tif->tif_defstripsize = JPEGDefaultStripSize;
2267         sp->deftparent = tif->tif_deftilesize;
2268         tif->tif_deftilesize = JPEGDefaultTileSize;
2269         tif->tif_flags |= TIFF_NOBITREV;        /* no bit reversal, please */
2270
2271         sp->cinfo_initialized = FALSE;
2272
2273         /*
2274         ** Create a JPEGTables field if no directory has yet been created. 
2275         ** We do this just to ensure that sufficient space is reserved for
2276         ** the JPEGTables field.  It will be properly created the right
2277         ** size later. 
2278         */
2279         if( tif->tif_diroff == 0 )
2280         {
2281 #define SIZE_OF_JPEGTABLES 2000
2282 /*
2283 The following line assumes incorrectly that all JPEG-in-TIFF files will have
2284 a JPEGTABLES tag generated and causes null-filled JPEGTABLES tags to be written
2285 when the JPEG data is placed with TIFFWriteRawStrip.  The field bit should be 
2286 set, anyway, later when actual JPEGTABLES header is generated, so removing it 
2287 here hopefully is harmless.
2288             TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2289 */
2290             sp->jpegtables_length = SIZE_OF_JPEGTABLES;
2291             sp->jpegtables = (void *) _TIFFmalloc(sp->jpegtables_length);
2292             // FIXME: NULL-deref after malloc failure
2293             _TIFFmemset(sp->jpegtables, 0, SIZE_OF_JPEGTABLES);
2294 #undef SIZE_OF_JPEGTABLES
2295         }
2296
2297         return 1;
2298 }
2299 #endif /* JPEG_SUPPORT */
2300
2301 /* vim: set ts=8 sts=8 sw=8 noet: */
2302
2303 /*
2304  * Local Variables:
2305  * mode: c
2306  * c-basic-offset: 8
2307  * fill-column: 78
2308  * End:
2309  */
2310 #endif
2311