Coverage Report

Created: 2026-09-14 07:37

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