Coverage Report

Created: 2025-08-11 09:23

/src/gdal/frmts/gtiff/libtiff/tif_jpeg.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1994-1997 Sam Leffler
3
 * Copyright (c) 1994-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22
 * OF THIS SOFTWARE.
23
 */
24
25
#define WIN32_LEAN_AND_MEAN
26
#define VC_EXTRALEAN
27
28
#include "tiffiop.h"
29
#include <stdlib.h>
30
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
/* Settings that are independent of libjpeg ABI. Used when reinitializing the */
48
/* JPEGState from libjpegs 8 bit to libjpeg 12 bits, which have potentially */
49
/* different ABI */
50
typedef struct
51
{
52
    TIFFVGetMethod vgetparent;  /* super-class method */
53
    TIFFVSetMethod vsetparent;  /* super-class method */
54
    TIFFPrintMethod printdir;   /* super-class method */
55
    TIFFStripMethod defsparent; /* super-class method */
56
    TIFFTileMethod deftparent;  /* super-class method */
57
58
    /* pseudo-tag fields */
59
    void *jpegtables;           /* JPEGTables tag value, or NULL */
60
    uint32_t jpegtables_length; /* number of bytes in same */
61
    int jpegquality;            /* Compression quality level */
62
    int jpegcolormode;          /* Auto RGB<=>YCbCr convert? */
63
    int jpegtablesmode;         /* What to put in JPEGTables */
64
65
    int ycbcrsampling_fetched;
66
    int max_allowed_scan_number;
67
    int has_warned_about_progressive_mode;
68
} JPEGOtherSettings;
69
70
int TIFFFillStrip(TIFF *tif, uint32_t strip);
71
int TIFFFillTile(TIFF *tif, uint32_t tile);
72
int TIFFReInitJPEG_12(TIFF *tif, const JPEGOtherSettings *otherSettings,
73
                      int scheme, int is_encode);
74
int TIFFJPEGIsFullStripRequired_12(TIFF *tif);
75
76
#include "jerror.h"
77
#include "jpeglib.h"
78
79
/* Do optional compile-time version check */
80
#if defined(EXPECTED_JPEG_LIB_VERSION) && !defined(LIBJPEG_12_PATH)
81
#if EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION
82
#error EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION
83
#endif
84
#endif
85
86
/*
87
 * Do we want to do special processing suitable for when JSAMPLE is a
88
 * 16bit value?
89
 */
90
91
/* HAVE_JPEGTURBO_DUAL_MODE_8_12 is defined for libjpeg-turbo >= 3.0 which
92
 * adds a dual-mode 8/12 bit API in the same library.
93
 */
94
95
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12)
96
#define JPEG_DUAL_MODE_8_12
97
/* Start by undefining BITS_IN_JSAMPLE which is always set to 8 in libjpeg-turbo
98
 * >= 3.0 Cf
99
 * https://github.com/libjpeg-turbo/libjpeg-turbo/commit/8b9bc4b9635a2a047fb23ebe70c9acd728d3f99b
100
 */
101
#undef BITS_IN_JSAMPLE
102
/* libjpeg-turbo >= 3.0 adds J12xxxx datatypes for the 12-bit mode. */
103
#if defined(FROM_TIF_JPEG_12)
104
#define BITS_IN_JSAMPLE 12
105
#define TIFF_JSAMPLE J12SAMPLE
106
#define TIFF_JSAMPARRAY J12SAMPARRAY
107
#define TIFF_JSAMPIMAGE J12SAMPIMAGE
108
#define TIFF_JSAMPROW J12SAMPROW
109
#else
110
#define BITS_IN_JSAMPLE 8
111
#define TIFF_JSAMPLE JSAMPLE
112
#define TIFF_JSAMPARRAY JSAMPARRAY
113
#define TIFF_JSAMPIMAGE JSAMPIMAGE
114
#define TIFF_JSAMPROW JSAMPROW
115
#endif
116
#else
117
0
#define TIFF_JSAMPLE JSAMPLE
118
0
#define TIFF_JSAMPARRAY JSAMPARRAY
119
#define TIFF_JSAMPIMAGE JSAMPIMAGE
120
211k
#define TIFF_JSAMPROW JSAMPROW
121
#endif
122
123
#if defined(JPEG_LIB_MK1)
124
#define JPEG_LIB_MK1_OR_12BIT 1
125
#elif BITS_IN_JSAMPLE == 12
126
#define JPEG_LIB_MK1_OR_12BIT 1
127
#endif
128
129
/*
130
 * We are using width_in_blocks which is supposed to be private to
131
 * libjpeg. Unfortunately, the libjpeg delivered with Cygwin has
132
 * renamed this member to width_in_data_units.  Since the header has
133
 * also renamed a define, use that unique define name in order to
134
 * detect the problem header and adjust to suit.
135
 */
136
#if defined(D_MAX_DATA_UNITS_IN_MCU)
137
#define width_in_blocks width_in_data_units
138
#endif
139
140
/*
141
 * On some machines it may be worthwhile to use _setjmp or sigsetjmp
142
 * in place of plain setjmp.  These macros will make it easier.
143
 */
144
15.4M
#define SETJMP(jbuf) setjmp(jbuf)
145
112k
#define LONGJMP(jbuf, code) longjmp(jbuf, code)
146
#define JMP_BUF jmp_buf
147
148
#ifndef TIFF_jpeg_destination_mgr_defined
149
#define TIFF_jpeg_destination_mgr_defined
150
typedef struct jpeg_destination_mgr jpeg_destination_mgr;
151
#endif
152
153
#ifndef TIFF_jpeg_source_mgr_defined
154
#define TIFF_jpeg_source_mgr_defined
155
typedef struct jpeg_source_mgr jpeg_source_mgr;
156
#endif
157
158
#ifndef TIFF_jpeg_error_mgr_defined
159
#define TIFF_jpeg_error_mgr_defined
160
typedef struct jpeg_error_mgr jpeg_error_mgr;
161
#endif
162
163
/*
164
 * State block for each open TIFF file using
165
 * libjpeg to do JPEG compression/decompression.
166
 *
167
 * libjpeg's visible state is either a jpeg_compress_struct
168
 * or jpeg_decompress_struct depending on which way we
169
 * are going.  comm can be used to refer to the fields
170
 * which are common to both.
171
 *
172
 * NB: cinfo is required to be the first member of JPEGState,
173
 *     so we can safely cast JPEGState* -> jpeg_xxx_struct*
174
 *     and vice versa!
175
 */
176
typedef struct
177
{
178
    union
179
    {
180
        struct jpeg_compress_struct c;
181
        struct jpeg_decompress_struct d;
182
        struct jpeg_common_struct comm;
183
    } cinfo; /* NB: must be first */
184
    int cinfo_initialized;
185
186
    jpeg_error_mgr err;  /* libjpeg error manager */
187
    JMP_BUF exit_jmpbuf; /* for catching libjpeg failures */
188
189
    struct jpeg_progress_mgr progress;
190
    /*
191
     * The following two members could be a union, but
192
     * they're small enough that it's not worth the effort.
193
     */
194
    jpeg_destination_mgr dest; /* data dest for compression */
195
    jpeg_source_mgr src;       /* data source for decompression */
196
                               /* private state */
197
    TIFF *tif;                 /* back link needed by some code */
198
    uint16_t photometric;      /* copy of PhotometricInterpretation */
199
    uint16_t h_sampling;       /* luminance sampling factors */
200
    uint16_t v_sampling;
201
    tmsize_t bytesperline; /* decompressed bytes per scanline */
202
    /* pointers to intermediate buffers when processing downsampled data */
203
    TIFF_JSAMPARRAY ds_buffer[MAX_COMPONENTS];
204
    int scancount; /* number of "scanlines" accumulated */
205
    int samplesperclump;
206
207
    JPEGOtherSettings otherSettings;
208
} JPEGState;
209
210
21.4M
#define JState(tif) ((JPEGState *)(tif)->tif_data)
211
212
static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
213
static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
214
static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
215
static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
216
static int JPEGInitializeLibJPEG(TIFF *tif, int decode);
217
static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
218
219
#define FIELD_JPEGTABLES (FIELD_CODEC + 0)
220
221
static const TIFFField jpegFields[] = {
222
    {TIFFTAG_JPEGTABLES, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8,
223
     FIELD_JPEGTABLES, FALSE, TRUE, "JPEGTables", NULL},
224
    {TIFFTAG_JPEGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
225
     TRUE, FALSE, "", NULL},
226
    {TIFFTAG_JPEGCOLORMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
227
     FALSE, FALSE, "", NULL},
228
    {TIFFTAG_JPEGTABLESMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
229
     FALSE, FALSE, "", NULL}};
230
231
/*
232
 * libjpeg interface layer.
233
 *
234
 * We use setjmp/longjmp to return control to libtiff
235
 * when a fatal error is encountered within the JPEG
236
 * library.  We also direct libjpeg error and warning
237
 * messages through the appropriate libtiff handlers.
238
 */
239
240
/*
241
 * Error handling routines (these replace corresponding
242
 * IJG routines from jerror.c).  These are used for both
243
 * compression and decompression.
244
 */
245
static void TIFFjpeg_error_exit(j_common_ptr cinfo)
246
112k
{
247
112k
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
248
112k
    char buffer[JMSG_LENGTH_MAX];
249
250
112k
    (*cinfo->err->format_message)(cinfo, buffer);
251
112k
    TIFFErrorExtR(sp->tif, "JPEGLib", "%s",
252
112k
                  buffer);       /* display the error message */
253
112k
    jpeg_abort(cinfo);           /* clean up libjpeg state */
254
112k
    LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
255
112k
}
tif_jpeg.c:TIFFjpeg_error_exit
Line
Count
Source
246
103k
{
247
103k
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
248
103k
    char buffer[JMSG_LENGTH_MAX];
249
250
103k
    (*cinfo->err->format_message)(cinfo, buffer);
251
103k
    TIFFErrorExtR(sp->tif, "JPEGLib", "%s",
252
103k
                  buffer);       /* display the error message */
253
103k
    jpeg_abort(cinfo);           /* clean up libjpeg state */
254
103k
    LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
255
103k
}
tif_jpeg_12.c:TIFFjpeg_error_exit
Line
Count
Source
246
9.37k
{
247
9.37k
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
248
9.37k
    char buffer[JMSG_LENGTH_MAX];
249
250
9.37k
    (*cinfo->err->format_message)(cinfo, buffer);
251
9.37k
    TIFFErrorExtR(sp->tif, "JPEGLib", "%s",
252
9.37k
                  buffer);       /* display the error message */
253
9.37k
    jpeg_abort(cinfo);           /* clean up libjpeg state */
254
9.37k
    LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
255
9.37k
}
256
257
/*
258
 * This routine is invoked only for warning messages,
259
 * since error_exit does its own thing and trace_level
260
 * is never set > 0.
261
 */
262
static void TIFFjpeg_output_message(j_common_ptr cinfo)
263
34.7k
{
264
34.7k
    char buffer[JMSG_LENGTH_MAX];
265
266
34.7k
    (*cinfo->err->format_message)(cinfo, buffer);
267
34.7k
    TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer);
268
34.7k
}
tif_jpeg.c:TIFFjpeg_output_message
Line
Count
Source
263
24.8k
{
264
24.8k
    char buffer[JMSG_LENGTH_MAX];
265
266
24.8k
    (*cinfo->err->format_message)(cinfo, buffer);
267
24.8k
    TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer);
268
24.8k
}
tif_jpeg_12.c:TIFFjpeg_output_message
Line
Count
Source
263
9.84k
{
264
9.84k
    char buffer[JMSG_LENGTH_MAX];
265
266
9.84k
    (*cinfo->err->format_message)(cinfo, buffer);
267
9.84k
    TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer);
268
9.84k
}
269
270
/* Avoid the risk of denial-of-service on crafted JPEGs with an insane */
271
/* number of scans. */
272
/* See
273
 * http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf
274
 */
275
static void TIFFjpeg_progress_monitor(j_common_ptr cinfo)
276
26.5M
{
277
26.5M
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
278
26.5M
    if (cinfo->is_decompressor)
279
26.5M
    {
280
26.5M
        const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number;
281
26.5M
        if (scan_no >= sp->otherSettings.max_allowed_scan_number)
282
21
        {
283
21
            TIFFErrorExtR(
284
21
                ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor",
285
21
                "Scan number %d exceeds maximum scans (%d). This limit "
286
21
                "can be raised through the "
287
21
                "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER "
288
21
                "environment variable.",
289
21
                scan_no, sp->otherSettings.max_allowed_scan_number);
290
291
21
            jpeg_abort(cinfo);           /* clean up libjpeg state */
292
21
            LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
293
21
        }
294
26.5M
    }
295
26.5M
}
tif_jpeg.c:TIFFjpeg_progress_monitor
Line
Count
Source
276
82.2k
{
277
82.2k
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
278
82.2k
    if (cinfo->is_decompressor)
279
82.2k
    {
280
82.2k
        const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number;
281
82.2k
        if (scan_no >= sp->otherSettings.max_allowed_scan_number)
282
12
        {
283
12
            TIFFErrorExtR(
284
12
                ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor",
285
12
                "Scan number %d exceeds maximum scans (%d). This limit "
286
12
                "can be raised through the "
287
12
                "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER "
288
12
                "environment variable.",
289
12
                scan_no, sp->otherSettings.max_allowed_scan_number);
290
291
12
            jpeg_abort(cinfo);           /* clean up libjpeg state */
292
12
            LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
293
12
        }
294
82.2k
    }
295
82.2k
}
tif_jpeg_12.c:TIFFjpeg_progress_monitor
Line
Count
Source
276
26.5M
{
277
26.5M
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
278
26.5M
    if (cinfo->is_decompressor)
279
26.5M
    {
280
26.5M
        const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number;
281
26.5M
        if (scan_no >= sp->otherSettings.max_allowed_scan_number)
282
9
        {
283
9
            TIFFErrorExtR(
284
9
                ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor",
285
9
                "Scan number %d exceeds maximum scans (%d). This limit "
286
9
                "can be raised through the "
287
9
                "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER "
288
9
                "environment variable.",
289
9
                scan_no, sp->otherSettings.max_allowed_scan_number);
290
291
9
            jpeg_abort(cinfo);           /* clean up libjpeg state */
292
9
            LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
293
9
        }
294
26.5M
    }
295
26.5M
}
296
297
/*
298
 * Interface routines.  This layer of routines exists
299
 * primarily to limit side-effects from using setjmp.
300
 * Also, normal/error returns are converted into return
301
 * values per libtiff practice.
302
 */
303
15.4M
#define CALLJPEG(sp, fail, op) (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op))
304
4.28M
#define CALLVJPEG(sp, op) CALLJPEG(sp, 0, ((op), 1))
305
306
static int TIFFjpeg_create_compress(JPEGState *sp)
307
632k
{
308
    /* initialize JPEG error handling */
309
632k
    sp->cinfo.c.err = jpeg_std_error(&sp->err);
310
632k
    sp->err.error_exit = TIFFjpeg_error_exit;
311
632k
    sp->err.output_message = TIFFjpeg_output_message;
312
313
    /* set client_data to avoid UMR warning from tools like Purify */
314
632k
    sp->cinfo.c.client_data = NULL;
315
316
632k
    return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c));
317
632k
}
tif_jpeg.c:TIFFjpeg_create_compress
Line
Count
Source
307
415k
{
308
    /* initialize JPEG error handling */
309
415k
    sp->cinfo.c.err = jpeg_std_error(&sp->err);
310
415k
    sp->err.error_exit = TIFFjpeg_error_exit;
311
415k
    sp->err.output_message = TIFFjpeg_output_message;
312
313
    /* set client_data to avoid UMR warning from tools like Purify */
314
415k
    sp->cinfo.c.client_data = NULL;
315
316
415k
    return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c));
317
415k
}
tif_jpeg_12.c:TIFFjpeg_create_compress
Line
Count
Source
307
217k
{
308
    /* initialize JPEG error handling */
309
217k
    sp->cinfo.c.err = jpeg_std_error(&sp->err);
310
217k
    sp->err.error_exit = TIFFjpeg_error_exit;
311
217k
    sp->err.output_message = TIFFjpeg_output_message;
312
313
    /* set client_data to avoid UMR warning from tools like Purify */
314
217k
    sp->cinfo.c.client_data = NULL;
315
316
217k
    return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c));
317
217k
}
318
319
static int TIFFjpeg_create_decompress(JPEGState *sp)
320
5.36k
{
321
    /* initialize JPEG error handling */
322
5.36k
    sp->cinfo.d.err = jpeg_std_error(&sp->err);
323
5.36k
    sp->err.error_exit = TIFFjpeg_error_exit;
324
5.36k
    sp->err.output_message = TIFFjpeg_output_message;
325
326
    /* set client_data to avoid UMR warning from tools like Purify */
327
5.36k
    sp->cinfo.d.client_data = NULL;
328
329
5.36k
    return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d));
330
5.36k
}
tif_jpeg.c:TIFFjpeg_create_decompress
Line
Count
Source
320
1.32k
{
321
    /* initialize JPEG error handling */
322
1.32k
    sp->cinfo.d.err = jpeg_std_error(&sp->err);
323
1.32k
    sp->err.error_exit = TIFFjpeg_error_exit;
324
1.32k
    sp->err.output_message = TIFFjpeg_output_message;
325
326
    /* set client_data to avoid UMR warning from tools like Purify */
327
1.32k
    sp->cinfo.d.client_data = NULL;
328
329
1.32k
    return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d));
330
1.32k
}
tif_jpeg_12.c:TIFFjpeg_create_decompress
Line
Count
Source
320
4.03k
{
321
    /* initialize JPEG error handling */
322
4.03k
    sp->cinfo.d.err = jpeg_std_error(&sp->err);
323
4.03k
    sp->err.error_exit = TIFFjpeg_error_exit;
324
4.03k
    sp->err.output_message = TIFFjpeg_output_message;
325
326
    /* set client_data to avoid UMR warning from tools like Purify */
327
4.03k
    sp->cinfo.d.client_data = NULL;
328
329
4.03k
    return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d));
330
4.03k
}
331
332
static int TIFFjpeg_set_defaults(JPEGState *sp)
333
632k
{
334
632k
    return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c));
335
632k
}
tif_jpeg.c:TIFFjpeg_set_defaults
Line
Count
Source
333
415k
{
334
415k
    return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c));
335
415k
}
tif_jpeg_12.c:TIFFjpeg_set_defaults
Line
Count
Source
333
217k
{
334
217k
    return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c));
335
217k
}
336
337
static int TIFFjpeg_set_colorspace(JPEGState *sp, J_COLOR_SPACE colorspace)
338
428k
{
339
428k
    return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace));
340
428k
}
tif_jpeg.c:TIFFjpeg_set_colorspace
Line
Count
Source
338
211k
{
339
211k
    return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace));
340
211k
}
tif_jpeg_12.c:TIFFjpeg_set_colorspace
Line
Count
Source
338
217k
{
339
217k
    return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace));
340
217k
}
341
342
static int TIFFjpeg_set_quality(JPEGState *sp, int quality,
343
                                boolean force_baseline)
344
642k
{
345
642k
    return CALLVJPEG(sp,
346
642k
                     jpeg_set_quality(&sp->cinfo.c, quality, force_baseline));
347
642k
}
tif_jpeg.c:TIFFjpeg_set_quality
Line
Count
Source
344
317k
{
345
317k
    return CALLVJPEG(sp,
346
317k
                     jpeg_set_quality(&sp->cinfo.c, quality, force_baseline));
347
317k
}
tif_jpeg_12.c:TIFFjpeg_set_quality
Line
Count
Source
344
325k
{
345
325k
    return CALLVJPEG(sp,
346
325k
                     jpeg_set_quality(&sp->cinfo.c, quality, force_baseline));
347
325k
}
348
349
static int TIFFjpeg_suppress_tables(JPEGState *sp, boolean suppress)
350
214k
{
351
214k
    return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress));
352
214k
}
tif_jpeg.c:TIFFjpeg_suppress_tables
Line
Count
Source
350
105k
{
351
105k
    return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress));
352
105k
}
tif_jpeg_12.c:TIFFjpeg_suppress_tables
Line
Count
Source
350
108k
{
351
108k
    return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress));
352
108k
}
353
354
static int TIFFjpeg_start_compress(JPEGState *sp, boolean write_all_tables)
355
428k
{
356
428k
    return CALLVJPEG(sp, jpeg_start_compress(&sp->cinfo.c, write_all_tables));
357
428k
}
tif_jpeg.c:TIFFjpeg_start_compress
Line
Count
Source
355
211k
{
356
211k
    return CALLVJPEG(sp, jpeg_start_compress(&sp->cinfo.c, write_all_tables));
357
211k
}
tif_jpeg_12.c:TIFFjpeg_start_compress
Line
Count
Source
355
217k
{
356
217k
    return CALLVJPEG(sp, jpeg_start_compress(&sp->cinfo.c, write_all_tables));
357
217k
}
358
359
static int TIFFjpeg_write_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines,
360
                                    int num_lines)
361
5.46M
{
362
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
363
    return CALLJPEG(sp, -1,
364
                    (int)jpeg12_write_scanlines(&sp->cinfo.c, scanlines,
365
                                                (JDIMENSION)num_lines));
366
#else
367
5.46M
    return CALLJPEG(sp, -1,
368
5.46M
                    (int)jpeg_write_scanlines(&sp->cinfo.c, scanlines,
369
5.46M
                                              (JDIMENSION)num_lines));
370
5.46M
#endif
371
5.46M
}
tif_jpeg.c:TIFFjpeg_write_scanlines
Line
Count
Source
361
1.98M
{
362
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
363
    return CALLJPEG(sp, -1,
364
                    (int)jpeg12_write_scanlines(&sp->cinfo.c, scanlines,
365
                                                (JDIMENSION)num_lines));
366
#else
367
1.98M
    return CALLJPEG(sp, -1,
368
1.98M
                    (int)jpeg_write_scanlines(&sp->cinfo.c, scanlines,
369
1.98M
                                              (JDIMENSION)num_lines));
370
1.98M
#endif
371
1.98M
}
tif_jpeg_12.c:TIFFjpeg_write_scanlines
Line
Count
Source
361
3.47M
{
362
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
363
    return CALLJPEG(sp, -1,
364
                    (int)jpeg12_write_scanlines(&sp->cinfo.c, scanlines,
365
                                                (JDIMENSION)num_lines));
366
#else
367
3.47M
    return CALLJPEG(sp, -1,
368
3.47M
                    (int)jpeg_write_scanlines(&sp->cinfo.c, scanlines,
369
3.47M
                                              (JDIMENSION)num_lines));
370
3.47M
#endif
371
3.47M
}
372
373
static int TIFFjpeg_write_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data,
374
                                   int num_lines)
375
0
{
376
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
377
    return CALLJPEG(
378
        sp, -1,
379
        (int)jpeg12_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines));
380
#else
381
0
    return CALLJPEG(
382
0
        sp, -1,
383
0
        (int)jpeg_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines));
384
0
#endif
385
0
}
Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_write_raw_data
Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_write_raw_data
386
387
static int TIFFjpeg_finish_compress(JPEGState *sp)
388
428k
{
389
428k
    return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c));
390
428k
}
tif_jpeg.c:TIFFjpeg_finish_compress
Line
Count
Source
388
211k
{
389
211k
    return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c));
390
211k
}
tif_jpeg_12.c:TIFFjpeg_finish_compress
Line
Count
Source
388
217k
{
389
217k
    return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c));
390
217k
}
391
392
static int TIFFjpeg_write_tables(JPEGState *sp)
393
214k
{
394
214k
    return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c));
395
214k
}
tif_jpeg.c:TIFFjpeg_write_tables
Line
Count
Source
393
105k
{
394
105k
    return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c));
395
105k
}
tif_jpeg_12.c:TIFFjpeg_write_tables
Line
Count
Source
393
108k
{
394
108k
    return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c));
395
108k
}
396
397
static int TIFFjpeg_read_header(JPEGState *sp, boolean require_image)
398
15.4k
{
399
15.4k
    return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image));
400
15.4k
}
tif_jpeg.c:TIFFjpeg_read_header
Line
Count
Source
398
4.21k
{
399
4.21k
    return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image));
400
4.21k
}
tif_jpeg_12.c:TIFFjpeg_read_header
Line
Count
Source
398
11.2k
{
399
11.2k
    return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image));
400
11.2k
}
401
402
static int TIFFjpeg_has_multiple_scans(JPEGState *sp)
403
2.92k
{
404
2.92k
    return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d));
405
2.92k
}
tif_jpeg.c:TIFFjpeg_has_multiple_scans
Line
Count
Source
403
415
{
404
415
    return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d));
405
415
}
tif_jpeg_12.c:TIFFjpeg_has_multiple_scans
Line
Count
Source
403
2.51k
{
404
2.51k
    return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d));
405
2.51k
}
406
407
static int TIFFjpeg_start_decompress(JPEGState *sp)
408
2.85k
{
409
2.85k
    const char *sz_max_allowed_scan_number;
410
    /* progress monitor */
411
2.85k
    sp->cinfo.d.progress = &sp->progress;
412
2.85k
    sp->progress.progress_monitor = TIFFjpeg_progress_monitor;
413
2.85k
    sp->otherSettings.max_allowed_scan_number = 100;
414
2.85k
    sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER");
415
2.85k
    if (sz_max_allowed_scan_number)
416
0
        sp->otherSettings.max_allowed_scan_number =
417
0
            atoi(sz_max_allowed_scan_number);
418
419
2.85k
    return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d));
420
2.85k
}
tif_jpeg.c:TIFFjpeg_start_decompress
Line
Count
Source
408
386
{
409
386
    const char *sz_max_allowed_scan_number;
410
    /* progress monitor */
411
386
    sp->cinfo.d.progress = &sp->progress;
412
386
    sp->progress.progress_monitor = TIFFjpeg_progress_monitor;
413
386
    sp->otherSettings.max_allowed_scan_number = 100;
414
386
    sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER");
415
386
    if (sz_max_allowed_scan_number)
416
0
        sp->otherSettings.max_allowed_scan_number =
417
0
            atoi(sz_max_allowed_scan_number);
418
419
386
    return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d));
420
386
}
tif_jpeg_12.c:TIFFjpeg_start_decompress
Line
Count
Source
408
2.47k
{
409
2.47k
    const char *sz_max_allowed_scan_number;
410
    /* progress monitor */
411
2.47k
    sp->cinfo.d.progress = &sp->progress;
412
2.47k
    sp->progress.progress_monitor = TIFFjpeg_progress_monitor;
413
2.47k
    sp->otherSettings.max_allowed_scan_number = 100;
414
2.47k
    sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER");
415
2.47k
    if (sz_max_allowed_scan_number)
416
0
        sp->otherSettings.max_allowed_scan_number =
417
0
            atoi(sz_max_allowed_scan_number);
418
419
2.47k
    return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d));
420
2.47k
}
421
422
static int TIFFjpeg_read_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines,
423
                                   int max_lines)
424
5.70M
{
425
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
426
    return CALLJPEG(sp, -1,
427
                    (int)jpeg12_read_scanlines(&sp->cinfo.d, scanlines,
428
                                               (JDIMENSION)max_lines));
429
#else
430
5.70M
    return CALLJPEG(sp, -1,
431
5.70M
                    (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines,
432
5.70M
                                             (JDIMENSION)max_lines));
433
5.70M
#endif
434
5.70M
}
tif_jpeg.c:TIFFjpeg_read_scanlines
Line
Count
Source
424
17.8k
{
425
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
426
    return CALLJPEG(sp, -1,
427
                    (int)jpeg12_read_scanlines(&sp->cinfo.d, scanlines,
428
                                               (JDIMENSION)max_lines));
429
#else
430
17.8k
    return CALLJPEG(sp, -1,
431
17.8k
                    (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines,
432
17.8k
                                             (JDIMENSION)max_lines));
433
17.8k
#endif
434
17.8k
}
tif_jpeg_12.c:TIFFjpeg_read_scanlines
Line
Count
Source
424
5.68M
{
425
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
426
    return CALLJPEG(sp, -1,
427
                    (int)jpeg12_read_scanlines(&sp->cinfo.d, scanlines,
428
                                               (JDIMENSION)max_lines));
429
#else
430
5.68M
    return CALLJPEG(sp, -1,
431
5.68M
                    (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines,
432
5.68M
                                             (JDIMENSION)max_lines));
433
5.68M
#endif
434
5.68M
}
435
436
static int TIFFjpeg_read_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data,
437
                                  int max_lines)
438
0
{
439
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
440
    return CALLJPEG(
441
        sp, -1,
442
        (int)jpeg12_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines));
443
#else
444
0
    return CALLJPEG(
445
0
        sp, -1,
446
0
        (int)jpeg_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines));
447
0
#endif
448
0
}
Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_read_raw_data
Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_read_raw_data
449
450
static int TIFFjpeg_finish_decompress(JPEGState *sp)
451
1.52k
{
452
1.52k
    return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d));
453
1.52k
}
tif_jpeg.c:TIFFjpeg_finish_decompress
Line
Count
Source
451
284
{
452
284
    return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d));
453
284
}
tif_jpeg_12.c:TIFFjpeg_finish_decompress
Line
Count
Source
451
1.23k
{
452
1.23k
    return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d));
453
1.23k
}
454
455
static int TIFFjpeg_abort(JPEGState *sp)
456
14.6k
{
457
14.6k
    return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm));
458
14.6k
}
tif_jpeg.c:TIFFjpeg_abort
Line
Count
Source
456
3.72k
{
457
3.72k
    return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm));
458
3.72k
}
tif_jpeg_12.c:TIFFjpeg_abort
Line
Count
Source
456
10.9k
{
457
10.9k
    return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm));
458
10.9k
}
459
460
static int TIFFjpeg_destroy(JPEGState *sp)
461
637k
{
462
637k
    return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm));
463
637k
}
tif_jpeg.c:TIFFjpeg_destroy
Line
Count
Source
461
416k
{
462
416k
    return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm));
463
416k
}
tif_jpeg_12.c:TIFFjpeg_destroy
Line
Count
Source
461
221k
{
462
221k
    return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm));
463
221k
}
464
465
static JSAMPARRAY TIFFjpeg_alloc_sarray(JPEGState *sp, int pool_id,
466
                                        JDIMENSION samplesperrow,
467
                                        JDIMENSION numrows)
468
0
{
469
0
    return CALLJPEG(sp, (JSAMPARRAY)NULL,
470
0
                    (*sp->cinfo.comm.mem->alloc_sarray)(
471
0
                        &sp->cinfo.comm, pool_id, samplesperrow, numrows));
472
0
}
Unexecuted instantiation: tif_jpeg.c:TIFFjpeg_alloc_sarray
Unexecuted instantiation: tif_jpeg_12.c:TIFFjpeg_alloc_sarray
473
474
/*
475
 * JPEG library destination data manager.
476
 * These routines direct compressed data from libjpeg into the
477
 * libtiff output buffer.
478
 */
