Coverage Report

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