479
480
static void std_init_destination(j_compress_ptr cinfo)
481
428k
{
482
428k
    JPEGState *sp = (JPEGState *)cinfo;
483
428k
    TIFF *tif = sp->tif;
484
485
428k
    sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
486
428k
    sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
487
428k
}
tif_jpeg.c:std_init_destination
Line
Count
Source
481
211k
{
482
211k
    JPEGState *sp = (JPEGState *)cinfo;
483
211k
    TIFF *tif = sp->tif;
484
485
211k
    sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
486
211k
    sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
487
211k
}
tif_jpeg_12.c:std_init_destination
Line
Count
Source
481
217k
{
482
217k
    JPEGState *sp = (JPEGState *)cinfo;
483
217k
    TIFF *tif = sp->tif;
484
485
217k
    sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
486
217k
    sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
487
217k
}
488
489
static boolean std_empty_output_buffer(j_compress_ptr cinfo)
490
0
{
491
0
    JPEGState *sp = (JPEGState *)cinfo;
492
0
    TIFF *tif = sp->tif;
493
494
    /* the entire buffer has been filled */
495
0
    tif->tif_rawcc = tif->tif_rawdatasize;
496
497
#ifdef IPPJ_HUFF
498
    /*
499
     * The Intel IPP performance library does not necessarily fill up
500
     * the whole output buffer on each pass, so only dump out the parts
501
     * that have been filled.
502
     *   http://trac.osgeo.org/gdal/wiki/JpegIPP
503
     */
504
    if (sp->dest.free_in_buffer >= 0)
505
    {
506
        tif->tif_rawcc = tif->tif_rawdatasize - sp->dest.free_in_buffer;
507
    }
508
#endif
509
510
0
    if (!TIFFFlushData1(tif))
511
0
        return FALSE;
512
0
    sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
513
0
    sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
514
515
0
    return (TRUE);
516
0
}
Unexecuted instantiation: tif_jpeg.c:std_empty_output_buffer
Unexecuted instantiation: tif_jpeg_12.c:std_empty_output_buffer
517
518
static void std_term_destination(j_compress_ptr cinfo)
519
328k
{
520
328k
    JPEGState *sp = (JPEGState *)cinfo;
521
328k
    TIFF *tif = sp->tif;
522
523
328k
    tif->tif_rawcp = (uint8_t *)sp->dest.next_output_byte;
524
328k
    tif->tif_rawcc = tif->tif_rawdatasize - (tmsize_t)sp->dest.free_in_buffer;
525
    /* NB: libtiff does the final buffer flush */
526
328k
}
tif_jpeg.c:std_term_destination
Line
Count
Source
519
111k
{
520
111k
    JPEGState *sp = (JPEGState *)cinfo;
521
111k
    TIFF *tif = sp->tif;
522
523
111k
    tif->tif_rawcp = (uint8_t *)sp->dest.next_output_byte;
524
111k
    tif->tif_rawcc = tif->tif_rawdatasize - (tmsize_t)sp->dest.free_in_buffer;
525
    /* NB: libtiff does the final buffer flush */
526
111k
}
tif_jpeg_12.c:std_term_destination
Line
Count
Source
519
217k
{
520
217k
    JPEGState *sp = (JPEGState *)cinfo;
521
217k
    TIFF *tif = sp->tif;
522
523
217k
    tif->tif_rawcp = (uint8_t *)sp->dest.next_output_byte;
524
217k
    tif->tif_rawcc = tif->tif_rawdatasize - (tmsize_t)sp->dest.free_in_buffer;
525
    /* NB: libtiff does the final buffer flush */
526
217k
}
527
528
static void TIFFjpeg_data_dest(JPEGState *sp, TIFF *tif)
529
428k
{
530
428k
    (void)tif;
531
428k
    sp->cinfo.c.dest = &sp->dest;
532
428k
    sp->dest.init_destination = std_init_destination;
533
428k
    sp->dest.empty_output_buffer = std_empty_output_buffer;
534
428k
    sp->dest.term_destination = std_term_destination;
535
428k
}
tif_jpeg.c:TIFFjpeg_data_dest
Line
Count
Source
529
211k
{
530
211k
    (void)tif;
531
211k
    sp->cinfo.c.dest = &sp->dest;
532
211k
    sp->dest.init_destination = std_init_destination;
533
211k
    sp->dest.empty_output_buffer = std_empty_output_buffer;
534
211k
    sp->dest.term_destination = std_term_destination;
535
211k
}
tif_jpeg_12.c:TIFFjpeg_data_dest
Line
Count
Source
529
217k
{
530
217k
    (void)tif;
531
217k
    sp->cinfo.c.dest = &sp->dest;
532
217k
    sp->dest.init_destination = std_init_destination;
533
217k
    sp->dest.empty_output_buffer = std_empty_output_buffer;
534
217k
    sp->dest.term_destination = std_term_destination;
535
217k
}
536
537
/*
538
 * Alternate destination manager for outputting to JPEGTables field.
539
 */
540
541
static void tables_init_destination(j_compress_ptr cinfo)
542
214k
{
543
214k
    JPEGState *sp = (JPEGState *)cinfo;
544
545
    /* while building, otherSettings.jpegtables_length is allocated buffer size
546
     */
547
214k
    sp->dest.next_output_byte = (JOCTET *)sp->otherSettings.jpegtables;
548
214k
    sp->dest.free_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
549
214k
}
tif_jpeg.c:tables_init_destination
Line
Count
Source
542
105k
{
543
105k
    JPEGState *sp = (JPEGState *)cinfo;
544
545
    /* while building, otherSettings.jpegtables_length is allocated buffer size
546
     */
547
105k
    sp->dest.next_output_byte = (JOCTET *)sp->otherSettings.jpegtables;
548
105k
    sp->dest.free_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
549
105k
}
tif_jpeg_12.c:tables_init_destination
Line
Count
Source
542
108k
{
543
108k
    JPEGState *sp = (JPEGState *)cinfo;
544
545
    /* while building, otherSettings.jpegtables_length is allocated buffer size
546
     */
547
108k
    sp->dest.next_output_byte = (JOCTET *)sp->otherSettings.jpegtables;
548
108k
    sp->dest.free_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
549
108k
}
550
551
static boolean tables_empty_output_buffer(j_compress_ptr cinfo)
552
0
{
553
0
    JPEGState *sp = (JPEGState *)cinfo;
554
0
    void *newbuf;
555
556
    /* the entire buffer has been filled; enlarge it by 1000 bytes */
557
0
    newbuf =
558
0
        _TIFFreallocExt(sp->tif, (void *)sp->otherSettings.jpegtables,
559
0
                        (tmsize_t)(sp->otherSettings.jpegtables_length + 1000));
560
0
    if (newbuf == NULL)
561
0
        ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 100);
562
0
    sp->dest.next_output_byte =
563
0
        (JOCTET *)newbuf + sp->otherSettings.jpegtables_length;
564
0
    sp->dest.free_in_buffer = (size_t)1000;
565
0
    sp->otherSettings.jpegtables = newbuf;
566
0
    sp->otherSettings.jpegtables_length += 1000;
567
0
    return (TRUE);
568
0
}
Unexecuted instantiation: tif_jpeg.c:tables_empty_output_buffer
Unexecuted instantiation: tif_jpeg_12.c:tables_empty_output_buffer
569
570
static void tables_term_destination(j_compress_ptr cinfo)
571
214k
{
572
214k
    JPEGState *sp = (JPEGState *)cinfo;
573
574
    /* set tables length to number of bytes actually emitted */
575
214k
    sp->otherSettings.jpegtables_length -= (uint32_t)sp->dest.free_in_buffer;
576
214k
}
tif_jpeg.c:tables_term_destination
Line
Count
Source
571
105k
{
572
105k
    JPEGState *sp = (JPEGState *)cinfo;
573
574
    /* set tables length to number of bytes actually emitted */
575
105k
    sp->otherSettings.jpegtables_length -= (uint32_t)sp->dest.free_in_buffer;
576
105k
}
tif_jpeg_12.c:tables_term_destination
Line
Count
Source
571
108k
{
572
108k
    JPEGState *sp = (JPEGState *)cinfo;
573
574
    /* set tables length to number of bytes actually emitted */
575
108k
    sp->otherSettings.jpegtables_length -= (uint32_t)sp->dest.free_in_buffer;
576
108k
}
577
578
static int TIFFjpeg_tables_dest(JPEGState *sp, TIFF *tif)
579
214k
{
580
214k
    (void)tif;
581
    /*
582
     * Allocate a working buffer for building tables.
583
     * Initial size is 1000 bytes, which is usually adequate.
584
     */
585
214k
    if (sp->otherSettings.jpegtables)
586
0
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
587
214k
    sp->otherSettings.jpegtables_length = 1000;
588
214k
    sp->otherSettings.jpegtables = (void *)_TIFFmallocExt(
589
214k
        tif, (tmsize_t)sp->otherSettings.jpegtables_length);
590
214k
    if (sp->otherSettings.jpegtables == NULL)
591
0
    {
592
0
        sp->otherSettings.jpegtables_length = 0;
593
0
        TIFFErrorExtR(sp->tif, "TIFFjpeg_tables_dest",
594
0
                      "No space for JPEGTables");
595
0
        return (0);
596
0
    }
597
214k
    sp->cinfo.c.dest = &sp->dest;
598
214k
    sp->dest.init_destination = tables_init_destination;
599
214k
    sp->dest.empty_output_buffer = tables_empty_output_buffer;
600
214k
    sp->dest.term_destination = tables_term_destination;
601
214k
    return (1);
602
214k
}
tif_jpeg.c:TIFFjpeg_tables_dest
Line
Count
Source
579
105k
{
580
105k
    (void)tif;
581
    /*
582
     * Allocate a working buffer for building tables.
583
     * Initial size is 1000 bytes, which is usually adequate.
584
     */
585
105k
    if (sp->otherSettings.jpegtables)
586
0
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
587
105k
    sp->otherSettings.jpegtables_length = 1000;
588
105k
    sp->otherSettings.jpegtables = (void *)_TIFFmallocExt(
589
105k
        tif, (tmsize_t)sp->otherSettings.jpegtables_length);
590
105k
    if (sp->otherSettings.jpegtables == NULL)
591
0
    {
592
0
        sp->otherSettings.jpegtables_length = 0;
593
0
        TIFFErrorExtR(sp->tif, "TIFFjpeg_tables_dest",
594
0
                      "No space for JPEGTables");
595
0
        return (0);
596
0
    }
597
105k
    sp->cinfo.c.dest = &sp->dest;
598
105k
    sp->dest.init_destination = tables_init_destination;
599
105k
    sp->dest.empty_output_buffer = tables_empty_output_buffer;
600
105k
    sp->dest.term_destination = tables_term_destination;
601
105k
    return (1);
602
105k
}
tif_jpeg_12.c:TIFFjpeg_tables_dest
Line
Count
Source
579
108k
{
580
108k
    (void)tif;
581
    /*
582
     * Allocate a working buffer for building tables.
583
     * Initial size is 1000 bytes, which is usually adequate.
584
     */
585
108k
    if (sp->otherSettings.jpegtables)
586
0
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
587
108k
    sp->otherSettings.jpegtables_length = 1000;
588
108k
    sp->otherSettings.jpegtables = (void *)_TIFFmallocExt(
589
108k
        tif, (tmsize_t)sp->otherSettings.jpegtables_length);
590
108k
    if (sp->otherSettings.jpegtables == NULL)
591
0
    {
592
0
        sp->otherSettings.jpegtables_length = 0;
593
0
        TIFFErrorExtR(sp->tif, "TIFFjpeg_tables_dest",
594
0
                      "No space for JPEGTables");
595
0
        return (0);
596
0
    }
597
108k
    sp->cinfo.c.dest = &sp->dest;
598
108k
    sp->dest.init_destination = tables_init_destination;
599
108k
    sp->dest.empty_output_buffer = tables_empty_output_buffer;
600
108k
    sp->dest.term_destination = tables_term_destination;
601
108k
    return (1);
602
108k
}
603
604
/*
605
 * JPEG library source data manager.
606
 * These routines supply compressed data to libjpeg.
607
 */
608
609
static void std_init_source(j_decompress_ptr cinfo)
610
14.8k
{
611
14.8k
    JPEGState *sp = (JPEGState *)cinfo;
612
14.8k
    TIFF *tif = sp->tif;
613
614
14.8k
    sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata;
615
14.8k
    sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
616
14.8k
}
tif_jpeg.c:std_init_source
Line
Count
Source
610
3.95k
{
611
3.95k
    JPEGState *sp = (JPEGState *)cinfo;
612
3.95k
    TIFF *tif = sp->tif;
613
614
3.95k
    sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata;
615
3.95k
    sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
616
3.95k
}
tif_jpeg_12.c:std_init_source
Line
Count
Source
610
10.9k
{
611
10.9k
    JPEGState *sp = (JPEGState *)cinfo;
612
10.9k
    TIFF *tif = sp->tif;
613
614
10.9k
    sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata;
615
10.9k
    sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
616
10.9k
}
617
618
static boolean std_fill_input_buffer(j_decompress_ptr cinfo)
619
62.8k
{
620
62.8k
    JPEGState *sp = (JPEGState *)cinfo;
621
62.8k
    static const JOCTET dummy_EOI[2] = {0xFF, JPEG_EOI};
622
623
#ifdef IPPJ_HUFF
624
    /*
625
     * The Intel IPP performance library does not necessarily read the whole
626
     * input buffer in one pass, so it is possible to get here with data
627
     * yet to read.
628
     *
629
     * We just return without doing anything, until the entire buffer has
630
     * been read.
631
     * http://trac.osgeo.org/gdal/wiki/JpegIPP
632
     */
633
    if (sp->src.bytes_in_buffer > 0)
634
    {
635
        return (TRUE);
636
    }
637
#endif
638
639
    /*
640
     * Normally the whole strip/tile is read and so we don't need to do
641
     * a fill.  In the case of CHUNKY_STRIP_READ_SUPPORT we might not have
642
     * all the data, but the rawdata is refreshed between scanlines and
643
     * we push this into the io machinery in JPEGDecode().
644
     * http://trac.osgeo.org/gdal/ticket/3894
645
     */
646
647
62.8k
    WARNMS(cinfo, JWRN_JPEG_EOF);
648
    /* insert a fake EOI marker */
649
62.8k
    sp->src.next_input_byte = dummy_EOI;
650
62.8k
    sp->src.bytes_in_buffer = 2;
651
62.8k
    return (TRUE);
652
62.8k
}
tif_jpeg.c:std_fill_input_buffer
Line
Count
Source
619
2.25k
{
620
2.25k
    JPEGState *sp = (JPEGState *)cinfo;
621
2.25k
    static const JOCTET dummy_EOI[2] = {0xFF, JPEG_EOI};
622
623
#ifdef IPPJ_HUFF
624
    /*
625
     * The Intel IPP performance library does not necessarily read the whole
626
     * input buffer in one pass, so it is possible to get here with data
627
     * yet to read.
628
     *
629
     * We just return without doing anything, until the entire buffer has
630
     * been read.
631
     * http://trac.osgeo.org/gdal/wiki/JpegIPP
632
     */
633
    if (sp->src.bytes_in_buffer > 0)
634
    {
635
        return (TRUE);
636
    }
637
#endif
638
639
    /*
640
     * Normally the whole strip/tile is read and so we don't need to do
641
     * a fill.  In the case of CHUNKY_STRIP_READ_SUPPORT we might not have
642
     * all the data, but the rawdata is refreshed between scanlines and
643
     * we push this into the io machinery in JPEGDecode().
644
     * http://trac.osgeo.org/gdal/ticket/3894
645
     */
646
647
2.25k
    WARNMS(cinfo, JWRN_JPEG_EOF);
648
    /* insert a fake EOI marker */
649
2.25k
    sp->src.next_input_byte = dummy_EOI;
650
2.25k
    sp->src.bytes_in_buffer = 2;
651
2.25k
    return (TRUE);
652
2.25k
}
tif_jpeg_12.c:std_fill_input_buffer
Line
Count
Source
619
60.5k
{
620
60.5k
    JPEGState *sp = (JPEGState *)cinfo;
621
60.5k
    static const JOCTET dummy_EOI[2] = {0xFF, JPEG_EOI};
622
623
#ifdef IPPJ_HUFF
624
    /*
625
     * The Intel IPP performance library does not necessarily read the whole
626
     * input buffer in one pass, so it is possible to get here with data
627
     * yet to read.
628
     *
629
     * We just return without doing anything, until the entire buffer has
630
     * been read.
631
     * http://trac.osgeo.org/gdal/wiki/JpegIPP
632
     */
633
    if (sp->src.bytes_in_buffer > 0)
634
    {
635
        return (TRUE);
636
    }
637
#endif
638
639
    /*
640
     * Normally the whole strip/tile is read and so we don't need to do
641
     * a fill.  In the case of CHUNKY_STRIP_READ_SUPPORT we might not have
642
     * all the data, but the rawdata is refreshed between scanlines and
643
     * we push this into the io machinery in JPEGDecode().
644
     * http://trac.osgeo.org/gdal/ticket/3894
645
     */
646
647
60.5k
    WARNMS(cinfo, JWRN_JPEG_EOF);
648
    /* insert a fake EOI marker */
649
60.5k
    sp->src.next_input_byte = dummy_EOI;
650
60.5k
    sp->src.bytes_in_buffer = 2;
651
60.5k
    return (TRUE);
652
60.5k
}
653
654
static void std_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
655
80.9k
{
656
80.9k
    JPEGState *sp = (JPEGState *)cinfo;
657
658
80.9k
    if (num_bytes > 0)
659
80.9k
    {
660
80.9k
        if ((size_t)num_bytes > sp->src.bytes_in_buffer)
661
1.57k
        {
662
            /* oops, buffer overrun */
663
1.57k
            (void)std_fill_input_buffer(cinfo);
664
1.57k
        }
665
79.3k
        else
666
79.3k
        {
667
79.3k
            sp->src.next_input_byte += (size_t)num_bytes;
668
79.3k
            sp->src.bytes_in_buffer -= (size_t)num_bytes;
669
79.3k
        }
670
80.9k
    }
671
80.9k
}
tif_jpeg.c:std_skip_input_data
Line
Count
Source
655
12.1k
{
656
12.1k
    JPEGState *sp = (JPEGState *)cinfo;
657
658
12.1k
    if (num_bytes > 0)
659
12.1k
    {
660
12.1k
        if ((size_t)num_bytes > sp->src.bytes_in_buffer)
661
135
        {
662
            /* oops, buffer overrun */
663
135
            (void)std_fill_input_buffer(cinfo);
664
135
        }
665
12.0k
        else
666
12.0k
        {
667
12.0k
            sp->src.next_input_byte += (size_t)num_bytes;
668
12.0k
            sp->src.bytes_in_buffer -= (size_t)num_bytes;
669
12.0k
        }
670
12.1k
    }
671
12.1k
}
tif_jpeg_12.c:std_skip_input_data
Line
Count
Source
655
68.7k
{
656
68.7k
    JPEGState *sp = (JPEGState *)cinfo;
657
658
68.7k
    if (num_bytes > 0)
659
68.7k
    {
660
68.7k
        if ((size_t)num_bytes > sp->src.bytes_in_buffer)
661
1.43k
        {
662
            /* oops, buffer overrun */
663
1.43k
            (void)std_fill_input_buffer(cinfo);
664
1.43k
        }
665
67.3k
        else
666
67.3k
        {
667
67.3k
            sp->src.next_input_byte += (size_t)num_bytes;
668
67.3k
            sp->src.bytes_in_buffer -= (size_t)num_bytes;
669
67.3k
        }
670
68.7k
    }
671
68.7k
}
672
673
static void std_term_source(j_decompress_ptr cinfo)
674
1.43k
{
675
    /* No work necessary here */
676
1.43k
    (void)cinfo;
677
1.43k
}
tif_jpeg.c:std_term_source
Line
Count
Source
674
255
{
675
    /* No work necessary here */
676
255
    (void)cinfo;
677
255
}
tif_jpeg_12.c:std_term_source
Line
Count
Source
674
1.17k
{
675
    /* No work necessary here */
676
1.17k
    (void)cinfo;
677
1.17k
}
678
679
static void TIFFjpeg_data_src(JPEGState *sp)
680
5.81k
{
681
5.81k
    sp->cinfo.d.src = &sp->src;
682
5.81k
    sp->src.init_source = std_init_source;
683
5.81k
    sp->src.fill_input_buffer = std_fill_input_buffer;
684
5.81k
    sp->src.skip_input_data = std_skip_input_data;
685
5.81k
    sp->src.resync_to_restart = jpeg_resync_to_restart;
686
5.81k
    sp->src.term_source = std_term_source;
687
5.81k
    sp->src.bytes_in_buffer = 0; /* for safety */
688
5.81k
    sp->src.next_input_byte = NULL;
689
5.81k
}
tif_jpeg.c:TIFFjpeg_data_src
Line
Count
Source
680
1.54k
{
681
1.54k
    sp->cinfo.d.src = &sp->src;
682
1.54k
    sp->src.init_source = std_init_source;
683
1.54k
    sp->src.fill_input_buffer = std_fill_input_buffer;
684
1.54k
    sp->src.skip_input_data = std_skip_input_data;
685
1.54k
    sp->src.resync_to_restart = jpeg_resync_to_restart;
686
1.54k
    sp->src.term_source = std_term_source;
687
1.54k
    sp->src.bytes_in_buffer = 0; /* for safety */
688
1.54k
    sp->src.next_input_byte = NULL;
689
1.54k
}
tif_jpeg_12.c:TIFFjpeg_data_src
Line
Count
Source
680
4.27k
{
681
4.27k
    sp->cinfo.d.src = &sp->src;
682
4.27k
    sp->src.init_source = std_init_source;
683
4.27k
    sp->src.fill_input_buffer = std_fill_input_buffer;
684
4.27k
    sp->src.skip_input_data = std_skip_input_data;
685
4.27k
    sp->src.resync_to_restart = jpeg_resync_to_restart;
686
4.27k
    sp->src.term_source = std_term_source;
687
4.27k
    sp->src.bytes_in_buffer = 0; /* for safety */
688
4.27k
    sp->src.next_input_byte = NULL;
689
4.27k
}
690
691
/*
692
 * Alternate source manager for reading from JPEGTables.
693
 * We can share all the code except for the init routine.
694
 */
695
696
static void tables_init_source(j_decompress_ptr cinfo)
697
537
{
698
537
    JPEGState *sp = (JPEGState *)cinfo;
699
700
537
    sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables;
701
537
    sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
702
537
}
tif_jpeg.c:tables_init_source
Line
Count
Source
697
258
{
698
258
    JPEGState *sp = (JPEGState *)cinfo;
699
700
258
    sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables;
701
258
    sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
702
258
}
tif_jpeg_12.c:tables_init_source
Line
Count
Source
697
279
{
698
279
    JPEGState *sp = (JPEGState *)cinfo;
699
700
279
    sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables;
701
279
    sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
702
279
}
703
704
static void TIFFjpeg_tables_src(JPEGState *sp)
705
539
{
706
539
    TIFFjpeg_data_src(sp);
707
539
    sp->src.init_source = tables_init_source;
708
539
}
tif_jpeg.c:TIFFjpeg_tables_src
Line
Count
Source
705
258
{
706
258
    TIFFjpeg_data_src(sp);
707
258
    sp->src.init_source = tables_init_source;
708
258
}
tif_jpeg_12.c:TIFFjpeg_tables_src
Line
Count
Source
705
281
{
706
281
    TIFFjpeg_data_src(sp);
707
281
    sp->src.init_source = tables_init_source;
708
281
}
709
710
/*
711
 * Allocate downsampled-data buffers needed for downsampled I/O.
712
 * We use values computed in jpeg_start_compress or jpeg_start_decompress.
713
 * We use libjpeg's allocator so that buffers will be released automatically
714
 * when done with strip/tile.
715
 * This is also a handy place to compute samplesperclump, bytesperline.
716
 */
717
static int alloc_downsampled_buffers(TIFF *tif, jpeg_component_info *comp_info,
718
                                     int num_components)
719
0
{
720
0
    JPEGState *sp = JState(tif);
721
0
    int ci;
722
0
    jpeg_component_info *compptr;
723
0
    TIFF_JSAMPARRAY buf;
724
0
    int samples_per_clump = 0;
725
726
0
    for (ci = 0, compptr = comp_info; ci < num_components; ci++, compptr++)
727
0
    {
728
0
        samples_per_clump += compptr->h_samp_factor * compptr->v_samp_factor;
729
0
        buf = (TIFF_JSAMPARRAY)TIFFjpeg_alloc_sarray(
730
0
            sp, JPOOL_IMAGE, compptr->width_in_blocks * DCTSIZE,
731
0
            (JDIMENSION)(compptr->v_samp_factor * DCTSIZE));
732
0
        if (buf == NULL)
733
0
            return (0);
734
0
        sp->ds_buffer[ci] = buf;
735
0
    }
736
0
    sp->samplesperclump = samples_per_clump;
737
0
    return (1);
738
0
}
Unexecuted instantiation: tif_jpeg.c:alloc_downsampled_buffers
Unexecuted instantiation: tif_jpeg_12.c:alloc_downsampled_buffers
739
740
/*
741
 * JPEG Decoding.
742
 */
743
744
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
745
746
11.8k
#define JPEG_MARKER_SOF0 0xC0
747
13.2k
#define JPEG_MARKER_SOF1 0xC1
748
14.8k
#define JPEG_MARKER_SOF2 0xC2
749
15.4k
#define JPEG_MARKER_SOF9 0xC9
750
16.0k
#define JPEG_MARKER_SOF10 0xCA
751
1.01M
#define JPEG_MARKER_DHT 0xC4
752
19.5k
#define JPEG_MARKER_SOI 0xD8
753
1.00M
#define JPEG_MARKER_SOS 0xDA
754
999k
#define JPEG_MARKER_DQT 0xDB
755
1.02M
#define JPEG_MARKER_DRI 0xDD
756
8.29M
#define JPEG_MARKER_APP0 0xE0
757
47.9k
#define JPEG_MARKER_COM 0xFE
758
struct JPEGFixupTagsSubsamplingData
759
{
760
    TIFF *tif;
761
    void *buffer;
762
    uint32_t buffersize;
763
    uint8_t *buffercurrentbyte;
764
    uint32_t bufferbytesleft;
765
    uint64_t fileoffset;
766
    uint64_t filebytesleft;
767
    uint8_t filepositioned;
768
};
769
static void JPEGFixupTagsSubsampling(TIFF *tif);
770
static int
771
JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData *data);
772
static int
773
JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData *data,
774
                                 uint8_t *result);
775
static int
776
JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData *data,
777
                                 uint16_t *result);
778
static void
779
JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData *data,
780
                             uint16_t skiplength);
781
782
#endif
783
784
static int JPEGFixupTags(TIFF *tif)
785
699k
{
786
699k
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
787
699k
    JPEGState *sp = JState(tif);
788
699k
    if ((tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR) &&
789
699k
        (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
790
699k
        (tif->tif_dir.td_samplesperpixel == 3) &&
791
699k
        !sp->otherSettings.ycbcrsampling_fetched)
792
42.0k
        JPEGFixupTagsSubsampling(tif);
793
699k
#endif
794
795
699k
    return (1);
796
699k
}
tif_jpeg.c:JPEGFixupTags
Line
Count
Source
785
699k
{
786
699k
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
787
699k
    JPEGState *sp = JState(tif);
788
699k
    if ((tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR) &&
789
699k
        (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
790
699k
        (tif->tif_dir.td_samplesperpixel == 3) &&
791
699k
        !sp->otherSettings.ycbcrsampling_fetched)
792
42.0k
        JPEGFixupTagsSubsampling(tif);
793
699k
#endif
794
795
699k
    return (1);
796
699k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTags
797
798
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
799
800
static void JPEGFixupTagsSubsampling(TIFF *tif)
801
42.0k
{
802
    /*
803
     * Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in
804
     * the TIFF tags, but still use non-default (2,2) values within the jpeg
805
     * data stream itself.  In order for TIFF applications to work properly
806
     * - for instance to get the strip buffer size right - it is imperative
807
     * that the subsampling be available before we start reading the image
808
     * data normally.  This function will attempt to analyze the first strip in
809
     * order to get the sampling values from the jpeg data stream.
810
     *
811
     * Note that JPEGPreDeocode() will produce a fairly loud warning when the
812
     * discovered sampling does not match the default sampling (2,2) or whatever
813
     * was actually in the tiff tags.
814
     *
815
     * See the bug in bugzilla for details:
816
     *
817
     * http://bugzilla.remotesensing.org/show_bug.cgi?id=168
818
     *
819
     * Frank Warmerdam, July 2002
820
     * Joris Van Damme, May 2007
821
     */
822
42.0k
    static const char module[] = "JPEGFixupTagsSubsampling";
823
42.0k
    struct JPEGFixupTagsSubsamplingData m;
824
42.0k
    uint64_t fileoffset = TIFFGetStrileOffset(tif, 0);
825
826
42.0k
    if (fileoffset == 0)
827
5.45k
    {
828
        /* Do not even try to check if the first strip/tile does not
829
           yet exist, as occurs when GDAL has created a new NULL file
830
           for instance. */
831
5.45k
        return;
832
5.45k
    }
833
834
36.5k
    m.tif = tif;
835
36.5k
    m.buffersize = 2048;
836
36.5k
    m.buffer = _TIFFmallocExt(tif, m.buffersize);
837
36.5k
    if (m.buffer == NULL)
838
0
    {
839
0
        TIFFWarningExtR(tif, module,
840
0
                        "Unable to allocate memory for auto-correcting of "
841
0
                        "subsampling values; auto-correcting skipped");
842
0
        return;
843
0
    }
844
36.5k
    m.buffercurrentbyte = NULL;
845
36.5k
    m.bufferbytesleft = 0;
846
36.5k
    m.fileoffset = fileoffset;
847
36.5k
    m.filepositioned = 0;
848
36.5k
    m.filebytesleft = TIFFGetStrileByteCount(tif, 0);
849
36.5k
    if (!JPEGFixupTagsSubsamplingSec(&m))
850
30.1k
        TIFFWarningExtR(
851
30.1k
            tif, module,
852
30.1k
            "Unable to auto-correct subsampling values, likely corrupt JPEG "
853
30.1k
            "compressed data in first strip/tile; auto-correcting skipped");
854
36.5k
    _TIFFfreeExt(tif, m.buffer);
855
36.5k
}
tif_jpeg.c:JPEGFixupTagsSubsampling
Line
Count
Source
801
42.0k
{
802
    /*
803
     * Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in
804
     * the TIFF tags, but still use non-default (2,2) values within the jpeg
805
     * data stream itself.  In order for TIFF applications to work properly
806
     * - for instance to get the strip buffer size right - it is imperative
807
     * that the subsampling be available before we start reading the image
808
     * data normally.  This function will attempt to analyze the first strip in
809
     * order to get the sampling values from the jpeg data stream.
810
     *
811
     * Note that JPEGPreDeocode() will produce a fairly loud warning when the
812
     * discovered sampling does not match the default sampling (2,2) or whatever
813
     * was actually in the tiff tags.
814
     *
815
     * See the bug in bugzilla for details:
816
     *
817
     * http://bugzilla.remotesensing.org/show_bug.cgi?id=168
818
     *
819
     * Frank Warmerdam, July 2002
820
     * Joris Van Damme, May 2007
821
     */
822
42.0k
    static const char module[] = "JPEGFixupTagsSubsampling";
823
42.0k
    struct JPEGFixupTagsSubsamplingData m;
824
42.0k
    uint64_t fileoffset = TIFFGetStrileOffset(tif, 0);
825
826
42.0k
    if (fileoffset == 0)
827
5.45k
    {
828
        /* Do not even try to check if the first strip/tile does not
829
           yet exist, as occurs when GDAL has created a new NULL file
830
           for instance. */
831
5.45k
        return;
832
5.45k
    }
833
834
36.5k
    m.tif = tif;
835
36.5k
    m.buffersize = 2048;
836
36.5k
    m.buffer = _TIFFmallocExt(tif, m.buffersize);
837
36.5k
    if (m.buffer == NULL)
838
0
    {
839
0
        TIFFWarningExtR(tif, module,
840
0
                        "Unable to allocate memory for auto-correcting of "
841
0
                        "subsampling values; auto-correcting skipped");
842
0
        return;
843
0
    }
844
36.5k
    m.buffercurrentbyte = NULL;
845
36.5k
    m.bufferbytesleft = 0;
846
36.5k
    m.fileoffset = fileoffset;
847
36.5k
    m.filepositioned = 0;
848
36.5k
    m.filebytesleft = TIFFGetStrileByteCount(tif, 0);
849
36.5k
    if (!JPEGFixupTagsSubsamplingSec(&m))
850
30.1k
        TIFFWarningExtR(
851
30.1k
            tif, module,
852
30.1k
            "Unable to auto-correct subsampling values, likely corrupt JPEG "
853
30.1k
            "compressed data in first strip/tile; auto-correcting skipped");
854
36.5k
    _TIFFfreeExt(tif, m.buffer);
855
36.5k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsampling
856
857
static int
858
JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData *data)
859
36.5k
{
860
36.5k
    static const char module[] = "JPEGFixupTagsSubsamplingSec";
861
36.5k
    uint8_t m;
862
1.08M
    while (1)
863
1.08M
    {
864
49.9M
        while (1)
865
49.9M
        {
866
49.9M
            if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
867
11.1k
                return (0);
868
49.9M
            if (m == 255)
869
1.07M
                break;
870
49.9M
        }
871
4.34M
        while (1)
872
4.34M
        {
873
4.34M
            if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
874
1.75k
                return (0);
875
4.34M
            if (m != 255)
876
1.06M
                break;
877
4.34M
        }
878
1.06M
        switch (m)
879
1.06M
        {
880
19.5k
            case JPEG_MARKER_SOI:
881
                /* this type of marker has no data and should be skipped */
882
19.5k
                break;
883
47.9k
            case JPEG_MARKER_COM:
884
184k
            case JPEG_MARKER_APP0:
885
211k
            case JPEG_MARKER_APP0 + 1:
886
234k
            case JPEG_MARKER_APP0 + 2:
887
237k
            case JPEG_MARKER_APP0 + 3:
888
326k
            case JPEG_MARKER_APP0 + 4:
889
437k
            case JPEG_MARKER_APP0 + 5:
890
473k
            case JPEG_MARKER_APP0 + 6:
891
524k
            case JPEG_MARKER_APP0 + 7:
892
532k
            case JPEG_MARKER_APP0 + 8:
893
599k
            case JPEG_MARKER_APP0 + 9:
894
629k
            case JPEG_MARKER_APP0 + 10:
895
654k
            case JPEG_MARKER_APP0 + 11:
896
673k
            case JPEG_MARKER_APP0 + 12:
897
690k
            case JPEG_MARKER_APP0 + 13:
898
898k
            case JPEG_MARKER_APP0 + 14:
899
991k
            case JPEG_MARKER_APP0 + 15:
900
999k
            case JPEG_MARKER_DQT:
901
1.00M
            case JPEG_MARKER_SOS:
902
1.01M
            case JPEG_MARKER_DHT:
903
1.02M
            case JPEG_MARKER_DRI:
904
                /* this type of marker has data, but it has no use to us and
905
                 * should be skipped */
906
1.02M
                {
907
1.02M
                    uint16_t n;
908
1.02M
                    if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
909
1.93k
                        return (0);
910
1.02M
                    if (n < 2)
911
1.02k
                        return (0);
912
1.02M
                    n -= 2;
913
1.02M
                    if (n > 0)
914
840k
                        JPEGFixupTagsSubsamplingSkip(data, n);
915
1.02M
                }
916
0
                break;
917
11.8k
            case JPEG_MARKER_SOF0:  /* Baseline sequential Huffman */
918
13.2k
            case JPEG_MARKER_SOF1:  /* Extended sequential Huffman */
919
14.8k
            case JPEG_MARKER_SOF2:  /* Progressive Huffman: normally not allowed
920
                                       by  TechNote, but that doesn't hurt
921
                                       supporting it */
922
15.4k
            case JPEG_MARKER_SOF9:  /* Extended sequential arithmetic */
923
16.0k
            case JPEG_MARKER_SOF10: /* Progressive arithmetic: normally not
924
                                       allowed by TechNote, but that doesn't
925
                                       hurt supporting it */
926
                /* this marker contains the subsampling factors we're scanning
927
                 * for */
928
16.0k
                {
929
16.0k
                    uint16_t n;
930
16.0k
                    uint16_t o;
931
16.0k
                    uint8_t p;
932
16.0k
                    uint8_t ph, pv;
933
16.0k
                    if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
934
2.65k
                        return (0);
935
13.4k
                    if (n != 8 + data->tif->tif_dir.td_samplesperpixel * 3)
936
1.88k
                        return (0);
937
11.5k
                    JPEGFixupTagsSubsamplingSkip(data, 7);
938
11.5k
                    if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
939
1.75k
                        return (0);
940
9.76k
                    ph = (p >> 4);
941
9.76k
                    pv = (p & 15);
942
9.76k
                    JPEGFixupTagsSubsamplingSkip(data, 1);
943
20.6k
                    for (o = 1; o < data->tif->tif_dir.td_samplesperpixel; o++)
944
16.5k
                    {
945
16.5k
                        JPEGFixupTagsSubsamplingSkip(data, 1);
946
16.5k
                        if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
947
3.33k
                            return (0);
948
13.2k
                        if (p != 0x11)
949
2.31k
                        {
950
2.31k
                            TIFFWarningExtR(data->tif, module,
951
2.31k
                                            "Subsampling values inside JPEG "
952
2.31k
                                            "compressed data "
953
2.31k
                                            "have no TIFF equivalent, "
954
2.31k
                                            "auto-correction of TIFF "
955
2.31k
                                            "subsampling values failed");
956
2.31k
                            return (1);
957
2.31k
                        }
958
10.9k
                        JPEGFixupTagsSubsamplingSkip(data, 1);
959
10.9k
                    }
960
4.11k
                    if (((ph != 1) && (ph != 2) && (ph != 4)) ||
961
4.11k
                        ((pv != 1) && (pv != 2) && (pv != 4)))
962
307
                    {
963
307
                        TIFFWarningExtR(data->tif, module,
964
307
                                        "Subsampling values inside JPEG "
965
307
                                        "compressed data have no TIFF "
966
307
                                        "equivalent, auto-correction of TIFF "
967
307
                                        "subsampling values failed");
968
307
                        return (1);
969
307
                    }
970
3.81k
                    if ((ph != data->tif->tif_dir.td_ycbcrsubsampling[0]) ||
971
3.81k
                        (pv != data->tif->tif_dir.td_ycbcrsubsampling[1]))
972
3.34k
                    {
973
3.34k
                        TIFFWarningExtR(
974
3.34k
                            data->tif, module,
975
3.34k
                            "Auto-corrected former TIFF subsampling values "
976
3.34k
                            "[%" PRIu16 ",%" PRIu16
977
3.34k
                            "] to match subsampling values inside JPEG "
978
3.34k
                            "compressed data [%" PRIu8 ",%" PRIu8 "]",
979
3.34k
                            data->tif->tif_dir.td_ycbcrsubsampling[0],
980
3.34k
                            data->tif->tif_dir.td_ycbcrsubsampling[1], ph, pv);
981
3.34k
                        data->tif->tif_dir.td_ycbcrsubsampling[0] = ph;
982
3.34k
                        data->tif->tif_dir.td_ycbcrsubsampling[1] = pv;
983
3.34k
                    }
984
3.81k
                }
985
0
                return (1);
986
4.65k
            default:
987
4.65k
                return (0);
988
1.06M
        }
989
1.06M
    }
990
36.5k
}
tif_jpeg.c:JPEGFixupTagsSubsamplingSec
Line
Count
Source
859
36.5k
{
860
36.5k
    static const char module[] = "JPEGFixupTagsSubsamplingSec";
861
36.5k
    uint8_t m;
862
1.08M
    while (1)
863
1.08M
    {
864
49.9M
        while (1)
865
49.9M
        {
866
49.9M
            if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
867
11.1k
                return (0);
868
49.9M
            if (m == 255)
869
1.07M
                break;
870
49.9M
        }
871
4.34M
        while (1)
872
4.34M
        {
873
4.34M
            if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
874
1.75k
                return (0);
875
4.34M
            if (m != 255)
876
1.06M
                break;
877
4.34M
        }
878
1.06M
        switch (m)
879
1.06M
        {
880
19.5k
            case JPEG_MARKER_SOI:
881
                /* this type of marker has no data and should be skipped */
882
19.5k
                break;
883
47.9k
            case JPEG_MARKER_COM:
884
184k
            case JPEG_MARKER_APP0:
885
211k
            case JPEG_MARKER_APP0 + 1:
886
234k
            case JPEG_MARKER_APP0 + 2:
887
237k
            case JPEG_MARKER_APP0 + 3:
888
326k
            case JPEG_MARKER_APP0 + 4:
889
437k
            case JPEG_MARKER_APP0 + 5:
890
473k
            case JPEG_MARKER_APP0 + 6:
891
524k
            case JPEG_MARKER_APP0 + 7:
892
532k
            case JPEG_MARKER_APP0 + 8:
893
599k
            case JPEG_MARKER_APP0 + 9:
894
629k
            case JPEG_MARKER_APP0 + 10:
895
654k
            case JPEG_MARKER_APP0 + 11:
896
673k
            case JPEG_MARKER_APP0 + 12:
897
690k
            case JPEG_MARKER_APP0 + 13:
898
898k
            case JPEG_MARKER_APP0 + 14:
899
991k
            case JPEG_MARKER_APP0 + 15:
900
999k
            case JPEG_MARKER_DQT:
901
1.00M
            case JPEG_MARKER_SOS:
902
1.01M
            case JPEG_MARKER_DHT:
903
1.02M
            case JPEG_MARKER_DRI:
904
                /* this type of marker has data, but it has no use to us and
905
                 * should be skipped */
906
1.02M
                {
907
1.02M
                    uint16_t n;
908
1.02M
                    if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
909
1.93k
                        return (0);
910
1.02M
                    if (n < 2)
911
1.02k
                        return (0);
912
1.02M
                    n -= 2;
913
1.02M
                    if (n > 0)
914
840k
                        JPEGFixupTagsSubsamplingSkip(data, n);
915
1.02M
                }
916
0
                break;
917
11.8k
            case JPEG_MARKER_SOF0:  /* Baseline sequential Huffman */
918
13.2k
            case JPEG_MARKER_SOF1:  /* Extended sequential Huffman */
919
14.8k
            case JPEG_MARKER_SOF2:  /* Progressive Huffman: normally not allowed
920
                                       by  TechNote, but that doesn't hurt
921
                                       supporting it */
922
15.4k
            case JPEG_MARKER_SOF9:  /* Extended sequential arithmetic */
923
16.0k
            case JPEG_MARKER_SOF10: /* Progressive arithmetic: normally not
924
                                       allowed by TechNote, but that doesn't
925
                                       hurt supporting it */
926
                /* this marker contains the subsampling factors we're scanning
927
                 * for */
928
16.0k
                {
929
16.0k
                    uint16_t n;
930
16.0k
                    uint16_t o;
931
16.0k
                    uint8_t p;
932
16.0k
                    uint8_t ph, pv;
933
16.0k
                    if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
934
2.65k
                        return (0);
935
13.4k
                    if (n != 8 + data->tif->tif_dir.td_samplesperpixel * 3)
936
1.88k
                        return (0);
937
11.5k
                    JPEGFixupTagsSubsamplingSkip(data, 7);
938
11.5k
                    if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
939
1.75k
                        return (0);
940
9.76k
                    ph = (p >> 4);
941
9.76k
                    pv = (p & 15);
942
9.76k
                    JPEGFixupTagsSubsamplingSkip(data, 1);
943
20.6k
                    for (o = 1; o < data->tif->tif_dir.td_samplesperpixel; o++)
944
16.5k
                    {
945
16.5k
                        JPEGFixupTagsSubsamplingSkip(data, 1);
946
16.5k
                        if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
947
3.33k
                            return (0);
948
13.2k
                        if (p != 0x11)
949
2.31k
                        {
950
2.31k
                            TIFFWarningExtR(data->tif, module,
951
2.31k
                                            "Subsampling values inside JPEG "
952
2.31k
                                            "compressed data "
953
2.31k
                                            "have no TIFF equivalent, "
954
2.31k
                                            "auto-correction of TIFF "
955
2.31k
                                            "subsampling values failed");
956
2.31k
                            return (1);
957
2.31k
                        }
958
10.9k
                        JPEGFixupTagsSubsamplingSkip(data, 1);
959
10.9k
                    }
960
4.11k
                    if (((ph != 1) && (ph != 2) && (ph != 4)) ||
961
4.11k
                        ((pv != 1) && (pv != 2) && (pv != 4)))
962
307
                    {
963
307
                        TIFFWarningExtR(data->tif, module,
964
307
                                        "Subsampling values inside JPEG "
965
307
                                        "compressed data have no TIFF "
966
307
                                        "equivalent, auto-correction of TIFF "
967
307
                                        "subsampling values failed");
968
307
                        return (1);
969
307
                    }
970
3.81k
                    if ((ph != data->tif->tif_dir.td_ycbcrsubsampling[0]) ||
971
3.81k
                        (pv != data->tif->tif_dir.td_ycbcrsubsampling[1]))
972
3.34k
                    {
973
3.34k
                        TIFFWarningExtR(
974
3.34k
                            data->tif, module,
975
3.34k
                            "Auto-corrected former TIFF subsampling values "
976
3.34k
                            "[%" PRIu16 ",%" PRIu16
977
3.34k
                            "] to match subsampling values inside JPEG "
978
3.34k
                            "compressed data [%" PRIu8 ",%" PRIu8 "]",
979
3.34k
                            data->tif->tif_dir.td_ycbcrsubsampling[0],
980
3.34k
                            data->tif->tif_dir.td_ycbcrsubsampling[1], ph, pv);
981
3.34k
                        data->tif->tif_dir.td_ycbcrsubsampling[0] = ph;
982
3.34k
                        data->tif->tif_dir.td_ycbcrsubsampling[1] = pv;
983
3.34k
                    }
984
3.81k
                }
985
0
                return (1);
986
4.65k
            default:
987
4.65k
                return (0);
988
1.06M
        }
989
1.06M
    }
990
36.5k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingSec
991
992
static int
993
JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData *data,
994
                                 uint8_t *result)
995
56.4M
{
996
56.4M
    if (data->bufferbytesleft == 0)
997
82.4k
    {
998
82.4k
        uint32_t m;
999
82.4k
        if (data->filebytesleft == 0)
1000
7.66k
            return (0);
1001
74.7k
        if (!data->filepositioned)
1002
50.0k
        {
1003
50.0k
            if (TIFFSeekFile(data->tif, data->fileoffset, SEEK_SET) ==
1004
50.0k
                (toff_t)-1)
1005
49
            {
1006
49
                return 0;
1007
49
            }
1008
50.0k
            data->filepositioned = 1;
1009
50.0k
        }
1010
74.6k
        m = data->buffersize;
1011
74.6k
        if ((uint64_t)m > data->filebytesleft)
1012
18.3k
            m = (uint32_t)data->filebytesleft;
1013
74.6k
        assert(m < 0x80000000UL);
1014
74.6k
        if (TIFFReadFile(data->tif, data->buffer, (tmsize_t)m) != (tmsize_t)m)
1015
14.8k
            return (0);
1016
59.8k
        data->buffercurrentbyte = data->buffer;
1017
59.8k
        data->bufferbytesleft = m;
1018
59.8k
        data->fileoffset += m;
1019
59.8k
        data->filebytesleft -= m;
1020
59.8k
    }
1021
56.4M
    *result = *data->buffercurrentbyte;
1022
56.4M
    data->buffercurrentbyte++;
1023
56.4M
    data->bufferbytesleft--;
1024
56.4M
    return (1);
1025
56.4M
}
tif_jpeg.c:JPEGFixupTagsSubsamplingReadByte
Line
Count
Source
995
56.4M
{
996
56.4M
    if (data->bufferbytesleft == 0)
997
82.4k
    {
998
82.4k
        uint32_t m;
999
82.4k
        if (data->filebytesleft == 0)
1000
7.66k
            return (0);
1001
74.7k
        if (!data->filepositioned)
1002
50.0k
        {
1003
50.0k
            if (TIFFSeekFile(data->tif, data->fileoffset, SEEK_SET) ==
1004
50.0k
                (toff_t)-1)
1005
49
            {
1006
49
                return 0;
1007
49
            }
1008
50.0k
            data->filepositioned = 1;
1009
50.0k
        }
1010
74.6k
        m = data->buffersize;
1011
74.6k
        if ((uint64_t)m > data->filebytesleft)
1012
18.3k
            m = (uint32_t)data->filebytesleft;
1013
74.6k
        assert(m < 0x80000000UL);
1014
74.6k
        if (TIFFReadFile(data->tif, data->buffer, (tmsize_t)m) != (tmsize_t)m)
1015
14.8k
            return (0);
1016
59.8k
        data->buffercurrentbyte = data->buffer;
1017
59.8k
        data->bufferbytesleft = m;
1018
59.8k
        data->fileoffset += m;
1019
59.8k
        data->filebytesleft -= m;
1020
59.8k
    }
1021
56.4M
    *result = *data->buffercurrentbyte;
1022
56.4M
    data->buffercurrentbyte++;
1023
56.4M
    data->bufferbytesleft--;
1024
56.4M
    return (1);
1025
56.4M
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingReadByte
1026
1027
static int
1028
JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData *data,
1029
                                 uint16_t *result)
1030
1.04M
{
1031
1.04M
    uint8_t ma;
1032
1.04M
    uint8_t mb;
1033
1.04M
    if (!JPEGFixupTagsSubsamplingReadByte(data, &ma))
1034
2.58k
        return (0);
1035
1.04M
    if (!JPEGFixupTagsSubsamplingReadByte(data, &mb))
1036
2.00k
        return (0);
1037
1.04M
    *result = (ma << 8) | mb;
1038
1.04M
    return (1);
1039
1.04M
}
tif_jpeg.c:JPEGFixupTagsSubsamplingReadWord
Line
Count
Source
1030
1.04M
{
1031
1.04M
    uint8_t ma;
1032
1.04M
    uint8_t mb;
1033
1.04M
    if (!JPEGFixupTagsSubsamplingReadByte(data, &ma))
1034
2.58k
        return (0);
1035
1.04M
    if (!JPEGFixupTagsSubsamplingReadByte(data, &mb))
1036
2.00k
        return (0);
1037
1.04M
    *result = (ma << 8) | mb;
1038
1.04M
    return (1);
1039
1.04M
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingReadWord
1040
1041
static void
1042
JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData *data,
1043
                             uint16_t skiplength)
1044
888k
{
1045
888k
    if ((uint32_t)skiplength <= data->bufferbytesleft)
1046
868k
    {
1047
868k
        data->buffercurrentbyte += skiplength;
1048
868k
        data->bufferbytesleft -= skiplength;
1049
868k
    }
1050
20.0k
    else
1051
20.0k
    {
1052
20.0k
        uint16_t m;
1053
20.0k
        m = (uint16_t)(skiplength - data->bufferbytesleft);
1054
20.0k
        if (m <= data->filebytesleft)
1055
16.2k
        {
1056
16.2k
            data->bufferbytesleft = 0;
1057
16.2k
            data->fileoffset += m;
1058
16.2k
            data->filebytesleft -= m;
1059
16.2k
            data->filepositioned = 0;
1060
16.2k
        }
1061
3.76k
        else
1062
3.76k
        {
1063
3.76k
            data->bufferbytesleft = 0;
1064
3.76k
            data->filebytesleft = 0;
1065
3.76k
        }
1066
20.0k
    }
1067
888k
}
tif_jpeg.c:JPEGFixupTagsSubsamplingSkip
Line
Count
Source
1044
888k
{
1045
888k
    if ((uint32_t)skiplength <= data->bufferbytesleft)
1046
868k
    {
1047
868k
        data->buffercurrentbyte += skiplength;
1048
868k
        data->bufferbytesleft -= skiplength;
1049
868k
    }
1050
20.0k
    else
1051
20.0k
    {
1052
20.0k
        uint16_t m;
1053
20.0k
        m = (uint16_t)(skiplength - data->bufferbytesleft);
1054
20.0k
        if (m <= data->filebytesleft)
1055
16.2k
        {
1056
16.2k
            data->bufferbytesleft = 0;
1057
16.2k
            data->fileoffset += m;
1058
16.2k
            data->filebytesleft -= m;
1059
16.2k
            data->filepositioned = 0;
1060
16.2k
        }
1061
3.76k
        else
1062
3.76k
        {
1063
3.76k
            data->bufferbytesleft = 0;
1064
3.76k
            data->filebytesleft = 0;
1065
3.76k
        }
1066
20.0k
    }
1067
888k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingSkip
1068
1069
#endif
1070
1071
static int JPEGSetupDecode(TIFF *tif)
1072
9.39k
{
1073
9.39k
    JPEGState *sp = JState(tif);
1074
9.39k
    TIFFDirectory *td = &tif->tif_dir;
1075
1076
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1077
5.24k
    if (tif->tif_dir.td_bitspersample == 12)
1078
4.03k
    {
1079
        /* We pass a pointer to a copy of otherSettings, since */
1080
        /* TIFFReInitJPEG_12() will clear sp */
1081
4.03k
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1082
4.03k
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0);
1083
4.03k
    }
1084
1.20k
#endif
1085
1086
5.35k
    JPEGInitializeLibJPEG(tif, TRUE);
1087
1088
1.20k
    assert(sp != NULL);
1089
5.35k
    assert(sp->cinfo.comm.is_decompressor);
1090
1091
    /* Read JPEGTables if it is present */
1092
5.35k
    if (TIFFFieldSet(tif, FIELD_JPEGTABLES))
1093
539
    {
1094
539
        TIFFjpeg_tables_src(sp);
1095
539
        if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY)
1096
313
        {
1097
313
            TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field");
1098
313
            return (0);
1099
313
        }
1100
539
    }
1101
1102
    /* Grab parameters that are same for all strips/tiles */
1103
5.04k
    sp->photometric = td->td_photometric;
1104
5.04k
    switch (sp->photometric)
1105
5.04k
    {
1106
1.21k
        case PHOTOMETRIC_YCBCR:
1107
1.21k
            sp->h_sampling = td->td_ycbcrsubsampling[0];
1108
1.21k
            sp->v_sampling = td->td_ycbcrsubsampling[1];
1109
1.21k
            break;
1110
3.82k
        default:
1111
            /* TIFF 6.0 forbids subsampling of all other color spaces */
1112
3.82k
            sp->h_sampling = 1;
1113
3.82k
            sp->v_sampling = 1;
1114
3.82k
            break;
1115
5.04k
    }
1116
1117
    /* Set up for reading normal data */
1118
5.04k
    TIFFjpeg_data_src(sp);
1119
5.04k
    tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */
1120
5.04k
    return (1);
1121
5.04k
}
tif_jpeg.c:JPEGSetupDecode
Line
Count
Source
1072
5.24k
{
1073
5.24k
    JPEGState *sp = JState(tif);
1074
5.24k
    TIFFDirectory *td = &tif->tif_dir;
1075
1076
5.24k
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1077
5.24k
    if (tif->tif_dir.td_bitspersample == 12)
1078
4.03k
    {
1079
        /* We pass a pointer to a copy of otherSettings, since */
1080
        /* TIFFReInitJPEG_12() will clear sp */
1081
4.03k
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1082
4.03k
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0);
1083
4.03k
    }
1084
1.20k
#endif
1085
1086
1.20k
    JPEGInitializeLibJPEG(tif, TRUE);
1087
1088
1.20k
    assert(sp != NULL);
1089
1.20k
    assert(sp->cinfo.comm.is_decompressor);
1090
1091
    /* Read JPEGTables if it is present */
1092
1.20k
    if (TIFFFieldSet(tif, FIELD_JPEGTABLES))
1093
258
    {
1094
258
        TIFFjpeg_tables_src(sp);
1095
258
        if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY)
1096
154
        {
1097
154
            TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field");
1098
154
            return (0);
1099
154
        }
1100
258
    }
1101
1102
    /* Grab parameters that are same for all strips/tiles */
1103
1.05k
    sp->photometric = td->td_photometric;
1104
1.05k
    switch (sp->photometric)
1105
1.05k
    {
1106
698
        case PHOTOMETRIC_YCBCR:
1107
698
            sp->h_sampling = td->td_ycbcrsubsampling[0];
1108
698
            sp->v_sampling = td->td_ycbcrsubsampling[1];
1109
698
            break;
1110
353
        default:
1111
            /* TIFF 6.0 forbids subsampling of all other color spaces */
1112
353
            sp->h_sampling = 1;
1113
353
            sp->v_sampling = 1;
1114
353
            break;
1115
1.05k
    }
1116
1117
    /* Set up for reading normal data */
1118
1.05k
    TIFFjpeg_data_src(sp);
1119
1.05k
    tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */
1120
1.05k
    return (1);
1121
1.05k
}
tif_jpeg_12.c:JPEGSetupDecode
Line
Count
Source
1072
4.15k
{
1073
4.15k
    JPEGState *sp = JState(tif);
1074
4.15k
    TIFFDirectory *td = &tif->tif_dir;
1075
1076
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1077
    if (tif->tif_dir.td_bitspersample == 12)
1078
    {
1079
        /* We pass a pointer to a copy of otherSettings, since */
1080
        /* TIFFReInitJPEG_12() will clear sp */
1081
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1082
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0);
1083
    }
1084
#endif
1085
1086
4.15k
    JPEGInitializeLibJPEG(tif, TRUE);
1087
1088
4.15k
    assert(sp != NULL);
1089
4.15k
    assert(sp->cinfo.comm.is_decompressor);
1090
1091
    /* Read JPEGTables if it is present */
1092
4.15k
    if (TIFFFieldSet(tif, FIELD_JPEGTABLES))
1093
281
    {
1094
281
        TIFFjpeg_tables_src(sp);
1095
281
        if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY)
1096
159
        {
1097
159
            TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field");
1098
159
            return (0);
1099
159
        }
1100
281
    }
1101
1102
    /* Grab parameters that are same for all strips/tiles */
1103
3.99k
    sp->photometric = td->td_photometric;
1104
3.99k
    switch (sp->photometric)
1105
3.99k
    {
1106
517
        case PHOTOMETRIC_YCBCR:
1107
517
            sp->h_sampling = td->td_ycbcrsubsampling[0];
1108
517
            sp->v_sampling = td->td_ycbcrsubsampling[1];
1109
517
            break;
1110
3.47k
        default:
1111
            /* TIFF 6.0 forbids subsampling of all other color spaces */
1112
3.47k
            sp->h_sampling = 1;
1113
3.47k
            sp->v_sampling = 1;
1114
3.47k
            break;
1115
3.99k
    }
1116
1117
    /* Set up for reading normal data */
1118
3.99k
    TIFFjpeg_data_src(sp);
1119
3.99k
    tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */
1120
3.99k
    return (1);
1121
3.99k
}
1122
1123
/* Returns 1 if the full strip should be read, even when doing scanline per */
1124
/* scanline decoding. This happens when the JPEG stream uses multiple scans. */
1125
/* Currently only called in CHUNKY_STRIP_READ_SUPPORT mode through */
1126
/* scanline interface. */
1127
/* Only reads tif->tif_dir.td_bitspersample, tif->tif_rawdata and */
1128
/* tif->tif_rawcc members. */
1129
/* Can be called independently of the usual setup/predecode/decode states */
1130
int TIFFJPEGIsFullStripRequired(TIFF *tif)
1131
232
{
1132
232
    int ret;
1133
232
    JPEGState state;
1134
1135
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1136
232
    if (tif->tif_dir.td_bitspersample == 12)
1137
0
        return TIFFJPEGIsFullStripRequired_12(tif);
1138
232
#endif
1139
1140
232
    memset(&state, 0, sizeof(JPEGState));
1141
232
    state.tif = tif;
1142
1143
232
    TIFFjpeg_create_decompress(&state);
1144
1145
232
    TIFFjpeg_data_src(&state);
1146
1147
232
    if (TIFFjpeg_read_header(&state, TRUE) != JPEG_HEADER_OK)
1148
231
    {
1149
231
        TIFFjpeg_destroy(&state);
1150
231
        return (0);
1151
231
    }
1152
1
    ret = TIFFjpeg_has_multiple_scans(&state);
1153
1154
1
    TIFFjpeg_destroy(&state);
1155
1156
1
    return ret;
1157
232
}
gdal_TIFFJPEGIsFullStripRequired
Line
Count
Source
1131
232
{
1132
232
    int ret;
1133
232
    JPEGState state;
1134
1135
232
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1136
232
    if (tif->tif_dir.td_bitspersample == 12)
1137
0
        return TIFFJPEGIsFullStripRequired_12(tif);
1138
232
#endif
1139
1140
232
    memset(&state, 0, sizeof(JPEGState));
1141
232
    state.tif = tif;
1142
1143
232
    TIFFjpeg_create_decompress(&state);
1144
1145
232
    TIFFjpeg_data_src(&state);
1146
1147
232
    if (TIFFjpeg_read_header(&state, TRUE) != JPEG_HEADER_OK)
1148
231
    {
1149
231
        TIFFjpeg_destroy(&state);
1150
231
        return (0);
1151
231
    }
1152
1
    ret = TIFFjpeg_has_multiple_scans(&state);
1153
1154
1
    TIFFjpeg_destroy(&state);
1155
1156
1
    return ret;
1157
232
}
Unexecuted instantiation: gdal_TIFFJPEGIsFullStripRequired_12
1158
1159
/*
1160
 * Set up for decoding a strip or tile.
1161
 */
1162
/*ARGSUSED*/ static int JPEGPreDecode(TIFF *tif, uint16_t s)
1163
14.6k
{
1164
14.6k
    JPEGState *sp = JState(tif);
1165
14.6k
    TIFFDirectory *td = &tif->tif_dir;
1166
14.6k
    static const char module[] = "JPEGPreDecode";
1167
14.6k
    uint32_t segment_width, segment_height;
1168
14.6k
    int downsampled_output;
1169
14.6k
    int ci;
1170
1171
14.6k
    assert(sp != NULL);
1172
1173
14.6k
    if (sp->cinfo.comm.is_decompressor == 0)
1174
0
    {
1175
0
        tif->tif_setupdecode(tif);
1176
0
    }
1177
1178
14.6k
    assert(sp->cinfo.comm.is_decompressor);
1179
    /*
1180
     * Reset decoder state from any previous strip/tile,
1181
     * in case application didn't read the whole strip.
1182
     */
1183
14.6k
    if (!TIFFjpeg_abort(sp))
1184
0
        return (0);
1185
    /*
1186
     * Read the header for this strip/tile.
1187
     */
1188
1189
14.6k
    if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
1190
11.2k
        return (0);
1191
1192
3.35k
    tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
1193
3.35k
    tif->tif_rawcc = sp->src.bytes_in_buffer;
1194
1195
    /*
1196
     * Check image parameters and set decompression parameters.
1197
     */
1198
3.35k
    if (isTiled(tif))
1199
202
    {
1200
202
        segment_width = td->td_tilewidth;
1201
202
        segment_height = td->td_tilelength;
1202
202
        sp->bytesperline = TIFFTileRowSize(tif);
1203
202
    }
1204
3.15k
    else
1205
3.15k
    {
1206
3.15k
        segment_width = td->td_imagewidth;
1207
3.15k
        segment_height = td->td_imagelength - tif->tif_row;
1208
3.15k
        if (segment_height > td->td_rowsperstrip)
1209
262
            segment_height = td->td_rowsperstrip;
1210
3.15k
        sp->bytesperline = TIFFScanlineSize(tif);
1211
3.15k
    }
1212
3.35k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
1213
128
    {
1214
        /*
1215
         * For PC 2, scale down the expected strip/tile size
1216
         * to match a downsampled component
1217
         */
1218
128
        if (sp->h_sampling == 0 || sp->v_sampling == 0)
1219
0
        {
1220
0
            TIFFErrorExtR(tif, module,
1221
0
                          "JPEG horizontal or vertical sampling is zero");
1222
0
            return (0);
1223
0
        }
1224
128
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
1225
128
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
1226
128
    }
1227
3.35k
    if (sp->cinfo.d.image_width < segment_width ||
1228
3.35k
        sp->cinfo.d.image_height < segment_height)
1229
2.36k
    {
1230
2.36k
        TIFFWarningExtR(tif, module,
1231
2.36k
                        "Improper JPEG strip/tile size, "
1232
2.36k
                        "expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1233
2.36k
                        segment_width, segment_height, sp->cinfo.d.image_width,
1234
2.36k
                        sp->cinfo.d.image_height);
1235
2.36k
    }
1236
3.35k
    if (sp->cinfo.d.image_width == segment_width &&
1237
3.35k
        sp->cinfo.d.image_height > segment_height &&
1238
3.35k
        tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif))
1239
541
    {
1240
        /* Some files have a last strip, that should be truncated, */
1241
        /* but their JPEG codestream has still the maximum strip */
1242
        /* height. Warn about this as this is non compliant, but */
1243
        /* we can safely recover from that. */
1244
541
        TIFFWarningExtR(tif, module,
1245
541
                        "JPEG strip size exceeds expected dimensions,"
1246
541
                        " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1247
541
                        segment_width, segment_height, sp->cinfo.d.image_width,
1248
541
                        sp->cinfo.d.image_height);
1249
541
    }
1250
2.81k
    else if (sp->cinfo.d.image_width > segment_width ||
1251
2.81k
             sp->cinfo.d.image_height > segment_height)
1252
266
    {
1253
        /*
1254
         * This case could be dangerous, if the strip or tile size has
1255
         * been reported as less than the amount of data jpeg will
1256
         * return, some potential security issues arise. Catch this
1257
         * case and error out.
1258
         */
1259
266
        TIFFErrorExtR(tif, module,
1260
266
                      "JPEG strip/tile size exceeds expected dimensions,"
1261
266
                      " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1262
266
                      segment_width, segment_height, sp->cinfo.d.image_width,
1263
266
                      sp->cinfo.d.image_height);
1264
266
        return (0);
1265
266
    }
1266
3.08k
    if (sp->cinfo.d.num_components !=
1267
3.08k
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
1268
3.08k
                                                    : 1))
1269
146
    {
1270
146
        TIFFErrorExtR(tif, module, "Improper JPEG component count");
1271
146
        return (0);
1272
146
    }
1273
#ifdef JPEG_LIB_MK1
1274
    if (12 != td->td_bitspersample && 8 != td->td_bitspersample)
1275
    {
1276
        TIFFErrorExtR(tif, module, "Improper JPEG data precision");
1277
        return (0);
1278
    }
1279
    sp->cinfo.d.data_precision = td->td_bitspersample;
1280
    sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
1281
#else
1282
2.94k
    if (sp->cinfo.d.data_precision != td->td_bitspersample)
1283
14
    {
1284
14
        TIFFErrorExtR(tif, module, "Improper JPEG data precision");
1285
14
        return (0);
1286
14
    }
1287
2.92k
#endif
1288
1289
2.92k
    if (sp->cinfo.d.progressive_mode &&
1290
2.92k
        !sp->otherSettings.has_warned_about_progressive_mode)
1291
1.76k
    {
1292
1.76k
        TIFFWarningExtR(tif, module,
1293
1.76k
                        "The JPEG strip/tile is encoded with progressive mode, "
1294
1.76k
                        "which is normally not legal for JPEG-in-TIFF.\n"
1295
1.76k
                        "libtiff should be able to decode it, but it might "
1296
1.76k
                        "cause compatibility issues with other readers");
1297
1.76k
        sp->otherSettings.has_warned_about_progressive_mode = TRUE;
1298
1.76k
    }
1299
1300
    /* In some cases, libjpeg needs to allocate a lot of memory */
1301
    /* http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf
1302
     */
1303
2.92k
    if (TIFFjpeg_has_multiple_scans(sp))
1304
2.29k
    {
1305
        /* In this case libjpeg will need to allocate memory or backing */
1306
        /* store for all coefficients */
1307
        /* See call to jinit_d_coef_controller() from master_selection() */
1308
        /* in libjpeg */
1309
1310
        /* 1 MB for regular libjpeg usage */
1311
2.29k
        toff_t nRequiredMemory = 1024 * 1024;
1312
1313
6.14k
        for (ci = 0; ci < sp->cinfo.d.num_components; ci++)
1314
3.84k
        {
1315
3.84k
            const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]);
1316
3.84k
            if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0)
1317
3.84k
            {
1318
3.84k
                nRequiredMemory +=
1319
3.84k
                    (toff_t)(((compptr->width_in_blocks +
1320
3.84k
                               compptr->h_samp_factor - 1) /
1321
3.84k
                              compptr->h_samp_factor)) *
1322
3.84k
                    ((compptr->height_in_blocks + compptr->v_samp_factor - 1) /
1323
3.84k
                     compptr->v_samp_factor) *
1324
3.84k
                    sizeof(JBLOCK);
1325
3.84k
            }
1326
3.84k
        }
1327
1328
2.29k
        if (sp->cinfo.d.mem->max_memory_to_use > 0 &&
1329
2.29k
            nRequiredMemory > (toff_t)(sp->cinfo.d.mem->max_memory_to_use) &&
1330
2.29k
            getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL)
1331
0
        {
1332
0
            TIFFErrorExtR(
1333
0
                tif, module,
1334
0
                "Reading this image would require libjpeg to allocate "
1335
0
                "at least %" PRIu64 " bytes. "
1336
0
                "This is disabled since above the %ld threshold. "
1337
0
                "You may override this restriction by defining the "
1338
0
                "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, "
1339
0
                "or setting the JPEGMEM environment variable to a value "
1340
0
                "greater "
1341
0
                "or equal to '%" PRIu64 "M'",
1342
0
                nRequiredMemory, sp->cinfo.d.mem->max_memory_to_use,
1343
0
                (nRequiredMemory + 1000000u - 1u) / 1000000u);
1344
0
            return 0;
1345
0
        }
1346
2.29k
    }
1347
1348
2.92k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1349
2.75k
    {
1350
        /* Component 0 should have expected sampling factors */
1351
2.75k
        if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
1352
2.75k
            sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling)
1353
36
        {
1354
36
            TIFFErrorExtR(tif, module,
1355
36
                          "Improper JPEG sampling factors %d,%d\n"
1356
36
                          "Apparently should be %" PRIu16 ",%" PRIu16 ".",
1357
36
                          sp->cinfo.d.comp_info[0].h_samp_factor,
1358
36
                          sp->cinfo.d.comp_info[0].v_samp_factor,
1359
36
                          sp->h_sampling, sp->v_sampling);
1360
36
            return (0);
1361
36
        }
1362
        /* Rest should have sampling factors 1,1 */
1363
4.77k
        for (ci = 1; ci < sp->cinfo.d.num_components; ci++)
1364
2.07k
        {
1365
2.07k
            if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
1366
2.07k
                sp->cinfo.d.comp_info[ci].v_samp_factor != 1)
1367
28
            {
1368
28
                TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1369
28
                return (0);
1370
28
            }
1371
2.07k
        }
1372
2.72k
    }
1373
167
    else
1374
167
    {
1375
        /* PC 2's single component should have sampling factors 1,1 */
1376
167
        if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
1377
167
            sp->cinfo.d.comp_info[0].v_samp_factor != 1)
1378
3
        {
1379
3
            TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1380
3
            return (0);
1381
3
        }
1382
167
    }
1383
2.85k
    downsampled_output = FALSE;
1384
2.85k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1385
2.85k
        sp->photometric == PHOTOMETRIC_YCBCR &&
1386
2.85k
        sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1387
843
    {
1388
        /* Convert YCbCr to RGB */
1389
843
        sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
1390
843
        sp->cinfo.d.out_color_space = JCS_RGB;
1391
843
    }
1392
2.01k
    else
1393
2.01k
    {
1394
        /* Suppress colorspace handling */
1395
2.01k
        sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
1396
2.01k
        sp->cinfo.d.out_color_space = JCS_UNKNOWN;
1397
2.01k
        if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1398
2.01k
            (sp->h_sampling != 1 || sp->v_sampling != 1))
1399
0
            downsampled_output = TRUE;
1400
        /* XXX what about up-sampling? */
1401
2.01k
    }
1402
2.85k
    if (downsampled_output)
1403
0
    {
1404
        /* Need to use raw-data interface to libjpeg */
1405
0
        sp->cinfo.d.raw_data_out = TRUE;
1406
#if JPEG_LIB_VERSION >= 70
1407
0
        sp->cinfo.d.do_fancy_upsampling = FALSE;
1408
#endif /* JPEG_LIB_VERSION >= 70 */
1409
0
        tif->tif_decoderow = DecodeRowError;
1410
0
        tif->tif_decodestrip = JPEGDecodeRaw;
1411
0
        tif->tif_decodetile = JPEGDecodeRaw;
1412
0
    }
1413
2.85k
    else
1414
2.85k
    {
1415
        /* Use normal interface to libjpeg */
1416
2.85k
        sp->cinfo.d.raw_data_out = FALSE;
1417
2.85k
        tif->tif_decoderow = JPEGDecode;
1418
2.85k
        tif->tif_decodestrip = JPEGDecode;
1419
2.85k
        tif->tif_decodetile = JPEGDecode;
1420
2.85k
    }
1421
    /* Start JPEG decompressor */
1422
2.85k
    if (!TIFFjpeg_start_decompress(sp))
1423
1.06k
        return (0);
1424
    /* Allocate downsampled-data buffers if needed */
1425
1.79k
    if (downsampled_output)
1426
0
    {
1427
0
        if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info,
1428
0
                                       sp->cinfo.d.num_components))
1429
0
            return (0);
1430
0
        sp->scancount = DCTSIZE; /* mark buffer empty */
1431
0
    }
1432
1.79k
    return (1);
1433
1.79k
}
tif_jpeg.c:JPEGPreDecode
Line
Count
Source
1163
3.72k
{
1164
3.72k
    JPEGState *sp = JState(tif);
1165
3.72k
    TIFFDirectory *td = &tif->tif_dir;
1166
3.72k
    static const char module[] = "JPEGPreDecode";
1167
3.72k
    uint32_t segment_width, segment_height;
1168
3.72k
    int downsampled_output;
1169
3.72k
    int ci;
1170
1171
3.72k
    assert(sp != NULL);
1172
1173
3.72k
    if (sp->cinfo.comm.is_decompressor == 0)
1174
0
    {
1175
0
        tif->tif_setupdecode(tif);
1176
0
    }
1177
1178
3.72k
    assert(sp->cinfo.comm.is_decompressor);
1179
    /*
1180
     * Reset decoder state from any previous strip/tile,
1181
     * in case application didn't read the whole strip.
1182
     */
1183
3.72k
    if (!TIFFjpeg_abort(sp))
1184
0
        return (0);
1185
    /*
1186
     * Read the header for this strip/tile.
1187
     */
1188
1189
3.72k
    if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
1190
3.13k
        return (0);
1191
1192
586
    tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
1193
586
    tif->tif_rawcc = sp->src.bytes_in_buffer;
1194
1195
    /*
1196
     * Check image parameters and set decompression parameters.
1197
     */
1198
586
    if (isTiled(tif))
1199
159
    {
1200
159
        segment_width = td->td_tilewidth;
1201
159
        segment_height = td->td_tilelength;
1202
159
        sp->bytesperline = TIFFTileRowSize(tif);
1203
159
    }
1204
427
    else
1205
427
    {
1206
427
        segment_width = td->td_imagewidth;
1207
427
        segment_height = td->td_imagelength - tif->tif_row;
1208
427
        if (segment_height > td->td_rowsperstrip)
1209
239
            segment_height = td->td_rowsperstrip;
1210
427
        sp->bytesperline = TIFFScanlineSize(tif);
1211
427
    }
1212
586
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
1213
125
    {
1214
        /*
1215
         * For PC 2, scale down the expected strip/tile size
1216
         * to match a downsampled component
1217
         */
1218
125
        if (sp->h_sampling == 0 || sp->v_sampling == 0)
1219
0
        {
1220
0
            TIFFErrorExtR(tif, module,
1221
0
                          "JPEG horizontal or vertical sampling is zero");
1222
0
            return (0);
1223
0
        }
1224
125
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
1225
125
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
1226
125
    }
1227
586
    if (sp->cinfo.d.image_width < segment_width ||
1228
586
        sp->cinfo.d.image_height < segment_height)
1229
179
    {
1230
179
        TIFFWarningExtR(tif, module,
1231
179
                        "Improper JPEG strip/tile size, "
1232
179
                        "expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1233
179
                        segment_width, segment_height, sp->cinfo.d.image_width,
1234
179
                        sp->cinfo.d.image_height);
1235
179
    }
1236
586
    if (sp->cinfo.d.image_width == segment_width &&
1237
586
        sp->cinfo.d.image_height > segment_height &&
1238
586
        tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif))
1239
44
    {
1240
        /* Some files have a last strip, that should be truncated, */
1241
        /* but their JPEG codestream has still the maximum strip */
1242
        /* height. Warn about this as this is non compliant, but */
1243
        /* we can safely recover from that. */
1244
44
        TIFFWarningExtR(tif, module,
1245
44
                        "JPEG strip size exceeds expected dimensions,"
1246
44
                        " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1247
44
                        segment_width, segment_height, sp->cinfo.d.image_width,
1248
44
                        sp->cinfo.d.image_height);
1249
44
    }
1250
542
    else if (sp->cinfo.d.image_width > segment_width ||
1251
542
             sp->cinfo.d.image_height > segment_height)
1252
89
    {
1253
        /*
1254
         * This case could be dangerous, if the strip or tile size has
1255
         * been reported as less than the amount of data jpeg will
1256
         * return, some potential security issues arise. Catch this
1257
         * case and error out.
1258
         */
1259
89
        TIFFErrorExtR(tif, module,
1260
89
                      "JPEG strip/tile size exceeds expected dimensions,"
1261
89
                      " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1262
89
                      segment_width, segment_height, sp->cinfo.d.image_width,
1263
89
                      sp->cinfo.d.image_height);
1264
89
        return (0);
1265
89
    }
1266
497
    if (sp->cinfo.d.num_components !=
1267
497
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
1268
497
                                                    : 1))
1269
69
    {
1270
69
        TIFFErrorExtR(tif, module, "Improper JPEG component count");
1271
69
        return (0);
1272
69
    }
1273
#ifdef JPEG_LIB_MK1
1274
    if (12 != td->td_bitspersample && 8 != td->td_bitspersample)
1275
    {
1276
        TIFFErrorExtR(tif, module, "Improper JPEG data precision");
1277
        return (0);
1278
    }
1279
    sp->cinfo.d.data_precision = td->td_bitspersample;
1280
    sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
1281
#else
1282
428
    if (sp->cinfo.d.data_precision != td->td_bitspersample)
1283
14
    {
1284
14
        TIFFErrorExtR(tif, module, "Improper JPEG data precision");
1285
14
        return (0);
1286
14
    }
1287
414
#endif
1288
1289
414
    if (sp->cinfo.d.progressive_mode &&
1290
414
        !sp->otherSettings.has_warned_about_progressive_mode)
1291
21
    {
1292
21
        TIFFWarningExtR(tif, module,
1293
21
                        "The JPEG strip/tile is encoded with progressive mode, "
1294
21
                        "which is normally not legal for JPEG-in-TIFF.\n"
1295
21
                        "libtiff should be able to decode it, but it might "
1296
21
                        "cause compatibility issues with other readers");
1297
21
        sp->otherSettings.has_warned_about_progressive_mode = TRUE;
1298
21
    }
1299
1300
    /* In some cases, libjpeg needs to allocate a lot of memory */
1301
    /* http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf
1302
     */
1303
414
    if (TIFFjpeg_has_multiple_scans(sp))
1304
71
    {
1305
        /* In this case libjpeg will need to allocate memory or backing */
1306
        /* store for all coefficients */
1307
        /* See call to jinit_d_coef_controller() from master_selection() */
1308
        /* in libjpeg */
1309
1310
        /* 1 MB for regular libjpeg usage */
1311
71
        toff_t nRequiredMemory = 1024 * 1024;
1312
1313
309
        for (ci = 0; ci < sp->cinfo.d.num_components; ci++)
1314
238
        {
1315
238
            const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]);
1316
238
            if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0)
1317
238
            {
1318
238
                nRequiredMemory +=
1319
238
                    (toff_t)(((compptr->width_in_blocks +
1320
238
                               compptr->h_samp_factor - 1) /
1321
238
                              compptr->h_samp_factor)) *
1322
238
                    ((compptr->height_in_blocks + compptr->v_samp_factor - 1) /
1323
238
                     compptr->v_samp_factor) *
1324
238
                    sizeof(JBLOCK);
1325
238
            }
1326
238
        }
1327
1328
71
        if (sp->cinfo.d.mem->max_memory_to_use > 0 &&
1329
71
            nRequiredMemory > (toff_t)(sp->cinfo.d.mem->max_memory_to_use) &&
1330
71
            getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL)
1331
0
        {
1332
0
            TIFFErrorExtR(
1333
0
                tif, module,
1334
0
                "Reading this image would require libjpeg to allocate "
1335
0
                "at least %" PRIu64 " bytes. "
1336
0
                "This is disabled since above the %ld threshold. "
1337
0
                "You may override this restriction by defining the "
1338
0
                "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, "
1339
0
                "or setting the JPEGMEM environment variable to a value "
1340
0
                "greater "
1341
0
                "or equal to '%" PRIu64 "M'",
1342
0
                nRequiredMemory, sp->cinfo.d.mem->max_memory_to_use,
1343
0
                (nRequiredMemory + 1000000u - 1u) / 1000000u);
1344
0
            return 0;
1345
0
        }
1346
71
    }
1347
1348
414
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1349
255
    {
1350
        /* Component 0 should have expected sampling factors */
1351
255
        if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
1352
255
            sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling)
1353
15
        {
1354
15
            TIFFErrorExtR(tif, module,
1355
15
                          "Improper JPEG sampling factors %d,%d\n"
1356
15
                          "Apparently should be %" PRIu16 ",%" PRIu16 ".",
1357
15
                          sp->cinfo.d.comp_info[0].h_samp_factor,
1358
15
                          sp->cinfo.d.comp_info[0].v_samp_factor,
1359
15
                          sp->h_sampling, sp->v_sampling);
1360
15
            return (0);
1361
15
        }
1362
        /* Rest should have sampling factors 1,1 */
1363
748
        for (ci = 1; ci < sp->cinfo.d.num_components; ci++)
1364
519
        {
1365
519
            if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
1366
519
                sp->cinfo.d.comp_info[ci].v_samp_factor != 1)
1367
11
            {
1368
11
                TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1369
11
                return (0);
1370
11
            }
1371
519
        }
1372
240
    }
1373
159
    else
1374
159
    {
1375
        /* PC 2's single component should have sampling factors 1,1 */
1376
159
        if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
1377
159
            sp->cinfo.d.comp_info[0].v_samp_factor != 1)
1378
2
        {
1379
2
            TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1380
2
            return (0);
1381
2
        }
1382
159
    }
1383
386
    downsampled_output = FALSE;
1384
386
    if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1385
386
        sp->photometric == PHOTOMETRIC_YCBCR &&
1386
386
        sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1387
120
    {
1388
        /* Convert YCbCr to RGB */
1389
120
        sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
1390
120
        sp->cinfo.d.out_color_space = JCS_RGB;
1391
120
    }
1392
266
    else
1393
266
    {
1394
        /* Suppress colorspace handling */
1395
266
        sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
1396
266
        sp->cinfo.d.out_color_space = JCS_UNKNOWN;
1397
266
        if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1398
266
            (sp->h_sampling != 1 || sp->v_sampling != 1))
1399
0
            downsampled_output = TRUE;
1400
        /* XXX what about up-sampling? */
1401
266
    }
1402
386
    if (downsampled_output)
1403
0
    {
1404
        /* Need to use raw-data interface to libjpeg */
1405
0
        sp->cinfo.d.raw_data_out = TRUE;
1406
0
#if JPEG_LIB_VERSION >= 70
1407
0
        sp->cinfo.d.do_fancy_upsampling = FALSE;
1408
0
#endif /* JPEG_LIB_VERSION >= 70 */
1409
0
        tif->tif_decoderow = DecodeRowError;
1410
0
        tif->tif_decodestrip = JPEGDecodeRaw;
1411
0
        tif->tif_decodetile = JPEGDecodeRaw;
1412
0
    }
1413
386
    else
1414
386
    {
1415
        /* Use normal interface to libjpeg */
1416
386
        sp->cinfo.d.raw_data_out = FALSE;
1417
386
        tif->tif_decoderow = JPEGDecode;
1418
386
        tif->tif_decodestrip = JPEGDecode;
1419
386
        tif->tif_decodetile = JPEGDecode;
1420
386
    }
1421
    /* Start JPEG decompressor */
1422
386
    if (!TIFFjpeg_start_decompress(sp))
1423
54
        return (0);
1424
    /* Allocate downsampled-data buffers if needed */
1425
332
    if (downsampled_output)
1426
0
    {
1427
0
        if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info,
1428
0
                                       sp->cinfo.d.num_components))
1429
0
            return (0);
1430
0
        sp->scancount = DCTSIZE; /* mark buffer empty */
1431
0
    }
1432
332
    return (1);
1433
332
}
tif_jpeg_12.c:JPEGPreDecode
Line
Count
Source
1163
10.9k
{
1164
10.9k
    JPEGState *sp = JState(tif);
1165
10.9k
    TIFFDirectory *td = &tif->tif_dir;
1166
10.9k
    static const char module[] = "JPEGPreDecode";
1167
10.9k
    uint32_t segment_width, segment_height;
1168
10.9k
    int downsampled_output;
1169
10.9k
    int ci;
1170
1171
10.9k
    assert(sp != NULL);
1172
1173
10.9k
    if (sp->cinfo.comm.is_decompressor == 0)
1174
0
    {
1175
0
        tif->tif_setupdecode(tif);
1176
0
    }
1177
1178
10.9k
    assert(sp->cinfo.comm.is_decompressor);
1179
    /*
1180
     * Reset decoder state from any previous strip/tile,
1181
     * in case application didn't read the whole strip.
1182
     */
1183
10.9k
    if (!TIFFjpeg_abort(sp))
1184
0
        return (0);
1185
    /*
1186
     * Read the header for this strip/tile.
1187
     */
1188
1189
10.9k
    if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
1190
8.16k
        return (0);
1191
1192
2.76k
    tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
1193
2.76k
    tif->tif_rawcc = sp->src.bytes_in_buffer;
1194
1195
    /*
1196
     * Check image parameters and set decompression parameters.
1197
     */
1198
2.76k
    if (isTiled(tif))
1199
43
    {
1200
43
        segment_width = td->td_tilewidth;
1201
43
        segment_height = td->td_tilelength;
1202
43
        sp->bytesperline = TIFFTileRowSize(tif);
1203
43
    }
1204
2.72k
    else
1205
2.72k
    {
1206
2.72k
        segment_width = td->td_imagewidth;
1207
2.72k
        segment_height = td->td_imagelength - tif->tif_row;
1208
2.72k
        if (segment_height > td->td_rowsperstrip)
1209
23
            segment_height = td->td_rowsperstrip;
1210
2.72k
        sp->bytesperline = TIFFScanlineSize(tif);
1211
2.72k
    }
1212
2.76k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
1213
3
    {
1214
        /*
1215
         * For PC 2, scale down the expected strip/tile size
1216
         * to match a downsampled component
1217
         */
1218
3
        if (sp->h_sampling == 0 || sp->v_sampling == 0)
1219
0
        {
1220
0
            TIFFErrorExtR(tif, module,
1221
0
                          "JPEG horizontal or vertical sampling is zero");
1222
0
            return (0);
1223
0
        }
1224
3
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
1225
3
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
1226
3
    }
1227
2.76k
    if (sp->cinfo.d.image_width < segment_width ||
1228
2.76k
        sp->cinfo.d.image_height < segment_height)
1229
2.18k
    {
1230
2.18k
        TIFFWarningExtR(tif, module,
1231
2.18k
                        "Improper JPEG strip/tile size, "
1232
2.18k
                        "expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1233
2.18k
                        segment_width, segment_height, sp->cinfo.d.image_width,
1234
2.18k
                        sp->cinfo.d.image_height);
1235
2.18k
    }
1236
2.76k
    if (sp->cinfo.d.image_width == segment_width &&
1237
2.76k
        sp->cinfo.d.image_height > segment_height &&
1238
2.76k
        tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif))
1239
497
    {
1240
        /* Some files have a last strip, that should be truncated, */
1241
        /* but their JPEG codestream has still the maximum strip */
1242
        /* height. Warn about this as this is non compliant, but */
1243
        /* we can safely recover from that. */
1244
497
        TIFFWarningExtR(tif, module,
1245
497
                        "JPEG strip size exceeds expected dimensions,"
1246
497
                        " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1247
497
                        segment_width, segment_height, sp->cinfo.d.image_width,
1248
497
                        sp->cinfo.d.image_height);
1249
497
    }
1250
2.26k
    else if (sp->cinfo.d.image_width > segment_width ||
1251
2.26k
             sp->cinfo.d.image_height > segment_height)
1252
177
    {
1253
        /*
1254
         * This case could be dangerous, if the strip or tile size has
1255
         * been reported as less than the amount of data jpeg will
1256
         * return, some potential security issues arise. Catch this
1257
         * case and error out.
1258
         */
1259
177
        TIFFErrorExtR(tif, module,
1260
177
                      "JPEG strip/tile size exceeds expected dimensions,"
1261
177
                      " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1262
177
                      segment_width, segment_height, sp->cinfo.d.image_width,
1263
177
                      sp->cinfo.d.image_height);
1264
177
        return (0);
1265
177
    }
1266
2.58k
    if (sp->cinfo.d.num_components !=
1267
2.58k
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
1268
2.58k
                                                    : 1))
1269
77
    {
1270
77
        TIFFErrorExtR(tif, module, "Improper JPEG component count");
1271
77
        return (0);
1272
77
    }
1273
#ifdef JPEG_LIB_MK1
1274
    if (12 != td->td_bitspersample && 8 != td->td_bitspersample)
1275
    {
1276
        TIFFErrorExtR(tif, module, "Improper JPEG data precision");
1277
        return (0);
1278
    }
1279
    sp->cinfo.d.data_precision = td->td_bitspersample;
1280
    sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
1281
#else
1282
2.51k
    if (sp->cinfo.d.data_precision != td->td_bitspersample)
1283
0
    {
1284
0
        TIFFErrorExtR(tif, module, "Improper JPEG data precision");
1285
0
        return (0);
1286
0
    }
1287
2.51k
#endif
1288
1289
2.51k
    if (sp->cinfo.d.progressive_mode &&
1290
2.51k
        !sp->otherSettings.has_warned_about_progressive_mode)
1291
1.74k
    {
1292
1.74k
        TIFFWarningExtR(tif, module,
1293
1.74k
                        "The JPEG strip/tile is encoded with progressive mode, "
1294
1.74k
                        "which is normally not legal for JPEG-in-TIFF.\n"
1295
1.74k
                        "libtiff should be able to decode it, but it might "
1296
1.74k
                        "cause compatibility issues with other readers");
1297
1.74k
        sp->otherSettings.has_warned_about_progressive_mode = TRUE;
1298
1.74k
    }
1299
1300
    /* In some cases, libjpeg needs to allocate a lot of memory */
1301
    /* http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf
1302
     */
1303
2.51k
    if (TIFFjpeg_has_multiple_scans(sp))
1304
2.22k
    {
1305
        /* In this case libjpeg will need to allocate memory or backing */
1306
        /* store for all coefficients */
1307
        /* See call to jinit_d_coef_controller() from master_selection() */
1308
        /* in libjpeg */
1309
1310
        /* 1 MB for regular libjpeg usage */
1311
2.22k
        toff_t nRequiredMemory = 1024 * 1024;
1312
1313
5.83k
        for (ci = 0; ci < sp->cinfo.d.num_components; ci++)
1314
3.60k
        {
1315
3.60k
            const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]);
1316
3.60k
            if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0)
1317
3.60k
            {
1318
3.60k
                nRequiredMemory +=
1319
3.60k
                    (toff_t)(((compptr->width_in_blocks +
1320
3.60k
                               compptr->h_samp_factor - 1) /
1321
3.60k
                              compptr->h_samp_factor)) *
1322
3.60k
                    ((compptr->height_in_blocks + compptr->v_samp_factor - 1) /
1323
3.60k
                     compptr->v_samp_factor) *
1324
3.60k
                    sizeof(JBLOCK);
1325
3.60k
            }
1326
3.60k
        }
1327
1328
2.22k
        if (sp->cinfo.d.mem->max_memory_to_use > 0 &&
1329
2.22k
            nRequiredMemory > (toff_t)(sp->cinfo.d.mem->max_memory_to_use) &&
1330
2.22k
            getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL)
1331
0
        {
1332
0
            TIFFErrorExtR(
1333
0
                tif, module,
1334
0
                "Reading this image would require libjpeg to allocate "
1335
0
                "at least %" PRIu64 " bytes. "
1336
0
                "This is disabled since above the %ld threshold. "
1337
0
                "You may override this restriction by defining the "
1338
0
                "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, "
1339
0
                "or setting the JPEGMEM environment variable to a value "
1340
0
                "greater "
1341
0
                "or equal to '%" PRIu64 "M'",
1342
0
                nRequiredMemory, sp->cinfo.d.mem->max_memory_to_use,
1343
0
                (nRequiredMemory + 1000000u - 1u) / 1000000u);
1344
0
            return 0;
1345
0
        }
1346
2.22k
    }
1347
1348
2.51k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1349
2.50k
    {
1350
        /* Component 0 should have expected sampling factors */
1351
2.50k
        if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
1352
2.50k
            sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling)
1353
21
        {
1354
21
            TIFFErrorExtR(tif, module,
1355
21
                          "Improper JPEG sampling factors %d,%d\n"
1356
21
                          "Apparently should be %" PRIu16 ",%" PRIu16 ".",
1357
21
                          sp->cinfo.d.comp_info[0].h_samp_factor,
1358
21
                          sp->cinfo.d.comp_info[0].v_samp_factor,
1359
21
                          sp->h_sampling, sp->v_sampling);
1360
21
            return (0);
1361
21
        }
1362
        /* Rest should have sampling factors 1,1 */
1363
4.02k
        for (ci = 1; ci < sp->cinfo.d.num_components; ci++)
1364
1.55k
        {
1365
1.55k
            if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
1366
1.55k
                sp->cinfo.d.comp_info[ci].v_samp_factor != 1)
1367
17
            {
1368
17
                TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1369
17
                return (0);
1370
17
            }
1371
1.55k
        }
1372
2.48k
    }
1373
8
    else
1374
8
    {
1375
        /* PC 2's single component should have sampling factors 1,1 */
1376
8
        if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
1377
8
            sp->cinfo.d.comp_info[0].v_samp_factor != 1)
1378
1
        {
1379
1
            TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1380
1
            return (0);
1381
1
        }
1382
8
    }
1383
2.47k
    downsampled_output = FALSE;
1384
2.47k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1385
2.47k
        sp->photometric == PHOTOMETRIC_YCBCR &&
1386
2.47k
        sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1387
723
    {
1388
        /* Convert YCbCr to RGB */
1389
723
        sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
1390
723
        sp->cinfo.d.out_color_space = JCS_RGB;
1391
723
    }
1392
1.75k
    else
1393
1.75k
    {
1394
        /* Suppress colorspace handling */
1395
1.75k
        sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
1396
1.75k
        sp->cinfo.d.out_color_space = JCS_UNKNOWN;
1397
1.75k
        if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1398
1.75k
            (sp->h_sampling != 1 || sp->v_sampling != 1))
1399
0
            downsampled_output = TRUE;
1400
        /* XXX what about up-sampling? */
1401
1.75k
    }
1402
2.47k
    if (downsampled_output)
1403
0
    {
1404
        /* Need to use raw-data interface to libjpeg */
1405
0
        sp->cinfo.d.raw_data_out = TRUE;
1406
#if JPEG_LIB_VERSION >= 70
1407
        sp->cinfo.d.do_fancy_upsampling = FALSE;
1408
#endif /* JPEG_LIB_VERSION >= 70 */
1409
0
        tif->tif_decoderow = DecodeRowError;
1410
0
        tif->tif_decodestrip = JPEGDecodeRaw;
1411
0
        tif->tif_decodetile = JPEGDecodeRaw;
1412
0
    }
1413
2.47k
    else
1414
2.47k
    {
1415
        /* Use normal interface to libjpeg */
1416
2.47k
        sp->cinfo.d.raw_data_out = FALSE;
1417
2.47k
        tif->tif_decoderow = JPEGDecode;
1418
2.47k
        tif->tif_decodestrip = JPEGDecode;
1419
2.47k
        tif->tif_decodetile = JPEGDecode;
1420
2.47k
    }
1421
    /* Start JPEG decompressor */
1422
2.47k
    if (!TIFFjpeg_start_decompress(sp))
1423
1.00k
        return (0);
1424
    /* Allocate downsampled-data buffers if needed */
1425
1.46k
    if (downsampled_output)
1426
0
    {
1427
0
        if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info,
1428
0
                                       sp->cinfo.d.num_components))
1429
0
            return (0);
1430
0
        sp->scancount = DCTSIZE; /* mark buffer empty */
1431
0
    }
1432
1.46k
    return (1);
1433
1.46k
}
1434
1435
/*
1436
 * Decode a chunk of pixels.
1437
 * "Standard" case: returned data is not downsampled.
1438
 */
1439
#if !JPEG_LIB_MK1_OR_12BIT
1440
static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
1441
419
{
1442
419
    JPEGState *sp = JState(tif);
1443
419
    tmsize_t nrows;
1444
419
    (void)s;
1445
1446
    /*
1447
    ** Update available information, buffer may have been refilled
1448
    ** between decode requests
1449
    */
1450
419
    sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp;
1451
419
    sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
1452
1453
419
    if (sp->bytesperline == 0)
1454
0
    {
1455
0
        memset(buf, 0, (size_t)cc);
1456
0
        return 0;
1457
0
    }
1458
1459
419
    nrows = cc / sp->bytesperline;
1460
419
    if (cc % sp->bytesperline)
1461
0
        TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read");
1462
1463
419
    if (nrows > (tmsize_t)sp->cinfo.d.image_height)
1464
38
        nrows = sp->cinfo.d.image_height;
1465
1466
    /* data is expected to be read in multiples of a scanline */
1467
419
    if (nrows)
1468
419
    {
1469
419
        do
1470
17.8k
        {
1471
            /*
1472
             * In the libjpeg6b-9a 8bit case.  We read directly into
1473
             * the TIFF buffer.
1474
             */
1475
17.8k
            JSAMPROW bufptr = (JSAMPROW)buf;
1476
1477
17.8k
            if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1)
1478
2
            {
1479
2
                memset(buf, 0, (size_t)cc);
1480
2
                return (0);
1481
2
            }
1482
1483
17.8k
            ++tif->tif_row;
1484
17.8k
            buf += sp->bytesperline;
1485
17.8k
            cc -= sp->bytesperline;
1486
17.8k
        } while (--nrows > 0);
1487
419
    }
1488
1489
    /* Update information on consumed data */
1490
417
    tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
1491
417
    tif->tif_rawcc = sp->src.bytes_in_buffer;
1492
1493
    /* Close down the decompressor if we've finished the strip or tile. */
1494
417
    return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
1495
417
           TIFFjpeg_finish_decompress(sp);
1496
419
}
1497
#endif /* !JPEG_LIB_MK1_OR_12BIT */
1498
1499
#if JPEG_LIB_MK1_OR_12BIT
1500
/*ARGSUSED*/ static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc,
1501
                                   uint16_t s)
1502
1.46k
{
1503
1.46k
    JPEGState *sp = JState(tif);
1504
1.46k
    tmsize_t nrows;
1505
1.46k
    (void)s;
1506
1507
    /*
1508
    ** Update available information, buffer may have been refilled
1509
    ** between decode requests
1510
    */
1511
1.46k
    sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp;
1512
1.46k
    sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
1513
1514
1.46k
    if (sp->bytesperline == 0)
1515
0
    {
1516
0
        memset(buf, 0, (size_t)cc);
1517
0
        return 0;
1518
0
    }
1519
1520
1.46k
    nrows = cc / sp->bytesperline;
1521
1.46k
    if (cc % sp->bytesperline)
1522
0
        TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read");
1523
1524
1.46k
    if (nrows > (tmsize_t)sp->cinfo.d.image_height)
1525
1.16k
        nrows = sp->cinfo.d.image_height;
1526
1527
    /* data is expected to be read in multiples of a scanline */
1528
1.46k
    if (nrows)
1529
1.46k
    {
1530
1.46k
        TIFF_JSAMPROW line_work_buf = NULL;
1531
1532
        /*
1533
         * For 6B, only use temporary buffer for 12 bit imagery.
1534
         * For Mk1 always use it.
1535
         */
1536
1.46k
        if (sp->cinfo.d.data_precision == 12)
1537
1.46k
        {
1538
1.46k
            line_work_buf = (TIFF_JSAMPROW)_TIFFmallocExt(
1539
1.46k
                tif, sizeof(short) * sp->cinfo.d.output_width *
1540
1.46k
                         sp->cinfo.d.num_components);
1541
1.46k
        }
1542
1543
1.46k
        do
1544
5.68M
        {
1545
5.68M
            if (line_work_buf != NULL)
1546
5.68M
            {
1547
                /*
1548
                 * In the MK1 case, we always read into a 16bit
1549
                 * buffer, and then pack down to 12bit or 8bit.
1550
                 * In 6B case we only read into 16 bit buffer
1551
                 * for 12bit data, which we need to repack.
1552
                 */
1553
5.68M
                if (TIFFjpeg_read_scanlines(sp, &line_work_buf, 1) != 1)
1554
0
                {
1555
0
                    memset(buf, 0, (size_t)cc);
1556
0
                    return (0);
1557
0
                }
1558
1559
5.68M
                if (sp->cinfo.d.data_precision == 12)
1560
5.68M
                {
1561
5.68M
                    int value_pairs = (sp->cinfo.d.output_width *
1562
5.68M
                                       sp->cinfo.d.num_components) /
1563
5.68M
                                      2;
1564
5.68M
                    int iPair;
1565
1566
134M
                    for (iPair = 0; iPair < value_pairs; iPair++)
1567
128M
                    {
1568
128M
                        unsigned char *out_ptr =
1569
128M
                            ((unsigned char *)buf) + iPair * 3;
1570
128M
                        TIFF_JSAMPLE *in_ptr = line_work_buf + iPair * 2;
1571
1572
128M
                        out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4);
1573
128M
                        out_ptr[1] =
1574
128M
                            (unsigned char)(((in_ptr[0] & 0xf) << 4) |
1575
128M
                                            ((in_ptr[1] & 0xf00) >> 8));
1576
128M
                        out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0));
1577
128M
                    }
1578
5.68M
                }
1579
0
                else if (sp->cinfo.d.data_precision == 8)
1580
0
                {
1581
0
                    int value_count =
1582
0
                        (sp->cinfo.d.output_width * sp->cinfo.d.num_components);
1583
0
                    int iValue;
1584
1585
0
                    for (iValue = 0; iValue < value_count; iValue++)
1586
0
                    {
1587
0
                        ((unsigned char *)buf)[iValue] =
1588
0
                            line_work_buf[iValue] & 0xff;
1589
0
                    }
1590
0
                }
1591
5.68M
            }
1592
1593
5.68M
            ++tif->tif_row;
1594
5.68M
            buf += sp->bytesperline;
1595
5.68M
            cc -= sp->bytesperline;
1596
5.68M
        } while (--nrows > 0);
1597
1598
1.46k
        if (line_work_buf != NULL)
1599
1.46k
            _TIFFfreeExt(tif, line_work_buf);
1600
1.46k
    }
1601
1602
    /* Update information on consumed data */
1603
1.46k
    tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
1604
1.46k
    tif->tif_rawcc = sp->src.bytes_in_buffer;
1605
1606
    /* Close down the decompressor if we've finished the strip or tile. */
1607
1.46k
    return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
1608
1.46k
           TIFFjpeg_finish_decompress(sp);
1609
1.46k
}
1610
#endif /* JPEG_LIB_MK1_OR_12BIT */
1611
1612
/*ARGSUSED*/ static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc,
1613
                                       uint16_t s)
1614
1615
0
{
1616
0
    (void)buf;
1617
0
    (void)cc;
1618
0
    (void)s;
1619
1620
0
    TIFFErrorExtR(
1621
0
        tif, "TIFFReadScanline",
1622
0
        "scanline oriented access is not supported for downsampled JPEG "
1623
0
        "compressed images, consider enabling TIFFTAG_JPEGCOLORMODE as "
1624
0
        "JPEGCOLORMODE_RGB.");
1625
0
    return 0;
1626
0
}
Unexecuted instantiation: tif_jpeg.c:DecodeRowError
Unexecuted instantiation: tif_jpeg_12.c:DecodeRowError
1627
1628
/*
1629
 * Decode a chunk of pixels.
1630
 * Returned data is downsampled per sampling factors.
1631
 */
1632
/*ARGSUSED*/ static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc,
1633
                                      uint16_t s)
1634
0
{
1635
0
    JPEGState *sp = JState(tif);
1636
0
    tmsize_t nrows;
1637
0
    TIFFDirectory *td = &tif->tif_dir;
1638
0
    (void)s;
1639
1640
0
    nrows = sp->cinfo.d.image_height;
1641
    /* For last strip, limit number of rows to its truncated height */
1642
    /* even if the codestream height is larger (which is not compliant, */
1643
    /* but that we tolerate) */
1644
0
    if ((uint32_t)nrows > td->td_imagelength - tif->tif_row && !isTiled(tif))
1645
0
        nrows = td->td_imagelength - tif->tif_row;
1646
1647
#if defined(JPEG_LIB_MK1_OR_12BIT)
1648
    unsigned short *tmpbuf = NULL;
1649
#endif
1650
1651
    /* data is expected to be read in multiples of a scanline */
1652
0
    if (nrows != 0)
1653
0
    {
1654
1655
        /* Cb,Cr both have sampling factors 1, so this is correct */
1656
0
        JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width;
1657
0
        int samples_per_clump = sp->samplesperclump;
1658
1659
#if defined(JPEG_LIB_MK1_OR_12BIT)
1660
0
        tmpbuf = _TIFFmallocExt(tif, sizeof(unsigned short) *
1661
                                         sp->cinfo.d.output_width *
1662
                                         sp->cinfo.d.num_components);
1663
0
        if (tmpbuf == NULL)
1664
0
        {
1665
0
            TIFFErrorExtR(tif, "JPEGDecodeRaw", "Out of memory");
1666
0
            return 0;
1667
0
        }
1668
0
#endif
1669
1670
0
        do
1671
0
        {
1672
0
            jpeg_component_info *compptr;
1673
0
            int ci, clumpoffset;
1674
1675
0
            if (cc < sp->bytesperline)
1676
0
            {
1677
0
                TIFFErrorExtR(
1678
0
                    tif, "JPEGDecodeRaw",
1679
0
                    "application buffer not large enough for all data.");
1680
0
                goto error;
1681
0
            }
1682
1683
            /* Reload downsampled-data buffer if needed */
1684
0
            if (sp->scancount >= DCTSIZE)
1685
0
            {
1686
0
                int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE;
1687
0
                if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n) != n)
1688
0
                    goto error;
1689
0
                sp->scancount = 0;
1690
0
            }
1691
            /*
1692
             * Fastest way to unseparate data is to make one pass
1693
             * over the scanline for each row of each component.
1694
             */
1695
0
            clumpoffset = 0; /* first sample in clump */
1696
0
            for (ci = 0, compptr = sp->cinfo.d.comp_info;
1697
0
                 ci < sp->cinfo.d.num_components; ci++, compptr++)
1698
0
            {
1699
0
                int hsamp = compptr->h_samp_factor;
1700
0
                int vsamp = compptr->v_samp_factor;
1701
0
                int ypos;
1702
1703
0
                for (ypos = 0; ypos < vsamp; ypos++)
1704
0
                {
1705
0
                    TIFF_JSAMPLE *inptr =
1706
0
                        sp->ds_buffer[ci][sp->scancount * vsamp + ypos];
1707
0
                    JDIMENSION nclump;
1708
#if defined(JPEG_LIB_MK1_OR_12BIT)
1709
0
                    TIFF_JSAMPLE *outptr = (TIFF_JSAMPLE *)tmpbuf + clumpoffset;
1710
#else
1711
0
                    TIFF_JSAMPLE *outptr = (TIFF_JSAMPLE *)buf + clumpoffset;
1712
0
                    if (cc < (tmsize_t)(clumpoffset +
1713
0
                                        (tmsize_t)samples_per_clump *
1714
0
                                            (clumps_per_line - 1) +
1715
0
                                        hsamp))
1716
0
                    {
1717
0
                        TIFFErrorExtR(
1718
0
                            tif, "JPEGDecodeRaw",
1719
0
                            "application buffer not large enough for all data, "
1720
0
                            "possible subsampling issue");
1721
0
                        goto error;
1722
0
                    }
1723
0
#endif
1724
1725
0
                    if (hsamp == 1)
1726
0
                    {
1727
                        /* fast path for at least Cb and Cr */
1728
0
                        for (nclump = clumps_per_line; nclump-- > 0;)
1729
0
                        {
1730
0
                            outptr[0] = *inptr++;
1731
0
                            outptr += samples_per_clump;
1732
0
                        }
1733
0
                    }
1734
0
                    else
1735
0
                    {
1736
0
                        int xpos;
1737
1738
                        /* general case */
1739
0
                        for (nclump = clumps_per_line; nclump-- > 0;)
1740
0
                        {
1741
0
                            for (xpos = 0; xpos < hsamp; xpos++)
1742
0
                                outptr[xpos] = *inptr++;
1743
0
                            outptr += samples_per_clump;
1744
0
                        }
1745
0
                    }
1746
0
                    clumpoffset += hsamp;
1747
0
                }
1748
0
            }
1749
1750
#if defined(JPEG_LIB_MK1_OR_12BIT)
1751
            {
1752
0
                if (sp->cinfo.d.data_precision == 8)
1753
0
                {
1754
0
                    int i = 0;
1755
0
                    int len =
1756
0
                        sp->cinfo.d.output_width * sp->cinfo.d.num_components;
1757
0
                    for (i = 0; i < len; i++)
1758
0
                    {
1759
0
                        ((unsigned char *)buf)[i] = tmpbuf[i] & 0xff;
1760
0
                    }
1761
0
                }
1762
0
                else
1763
0
                { /* 12-bit */
1764
0
                    int value_pairs = (sp->cinfo.d.output_width *
1765
0
                                       sp->cinfo.d.num_components) /
1766
0
                                      2;
1767
0
                    int iPair;
1768
0
                    for (iPair = 0; iPair < value_pairs; iPair++)
1769
0
                    {
1770
0
                        unsigned char *out_ptr =
1771
0
                            ((unsigned char *)buf) + iPair * 3;
1772
0
                        JSAMPLE *in_ptr = (JSAMPLE *)(tmpbuf + iPair * 2);
1773
0
                        out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4);
1774
0
                        out_ptr[1] =
1775
0
                            (unsigned char)(((in_ptr[0] & 0xf) << 4) |
1776
0
                                            ((in_ptr[1] & 0xf00) >> 8));
1777
0
                        out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0));
1778
0
                    }
1779
0
                }
1780
            }
1781
#endif
1782
1783
0
            sp->scancount++;
1784
0
            tif->tif_row += sp->v_sampling;
1785
1786
0
            buf += sp->bytesperline;
1787
0
            cc -= sp->bytesperline;
1788
1789
0
            nrows -= sp->v_sampling;
1790
0
        } while (nrows > 0);
1791
1792
#if defined(JPEG_LIB_MK1_OR_12BIT)
1793
0
        _TIFFfreeExt(tif, tmpbuf);
1794
0
#endif
1795
0
    }
1796
1797
    /* Close down the decompressor if done. */
1798
0
    return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
1799
0
           TIFFjpeg_finish_decompress(sp);
1800
1801
0
error:
1802
#if defined(JPEG_LIB_MK1_OR_12BIT)
1803
0
    _TIFFfreeExt(tif, tmpbuf);
1804
#endif
1805
0
    return 0;
1806
0
}
Unexecuted instantiation: tif_jpeg.c:JPEGDecodeRaw
Unexecuted instantiation: tif_jpeg_12.c:JPEGDecodeRaw
1807
1808
/*
1809
 * JPEG Encoding.
1810
 */
1811
1812
static void unsuppress_quant_table(JPEGState *sp, int tblno)
1813
226k
{
1814
226k
    JQUANT_TBL *qtbl;
1815
1816
226k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1817
226k
        qtbl->sent_table = FALSE;
1818
226k
}
tif_jpeg.c:unsuppress_quant_table
Line
Count
Source
1813
106k
{
1814
106k
    JQUANT_TBL *qtbl;
1815
1816
106k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1817
106k
        qtbl->sent_table = FALSE;
1818
106k
}
tif_jpeg_12.c:unsuppress_quant_table
Line
Count
Source
1813
119k
{
1814
119k
    JQUANT_TBL *qtbl;
1815
1816
119k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1817
119k
        qtbl->sent_table = FALSE;
1818
119k
}
1819
1820
static void suppress_quant_table(JPEGState *sp, int tblno)
1821
856k
{
1822
856k
    JQUANT_TBL *qtbl;
1823
1824
856k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1825
856k
        qtbl->sent_table = TRUE;
1826
856k
}
tif_jpeg.c:suppress_quant_table
Line
Count
Source
1821
422k
{
1822
422k
    JQUANT_TBL *qtbl;
1823
1824
422k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1825
422k
        qtbl->sent_table = TRUE;
1826
422k
}
tif_jpeg_12.c:suppress_quant_table
Line
Count
Source
1821
434k
{
1822
434k
    JQUANT_TBL *qtbl;
1823
1824
434k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1825
434k
        qtbl->sent_table = TRUE;
1826
434k
}
1827
1828
static void unsuppress_huff_table(JPEGState *sp, int tblno)
1829
0
{
1830
0
    JHUFF_TBL *htbl;
1831
1832
0
    if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
1833
0
        htbl->sent_table = FALSE;
1834
0
    if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
1835
0
        htbl->sent_table = FALSE;
1836
0
}
Unexecuted instantiation: tif_jpeg.c:unsuppress_huff_table
Unexecuted instantiation: tif_jpeg_12.c:unsuppress_huff_table
1837
1838
static void suppress_huff_table(JPEGState *sp, int tblno)
1839
428k
{
1840
428k
    JHUFF_TBL *htbl;
1841
1842
428k
    if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
1843
428k
        htbl->sent_table = TRUE;
1844
428k
    if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
1845
428k
        htbl->sent_table = TRUE;
1846
428k
}
tif_jpeg.c:suppress_huff_table
Line
Count
Source
1839
211k
{
1840
211k
    JHUFF_TBL *htbl;
1841
1842
211k
    if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
1843
211k
        htbl->sent_table = TRUE;
1844
211k
    if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
1845
211k
        htbl->sent_table = TRUE;
1846
211k
}
tif_jpeg_12.c:suppress_huff_table
Line
Count
Source
1839
217k
{
1840
217k
    JHUFF_TBL *htbl;
1841
1842
217k
    if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
1843
217k
        htbl->sent_table = TRUE;
1844
217k
    if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
1845
217k
        htbl->sent_table = TRUE;
1846
217k
}
1847
1848
static int prepare_JPEGTables(TIFF *tif)
1849
214k
{
1850
214k
    JPEGState *sp = JState(tif);
1851
1852
    /* Initialize quant tables for current quality setting */
1853
214k
    if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
1854
0
        return (0);
1855
    /* Mark only the tables we want for output */
1856
    /* NB: chrominance tables are currently used only with YCbCr */
1857
214k
    if (!TIFFjpeg_suppress_tables(sp, TRUE))
1858
0
        return (0);
1859
214k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
1860
214k
    {
1861
214k
        unsuppress_quant_table(sp, 0);
1862
214k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1863
12.4k
            unsuppress_quant_table(sp, 1);
1864
214k
    }
1865
214k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
1866
0
    {
1867
0
        unsuppress_huff_table(sp, 0);
1868
0
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1869
0
            unsuppress_huff_table(sp, 1);
1870
0
    }
1871
    /* Direct libjpeg output into otherSettings.jpegtables */
1872
214k
    if (!TIFFjpeg_tables_dest(sp, tif))
1873
0
        return (0);
1874
    /* Emit tables-only datastream */
1875
214k
    if (!TIFFjpeg_write_tables(sp))
1876
0
        return (0);
1877
1878
214k
    return (1);
1879
214k
}
tif_jpeg.c:prepare_JPEGTables
Line
Count
Source
1849
105k
{
1850
105k
    JPEGState *sp = JState(tif);
1851
1852
    /* Initialize quant tables for current quality setting */
1853
105k
    if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
1854
0
        return (0);
1855
    /* Mark only the tables we want for output */
1856
    /* NB: chrominance tables are currently used only with YCbCr */
1857
105k
    if (!TIFFjpeg_suppress_tables(sp, TRUE))
1858
0
        return (0);
1859
105k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
1860
105k
    {
1861
105k
        unsuppress_quant_table(sp, 0);
1862
105k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1863
1.15k
            unsuppress_quant_table(sp, 1);
1864
105k
    }
1865
105k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
1866
0
    {
1867
0
        unsuppress_huff_table(sp, 0);
1868
0
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1869
0
            unsuppress_huff_table(sp, 1);
1870
0
    }
1871
    /* Direct libjpeg output into otherSettings.jpegtables */
1872
105k
    if (!TIFFjpeg_tables_dest(sp, tif))
1873
0
        return (0);
1874
    /* Emit tables-only datastream */
1875
105k
    if (!TIFFjpeg_write_tables(sp))
1876
0
        return (0);
1877
1878
105k
    return (1);
1879
105k
}
tif_jpeg_12.c:prepare_JPEGTables
Line
Count
Source
1849
108k
{
1850
108k
    JPEGState *sp = JState(tif);
1851
1852
    /* Initialize quant tables for current quality setting */
1853
108k
    if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
1854
0
        return (0);
1855
    /* Mark only the tables we want for output */
1856
    /* NB: chrominance tables are currently used only with YCbCr */
1857
108k
    if (!TIFFjpeg_suppress_tables(sp, TRUE))
1858
0
        return (0);
1859
108k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
1860
108k
    {
1861
108k
        unsuppress_quant_table(sp, 0);
1862
108k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1863
11.3k
            unsuppress_quant_table(sp, 1);
1864
108k
    }
1865
108k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
1866
0
    {
1867
0
        unsuppress_huff_table(sp, 0);
1868
0
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1869
0
            unsuppress_huff_table(sp, 1);
1870
0
    }
1871
    /* Direct libjpeg output into otherSettings.jpegtables */
1872
108k
    if (!TIFFjpeg_tables_dest(sp, tif))
1873
0
        return (0);
1874
    /* Emit tables-only datastream */
1875
108k
    if (!TIFFjpeg_write_tables(sp))
1876
0
        return (0);
1877
1878
108k
    return (1);
1879
108k
}
1880
1881
#if defined(JPEG_LIB_VERSION_MAJOR) &&                                         \
1882
    (JPEG_LIB_VERSION_MAJOR > 9 ||                                             \
1883
     (JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4))
1884
/* This is a modified version of std_huff_tables() from jcparam.c
1885
 * in libjpeg-9d because it no longer initializes default Huffman
1886
 * tables in jpeg_set_defaults(). */
1887
static void TIFF_std_huff_tables(j_compress_ptr cinfo)
1888
{
1889
1890
    if (cinfo->dc_huff_tbl_ptrs[0] == NULL)
1891
    {
1892
        (void)jpeg_std_huff_table((j_common_ptr)cinfo, TRUE, 0);
1893
    }
1894
    if (cinfo->ac_huff_tbl_ptrs[0] == NULL)
1895
    {
1896
        (void)jpeg_std_huff_table((j_common_ptr)cinfo, FALSE, 0);
1897
    }
1898
    if (cinfo->dc_huff_tbl_ptrs[1] == NULL)
1899
    {
1900
        (void)jpeg_std_huff_table((j_common_ptr)cinfo, TRUE, 1);
1901
    }
1902
    if (cinfo->ac_huff_tbl_ptrs[1] == NULL)
1903
    {
1904
        (void)jpeg_std_huff_table((j_common_ptr)cinfo, FALSE, 1);
1905
    }
1906
}
1907
#endif
1908
1909
static int JPEGSetupEncode(TIFF *tif)
1910
849k
{
1911
849k
    JPEGState *sp = JState(tif);
1912
849k
    TIFFDirectory *td = &tif->tif_dir;
1913
849k
    static const char module[] = "JPEGSetupEncode";
1914
1915
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1916
632k
    if (tif->tif_dir.td_bitspersample == 12)
1917
217k
    {
1918
        /* We pass a pointer to a copy of otherSettings, since */
1919
        /* TIFFReInitJPEG_12() will clear sp */
1920
217k
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1921
217k
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 1);
1922
217k
    }
1923
415k
#endif
1924
1925
632k
    JPEGInitializeLibJPEG(tif, FALSE);
1926
1927
415k
    assert(sp != NULL);
1928
632k
    assert(!sp->cinfo.comm.is_decompressor);
1929
1930
632k
    sp->photometric = td->td_photometric;
1931
1932
    /*
1933
     * Initialize all JPEG parameters to default values.
1934
     * Note that jpeg_set_defaults needs legal values for
1935
     * in_color_space and input_components.
1936
     */
1937
632k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1938
632k
    {
1939
632k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
1940
632k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1941
28.4k
        {
1942
28.4k
            if (sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1943
28.4k
            {
1944
28.4k
                sp->cinfo.c.in_color_space = JCS_RGB;
1945
28.4k
            }
1946
0
            else
1947
0
            {
1948
0
                sp->cinfo.c.in_color_space = JCS_YCbCr;
1949
0
            }
1950
28.4k
        }
1951
603k
        else
1952
603k
        {
1953
603k
            if ((td->td_photometric == PHOTOMETRIC_MINISWHITE ||
1954
603k
                 td->td_photometric == PHOTOMETRIC_MINISBLACK) &&
1955
603k
                td->td_samplesperpixel == 1)
1956
569k
                sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
1957
33.8k
            else if (td->td_photometric == PHOTOMETRIC_RGB &&
1958
33.8k
                     td->td_samplesperpixel == 3)
1959
5.77k
                sp->cinfo.c.in_color_space = JCS_RGB;
1960
28.0k
            else if (td->td_photometric == PHOTOMETRIC_SEPARATED &&
1961
28.0k
                     td->td_samplesperpixel == 4)
1962
2.82k
                sp->cinfo.c.in_color_space = JCS_CMYK;
1963
25.2k
            else
1964
25.2k
                sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1965
603k
        }
1966
632k
    }
1967
0
    else
1968
0
    {
1969
0
        sp->cinfo.c.input_components = 1;
1970
0
        sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1971
0
    }
1972
632k
    if (!TIFFjpeg_set_defaults(sp))
1973
0
        return (0);
1974
1975
    /* mozjpeg by default enables progressive JPEG, which is illegal in
1976
     * JPEG-in-TIFF */
1977
    /* So explicitly disable it. */
1978
632k
    if (sp->cinfo.c.num_scans != 0 &&
1979
632k
        (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0)
1980
0
    {
1981
        /* it has been found that mozjpeg could create corrupt strips/tiles */
1982
        /* in non optimize_coding mode. */
1983
0
        TIFFWarningExtR(
1984
0
            tif, module,
1985
0
            "mozjpeg library likely detected. Disable emission of "
1986
0
            "Huffman tables in JpegTables tag, and use optimize_coding "
1987
0
            "to avoid potential issues");
1988
0
        sp->otherSettings.jpegtablesmode &= ~JPEGTABLESMODE_HUFF;
1989
0
    }
1990
632k
    sp->cinfo.c.num_scans = 0;
1991
632k
    sp->cinfo.c.scan_info = NULL;
1992
1993
    /* Set per-file parameters */
1994
632k
    switch (sp->photometric)
1995
632k
    {
1996
28.4k
        case PHOTOMETRIC_YCBCR:
1997
28.4k
            sp->h_sampling = td->td_ycbcrsubsampling[0];
1998
28.4k
            sp->v_sampling = td->td_ycbcrsubsampling[1];
1999
28.4k
            if (sp->h_sampling == 0 || sp->v_sampling == 0)
2000
0
            {
2001
0
                TIFFErrorExtR(tif, module,
2002
0
                              "Invalig horizontal/vertical sampling value");
2003
0
                return (0);
2004
0
            }
2005
28.4k
            if (td->td_bitspersample > 16)
2006
1.11k
            {
2007
1.11k
                TIFFErrorExtR(tif, module,
2008
1.11k
                              "BitsPerSample %" PRIu16 " not allowed for JPEG",
2009
1.11k
                              td->td_bitspersample);
2010
1.11k
                return (0);
2011
1.11k
            }
2012
2013
            /*
2014
             * A ReferenceBlackWhite field *must* be present since the
2015
             * default value is inappropriate for YCbCr.  Fill in the
2016
             * proper value if application didn't set it.
2017
             */
2018
27.3k
            {
2019
27.3k
                float *ref;
2020
27.3k
                if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE, &ref))
2021
14.2k
                {
2022
14.2k
                    float refbw[6];
2023
14.2k
                    long top = 1L << td->td_bitspersample;
2024
14.2k
                    refbw[0] = 0;
2025
14.2k
                    refbw[1] = (float)(top - 1L);
2026
14.2k
                    refbw[2] = (float)(top >> 1);
2027
14.2k
                    refbw[3] = refbw[1];
2028
14.2k
                    refbw[4] = refbw[2];
2029
14.2k
                    refbw[5] = refbw[1];
2030
14.2k
                    TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw);
2031
14.2k
                }
2032
27.3k
            }
2033
27.3k
            break;
2034
0
        case PHOTOMETRIC_PALETTE: /* disallowed by Tech Note */
2035
0
        case PHOTOMETRIC_MASK:
2036
0
            TIFFErrorExtR(tif, module,
2037
0
                          "PhotometricInterpretation %" PRIu16
2038
0
                          " not allowed for JPEG",
2039
0
                          sp->photometric);
2040
0
            return (0);
2041
603k
        default:
2042
            /* TIFF 6.0 forbids subsampling of all other color spaces */
2043
603k
            sp->h_sampling = 1;
2044
603k
            sp->v_sampling = 1;
2045
603k
            break;
2046
632k
    }
2047
2048
        /* Verify miscellaneous parameters */
2049
2050
        /*
2051
         * This would need work if libtiff ever supports different
2052
         * depths for different components, or if libjpeg ever supports
2053
         * run-time selection of depth.  Neither is imminent.
2054
         */
2055
#ifdef JPEG_LIB_MK1
2056
    /* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */
2057
    if (td->td_bitspersample != 8 && td->td_bitspersample != 12)
2058
#else
2059
631k
    if (td->td_bitspersample != BITS_IN_JSAMPLE)
2060
202k
#endif
2061
202k
    {
2062
202k
        TIFFErrorExtR(tif, module,
2063
202k
                      "BitsPerSample %" PRIu16 " not allowed for JPEG",
2064
202k
                      td->td_bitspersample);
2065
202k
        return (0);
2066
202k
    }
2067
428k
    sp->cinfo.c.data_precision = td->td_bitspersample;
2068
#ifdef JPEG_LIB_MK1
2069
    sp->cinfo.c.bits_in_jsample = td->td_bitspersample;
2070
#endif
2071
428k
    if (isTiled(tif))
2072
0
    {
2073
0
        if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0)
2074
0
        {
2075
0
            TIFFErrorExtR(tif, module,
2076
0
                          "JPEG tile height must be multiple of %" PRIu32,
2077
0
                          (uint32_t)(sp->v_sampling * DCTSIZE));
2078
0
            return (0);
2079
0
        }
2080
0
        if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0)
2081
0
        {
2082
0
            TIFFErrorExtR(tif, module,
2083
0
                          "JPEG tile width must be multiple of %" PRIu32,
2084
0
                          (uint32_t)(sp->h_sampling * DCTSIZE));
2085
0
            return (0);
2086
0
        }
2087
0
    }
2088
428k
    else
2089
428k
    {
2090
428k
        if (td->td_rowsperstrip < td->td_imagelength &&
2091
428k
            (td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0)
2092
0
        {
2093
0
            TIFFErrorExtR(tif, module,
2094
0
                          "RowsPerStrip must be multiple of %" PRIu32
2095
0
                          " for JPEG",
2096
0
                          (uint32_t)(sp->v_sampling * DCTSIZE));
2097
0
            return (0);
2098
0
        }
2099
428k
    }
2100
2101
    /* Create a JPEGTables field if appropriate */
2102
428k
    if (sp->otherSettings.jpegtablesmode &
2103
428k
        (JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF))
2104
428k
    {
2105
428k
        if (sp->otherSettings.jpegtables == NULL ||
2106
428k
            memcmp(sp->otherSettings.jpegtables, "\0\0\0\0\0\0\0\0\0", 8) == 0)
2107
214k
        {
2108
#if defined(JPEG_LIB_VERSION_MAJOR) &&                                         \
2109
    (JPEG_LIB_VERSION_MAJOR > 9 ||                                             \
2110
     (JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4))
2111
            if ((sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0 &&
2112
                (sp->cinfo.c.dc_huff_tbl_ptrs[0] == NULL ||
2113
                 sp->cinfo.c.dc_huff_tbl_ptrs[1] == NULL ||
2114
                 sp->cinfo.c.ac_huff_tbl_ptrs[0] == NULL ||
2115
                 sp->cinfo.c.ac_huff_tbl_ptrs[1] == NULL))
2116
            {
2117
                /* libjpeg-9d no longer initializes default Huffman tables in */
2118
                /* jpeg_set_defaults() */
2119
                TIFF_std_huff_tables(&sp->cinfo.c);
2120
            }
2121
#endif
2122
2123
214k
            if (!prepare_JPEGTables(tif))
2124
0
                return (0);
2125
            /* Mark the field present */
2126
            /* Can't use TIFFSetField since BEENWRITING is already set! */
2127
214k
            tif->tif_flags |= TIFF_DIRTYDIRECT;
2128
214k
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2129
214k
        }
2130
428k
    }
2131
0
    else
2132
0
    {
2133
        /* We do not support application-supplied JPEGTables, */
2134
        /* so mark the field not present */
2135
0
        TIFFClrFieldBit(tif, FIELD_JPEGTABLES);
2136
0
    }
2137
2138
    /* Direct libjpeg output to libtiff's output buffer */
2139
428k
    TIFFjpeg_data_dest(sp, tif);
2140
2141
428k
    return (1);
2142
428k
}
tif_jpeg.c:JPEGSetupEncode
Line
Count
Source
1910
632k
{
1911
632k
    JPEGState *sp = JState(tif);
1912
632k
    TIFFDirectory *td = &tif->tif_dir;
1913
632k
    static const char module[] = "JPEGSetupEncode";
1914
1915
632k
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1916
632k
    if (tif->tif_dir.td_bitspersample == 12)
1917
217k
    {
1918
        /* We pass a pointer to a copy of otherSettings, since */
1919
        /* TIFFReInitJPEG_12() will clear sp */
1920
217k
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1921
217k
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 1);
1922
217k
    }
1923
415k
#endif
1924
1925
415k
    JPEGInitializeLibJPEG(tif, FALSE);
1926
1927
415k
    assert(sp != NULL);
1928
415k
    assert(!sp->cinfo.comm.is_decompressor);
1929
1930
415k
    sp->photometric = td->td_photometric;
1931
1932
    /*
1933
     * Initialize all JPEG parameters to default values.
1934
     * Note that jpeg_set_defaults needs legal values for
1935
     * in_color_space and input_components.
1936
     */
1937
415k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1938
415k
    {
1939
415k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
1940
415k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1941
5.73k
        {
1942
5.73k
            if (sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1943
5.73k
            {
1944
5.73k
                sp->cinfo.c.in_color_space = JCS_RGB;
1945
5.73k
            }
1946
0
            else
1947
0
            {
1948
0
                sp->cinfo.c.in_color_space = JCS_YCbCr;
1949
0
            }
1950
5.73k
        }
1951
409k
        else
1952
409k
        {
1953
409k
            if ((td->td_photometric == PHOTOMETRIC_MINISWHITE ||
1954
409k
                 td->td_photometric == PHOTOMETRIC_MINISBLACK) &&
1955
409k
                td->td_samplesperpixel == 1)
1956
382k
                sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
1957
27.1k
            else if (td->td_photometric == PHOTOMETRIC_RGB &&
1958
27.1k
                     td->td_samplesperpixel == 3)
1959
5.77k
                sp->cinfo.c.in_color_space = JCS_RGB;
1960
21.3k
            else if (td->td_photometric == PHOTOMETRIC_SEPARATED &&
1961
21.3k
                     td->td_samplesperpixel == 4)
1962
1.41k
                sp->cinfo.c.in_color_space = JCS_CMYK;
1963
19.9k
            else
1964
19.9k
                sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1965
409k
        }
1966
415k
    }
1967
0
    else
1968
0
    {
1969
0
        sp->cinfo.c.input_components = 1;
1970
0
        sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1971
0
    }
1972
415k
    if (!TIFFjpeg_set_defaults(sp))
1973
0
        return (0);
1974
1975
    /* mozjpeg by default enables progressive JPEG, which is illegal in
1976
     * JPEG-in-TIFF */
1977
    /* So explicitly disable it. */
1978
415k
    if (sp->cinfo.c.num_scans != 0 &&
1979
415k
        (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0)
1980
0
    {
1981
        /* it has been found that mozjpeg could create corrupt strips/tiles */
1982
        /* in non optimize_coding mode. */
1983
0
        TIFFWarningExtR(
1984
0
            tif, module,
1985
0
            "mozjpeg library likely detected. Disable emission of "
1986
0
            "Huffman tables in JpegTables tag, and use optimize_coding "
1987
0
            "to avoid potential issues");
1988
0
        sp->otherSettings.jpegtablesmode &= ~JPEGTABLESMODE_HUFF;
1989
0
    }
1990
415k
    sp->cinfo.c.num_scans = 0;
1991
415k
    sp->cinfo.c.scan_info = NULL;
1992
1993
    /* Set per-file parameters */
1994
415k
    switch (sp->photometric)
1995
415k
    {
1996
5.73k
        case PHOTOMETRIC_YCBCR:
1997
5.73k
            sp->h_sampling = td->td_ycbcrsubsampling[0];
1998
5.73k
            sp->v_sampling = td->td_ycbcrsubsampling[1];
1999
5.73k
            if (sp->h_sampling == 0 || sp->v_sampling == 0)
2000
0
            {
2001
0
                TIFFErrorExtR(tif, module,
2002
0
                              "Invalig horizontal/vertical sampling value");
2003
0
                return (0);
2004
0
            }
2005
5.73k
            if (td->td_bitspersample > 16)
2006
1.11k
            {
2007
1.11k
                TIFFErrorExtR(tif, module,
2008
1.11k
                              "BitsPerSample %" PRIu16 " not allowed for JPEG",
2009
1.11k
                              td->td_bitspersample);
2010
1.11k
                return (0);
2011
1.11k
            }
2012
2013
            /*
2014
             * A ReferenceBlackWhite field *must* be present since the
2015
             * default value is inappropriate for YCbCr.  Fill in the
2016
             * proper value if application didn't set it.
2017
             */
2018
4.62k
            {
2019
4.62k
                float *ref;
2020
4.62k
                if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE, &ref))
2021
2.86k
                {
2022
2.86k
                    float refbw[6];
2023
2.86k
                    long top = 1L << td->td_bitspersample;
2024
2.86k
                    refbw[0] = 0;
2025
2.86k
                    refbw[1] = (float)(top - 1L);
2026
2.86k
                    refbw[2] = (float)(top >> 1);
2027
2.86k
                    refbw[3] = refbw[1];
2028
2.86k
                    refbw[4] = refbw[2];
2029
2.86k
                    refbw[5] = refbw[1];
2030
2.86k
                    TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw);
2031
2.86k
                }
2032
4.62k
            }
2033
4.62k
            break;
2034
0
        case PHOTOMETRIC_PALETTE: /* disallowed by Tech Note */
2035
0
        case PHOTOMETRIC_MASK:
2036
0
            TIFFErrorExtR(tif, module,
2037
0
                          "PhotometricInterpretation %" PRIu16
2038
0
                          " not allowed for JPEG",
2039
0
                          sp->photometric);
2040
0
            return (0);
2041
409k
        default:
2042
            /* TIFF 6.0 forbids subsampling of all other color spaces */
2043
409k
            sp->h_sampling = 1;
2044
409k
            sp->v_sampling = 1;
2045
409k
            break;
2046
415k
    }
2047
2048
        /* Verify miscellaneous parameters */
2049
2050
        /*
2051
         * This would need work if libtiff ever supports different
2052
         * depths for different components, or if libjpeg ever supports
2053
         * run-time selection of depth.  Neither is imminent.
2054
         */
2055
#ifdef JPEG_LIB_MK1
2056
    /* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */
2057
    if (td->td_bitspersample != 8 && td->td_bitspersample != 12)
2058
#else
2059
413k
    if (td->td_bitspersample != BITS_IN_JSAMPLE)
2060
202k
#endif
2061
202k
    {
2062
202k
        TIFFErrorExtR(tif, module,
2063
202k
                      "BitsPerSample %" PRIu16 " not allowed for JPEG",
2064
202k
                      td->td_bitspersample);
2065
202k
        return (0);
2066
202k
    }
2067
211k
    sp->cinfo.c.data_precision = td->td_bitspersample;
2068
#ifdef JPEG_LIB_MK1
2069
    sp->cinfo.c.bits_in_jsample = td->td_bitspersample;
2070
#endif
2071
211k
    if (isTiled(tif))
2072
0
    {
2073
0
        if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0)
2074
0
        {
2075
0
            TIFFErrorExtR(tif, module,
2076
0
                          "JPEG tile height must be multiple of %" PRIu32,
2077
0
                          (uint32_t)(sp->v_sampling * DCTSIZE));
2078
0
            return (0);
2079
0
        }
2080
0
        if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0)
2081
0
        {
2082
0
            TIFFErrorExtR(tif, module,
2083
0
                          "JPEG tile width must be multiple of %" PRIu32,
2084
0
                          (uint32_t)(sp->h_sampling * DCTSIZE));
2085
0
            return (0);
2086
0
        }
2087
0
    }
2088
211k
    else
2089
211k
    {
2090
211k
        if (td->td_rowsperstrip < td->td_imagelength &&
2091
211k
            (td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0)
2092
0
        {
2093
0
            TIFFErrorExtR(tif, module,
2094
0
                          "RowsPerStrip must be multiple of %" PRIu32
2095
0
                          " for JPEG",
2096
0
                          (uint32_t)(sp->v_sampling * DCTSIZE));
2097
0
            return (0);
2098
0
        }
2099
211k
    }
2100
2101
    /* Create a JPEGTables field if appropriate */
2102
211k
    if (sp->otherSettings.jpegtablesmode &
2103
211k
        (JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF))
2104
211k
    {
2105
211k
        if (sp->otherSettings.jpegtables == NULL ||
2106
211k
            memcmp(sp->otherSettings.jpegtables, "\0\0\0\0\0\0\0\0\0", 8) == 0)
2107
105k
        {
2108
#if defined(JPEG_LIB_VERSION_MAJOR) &&                                         \
2109
    (JPEG_LIB_VERSION_MAJOR > 9 ||                                             \
2110
     (JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4))
2111
            if ((sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0 &&
2112
                (sp->cinfo.c.dc_huff_tbl_ptrs[0] == NULL ||
2113
                 sp->cinfo.c.dc_huff_tbl_ptrs[1] == NULL ||
2114
                 sp->cinfo.c.ac_huff_tbl_ptrs[0] == NULL ||
2115
                 sp->cinfo.c.ac_huff_tbl_ptrs[1] == NULL))
2116
            {
2117
                /* libjpeg-9d no longer initializes default Huffman tables in */
2118
                /* jpeg_set_defaults() */
2119
                TIFF_std_huff_tables(&sp->cinfo.c);
2120
            }
2121
#endif
2122
2123
105k
            if (!prepare_JPEGTables(tif))
2124
0
                return (0);
2125
            /* Mark the field present */
2126
            /* Can't use TIFFSetField since BEENWRITING is already set! */
2127
105k
            tif->tif_flags |= TIFF_DIRTYDIRECT;
2128
105k
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2129
105k
        }
2130
211k
    }
2131
0
    else
2132
0
    {
2133
        /* We do not support application-supplied JPEGTables, */
2134
        /* so mark the field not present */
2135
0
        TIFFClrFieldBit(tif, FIELD_JPEGTABLES);
2136
0
    }
2137
2138
    /* Direct libjpeg output to libtiff's output buffer */
2139
211k
    TIFFjpeg_data_dest(sp, tif);
2140
2141
211k
    return (1);
2142
211k
}
tif_jpeg_12.c:JPEGSetupEncode
Line
Count
Source
1910
217k
{
1911
217k
    JPEGState *sp = JState(tif);
1912
217k
    TIFFDirectory *td = &tif->tif_dir;
1913
217k
    static const char module[] = "JPEGSetupEncode";
1914
1915
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1916
    if (tif->tif_dir.td_bitspersample == 12)
1917
    {
1918
        /* We pass a pointer to a copy of otherSettings, since */
1919
        /* TIFFReInitJPEG_12() will clear sp */
1920
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1921
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 1);
1922
    }
1923
#endif
1924
1925
217k
    JPEGInitializeLibJPEG(tif, FALSE);
1926
1927
217k
    assert(sp != NULL);
1928
217k
    assert(!sp->cinfo.comm.is_decompressor);
1929
1930
217k
    sp->photometric = td->td_photometric;
1931
1932
    /*
1933
     * Initialize all JPEG parameters to default values.
1934
     * Note that jpeg_set_defaults needs legal values for
1935
     * in_color_space and input_components.
1936
     */
1937
217k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1938
217k
    {
1939
217k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
1940
217k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1941
22.6k
        {
1942
22.6k
            if (sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1943
22.6k
            {
1944
22.6k
                sp->cinfo.c.in_color_space = JCS_RGB;
1945
22.6k
            }
1946
0
            else
1947
0
            {
1948
0
                sp->cinfo.c.in_color_space = JCS_YCbCr;
1949
0
            }
1950
22.6k
        }
1951
194k
        else
1952
194k
        {
1953
194k
            if ((td->td_photometric == PHOTOMETRIC_MINISWHITE ||
1954
194k
                 td->td_photometric == PHOTOMETRIC_MINISBLACK) &&
1955
194k
                td->td_samplesperpixel == 1)
1956
187k
                sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
1957
6.67k
            else if (td->td_photometric == PHOTOMETRIC_RGB &&
1958
6.67k
                     td->td_samplesperpixel == 3)
1959
0
                sp->cinfo.c.in_color_space = JCS_RGB;
1960
6.67k
            else if (td->td_photometric == PHOTOMETRIC_SEPARATED &&
1961
6.67k
                     td->td_samplesperpixel == 4)
1962
1.41k
                sp->cinfo.c.in_color_space = JCS_CMYK;
1963
5.26k
            else
1964
5.26k
                sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1965
194k
        }
1966
217k
    }
1967
0
    else
1968
0
    {
1969
0
        sp->cinfo.c.input_components = 1;
1970
0
        sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1971
0
    }
1972
217k
    if (!TIFFjpeg_set_defaults(sp))
1973
0
        return (0);
1974
1975
    /* mozjpeg by default enables progressive JPEG, which is illegal in
1976
     * JPEG-in-TIFF */
1977
    /* So explicitly disable it. */
1978
217k
    if (sp->cinfo.c.num_scans != 0 &&
1979
217k
        (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0)
1980
0
    {
1981
        /* it has been found that mozjpeg could create corrupt strips/tiles */
1982
        /* in non optimize_coding mode. */
1983
0
        TIFFWarningExtR(
1984
0
            tif, module,
1985
0
            "mozjpeg library likely detected. Disable emission of "
1986
0
            "Huffman tables in JpegTables tag, and use optimize_coding "
1987
0
            "to avoid potential issues");
1988
0
        sp->otherSettings.jpegtablesmode &= ~JPEGTABLESMODE_HUFF;
1989
0
    }
1990
217k
    sp->cinfo.c.num_scans = 0;
1991
217k
    sp->cinfo.c.scan_info = NULL;
1992
1993
    /* Set per-file parameters */
1994
217k
    switch (sp->photometric)
1995
217k
    {
1996
22.6k
        case PHOTOMETRIC_YCBCR:
1997
22.6k
            sp->h_sampling = td->td_ycbcrsubsampling[0];
1998
22.6k
            sp->v_sampling = td->td_ycbcrsubsampling[1];
1999
22.6k
            if (sp->h_sampling == 0 || sp->v_sampling == 0)
2000
0
            {
2001
0
                TIFFErrorExtR(tif, module,
2002
0
                              "Invalig horizontal/vertical sampling value");
2003
0
                return (0);
2004
0
            }
2005
22.6k
            if (td->td_bitspersample > 16)
2006
0
            {
2007
0
                TIFFErrorExtR(tif, module,
2008
0
                              "BitsPerSample %" PRIu16 " not allowed for JPEG",
2009
0
                              td->td_bitspersample);
2010
0
                return (0);
2011
0
            }
2012
2013
            /*
2014
             * A ReferenceBlackWhite field *must* be present since the
2015
             * default value is inappropriate for YCbCr.  Fill in the
2016
             * proper value if application didn't set it.
2017
             */
2018
22.6k
            {
2019
22.6k
                float *ref;
2020
22.6k
                if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE, &ref))
2021
11.3k
                {
2022
11.3k
                    float refbw[6];
2023
11.3k
                    long top = 1L << td->td_bitspersample;
2024
11.3k
                    refbw[0] = 0;
2025
11.3k
                    refbw[1] = (float)(top - 1L);
2026
11.3k
                    refbw[2] = (float)(top >> 1);
2027
11.3k
                    refbw[3] = refbw[1];
2028
11.3k
                    refbw[4] = refbw[2];
2029
11.3k
                    refbw[5] = refbw[1];
2030
11.3k
                    TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw);
2031
11.3k
                }
2032
22.6k
            }
2033
22.6k
            break;
2034
0
        case PHOTOMETRIC_PALETTE: /* disallowed by Tech Note */
2035
0
        case PHOTOMETRIC_MASK:
2036
0
            TIFFErrorExtR(tif, module,
2037
0
                          "PhotometricInterpretation %" PRIu16
2038
0
                          " not allowed for JPEG",
2039
0
                          sp->photometric);
2040
0
            return (0);
2041
194k
        default:
2042
            /* TIFF 6.0 forbids subsampling of all other color spaces */
2043
194k
            sp->h_sampling = 1;
2044
194k
            sp->v_sampling = 1;
2045
194k
            break;
2046
217k
    }
2047
2048
        /* Verify miscellaneous parameters */
2049
2050
        /*
2051
         * This would need work if libtiff ever supports different
2052
         * depths for different components, or if libjpeg ever supports
2053
         * run-time selection of depth.  Neither is imminent.
2054
         */
2055
#ifdef JPEG_LIB_MK1
2056
    /* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */
2057
    if (td->td_bitspersample != 8 && td->td_bitspersample != 12)
2058
#else
2059
217k
    if (td->td_bitspersample != BITS_IN_JSAMPLE)
2060
0
#endif
2061
0
    {
2062
0
        TIFFErrorExtR(tif, module,
2063
0
                      "BitsPerSample %" PRIu16 " not allowed for JPEG",
2064
0
                      td->td_bitspersample);
2065
0
        return (0);
2066
0
    }
2067
217k
    sp->cinfo.c.data_precision = td->td_bitspersample;
2068
#ifdef JPEG_LIB_MK1
2069
    sp->cinfo.c.bits_in_jsample = td->td_bitspersample;
2070
#endif
2071
217k
    if (isTiled(tif))
2072
0
    {
2073
0
        if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0)
2074
0
        {
2075
0
            TIFFErrorExtR(tif, module,
2076
0
                          "JPEG tile height must be multiple of %" PRIu32,
2077
0
                          (uint32_t)(sp->v_sampling * DCTSIZE));
2078
0
            return (0);
2079
0
        }
2080
0
        if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0)
2081
0
        {
2082
0
            TIFFErrorExtR(tif, module,
2083
0
                          "JPEG tile width must be multiple of %" PRIu32,
2084
0
                          (uint32_t)(sp->h_sampling * DCTSIZE));
2085
0
            return (0);
2086
0
        }
2087
0
    }
2088
217k
    else
2089
217k
    {
2090
217k
        if (td->td_rowsperstrip < td->td_imagelength &&
2091
217k
            (td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0)
2092
0
        {
2093
0
            TIFFErrorExtR(tif, module,
2094
0
                          "RowsPerStrip must be multiple of %" PRIu32
2095
0
                          " for JPEG",
2096
0
                          (uint32_t)(sp->v_sampling * DCTSIZE));
2097
0
            return (0);
2098
0
        }
2099
217k
    }
2100
2101
    /* Create a JPEGTables field if appropriate */
2102
217k
    if (sp->otherSettings.jpegtablesmode &
2103
217k
        (JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF))
2104
217k
    {
2105
217k
        if (sp->otherSettings.jpegtables == NULL ||
2106
217k
            memcmp(sp->otherSettings.jpegtables, "\0\0\0\0\0\0\0\0\0", 8) == 0)
2107
108k
        {
2108
#if defined(JPEG_LIB_VERSION_MAJOR) &&                                         \
2109
    (JPEG_LIB_VERSION_MAJOR > 9 ||                                             \
2110
     (JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4))
2111
            if ((sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0 &&
2112
                (sp->cinfo.c.dc_huff_tbl_ptrs[0] == NULL ||
2113
                 sp->cinfo.c.dc_huff_tbl_ptrs[1] == NULL ||
2114
                 sp->cinfo.c.ac_huff_tbl_ptrs[0] == NULL ||
2115
                 sp->cinfo.c.ac_huff_tbl_ptrs[1] == NULL))
2116
            {
2117
                /* libjpeg-9d no longer initializes default Huffman tables in */
2118
                /* jpeg_set_defaults() */
2119
                TIFF_std_huff_tables(&sp->cinfo.c);
2120
            }
2121
#endif
2122
2123
108k
            if (!prepare_JPEGTables(tif))
2124
0
                return (0);
2125
            /* Mark the field present */
2126
            /* Can't use TIFFSetField since BEENWRITING is already set! */
2127
108k
            tif->tif_flags |= TIFF_DIRTYDIRECT;
2128
108k
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2129
108k
        }
2130
217k
    }
2131
0
    else
2132
0
    {
2133
        /* We do not support application-supplied JPEGTables, */
2134
        /* so mark the field not present */
2135
0
        TIFFClrFieldBit(tif, FIELD_JPEGTABLES);
2136
0
    }
2137
2138
    /* Direct libjpeg output to libtiff's output buffer */
2139
217k
    TIFFjpeg_data_dest(sp, tif);
2140
2141
217k
    return (1);
2142
217k
}
2143
2144
/*
2145
 * Set encoding state at the start of a strip or tile.
2146
 */
2147
static int JPEGPreEncode(TIFF *tif, uint16_t s)
2148
428k
{
2149
428k
    JPEGState *sp = JState(tif);
2150
428k
    TIFFDirectory *td = &tif->tif_dir;
2151
428k
    static const char module[] = "JPEGPreEncode";
2152
428k
    uint32_t segment_width, segment_height;
2153
428k
    int downsampled_input;
2154
2155
428k
    assert(sp != NULL);
2156
2157
428k
    if (sp->cinfo.comm.is_decompressor == 1)
2158
0
    {
2159
0
        tif->tif_setupencode(tif);
2160
0
    }
2161
2162
428k
    assert(!sp->cinfo.comm.is_decompressor);
2163
    /*
2164
     * Set encoding parameters for this strip/tile.
2165
     */
2166
428k
    if (isTiled(tif))
2167
0
    {
2168
0
        segment_width = td->td_tilewidth;
2169
0
        segment_height = td->td_tilelength;
2170
0
        sp->bytesperline = TIFFTileRowSize(tif);
2171
0
    }
2172
428k
    else
2173
428k
    {
2174
428k
        segment_width = td->td_imagewidth;
2175
428k
        segment_height = td->td_imagelength - tif->tif_row;
2176
428k
        if (segment_height > td->td_rowsperstrip)
2177
0
            segment_height = td->td_rowsperstrip;
2178
428k
        sp->bytesperline = TIFFScanlineSize(tif);
2179
428k
    }
2180
428k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
2181
0
    {
2182
        /* for PC 2, scale down the strip/tile size
2183
         * to match a downsampled component
2184
         */
2185
0
        if (sp->h_sampling == 0 || sp->v_sampling == 0)
2186
0
        {
2187
0
            TIFFErrorExtR(tif, module,
2188
0
                          "JPEG horizontal or vertical sampling is zero");
2189
0
            return (0);
2190
0
        }
2191
0
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
2192
0
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
2193
0
    }
2194
428k
    if (segment_width > (uint32_t)JPEG_MAX_DIMENSION ||
2195
428k
        segment_height > (uint32_t)JPEG_MAX_DIMENSION)
2196
0
    {
2197
0
        TIFFErrorExtR(tif, module,
2198
0
                      "Strip/tile too large for JPEG. Maximum dimension is %d",
2199
0
                      (int)JPEG_MAX_DIMENSION);
2200
0
        return (0);
2201
0
    }
2202
428k
    sp->cinfo.c.image_width = segment_width;
2203
428k
    sp->cinfo.c.image_height = segment_height;
2204
428k
    downsampled_input = FALSE;
2205
428k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
2206
428k
    {
2207
428k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
2208
428k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
2209
24.9k
        {
2210
24.9k
            if (sp->otherSettings.jpegcolormode != JPEGCOLORMODE_RGB)
2211
0
            {
2212
0
                if (sp->h_sampling != 1 || sp->v_sampling != 1)
2213
0
                    downsampled_input = TRUE;
2214
0
            }
2215
24.9k
            if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr))
2216
0
                return (0);
2217
            /*
2218
             * Set Y sampling factors;
2219
             * we assume jpeg_set_colorspace() set the rest to 1
2220
             */
2221
24.9k
            sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
2222
24.9k
            sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
2223
24.9k
        }
2224
403k
        else
2225
403k
        {
2226
403k
            if (!TIFFjpeg_set_colorspace(sp, sp->cinfo.c.in_color_space))
2227
0
                return (0);
2228
            /* jpeg_set_colorspace set all sampling factors to 1 */
2229
403k
        }
2230
428k
    }
2231
0
    else
2232
0
    {
2233
0
        if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
2234
0
            return (0);
2235
0
        sp->cinfo.c.comp_info[0].component_id = s;
2236
        /* jpeg_set_colorspace() set sampling factors to 1 */
2237
0
        if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0)
2238
0
        {
2239
0
            sp->cinfo.c.comp_info[0].quant_tbl_no = 1;
2240
0
            sp->cinfo.c.comp_info[0].dc_tbl_no = 1;
2241
0
            sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
2242
0
        }
2243
0
    }
2244
    /* ensure libjpeg won't write any extraneous markers */
2245
428k
    sp->cinfo.c.write_JFIF_header = FALSE;
2246
428k
    sp->cinfo.c.write_Adobe_marker = FALSE;
2247
    /* set up table handling correctly */
2248
    /* calling TIFFjpeg_set_quality() causes quantization tables to be flagged
2249
     */
2250
    /* as being to be emitted, which we don't want in the JPEGTABLESMODE_QUANT
2251
     */
2252
    /* mode, so we must manually suppress them. However TIFFjpeg_set_quality()
2253
     */
2254
    /* should really be called when dealing with files with directories with */
2255
    /* mixed qualities. see http://trac.osgeo.org/gdal/ticket/3539 */
2256
428k
    if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
2257
0
        return (0);
2258
428k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
2259
428k
    {
2260
428k
        suppress_quant_table(sp, 0);
2261
428k
        suppress_quant_table(sp, 1);
2262
428k
    }
2263
0
    else
2264
0
    {
2265
0
        unsuppress_quant_table(sp, 0);
2266
0
        unsuppress_quant_table(sp, 1);
2267
0
    }
2268
428k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
2269
214k
    {
2270
        /* Explicit suppression is only needed if we did not go through the */
2271
        /* prepare_JPEGTables() code path, which may be the case if updating */
2272
        /* an existing file */
2273
214k
        suppress_huff_table(sp, 0);
2274
214k
        suppress_huff_table(sp, 1);
2275
214k
        sp->cinfo.c.optimize_coding = FALSE;
2276
214k
    }
2277
214k
    else
2278
214k
        sp->cinfo.c.optimize_coding = TRUE;
2279
428k
    if (downsampled_input)
2280
0
    {
2281
        /* Need to use raw-data interface to libjpeg */
2282
0
        sp->cinfo.c.raw_data_in = TRUE;
2283
0
        tif->tif_encoderow = JPEGEncodeRaw;
2284
0
        tif->tif_encodestrip = JPEGEncodeRaw;
2285
0
        tif->tif_encodetile = JPEGEncodeRaw;
2286
0
    }
2287
428k
    else
2288
428k
    {
2289
        /* Use normal interface to libjpeg */
2290
428k
        sp->cinfo.c.raw_data_in = FALSE;
2291
428k
        tif->tif_encoderow = JPEGEncode;
2292
428k
        tif->tif_encodestrip = JPEGEncode;
2293
428k
        tif->tif_encodetile = JPEGEncode;
2294
428k
    }
2295
    /* Start JPEG compressor */
2296
428k
    if (!TIFFjpeg_start_compress(sp, FALSE))
2297
0
        return (0);
2298
    /* Allocate downsampled-data buffers if needed */
2299
428k
    if (downsampled_input)
2300
0
    {
2301
0
        if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info,
2302
0
                                       sp->cinfo.c.num_components))
2303
0
            return (0);
2304
0
    }
2305
428k
    sp->scancount = 0;
2306
2307
428k
    return (1);
2308
428k
}
tif_jpeg.c:JPEGPreEncode
Line
Count
Source
2148
211k
{
2149
211k
    JPEGState *sp = JState(tif);
2150
211k
    TIFFDirectory *td = &tif->tif_dir;
2151
211k
    static const char module[] = "JPEGPreEncode";
2152
211k
    uint32_t segment_width, segment_height;
2153
211k
    int downsampled_input;
2154
2155
211k
    assert(sp != NULL);
2156
2157
211k
    if (sp->cinfo.comm.is_decompressor == 1)
2158
0
    {
2159
0
        tif->tif_setupencode(tif);
2160
0
    }
2161
2162
211k
    assert(!sp->cinfo.comm.is_decompressor);
2163
    /*
2164
     * Set encoding parameters for this strip/tile.
2165
     */
2166
211k
    if (isTiled(tif))
2167
0
    {
2168
0
        segment_width = td->td_tilewidth;
2169
0
        segment_height = td->td_tilelength;
2170
0
        sp->bytesperline = TIFFTileRowSize(tif);
2171
0
    }
2172
211k
    else
2173
211k
    {
2174
211k
        segment_width = td->td_imagewidth;
2175
211k
        segment_height = td->td_imagelength - tif->tif_row;
2176
211k
        if (segment_height > td->td_rowsperstrip)
2177
0
            segment_height = td->td_rowsperstrip;
2178
211k
        sp->bytesperline = TIFFScanlineSize(tif);
2179
211k
    }
2180
211k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
2181
0
    {
2182
        /* for PC 2, scale down the strip/tile size
2183
         * to match a downsampled component
2184
         */
2185
0
        if (sp->h_sampling == 0 || sp->v_sampling == 0)
2186
0
        {
2187
0
            TIFFErrorExtR(tif, module,
2188
0
                          "JPEG horizontal or vertical sampling is zero");
2189
0
            return (0);
2190
0
        }
2191
0
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
2192
0
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
2193
0
    }
2194
211k
    if (segment_width > (uint32_t)JPEG_MAX_DIMENSION ||
2195
211k
        segment_height > (uint32_t)JPEG_MAX_DIMENSION)
2196
0
    {
2197
0
        TIFFErrorExtR(tif, module,
2198
0
                      "Strip/tile too large for JPEG. Maximum dimension is %d",
2199
0
                      (int)JPEG_MAX_DIMENSION);
2200
0
        return (0);
2201
0
    }
2202
211k
    sp->cinfo.c.image_width = segment_width;
2203
211k
    sp->cinfo.c.image_height = segment_height;
2204
211k
    downsampled_input = FALSE;
2205
211k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
2206
211k
    {
2207
211k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
2208
211k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
2209
2.30k
        {
2210
2.30k
            if (sp->otherSettings.jpegcolormode != JPEGCOLORMODE_RGB)
2211
0
            {
2212
0
                if (sp->h_sampling != 1 || sp->v_sampling != 1)
2213
0
                    downsampled_input = TRUE;
2214
0
            }
2215
2.30k
            if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr))
2216
0
                return (0);
2217
            /*
2218
             * Set Y sampling factors;
2219
             * we assume jpeg_set_colorspace() set the rest to 1
2220
             */
2221
2.30k
            sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
2222
2.30k
            sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
2223
2.30k
        }
2224
209k
        else
2225
209k
        {
2226
209k
            if (!TIFFjpeg_set_colorspace(sp, sp->cinfo.c.in_color_space))
2227
0
                return (0);
2228
            /* jpeg_set_colorspace set all sampling factors to 1 */
2229
209k
        }
2230
211k
    }
2231
0
    else
2232
0
    {
2233
0
        if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
2234
0
            return (0);
2235
0
        sp->cinfo.c.comp_info[0].component_id = s;
2236
        /* jpeg_set_colorspace() set sampling factors to 1 */
2237
0
        if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0)
2238
0
        {
2239
0
            sp->cinfo.c.comp_info[0].quant_tbl_no = 1;
2240
0
            sp->cinfo.c.comp_info[0].dc_tbl_no = 1;
2241
0
            sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
2242
0
        }
2243
0
    }
2244
    /* ensure libjpeg won't write any extraneous markers */
2245
211k
    sp->cinfo.c.write_JFIF_header = FALSE;
2246
211k
    sp->cinfo.c.write_Adobe_marker = FALSE;
2247
    /* set up table handling correctly */
2248
    /* calling TIFFjpeg_set_quality() causes quantization tables to be flagged
2249
     */
2250
    /* as being to be emitted, which we don't want in the JPEGTABLESMODE_QUANT
2251
     */
2252
    /* mode, so we must manually suppress them. However TIFFjpeg_set_quality()
2253
     */
2254
    /* should really be called when dealing with files with directories with */
2255
    /* mixed qualities. see http://trac.osgeo.org/gdal/ticket/3539 */
2256
211k
    if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
2257
0
        return (0);
2258
211k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
2259
211k
    {
2260
211k
        suppress_quant_table(sp, 0);
2261
211k
        suppress_quant_table(sp, 1);
2262
211k
    }
2263
0
    else
2264
0
    {
2265
0
        unsuppress_quant_table(sp, 0);
2266
0
        unsuppress_quant_table(sp, 1);
2267
0
    }
2268
211k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
2269
105k
    {
2270
        /* Explicit suppression is only needed if we did not go through the */
2271
        /* prepare_JPEGTables() code path, which may be the case if updating */
2272
        /* an existing file */
2273
105k
        suppress_huff_table(sp, 0);
2274
105k
        suppress_huff_table(sp, 1);
2275
105k
        sp->cinfo.c.optimize_coding = FALSE;
2276
105k
    }
2277
105k
    else
2278
105k
        sp->cinfo.c.optimize_coding = TRUE;
2279
211k
    if (downsampled_input)
2280
0
    {
2281
        /* Need to use raw-data interface to libjpeg */
2282
0
        sp->cinfo.c.raw_data_in = TRUE;
2283
0
        tif->tif_encoderow = JPEGEncodeRaw;
2284
0
        tif->tif_encodestrip = JPEGEncodeRaw;
2285
0
        tif->tif_encodetile = JPEGEncodeRaw;
2286
0
    }
2287
211k
    else
2288
211k
    {
2289
        /* Use normal interface to libjpeg */
2290
211k
        sp->cinfo.c.raw_data_in = FALSE;
2291
211k
        tif->tif_encoderow = JPEGEncode;
2292
211k
        tif->tif_encodestrip = JPEGEncode;
2293
211k
        tif->tif_encodetile = JPEGEncode;
2294
211k
    }
2295
    /* Start JPEG compressor */
2296
211k
    if (!TIFFjpeg_start_compress(sp, FALSE))
2297
0
        return (0);
2298
    /* Allocate downsampled-data buffers if needed */
2299
211k
    if (downsampled_input)
2300
0
    {
2301
0
        if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info,
2302
0
                                       sp->cinfo.c.num_components))
2303
0
            return (0);
2304
0
    }
2305
211k
    sp->scancount = 0;
2306
2307
211k
    return (1);
2308
211k
}
tif_jpeg_12.c:JPEGPreEncode
Line
Count
Source
2148
217k
{
2149
217k
    JPEGState *sp = JState(tif);
2150
217k
    TIFFDirectory *td = &tif->tif_dir;
2151
217k
    static const char module[] = "JPEGPreEncode";
2152
217k
    uint32_t segment_width, segment_height;
2153
217k
    int downsampled_input;
2154
2155
217k
    assert(sp != NULL);
2156
2157
217k
    if (sp->cinfo.comm.is_decompressor == 1)
2158
0
    {
2159
0
        tif->tif_setupencode(tif);
2160
0
    }
2161
2162
217k
    assert(!sp->cinfo.comm.is_decompressor);
2163
    /*
2164
     * Set encoding parameters for this strip/tile.
2165
     */
2166
217k
    if (isTiled(tif))
2167
0
    {
2168
0
        segment_width = td->td_tilewidth;
2169
0
        segment_height = td->td_tilelength;
2170
0
        sp->bytesperline = TIFFTileRowSize(tif);
2171
0
    }
2172
217k
    else
2173
217k
    {
2174
217k
        segment_width = td->td_imagewidth;
2175
217k
        segment_height = td->td_imagelength - tif->tif_row;
2176
217k
        if (segment_height > td->td_rowsperstrip)
2177
0
            segment_height = td->td_rowsperstrip;
2178
217k
        sp->bytesperline = TIFFScanlineSize(tif);
2179
217k
    }
2180
217k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
2181
0
    {
2182
        /* for PC 2, scale down the strip/tile size
2183
         * to match a downsampled component
2184
         */
2185
0
        if (sp->h_sampling == 0 || sp->v_sampling == 0)
2186
0
        {
2187
0
            TIFFErrorExtR(tif, module,
2188
0
                          "JPEG horizontal or vertical sampling is zero");
2189
0
            return (0);
2190
0
        }
2191
0
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
2192
0
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
2193
0
    }
2194
217k
    if (segment_width > (uint32_t)JPEG_MAX_DIMENSION ||
2195
217k
        segment_height > (uint32_t)JPEG_MAX_DIMENSION)
2196
0
    {
2197
0
        TIFFErrorExtR(tif, module,
2198
0
                      "Strip/tile too large for JPEG. Maximum dimension is %d",
2199
0
                      (int)JPEG_MAX_DIMENSION);
2200
0
        return (0);
2201
0
    }
2202
217k
    sp->cinfo.c.image_width = segment_width;
2203
217k
    sp->cinfo.c.image_height = segment_height;
2204
217k
    downsampled_input = FALSE;
2205
217k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
2206
217k
    {
2207
217k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
2208
217k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
2209
22.6k
        {
2210
22.6k
            if (sp->otherSettings.jpegcolormode != JPEGCOLORMODE_RGB)
2211
0
            {
2212
0
                if (sp->h_sampling != 1 || sp->v_sampling != 1)
2213
0
                    downsampled_input = TRUE;
2214
0
            }
2215
22.6k
            if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr))
2216
0
                return (0);
2217
            /*
2218
             * Set Y sampling factors;
2219
             * we assume jpeg_set_colorspace() set the rest to 1
2220
             */
2221
22.6k
            sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
2222
22.6k
            sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
2223
22.6k
        }
2224
194k
        else
2225
194k
        {
2226
194k
            if (!TIFFjpeg_set_colorspace(sp, sp->cinfo.c.in_color_space))
2227
0
                return (0);
2228
            /* jpeg_set_colorspace set all sampling factors to 1 */
2229
194k
        }
2230
217k
    }
2231
0
    else
2232
0
    {
2233
0
        if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
2234
0
            return (0);
2235
0
        sp->cinfo.c.comp_info[0].component_id = s;
2236
        /* jpeg_set_colorspace() set sampling factors to 1 */
2237
0
        if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0)
2238
0
        {
2239
0
            sp->cinfo.c.comp_info[0].quant_tbl_no = 1;
2240
0
            sp->cinfo.c.comp_info[0].dc_tbl_no = 1;
2241
0
            sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
2242
0
        }
2243
0
    }
2244
    /* ensure libjpeg won't write any extraneous markers */
2245
217k
    sp->cinfo.c.write_JFIF_header = FALSE;
2246
217k
    sp->cinfo.c.write_Adobe_marker = FALSE;
2247
    /* set up table handling correctly */
2248
    /* calling TIFFjpeg_set_quality() causes quantization tables to be flagged
2249
     */
2250
    /* as being to be emitted, which we don't want in the JPEGTABLESMODE_QUANT
2251
     */
2252
    /* mode, so we must manually suppress them. However TIFFjpeg_set_quality()
2253
     */
2254
    /* should really be called when dealing with files with directories with */
2255
    /* mixed qualities. see http://trac.osgeo.org/gdal/ticket/3539 */
2256
217k
    if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
2257
0
        return (0);
2258
217k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
2259
217k
    {
2260
217k
        suppress_quant_table(sp, 0);
2261
217k
        suppress_quant_table(sp, 1);
2262
217k
    }
2263
0
    else
2264
0
    {
2265
0
        unsuppress_quant_table(sp, 0);
2266
0
        unsuppress_quant_table(sp, 1);
2267
0
    }
2268
217k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
2269
108k
    {
2270
        /* Explicit suppression is only needed if we did not go through the */
2271
        /* prepare_JPEGTables() code path, which may be the case if updating */
2272
        /* an existing file */
2273
108k
        suppress_huff_table(sp, 0);
2274
108k
        suppress_huff_table(sp, 1);
2275
108k
        sp->cinfo.c.optimize_coding = FALSE;
2276
108k
    }
2277
108k
    else
2278
108k
        sp->cinfo.c.optimize_coding = TRUE;
2279
217k
    if (downsampled_input)
2280
0
    {
2281
        /* Need to use raw-data interface to libjpeg */
2282
0
        sp->cinfo.c.raw_data_in = TRUE;
2283
0
        tif->tif_encoderow = JPEGEncodeRaw;
2284
0
        tif->tif_encodestrip = JPEGEncodeRaw;
2285
0
        tif->tif_encodetile = JPEGEncodeRaw;
2286
0
    }
2287
217k
    else
2288
217k
    {
2289
        /* Use normal interface to libjpeg */
2290
217k
        sp->cinfo.c.raw_data_in = FALSE;
2291
217k
        tif->tif_encoderow = JPEGEncode;
2292
217k
        tif->tif_encodestrip = JPEGEncode;
2293
217k
        tif->tif_encodetile = JPEGEncode;
2294
217k
    }
2295
    /* Start JPEG compressor */
2296
217k
    if (!TIFFjpeg_start_compress(sp, FALSE))
2297
0
        return (0);
2298
    /* Allocate downsampled-data buffers if needed */
2299
217k
    if (downsampled_input)
2300
0
    {
2301
0
        if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info,
2302
0
                                       sp->cinfo.c.num_components))
2303
0
            return (0);
2304
0
    }
2305
217k
    sp->scancount = 0;
2306
2307
217k
    return (1);
2308
217k
}
2309
2310
/*
2311
 * Encode a chunk of pixels.
2312
 * "Standard" case: incoming data is not downsampled.
2313
 */
2314
static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
2315
428k
{
2316
428k
    JPEGState *sp = JState(tif);
2317
428k
    tmsize_t nrows;
2318
428k
    TIFF_JSAMPROW bufptr[1];
2319
428k
    short *line16 = NULL;
2320
428k
    int line16_count = 0;
2321
2322
428k
    (void)s;
2323
428k
    assert(sp != NULL);
2324
    /* data is expected to be supplied in multiples of a scanline */
2325
428k
    nrows = cc / sp->bytesperline;
2326
428k
    if (cc % sp->bytesperline)
2327
0
        TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded");
2328
2329
    /* The last strip will be limited to image size */
2330
428k
    if (!isTiled(tif) && tif->tif_row + nrows > tif->tif_dir.td_imagelength)
2331
0
        nrows = tif->tif_dir.td_imagelength - tif->tif_row;
2332
2333
428k
    if (sp->cinfo.c.data_precision == 12)
2334
217k
    {
2335
217k
        line16_count = (int)((sp->bytesperline * 2) / 3);
2336
217k
        line16 = (short *)_TIFFmallocExt(tif, sizeof(short) * line16_count);
2337
217k
        if (!line16)
2338
0
        {
2339
0
            TIFFErrorExtR(tif, "JPEGEncode", "Failed to allocate memory");
2340
2341
0
            return 0;
2342
0
        }
2343
217k
    }
2344
2345
5.89M
    while (nrows-- > 0)
2346
5.46M
    {
2347
2348
5.46M
        if (sp->cinfo.c.data_precision == 12)
2349
3.47M
        {
2350
2351
3.47M
            int value_pairs = line16_count / 2;
2352
3.47M
            int iPair;
2353
2354
3.47M
            bufptr[0] = (TIFF_JSAMPROW)line16;
2355
2356
39.2M
            for (iPair = 0; iPair < value_pairs; iPair++)
2357
35.7M
            {
2358
35.7M
                unsigned char *in_ptr = ((unsigned char *)buf) + iPair * 3;
2359
35.7M
                TIFF_JSAMPLE *out_ptr = (TIFF_JSAMPLE *)(line16 + iPair * 2);
2360
2361
35.7M
                out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4);
2362
35.7M
                out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2];
2363
35.7M
            }
2364
3.47M
        }
2365
1.98M
        else
2366
1.98M
        {
2367
1.98M
            bufptr[0] = (TIFF_JSAMPROW)buf;
2368
1.98M
        }
2369
5.46M
        if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
2370
0
            return (0);
2371
5.46M
        if (nrows > 0)
2372
5.03M
            tif->tif_row++;
2373
5.46M
        buf += sp->bytesperline;
2374
5.46M
    }
2375
2376
428k
    if (sp->cinfo.c.data_precision == 12)
2377
217k
    {
2378
217k
        _TIFFfreeExt(tif, line16);
2379
217k
    }
2380
2381
428k
    return (1);
2382
428k
}
tif_jpeg.c:JPEGEncode
Line
Count
Source
2315
211k
{
2316
211k
    JPEGState *sp = JState(tif);
2317
211k
    tmsize_t nrows;
2318
211k
    TIFF_JSAMPROW bufptr[1];
2319
211k
    short *line16 = NULL;
2320
211k
    int line16_count = 0;
2321
2322
211k
    (void)s;
2323
211k
    assert(sp != NULL);
2324
    /* data is expected to be supplied in multiples of a scanline */
2325
211k
    nrows = cc / sp->bytesperline;
2326
211k
    if (cc % sp->bytesperline)
2327
0
        TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded");
2328
2329
    /* The last strip will be limited to image size */
2330
211k
    if (!isTiled(tif) && tif->tif_row + nrows > tif->tif_dir.td_imagelength)
2331
0
        nrows = tif->tif_dir.td_imagelength - tif->tif_row;
2332
2333
211k
    if (sp->cinfo.c.data_precision == 12)
2334
0
    {
2335
0
        line16_count = (int)((sp->bytesperline * 2) / 3);
2336
0
        line16 = (short *)_TIFFmallocExt(tif, sizeof(short) * line16_count);
2337
0
        if (!line16)
2338
0
        {
2339
0
            TIFFErrorExtR(tif, "JPEGEncode", "Failed to allocate memory");
2340
2341
0
            return 0;
2342
0
        }
2343
0
    }
2344
2345
2.20M
    while (nrows-- > 0)
2346
1.98M
    {
2347
2348
1.98M
        if (sp->cinfo.c.data_precision == 12)
2349
0
        {
2350
2351
0
            int value_pairs = line16_count / 2;
2352
0
            int iPair;
2353
2354
0
            bufptr[0] = (TIFF_JSAMPROW)line16;
2355
2356
0
            for (iPair = 0; iPair < value_pairs; iPair++)
2357
0
            {
2358
0
                unsigned char *in_ptr = ((unsigned char *)buf) + iPair * 3;
2359
0
                TIFF_JSAMPLE *out_ptr = (TIFF_JSAMPLE *)(line16 + iPair * 2);
2360
2361
0
                out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4);
2362
0
                out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2];
2363
0
            }
2364
0
        }
2365
1.98M
        else
2366
1.98M
        {
2367
1.98M
            bufptr[0] = (TIFF_JSAMPROW)buf;
2368
1.98M
        }
2369
1.98M
        if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
2370
0
            return (0);
2371
1.98M
        if (nrows > 0)
2372
1.77M
            tif->tif_row++;
2373
1.98M
        buf += sp->bytesperline;
2374
1.98M
    }
2375
2376
211k
    if (sp->cinfo.c.data_precision == 12)
2377
0
    {
2378
0
        _TIFFfreeExt(tif, line16);
2379
0
    }
2380
2381
211k
    return (1);
2382
211k
}
tif_jpeg_12.c:JPEGEncode
Line
Count
Source
2315
217k
{
2316
217k
    JPEGState *sp = JState(tif);
2317
217k
    tmsize_t nrows;
2318
217k
    TIFF_JSAMPROW bufptr[1];
2319
217k
    short *line16 = NULL;
2320
217k
    int line16_count = 0;
2321
2322
217k
    (void)s;
2323
217k
    assert(sp != NULL);
2324
    /* data is expected to be supplied in multiples of a scanline */
2325
217k
    nrows = cc / sp->bytesperline;
2326
217k
    if (cc % sp->bytesperline)
2327
0
        TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded");
2328
2329
    /* The last strip will be limited to image size */
2330
217k
    if (!isTiled(tif) && tif->tif_row + nrows > tif->tif_dir.td_imagelength)
2331
0
        nrows = tif->tif_dir.td_imagelength - tif->tif_row;
2332
2333
217k
    if (sp->cinfo.c.data_precision == 12)
2334
217k
    {
2335
217k
        line16_count = (int)((sp->bytesperline * 2) / 3);
2336
217k
        line16 = (short *)_TIFFmallocExt(tif, sizeof(short) * line16_count);
2337
217k
        if (!line16)
2338
0
        {
2339
0
            TIFFErrorExtR(tif, "JPEGEncode", "Failed to allocate memory");
2340
2341
0
            return 0;
2342
0
        }
2343
217k
    }
2344
2345
3.68M
    while (nrows-- > 0)
2346
3.47M
    {
2347
2348
3.47M
        if (sp->cinfo.c.data_precision == 12)
2349
3.47M
        {
2350
2351
3.47M
            int value_pairs = line16_count / 2;
2352
3.47M
            int iPair;
2353
2354
3.47M
            bufptr[0] = (TIFF_JSAMPROW)line16;
2355
2356
39.2M
            for (iPair = 0; iPair < value_pairs; iPair++)
2357
35.7M
            {
2358
35.7M
                unsigned char *in_ptr = ((unsigned char *)buf) + iPair * 3;
2359
35.7M
                TIFF_JSAMPLE *out_ptr = (TIFF_JSAMPLE *)(line16 + iPair * 2);
2360
2361
35.7M
                out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4);
2362
35.7M
                out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2];
2363
35.7M
            }
2364
3.47M
        }
2365
0
        else
2366
0
        {
2367
0
            bufptr[0] = (TIFF_JSAMPROW)buf;
2368
0
        }
2369
3.47M
        if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
2370
0
            return (0);
2371
3.47M
        if (nrows > 0)
2372
3.25M
            tif->tif_row++;
2373
3.47M
        buf += sp->bytesperline;
2374
3.47M
    }
2375
2376
217k
    if (sp->cinfo.c.data_precision == 12)
2377
217k
    {
2378
217k
        _TIFFfreeExt(tif, line16);
2379
217k
    }
2380
2381
217k
    return (1);
2382
217k
}
2383
2384
/*
2385
 * Encode a chunk of pixels.
2386
 * Incoming data is expected to be downsampled per sampling factors.
2387
 */
2388
static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
2389
0
{
2390
0
    JPEGState *sp = JState(tif);
2391
0
    TIFF_JSAMPLE *inptr;
2392
0
    TIFF_JSAMPLE *outptr;
2393
0
    tmsize_t nrows;
2394
0
    JDIMENSION clumps_per_line, nclump;
2395
0
    int clumpoffset, ci, xpos, ypos;
2396
0
    jpeg_component_info *compptr;
2397
0
    int samples_per_clump = sp->samplesperclump;
2398
0
    tmsize_t bytesperclumpline;
2399
2400
0
    (void)s;
2401
0
    assert(sp != NULL);
2402
    /* data is expected to be supplied in multiples of a clumpline */
2403
    /* a clumpline is equivalent to v_sampling desubsampled scanlines */
2404
    /* TODO: the following calculation of bytesperclumpline, should substitute
2405
     * calculation of sp->bytesperline, except that it is per v_sampling lines
2406
     */
2407
0
    bytesperclumpline =
2408
0
        ((((tmsize_t)sp->cinfo.c.image_width + sp->h_sampling - 1) /
2409
0
          sp->h_sampling) *
2410
0
             ((tmsize_t)sp->h_sampling * sp->v_sampling + 2) *
2411
0
             sp->cinfo.c.data_precision +
2412
0
         7) /
2413
0
        8;
2414
2415
0
    nrows = (cc / bytesperclumpline) * sp->v_sampling;
2416
0
    if (cc % bytesperclumpline)
2417
0
        TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded");
2418
2419
    /* Cb,Cr both have sampling factors 1, so this is correct */
2420
0
    clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width;
2421
2422
0
    while (nrows > 0)
2423
0
    {
2424
        /*
2425
         * Fastest way to separate the data is to make one pass
2426
         * over the scanline for each row of each component.
2427
         */
2428
0
        clumpoffset = 0; /* first sample in clump */
2429
0
        for (ci = 0, compptr = sp->cinfo.c.comp_info;
2430
0
             ci < sp->cinfo.c.num_components; ci++, compptr++)
2431
0
        {
2432
0
            int hsamp = compptr->h_samp_factor;
2433
0
            int vsamp = compptr->v_samp_factor;
2434
0
            int padding = (int)(compptr->width_in_blocks * DCTSIZE -
2435
0
                                clumps_per_line * hsamp);
2436
0
            for (ypos = 0; ypos < vsamp; ypos++)
2437
0
            {
2438
0
                inptr = ((TIFF_JSAMPLE *)buf) + clumpoffset;
2439
0
                outptr = sp->ds_buffer[ci][sp->scancount * vsamp + ypos];
2440
0
                if (hsamp == 1)
2441
0
                {
2442
                    /* fast path for at least Cb and Cr */
2443
0
                    for (nclump = clumps_per_line; nclump-- > 0;)
2444
0
                    {
2445
0
                        *outptr++ = inptr[0];
2446
0
                        inptr += samples_per_clump;
2447
0
                    }
2448
0
                }
2449
0
                else
2450
0
                {
2451
                    /* general case */
2452
0
                    for (nclump = clumps_per_line; nclump-- > 0;)
2453
0
                    {
2454
0
                        for (xpos = 0; xpos < hsamp; xpos++)
2455
0
                            *outptr++ = inptr[xpos];
2456
0
                        inptr += samples_per_clump;
2457
0
                    }
2458
0
                }
2459
                /* pad each scanline as needed */
2460
0
                for (xpos = 0; xpos < padding; xpos++)
2461
0
                {
2462
0
                    *outptr = outptr[-1];
2463
0
                    outptr++;
2464
0
                }
2465
0
                clumpoffset += hsamp;
2466
0
            }
2467
0
        }
2468
0
        sp->scancount++;
2469
0
        if (sp->scancount >= DCTSIZE)
2470
0
        {
2471
0
            int n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
2472
0
            if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
2473
0
                return (0);
2474
0
            sp->scancount = 0;
2475
0
        }
2476
0
        tif->tif_row += sp->v_sampling;
2477
0
        buf += bytesperclumpline;
2478
0
        nrows -= sp->v_sampling;
2479
0
    }
2480
0
    return (1);
2481
0
}
Unexecuted instantiation: tif_jpeg.c:JPEGEncodeRaw
Unexecuted instantiation: tif_jpeg_12.c:JPEGEncodeRaw
2482
2483
/*
2484
 * Finish up at the end of a strip or tile.
2485
 */
2486
static int JPEGPostEncode(TIFF *tif)
2487
428k
{
2488
428k
    JPEGState *sp = JState(tif);
2489
2490
428k
    if (sp->scancount > 0)
2491
0
    {
2492
        /*
2493
         * Need to emit a partial bufferload of downsampled data.
2494
         * Pad the data vertically.
2495
         */
2496
0
        int ci, ypos, n;
2497
0
        jpeg_component_info *compptr;
2498
2499
0
        for (ci = 0, compptr = sp->cinfo.c.comp_info;
2500
0
             ci < sp->cinfo.c.num_components; ci++, compptr++)
2501
0
        {
2502
0
            int vsamp = compptr->v_samp_factor;
2503
0
            tmsize_t row_width =
2504
0
                compptr->width_in_blocks * DCTSIZE * sizeof(JSAMPLE);
2505
0
            for (ypos = sp->scancount * vsamp; ypos < DCTSIZE * vsamp; ypos++)
2506
0
            {
2507
0
                _TIFFmemcpy((void *)sp->ds_buffer[ci][ypos],
2508
0
                            (void *)sp->ds_buffer[ci][ypos - 1], row_width);
2509
0
            }
2510
0
        }
2511
0
        n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
2512
0
        if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
2513
0
            return (0);
2514
0
    }
2515
2516
428k
    return (TIFFjpeg_finish_compress(JState(tif)));
2517
428k
}
tif_jpeg.c:JPEGPostEncode
Line
Count
Source
2487
211k
{
2488
211k
    JPEGState *sp = JState(tif);
2489
2490
211k
    if (sp->scancount > 0)
2491
0
    {
2492
        /*
2493
         * Need to emit a partial bufferload of downsampled data.
2494
         * Pad the data vertically.
2495
         */
2496
0
        int ci, ypos, n;
2497
0
        jpeg_component_info *compptr;
2498
2499
0
        for (ci = 0, compptr = sp->cinfo.c.comp_info;
2500
0
             ci < sp->cinfo.c.num_components; ci++, compptr++)
2501
0
        {
2502
0
            int vsamp = compptr->v_samp_factor;
2503
0
            tmsize_t row_width =
2504
0
                compptr->width_in_blocks * DCTSIZE * sizeof(JSAMPLE);
2505
0
            for (ypos = sp->scancount * vsamp; ypos < DCTSIZE * vsamp; ypos++)
2506
0
            {
2507
0
                _TIFFmemcpy((void *)sp->ds_buffer[ci][ypos],
2508
0
                            (void *)sp->ds_buffer[ci][ypos - 1], row_width);
2509
0
            }
2510
0
        }
2511
0
        n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
2512
0
        if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
2513
0
            return (0);
2514
0
    }
2515
2516
211k
    return (TIFFjpeg_finish_compress(JState(tif)));
2517
211k
}
tif_jpeg_12.c:JPEGPostEncode
Line
Count
Source
2487
217k
{
2488
217k
    JPEGState *sp = JState(tif);
2489
2490
217k
    if (sp->scancount > 0)
2491
0
    {
2492
        /*
2493
         * Need to emit a partial bufferload of downsampled data.
2494
         * Pad the data vertically.
2495
         */
2496
0
        int ci, ypos, n;
2497
0
        jpeg_component_info *compptr;
2498
2499
0
        for (ci = 0, compptr = sp->cinfo.c.comp_info;
2500
0
             ci < sp->cinfo.c.num_components; ci++, compptr++)
2501
0
        {
2502
0
            int vsamp = compptr->v_samp_factor;
2503
0
            tmsize_t row_width =
2504
0
                compptr->width_in_blocks * DCTSIZE * sizeof(JSAMPLE);
2505
0
            for (ypos = sp->scancount * vsamp; ypos < DCTSIZE * vsamp; ypos++)
2506
0
            {
2507
0
                _TIFFmemcpy((void *)sp->ds_buffer[ci][ypos],
2508
0
                            (void *)sp->ds_buffer[ci][ypos - 1], row_width);
2509
0
            }
2510
0
        }
2511
0
        n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
2512
0
        if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
2513
0
            return (0);
2514
0
    }
2515
2516
217k
    return (TIFFjpeg_finish_compress(JState(tif)));
2517
217k
}
2518
2519
static void JPEGCleanup(TIFF *tif)
2520
1.33M
{
2521
1.33M
    JPEGState *sp = JState(tif);
2522
2523
1.33M
    assert(sp != 0);
2524
2525
1.33M
    tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent;
2526
1.33M
    tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent;
2527
1.33M
    tif->tif_tagmethods.printdir = sp->otherSettings.printdir;
2528
1.33M
    if (sp->cinfo_initialized)
2529
637k
        TIFFjpeg_destroy(sp);         /* release libjpeg resources */
2530
1.33M
    if (sp->otherSettings.jpegtables) /* tag value */
2531
1.07M
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
2532
1.33M
    _TIFFfreeExt(tif, tif->tif_data); /* release local state */
2533
1.33M
    tif->tif_data = NULL;
2534
2535
1.33M
    _TIFFSetDefaultCompressionState(tif);
2536
1.33M
}
tif_jpeg.c:JPEGCleanup
Line
Count
Source
2520
1.11M
{
2521
1.11M
    JPEGState *sp = JState(tif);
2522
2523
1.11M
    assert(sp != 0);
2524
2525
1.11M
    tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent;
2526
1.11M
    tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent;
2527
1.11M
    tif->tif_tagmethods.printdir = sp->otherSettings.printdir;
2528
1.11M
    if (sp->cinfo_initialized)
2529
416k
        TIFFjpeg_destroy(sp);         /* release libjpeg resources */
2530
1.11M
    if (sp->otherSettings.jpegtables) /* tag value */
2531
852k
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
2532
1.11M
    _TIFFfreeExt(tif, tif->tif_data); /* release local state */
2533
1.11M
    tif->tif_data = NULL;
2534
2535
1.11M
    _TIFFSetDefaultCompressionState(tif);
2536
1.11M
}
tif_jpeg_12.c:JPEGCleanup
Line
Count
Source
2520
221k
{
2521
221k
    JPEGState *sp = JState(tif);
2522
2523
221k
    assert(sp != 0);
2524
2525
221k
    tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent;
2526
221k
    tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent;
2527
221k
    tif->tif_tagmethods.printdir = sp->otherSettings.printdir;
2528
221k
    if (sp->cinfo_initialized)
2529
221k
        TIFFjpeg_destroy(sp);         /* release libjpeg resources */
2530
221k
    if (sp->otherSettings.jpegtables) /* tag value */
2531
217k
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
2532
221k
    _TIFFfreeExt(tif, tif->tif_data); /* release local state */
2533
221k
    tif->tif_data = NULL;
2534
2535
221k
    _TIFFSetDefaultCompressionState(tif);
2536
221k
}
2537
2538
static void JPEGResetUpsampled(TIFF *tif)
2539
733k
{
2540
733k
    JPEGState *sp = JState(tif);
2541
733k
    TIFFDirectory *td = &tif->tif_dir;
2542
2543
    /*
2544
     * Mark whether returned data is up-sampled or not so TIFFStripSize
2545
     * and TIFFTileSize return values that reflect the true amount of
2546
     * data.
2547
     */
2548
733k
    tif->tif_flags &= ~TIFF_UPSAMPLED;
2549
733k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
2550
731k
    {
2551
731k
        if (td->td_photometric == PHOTOMETRIC_YCBCR &&
2552
731k
            sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
2553
50.2k
        {
2554
50.2k
            tif->tif_flags |= TIFF_UPSAMPLED;
2555
50.2k
        }
2556
681k
        else
2557
681k
        {
2558
#ifdef notdef
2559
            if (td->td_ycbcrsubsampling[0] != 1 ||
2560
                td->td_ycbcrsubsampling[1] != 1)
2561
                ; /* XXX what about up-sampling? */
2562
#endif
2563
681k
        }
2564
731k
    }
2565
2566
    /*
2567
     * Must recalculate cached tile size in case sampling state changed.
2568
     * Should we really be doing this now if image size isn't set?
2569
     */
2570
733k
    if (tif->tif_tilesize > 0)
2571
14.9k
        tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
2572
733k
    if (tif->tif_scanlinesize > 0)
2573
723k
        tif->tif_scanlinesize = TIFFScanlineSize(tif);
2574
733k
}
tif_jpeg.c:JPEGResetUpsampled
Line
Count
Source
2539
733k
{
2540
733k
    JPEGState *sp = JState(tif);
2541
733k
    TIFFDirectory *td = &tif->tif_dir;
2542
2543
    /*
2544
     * Mark whether returned data is up-sampled or not so TIFFStripSize
2545
     * and TIFFTileSize return values that reflect the true amount of
2546
     * data.
2547
     */
2548
733k
    tif->tif_flags &= ~TIFF_UPSAMPLED;
2549
733k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
2550
731k
    {
2551
731k
        if (td->td_photometric == PHOTOMETRIC_YCBCR &&
2552
731k
            sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
2553
50.2k
        {
2554
50.2k
            tif->tif_flags |= TIFF_UPSAMPLED;
2555
50.2k
        }
2556
681k
        else
2557
681k
        {
2558
#ifdef notdef
2559
            if (td->td_ycbcrsubsampling[0] != 1 ||
2560
                td->td_ycbcrsubsampling[1] != 1)
2561
                ; /* XXX what about up-sampling? */
2562
#endif
2563
681k
        }
2564
731k
    }
2565
2566
    /*
2567
     * Must recalculate cached tile size in case sampling state changed.
2568
     * Should we really be doing this now if image size isn't set?
2569
     */
2570
733k
    if (tif->tif_tilesize > 0)
2571
14.9k
        tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
2572
733k
    if (tif->tif_scanlinesize > 0)
2573
723k
        tif->tif_scanlinesize = TIFFScanlineSize(tif);
2574
733k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGResetUpsampled
2575
2576
static int JPEGVSetField(TIFF *tif, uint32_t tag, va_list ap)
2577
7.98M
{
2578
7.98M
    JPEGState *sp = JState(tif);
2579
7.98M
    const TIFFField *fip;
2580
7.98M
    uint32_t v32;
2581
2582
7.98M
    assert(sp != NULL);
2583
2584
7.98M
    switch (tag)
2585
7.98M
    {
2586
438k
        case TIFFTAG_JPEGTABLES:
2587
438k
            v32 = (uint32_t)va_arg(ap, uint32_t);
2588
438k
            if (v32 == 0)
2589
492
            {
2590
                /* XXX */
2591
492
                return (0);
2592
492
            }
2593
438k
            _TIFFsetByteArrayExt(tif, &sp->otherSettings.jpegtables,
2594
438k
                                 va_arg(ap, void *), v32);
2595
438k
            sp->otherSettings.jpegtables_length = v32;
2596
438k
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2597
438k
            break;
2598
948k
        case TIFFTAG_JPEGQUALITY:
2599
948k
            sp->otherSettings.jpegquality = (int)va_arg(ap, int);
2600
948k
            return (1); /* pseudo tag */
2601
50.9k
        case TIFFTAG_JPEGCOLORMODE:
2602
50.9k
            sp->otherSettings.jpegcolormode = (int)va_arg(ap, int);
2603
50.9k
            JPEGResetUpsampled(tif);
2604
50.9k
            return (1); /* pseudo tag */
2605
682k
        case TIFFTAG_PHOTOMETRIC:
2606
682k
        {
2607
682k
            int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap);
2608
682k
            JPEGResetUpsampled(tif);
2609
682k
            return ret_value;
2610
438k
        }
2611
948k
        case TIFFTAG_JPEGTABLESMODE:
2612
948k
            sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int);
2613
948k
            return (1); /* pseudo tag */
2614
29.1k
        case TIFFTAG_YCBCRSUBSAMPLING:
2615
            /* mark the fact that we have a real ycbcrsubsampling! */
2616
29.1k
            sp->otherSettings.ycbcrsampling_fetched = 1;
2617
            /* should we be recomputing upsampling info here? */
2618
29.1k
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2619
4.88M
        default:
2620
4.88M
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2621
7.98M
    }
2622
2623
438k
    if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
2624
438k
    {
2625
438k
        TIFFSetFieldBit(tif, fip->field_bit);
2626
438k
    }
2627
0
    else
2628
0
    {
2629
0
        return (0);
2630
0
    }
2631
2632
438k
    tif->tif_flags |= TIFF_DIRTYDIRECT;
2633
438k
    return (1);
2634
438k
}
tif_jpeg.c:JPEGVSetField
Line
Count
Source
2577
7.97M
{
2578
7.97M
    JPEGState *sp = JState(tif);
2579
7.97M
    const TIFFField *fip;
2580
7.97M
    uint32_t v32;
2581
2582
7.97M
    assert(sp != NULL);
2583
2584
7.97M
    switch (tag)
2585
7.97M
    {
2586
438k
        case TIFFTAG_JPEGTABLES:
2587
438k
            v32 = (uint32_t)va_arg(ap, uint32_t);
2588
438k
            if (v32 == 0)
2589
492
            {
2590
                /* XXX */
2591
492
                return (0);
2592
492
            }
2593
438k
            _TIFFsetByteArrayExt(tif, &sp->otherSettings.jpegtables,
2594
438k
                                 va_arg(ap, void *), v32);
2595
438k
            sp->otherSettings.jpegtables_length = v32;
2596
438k
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2597
438k
            break;
2598
948k
        case TIFFTAG_JPEGQUALITY:
2599
948k
            sp->otherSettings.jpegquality = (int)va_arg(ap, int);
2600
948k
            return (1); /* pseudo tag */
2601
50.9k
        case TIFFTAG_JPEGCOLORMODE:
2602
50.9k
            sp->otherSettings.jpegcolormode = (int)va_arg(ap, int);
2603
50.9k
            JPEGResetUpsampled(tif);
2604
50.9k
            return (1); /* pseudo tag */
2605
682k
        case TIFFTAG_PHOTOMETRIC:
2606
682k
        {
2607
682k
            int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap);
2608
682k
            JPEGResetUpsampled(tif);
2609
682k
            return ret_value;
2610
438k
        }
2611
948k
        case TIFFTAG_JPEGTABLESMODE:
2612
948k
            sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int);
2613
948k
            return (1); /* pseudo tag */
2614
29.1k
        case TIFFTAG_YCBCRSUBSAMPLING:
2615
            /* mark the fact that we have a real ycbcrsubsampling! */
2616
29.1k
            sp->otherSettings.ycbcrsampling_fetched = 1;
2617
            /* should we be recomputing upsampling info here? */
2618
29.1k
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2619
4.87M
        default:
2620
4.87M
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2621
7.97M
    }
2622
2623
438k
    if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
2624
438k
    {
2625
438k
        TIFFSetFieldBit(tif, fip->field_bit);
2626
438k
    }
2627
0
    else
2628
0
    {
2629
0
        return (0);
2630
0
    }
2631
2632
438k
    tif->tif_flags |= TIFF_DIRTYDIRECT;
2633
438k
    return (1);
2634
438k
}
tif_jpeg_12.c:JPEGVSetField
Line
Count
Source
2577
11.3k
{
2578
11.3k
    JPEGState *sp = JState(tif);
2579
11.3k
    const TIFFField *fip;
2580
11.3k
    uint32_t v32;
2581
2582
11.3k
    assert(sp != NULL);
2583
2584
11.3k
    switch (tag)
2585
11.3k
    {
2586
0
        case TIFFTAG_JPEGTABLES:
2587
0
            v32 = (uint32_t)va_arg(ap, uint32_t);
2588
0
            if (v32 == 0)
2589
0
            {
2590
                /* XXX */
2591
0
                return (0);
2592
0
            }
2593
0
            _TIFFsetByteArrayExt(tif, &sp->otherSettings.jpegtables,
2594
0
                                 va_arg(ap, void *), v32);
2595
0
            sp->otherSettings.jpegtables_length = v32;
2596
0
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2597
0
            break;
2598
0
        case TIFFTAG_JPEGQUALITY:
2599
0
            sp->otherSettings.jpegquality = (int)va_arg(ap, int);
2600
0
            return (1); /* pseudo tag */
2601
0
        case TIFFTAG_JPEGCOLORMODE:
2602
0
            sp->otherSettings.jpegcolormode = (int)va_arg(ap, int);
2603
0
            JPEGResetUpsampled(tif);
2604
0
            return (1); /* pseudo tag */
2605
0
        case TIFFTAG_PHOTOMETRIC:
2606
0
        {
2607
0
            int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap);
2608
0
            JPEGResetUpsampled(tif);
2609
0
            return ret_value;
2610
0
        }
2611
0
        case TIFFTAG_JPEGTABLESMODE:
2612
0
            sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int);
2613
0
            return (1); /* pseudo tag */
2614
0
        case TIFFTAG_YCBCRSUBSAMPLING:
2615
            /* mark the fact that we have a real ycbcrsubsampling! */
2616
0
            sp->otherSettings.ycbcrsampling_fetched = 1;
2617
            /* should we be recomputing upsampling info here? */
2618
0
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2619
11.3k
        default:
2620
11.3k
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2621
11.3k
    }
2622
2623
0
    if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
2624
0
    {
2625
0
        TIFFSetFieldBit(tif, fip->field_bit);
2626
0
    }
2627
0
    else
2628
0
    {
2629
0
        return (0);
2630
0
    }
2631
2632
0
    tif->tif_flags |= TIFF_DIRTYDIRECT;
2633
0
    return (1);
2634
0
}
2635
2636
static int JPEGVGetField(TIFF *tif, uint32_t tag, va_list ap)
2637
4.14M
{
2638
4.14M
    JPEGState *sp = JState(tif);
2639
2640
4.14M
    assert(sp != NULL);
2641
2642
4.14M
    switch (tag)
2643
4.14M
    {
2644
1.29M
        case TIFFTAG_JPEGTABLES:
2645
1.29M
            *va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length;
2646
1.29M
            *va_arg(ap, const void **) = sp->otherSettings.jpegtables;
2647
1.29M
            break;
2648
0
        case TIFFTAG_JPEGQUALITY:
2649
0
            *va_arg(ap, int *) = sp->otherSettings.jpegquality;
2650
0
            break;
2651
22.4k
        case TIFFTAG_JPEGCOLORMODE:
2652
22.4k
            *va_arg(ap, int *) = sp->otherSettings.jpegcolormode;
2653
22.4k
            break;
2654
316k
        case TIFFTAG_JPEGTABLESMODE:
2655
316k
            *va_arg(ap, int *) = sp->otherSettings.jpegtablesmode;
2656
316k
            break;
2657
2.51M
        default:
2658
2.51M
            return (*sp->otherSettings.vgetparent)(tif, tag, ap);
2659
4.14M
    }
2660
1.62M
    return (1);
2661
4.14M
}
tif_jpeg.c:JPEGVGetField
Line
Count
Source
2637
3.65M
{
2638
3.65M
    JPEGState *sp = JState(tif);
2639
2640
3.65M
    assert(sp != NULL);
2641
2642
3.65M
    switch (tag)
2643
3.65M
    {
2644
856k
        case TIFFTAG_JPEGTABLES:
2645
856k
            *va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length;
2646
856k
            *va_arg(ap, const void **) = sp->otherSettings.jpegtables;
2647
856k
            break;
2648
0
        case TIFFTAG_JPEGQUALITY:
2649
0
            *va_arg(ap, int *) = sp->otherSettings.jpegquality;
2650
0
            break;
2651
22.4k
        case TIFFTAG_JPEGCOLORMODE:
2652
22.4k
            *va_arg(ap, int *) = sp->otherSettings.jpegcolormode;
2653
22.4k
            break;
2654
316k
        case TIFFTAG_JPEGTABLESMODE:
2655
316k
            *va_arg(ap, int *) = sp->otherSettings.jpegtablesmode;
2656
316k
            break;
2657
2.46M
        default:
2658
2.46M
            return (*sp->otherSettings.vgetparent)(tif, tag, ap);
2659
3.65M
    }
2660
1.19M
    return (1);
2661
3.65M
}
tif_jpeg_12.c:JPEGVGetField
Line
Count
Source
2637
489k
{
2638
489k
    JPEGState *sp = JState(tif);
2639
2640
489k
    assert(sp != NULL);
2641
2642
489k
    switch (tag)
2643
489k
    {
2644
434k
        case TIFFTAG_JPEGTABLES:
2645
434k
            *va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length;
2646
434k
            *va_arg(ap, const void **) = sp->otherSettings.jpegtables;
2647
434k
            break;
2648
0
        case TIFFTAG_JPEGQUALITY:
2649
0
            *va_arg(ap, int *) = sp->otherSettings.jpegquality;
2650
0
            break;
2651
0
        case TIFFTAG_JPEGCOLORMODE:
2652
0
            *va_arg(ap, int *) = sp->otherSettings.jpegcolormode;
2653
0
            break;
2654
0
        case TIFFTAG_JPEGTABLESMODE:
2655
0
            *va_arg(ap, int *) = sp->otherSettings.jpegtablesmode;
2656
0
            break;
2657
55.5k
        default:
2658
55.5k
            return (*sp->otherSettings.vgetparent)(tif, tag, ap);
2659
489k
    }
2660
434k
    return (1);
2661
489k
}
2662
2663
static void JPEGPrintDir(TIFF *tif, FILE *fd, long flags)
2664
0
{
2665
0
    JPEGState *sp = JState(tif);
2666
2667
0
    assert(sp != NULL);
2668
0
    (void)flags;
2669
2670
0
    if (sp != NULL)
2671
0
    {
2672
0
        if (TIFFFieldSet(tif, FIELD_JPEGTABLES))
2673
0
            fprintf(fd, "  JPEG Tables: (%" PRIu32 " bytes)\n",
2674
0
                    sp->otherSettings.jpegtables_length);
2675
0
        if (sp->otherSettings.printdir)
2676
0
            (*sp->otherSettings.printdir)(tif, fd, flags);
2677
0
    }
2678
0
}
Unexecuted instantiation: tif_jpeg.c:JPEGPrintDir
Unexecuted instantiation: tif_jpeg_12.c:JPEGPrintDir
2679
2680
static uint32_t JPEGDefaultStripSize(TIFF *tif, uint32_t s)
2681
0
{
2682
0
    JPEGState *sp = JState(tif);
2683
0
    TIFFDirectory *td = &tif->tif_dir;
2684
2685
0
    s = (*sp->otherSettings.defsparent)(tif, s);
2686
0
    if (s < td->td_imagelength)
2687
0
        s = TIFFroundup_32(s, td->td_ycbcrsubsampling[1] * DCTSIZE);
2688
0
    return (s);
2689
0
}
Unexecuted instantiation: tif_jpeg.c:JPEGDefaultStripSize
Unexecuted instantiation: tif_jpeg_12.c:JPEGDefaultStripSize
2690
2691
static void JPEGDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
2692
0
{
2693
0
    JPEGState *sp = JState(tif);
2694
0
    TIFFDirectory *td = &tif->tif_dir;
2695
2696
0
    (*sp->otherSettings.deftparent)(tif, tw, th);
2697
0
    *tw = TIFFroundup_32(*tw, td->td_ycbcrsubsampling[0] * DCTSIZE);
2698
0
    *th = TIFFroundup_32(*th, td->td_ycbcrsubsampling[1] * DCTSIZE);
2699
0
}
Unexecuted instantiation: tif_jpeg.c:JPEGDefaultTileSize
Unexecuted instantiation: tif_jpeg_12.c:JPEGDefaultTileSize
2700
2701
/*
2702
 * The JPEG library initialized used to be done in TIFFInitJPEG(), but
2703
 * now that we allow a TIFF file to be opened in update mode it is necessary
2704
 * to have some way of deciding whether compression or decompression is
2705
 * desired other than looking at tif->tif_mode.  We accomplish this by
2706
 * examining {TILE/STRIP}BYTECOUNTS to see if there is a non-zero entry.
2707
 * If so, we assume decompression is desired.
2708
 *
2709
 * This is tricky, because TIFFInitJPEG() is called while the directory is
2710
 * being read, and generally speaking the BYTECOUNTS tag won't have been read
2711
 * at that point.  So we try to defer jpeg library initialization till we
2712
 * do have that tag ... basically any access that might require the compressor
2713
 * or decompressor that occurs after the reading of the directory.
2714
 *
2715
 * In an ideal world compressors or decompressors would be setup
2716
 * at the point where a single tile or strip was accessed (for read or write)
2717
 * so that stuff like update of missing tiles, or replacement of tiles could
2718
 * be done. However, we aren't trying to crack that nut just yet ...
2719
 *
2720
 * NFW, Feb 3rd, 2003.
2721
 */
2722
2723
static int JPEGInitializeLibJPEG(TIFF *tif, int decompress)
2724
637k
{
2725
637k
    JPEGState *sp = JState(tif);
2726
2727
637k
    if (sp->cinfo_initialized)
2728
227
    {
2729
227
        if (!decompress && sp->cinfo.comm.is_decompressor)
2730
0
            TIFFjpeg_destroy(sp);
2731
227
        else if (decompress && !sp->cinfo.comm.is_decompressor)
2732
0
            TIFFjpeg_destroy(sp);
2733
227
        else
2734
227
            return 1;
2735
2736
0
        sp->cinfo_initialized = 0;
2737
0
    }
2738
2739
    /*
2740
     * Initialize libjpeg.
2741
     */
2742
637k
    if (decompress)
2743
5.12k
    {
2744
5.12k
        if (!TIFFjpeg_create_decompress(sp))
2745
0
            return (0);
2746
5.12k
    }
2747
632k
    else
2748
632k
    {
2749
632k
        if (!TIFFjpeg_create_compress(sp))
2750
0
            return (0);
2751
632k
#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE
2752
632k
#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024)
2753
632k
#endif
2754
        /* libjpeg turbo 1.5.2 honours max_memory_to_use, but has no backing */
2755
        /* store implementation, so better not set max_memory_to_use ourselves.
2756
         */
2757
        /* See https://github.com/libjpeg-turbo/libjpeg-turbo/issues/162 */
2758
632k
        if (sp->cinfo.c.mem->max_memory_to_use > 0)
2759
0
        {
2760
            /* This is to address bug related in ticket GDAL #1795. */
2761
0
            if (getenv("JPEGMEM") == NULL)
2762
0
            {
2763
                /* Increase the max memory usable. This helps when creating
2764
                 * files */
2765
                /* with "big" tile, without using libjpeg temporary files. */
2766
                /* For example a 512x512 tile with 3 bands */
2767
                /* requires 1.5 MB which is above libjpeg 1MB default */
2768
0
                if (sp->cinfo.c.mem->max_memory_to_use <
2769
0
                    TIFF_JPEG_MAX_MEMORY_TO_USE)
2770
0
                    sp->cinfo.c.mem->max_memory_to_use =
2771
0
                        TIFF_JPEG_MAX_MEMORY_TO_USE;
2772
0
            }
2773
0
        }
2774
632k
    }
2775
2776
637k
    sp->cinfo_initialized = TRUE;
2777
2778
637k
    return 1;
2779
637k
}
tif_jpeg.c:JPEGInitializeLibJPEG
Line
Count
Source
2724
416k
{
2725
416k
    JPEGState *sp = JState(tif);
2726
2727
416k
    if (sp->cinfo_initialized)
2728
113
    {
2729
113
        if (!decompress && sp->cinfo.comm.is_decompressor)
2730
0
            TIFFjpeg_destroy(sp);
2731
113
        else if (decompress && !sp->cinfo.comm.is_decompressor)
2732
0
            TIFFjpeg_destroy(sp);
2733
113
        else
2734
113
            return 1;
2735
2736
0
        sp->cinfo_initialized = 0;
2737
0
    }
2738
2739
    /*
2740
     * Initialize libjpeg.
2741
     */
2742
416k
    if (decompress)
2743
1.09k
    {
2744
1.09k
        if (!TIFFjpeg_create_decompress(sp))
2745
0
            return (0);
2746
1.09k
    }
2747
415k
    else
2748
415k
    {
2749
415k
        if (!TIFFjpeg_create_compress(sp))
2750
0
            return (0);
2751
415k
#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE
2752
415k
#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024)
2753
415k
#endif
2754
        /* libjpeg turbo 1.5.2 honours max_memory_to_use, but has no backing */
2755
        /* store implementation, so better not set max_memory_to_use ourselves.
2756
         */
2757
        /* See https://github.com/libjpeg-turbo/libjpeg-turbo/issues/162 */
2758
415k
        if (sp->cinfo.c.mem->max_memory_to_use > 0)
2759
0
        {
2760
            /* This is to address bug related in ticket GDAL #1795. */
2761
0
            if (getenv("JPEGMEM") == NULL)
2762
0
            {
2763
                /* Increase the max memory usable. This helps when creating
2764
                 * files */
2765
                /* with "big" tile, without using libjpeg temporary files. */
2766
                /* For example a 512x512 tile with 3 bands */
2767
                /* requires 1.5 MB which is above libjpeg 1MB default */
2768
0
                if (sp->cinfo.c.mem->max_memory_to_use <
2769
0
                    TIFF_JPEG_MAX_MEMORY_TO_USE)
2770
0
                    sp->cinfo.c.mem->max_memory_to_use =
2771
0
                        TIFF_JPEG_MAX_MEMORY_TO_USE;
2772
0
            }
2773
0
        }
2774
415k
    }
2775
2776
416k
    sp->cinfo_initialized = TRUE;
2777
2778
416k
    return 1;
2779
416k
}
tif_jpeg_12.c:JPEGInitializeLibJPEG
Line
Count
Source
2724
221k
{
2725
221k
    JPEGState *sp = JState(tif);
2726
2727
221k
    if (sp->cinfo_initialized)
2728
114
    {
2729
114
        if (!decompress && sp->cinfo.comm.is_decompressor)
2730
0
            TIFFjpeg_destroy(sp);
2731
114
        else if (decompress && !sp->cinfo.comm.is_decompressor)
2732
0
            TIFFjpeg_destroy(sp);
2733
114
        else
2734
114
            return 1;
2735
2736
0
        sp->cinfo_initialized = 0;
2737
0
    }
2738
2739
    /*
2740
     * Initialize libjpeg.
2741
     */
2742
221k
    if (decompress)
2743
4.03k
    {
2744
4.03k
        if (!TIFFjpeg_create_decompress(sp))
2745
0
            return (0);
2746
4.03k
    }
2747
217k
    else
2748
217k
    {
2749
217k
        if (!TIFFjpeg_create_compress(sp))
2750
0
            return (0);
2751
217k
#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE
2752
217k
#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024)
2753
217k
#endif
2754
        /* libjpeg turbo 1.5.2 honours max_memory_to_use, but has no backing */
2755
        /* store implementation, so better not set max_memory_to_use ourselves.
2756
         */
2757
        /* See https://github.com/libjpeg-turbo/libjpeg-turbo/issues/162 */
2758
217k
        if (sp->cinfo.c.mem->max_memory_to_use > 0)
2759
0
        {
2760
            /* This is to address bug related in ticket GDAL #1795. */
2761
0
            if (getenv("JPEGMEM") == NULL)
2762
0
            {
2763
                /* Increase the max memory usable. This helps when creating
2764
                 * files */
2765
                /* with "big" tile, without using libjpeg temporary files. */
2766
                /* For example a 512x512 tile with 3 bands */
2767
                /* requires 1.5 MB which is above libjpeg 1MB default */
2768
0
                if (sp->cinfo.c.mem->max_memory_to_use <
2769
0
                    TIFF_JPEG_MAX_MEMORY_TO_USE)
2770
0
                    sp->cinfo.c.mem->max_memory_to_use =
2771
0
                        TIFF_JPEG_MAX_MEMORY_TO_USE;
2772
0
            }
2773
0
        }
2774
217k
    }
2775
2776
221k
    sp->cinfo_initialized = TRUE;
2777
2778
221k
    return 1;
2779
221k
}
2780
2781
/* Common to tif_jpeg.c and tif_jpeg_12.c */
2782
static void TIFFInitJPEGCommon(TIFF *tif)
2783
1.55M
{
2784
1.55M
    JPEGState *sp;
2785
2786
1.55M
    sp = JState(tif);
2787
1.55M
    sp->tif = tif; /* back link */
2788
2789
    /* Default values for codec-specific fields */
2790
1.55M
    sp->otherSettings.jpegtables = NULL;
2791
1.55M
    sp->otherSettings.jpegtables_length = 0;
2792
1.55M
    sp->otherSettings.jpegquality = 75; /* Default IJG quality */
2793
1.55M
    sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW;
2794
1.55M
    sp->otherSettings.jpegtablesmode =
2795
1.55M
        JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF;
2796
1.55M
    sp->otherSettings.ycbcrsampling_fetched = 0;
2797
2798
1.55M
    tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
2799
1.55M
    tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
2800
1.55M
    tif->tif_tagmethods.printdir = JPEGPrintDir;   /* hook for codec tags */
2801
2802
    /*
2803
     * Install codec methods.
2804
     */
2805
1.55M
    tif->tif_fixuptags = JPEGFixupTags;
2806
1.55M
    tif->tif_setupdecode = JPEGSetupDecode;
2807
1.55M
    tif->tif_predecode = JPEGPreDecode;
2808
1.55M
    tif->tif_decoderow = JPEGDecode;
2809
1.55M
    tif->tif_decodestrip = JPEGDecode;
2810
1.55M
    tif->tif_decodetile = JPEGDecode;
2811
1.55M
    tif->tif_setupencode = JPEGSetupEncode;
2812
1.55M
    tif->tif_preencode = JPEGPreEncode;
2813
1.55M
    tif->tif_postencode = JPEGPostEncode;
2814
1.55M
    tif->tif_encoderow = JPEGEncode;
2815
1.55M
    tif->tif_encodestrip = JPEGEncode;
2816
1.55M
    tif->tif_encodetile = JPEGEncode;
2817
1.55M
    tif->tif_cleanup = JPEGCleanup;
2818
2819
1.55M
    tif->tif_defstripsize = JPEGDefaultStripSize;
2820
1.55M
    tif->tif_deftilesize = JPEGDefaultTileSize;
2821
1.55M
    tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */
2822
1.55M
    sp->cinfo_initialized = FALSE;
2823
1.55M
}
tif_jpeg.c:TIFFInitJPEGCommon
Line
Count
Source
2783
1.33M
{
2784
1.33M
    JPEGState *sp;
2785
2786
1.33M
    sp = JState(tif);
2787
1.33M
    sp->tif = tif; /* back link */
2788
2789
    /* Default values for codec-specific fields */
2790
1.33M
    sp->otherSettings.jpegtables = NULL;
2791
1.33M
    sp->otherSettings.jpegtables_length = 0;
2792
1.33M
    sp->otherSettings.jpegquality = 75; /* Default IJG quality */
2793
1.33M
    sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW;
2794
1.33M
    sp->otherSettings.jpegtablesmode =
2795
1.33M
        JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF;
2796
1.33M
    sp->otherSettings.ycbcrsampling_fetched = 0;
2797
2798
1.33M
    tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
2799
1.33M
    tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
2800
1.33M
    tif->tif_tagmethods.printdir = JPEGPrintDir;   /* hook for codec tags */
2801
2802
    /*
2803
     * Install codec methods.
2804
     */
2805
1.33M
    tif->tif_fixuptags = JPEGFixupTags;
2806
1.33M
    tif->tif_setupdecode = JPEGSetupDecode;
2807
1.33M
    tif->tif_predecode = JPEGPreDecode;
2808
1.33M
    tif->tif_decoderow = JPEGDecode;
2809
1.33M
    tif->tif_decodestrip = JPEGDecode;
2810
1.33M
    tif->tif_decodetile = JPEGDecode;
2811
1.33M
    tif->tif_setupencode = JPEGSetupEncode;
2812
1.33M
    tif->tif_preencode = JPEGPreEncode;
2813
1.33M
    tif->tif_postencode = JPEGPostEncode;
2814
1.33M
    tif->tif_encoderow = JPEGEncode;
2815
1.33M
    tif->tif_encodestrip = JPEGEncode;
2816
1.33M
    tif->tif_encodetile = JPEGEncode;
2817
1.33M
    tif->tif_cleanup = JPEGCleanup;
2818
2819
1.33M
    tif->tif_defstripsize = JPEGDefaultStripSize;
2820
1.33M
    tif->tif_deftilesize = JPEGDefaultTileSize;
2821
1.33M
    tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */
2822
1.33M
    sp->cinfo_initialized = FALSE;
2823
1.33M
}
tif_jpeg_12.c:TIFFInitJPEGCommon
Line
Count
Source
2783
221k
{
2784
221k
    JPEGState *sp;
2785
2786
221k
    sp = JState(tif);
2787
221k
    sp->tif = tif; /* back link */
2788
2789
    /* Default values for codec-specific fields */
2790
221k
    sp->otherSettings.jpegtables = NULL;
2791
221k
    sp->otherSettings.jpegtables_length = 0;
2792
221k
    sp->otherSettings.jpegquality = 75; /* Default IJG quality */
2793
221k
    sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW;
2794
221k
    sp->otherSettings.jpegtablesmode =
2795
221k
        JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF;
2796
221k
    sp->otherSettings.ycbcrsampling_fetched = 0;
2797
2798
221k
    tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
2799
221k
    tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
2800
221k
    tif->tif_tagmethods.printdir = JPEGPrintDir;   /* hook for codec tags */
2801
2802
    /*
2803
     * Install codec methods.
2804
     */
2805
221k
    tif->tif_fixuptags = JPEGFixupTags;
2806
221k
    tif->tif_setupdecode = JPEGSetupDecode;
2807
221k
    tif->tif_predecode = JPEGPreDecode;
2808
221k
    tif->tif_decoderow = JPEGDecode;
2809
221k
    tif->tif_decodestrip = JPEGDecode;
2810
221k
    tif->tif_decodetile = JPEGDecode;
2811
221k
    tif->tif_setupencode = JPEGSetupEncode;
2812
221k
    tif->tif_preencode = JPEGPreEncode;
2813
221k
    tif->tif_postencode = JPEGPostEncode;
2814
221k
    tif->tif_encoderow = JPEGEncode;
2815
221k
    tif->tif_encodestrip = JPEGEncode;
2816
221k
    tif->tif_encodetile = JPEGEncode;
2817
221k
    tif->tif_cleanup = JPEGCleanup;
2818
2819
221k
    tif->tif_defstripsize = JPEGDefaultStripSize;
2820
221k
    tif->tif_deftilesize = JPEGDefaultTileSize;
2821
221k
    tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */
2822
221k
    sp->cinfo_initialized = FALSE;
2823
221k
}
2824
2825
int TIFFInitJPEG(TIFF *tif, int scheme)
2826
1.33M
{
2827
1.33M
    JPEGState *sp;
2828
2829
1.33M
    (void)scheme;
2830
1.33M
    assert(scheme == COMPRESSION_JPEG);
2831
2832
    /*
2833
     * Merge codec-specific tag information.
2834
     */
2835
1.33M
    if (!_TIFFMergeFields(tif, jpegFields, TIFFArrayCount(jpegFields)))
2836
0
    {
2837
0
        TIFFErrorExtR(tif, "TIFFInitJPEG",
2838
0
                      "Merging JPEG codec-specific tags failed");
2839
0
        return 0;
2840
0
    }
2841
2842
    /*
2843
     * Allocate state block so tag methods have storage to record values.
2844
     */
2845
1.33M
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(JPEGState));
2846
2847
1.33M
    if (tif->tif_data == NULL)
2848
0
    {
2849
0
        TIFFErrorExtR(tif, "TIFFInitJPEG", "No space for JPEG state block");
2850
0
        return 0;
2851
0
    }
2852
1.33M
    _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState));
2853
2854
1.33M
    sp = JState(tif);
2855
    /*
2856
     * Override parent get/set field methods.
2857
     */
2858
1.33M
    sp->otherSettings.vgetparent = tif->tif_tagmethods.vgetfield;
2859
1.33M
    sp->otherSettings.vsetparent = tif->tif_tagmethods.vsetfield;
2860
1.33M
    sp->otherSettings.printdir = tif->tif_tagmethods.printdir;
2861
2862
1.33M
    sp->otherSettings.defsparent = tif->tif_defstripsize;
2863
1.33M
    sp->otherSettings.deftparent = tif->tif_deftilesize;
2864
2865
1.33M
    TIFFInitJPEGCommon(tif);
2866
2867
    /*
2868
    ** Create a JPEGTables field if no directory has yet been created.
2869
    ** We do this just to ensure that sufficient space is reserved for
2870
    ** the JPEGTables field.  It will be properly created the right
2871
    ** size later.
2872
    */
2873
1.33M
    if (tif->tif_diroff == 0)
2874
632k
    {
2875
1.26M
#define SIZE_OF_JPEGTABLES 2000
2876
        /*
2877
        The following line assumes incorrectly that all JPEG-in-TIFF files will
2878
        have a JPEGTABLES tag generated and causes null-filled JPEGTABLES tags
2879
        to be written when the JPEG data is placed with TIFFWriteRawStrip.  The
2880
        field bit should be set, anyway, later when actual JPEGTABLES header is
2881
        generated, so removing it here hopefully is harmless.
2882
        TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2883
        */
2884
632k
        sp->otherSettings.jpegtables_length = SIZE_OF_JPEGTABLES;
2885
632k
        sp->otherSettings.jpegtables =
2886
632k
            (void *)_TIFFmallocExt(tif, sp->otherSettings.jpegtables_length);
2887
632k
        if (sp->otherSettings.jpegtables)
2888
632k
        {
2889
632k
            _TIFFmemset(sp->otherSettings.jpegtables, 0, SIZE_OF_JPEGTABLES);
2890
632k
        }
2891
0
        else
2892
0
        {
2893
0
            TIFFErrorExtR(tif, "TIFFInitJPEG",
2894
0
                          "Failed to allocate memory for JPEG tables");
2895
0
            return 0;
2896
0
        }
2897
632k
#undef SIZE_OF_JPEGTABLES
2898
632k
    }
2899
1.33M
    return 1;
2900
1.33M
}
gdal_TIFFInitJPEG
Line
Count
Source
2826
1.33M
{
2827
1.33M
    JPEGState *sp;
2828
2829
1.33M
    (void)scheme;
2830
1.33M
    assert(scheme == COMPRESSION_JPEG);
2831
2832
    /*
2833
     * Merge codec-specific tag information.
2834
     */
2835
1.33M
    if (!_TIFFMergeFields(tif, jpegFields, TIFFArrayCount(jpegFields)))
2836
0
    {
2837
0
        TIFFErrorExtR(tif, "TIFFInitJPEG",
2838
0
                      "Merging JPEG codec-specific tags failed");
2839
0
        return 0;
2840
0
    }
2841
2842
    /*
2843
     * Allocate state block so tag methods have storage to record values.
2844
     */
2845
1.33M
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(JPEGState));
2846
2847
1.33M
    if (tif->tif_data == NULL)
2848
0
    {
2849
0
        TIFFErrorExtR(tif, "TIFFInitJPEG", "No space for JPEG state block");
2850
0
        return 0;
2851
0
    }
2852
1.33M
    _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState));
2853
2854
1.33M
    sp = JState(tif);
2855
    /*
2856
     * Override parent get/set field methods.
2857
     */
2858
1.33M
    sp->otherSettings.vgetparent = tif->tif_tagmethods.vgetfield;
2859
1.33M
    sp->otherSettings.vsetparent = tif->tif_tagmethods.vsetfield;
2860
1.33M
    sp->otherSettings.printdir = tif->tif_tagmethods.printdir;
2861
2862
1.33M
    sp->otherSettings.defsparent = tif->tif_defstripsize;
2863
1.33M
    sp->otherSettings.deftparent = tif->tif_deftilesize;
2864
2865
1.33M
    TIFFInitJPEGCommon(tif);
2866
2867
    /*
2868
    ** Create a JPEGTables field if no directory has yet been created.
2869
    ** We do this just to ensure that sufficient space is reserved for
2870
    ** the JPEGTables field.  It will be properly created the right
2871
    ** size later.
2872
    */
2873
1.33M
    if (tif->tif_diroff == 0)
2874
632k
    {
2875
632k
#define SIZE_OF_JPEGTABLES 2000
2876
        /*
2877
        The following line assumes incorrectly that all JPEG-in-TIFF files will
2878
        have a JPEGTABLES tag generated and causes null-filled JPEGTABLES tags
2879
        to be written when the JPEG data is placed with TIFFWriteRawStrip.  The
2880
        field bit should be set, anyway, later when actual JPEGTABLES header is
2881
        generated, so removing it here hopefully is harmless.
2882
        TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2883
        */
2884
632k
        sp->otherSettings.jpegtables_length = SIZE_OF_JPEGTABLES;
2885
632k
        sp->otherSettings.jpegtables =
2886
632k
            (void *)_TIFFmallocExt(tif, sp->otherSettings.jpegtables_length);
2887
632k
        if (sp->otherSettings.jpegtables)
2888
632k
        {
2889
632k
            _TIFFmemset(sp->otherSettings.jpegtables, 0, SIZE_OF_JPEGTABLES);
2890
632k
        }
2891
0
        else
2892
0
        {
2893
0
            TIFFErrorExtR(tif, "TIFFInitJPEG",
2894
0
                          "Failed to allocate memory for JPEG tables");
2895
0
            return 0;
2896
0
        }
2897
632k
#undef SIZE_OF_JPEGTABLES
2898
632k
    }
2899
1.33M
    return 1;
2900
1.33M
}
Unexecuted instantiation: gdal_TIFFInitJPEG_12
2901
#endif /* JPEG_SUPPORT */