Coverage Report

Created: 2026-02-14 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/gtiff/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
#define BITS_IN_JSAMPLE 12
106
#define TIFF_JSAMPLE J12SAMPLE
107
#define TIFF_JSAMPARRAY J12SAMPARRAY
108
#define TIFF_JSAMPIMAGE J12SAMPIMAGE
109
#define TIFF_JSAMPROW J12SAMPROW
110
#else
111
#define BITS_IN_JSAMPLE 8
112
#define TIFF_JSAMPLE JSAMPLE
113
#define TIFF_JSAMPARRAY JSAMPARRAY
114
#define TIFF_JSAMPIMAGE JSAMPIMAGE
115
#define TIFF_JSAMPROW JSAMPROW
116
#endif
117
#else
118
0
#define TIFF_JSAMPLE JSAMPLE
119
0
#define TIFF_JSAMPARRAY JSAMPARRAY
120
#define TIFF_JSAMPIMAGE JSAMPIMAGE
121
145k
#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
10.5M
#define SETJMP(jbuf) setjmp(jbuf)
146
77.5k
#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
12.9M
#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
77.4k
{
257
77.4k
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
258
77.4k
    char buffer[JMSG_LENGTH_MAX];
259
260
77.4k
    (*cinfo->err->format_message)(cinfo, buffer);
261
77.4k
    TIFFErrorExtR(sp->tif, "JPEGLib", "%s",
262
77.4k
                  buffer);       /* display the error message */
263
77.4k
    jpeg_abort(cinfo);           /* clean up libjpeg state */
264
77.4k
    LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
265
77.4k
}
tif_jpeg.c:TIFFjpeg_error_exit
Line
Count
Source
256
67.5k
{
257
67.5k
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
258
67.5k
    char buffer[JMSG_LENGTH_MAX];
259
260
67.5k
    (*cinfo->err->format_message)(cinfo, buffer);
261
67.5k
    TIFFErrorExtR(sp->tif, "JPEGLib", "%s",
262
67.5k
                  buffer);       /* display the error message */
263
67.5k
    jpeg_abort(cinfo);           /* clean up libjpeg state */
264
67.5k
    LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
265
67.5k
}
tif_jpeg_12.c:TIFFjpeg_error_exit
Line
Count
Source
256
9.94k
{
257
9.94k
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
258
9.94k
    char buffer[JMSG_LENGTH_MAX];
259
260
9.94k
    (*cinfo->err->format_message)(cinfo, buffer);
261
9.94k
    TIFFErrorExtR(sp->tif, "JPEGLib", "%s",
262
9.94k
                  buffer);       /* display the error message */
263
9.94k
    jpeg_abort(cinfo);           /* clean up libjpeg state */
264
9.94k
    LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
265
9.94k
}
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
28.2k
{
274
28.2k
    char buffer[JMSG_LENGTH_MAX];
275
276
28.2k
    (*cinfo->err->format_message)(cinfo, buffer);
277
28.2k
    TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer);
278
28.2k
}
tif_jpeg.c:TIFFjpeg_output_message
Line
Count
Source
273
17.5k
{
274
17.5k
    char buffer[JMSG_LENGTH_MAX];
275
276
17.5k
    (*cinfo->err->format_message)(cinfo, buffer);
277
17.5k
    TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer);
278
17.5k
}
tif_jpeg_12.c:TIFFjpeg_output_message
Line
Count
Source
273
10.6k
{
274
10.6k
    char buffer[JMSG_LENGTH_MAX];
275
276
10.6k
    (*cinfo->err->format_message)(cinfo, buffer);
277
10.6k
    TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer);
278
10.6k
}
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
22.5M
{
287
22.5M
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
288
22.5M
    if (cinfo->is_decompressor)
289
22.5M
    {
290
22.5M
        const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number;
291
22.5M
        if (scan_no >= sp->otherSettings.max_allowed_scan_number)
292
41
        {
293
41
            TIFFErrorExtR(
294
41
                ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor",
295
41
                "Scan number %d exceeds maximum scans (%d). This limit "
296
41
                "can be raised through the "
297
41
                "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER "
298
41
                "environment variable.",
299
41
                scan_no, sp->otherSettings.max_allowed_scan_number);
300
301
41
            jpeg_abort(cinfo);           /* clean up libjpeg state */
302
41
            LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
303
41
        }
304
22.5M
    }
305
22.5M
}
tif_jpeg.c:TIFFjpeg_progress_monitor
Line
Count
Source
286
46.9k
{
287
46.9k
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
288
46.9k
    if (cinfo->is_decompressor)
289
46.9k
    {
290
46.9k
        const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number;
291
46.9k
        if (scan_no >= sp->otherSettings.max_allowed_scan_number)
292
18
        {
293
18
            TIFFErrorExtR(
294
18
                ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor",
295
18
                "Scan number %d exceeds maximum scans (%d). This limit "
296
18
                "can be raised through the "
297
18
                "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER "
298
18
                "environment variable.",
299
18
                scan_no, sp->otherSettings.max_allowed_scan_number);
300
301
18
            jpeg_abort(cinfo);           /* clean up libjpeg state */
302
18
            LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
303
18
        }
304
46.9k
    }
305
46.9k
}
tif_jpeg_12.c:TIFFjpeg_progress_monitor
Line
Count
Source
286
22.4M
{
287
22.4M
    JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */
288
22.4M
    if (cinfo->is_decompressor)
289
22.4M
    {
290
22.4M
        const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number;
291
22.4M
        if (scan_no >= sp->otherSettings.max_allowed_scan_number)
292
23
        {
293
23
            TIFFErrorExtR(
294
23
                ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor",
295
23
                "Scan number %d exceeds maximum scans (%d). This limit "
296
23
                "can be raised through the "
297
23
                "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER "
298
23
                "environment variable.",
299
23
                scan_no, sp->otherSettings.max_allowed_scan_number);
300
301
23
            jpeg_abort(cinfo);           /* clean up libjpeg state */
302
23
            LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */
303
23
        }
304
22.4M
    }
305
22.4M
}
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
10.5M
#define CALLJPEG(sp, fail, op) (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op))
314
2.62M
#define CALLVJPEG(sp, op) CALLJPEG(sp, 0, ((op), 1))
315
316
static int TIFFjpeg_create_compress(JPEGState *sp)
317
382k
{
318
    /* initialize JPEG error handling */
319
382k
    sp->cinfo.c.err = jpeg_std_error(&sp->err);
320
382k
    sp->err.error_exit = TIFFjpeg_error_exit;
321
382k
    sp->err.output_message = TIFFjpeg_output_message;
322
323
    /* set client_data to avoid UMR warning from tools like Purify */
324
382k
    sp->cinfo.c.client_data = NULL;
325
326
382k
    return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c));
327
382k
}
tif_jpeg.c:TIFFjpeg_create_compress
Line
Count
Source
317
264k
{
318
    /* initialize JPEG error handling */
319
264k
    sp->cinfo.c.err = jpeg_std_error(&sp->err);
320
264k
    sp->err.error_exit = TIFFjpeg_error_exit;
321
264k
    sp->err.output_message = TIFFjpeg_output_message;
322
323
    /* set client_data to avoid UMR warning from tools like Purify */
324
264k
    sp->cinfo.c.client_data = NULL;
325
326
    return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c));
327
264k
}
tif_jpeg_12.c:TIFFjpeg_create_compress
Line
Count
Source
317
118k
{
318
    /* initialize JPEG error handling */
319
118k
    sp->cinfo.c.err = jpeg_std_error(&sp->err);
320
118k
    sp->err.error_exit = TIFFjpeg_error_exit;
321
118k
    sp->err.output_message = TIFFjpeg_output_message;
322
323
    /* set client_data to avoid UMR warning from tools like Purify */
324
118k
    sp->cinfo.c.client_data = NULL;
325
326
    return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c));
327
118k
}
328
329
static int TIFFjpeg_create_decompress(JPEGState *sp)
330
4.74k
{
331
    /* initialize JPEG error handling */
332
4.74k
    sp->cinfo.d.err = jpeg_std_error(&sp->err);
333
4.74k
    sp->err.error_exit = TIFFjpeg_error_exit;
334
4.74k
    sp->err.output_message = TIFFjpeg_output_message;
335
336
    /* set client_data to avoid UMR warning from tools like Purify */
337
4.74k
    sp->cinfo.d.client_data = NULL;
338
339
4.74k
    return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d));
340
4.74k
}
tif_jpeg.c:TIFFjpeg_create_decompress
Line
Count
Source
330
593
{
331
    /* initialize JPEG error handling */
332
593
    sp->cinfo.d.err = jpeg_std_error(&sp->err);
333
593
    sp->err.error_exit = TIFFjpeg_error_exit;
334
593
    sp->err.output_message = TIFFjpeg_output_message;
335
336
    /* set client_data to avoid UMR warning from tools like Purify */
337
593
    sp->cinfo.d.client_data = NULL;
338
339
    return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d));
340
593
}
tif_jpeg_12.c:TIFFjpeg_create_decompress
Line
Count
Source
330
4.15k
{
331
    /* initialize JPEG error handling */
332
4.15k
    sp->cinfo.d.err = jpeg_std_error(&sp->err);
333
4.15k
    sp->err.error_exit = TIFFjpeg_error_exit;
334
4.15k
    sp->err.output_message = TIFFjpeg_output_message;
335
336
    /* set client_data to avoid UMR warning from tools like Purify */
337
4.15k
    sp->cinfo.d.client_data = NULL;
338
339
    return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d));
340
4.15k
}
341
342
static int TIFFjpeg_set_defaults(JPEGState *sp)
343
382k
{
344
382k
    return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c));
345
382k
}
tif_jpeg.c:TIFFjpeg_set_defaults
Line
Count
Source
343
264k
{
344
    return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c));
345
264k
}
tif_jpeg_12.c:TIFFjpeg_set_defaults
Line
Count
Source
343
118k
{
344
    return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c));
345
118k
}
346
347
static int TIFFjpeg_set_colorspace(JPEGState *sp, J_COLOR_SPACE colorspace)
348
263k
{
349
263k
    return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace));
350
263k
}
tif_jpeg.c:TIFFjpeg_set_colorspace
Line
Count
Source
348
145k
{
349
    return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace));
350
145k
}
tif_jpeg_12.c:TIFFjpeg_set_colorspace
Line
Count
Source
348
118k
{
349
    return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace));
350
118k
}
351
352
static int TIFFjpeg_set_quality(JPEGState *sp, int quality,
353
                                boolean force_baseline)
354
394k
{
355
394k
    return CALLVJPEG(sp,
356
394k
                     jpeg_set_quality(&sp->cinfo.c, quality, force_baseline));
357
394k
}
tif_jpeg.c:TIFFjpeg_set_quality
Line
Count
Source
354
217k
{
355
    return CALLVJPEG(sp,
356
217k
                     jpeg_set_quality(&sp->cinfo.c, quality, force_baseline));
357
217k
}
tif_jpeg_12.c:TIFFjpeg_set_quality
Line
Count
Source
354
177k
{
355
    return CALLVJPEG(sp,
356
177k
                     jpeg_set_quality(&sp->cinfo.c, quality, force_baseline));
357
177k
}
358
359
static int TIFFjpeg_suppress_tables(JPEGState *sp, boolean suppress)
360
131k
{
361
131k
    return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress));
362
131k
}
tif_jpeg.c:TIFFjpeg_suppress_tables
Line
Count
Source
360
72.6k
{
361
    return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress));
362
72.6k
}
tif_jpeg_12.c:TIFFjpeg_suppress_tables
Line
Count
Source
360
59.0k
{
361
    return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress));
362
59.0k
}
363
364
static int TIFFjpeg_start_compress(JPEGState *sp, boolean write_all_tables)
365
263k
{
366
263k
    return CALLVJPEG(sp, jpeg_start_compress(&sp->cinfo.c, write_all_tables));
367
263k
}
tif_jpeg.c:TIFFjpeg_start_compress
Line
Count
Source
365
145k
{
366
    return CALLVJPEG(sp, jpeg_start_compress(&sp->cinfo.c, write_all_tables));
367
145k
}
tif_jpeg_12.c:TIFFjpeg_start_compress
Line
Count
Source
365
118k
{
366
    return CALLVJPEG(sp, jpeg_start_compress(&sp->cinfo.c, write_all_tables));
367
118k
}
368
369
static int TIFFjpeg_write_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines,
370
                                    int num_lines)
371
3.29M
{
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
3.29M
    return CALLJPEG(sp, -1,
378
3.29M
                    (int)jpeg_write_scanlines(&sp->cinfo.c, scanlines,
379
3.29M
                                              (JDIMENSION)num_lines));
380
3.29M
#endif
381
3.29M
}
tif_jpeg.c:TIFFjpeg_write_scanlines
Line
Count
Source
371
1.40M
{
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
1.40M
                    (int)jpeg_write_scanlines(&sp->cinfo.c, scanlines,
379
1.40M
                                              (JDIMENSION)num_lines));
380
1.40M
#endif
381
1.40M
}
tif_jpeg_12.c:TIFFjpeg_write_scanlines
Line
Count
Source
371
1.88M
{
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
1.88M
                    (int)jpeg_write_scanlines(&sp->cinfo.c, scanlines,
379
1.88M
                                              (JDIMENSION)num_lines));
380
1.88M
#endif
381
1.88M
}
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
    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
0
        sp, -1,
393
0
        (int)jpeg_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines));
394
0
#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
263k
{
399
263k
    return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c));
400
263k
}
tif_jpeg.c:TIFFjpeg_finish_compress
Line
Count
Source
398
145k
{
399
    return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c));
400
145k
}
tif_jpeg_12.c:TIFFjpeg_finish_compress
Line
Count
Source
398
118k
{
399
    return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c));
400
118k
}
401
402
static int TIFFjpeg_write_tables(JPEGState *sp)
403
131k
{
404
131k
    return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c));
405
131k
}
tif_jpeg.c:TIFFjpeg_write_tables
Line
Count
Source
403
72.6k
{
404
    return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c));
405
72.6k
}
tif_jpeg_12.c:TIFFjpeg_write_tables
Line
Count
Source
403
59.0k
{
404
    return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c));
405
59.0k
}
406
407
static int TIFFjpeg_read_header(JPEGState *sp, boolean require_image)
408
14.2k
{
409
14.2k
    return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image));
410
14.2k
}
tif_jpeg.c:TIFFjpeg_read_header
Line
Count
Source
408
2.39k
{
409
    return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image));
410
2.39k
}
tif_jpeg_12.c:TIFFjpeg_read_header
Line
Count
Source
408
11.8k
{
409
    return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image));
410
11.8k
}
411
412
static int TIFFjpeg_has_multiple_scans(JPEGState *sp)
413
3.08k
{
414
3.08k
    return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d));
415
3.08k
}
tif_jpeg.c:TIFFjpeg_has_multiple_scans
Line
Count
Source
413
450
{
414
    return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d));
415
450
}
tif_jpeg_12.c:TIFFjpeg_has_multiple_scans
Line
Count
Source
413
2.63k
{
414
    return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d));
415
2.63k
}
416
417
static int TIFFjpeg_start_decompress(JPEGState *sp)
418
2.99k
{
419
2.99k
    const char *sz_max_allowed_scan_number;
420
    /* progress monitor */
421
2.99k
    sp->cinfo.d.progress = &sp->progress;
422
2.99k
    sp->progress.progress_monitor = TIFFjpeg_progress_monitor;
423
2.99k
    sp->otherSettings.max_allowed_scan_number = 100;
424
2.99k
    sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER");
425
2.99k
    if (sz_max_allowed_scan_number)
426
0
        sp->otherSettings.max_allowed_scan_number =
427
0
            atoi(sz_max_allowed_scan_number);
428
429
2.99k
    return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d));
430
2.99k
}
tif_jpeg.c:TIFFjpeg_start_decompress
Line
Count
Source
418
405
{
419
405
    const char *sz_max_allowed_scan_number;
420
    /* progress monitor */
421
405
    sp->cinfo.d.progress = &sp->progress;
422
405
    sp->progress.progress_monitor = TIFFjpeg_progress_monitor;
423
405
    sp->otherSettings.max_allowed_scan_number = 100;
424
405
    sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER");
425
405
    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
405
}
tif_jpeg_12.c:TIFFjpeg_start_decompress
Line
Count
Source
418
2.58k
{
419
2.58k
    const char *sz_max_allowed_scan_number;
420
    /* progress monitor */
421
2.58k
    sp->cinfo.d.progress = &sp->progress;
422
2.58k
    sp->progress.progress_monitor = TIFFjpeg_progress_monitor;
423
2.58k
    sp->otherSettings.max_allowed_scan_number = 100;
424
2.58k
    sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER");
425
2.58k
    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
2.58k
}
431
432
static int TIFFjpeg_read_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines,
433
                                   int max_lines)
434
4.61M
{
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
4.61M
    return CALLJPEG(sp, -1,
441
4.61M
                    (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines,
442
4.61M
                                             (JDIMENSION)max_lines));
443
4.61M
#endif
444
4.61M
}
tif_jpeg.c:TIFFjpeg_read_scanlines
Line
Count
Source
434
21.7k
{
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
21.7k
                    (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines,
442
21.7k
                                             (JDIMENSION)max_lines));
443
21.7k
#endif
444
21.7k
}
tif_jpeg_12.c:TIFFjpeg_read_scanlines
Line
Count
Source
434
4.59M
{
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
4.59M
                    (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines,
442
4.59M
                                             (JDIMENSION)max_lines));
443
4.59M
#endif
444
4.59M
}
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
    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
0
        sp, -1,
456
0
        (int)jpeg_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines));
457
0
#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
1.55k
{
462
1.55k
    return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d));
463
1.55k
}
tif_jpeg.c:TIFFjpeg_finish_decompress
Line
Count
Source
461
294
{
462
    return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d));
463
294
}
tif_jpeg_12.c:TIFFjpeg_finish_decompress
Line
Count
Source
461
1.26k
{
462
    return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d));
463
1.26k
}
464
465
static int TIFFjpeg_abort(JPEGState *sp)
466
13.7k
{
467
13.7k
    return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm));
468
13.7k
}
tif_jpeg.c:TIFFjpeg_abort
Line
Count
Source
466
2.20k
{
467
    return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm));
468
2.20k
}
tif_jpeg_12.c:TIFFjpeg_abort
Line
Count
Source
466
11.5k
{
467
    return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm));
468
11.5k
}
469
470
static int TIFFjpeg_destroy(JPEGState *sp)
471
387k
{
472
387k
    return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm));
473
387k
}
tif_jpeg.c:TIFFjpeg_destroy
Line
Count
Source
471
265k
{
472
    return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm));
473
265k
}
tif_jpeg_12.c:TIFFjpeg_destroy
Line
Count
Source
471
122k
{
472
    return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm));
473
122k
}
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
263k
{
492
263k
    JPEGState *sp = (JPEGState *)cinfo;
493
263k
    TIFF *tif = sp->tif;
494
495
263k
    sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
496
263k
    sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
497
263k
}
tif_jpeg.c:std_init_destination
Line
Count
Source
491
145k
{
492
145k
    JPEGState *sp = (JPEGState *)cinfo;
493
145k
    TIFF *tif = sp->tif;
494
495
145k
    sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
496
145k
    sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
497
145k
}
tif_jpeg_12.c:std_init_destination
Line
Count
Source
491
118k
{
492
118k
    JPEGState *sp = (JPEGState *)cinfo;
493
118k
    TIFF *tif = sp->tif;
494
495
118k
    sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
496
118k
    sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
497
118k
}
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
197k
{
530
197k
    JPEGState *sp = (JPEGState *)cinfo;
531
197k
    TIFF *tif = sp->tif;
532
533
197k
    tif->tif_rawcp = (uint8_t *)sp->dest.next_output_byte;
534
197k
    tif->tif_rawcc = tif->tif_rawdatasize - (tmsize_t)sp->dest.free_in_buffer;
535
    /* NB: libtiff does the final buffer flush */
536
197k
}
tif_jpeg.c:std_term_destination
Line
Count
Source
529
79.4k
{
530
79.4k
    JPEGState *sp = (JPEGState *)cinfo;
531
79.4k
    TIFF *tif = sp->tif;
532
533
79.4k
    tif->tif_rawcp = (uint8_t *)sp->dest.next_output_byte;
534
79.4k
    tif->tif_rawcc = tif->tif_rawdatasize - (tmsize_t)sp->dest.free_in_buffer;
535
    /* NB: libtiff does the final buffer flush */
536
79.4k
}
tif_jpeg_12.c:std_term_destination
Line
Count
Source
529
118k
{
530
118k
    JPEGState *sp = (JPEGState *)cinfo;
531
118k
    TIFF *tif = sp->tif;
532
533
118k
    tif->tif_rawcp = (uint8_t *)sp->dest.next_output_byte;
534
118k
    tif->tif_rawcc = tif->tif_rawdatasize - (tmsize_t)sp->dest.free_in_buffer;
535
    /* NB: libtiff does the final buffer flush */
536
118k
}
537
538
static void TIFFjpeg_data_dest(JPEGState *sp, TIFF *tif)
539
263k
{
540
263k
    (void)tif;
541
263k
    sp->cinfo.c.dest = &sp->dest;
542
263k
    sp->dest.init_destination = std_init_destination;
543
263k
    sp->dest.empty_output_buffer = std_empty_output_buffer;
544
263k
    sp->dest.term_destination = std_term_destination;
545
263k
}
tif_jpeg.c:TIFFjpeg_data_dest
Line
Count
Source
539
145k
{
540
145k
    (void)tif;
541
145k
    sp->cinfo.c.dest = &sp->dest;
542
145k
    sp->dest.init_destination = std_init_destination;
543
145k
    sp->dest.empty_output_buffer = std_empty_output_buffer;
544
145k
    sp->dest.term_destination = std_term_destination;
545
145k
}
tif_jpeg_12.c:TIFFjpeg_data_dest
Line
Count
Source
539
118k
{
540
118k
    (void)tif;
541
118k
    sp->cinfo.c.dest = &sp->dest;
542
118k
    sp->dest.init_destination = std_init_destination;
543
118k
    sp->dest.empty_output_buffer = std_empty_output_buffer;
544
118k
    sp->dest.term_destination = std_term_destination;
545
118k
}
546
547
/*
548
 * Alternate destination manager for outputting to JPEGTables field.
549
 */
550
551
static void tables_init_destination(j_compress_ptr cinfo)
552
131k
{
553
131k
    JPEGState *sp = (JPEGState *)cinfo;
554
555
    /* while building, otherSettings.jpegtables_length is allocated buffer size
556
     */
557
131k
    sp->dest.next_output_byte = (JOCTET *)sp->otherSettings.jpegtables;
558
131k
    sp->dest.free_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
559
131k
}
tif_jpeg.c:tables_init_destination
Line
Count
Source
552
72.6k
{
553
72.6k
    JPEGState *sp = (JPEGState *)cinfo;
554
555
    /* while building, otherSettings.jpegtables_length is allocated buffer size
556
     */
557
72.6k
    sp->dest.next_output_byte = (JOCTET *)sp->otherSettings.jpegtables;
558
72.6k
    sp->dest.free_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
559
72.6k
}
tif_jpeg_12.c:tables_init_destination
Line
Count
Source
552
59.0k
{
553
59.0k
    JPEGState *sp = (JPEGState *)cinfo;
554
555
    /* while building, otherSettings.jpegtables_length is allocated buffer size
556
     */
557
59.0k
    sp->dest.next_output_byte = (JOCTET *)sp->otherSettings.jpegtables;
558
59.0k
    sp->dest.free_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
559
59.0k
}
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
131k
{
582
131k
    JPEGState *sp = (JPEGState *)cinfo;
583
584
    /* set tables length to number of bytes actually emitted */
585
131k
    sp->otherSettings.jpegtables_length -= (uint32_t)sp->dest.free_in_buffer;
586
131k
}
tif_jpeg.c:tables_term_destination
Line
Count
Source
581
72.6k
{
582
72.6k
    JPEGState *sp = (JPEGState *)cinfo;
583
584
    /* set tables length to number of bytes actually emitted */
585
72.6k
    sp->otherSettings.jpegtables_length -= (uint32_t)sp->dest.free_in_buffer;
586
72.6k
}
tif_jpeg_12.c:tables_term_destination
Line
Count
Source
581
59.0k
{
582
59.0k
    JPEGState *sp = (JPEGState *)cinfo;
583
584
    /* set tables length to number of bytes actually emitted */
585
59.0k
    sp->otherSettings.jpegtables_length -= (uint32_t)sp->dest.free_in_buffer;
586
59.0k
}
587
588
static int TIFFjpeg_tables_dest(JPEGState *sp, TIFF *tif)
589
131k
{
590
131k
    (void)tif;
591
    /*
592
     * Allocate a working buffer for building tables.
593
     * Initial size is 1000 bytes, which is usually adequate.
594
     */
595
131k
    if (sp->otherSettings.jpegtables)
596
0
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
597
131k
    sp->otherSettings.jpegtables_length = 1000;
598
131k
    sp->otherSettings.jpegtables = (void *)_TIFFmallocExt(
599
131k
        tif, (tmsize_t)sp->otherSettings.jpegtables_length);
600
131k
    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
131k
    sp->cinfo.c.dest = &sp->dest;
608
131k
    sp->dest.init_destination = tables_init_destination;
609
131k
    sp->dest.empty_output_buffer = tables_empty_output_buffer;
610
131k
    sp->dest.term_destination = tables_term_destination;
611
131k
    return (1);
612
131k
}
tif_jpeg.c:TIFFjpeg_tables_dest
Line
Count
Source
589
72.6k
{
590
72.6k
    (void)tif;
591
    /*
592
     * Allocate a working buffer for building tables.
593
     * Initial size is 1000 bytes, which is usually adequate.
594
     */
595
72.6k
    if (sp->otherSettings.jpegtables)
596
0
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
597
72.6k
    sp->otherSettings.jpegtables_length = 1000;
598
72.6k
    sp->otherSettings.jpegtables = (void *)_TIFFmallocExt(
599
72.6k
        tif, (tmsize_t)sp->otherSettings.jpegtables_length);
600
72.6k
    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
72.6k
    sp->cinfo.c.dest = &sp->dest;
608
72.6k
    sp->dest.init_destination = tables_init_destination;
609
72.6k
    sp->dest.empty_output_buffer = tables_empty_output_buffer;
610
72.6k
    sp->dest.term_destination = tables_term_destination;
611
72.6k
    return (1);
612
72.6k
}
tif_jpeg_12.c:TIFFjpeg_tables_dest
Line
Count
Source
589
59.0k
{
590
59.0k
    (void)tif;
591
    /*
592
     * Allocate a working buffer for building tables.
593
     * Initial size is 1000 bytes, which is usually adequate.
594
     */
595
59.0k
    if (sp->otherSettings.jpegtables)
596
0
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
597
59.0k
    sp->otherSettings.jpegtables_length = 1000;
598
59.0k
    sp->otherSettings.jpegtables = (void *)_TIFFmallocExt(
599
59.0k
        tif, (tmsize_t)sp->otherSettings.jpegtables_length);
600
59.0k
    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
59.0k
    sp->cinfo.c.dest = &sp->dest;
608
59.0k
    sp->dest.init_destination = tables_init_destination;
609
59.0k
    sp->dest.empty_output_buffer = tables_empty_output_buffer;
610
59.0k
    sp->dest.term_destination = tables_term_destination;
611
59.0k
    return (1);
612
59.0k
}
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
13.7k
{
621
13.7k
    JPEGState *sp = (JPEGState *)cinfo;
622
13.7k
    TIFF *tif = sp->tif;
623
624
13.7k
    sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata;
625
13.7k
    sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
626
13.7k
}
tif_jpeg.c:std_init_source
Line
Count
Source
620
2.20k
{
621
2.20k
    JPEGState *sp = (JPEGState *)cinfo;
622
2.20k
    TIFF *tif = sp->tif;
623
624
2.20k
    sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata;
625
2.20k
    sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
626
2.20k
}
tif_jpeg_12.c:std_init_source
Line
Count
Source
620
11.5k
{
621
11.5k
    JPEGState *sp = (JPEGState *)cinfo;
622
11.5k
    TIFF *tif = sp->tif;
623
624
11.5k
    sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata;
625
11.5k
    sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
626
11.5k
}
627
628
static boolean std_fill_input_buffer(j_decompress_ptr cinfo)
629
75.0k
{
630
75.0k
    JPEGState *sp = (JPEGState *)cinfo;
631
75.0k
    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
75.0k
    WARNMS(cinfo, JWRN_JPEG_EOF);
658
    /* insert a fake EOI marker */
659
75.0k
    sp->src.next_input_byte = dummy_EOI;
660
75.0k
    sp->src.bytes_in_buffer = 2;
661
75.0k
    return (TRUE);
662
75.0k
}
tif_jpeg.c:std_fill_input_buffer
Line
Count
Source
629
2.50k
{
630
2.50k
    JPEGState *sp = (JPEGState *)cinfo;
631
2.50k
    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
2.50k
    WARNMS(cinfo, JWRN_JPEG_EOF);
658
    /* insert a fake EOI marker */
659
2.50k
    sp->src.next_input_byte = dummy_EOI;
660
2.50k
    sp->src.bytes_in_buffer = 2;
661
2.50k
    return (TRUE);
662
2.50k
}
tif_jpeg_12.c:std_fill_input_buffer
Line
Count
Source
629
72.5k
{
630
72.5k
    JPEGState *sp = (JPEGState *)cinfo;
631
72.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
72.5k
    WARNMS(cinfo, JWRN_JPEG_EOF);
658
    /* insert a fake EOI marker */
659
72.5k
    sp->src.next_input_byte = dummy_EOI;
660
72.5k
    sp->src.bytes_in_buffer = 2;
661
72.5k
    return (TRUE);
662
72.5k
}
663
664
static void std_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
665
104k
{
666
104k
    JPEGState *sp = (JPEGState *)cinfo;
667
668
104k
    if (num_bytes > 0)
669
104k
    {
670
104k
        if ((size_t)num_bytes > sp->src.bytes_in_buffer)
671
1.64k
        {
672
            /* oops, buffer overrun */
673
1.64k
            (void)std_fill_input_buffer(cinfo);
674
1.64k
        }
675
103k
        else
676
103k
        {
677
103k
            sp->src.next_input_byte += (size_t)num_bytes;
678
103k
            sp->src.bytes_in_buffer -= (size_t)num_bytes;
679
103k
        }
680
104k
    }
681
104k
}
tif_jpeg.c:std_skip_input_data
Line
Count
Source
665
7.88k
{
666
7.88k
    JPEGState *sp = (JPEGState *)cinfo;
667
668
7.88k
    if (num_bytes > 0)
669
7.88k
    {
670
7.88k
        if ((size_t)num_bytes > sp->src.bytes_in_buffer)
671
134
        {
672
            /* oops, buffer overrun */
673
134
            (void)std_fill_input_buffer(cinfo);
674
134
        }
675
7.74k
        else
676
7.74k
        {
677
7.74k
            sp->src.next_input_byte += (size_t)num_bytes;
678
7.74k
            sp->src.bytes_in_buffer -= (size_t)num_bytes;
679
7.74k
        }
680
7.88k
    }
681
7.88k
}
tif_jpeg_12.c:std_skip_input_data
Line
Count
Source
665
96.9k
{
666
96.9k
    JPEGState *sp = (JPEGState *)cinfo;
667
668
96.9k
    if (num_bytes > 0)
669
96.9k
    {
670
96.9k
        if ((size_t)num_bytes > sp->src.bytes_in_buffer)
671
1.51k
        {
672
            /* oops, buffer overrun */
673
1.51k
            (void)std_fill_input_buffer(cinfo);
674
1.51k
        }
675
95.3k
        else
676
95.3k
        {
677
95.3k
            sp->src.next_input_byte += (size_t)num_bytes;
678
95.3k
            sp->src.bytes_in_buffer -= (size_t)num_bytes;
679
95.3k
        }
680
96.9k
    }
681
96.9k
}
682
683
static void std_term_source(j_decompress_ptr cinfo)
684
1.46k
{
685
    /* No work necessary here */
686
1.46k
    (void)cinfo;
687
1.46k
}
tif_jpeg.c:std_term_source
Line
Count
Source
684
256
{
685
    /* No work necessary here */
686
256
    (void)cinfo;
687
256
}
tif_jpeg_12.c:std_term_source
Line
Count
Source
684
1.20k
{
685
    /* No work necessary here */
686
1.20k
    (void)cinfo;
687
1.20k
}
688
689
static void TIFFjpeg_data_src(JPEGState *sp)
690
5.09k
{
691
5.09k
    sp->cinfo.d.src = &sp->src;
692
5.09k
    sp->src.init_source = std_init_source;
693
5.09k
    sp->src.fill_input_buffer = std_fill_input_buffer;
694
5.09k
    sp->src.skip_input_data = std_skip_input_data;
695
5.09k
    sp->src.resync_to_restart = jpeg_resync_to_restart;
696
5.09k
    sp->src.term_source = std_term_source;
697
5.09k
    sp->src.bytes_in_buffer = 0; /* for safety */
698
5.09k
    sp->src.next_input_byte = NULL;
699
5.09k
}
tif_jpeg.c:TIFFjpeg_data_src
Line
Count
Source
690
752
{
691
752
    sp->cinfo.d.src = &sp->src;
692
752
    sp->src.init_source = std_init_source;
693
752
    sp->src.fill_input_buffer = std_fill_input_buffer;
694
752
    sp->src.skip_input_data = std_skip_input_data;
695
752
    sp->src.resync_to_restart = jpeg_resync_to_restart;
696
752
    sp->src.term_source = std_term_source;
697
752
    sp->src.bytes_in_buffer = 0; /* for safety */
698
    sp->src.next_input_byte = NULL;
699
752
}
tif_jpeg_12.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
}
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
421
{
708
421
    JPEGState *sp = (JPEGState *)cinfo;
709
710
421
    sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables;
711
421
    sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
712
421
}
tif_jpeg.c:tables_init_source
Line
Count
Source
707
186
{
708
186
    JPEGState *sp = (JPEGState *)cinfo;
709
710
186
    sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables;
711
186
    sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
712
186
}
tif_jpeg_12.c:tables_init_source
Line
Count
Source
707
235
{
708
235
    JPEGState *sp = (JPEGState *)cinfo;
709
710
235
    sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables;
711
235
    sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
712
235
}
713
714
static void TIFFjpeg_tables_src(JPEGState *sp)
715
424
{
716
424
    TIFFjpeg_data_src(sp);
717
424
    sp->src.init_source = tables_init_source;
718
424
}
tif_jpeg.c:TIFFjpeg_tables_src
Line
Count
Source
715
186
{
716
186
    TIFFjpeg_data_src(sp);
717
186
    sp->src.init_source = tables_init_source;
718
186
}
tif_jpeg_12.c:TIFFjpeg_tables_src
Line
Count
Source
715
238
{
716
238
    TIFFjpeg_data_src(sp);
717
238
    sp->src.init_source = tables_init_source;
718
238
}
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
460
#define JPEG_MARKER_SOF0 0xC0
757
1.02k
#define JPEG_MARKER_SOF1 0xC1
758
2.06k
#define JPEG_MARKER_SOF2 0xC2
759
2.26k
#define JPEG_MARKER_SOF9 0xC9
760
2.51k
#define JPEG_MARKER_SOF10 0xCA
761
948k
#define JPEG_MARKER_DHT 0xC4
762
8.47k
#define JPEG_MARKER_SOI 0xD8
763
940k
#define JPEG_MARKER_SOS 0xDA
764
935k
#define JPEG_MARKER_DQT 0xDB
765
951k
#define JPEG_MARKER_DRI 0xDD
766
8.20M
#define JPEG_MARKER_APP0 0xE0
767
49.3k
#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
425k
{
796
425k
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
797
425k
    JPEGState *sp = JState(tif);
798
425k
    if ((tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR) &&
799
50.5k
        (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
800
49.4k
        (tif->tif_dir.td_samplesperpixel == 3) &&
801
46.8k
        !sp->otherSettings.ycbcrsampling_fetched)
802
20.0k
        JPEGFixupTagsSubsampling(tif);
803
425k
#endif
804
805
425k
    return (1);
806
425k
}
tif_jpeg.c:JPEGFixupTags
Line
Count
Source
795
425k
{
796
425k
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
797
425k
    JPEGState *sp = JState(tif);
798
425k
    if ((tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR) &&
799
50.5k
        (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
800
49.4k
        (tif->tif_dir.td_samplesperpixel == 3) &&
801
46.8k
        !sp->otherSettings.ycbcrsampling_fetched)
802
20.0k
        JPEGFixupTagsSubsampling(tif);
803
425k
#endif
804
805
425k
    return (1);
806
425k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTags
807
808
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
809
810
static void JPEGFixupTagsSubsampling(TIFF *tif)
811
20.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
20.0k
    static const char module[] = "JPEGFixupTagsSubsampling";
833
20.0k
    struct JPEGFixupTagsSubsamplingData m;
834
20.0k
    uint64_t fileoffset = TIFFGetStrileOffset(tif, 0);
835
836
20.0k
    if (fileoffset == 0)
837
11.7k
    {
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
11.7k
        return;
842
11.7k
    }
843
844
8.34k
    m.tif = tif;
845
8.34k
    m.buffersize = 2048;
846
8.34k
    m.buffer = _TIFFmallocExt(tif, m.buffersize);
847
8.34k
    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
8.34k
    m.buffercurrentbyte = NULL;
855
8.34k
    m.bufferbytesleft = 0;
856
8.34k
    m.fileoffset = fileoffset;
857
8.34k
    m.filepositioned = 0;
858
8.34k
    m.filebytesleft = TIFFGetStrileByteCount(tif, 0);
859
8.34k
    if (!JPEGFixupTagsSubsamplingSec(&m))
860
6.96k
        TIFFWarningExtR(
861
6.96k
            tif, module,
862
6.96k
            "Unable to auto-correct subsampling values, likely corrupt JPEG "
863
6.96k
            "compressed data in first strip/tile; auto-correcting skipped");
864
8.34k
    _TIFFfreeExt(tif, m.buffer);
865
8.34k
}
tif_jpeg.c:JPEGFixupTagsSubsampling
Line
Count
Source
811
20.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
20.0k
    static const char module[] = "JPEGFixupTagsSubsampling";
833
20.0k
    struct JPEGFixupTagsSubsamplingData m;
834
20.0k
    uint64_t fileoffset = TIFFGetStrileOffset(tif, 0);
835
836
20.0k
    if (fileoffset == 0)
837
11.7k
    {
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
11.7k
        return;
842
11.7k
    }
843
844
8.34k
    m.tif = tif;
845
8.34k
    m.buffersize = 2048;
846
8.34k
    m.buffer = _TIFFmallocExt(tif, m.buffersize);
847
8.34k
    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
8.34k
    m.buffercurrentbyte = NULL;
855
8.34k
    m.bufferbytesleft = 0;
856
8.34k
    m.fileoffset = fileoffset;
857
8.34k
    m.filepositioned = 0;
858
8.34k
    m.filebytesleft = TIFFGetStrileByteCount(tif, 0);
859
8.34k
    if (!JPEGFixupTagsSubsamplingSec(&m))
860
6.96k
        TIFFWarningExtR(
861
6.96k
            tif, module,
862
6.96k
            "Unable to auto-correct subsampling values, likely corrupt JPEG "
863
6.96k
            "compressed data in first strip/tile; auto-correcting skipped");
864
8.34k
    _TIFFfreeExt(tif, m.buffer);
865
8.34k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsampling
866
867
static int
868
JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData *data)
869
8.34k
{
870
8.34k
    static const char module[] = "JPEGFixupTagsSubsamplingSec";
871
8.34k
    uint8_t m;
872
967k
    while (1)
873
967k
    {
874
20.9M
        while (1)
875
20.9M
        {
876
20.9M
            if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
877
2.50k
                return (0);
878
20.9M
            if (m == 255)
879
965k
                break;
880
20.9M
        }
881
1.75M
        while (1)
882
1.75M
        {
883
1.75M
            if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
884
255
                return (0);
885
1.75M
            if (m != 255)
886
964k
                break;
887
1.75M
        }
888
964k
        switch (m)
889
964k
        {
890
8.47k
            case JPEG_MARKER_SOI:
891
                /* this type of marker has no data and should be skipped */
892
8.47k
                break;
893
49.3k
            case JPEG_MARKER_COM:
894
136k
            case JPEG_MARKER_APP0:
895
171k
            case JPEG_MARKER_APP0 + 1:
896
176k
            case JPEG_MARKER_APP0 + 2:
897
228k
            case JPEG_MARKER_APP0 + 3:
898
334k
            case JPEG_MARKER_APP0 + 4:
899
469k
            case JPEG_MARKER_APP0 + 5:
900
479k
            case JPEG_MARKER_APP0 + 6:
901
537k
            case JPEG_MARKER_APP0 + 7:
902
551k
            case JPEG_MARKER_APP0 + 8:
903
590k
            case JPEG_MARKER_APP0 + 9:
904
607k
            case JPEG_MARKER_APP0 + 10:
905
656k
            case JPEG_MARKER_APP0 + 11:
906
693k
            case JPEG_MARKER_APP0 + 12:
907
737k
            case JPEG_MARKER_APP0 + 13:
908
908k
            case JPEG_MARKER_APP0 + 14:
909
923k
            case JPEG_MARKER_APP0 + 15:
910
935k
            case JPEG_MARKER_DQT:
911
940k
            case JPEG_MARKER_SOS:
912
948k
            case JPEG_MARKER_DHT:
913
951k
            case JPEG_MARKER_DRI:
914
                /* this type of marker has data, but it has no use to us and
915
                 * should be skipped */
916
951k
                {
917
951k
                    uint16_t n;
918
951k
                    if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
919
537
                        return (0);
920
951k
                    if (n < 2)
921
393
                        return (0);
922
950k
                    n -= 2;
923
950k
                    if (n > 0)
924
786k
                        JPEGFixupTagsSubsamplingSkip(data, n);
925
950k
                }
926
0
                break;
927
460
            case JPEG_MARKER_SOF0:  /* Baseline sequential Huffman */
928
1.02k
            case JPEG_MARKER_SOF1:  /* Extended sequential Huffman */
929
2.06k
            case JPEG_MARKER_SOF2:  /* Progressive Huffman: normally not allowed
930
                                       by  TechNote, but that doesn't hurt
931
                                       supporting it */
932
2.26k
            case JPEG_MARKER_SOF9:  /* Extended sequential arithmetic */
933
2.51k
            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
2.51k
                {
939
2.51k
                    uint16_t n;
940
2.51k
                    uint16_t o;
941
2.51k
                    uint8_t p;
942
2.51k
                    uint8_t ph, pv;
943
2.51k
                    if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
944
64
                        return (0);
945
2.44k
                    if (n != 8 + data->tif->tif_dir.td_samplesperpixel * 3)
946
836
                        return (0);
947
1.61k
                    JPEGFixupTagsSubsamplingSkip(data, 7);
948
1.61k
                    if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
949
47
                        return (0);
950
1.56k
                    ph = (p >> 4);
951
1.56k
                    pv = (p & 15);
952
1.56k
                    JPEGFixupTagsSubsamplingSkip(data, 1);
953
3.97k
                    for (o = 1; o < data->tif->tif_dir.td_samplesperpixel; o++)
954
2.79k
                    {
955
2.79k
                        JPEGFixupTagsSubsamplingSkip(data, 1);
956
2.79k
                        if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
957
185
                            return (0);
958
2.60k
                        if (p != 0x11)
959
201
                        {
960
201
                            TIFFWarningExtR(data->tif, module,
961
201
                                            "Subsampling values inside JPEG "
962
201
                                            "compressed data "
963
201
                                            "have no TIFF equivalent, "
964
201
                                            "auto-correction of TIFF "
965
201
                                            "subsampling values failed");
966
201
                            return (1);
967
201
                        }
968
2.40k
                        JPEGFixupTagsSubsamplingSkip(data, 1);
969
2.40k
                    }
970
1.17k
                    if (((ph != 1) && (ph != 2) && (ph != 4)) ||
971
1.07k
                        ((pv != 1) && (pv != 2) && (pv != 4)))
972
152
                    {
973
152
                        TIFFWarningExtR(data->tif, module,
974
152
                                        "Subsampling values inside JPEG "
975
152
                                        "compressed data have no TIFF "
976
152
                                        "equivalent, auto-correction of TIFF "
977
152
                                        "subsampling values failed");
978
152
                        return (1);
979
152
                    }
980
1.02k
                    if ((ph != data->tif->tif_dir.td_ycbcrsubsampling[0]) ||
981
513
                        (pv != data->tif->tif_dir.td_ycbcrsubsampling[1]))
982
797
                    {
983
797
                        TIFFWarningExtR(
984
797
                            data->tif, module,
985
797
                            "Auto-corrected former TIFF subsampling values "
986
797
                            "[%" PRIu16 ",%" PRIu16
987
797
                            "] to match subsampling values inside JPEG "
988
797
                            "compressed data [%" PRIu8 ",%" PRIu8 "]",
989
797
                            data->tif->tif_dir.td_ycbcrsubsampling[0],
990
797
                            data->tif->tif_dir.td_ycbcrsubsampling[1], ph, pv);
991
797
                        data->tif->tif_dir.td_ycbcrsubsampling[0] = ph;
992
797
                        data->tif->tif_dir.td_ycbcrsubsampling[1] = pv;
993
797
                    }
994
1.02k
                }
995
0
                return (1);
996
2.13k
            default:
997
2.13k
                return (0);
998
964k
        }
999
964k
    }
1000
8.34k
}
tif_jpeg.c:JPEGFixupTagsSubsamplingSec
Line
Count
Source
869
8.34k
{
870
8.34k
    static const char module[] = "JPEGFixupTagsSubsamplingSec";
871
8.34k
    uint8_t m;
872
967k
    while (1)
873
967k
    {
874
20.9M
        while (1)
875
20.9M
        {
876
20.9M
            if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
877
2.50k
                return (0);
878
20.9M
            if (m == 255)
879
965k
                break;
880
20.9M
        }
881
1.75M
        while (1)
882
1.75M
        {
883
1.75M
            if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
884
255
                return (0);
885
1.75M
            if (m != 255)
886
964k
                break;
887
1.75M
        }
888
964k
        switch (m)
889
964k
        {
890
8.47k
            case JPEG_MARKER_SOI:
891
                /* this type of marker has no data and should be skipped */
892
8.47k
                break;
893
49.3k
            case JPEG_MARKER_COM:
894
136k
            case JPEG_MARKER_APP0:
895
171k
            case JPEG_MARKER_APP0 + 1:
896
176k
            case JPEG_MARKER_APP0 + 2:
897
228k
            case JPEG_MARKER_APP0 + 3:
898
334k
            case JPEG_MARKER_APP0 + 4:
899
469k
            case JPEG_MARKER_APP0 + 5:
900
479k
            case JPEG_MARKER_APP0 + 6:
901
537k
            case JPEG_MARKER_APP0 + 7:
902
551k
            case JPEG_MARKER_APP0 + 8:
903
590k
            case JPEG_MARKER_APP0 + 9:
904
607k
            case JPEG_MARKER_APP0 + 10:
905
656k
            case JPEG_MARKER_APP0 + 11:
906
693k
            case JPEG_MARKER_APP0 + 12:
907
737k
            case JPEG_MARKER_APP0 + 13:
908
908k
            case JPEG_MARKER_APP0 + 14:
909
923k
            case JPEG_MARKER_APP0 + 15:
910
935k
            case JPEG_MARKER_DQT:
911
940k
            case JPEG_MARKER_SOS:
912
948k
            case JPEG_MARKER_DHT:
913
951k
            case JPEG_MARKER_DRI:
914
                /* this type of marker has data, but it has no use to us and
915
                 * should be skipped */
916
951k
                {
917
951k
                    uint16_t n;
918
951k
                    if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
919
537
                        return (0);
920
951k
                    if (n < 2)
921
393
                        return (0);
922
950k
                    n -= 2;
923
950k
                    if (n > 0)
924
786k
                        JPEGFixupTagsSubsamplingSkip(data, n);
925
950k
                }
926
0
                break;
927
460
            case JPEG_MARKER_SOF0:  /* Baseline sequential Huffman */
928
1.02k
            case JPEG_MARKER_SOF1:  /* Extended sequential Huffman */
929
2.06k
            case JPEG_MARKER_SOF2:  /* Progressive Huffman: normally not allowed
930
                                       by  TechNote, but that doesn't hurt
931
                                       supporting it */
932
2.26k
            case JPEG_MARKER_SOF9:  /* Extended sequential arithmetic */
933
2.51k
            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
2.51k
                {
939
2.51k
                    uint16_t n;
940
2.51k
                    uint16_t o;
941
2.51k
                    uint8_t p;
942
2.51k
                    uint8_t ph, pv;
943
2.51k
                    if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
944
64
                        return (0);
945
2.44k
                    if (n != 8 + data->tif->tif_dir.td_samplesperpixel * 3)
946
836
                        return (0);
947
1.61k
                    JPEGFixupTagsSubsamplingSkip(data, 7);
948
1.61k
                    if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
949
47
                        return (0);
950
1.56k
                    ph = (p >> 4);
951
1.56k
                    pv = (p & 15);
952
1.56k
                    JPEGFixupTagsSubsamplingSkip(data, 1);
953
3.97k
                    for (o = 1; o < data->tif->tif_dir.td_samplesperpixel; o++)
954
2.79k
                    {
955
2.79k
                        JPEGFixupTagsSubsamplingSkip(data, 1);
956
2.79k
                        if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
957
185
                            return (0);
958
2.60k
                        if (p != 0x11)
959
201
                        {
960
201
                            TIFFWarningExtR(data->tif, module,
961
201
                                            "Subsampling values inside JPEG "
962
201
                                            "compressed data "
963
201
                                            "have no TIFF equivalent, "
964
201
                                            "auto-correction of TIFF "
965
201
                                            "subsampling values failed");
966
201
                            return (1);
967
201
                        }
968
2.40k
                        JPEGFixupTagsSubsamplingSkip(data, 1);
969
2.40k
                    }
970
1.17k
                    if (((ph != 1) && (ph != 2) && (ph != 4)) ||
971
1.07k
                        ((pv != 1) && (pv != 2) && (pv != 4)))
972
152
                    {
973
152
                        TIFFWarningExtR(data->tif, module,
974
152
                                        "Subsampling values inside JPEG "
975
152
                                        "compressed data have no TIFF "
976
152
                                        "equivalent, auto-correction of TIFF "
977
152
                                        "subsampling values failed");
978
152
                        return (1);
979
152
                    }
980
1.02k
                    if ((ph != data->tif->tif_dir.td_ycbcrsubsampling[0]) ||
981
513
                        (pv != data->tif->tif_dir.td_ycbcrsubsampling[1]))
982
797
                    {
983
797
                        TIFFWarningExtR(
984
797
                            data->tif, module,
985
797
                            "Auto-corrected former TIFF subsampling values "
986
797
                            "[%" PRIu16 ",%" PRIu16
987
797
                            "] to match subsampling values inside JPEG "
988
797
                            "compressed data [%" PRIu8 ",%" PRIu8 "]",
989
797
                            data->tif->tif_dir.td_ycbcrsubsampling[0],
990
797
                            data->tif->tif_dir.td_ycbcrsubsampling[1], ph, pv);
991
797
                        data->tif->tif_dir.td_ycbcrsubsampling[0] = ph;
992
797
                        data->tif->tif_dir.td_ycbcrsubsampling[1] = pv;
993
797
                    }
994
1.02k
                }
995
0
                return (1);
996
2.13k
            default:
997
2.13k
                return (0);
998
964k
        }
999
964k
    }
1000
8.34k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingSec
1001
1002
static int
1003
JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData *data,
1004
                                 uint8_t *result)
1005
24.6M
{
1006
24.6M
    if (data->bufferbytesleft == 0)
1007
28.4k
    {
1008
28.4k
        uint32_t m;
1009
28.4k
        if (data->filebytesleft == 0)
1010
2.67k
            return (0);
1011
25.7k
        if (!data->filepositioned)
1012
14.1k
        {
1013
14.1k
            if (TIFFSeekFile(data->tif, data->fileoffset, SEEK_SET) ==
1014
14.1k
                (toff_t)-1)
1015
2
            {
1016
2
                return 0;
1017
2
            }
1018
14.1k
            data->filepositioned = 1;
1019
14.1k
        }
1020
25.7k
        m = data->buffersize;
1021
25.7k
        if ((uint64_t)m > data->filebytesleft)
1022
6.48k
            m = (uint32_t)data->filebytesleft;
1023
25.7k
        assert(m < 0x80000000UL);
1024
25.7k
        if (TIFFReadFile(data->tif, data->buffer, (tmsize_t)m) != (tmsize_t)m)
1025
921
            return (0);
1026
24.8k
        data->buffercurrentbyte = (uint8_t *)data->buffer;
1027
24.8k
        data->bufferbytesleft = m;
1028
24.8k
        data->fileoffset += m;
1029
24.8k
        data->filebytesleft -= m;
1030
24.8k
    }
1031
24.6M
    *result = *data->buffercurrentbyte;
1032
24.6M
    data->buffercurrentbyte++;
1033
24.6M
    data->bufferbytesleft--;
1034
24.6M
    return (1);
1035
24.6M
}
tif_jpeg.c:JPEGFixupTagsSubsamplingReadByte
Line
Count
Source
1005
24.6M
{
1006
24.6M
    if (data->bufferbytesleft == 0)
1007
28.4k
    {
1008
28.4k
        uint32_t m;
1009
28.4k
        if (data->filebytesleft == 0)
1010
2.67k
            return (0);
1011
25.7k
        if (!data->filepositioned)
1012
14.1k
        {
1013
14.1k
            if (TIFFSeekFile(data->tif, data->fileoffset, SEEK_SET) ==
1014
14.1k
                (toff_t)-1)
1015
2
            {
1016
2
                return 0;
1017
2
            }
1018
14.1k
            data->filepositioned = 1;
1019
14.1k
        }
1020
25.7k
        m = data->buffersize;
1021
25.7k
        if ((uint64_t)m > data->filebytesleft)
1022
6.48k
            m = (uint32_t)data->filebytesleft;
1023
25.7k
        assert(m < 0x80000000UL);
1024
25.7k
        if (TIFFReadFile(data->tif, data->buffer, (tmsize_t)m) != (tmsize_t)m)
1025
921
            return (0);
1026
24.8k
        data->buffercurrentbyte = (uint8_t *)data->buffer;
1027
24.8k
        data->bufferbytesleft = m;
1028
24.8k
        data->fileoffset += m;
1029
24.8k
        data->filebytesleft -= m;
1030
24.8k
    }
1031
24.6M
    *result = *data->buffercurrentbyte;
1032
24.6M
    data->buffercurrentbyte++;
1033
24.6M
    data->bufferbytesleft--;
1034
24.6M
    return (1);
1035
24.6M
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingReadByte
1036
1037
static int
1038
JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData *data,
1039
                                 uint16_t *result)
1040
954k
{
1041
954k
    uint8_t ma;
1042
954k
    uint8_t mb;
1043
954k
    if (!JPEGFixupTagsSubsamplingReadByte(data, &ma))
1044
200
        return (0);
1045
954k
    if (!JPEGFixupTagsSubsamplingReadByte(data, &mb))
1046
401
        return (0);
1047
953k
    *result = (ma << 8) | mb;
1048
953k
    return (1);
1049
954k
}
tif_jpeg.c:JPEGFixupTagsSubsamplingReadWord
Line
Count
Source
1040
954k
{
1041
954k
    uint8_t ma;
1042
954k
    uint8_t mb;
1043
954k
    if (!JPEGFixupTagsSubsamplingReadByte(data, &ma))
1044
200
        return (0);
1045
954k
    if (!JPEGFixupTagsSubsamplingReadByte(data, &mb))
1046
401
        return (0);
1047
953k
    *result = (ma << 8) | mb;
1048
953k
    return (1);
1049
954k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingReadWord
1050
1051
static void
1052
JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData *data,
1053
                             uint16_t skiplength)
1054
795k
{
1055
795k
    if ((uint32_t)skiplength <= data->bufferbytesleft)
1056
788k
    {
1057
788k
        data->buffercurrentbyte += skiplength;
1058
788k
        data->bufferbytesleft -= skiplength;
1059
788k
    }
1060
7.25k
    else
1061
7.25k
    {
1062
7.25k
        uint16_t m;
1063
7.25k
        m = (uint16_t)(skiplength - data->bufferbytesleft);
1064
7.25k
        if (m <= data->filebytesleft)
1065
6.17k
        {
1066
6.17k
            data->bufferbytesleft = 0;
1067
6.17k
            data->fileoffset += m;
1068
6.17k
            data->filebytesleft -= m;
1069
6.17k
            data->filepositioned = 0;
1070
6.17k
        }
1071
1.07k
        else
1072
1.07k
        {
1073
1.07k
            data->bufferbytesleft = 0;
1074
1.07k
            data->filebytesleft = 0;
1075
1.07k
        }
1076
7.25k
    }
1077
795k
}
tif_jpeg.c:JPEGFixupTagsSubsamplingSkip
Line
Count
Source
1054
795k
{
1055
795k
    if ((uint32_t)skiplength <= data->bufferbytesleft)
1056
788k
    {
1057
788k
        data->buffercurrentbyte += skiplength;
1058
788k
        data->bufferbytesleft -= skiplength;
1059
788k
    }
1060
7.25k
    else
1061
7.25k
    {
1062
7.25k
        uint16_t m;
1063
7.25k
        m = (uint16_t)(skiplength - data->bufferbytesleft);
1064
7.25k
        if (m <= data->filebytesleft)
1065
6.17k
        {
1066
6.17k
            data->bufferbytesleft = 0;
1067
6.17k
            data->fileoffset += m;
1068
6.17k
            data->filebytesleft -= m;
1069
6.17k
            data->filepositioned = 0;
1070
6.17k
        }
1071
1.07k
        else
1072
1.07k
        {
1073
1.07k
            data->bufferbytesleft = 0;
1074
1.07k
            data->filebytesleft = 0;
1075
1.07k
        }
1076
7.25k
    }
1077
795k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGFixupTagsSubsamplingSkip
1078
1079
#endif
1080
1081
static int JPEGSetupDecode(TIFF *tif)
1082
9.06k
{
1083
9.06k
    JPEGState *sp = JState(tif);
1084
9.06k
    TIFFDirectory *td = &tif->tif_dir;
1085
1086
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1087
4.81k
    if (tif->tif_dir.td_bitspersample == 12)
1088
4.15k
    {
1089
        /* We pass a pointer to a copy of otherSettings, since */
1090
        /* TIFFReInitJPEG_12() will clear sp */
1091
4.15k
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1092
4.15k
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0);
1093
4.15k
    }
1094
664
#endif
1095
1096
4.91k
    JPEGInitializeLibJPEG(tif, TRUE);
1097
1098
664
    assert(sp != NULL);
1099
4.91k
    assert(sp->cinfo.comm.is_decompressor);
1100
1101
    /* Read JPEGTables if it is present */
1102
4.91k
    if (TIFFFieldSet(tif, FIELD_JPEGTABLES))
1103
424
    {
1104
424
        TIFFjpeg_tables_src(sp);
1105
424
        if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY)
1106
246
        {
1107
246
            TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field");
1108
246
            return (0);
1109
246
        }
1110
424
    }
1111
1112
    /* Grab parameters that are same for all strips/tiles */
1113
4.66k
    sp->photometric = td->td_photometric;
1114
4.66k
    switch (sp->photometric)
1115
4.66k
    {
1116
888
        case PHOTOMETRIC_YCBCR:
1117
888
            sp->h_sampling = td->td_ycbcrsubsampling[0];
1118
888
            sp->v_sampling = td->td_ycbcrsubsampling[1];
1119
888
            break;
1120
3.78k
        default:
1121
            /* TIFF 6.0 forbids subsampling of all other color spaces */
1122
3.78k
            sp->h_sampling = 1;
1123
3.78k
            sp->v_sampling = 1;
1124
3.78k
            break;
1125
4.66k
    }
1126
1127
    /* Set up for reading normal data */
1128
4.66k
    TIFFjpeg_data_src(sp);
1129
4.66k
    tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */
1130
4.66k
    return (1);
1131
4.66k
}
tif_jpeg.c:JPEGSetupDecode
Line
Count
Source
1082
4.81k
{
1083
4.81k
    JPEGState *sp = JState(tif);
1084
4.81k
    TIFFDirectory *td = &tif->tif_dir;
1085
1086
4.81k
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1087
4.81k
    if (tif->tif_dir.td_bitspersample == 12)
1088
4.15k
    {
1089
        /* We pass a pointer to a copy of otherSettings, since */
1090
        /* TIFFReInitJPEG_12() will clear sp */
1091
4.15k
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1092
4.15k
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0);
1093
4.15k
    }
1094
664
#endif
1095
1096
664
    JPEGInitializeLibJPEG(tif, TRUE);
1097
1098
664
    assert(sp != NULL);
1099
664
    assert(sp->cinfo.comm.is_decompressor);
1100
1101
    /* Read JPEGTables if it is present */
1102
664
    if (TIFFFieldSet(tif, FIELD_JPEGTABLES))
1103
186
    {
1104
186
        TIFFjpeg_tables_src(sp);
1105
186
        if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY)
1106
105
        {
1107
105
            TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field");
1108
105
            return (0);
1109
105
        }
1110
186
    }
1111
1112
    /* Grab parameters that are same for all strips/tiles */
1113
559
    sp->photometric = td->td_photometric;
1114
559
    switch (sp->photometric)
1115
559
    {
1116
312
        case PHOTOMETRIC_YCBCR:
1117
312
            sp->h_sampling = td->td_ycbcrsubsampling[0];
1118
312
            sp->v_sampling = td->td_ycbcrsubsampling[1];
1119
312
            break;
1120
247
        default:
1121
            /* TIFF 6.0 forbids subsampling of all other color spaces */
1122
247
            sp->h_sampling = 1;
1123
247
            sp->v_sampling = 1;
1124
247
            break;
1125
559
    }
1126
1127
    /* Set up for reading normal data */
1128
559
    TIFFjpeg_data_src(sp);
1129
559
    tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */
1130
559
    return (1);
1131
559
}
tif_jpeg_12.c:JPEGSetupDecode
Line
Count
Source
1082
4.25k
{
1083
4.25k
    JPEGState *sp = JState(tif);
1084
4.25k
    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
4.25k
    JPEGInitializeLibJPEG(tif, TRUE);
1097
1098
4.25k
    assert(sp != NULL);
1099
4.25k
    assert(sp->cinfo.comm.is_decompressor);
1100
1101
    /* Read JPEGTables if it is present */
1102
4.25k
    if (TIFFFieldSet(tif, FIELD_JPEGTABLES))
1103
238
    {
1104
238
        TIFFjpeg_tables_src(sp);
1105
238
        if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY)
1106
141
        {
1107
141
            TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field");
1108
141
            return (0);
1109
141
        }
1110
238
    }
1111
1112
    /* Grab parameters that are same for all strips/tiles */
1113
4.10k
    sp->photometric = td->td_photometric;
1114
4.10k
    switch (sp->photometric)
1115
4.10k
    {
1116
576
        case PHOTOMETRIC_YCBCR:
1117
576
            sp->h_sampling = td->td_ycbcrsubsampling[0];
1118
576
            sp->v_sampling = td->td_ycbcrsubsampling[1];
1119
576
            break;
1120
3.53k
        default:
1121
            /* TIFF 6.0 forbids subsampling of all other color spaces */
1122
3.53k
            sp->h_sampling = 1;
1123
3.53k
            sp->v_sampling = 1;
1124
3.53k
            break;
1125
4.10k
    }
1126
1127
    /* Set up for reading normal data */
1128
4.10k
    TIFFjpeg_data_src(sp);
1129
4.10k
    tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */
1130
4.10k
    return (1);
1131
4.10k
}
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
7
{
1142
7
    int ret;
1143
7
    JPEGState state;
1144
1145
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1146
7
    if (tif->tif_dir.td_bitspersample == 12)
1147
0
        return TIFFJPEGIsFullStripRequired_12(tif);
1148
7
#endif
1149
1150
7
    memset(&state, 0, sizeof(JPEGState));
1151
7
    state.tif = tif;
1152
1153
7
    TIFFjpeg_create_decompress(&state);
1154
1155
7
    TIFFjpeg_data_src(&state);
1156
1157
7
    if (TIFFjpeg_read_header(&state, TRUE) != JPEG_HEADER_OK)
1158
7
    {
1159
7
        TIFFjpeg_destroy(&state);
1160
7
        return (0);
1161
7
    }
1162
0
    ret = TIFFjpeg_has_multiple_scans(&state);
1163
1164
0
    TIFFjpeg_destroy(&state);
1165
1166
0
    return ret;
1167
7
}
gdal_TIFFJPEGIsFullStripRequired
Line
Count
Source
1141
7
{
1142
7
    int ret;
1143
7
    JPEGState state;
1144
1145
7
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1146
7
    if (tif->tif_dir.td_bitspersample == 12)
1147
0
        return TIFFJPEGIsFullStripRequired_12(tif);
1148
7
#endif
1149
1150
7
    memset(&state, 0, sizeof(JPEGState));
1151
7
    state.tif = tif;
1152
1153
7
    TIFFjpeg_create_decompress(&state);
1154
1155
7
    TIFFjpeg_data_src(&state);
1156
1157
7
    if (TIFFjpeg_read_header(&state, TRUE) != JPEG_HEADER_OK)
1158
7
    {
1159
7
        TIFFjpeg_destroy(&state);
1160
7
        return (0);
1161
7
    }
1162
0
    ret = TIFFjpeg_has_multiple_scans(&state);
1163
1164
0
    TIFFjpeg_destroy(&state);
1165
1166
0
    return ret;
1167
7
}
Unexecuted instantiation: gdal_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
13.7k
{
1174
13.7k
    JPEGState *sp = JState(tif);
1175
13.7k
    TIFFDirectory *td = &tif->tif_dir;
1176
13.7k
    static const char module[] = "JPEGPreDecode";
1177
13.7k
    uint32_t segment_width, segment_height;
1178
13.7k
    int downsampled_output;
1179
13.7k
    int ci;
1180
1181
13.7k
    assert(sp != NULL);
1182
1183
13.7k
    if (sp->cinfo.comm.is_decompressor == 0)
1184
0
    {
1185
0
        tif->tif_setupdecode(tif);
1186
0
    }
1187
1188
13.7k
    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
13.7k
    if (!TIFFjpeg_abort(sp))
1194
0
        return (0);
1195
    /*
1196
     * Read the header for this strip/tile.
1197
     */
1198
1199
13.7k
    if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
1200
10.1k
        return (0);
1201
1202
3.59k
    tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
1203
3.59k
    tif->tif_rawcc = sp->src.bytes_in_buffer;
1204
1205
    /*
1206
     * Check image parameters and set decompression parameters.
1207
     */
1208
3.59k
    if (isTiled(tif))
1209
284
    {
1210
284
        segment_width = td->td_tilewidth;
1211
284
        segment_height = td->td_tilelength;
1212
284
        sp->bytesperline = TIFFTileRowSize(tif);
1213
284
    }
1214
3.30k
    else
1215
3.30k
    {
1216
3.30k
        segment_width = td->td_imagewidth;
1217
3.30k
        segment_height = td->td_imagelength - tif->tif_row;
1218
3.30k
        if (segment_height > td->td_rowsperstrip)
1219
204
            segment_height = td->td_rowsperstrip;
1220
3.30k
        sp->bytesperline = TIFFScanlineSize(tif);
1221
3.30k
    }
1222
3.59k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
1223
145
    {
1224
        /*
1225
         * For PC 2, scale down the expected strip/tile size
1226
         * to match a downsampled component
1227
         */
1228
145
        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
145
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
1235
145
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
1236
145
    }
1237
3.59k
    if (sp->cinfo.d.image_width < segment_width ||
1238
1.27k
        sp->cinfo.d.image_height < segment_height)
1239
2.74k
    {
1240
2.74k
        TIFFWarningExtR(tif, module,
1241
2.74k
                        "Improper JPEG strip/tile size, "
1242
2.74k
                        "expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1243
2.74k
                        segment_width, segment_height, sp->cinfo.d.image_width,
1244
2.74k
                        sp->cinfo.d.image_height);
1245
2.74k
    }
1246
3.59k
    if (sp->cinfo.d.image_width == segment_width &&
1247
1.08k
        sp->cinfo.d.image_height > segment_height &&
1248
486
        tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif))
1249
473
    {
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
473
        TIFFWarningExtR(tif, module,
1255
473
                        "JPEG strip size exceeds expected dimensions,"
1256
473
                        " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1257
473
                        segment_width, segment_height, sp->cinfo.d.image_width,
1258
473
                        sp->cinfo.d.image_height);
1259
473
    }
1260
3.11k
    else if (sp->cinfo.d.image_width > segment_width ||
1261
2.93k
             sp->cinfo.d.image_height > segment_height)
1262
281
    {
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
281
        TIFFErrorExtR(tif, module,
1270
281
                      "JPEG strip/tile size exceeds expected dimensions,"
1271
281
                      " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1272
281
                      segment_width, segment_height, sp->cinfo.d.image_width,
1273
281
                      sp->cinfo.d.image_height);
1274
281
        return (0);
1275
281
    }
1276
3.31k
    if (sp->cinfo.d.num_components !=
1277
3.31k
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
1278
3.31k
                                                    : 1))
1279
199
    {
1280
199
        TIFFErrorExtR(tif, module, "Improper JPEG component count");
1281
199
        return (0);
1282
199
    }
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
3.11k
    if (td->td_bitspersample != BITS_IN_JSAMPLE ||
1293
3.08k
        sp->cinfo.d.data_precision != td->td_bitspersample)
1294
26
    {
1295
26
        TIFFErrorExtR(tif, module, "Improper JPEG data precision");
1296
26
        return (0);
1297
26
    }
1298
3.08k
#endif
1299
1300
3.08k
    if (sp->cinfo.d.progressive_mode &&
1301
2.24k
        !sp->otherSettings.has_warned_about_progressive_mode)
1302
1.85k
    {
1303
1.85k
        TIFFWarningExtR(tif, module,
1304
1.85k
                        "The JPEG strip/tile is encoded with progressive mode, "
1305
1.85k
                        "which is normally not legal for JPEG-in-TIFF.\n"
1306
1.85k
                        "libtiff should be able to decode it, but it might "
1307
1.85k
                        "cause compatibility issues with other readers");
1308
1.85k
        sp->otherSettings.has_warned_about_progressive_mode = TRUE;
1309
1.85k
    }
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
3.08k
    if (TIFFjpeg_has_multiple_scans(sp))
1315
2.44k
    {
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
2.44k
        toff_t nRequiredMemory = 1024 * 1024;
1323
1324
6.65k
        for (ci = 0; ci < sp->cinfo.d.num_components; ci++)
1325
4.21k
        {
1326
4.21k
            const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]);
1327
4.21k
            if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0)
1328
4.21k
            {
1329
4.21k
                nRequiredMemory +=
1330
4.21k
                    (toff_t)(((compptr->width_in_blocks +
1331
4.21k
                               compptr->h_samp_factor - 1) /
1332
4.21k
                              compptr->h_samp_factor)) *
1333
4.21k
                    ((compptr->height_in_blocks + compptr->v_samp_factor - 1) /
1334
4.21k
                     compptr->v_samp_factor) *
1335
4.21k
                    sizeof(JBLOCK);
1336
4.21k
            }
1337
4.21k
        }
1338
1339
2.44k
        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
2.44k
    }
1358
1359
3.08k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1360
2.90k
    {
1361
        /* Component 0 should have expected sampling factors */
1362
2.90k
        if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
1363
2.87k
            sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling)
1364
40
        {
1365
40
            TIFFErrorExtR(tif, module,
1366
40
                          "Improper JPEG sampling factors %d,%d\n"
1367
40
                          "Apparently should be %" PRIu16 ",%" PRIu16 ".",
1368
40
                          sp->cinfo.d.comp_info[0].h_samp_factor,
1369
40
                          sp->cinfo.d.comp_info[0].v_samp_factor,
1370
40
                          sp->h_sampling, sp->v_sampling);
1371
40
            return (0);
1372
40
        }
1373
        /* Rest should have sampling factors 1,1 */
1374
5.14k
        for (ci = 1; ci < sp->cinfo.d.num_components; ci++)
1375
2.32k
        {
1376
2.32k
            if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
1377
2.30k
                sp->cinfo.d.comp_info[ci].v_samp_factor != 1)
1378
42
            {
1379
42
                TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1380
42
                return (0);
1381
42
            }
1382
2.32k
        }
1383
2.86k
    }
1384
184
    else
1385
184
    {
1386
        /* PC 2's single component should have sampling factors 1,1 */
1387
184
        if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
1388
172
            sp->cinfo.d.comp_info[0].v_samp_factor != 1)
1389
12
        {
1390
12
            TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1391
12
            return (0);
1392
12
        }
1393
184
    }
1394
2.99k
    downsampled_output = FALSE;
1395
2.99k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1396
2.82k
        sp->photometric == PHOTOMETRIC_YCBCR &&
1397
920
        sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1398
920
    {
1399
        /* Convert YCbCr to RGB */
1400
920
        sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
1401
920
        sp->cinfo.d.out_color_space = JCS_RGB;
1402
920
    }
1403
2.07k
    else
1404
2.07k
    {
1405
        /* Suppress colorspace handling */
1406
2.07k
        sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
1407
2.07k
        sp->cinfo.d.out_color_space = JCS_UNKNOWN;
1408
2.07k
        if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1409
1.90k
            (sp->h_sampling != 1 || sp->v_sampling != 1))
1410
0
            downsampled_output = TRUE;
1411
        /* XXX what about up-sampling? */
1412
2.07k
    }
1413
2.99k
    if (downsampled_output)
1414
0
    {
1415
        /* Need to use raw-data interface to libjpeg */
1416
0
        sp->cinfo.d.raw_data_out = TRUE;
1417
#if JPEG_LIB_VERSION >= 70
1418
0
        sp->cinfo.d.do_fancy_upsampling = FALSE;
1419
#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
2.99k
    else
1425
2.99k
    {
1426
        /* Use normal interface to libjpeg */
1427
2.99k
        sp->cinfo.d.raw_data_out = FALSE;
1428
2.99k
        tif->tif_decoderow = JPEGDecode;
1429
2.99k
        tif->tif_decodestrip = JPEGDecode;
1430
2.99k
        tif->tif_decodetile = JPEGDecode;
1431
2.99k
    }
1432
    /* Start JPEG decompressor */
1433
2.99k
    if (!TIFFjpeg_start_decompress(sp))
1434
1.20k
        return (0);
1435
    /* Allocate downsampled-data buffers if needed */
1436
1.79k
    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
1.79k
    return (1);
1444
1.79k
}
tif_jpeg.c:JPEGPreDecode
Line
Count
Source
1173
2.20k
{
1174
2.20k
    JPEGState *sp = JState(tif);
1175
2.20k
    TIFFDirectory *td = &tif->tif_dir;
1176
2.20k
    static const char module[] = "JPEGPreDecode";
1177
2.20k
    uint32_t segment_width, segment_height;
1178
2.20k
    int downsampled_output;
1179
2.20k
    int ci;
1180
1181
2.20k
    assert(sp != NULL);
1182
1183
2.20k
    if (sp->cinfo.comm.is_decompressor == 0)
1184
0
    {
1185
0
        tif->tif_setupdecode(tif);
1186
0
    }
1187
1188
2.20k
    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
2.20k
    if (!TIFFjpeg_abort(sp))
1194
0
        return (0);
1195
    /*
1196
     * Read the header for this strip/tile.
1197
     */
1198
1199
2.20k
    if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
1200
1.54k
        return (0);
1201
1202
653
    tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
1203
653
    tif->tif_rawcc = sp->src.bytes_in_buffer;
1204
1205
    /*
1206
     * Check image parameters and set decompression parameters.
1207
     */
1208
653
    if (isTiled(tif))
1209
237
    {
1210
237
        segment_width = td->td_tilewidth;
1211
237
        segment_height = td->td_tilelength;
1212
237
        sp->bytesperline = TIFFTileRowSize(tif);
1213
237
    }
1214
416
    else
1215
416
    {
1216
416
        segment_width = td->td_imagewidth;
1217
416
        segment_height = td->td_imagelength - tif->tif_row;
1218
416
        if (segment_height > td->td_rowsperstrip)
1219
190
            segment_height = td->td_rowsperstrip;
1220
416
        sp->bytesperline = TIFFScanlineSize(tif);
1221
416
    }
1222
653
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
1223
133
    {
1224
        /*
1225
         * For PC 2, scale down the expected strip/tile size
1226
         * to match a downsampled component
1227
         */
1228
133
        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
133
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
1235
133
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
1236
133
    }
1237
653
    if (sp->cinfo.d.image_width < segment_width ||
1238
442
        sp->cinfo.d.image_height < segment_height)
1239
290
    {
1240
290
        TIFFWarningExtR(tif, module,
1241
290
                        "Improper JPEG strip/tile size, "
1242
290
                        "expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1243
290
                        segment_width, segment_height, sp->cinfo.d.image_width,
1244
290
                        sp->cinfo.d.image_height);
1245
290
    }
1246
653
    if (sp->cinfo.d.image_width == segment_width &&
1247
377
        sp->cinfo.d.image_height > segment_height &&
1248
55
        tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif))
1249
47
    {
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
47
        TIFFWarningExtR(tif, module,
1255
47
                        "JPEG strip size exceeds expected dimensions,"
1256
47
                        " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1257
47
                        segment_width, segment_height, sp->cinfo.d.image_width,
1258
47
                        sp->cinfo.d.image_height);
1259
47
    }
1260
606
    else if (sp->cinfo.d.image_width > segment_width ||
1261
541
             sp->cinfo.d.image_height > segment_height)
1262
98
    {
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
98
        TIFFErrorExtR(tif, module,
1270
98
                      "JPEG strip/tile size exceeds expected dimensions,"
1271
98
                      " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1272
98
                      segment_width, segment_height, sp->cinfo.d.image_width,
1273
98
                      sp->cinfo.d.image_height);
1274
98
        return (0);
1275
98
    }
1276
555
    if (sp->cinfo.d.num_components !=
1277
555
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
1278
555
                                                    : 1))
1279
79
    {
1280
79
        TIFFErrorExtR(tif, module, "Improper JPEG component count");
1281
79
        return (0);
1282
79
    }
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
476
    if (td->td_bitspersample != BITS_IN_JSAMPLE ||
1293
450
        sp->cinfo.d.data_precision != td->td_bitspersample)
1294
26
    {
1295
26
        TIFFErrorExtR(tif, module, "Improper JPEG data precision");
1296
26
        return (0);
1297
26
    }
1298
450
#endif
1299
1300
450
    if (sp->cinfo.d.progressive_mode &&
1301
58
        !sp->otherSettings.has_warned_about_progressive_mode)
1302
27
    {
1303
27
        TIFFWarningExtR(tif, module,
1304
27
                        "The JPEG strip/tile is encoded with progressive mode, "
1305
27
                        "which is normally not legal for JPEG-in-TIFF.\n"
1306
27
                        "libtiff should be able to decode it, but it might "
1307
27
                        "cause compatibility issues with other readers");
1308
27
        sp->otherSettings.has_warned_about_progressive_mode = TRUE;
1309
27
    }
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
450
    if (TIFFjpeg_has_multiple_scans(sp))
1315
90
    {
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
90
        toff_t nRequiredMemory = 1024 * 1024;
1323
1324
405
        for (ci = 0; ci < sp->cinfo.d.num_components; ci++)
1325
315
        {
1326
315
            const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]);
1327
315
            if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0)
1328
315
            {
1329
315
                nRequiredMemory +=
1330
315
                    (toff_t)(((compptr->width_in_blocks +
1331
315
                               compptr->h_samp_factor - 1) /
1332
315
                              compptr->h_samp_factor)) *
1333
315
                    ((compptr->height_in_blocks + compptr->v_samp_factor - 1) /
1334
315
                     compptr->v_samp_factor) *
1335
315
                    sizeof(JBLOCK);
1336
315
            }
1337
315
        }
1338
1339
90
        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
90
    }
1358
1359
450
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1360
276
    {
1361
        /* Component 0 should have expected sampling factors */
1362
276
        if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
1363
267
            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
838
        for (ci = 1; ci < sp->cinfo.d.num_components; ci++)
1375
597
        {
1376
597
            if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
1377
590
                sp->cinfo.d.comp_info[ci].v_samp_factor != 1)
1378
21
            {
1379
21
                TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1380
21
                return (0);
1381
21
            }
1382
597
        }
1383
262
    }
1384
174
    else
1385
174
    {
1386
        /* PC 2's single component should have sampling factors 1,1 */
1387
174
        if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
1388
164
            sp->cinfo.d.comp_info[0].v_samp_factor != 1)
1389
10
        {
1390
10
            TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1391
10
            return (0);
1392
10
        }
1393
174
    }
1394
405
    downsampled_output = FALSE;
1395
405
    if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1396
241
        sp->photometric == PHOTOMETRIC_YCBCR &&
1397
119
        sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1398
119
    {
1399
        /* Convert YCbCr to RGB */
1400
119
        sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
1401
119
        sp->cinfo.d.out_color_space = JCS_RGB;
1402
119
    }
1403
286
    else
1404
286
    {
1405
        /* Suppress colorspace handling */
1406
286
        sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
1407
286
        sp->cinfo.d.out_color_space = JCS_UNKNOWN;
1408
286
        if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1409
122
            (sp->h_sampling != 1 || sp->v_sampling != 1))
1410
0
            downsampled_output = TRUE;
1411
        /* XXX what about up-sampling? */
1412
286
    }
1413
405
    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
405
    else
1425
405
    {
1426
        /* Use normal interface to libjpeg */
1427
405
        sp->cinfo.d.raw_data_out = FALSE;
1428
405
        tif->tif_decoderow = JPEGDecode;
1429
405
        tif->tif_decodestrip = JPEGDecode;
1430
405
        tif->tif_decodetile = JPEGDecode;
1431
405
    }
1432
    /* Start JPEG decompressor */
1433
405
    if (!TIFFjpeg_start_decompress(sp))
1434
58
        return (0);
1435
    /* Allocate downsampled-data buffers if needed */
1436
347
    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
347
    return (1);
1444
347
}
tif_jpeg_12.c:JPEGPreDecode
Line
Count
Source
1173
11.5k
{
1174
11.5k
    JPEGState *sp = JState(tif);
1175
11.5k
    TIFFDirectory *td = &tif->tif_dir;
1176
11.5k
    static const char module[] = "JPEGPreDecode";
1177
11.5k
    uint32_t segment_width, segment_height;
1178
11.5k
    int downsampled_output;
1179
11.5k
    int ci;
1180
1181
11.5k
    assert(sp != NULL);
1182
1183
11.5k
    if (sp->cinfo.comm.is_decompressor == 0)
1184
0
    {
1185
0
        tif->tif_setupdecode(tif);
1186
0
    }
1187
1188
11.5k
    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.5k
    if (!TIFFjpeg_abort(sp))
1194
0
        return (0);
1195
    /*
1196
     * Read the header for this strip/tile.
1197
     */
1198
1199
11.5k
    if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
1200
8.63k
        return (0);
1201
1202
2.93k
    tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
1203
2.93k
    tif->tif_rawcc = sp->src.bytes_in_buffer;
1204
1205
    /*
1206
     * Check image parameters and set decompression parameters.
1207
     */
1208
2.93k
    if (isTiled(tif))
1209
47
    {
1210
47
        segment_width = td->td_tilewidth;
1211
47
        segment_height = td->td_tilelength;
1212
47
        sp->bytesperline = TIFFTileRowSize(tif);
1213
47
    }
1214
2.89k
    else
1215
2.89k
    {
1216
2.89k
        segment_width = td->td_imagewidth;
1217
2.89k
        segment_height = td->td_imagelength - tif->tif_row;
1218
2.89k
        if (segment_height > td->td_rowsperstrip)
1219
14
            segment_height = td->td_rowsperstrip;
1220
2.89k
        sp->bytesperline = TIFFScanlineSize(tif);
1221
2.89k
    }
1222
2.93k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
1223
12
    {
1224
        /*
1225
         * For PC 2, scale down the expected strip/tile size
1226
         * to match a downsampled component
1227
         */
1228
12
        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
12
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
1235
12
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
1236
12
    }
1237
2.93k
    if (sp->cinfo.d.image_width < segment_width ||
1238
829
        sp->cinfo.d.image_height < segment_height)
1239
2.45k
    {
1240
2.45k
        TIFFWarningExtR(tif, module,
1241
2.45k
                        "Improper JPEG strip/tile size, "
1242
2.45k
                        "expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1243
2.45k
                        segment_width, segment_height, sp->cinfo.d.image_width,
1244
2.45k
                        sp->cinfo.d.image_height);
1245
2.45k
    }
1246
2.93k
    if (sp->cinfo.d.image_width == segment_width &&
1247
711
        sp->cinfo.d.image_height > segment_height &&
1248
431
        tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif))
1249
426
    {
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
426
        TIFFWarningExtR(tif, module,
1255
426
                        "JPEG strip size exceeds expected dimensions,"
1256
426
                        " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1257
426
                        segment_width, segment_height, sp->cinfo.d.image_width,
1258
426
                        sp->cinfo.d.image_height);
1259
426
    }
1260
2.51k
    else if (sp->cinfo.d.image_width > segment_width ||
1261
2.39k
             sp->cinfo.d.image_height > segment_height)
1262
183
    {
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
183
        TIFFErrorExtR(tif, module,
1270
183
                      "JPEG strip/tile size exceeds expected dimensions,"
1271
183
                      " expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
1272
183
                      segment_width, segment_height, sp->cinfo.d.image_width,
1273
183
                      sp->cinfo.d.image_height);
1274
183
        return (0);
1275
183
    }
1276
2.75k
    if (sp->cinfo.d.num_components !=
1277
2.75k
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
1278
2.75k
                                                    : 1))
1279
120
    {
1280
120
        TIFFErrorExtR(tif, module, "Improper JPEG component count");
1281
120
        return (0);
1282
120
    }
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
2.63k
    if (td->td_bitspersample != BITS_IN_JSAMPLE ||
1293
2.63k
        sp->cinfo.d.data_precision != td->td_bitspersample)
1294
0
    {
1295
0
        TIFFErrorExtR(tif, module, "Improper JPEG data precision");
1296
0
        return (0);
1297
0
    }
1298
2.63k
#endif
1299
1300
2.63k
    if (sp->cinfo.d.progressive_mode &&
1301
2.18k
        !sp->otherSettings.has_warned_about_progressive_mode)
1302
1.83k
    {
1303
1.83k
        TIFFWarningExtR(tif, module,
1304
1.83k
                        "The JPEG strip/tile is encoded with progressive mode, "
1305
1.83k
                        "which is normally not legal for JPEG-in-TIFF.\n"
1306
1.83k
                        "libtiff should be able to decode it, but it might "
1307
1.83k
                        "cause compatibility issues with other readers");
1308
1.83k
        sp->otherSettings.has_warned_about_progressive_mode = TRUE;
1309
1.83k
    }
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
2.63k
    if (TIFFjpeg_has_multiple_scans(sp))
1315
2.35k
    {
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
2.35k
        toff_t nRequiredMemory = 1024 * 1024;
1323
1324
6.25k
        for (ci = 0; ci < sp->cinfo.d.num_components; ci++)
1325
3.89k
        {
1326
3.89k
            const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]);
1327
3.89k
            if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0)
1328
3.89k
            {
1329
3.89k
                nRequiredMemory +=
1330
3.89k
                    (toff_t)(((compptr->width_in_blocks +
1331
3.89k
                               compptr->h_samp_factor - 1) /
1332
3.89k
                              compptr->h_samp_factor)) *
1333
3.89k
                    ((compptr->height_in_blocks + compptr->v_samp_factor - 1) /
1334
3.89k
                     compptr->v_samp_factor) *
1335
3.89k
                    sizeof(JBLOCK);
1336
3.89k
            }
1337
3.89k
        }
1338
1339
2.35k
        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
2.35k
    }
1358
1359
2.63k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1360
2.62k
    {
1361
        /* Component 0 should have expected sampling factors */
1362
2.62k
        if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
1363
2.61k
            sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling)
1364
26
        {
1365
26
            TIFFErrorExtR(tif, module,
1366
26
                          "Improper JPEG sampling factors %d,%d\n"
1367
26
                          "Apparently should be %" PRIu16 ",%" PRIu16 ".",
1368
26
                          sp->cinfo.d.comp_info[0].h_samp_factor,
1369
26
                          sp->cinfo.d.comp_info[0].v_samp_factor,
1370
26
                          sp->h_sampling, sp->v_sampling);
1371
26
            return (0);
1372
26
        }
1373
        /* Rest should have sampling factors 1,1 */
1374
4.30k
        for (ci = 1; ci < sp->cinfo.d.num_components; ci++)
1375
1.72k
        {
1376
1.72k
            if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
1377
1.71k
                sp->cinfo.d.comp_info[ci].v_samp_factor != 1)
1378
21
            {
1379
21
                TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1380
21
                return (0);
1381
21
            }
1382
1.72k
        }
1383
2.60k
    }
1384
10
    else
1385
10
    {
1386
        /* PC 2's single component should have sampling factors 1,1 */
1387
10
        if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
1388
8
            sp->cinfo.d.comp_info[0].v_samp_factor != 1)
1389
2
        {
1390
2
            TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
1391
2
            return (0);
1392
2
        }
1393
10
    }
1394
2.58k
    downsampled_output = FALSE;
1395
2.58k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1396
2.57k
        sp->photometric == PHOTOMETRIC_YCBCR &&
1397
801
        sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1398
801
    {
1399
        /* Convert YCbCr to RGB */
1400
801
        sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
1401
801
        sp->cinfo.d.out_color_space = JCS_RGB;
1402
801
    }
1403
1.78k
    else
1404
1.78k
    {
1405
        /* Suppress colorspace handling */
1406
1.78k
        sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
1407
1.78k
        sp->cinfo.d.out_color_space = JCS_UNKNOWN;
1408
1.78k
        if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
1409
1.77k
            (sp->h_sampling != 1 || sp->v_sampling != 1))
1410
0
            downsampled_output = TRUE;
1411
        /* XXX what about up-sampling? */
1412
1.78k
    }
1413
2.58k
    if (downsampled_output)
1414
0
    {
1415
        /* Need to use raw-data interface to libjpeg */
1416
0
        sp->cinfo.d.raw_data_out = TRUE;
1417
#if JPEG_LIB_VERSION >= 70
1418
        sp->cinfo.d.do_fancy_upsampling = FALSE;
1419
#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
2.58k
    else
1425
2.58k
    {
1426
        /* Use normal interface to libjpeg */
1427
2.58k
        sp->cinfo.d.raw_data_out = FALSE;
1428
2.58k
        tif->tif_decoderow = JPEGDecode;
1429
2.58k
        tif->tif_decodestrip = JPEGDecode;
1430
2.58k
        tif->tif_decodetile = JPEGDecode;
1431
2.58k
    }
1432
    /* Start JPEG decompressor */
1433
2.58k
    if (!TIFFjpeg_start_decompress(sp))
1434
1.14k
        return (0);
1435
    /* Allocate downsampled-data buffers if needed */
1436
1.44k
    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
1.44k
    return (1);
1444
1.44k
}
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
1.27k
{
1453
1.27k
    JPEGState *sp = JState(tif);
1454
1.27k
    tmsize_t nrows;
1455
1.27k
    (void)s;
1456
1457
    /*
1458
    ** Update available information, buffer may have been refilled
1459
    ** between decode requests
1460
    */
1461
1.27k
    sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp;
1462
1.27k
    sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
1463
1464
1.27k
    if (sp->bytesperline == 0)
1465
0
    {
1466
0
        memset(buf, 0, (size_t)cc);
1467
0
        return 0;
1468
0
    }
1469
1470
1.27k
    nrows = cc / sp->bytesperline;
1471
1.27k
    if (cc % sp->bytesperline)
1472
0
        TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read");
1473
1474
1.27k
    if (nrows > (tmsize_t)sp->cinfo.d.image_height)
1475
61
        nrows = sp->cinfo.d.image_height;
1476
1477
    /* data is expected to be read in multiples of a scanline */
1478
1.27k
    if (nrows)
1479
1.27k
    {
1480
1.27k
        do
1481
21.7k
        {
1482
            /*
1483
             * In the libjpeg6b-9a 8bit case.  We read directly into
1484
             * the TIFF buffer.
1485
             */
1486
21.7k
            JSAMPROW bufptr = (JSAMPROW)buf;
1487
1488
21.7k
            if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1)
1489
13
            {
1490
13
                memset(buf, 0, (size_t)cc);
1491
13
                return (0);
1492
13
            }
1493
1494
21.7k
            ++tif->tif_row;
1495
21.7k
            buf += sp->bytesperline;
1496
21.7k
            cc -= sp->bytesperline;
1497
21.7k
        } while (--nrows > 0);
1498
1.27k
    }
1499
1500
    /* Update information on consumed data */
1501
1.26k
    tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
1502
1.26k
    tif->tif_rawcc = sp->src.bytes_in_buffer;
1503
1504
    /* Close down the decompressor if we've finished the strip or tile. */
1505
1.26k
    return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
1506
294
           TIFFjpeg_finish_decompress(sp);
1507
1.27k
}
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
1.44k
{
1514
1.44k
    JPEGState *sp = JState(tif);
1515
1.44k
    tmsize_t nrows;
1516
1.44k
    (void)s;
1517
1518
    /*
1519
    ** Update available information, buffer may have been refilled
1520
    ** between decode requests
1521
    */
1522
1.44k
    sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp;
1523
1.44k
    sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
1524
1525
1.44k
    if (sp->bytesperline == 0)
1526
0
    {
1527
0
        memset(buf, 0, (size_t)cc);
1528
0
        return 0;
1529
0
    }
1530
1531
1.44k
    nrows = cc / sp->bytesperline;
1532
1.44k
    if (cc % sp->bytesperline)
1533
0
        TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read");
1534
1535
1.44k
    if (nrows > (tmsize_t)sp->cinfo.d.image_height)
1536
1.18k
        nrows = sp->cinfo.d.image_height;
1537
1538
    /* data is expected to be read in multiples of a scanline */
1539
1.44k
    if (nrows)
1540
1.44k
    {
1541
1.44k
        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
1.44k
        if (sp->cinfo.d.data_precision == 12)
1548
1.44k
        {
1549
1.44k
            line_work_buf = (TIFF_JSAMPROW)_TIFFmallocExt(
1550
1.44k
                tif, sizeof(short) * sp->cinfo.d.output_width *
1551
1.44k
                         sp->cinfo.d.num_components);
1552
1.44k
        }
1553
1554
1.44k
        do
1555
4.59M
        {
1556
4.59M
            if (line_work_buf != NULL)
1557
4.59M
            {
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
4.59M
                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
4.59M
                if (sp->cinfo.d.data_precision == 12)
1572
4.59M
                {
1573
4.59M
                    int value_pairs = (sp->cinfo.d.output_width *
1574
4.59M
                                       sp->cinfo.d.num_components) /
1575
4.59M
                                      2;
1576
4.59M
                    int iPair;
1577
1578
125M
                    for (iPair = 0; iPair < value_pairs; iPair++)
1579
120M
                    {
1580
120M
                        unsigned char *out_ptr =
1581
120M
                            ((unsigned char *)buf) + iPair * 3;
1582
120M
                        TIFF_JSAMPLE *in_ptr = line_work_buf + iPair * 2;
1583
1584
120M
                        out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4);
1585
120M
                        out_ptr[1] =
1586
120M
                            (unsigned char)(((in_ptr[0] & 0xf) << 4) |
1587
120M
                                            ((in_ptr[1] & 0xf00) >> 8));
1588
120M
                        out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0));
1589
120M
                    }
1590
4.59M
                }
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
4.59M
            }
1604
1605
4.59M
            ++tif->tif_row;
1606
4.59M
            buf += sp->bytesperline;
1607
4.59M
            cc -= sp->bytesperline;
1608
4.59M
        } while (--nrows > 0);
1609
1610
1.44k
        if (line_work_buf != NULL)
1611
1.44k
            _TIFFfreeExt(tif, line_work_buf);
1612
1.44k
    }
1613
1614
    /* Update information on consumed data */
1615
1.44k
    tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
1616
1.44k
    tif->tif_rawcc = sp->src.bytes_in_buffer;
1617
1618
    /* Close down the decompressor if we've finished the strip or tile. */
1619
1.44k
    return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
1620
1.26k
           TIFFjpeg_finish_decompress(sp);
1621
1.44k
}
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
0
        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
0
    _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
142k
{
1827
142k
    JQUANT_TBL *qtbl;
1828
1829
142k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1830
142k
        qtbl->sent_table = FALSE;
1831
142k
}
tif_jpeg.c:unsuppress_quant_table
Line
Count
Source
1826
74.7k
{
1827
74.7k
    JQUANT_TBL *qtbl;
1828
1829
74.7k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1830
74.7k
        qtbl->sent_table = FALSE;
1831
74.7k
}
tif_jpeg_12.c:unsuppress_quant_table
Line
Count
Source
1826
68.2k
{
1827
68.2k
    JQUANT_TBL *qtbl;
1828
1829
68.2k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1830
68.2k
        qtbl->sent_table = FALSE;
1831
68.2k
}
1832
1833
static void suppress_quant_table(JPEGState *sp, int tblno)
1834
526k
{
1835
526k
    JQUANT_TBL *qtbl;
1836
1837
526k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1838
526k
        qtbl->sent_table = TRUE;
1839
526k
}
tif_jpeg.c:suppress_quant_table
Line
Count
Source
1834
290k
{
1835
290k
    JQUANT_TBL *qtbl;
1836
1837
290k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1838
290k
        qtbl->sent_table = TRUE;
1839
290k
}
tif_jpeg_12.c:suppress_quant_table
Line
Count
Source
1834
236k
{
1835
236k
    JQUANT_TBL *qtbl;
1836
1837
236k
    if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
1838
236k
        qtbl->sent_table = TRUE;
1839
236k
}
1840
1841
static void unsuppress_huff_table(JPEGState *sp, int tblno)
1842
0
{
1843
0
    JHUFF_TBL *htbl;
1844
1845
0
    if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
1846
0
        htbl->sent_table = FALSE;
1847
0
    if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
1848
0
        htbl->sent_table = FALSE;
1849
0
}
Unexecuted instantiation: tif_jpeg.c:unsuppress_huff_table
Unexecuted instantiation: tif_jpeg_12.c:unsuppress_huff_table
1850
1851
static void suppress_huff_table(JPEGState *sp, int tblno)
1852
263k
{
1853
263k
    JHUFF_TBL *htbl;
1854
1855
263k
    if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
1856
263k
        htbl->sent_table = TRUE;
1857
263k
    if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
1858
263k
        htbl->sent_table = TRUE;
1859
263k
}
tif_jpeg.c:suppress_huff_table
Line
Count
Source
1852
145k
{
1853
145k
    JHUFF_TBL *htbl;
1854
1855
145k
    if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
1856
145k
        htbl->sent_table = TRUE;
1857
145k
    if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
1858
145k
        htbl->sent_table = TRUE;
1859
145k
}
tif_jpeg_12.c:suppress_huff_table
Line
Count
Source
1852
118k
{
1853
118k
    JHUFF_TBL *htbl;
1854
1855
118k
    if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
1856
118k
        htbl->sent_table = TRUE;
1857
118k
    if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
1858
118k
        htbl->sent_table = TRUE;
1859
118k
}
1860
1861
static int prepare_JPEGTables(TIFF *tif)
1862
131k
{
1863
131k
    JPEGState *sp = JState(tif);
1864
1865
    /* Initialize quant tables for current quality setting */
1866
131k
    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
131k
    if (!TIFFjpeg_suppress_tables(sp, TRUE))
1871
0
        return (0);
1872
131k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
1873
131k
    {
1874
131k
        unsuppress_quant_table(sp, 0);
1875
131k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1876
11.3k
            unsuppress_quant_table(sp, 1);
1877
131k
    }
1878
131k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
1879
0
    {
1880
0
        unsuppress_huff_table(sp, 0);
1881
0
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1882
0
            unsuppress_huff_table(sp, 1);
1883
0
    }
1884
    /* Direct libjpeg output into otherSettings.jpegtables */
1885
131k
    if (!TIFFjpeg_tables_dest(sp, tif))
1886
0
        return (0);
1887
    /* Emit tables-only datastream */
1888
131k
    if (!TIFFjpeg_write_tables(sp))
1889
0
        return (0);
1890
1891
131k
    return (1);
1892
131k
}
tif_jpeg.c:prepare_JPEGTables
Line
Count
Source
1862
72.6k
{
1863
72.6k
    JPEGState *sp = JState(tif);
1864
1865
    /* Initialize quant tables for current quality setting */
1866
72.6k
    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
72.6k
    if (!TIFFjpeg_suppress_tables(sp, TRUE))
1871
0
        return (0);
1872
72.6k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
1873
72.6k
    {
1874
72.6k
        unsuppress_quant_table(sp, 0);
1875
72.6k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1876
2.15k
            unsuppress_quant_table(sp, 1);
1877
72.6k
    }
1878
72.6k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
1879
0
    {
1880
0
        unsuppress_huff_table(sp, 0);
1881
0
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1882
0
            unsuppress_huff_table(sp, 1);
1883
0
    }
1884
    /* Direct libjpeg output into otherSettings.jpegtables */
1885
72.6k
    if (!TIFFjpeg_tables_dest(sp, tif))
1886
0
        return (0);
1887
    /* Emit tables-only datastream */
1888
72.6k
    if (!TIFFjpeg_write_tables(sp))
1889
0
        return (0);
1890
1891
72.6k
    return (1);
1892
72.6k
}
tif_jpeg_12.c:prepare_JPEGTables
Line
Count
Source
1862
59.0k
{
1863
59.0k
    JPEGState *sp = JState(tif);
1864
1865
    /* Initialize quant tables for current quality setting */
1866
59.0k
    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
59.0k
    if (!TIFFjpeg_suppress_tables(sp, TRUE))
1871
0
        return (0);
1872
59.0k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
1873
59.0k
    {
1874
59.0k
        unsuppress_quant_table(sp, 0);
1875
59.0k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1876
9.21k
            unsuppress_quant_table(sp, 1);
1877
59.0k
    }
1878
59.0k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
1879
0
    {
1880
0
        unsuppress_huff_table(sp, 0);
1881
0
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1882
0
            unsuppress_huff_table(sp, 1);
1883
0
    }
1884
    /* Direct libjpeg output into otherSettings.jpegtables */
1885
59.0k
    if (!TIFFjpeg_tables_dest(sp, tif))
1886
0
        return (0);
1887
    /* Emit tables-only datastream */
1888
59.0k
    if (!TIFFjpeg_write_tables(sp))
1889
0
        return (0);
1890
1891
59.0k
    return (1);
1892
59.0k
}
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
500k
{
1924
500k
    JPEGState *sp = JState(tif);
1925
500k
    TIFFDirectory *td = &tif->tif_dir;
1926
500k
    static const char module[] = "JPEGSetupEncode";
1927
1928
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1929
382k
    if (tif->tif_dir.td_bitspersample == 12)
1930
118k
    {
1931
        /* We pass a pointer to a copy of otherSettings, since */
1932
        /* TIFFReInitJPEG_12() will clear sp */
1933
118k
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1934
118k
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 1);
1935
118k
    }
1936
264k
#endif
1937
1938
382k
    JPEGInitializeLibJPEG(tif, FALSE);
1939
1940
264k
    assert(sp != NULL);
1941
382k
    assert(!sp->cinfo.comm.is_decompressor);
1942
1943
382k
    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
382k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1951
382k
    {
1952
382k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
1953
382k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1954
25.5k
        {
1955
25.5k
            if (sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1956
25.5k
            {
1957
25.5k
                sp->cinfo.c.in_color_space = JCS_RGB;
1958
25.5k
            }
1959
0
            else
1960
0
            {
1961
0
                sp->cinfo.c.in_color_space = JCS_YCbCr;
1962
0
            }
1963
25.5k
        }
1964
357k
        else
1965
357k
        {
1966
357k
            if ((td->td_photometric == PHOTOMETRIC_MINISWHITE ||
1967
357k
                 td->td_photometric == PHOTOMETRIC_MINISBLACK) &&
1968
338k
                td->td_samplesperpixel == 1)
1969
316k
                sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
1970
40.3k
            else if (td->td_photometric == PHOTOMETRIC_RGB &&
1971
13.7k
                     td->td_samplesperpixel == 3)
1972
4.92k
                sp->cinfo.c.in_color_space = JCS_RGB;
1973
35.4k
            else if (td->td_photometric == PHOTOMETRIC_SEPARATED &&
1974
5.05k
                     td->td_samplesperpixel == 4)
1975
5.05k
                sp->cinfo.c.in_color_space = JCS_CMYK;
1976
30.3k
            else
1977
30.3k
                sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1978
357k
        }
1979
382k
    }
1980
0
    else
1981
0
    {
1982
0
        sp->cinfo.c.input_components = 1;
1983
0
        sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1984
0
    }
1985
382k
    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
382k
    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
382k
    sp->cinfo.c.num_scans = 0;
2004
382k
    sp->cinfo.c.scan_info = NULL;
2005
2006
    /* Set per-file parameters */
2007
382k
    switch (sp->photometric)
2008
382k
    {
2009
25.5k
        case PHOTOMETRIC_YCBCR:
2010
25.5k
            sp->h_sampling = td->td_ycbcrsubsampling[0];
2011
25.5k
            sp->v_sampling = td->td_ycbcrsubsampling[1];
2012
25.5k
            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
25.5k
            if (td->td_bitspersample > 16)
2019
1.41k
            {
2020
1.41k
                TIFFErrorExtR(tif, module,
2021
1.41k
                              "BitsPerSample %" PRIu16 " not allowed for JPEG",
2022
1.41k
                              td->td_bitspersample);
2023
1.41k
                return (0);
2024
1.41k
            }
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
24.1k
            {
2032
24.1k
                float *ref;
2033
24.1k
                if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE, &ref))
2034
12.7k
                {
2035
12.7k
                    float refbw[6];
2036
12.7k
                    long top = 1L << td->td_bitspersample;
2037
12.7k
                    refbw[0] = 0;
2038
12.7k
                    refbw[1] = (float)(top - 1L);
2039
12.7k
                    refbw[2] = (float)(top >> 1);
2040
12.7k
                    refbw[3] = refbw[1];
2041
12.7k
                    refbw[4] = refbw[2];
2042
12.7k
                    refbw[5] = refbw[1];
2043
12.7k
                    TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw);
2044
12.7k
                }
2045
24.1k
            }
2046
24.1k
            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
357k
        default:
2055
            /* TIFF 6.0 forbids subsampling of all other color spaces */
2056
357k
            sp->h_sampling = 1;
2057
357k
            sp->v_sampling = 1;
2058
357k
            break;
2059
382k
    }
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
381k
    if (td->td_bitspersample != BITS_IN_JSAMPLE)
2073
118k
#endif
2074
118k
    {
2075
118k
        TIFFErrorExtR(tif, module,
2076
118k
                      "BitsPerSample %" PRIu16 " not allowed for JPEG",
2077
118k
                      td->td_bitspersample);
2078
118k
        return (0);
2079
118k
    }
2080
263k
    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
263k
    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
263k
    else
2102
263k
    {
2103
263k
        if (td->td_rowsperstrip < td->td_imagelength &&
2104
0
            (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
263k
    }
2113
2114
    /* Create a JPEGTables field if appropriate */
2115
263k
    if (sp->otherSettings.jpegtablesmode &
2116
263k
        (JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF))
2117
263k
    {
2118
263k
        if (sp->otherSettings.jpegtables == NULL ||
2119
131k
            memcmp(sp->otherSettings.jpegtables, "\0\0\0\0\0\0\0\0\0", 8) == 0)
2120
131k
        {
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
131k
            if (!prepare_JPEGTables(tif))
2137
0
                return (0);
2138
            /* Mark the field present */
2139
            /* Can't use TIFFSetField since BEENWRITING is already set! */
2140
131k
            tif->tif_flags |= TIFF_DIRTYDIRECT;
2141
131k
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2142
131k
        }
2143
263k
    }
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
263k
    TIFFjpeg_data_dest(sp, tif);
2153
2154
263k
    return (1);
2155
263k
}
tif_jpeg.c:JPEGSetupEncode
Line
Count
Source
1923
382k
{
1924
382k
    JPEGState *sp = JState(tif);
1925
382k
    TIFFDirectory *td = &tif->tif_dir;
1926
382k
    static const char module[] = "JPEGSetupEncode";
1927
1928
382k
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1929
382k
    if (tif->tif_dir.td_bitspersample == 12)
1930
118k
    {
1931
        /* We pass a pointer to a copy of otherSettings, since */
1932
        /* TIFFReInitJPEG_12() will clear sp */
1933
118k
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1934
118k
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 1);
1935
118k
    }
1936
264k
#endif
1937
1938
264k
    JPEGInitializeLibJPEG(tif, FALSE);
1939
1940
264k
    assert(sp != NULL);
1941
264k
    assert(!sp->cinfo.comm.is_decompressor);
1942
1943
264k
    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
264k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1951
264k
    {
1952
264k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
1953
264k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1954
7.14k
        {
1955
7.14k
            if (sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1956
7.14k
            {
1957
7.14k
                sp->cinfo.c.in_color_space = JCS_RGB;
1958
7.14k
            }
1959
0
            else
1960
0
            {
1961
0
                sp->cinfo.c.in_color_space = JCS_YCbCr;
1962
0
            }
1963
7.14k
        }
1964
257k
        else
1965
257k
        {
1966
257k
            if ((td->td_photometric == PHOTOMETRIC_MINISWHITE ||
1967
257k
                 td->td_photometric == PHOTOMETRIC_MINISBLACK) &&
1968
240k
                td->td_samplesperpixel == 1)
1969
226k
                sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
1970
30.8k
            else if (td->td_photometric == PHOTOMETRIC_RGB &&
1971
13.7k
                     td->td_samplesperpixel == 3)
1972
4.92k
                sp->cinfo.c.in_color_space = JCS_RGB;
1973
25.9k
            else if (td->td_photometric == PHOTOMETRIC_SEPARATED &&
1974
3.03k
                     td->td_samplesperpixel == 4)
1975
3.03k
                sp->cinfo.c.in_color_space = JCS_CMYK;
1976
22.8k
            else
1977
22.8k
                sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1978
257k
        }
1979
264k
    }
1980
0
    else
1981
0
    {
1982
0
        sp->cinfo.c.input_components = 1;
1983
0
        sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1984
0
    }
1985
264k
    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
264k
    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
264k
    sp->cinfo.c.num_scans = 0;
2004
264k
    sp->cinfo.c.scan_info = NULL;
2005
2006
    /* Set per-file parameters */
2007
264k
    switch (sp->photometric)
2008
264k
    {
2009
7.14k
        case PHOTOMETRIC_YCBCR:
2010
7.14k
            sp->h_sampling = td->td_ycbcrsubsampling[0];
2011
7.14k
            sp->v_sampling = td->td_ycbcrsubsampling[1];
2012
7.14k
            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
7.14k
            if (td->td_bitspersample > 16)
2019
1.41k
            {
2020
1.41k
                TIFFErrorExtR(tif, module,
2021
1.41k
                              "BitsPerSample %" PRIu16 " not allowed for JPEG",
2022
1.41k
                              td->td_bitspersample);
2023
1.41k
                return (0);
2024
1.41k
            }
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
5.73k
            {
2032
5.73k
                float *ref;
2033
5.73k
                if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE, &ref))
2034
3.57k
                {
2035
3.57k
                    float refbw[6];
2036
3.57k
                    long top = 1L << td->td_bitspersample;
2037
3.57k
                    refbw[0] = 0;
2038
3.57k
                    refbw[1] = (float)(top - 1L);
2039
3.57k
                    refbw[2] = (float)(top >> 1);
2040
3.57k
                    refbw[3] = refbw[1];
2041
3.57k
                    refbw[4] = refbw[2];
2042
3.57k
                    refbw[5] = refbw[1];
2043
3.57k
                    TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw);
2044
3.57k
                }
2045
5.73k
            }
2046
5.73k
            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
257k
        default:
2055
            /* TIFF 6.0 forbids subsampling of all other color spaces */
2056
257k
            sp->h_sampling = 1;
2057
257k
            sp->v_sampling = 1;
2058
257k
            break;
2059
264k
    }
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
263k
    if (td->td_bitspersample != BITS_IN_JSAMPLE)
2073
118k
#endif
2074
118k
    {
2075
118k
        TIFFErrorExtR(tif, module,
2076
118k
                      "BitsPerSample %" PRIu16 " not allowed for JPEG",
2077
118k
                      td->td_bitspersample);
2078
118k
        return (0);
2079
118k
    }
2080
145k
    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
145k
    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
145k
    else
2102
145k
    {
2103
145k
        if (td->td_rowsperstrip < td->td_imagelength &&
2104
0
            (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
145k
    }
2113
2114
    /* Create a JPEGTables field if appropriate */
2115
145k
    if (sp->otherSettings.jpegtablesmode &
2116
145k
        (JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF))
2117
145k
    {
2118
145k
        if (sp->otherSettings.jpegtables == NULL ||
2119
72.6k
            memcmp(sp->otherSettings.jpegtables, "\0\0\0\0\0\0\0\0\0", 8) == 0)
2120
72.6k
        {
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
72.6k
            if (!prepare_JPEGTables(tif))
2137
0
                return (0);
2138
            /* Mark the field present */
2139
            /* Can't use TIFFSetField since BEENWRITING is already set! */
2140
72.6k
            tif->tif_flags |= TIFF_DIRTYDIRECT;
2141
72.6k
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2142
72.6k
        }
2143
145k
    }
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
145k
    TIFFjpeg_data_dest(sp, tif);
2153
2154
145k
    return (1);
2155
145k
}
tif_jpeg_12.c:JPEGSetupEncode
Line
Count
Source
1923
118k
{
1924
118k
    JPEGState *sp = JState(tif);
1925
118k
    TIFFDirectory *td = &tif->tif_dir;
1926
118k
    static const char module[] = "JPEGSetupEncode";
1927
1928
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
1929
    if (tif->tif_dir.td_bitspersample == 12)
1930
    {
1931
        /* We pass a pointer to a copy of otherSettings, since */
1932
        /* TIFFReInitJPEG_12() will clear sp */
1933
        JPEGOtherSettings savedOtherSettings = sp->otherSettings;
1934
        return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 1);
1935
    }
1936
#endif
1937
1938
118k
    JPEGInitializeLibJPEG(tif, FALSE);
1939
1940
118k
    assert(sp != NULL);
1941
118k
    assert(!sp->cinfo.comm.is_decompressor);
1942
1943
118k
    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
118k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
1951
118k
    {
1952
118k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
1953
118k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
1954
18.4k
        {
1955
18.4k
            if (sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
1956
18.4k
            {
1957
18.4k
                sp->cinfo.c.in_color_space = JCS_RGB;
1958
18.4k
            }
1959
0
            else
1960
0
            {
1961
0
                sp->cinfo.c.in_color_space = JCS_YCbCr;
1962
0
            }
1963
18.4k
        }
1964
99.5k
        else
1965
99.5k
        {
1966
99.5k
            if ((td->td_photometric == PHOTOMETRIC_MINISWHITE ||
1967
99.5k
                 td->td_photometric == PHOTOMETRIC_MINISBLACK) &&
1968
97.5k
                td->td_samplesperpixel == 1)
1969
90.0k
                sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
1970
9.50k
            else if (td->td_photometric == PHOTOMETRIC_RGB &&
1971
0
                     td->td_samplesperpixel == 3)
1972
0
                sp->cinfo.c.in_color_space = JCS_RGB;
1973
9.50k
            else if (td->td_photometric == PHOTOMETRIC_SEPARATED &&
1974
2.02k
                     td->td_samplesperpixel == 4)
1975
2.02k
                sp->cinfo.c.in_color_space = JCS_CMYK;
1976
7.48k
            else
1977
7.48k
                sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1978
99.5k
        }
1979
118k
    }
1980
0
    else
1981
0
    {
1982
0
        sp->cinfo.c.input_components = 1;
1983
0
        sp->cinfo.c.in_color_space = JCS_UNKNOWN;
1984
0
    }
1985
118k
    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
118k
    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
118k
    sp->cinfo.c.num_scans = 0;
2004
118k
    sp->cinfo.c.scan_info = NULL;
2005
2006
    /* Set per-file parameters */
2007
118k
    switch (sp->photometric)
2008
118k
    {
2009
18.4k
        case PHOTOMETRIC_YCBCR:
2010
18.4k
            sp->h_sampling = td->td_ycbcrsubsampling[0];
2011
18.4k
            sp->v_sampling = td->td_ycbcrsubsampling[1];
2012
18.4k
            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
18.4k
            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
18.4k
            {
2032
18.4k
                float *ref;
2033
18.4k
                if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE, &ref))
2034
9.21k
                {
2035
9.21k
                    float refbw[6];
2036
9.21k
                    long top = 1L << td->td_bitspersample;
2037
9.21k
                    refbw[0] = 0;
2038
9.21k
                    refbw[1] = (float)(top - 1L);
2039
9.21k
                    refbw[2] = (float)(top >> 1);
2040
9.21k
                    refbw[3] = refbw[1];
2041
9.21k
                    refbw[4] = refbw[2];
2042
9.21k
                    refbw[5] = refbw[1];
2043
9.21k
                    TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw);
2044
9.21k
                }
2045
18.4k
            }
2046
18.4k
            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
99.5k
        default:
2055
            /* TIFF 6.0 forbids subsampling of all other color spaces */
2056
99.5k
            sp->h_sampling = 1;
2057
99.5k
            sp->v_sampling = 1;
2058
99.5k
            break;
2059
118k
    }
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
118k
    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
118k
    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
118k
    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
118k
    else
2102
118k
    {
2103
118k
        if (td->td_rowsperstrip < td->td_imagelength &&
2104
0
            (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
118k
    }
2113
2114
    /* Create a JPEGTables field if appropriate */
2115
118k
    if (sp->otherSettings.jpegtablesmode &
2116
118k
        (JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF))
2117
118k
    {
2118
118k
        if (sp->otherSettings.jpegtables == NULL ||
2119
59.0k
            memcmp(sp->otherSettings.jpegtables, "\0\0\0\0\0\0\0\0\0", 8) == 0)
2120
59.0k
        {
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
59.0k
            if (!prepare_JPEGTables(tif))
2137
0
                return (0);
2138
            /* Mark the field present */
2139
            /* Can't use TIFFSetField since BEENWRITING is already set! */
2140
59.0k
            tif->tif_flags |= TIFF_DIRTYDIRECT;
2141
59.0k
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2142
59.0k
        }
2143
118k
    }
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
118k
    TIFFjpeg_data_dest(sp, tif);
2153
2154
118k
    return (1);
2155
118k
}
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
263k
{
2162
263k
    JPEGState *sp = JState(tif);
2163
263k
    TIFFDirectory *td = &tif->tif_dir;
2164
263k
    static const char module[] = "JPEGPreEncode";
2165
263k
    uint32_t segment_width, segment_height;
2166
263k
    int downsampled_input;
2167
2168
263k
    assert(sp != NULL);
2169
2170
263k
    if (sp->cinfo.comm.is_decompressor == 1)
2171
0
    {
2172
0
        tif->tif_setupencode(tif);
2173
0
    }
2174
2175
263k
    assert(!sp->cinfo.comm.is_decompressor);
2176
    /*
2177
     * Set encoding parameters for this strip/tile.
2178
     */
2179
263k
    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
263k
    else
2186
263k
    {
2187
263k
        segment_width = td->td_imagewidth;
2188
263k
        segment_height = td->td_imagelength - tif->tif_row;
2189
263k
        if (segment_height > td->td_rowsperstrip)
2190
0
            segment_height = td->td_rowsperstrip;
2191
263k
        sp->bytesperline = TIFFScanlineSize(tif);
2192
263k
    }
2193
263k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
2194
0
    {
2195
        /* for PC 2, scale down the strip/tile size
2196
         * to match a downsampled component
2197
         */
2198
0
        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
0
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
2205
0
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
2206
0
    }
2207
263k
    if (segment_width > (uint32_t)JPEG_MAX_DIMENSION ||
2208
263k
        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
263k
    sp->cinfo.c.image_width = segment_width;
2216
263k
    sp->cinfo.c.image_height = segment_height;
2217
263k
    downsampled_input = FALSE;
2218
263k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
2219
263k
    {
2220
263k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
2221
263k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
2222
22.7k
        {
2223
22.7k
            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
22.7k
            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
22.7k
            sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
2235
22.7k
            sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
2236
22.7k
        }
2237
240k
        else
2238
240k
        {
2239
240k
            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
240k
        }
2243
263k
    }
2244
0
    else
2245
0
    {
2246
0
        if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
2247
0
            return (0);
2248
0
        sp->cinfo.c.comp_info[0].component_id = s;
2249
        /* jpeg_set_colorspace() set sampling factors to 1 */
2250
0
        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
0
    }
2257
    /* ensure libjpeg won't write any extraneous markers */
2258
263k
    sp->cinfo.c.write_JFIF_header = FALSE;
2259
263k
    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
263k
    if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
2270
0
        return (0);
2271
263k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
2272
263k
    {
2273
263k
        suppress_quant_table(sp, 0);
2274
263k
        suppress_quant_table(sp, 1);
2275
263k
    }
2276
0
    else
2277
0
    {
2278
0
        unsuppress_quant_table(sp, 0);
2279
0
        unsuppress_quant_table(sp, 1);
2280
0
    }
2281
263k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
2282
131k
    {
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
131k
        suppress_huff_table(sp, 0);
2287
131k
        suppress_huff_table(sp, 1);
2288
131k
        sp->cinfo.c.optimize_coding = FALSE;
2289
131k
    }
2290
131k
    else
2291
131k
        sp->cinfo.c.optimize_coding = TRUE;
2292
263k
    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
263k
    else
2301
263k
    {
2302
        /* Use normal interface to libjpeg */
2303
263k
        sp->cinfo.c.raw_data_in = FALSE;
2304
263k
        tif->tif_encoderow = JPEGEncode;
2305
263k
        tif->tif_encodestrip = JPEGEncode;
2306
263k
        tif->tif_encodetile = JPEGEncode;
2307
263k
    }
2308
    /* Start JPEG compressor */
2309
263k
    if (!TIFFjpeg_start_compress(sp, FALSE))
2310
0
        return (0);
2311
    /* Allocate downsampled-data buffers if needed */
2312
263k
    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
263k
    sp->scancount = 0;
2319
263k
    sp->encode_raw_error = FALSE;
2320
2321
263k
    return (1);
2322
263k
}
tif_jpeg.c:JPEGPreEncode
Line
Count
Source
2161
145k
{
2162
145k
    JPEGState *sp = JState(tif);
2163
145k
    TIFFDirectory *td = &tif->tif_dir;
2164
145k
    static const char module[] = "JPEGPreEncode";
2165
145k
    uint32_t segment_width, segment_height;
2166
145k
    int downsampled_input;
2167
2168
145k
    assert(sp != NULL);
2169
2170
145k
    if (sp->cinfo.comm.is_decompressor == 1)
2171
0
    {
2172
0
        tif->tif_setupencode(tif);
2173
0
    }
2174
2175
145k
    assert(!sp->cinfo.comm.is_decompressor);
2176
    /*
2177
     * Set encoding parameters for this strip/tile.
2178
     */
2179
145k
    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
145k
    else
2186
145k
    {
2187
145k
        segment_width = td->td_imagewidth;
2188
145k
        segment_height = td->td_imagelength - tif->tif_row;
2189
145k
        if (segment_height > td->td_rowsperstrip)
2190
0
            segment_height = td->td_rowsperstrip;
2191
145k
        sp->bytesperline = TIFFScanlineSize(tif);
2192
145k
    }
2193
145k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
2194
0
    {
2195
        /* for PC 2, scale down the strip/tile size
2196
         * to match a downsampled component
2197
         */
2198
0
        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
0
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
2205
0
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
2206
0
    }
2207
145k
    if (segment_width > (uint32_t)JPEG_MAX_DIMENSION ||
2208
145k
        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
145k
    sp->cinfo.c.image_width = segment_width;
2216
145k
    sp->cinfo.c.image_height = segment_height;
2217
145k
    downsampled_input = FALSE;
2218
145k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
2219
145k
    {
2220
145k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
2221
145k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
2222
4.31k
        {
2223
4.31k
            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
4.31k
            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
4.31k
            sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
2235
4.31k
            sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
2236
4.31k
        }
2237
140k
        else
2238
140k
        {
2239
140k
            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
140k
        }
2243
145k
    }
2244
0
    else
2245
0
    {
2246
0
        if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
2247
0
            return (0);
2248
0
        sp->cinfo.c.comp_info[0].component_id = s;
2249
        /* jpeg_set_colorspace() set sampling factors to 1 */
2250
0
        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
0
    }
2257
    /* ensure libjpeg won't write any extraneous markers */
2258
145k
    sp->cinfo.c.write_JFIF_header = FALSE;
2259
145k
    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
145k
    if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
2270
0
        return (0);
2271
145k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
2272
145k
    {
2273
145k
        suppress_quant_table(sp, 0);
2274
145k
        suppress_quant_table(sp, 1);
2275
145k
    }
2276
0
    else
2277
0
    {
2278
0
        unsuppress_quant_table(sp, 0);
2279
0
        unsuppress_quant_table(sp, 1);
2280
0
    }
2281
145k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
2282
72.6k
    {
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
72.6k
        suppress_huff_table(sp, 0);
2287
72.6k
        suppress_huff_table(sp, 1);
2288
72.6k
        sp->cinfo.c.optimize_coding = FALSE;
2289
72.6k
    }
2290
72.6k
    else
2291
72.6k
        sp->cinfo.c.optimize_coding = TRUE;
2292
145k
    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
145k
    else
2301
145k
    {
2302
        /* Use normal interface to libjpeg */
2303
145k
        sp->cinfo.c.raw_data_in = FALSE;
2304
145k
        tif->tif_encoderow = JPEGEncode;
2305
145k
        tif->tif_encodestrip = JPEGEncode;
2306
145k
        tif->tif_encodetile = JPEGEncode;
2307
145k
    }
2308
    /* Start JPEG compressor */
2309
145k
    if (!TIFFjpeg_start_compress(sp, FALSE))
2310
0
        return (0);
2311
    /* Allocate downsampled-data buffers if needed */
2312
145k
    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
145k
    sp->scancount = 0;
2319
145k
    sp->encode_raw_error = FALSE;
2320
2321
145k
    return (1);
2322
145k
}
tif_jpeg_12.c:JPEGPreEncode
Line
Count
Source
2161
118k
{
2162
118k
    JPEGState *sp = JState(tif);
2163
118k
    TIFFDirectory *td = &tif->tif_dir;
2164
118k
    static const char module[] = "JPEGPreEncode";
2165
118k
    uint32_t segment_width, segment_height;
2166
118k
    int downsampled_input;
2167
2168
118k
    assert(sp != NULL);
2169
2170
118k
    if (sp->cinfo.comm.is_decompressor == 1)
2171
0
    {
2172
0
        tif->tif_setupencode(tif);
2173
0
    }
2174
2175
118k
    assert(!sp->cinfo.comm.is_decompressor);
2176
    /*
2177
     * Set encoding parameters for this strip/tile.
2178
     */
2179
118k
    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
118k
    else
2186
118k
    {
2187
118k
        segment_width = td->td_imagewidth;
2188
118k
        segment_height = td->td_imagelength - tif->tif_row;
2189
118k
        if (segment_height > td->td_rowsperstrip)
2190
0
            segment_height = td->td_rowsperstrip;
2191
118k
        sp->bytesperline = TIFFScanlineSize(tif);
2192
118k
    }
2193
118k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
2194
0
    {
2195
        /* for PC 2, scale down the strip/tile size
2196
         * to match a downsampled component
2197
         */
2198
0
        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
0
        segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
2205
0
        segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
2206
0
    }
2207
118k
    if (segment_width > (uint32_t)JPEG_MAX_DIMENSION ||
2208
118k
        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
118k
    sp->cinfo.c.image_width = segment_width;
2216
118k
    sp->cinfo.c.image_height = segment_height;
2217
118k
    downsampled_input = FALSE;
2218
118k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
2219
118k
    {
2220
118k
        sp->cinfo.c.input_components = td->td_samplesperpixel;
2221
118k
        if (sp->photometric == PHOTOMETRIC_YCBCR)
2222
18.4k
        {
2223
18.4k
            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
18.4k
            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
18.4k
            sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
2235
18.4k
            sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
2236
18.4k
        }
2237
99.5k
        else
2238
99.5k
        {
2239
99.5k
            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
99.5k
        }
2243
118k
    }
2244
0
    else
2245
0
    {
2246
0
        if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
2247
0
            return (0);
2248
0
        sp->cinfo.c.comp_info[0].component_id = s;
2249
        /* jpeg_set_colorspace() set sampling factors to 1 */
2250
0
        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
0
    }
2257
    /* ensure libjpeg won't write any extraneous markers */
2258
118k
    sp->cinfo.c.write_JFIF_header = FALSE;
2259
118k
    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
118k
    if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
2270
0
        return (0);
2271
118k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
2272
118k
    {
2273
118k
        suppress_quant_table(sp, 0);
2274
118k
        suppress_quant_table(sp, 1);
2275
118k
    }
2276
0
    else
2277
0
    {
2278
0
        unsuppress_quant_table(sp, 0);
2279
0
        unsuppress_quant_table(sp, 1);
2280
0
    }
2281
118k
    if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
2282
59.0k
    {
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
59.0k
        suppress_huff_table(sp, 0);
2287
59.0k
        suppress_huff_table(sp, 1);
2288
59.0k
        sp->cinfo.c.optimize_coding = FALSE;
2289
59.0k
    }
2290
59.0k
    else
2291
59.0k
        sp->cinfo.c.optimize_coding = TRUE;
2292
118k
    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
118k
    else
2301
118k
    {
2302
        /* Use normal interface to libjpeg */
2303
118k
        sp->cinfo.c.raw_data_in = FALSE;
2304
118k
        tif->tif_encoderow = JPEGEncode;
2305
118k
        tif->tif_encodestrip = JPEGEncode;
2306
118k
        tif->tif_encodetile = JPEGEncode;
2307
118k
    }
2308
    /* Start JPEG compressor */
2309
118k
    if (!TIFFjpeg_start_compress(sp, FALSE))
2310
0
        return (0);
2311
    /* Allocate downsampled-data buffers if needed */
2312
118k
    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
118k
    sp->scancount = 0;
2319
118k
    sp->encode_raw_error = FALSE;
2320
2321
118k
    return (1);
2322
118k
}
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
263k
{
2330
263k
    JPEGState *sp = JState(tif);
2331
263k
    tmsize_t nrows;
2332
263k
    TIFF_JSAMPROW bufptr[1];
2333
263k
    short *line16 = NULL;
2334
263k
    int line16_count = 0;
2335
2336
263k
    (void)s;
2337
263k
    assert(sp != NULL);
2338
    /* data is expected to be supplied in multiples of a scanline */
2339
263k
    nrows = cc / sp->bytesperline;
2340
263k
    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
263k
    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
263k
    if (sp->cinfo.c.data_precision == 12)
2348
118k
    {
2349
118k
        line16_count = (int)((sp->bytesperline * 2) / 3);
2350
118k
        line16 = (short *)_TIFFmallocExt(tif, sizeof(short) * line16_count);
2351
118k
        if (!line16)
2352
0
        {
2353
0
            TIFFErrorExtR(tif, "JPEGEncode", "Failed to allocate memory");
2354
2355
0
            return 0;
2356
0
        }
2357
118k
    }
2358
2359
3.55M
    while (nrows-- > 0)
2360
3.29M
    {
2361
2362
3.29M
        if (sp->cinfo.c.data_precision == 12)
2363
1.88M
        {
2364
2365
1.88M
            int value_pairs = line16_count / 2;
2366
1.88M
            int iPair;
2367
2368
1.88M
            bufptr[0] = (TIFF_JSAMPROW)line16;
2369
2370
24.8M
            for (iPair = 0; iPair < value_pairs; iPair++)
2371
22.9M
            {
2372
22.9M
                unsigned char *in_ptr = ((unsigned char *)buf) + iPair * 3;
2373
22.9M
                TIFF_JSAMPLE *out_ptr = (TIFF_JSAMPLE *)(line16 + iPair * 2);
2374
2375
22.9M
                out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4);
2376
22.9M
                out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2];
2377
22.9M
            }
2378
1.88M
        }
2379
1.40M
        else
2380
1.40M
        {
2381
1.40M
            bufptr[0] = (TIFF_JSAMPROW)buf;
2382
1.40M
        }
2383
3.29M
        if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
2384
0
            return (0);
2385
3.29M
        if (nrows > 0)
2386
3.03M
            tif->tif_row++;
2387
3.29M
        buf += sp->bytesperline;
2388
3.29M
    }
2389
2390
263k
    if (sp->cinfo.c.data_precision == 12)
2391
118k
    {
2392
118k
        _TIFFfreeExt(tif, line16);
2393
118k
    }
2394
2395
263k
    return (1);
2396
263k
}
tif_jpeg.c:JPEGEncode
Line
Count
Source
2329
145k
{
2330
145k
    JPEGState *sp = JState(tif);
2331
145k
    tmsize_t nrows;
2332
145k
    TIFF_JSAMPROW bufptr[1];
2333
145k
    short *line16 = NULL;
2334
145k
    int line16_count = 0;
2335
2336
145k
    (void)s;
2337
145k
    assert(sp != NULL);
2338
    /* data is expected to be supplied in multiples of a scanline */
2339
145k
    nrows = cc / sp->bytesperline;
2340
145k
    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
145k
    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
145k
    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
1.55M
    while (nrows-- > 0)
2360
1.40M
    {
2361
2362
1.40M
        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
1.40M
        else
2380
1.40M
        {
2381
1.40M
            bufptr[0] = (TIFF_JSAMPROW)buf;
2382
1.40M
        }
2383
1.40M
        if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
2384
0
            return (0);
2385
1.40M
        if (nrows > 0)
2386
1.26M
            tif->tif_row++;
2387
1.40M
        buf += sp->bytesperline;
2388
1.40M
    }
2389
2390
145k
    if (sp->cinfo.c.data_precision == 12)
2391
0
    {
2392
0
        _TIFFfreeExt(tif, line16);
2393
0
    }
2394
2395
145k
    return (1);
2396
145k
}
tif_jpeg_12.c:JPEGEncode
Line
Count
Source
2329
118k
{
2330
118k
    JPEGState *sp = JState(tif);
2331
118k
    tmsize_t nrows;
2332
118k
    TIFF_JSAMPROW bufptr[1];
2333
118k
    short *line16 = NULL;
2334
118k
    int line16_count = 0;
2335
2336
118k
    (void)s;
2337
118k
    assert(sp != NULL);
2338
    /* data is expected to be supplied in multiples of a scanline */
2339
118k
    nrows = cc / sp->bytesperline;
2340
118k
    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
118k
    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
118k
    if (sp->cinfo.c.data_precision == 12)
2348
118k
    {
2349
118k
        line16_count = (int)((sp->bytesperline * 2) / 3);
2350
118k
        line16 = (short *)_TIFFmallocExt(tif, sizeof(short) * line16_count);
2351
118k
        if (!line16)
2352
0
        {
2353
0
            TIFFErrorExtR(tif, "JPEGEncode", "Failed to allocate memory");
2354
2355
0
            return 0;
2356
0
        }
2357
118k
    }
2358
2359
2.00M
    while (nrows-- > 0)
2360
1.88M
    {
2361
2362
1.88M
        if (sp->cinfo.c.data_precision == 12)
2363
1.88M
        {
2364
2365
1.88M
            int value_pairs = line16_count / 2;
2366
1.88M
            int iPair;
2367
2368
1.88M
            bufptr[0] = (TIFF_JSAMPROW)line16;
2369
2370
24.8M
            for (iPair = 0; iPair < value_pairs; iPair++)
2371
22.9M
            {
2372
22.9M
                unsigned char *in_ptr = ((unsigned char *)buf) + iPair * 3;
2373
22.9M
                TIFF_JSAMPLE *out_ptr = (TIFF_JSAMPLE *)(line16 + iPair * 2);
2374
2375
22.9M
                out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4);
2376
22.9M
                out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2];
2377
22.9M
            }
2378
1.88M
        }
2379
0
        else
2380
0
        {
2381
0
            bufptr[0] = (TIFF_JSAMPROW)buf;
2382
0
        }
2383
1.88M
        if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
2384
0
            return (0);
2385
1.88M
        if (nrows > 0)
2386
1.77M
            tif->tif_row++;
2387
1.88M
        buf += sp->bytesperline;
2388
1.88M
    }
2389
2390
118k
    if (sp->cinfo.c.data_precision == 12)
2391
118k
    {
2392
118k
        _TIFFfreeExt(tif, line16);
2393
118k
    }
2394
2395
118k
    return (1);
2396
118k
}
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
263k
{
2512
263k
    JPEGState *sp = JState(tif);
2513
2514
263k
    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
263k
    return (TIFFjpeg_finish_compress(JState(tif)));
2541
263k
}
tif_jpeg.c:JPEGPostEncode
Line
Count
Source
2511
145k
{
2512
145k
    JPEGState *sp = JState(tif);
2513
2514
145k
    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
145k
    return (TIFFjpeg_finish_compress(JState(tif)));
2541
145k
}
tif_jpeg_12.c:JPEGPostEncode
Line
Count
Source
2511
118k
{
2512
118k
    JPEGState *sp = JState(tif);
2513
2514
118k
    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
118k
    return (TIFFjpeg_finish_compress(JState(tif)));
2541
118k
}
2542
2543
static void JPEGCleanup(TIFF *tif)
2544
809k
{
2545
809k
    JPEGState *sp = JState(tif);
2546
2547
809k
    assert(sp != 0);
2548
2549
809k
    tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent;
2550
809k
    tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent;
2551
809k
    tif->tif_tagmethods.printdir = sp->otherSettings.printdir;
2552
809k
    if (sp->cinfo_initialized)
2553
387k
        TIFFjpeg_destroy(sp);         /* release libjpeg resources */
2554
809k
    if (sp->otherSettings.jpegtables) /* tag value */
2555
653k
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
2556
809k
    _TIFFfreeExt(tif, tif->tif_data); /* release local state */
2557
809k
    tif->tif_data = NULL;
2558
2559
809k
    _TIFFSetDefaultCompressionState(tif);
2560
809k
}
tif_jpeg.c:JPEGCleanup
Line
Count
Source
2544
687k
{
2545
687k
    JPEGState *sp = JState(tif);
2546
2547
687k
    assert(sp != 0);
2548
2549
687k
    tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent;
2550
687k
    tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent;
2551
687k
    tif->tif_tagmethods.printdir = sp->otherSettings.printdir;
2552
687k
    if (sp->cinfo_initialized)
2553
265k
        TIFFjpeg_destroy(sp);         /* release libjpeg resources */
2554
687k
    if (sp->otherSettings.jpegtables) /* tag value */
2555
534k
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
2556
687k
    _TIFFfreeExt(tif, tif->tif_data); /* release local state */
2557
687k
    tif->tif_data = NULL;
2558
2559
687k
    _TIFFSetDefaultCompressionState(tif);
2560
687k
}
tif_jpeg_12.c:JPEGCleanup
Line
Count
Source
2544
122k
{
2545
122k
    JPEGState *sp = JState(tif);
2546
2547
122k
    assert(sp != 0);
2548
2549
122k
    tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent;
2550
122k
    tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent;
2551
122k
    tif->tif_tagmethods.printdir = sp->otherSettings.printdir;
2552
122k
    if (sp->cinfo_initialized)
2553
122k
        TIFFjpeg_destroy(sp);         /* release libjpeg resources */
2554
122k
    if (sp->otherSettings.jpegtables) /* tag value */
2555
118k
        _TIFFfreeExt(tif, sp->otherSettings.jpegtables);
2556
122k
    _TIFFfreeExt(tif, tif->tif_data); /* release local state */
2557
122k
    tif->tif_data = NULL;
2558
2559
122k
    _TIFFSetDefaultCompressionState(tif);
2560
122k
}
2561
2562
static void JPEGResetUpsampled(TIFF *tif)
2563
449k
{
2564
449k
    JPEGState *sp = JState(tif);
2565
449k
    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
449k
    tif->tif_flags &= ~TIFF_UPSAMPLED;
2573
449k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
2574
447k
    {
2575
447k
        if (td->td_photometric == PHOTOMETRIC_YCBCR &&
2576
86.5k
            sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
2577
37.0k
        {
2578
37.0k
            tif->tif_flags |= TIFF_UPSAMPLED;
2579
37.0k
        }
2580
410k
        else
2581
410k
        {
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
410k
        }
2588
447k
    }
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
449k
    if (tif->tif_tilesize > 0)
2595
11.9k
        tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
2596
449k
    if (tif->tif_scanlinesize > 0)
2597
442k
        tif->tif_scanlinesize = TIFFScanlineSize(tif);
2598
449k
}
tif_jpeg.c:JPEGResetUpsampled
Line
Count
Source
2563
449k
{
2564
449k
    JPEGState *sp = JState(tif);
2565
449k
    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
449k
    tif->tif_flags &= ~TIFF_UPSAMPLED;
2573
449k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
2574
447k
    {
2575
447k
        if (td->td_photometric == PHOTOMETRIC_YCBCR &&
2576
86.5k
            sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
2577
37.0k
        {
2578
37.0k
            tif->tif_flags |= TIFF_UPSAMPLED;
2579
37.0k
        }
2580
410k
        else
2581
410k
        {
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
410k
        }
2588
447k
    }
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
449k
    if (tif->tif_tilesize > 0)
2595
11.9k
        tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
2596
449k
    if (tif->tif_scanlinesize > 0)
2597
442k
        tif->tif_scanlinesize = TIFFScanlineSize(tif);
2598
449k
}
Unexecuted instantiation: tif_jpeg_12.c:JPEGResetUpsampled
2599
2600
static int JPEGVSetField(TIFF *tif, uint32_t tag, va_list ap)
2601
4.90M
{
2602
4.90M
    JPEGState *sp = JState(tif);
2603
4.90M
    const TIFFField *fip;
2604
4.90M
    uint32_t v32;
2605
2606
4.90M
    assert(sp != NULL);
2607
2608
4.90M
    switch (tag)
2609
4.90M
    {
2610
270k
        case TIFFTAG_JPEGTABLES:
2611
270k
            v32 = (uint32_t)va_arg(ap, uint32_t);
2612
270k
            if (v32 == 0)
2613
231
            {
2614
                /* XXX */
2615
231
                return (0);
2616
231
            }
2617
270k
            _TIFFsetByteArrayExt(tif, &sp->otherSettings.jpegtables,
2618
270k
                                 va_arg(ap, void *), v32);
2619
270k
            sp->otherSettings.jpegtables_length = v32;
2620
270k
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2621
270k
            break;
2622
574k
        case TIFFTAG_JPEGQUALITY:
2623
574k
            sp->otherSettings.jpegquality = (int)va_arg(ap, int);
2624
574k
            return (1); /* pseudo tag */
2625
37.7k
        case TIFFTAG_JPEGCOLORMODE:
2626
37.7k
            sp->otherSettings.jpegcolormode = (int)va_arg(ap, int);
2627
37.7k
            JPEGResetUpsampled(tif);
2628
37.7k
            return (1); /* pseudo tag */
2629
411k
        case TIFFTAG_PHOTOMETRIC:
2630
411k
        {
2631
411k
            int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap);
2632
411k
            JPEGResetUpsampled(tif);
2633
411k
            return ret_value;
2634
270k
        }
2635
574k
        case TIFFTAG_JPEGTABLESMODE:
2636
574k
            sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int);
2637
574k
            return (1); /* pseudo tag */
2638
26.8k
        case TIFFTAG_YCBCRSUBSAMPLING:
2639
            /* mark the fact that we have a real ycbcrsubsampling! */
2640
26.8k
            sp->otherSettings.ycbcrsampling_fetched = 1;
2641
            /* should we be recomputing upsampling info here? */
2642
26.8k
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2643
3.00M
        default:
2644
3.00M
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2645
4.90M
    }
2646
2647
270k
    if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
2648
270k
    {
2649
270k
        TIFFSetFieldBit(tif, fip->field_bit);
2650
270k
    }
2651
0
    else
2652
0
    {
2653
0
        return (0);
2654
0
    }
2655
2656
270k
    tif->tif_flags |= TIFF_DIRTYDIRECT;
2657
270k
    return (1);
2658
270k
}
tif_jpeg.c:JPEGVSetField
Line
Count
Source
2601
4.89M
{
2602
4.89M
    JPEGState *sp = JState(tif);
2603
4.89M
    const TIFFField *fip;
2604
4.89M
    uint32_t v32;
2605
2606
4.89M
    assert(sp != NULL);
2607
2608
4.89M
    switch (tag)
2609
4.89M
    {
2610
270k
        case TIFFTAG_JPEGTABLES:
2611
270k
            v32 = (uint32_t)va_arg(ap, uint32_t);
2612
270k
            if (v32 == 0)
2613
231
            {
2614
                /* XXX */
2615
231
                return (0);
2616
231
            }
2617
270k
            _TIFFsetByteArrayExt(tif, &sp->otherSettings.jpegtables,
2618
270k
                                 va_arg(ap, void *), v32);
2619
270k
            sp->otherSettings.jpegtables_length = v32;
2620
270k
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2621
270k
            break;
2622
574k
        case TIFFTAG_JPEGQUALITY:
2623
574k
            sp->otherSettings.jpegquality = (int)va_arg(ap, int);
2624
574k
            return (1); /* pseudo tag */
2625
37.7k
        case TIFFTAG_JPEGCOLORMODE:
2626
37.7k
            sp->otherSettings.jpegcolormode = (int)va_arg(ap, int);
2627
37.7k
            JPEGResetUpsampled(tif);
2628
37.7k
            return (1); /* pseudo tag */
2629
411k
        case TIFFTAG_PHOTOMETRIC:
2630
411k
        {
2631
411k
            int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap);
2632
411k
            JPEGResetUpsampled(tif);
2633
411k
            return ret_value;
2634
270k
        }
2635
574k
        case TIFFTAG_JPEGTABLESMODE:
2636
574k
            sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int);
2637
574k
            return (1); /* pseudo tag */
2638
26.8k
        case TIFFTAG_YCBCRSUBSAMPLING:
2639
            /* mark the fact that we have a real ycbcrsubsampling! */
2640
26.8k
            sp->otherSettings.ycbcrsampling_fetched = 1;
2641
            /* should we be recomputing upsampling info here? */
2642
26.8k
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2643
2.99M
        default:
2644
2.99M
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2645
4.89M
    }
2646
2647
270k
    if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
2648
270k
    {
2649
270k
        TIFFSetFieldBit(tif, fip->field_bit);
2650
270k
    }
2651
0
    else
2652
0
    {
2653
0
        return (0);
2654
0
    }
2655
2656
270k
    tif->tif_flags |= TIFF_DIRTYDIRECT;
2657
270k
    return (1);
2658
270k
}
tif_jpeg_12.c:JPEGVSetField
Line
Count
Source
2601
9.21k
{
2602
9.21k
    JPEGState *sp = JState(tif);
2603
9.21k
    const TIFFField *fip;
2604
9.21k
    uint32_t v32;
2605
2606
9.21k
    assert(sp != NULL);
2607
2608
9.21k
    switch (tag)
2609
9.21k
    {
2610
0
        case TIFFTAG_JPEGTABLES:
2611
0
            v32 = (uint32_t)va_arg(ap, uint32_t);
2612
0
            if (v32 == 0)
2613
0
            {
2614
                /* XXX */
2615
0
                return (0);
2616
0
            }
2617
0
            _TIFFsetByteArrayExt(tif, &sp->otherSettings.jpegtables,
2618
0
                                 va_arg(ap, void *), v32);
2619
0
            sp->otherSettings.jpegtables_length = v32;
2620
0
            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
2621
0
            break;
2622
0
        case TIFFTAG_JPEGQUALITY:
2623
0
            sp->otherSettings.jpegquality = (int)va_arg(ap, int);
2624
0
            return (1); /* pseudo tag */
2625
0
        case TIFFTAG_JPEGCOLORMODE:
2626
0
            sp->otherSettings.jpegcolormode = (int)va_arg(ap, int);
2627
0
            JPEGResetUpsampled(tif);
2628
0
            return (1); /* pseudo tag */
2629
0
        case TIFFTAG_PHOTOMETRIC:
2630
0
        {
2631
0
            int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap);
2632
0
            JPEGResetUpsampled(tif);
2633
0
            return ret_value;
2634
0
        }
2635
0
        case TIFFTAG_JPEGTABLESMODE:
2636
0
            sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int);
2637
0
            return (1); /* pseudo tag */
2638
0
        case TIFFTAG_YCBCRSUBSAMPLING:
2639
            /* mark the fact that we have a real ycbcrsubsampling! */
2640
0
            sp->otherSettings.ycbcrsampling_fetched = 1;
2641
            /* should we be recomputing upsampling info here? */
2642
0
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2643
9.21k
        default:
2644
9.21k
            return (*sp->otherSettings.vsetparent)(tif, tag, ap);
2645
9.21k
    }
2646
2647
0
    if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
2648
0
    {
2649
0
        TIFFSetFieldBit(tif, fip->field_bit);
2650
0
    }
2651
0
    else
2652
0
    {
2653
0
        return (0);
2654
0
    }
2655
2656
0
    tif->tif_flags |= TIFF_DIRTYDIRECT;
2657
0
    return (1);
2658
0
}
2659
2660
static int JPEGVGetField(TIFF *tif, uint32_t tag, va_list ap)
2661
2.40M
{
2662
2.40M
    JPEGState *sp = JState(tif);
2663
2664
2.40M
    assert(sp != NULL);
2665
2666
2.40M
    switch (tag)
2667
2.40M
    {
2668
793k
        case TIFFTAG_JPEGTABLES:
2669
793k
            *va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length;
2670
793k
            *va_arg(ap, const void **) = sp->otherSettings.jpegtables;
2671
793k
            break;
2672
0
        case TIFFTAG_JPEGQUALITY:
2673
0
            *va_arg(ap, int *) = sp->otherSettings.jpegquality;
2674
0
            break;
2675
12.1k
        case TIFFTAG_JPEGCOLORMODE:
2676
12.1k
            *va_arg(ap, int *) = sp->otherSettings.jpegcolormode;
2677
12.1k
            break;
2678
191k
        case TIFFTAG_JPEGTABLESMODE:
2679
191k
            *va_arg(ap, int *) = sp->otherSettings.jpegtablesmode;
2680
191k
            break;
2681
1.40M
        default:
2682
1.40M
            return (*sp->otherSettings.vgetparent)(tif, tag, ap);
2683
2.40M
    }
2684
996k
    return (1);
2685
2.40M
}
tif_jpeg.c:JPEGVGetField
Line
Count
Source
2661
2.11M
{
2662
2.11M
    JPEGState *sp = JState(tif);
2663
2664
2.11M
    assert(sp != NULL);
2665
2666
2.11M
    switch (tag)
2667
2.11M
    {
2668
557k
        case TIFFTAG_JPEGTABLES:
2669
557k
            *va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length;
2670
557k
            *va_arg(ap, const void **) = sp->otherSettings.jpegtables;
2671
557k
            break;
2672
0
        case TIFFTAG_JPEGQUALITY:
2673
0
            *va_arg(ap, int *) = sp->otherSettings.jpegquality;
2674
0
            break;
2675
12.1k
        case TIFFTAG_JPEGCOLORMODE:
2676
12.1k
            *va_arg(ap, int *) = sp->otherSettings.jpegcolormode;
2677
12.1k
            break;
2678
191k
        case TIFFTAG_JPEGTABLESMODE:
2679
191k
            *va_arg(ap, int *) = sp->otherSettings.jpegtablesmode;
2680
191k
            break;
2681
1.35M
        default:
2682
1.35M
            return (*sp->otherSettings.vgetparent)(tif, tag, ap);
2683
2.11M
    }
2684
760k
    return (1);
2685
2.11M
}
tif_jpeg_12.c:JPEGVGetField
Line
Count
Source
2661
283k
{
2662
283k
    JPEGState *sp = JState(tif);
2663
2664
283k
    assert(sp != NULL);
2665
2666
283k
    switch (tag)
2667
283k
    {
2668
236k
        case TIFFTAG_JPEGTABLES:
2669
236k
            *va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length;
2670
236k
            *va_arg(ap, const void **) = sp->otherSettings.jpegtables;
2671
236k
            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
47.9k
        default:
2682
47.9k
            return (*sp->otherSettings.vgetparent)(tif, tag, ap);
2683
283k
    }
2684
236k
    return (1);
2685
283k
}
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
0
{
2706
0
    JPEGState *sp = JState(tif);
2707
0
    TIFFDirectory *td = &tif->tif_dir;
2708
2709
0
    s = (*sp->otherSettings.defsparent)(tif, s);
2710
0
    if (s < td->td_imagelength)
2711
0
        s = TIFFroundup_32(s, td->td_ycbcrsubsampling[1] * DCTSIZE);
2712
0
    return (s);
2713
0
}
Unexecuted instantiation: tif_jpeg.c:JPEGDefaultStripSize
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
387k
{
2749
387k
    JPEGState *sp = JState(tif);
2750
2751
387k
    if (sp->cinfo_initialized)
2752
178
    {
2753
178
        if (!decompress && sp->cinfo.comm.is_decompressor)
2754
0
            TIFFjpeg_destroy(sp);
2755
178
        else if (decompress && !sp->cinfo.comm.is_decompressor)
2756
0
            TIFFjpeg_destroy(sp);
2757
178
        else
2758
178
            return 1;
2759
2760
0
        sp->cinfo_initialized = 0;
2761
0
    }
2762
2763
    /*
2764
     * Initialize libjpeg.
2765
     */
2766
387k
    if (decompress)
2767
4.73k
    {
2768
4.73k
        if (!TIFFjpeg_create_decompress(sp))
2769
0
            return (0);
2770
4.73k
    }
2771
382k
    else
2772
382k
    {
2773
382k
        if (!TIFFjpeg_create_compress(sp))
2774
0
            return (0);
2775
382k
#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE
2776
382k
#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024)
2777
382k
#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
382k
        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
382k
    }
2799
2800
387k
    sp->cinfo_initialized = TRUE;
2801
2802
387k
    return 1;
2803
387k
}
tif_jpeg.c:JPEGInitializeLibJPEG
Line
Count
Source
2748
265k
{
2749
265k
    JPEGState *sp = JState(tif);
2750
2751
265k
    if (sp->cinfo_initialized)
2752
78
    {
2753
78
        if (!decompress && sp->cinfo.comm.is_decompressor)
2754
0
            TIFFjpeg_destroy(sp);
2755
78
        else if (decompress && !sp->cinfo.comm.is_decompressor)
2756
0
            TIFFjpeg_destroy(sp);
2757
78
        else
2758
78
            return 1;
2759
2760
0
        sp->cinfo_initialized = 0;
2761
0
    }
2762
2763
    /*
2764
     * Initialize libjpeg.
2765
     */
2766
265k
    if (decompress)
2767
586
    {
2768
586
        if (!TIFFjpeg_create_decompress(sp))
2769
0
            return (0);
2770
586
    }
2771
264k
    else
2772
264k
    {
2773
264k
        if (!TIFFjpeg_create_compress(sp))
2774
0
            return (0);
2775
264k
#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE
2776
264k
#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024)
2777
264k
#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
264k
        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
264k
    }
2799
2800
265k
    sp->cinfo_initialized = TRUE;
2801
2802
265k
    return 1;
2803
265k
}
tif_jpeg_12.c:JPEGInitializeLibJPEG
Line
Count
Source
2748
122k
{
2749
122k
    JPEGState *sp = JState(tif);
2750
2751
122k
    if (sp->cinfo_initialized)
2752
100
    {
2753
100
        if (!decompress && sp->cinfo.comm.is_decompressor)
2754
0
            TIFFjpeg_destroy(sp);
2755
100
        else if (decompress && !sp->cinfo.comm.is_decompressor)
2756
0
            TIFFjpeg_destroy(sp);
2757
100
        else
2758
100
            return 1;
2759
2760
0
        sp->cinfo_initialized = 0;
2761
0
    }
2762
2763
    /*
2764
     * Initialize libjpeg.
2765
     */
2766
122k
    if (decompress)
2767
4.15k
    {
2768
4.15k
        if (!TIFFjpeg_create_decompress(sp))
2769
0
            return (0);
2770
4.15k
    }
2771
118k
    else
2772
118k
    {
2773
118k
        if (!TIFFjpeg_create_compress(sp))
2774
0
            return (0);
2775
118k
#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE
2776
118k
#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024)
2777
118k
#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
118k
        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
118k
    }
2799
2800
122k
    sp->cinfo_initialized = TRUE;
2801
2802
122k
    return 1;
2803
122k
}
2804
2805
/* Common to tif_jpeg.c and tif_jpeg_12.c */
2806
static void TIFFInitJPEGCommon(TIFF *tif)
2807
931k
{
2808
931k
    JPEGState *sp;
2809
2810
931k
    sp = JState(tif);
2811
931k
    sp->tif = tif; /* back link */
2812
2813
    /* Default values for codec-specific fields */
2814
931k
    sp->otherSettings.jpegtables = NULL;
2815
931k
    sp->otherSettings.jpegtables_length = 0;
2816
931k
    sp->otherSettings.jpegquality = 75; /* Default IJG quality */
2817
931k
    sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW;
2818
931k
    sp->otherSettings.jpegtablesmode =
2819
931k
        JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF;
2820
931k
    sp->otherSettings.ycbcrsampling_fetched = 0;
2821
2822
931k
    tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
2823
931k
    tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
2824
931k
    tif->tif_tagmethods.printdir = JPEGPrintDir;   /* hook for codec tags */
2825
2826
    /*
2827
     * Install codec methods.
2828
     */
2829
931k
    tif->tif_fixuptags = JPEGFixupTags;
2830
931k
    tif->tif_setupdecode = JPEGSetupDecode;
2831
931k
    tif->tif_predecode = JPEGPreDecode;
2832
931k
    tif->tif_decoderow = JPEGDecode;
2833
931k
    tif->tif_decodestrip = JPEGDecode;
2834
931k
    tif->tif_decodetile = JPEGDecode;
2835
931k
    tif->tif_setupencode = JPEGSetupEncode;
2836
931k
    tif->tif_preencode = JPEGPreEncode;
2837
931k
    tif->tif_postencode = JPEGPostEncode;
2838
931k
    tif->tif_encoderow = JPEGEncode;
2839
931k
    tif->tif_encodestrip = JPEGEncode;
2840
931k
    tif->tif_encodetile = JPEGEncode;
2841
931k
    tif->tif_cleanup = JPEGCleanup;
2842
2843
931k
    tif->tif_defstripsize = JPEGDefaultStripSize;
2844
931k
    tif->tif_deftilesize = JPEGDefaultTileSize;
2845
931k
    tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */
2846
931k
    sp->cinfo_initialized = FALSE;
2847
931k
}
tif_jpeg.c:TIFFInitJPEGCommon
Line
Count
Source
2807
809k
{
2808
809k
    JPEGState *sp;
2809
2810
809k
    sp = JState(tif);
2811
809k
    sp->tif = tif; /* back link */
2812
2813
    /* Default values for codec-specific fields */
2814
809k
    sp->otherSettings.jpegtables = NULL;
2815
809k
    sp->otherSettings.jpegtables_length = 0;
2816
809k
    sp->otherSettings.jpegquality = 75; /* Default IJG quality */
2817
809k
    sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW;
2818
809k
    sp->otherSettings.jpegtablesmode =
2819
809k
        JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF;
2820
809k
    sp->otherSettings.ycbcrsampling_fetched = 0;
2821
2822
809k
    tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
2823
809k
    tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
2824
809k
    tif->tif_tagmethods.printdir = JPEGPrintDir;   /* hook for codec tags */
2825
2826
    /*
2827
     * Install codec methods.
2828
     */
2829
809k
    tif->tif_fixuptags = JPEGFixupTags;
2830
809k
    tif->tif_setupdecode = JPEGSetupDecode;
2831
809k
    tif->tif_predecode = JPEGPreDecode;
2832
809k
    tif->tif_decoderow = JPEGDecode;
2833
809k
    tif->tif_decodestrip = JPEGDecode;
2834
809k
    tif->tif_decodetile = JPEGDecode;
2835
809k
    tif->tif_setupencode = JPEGSetupEncode;
2836
809k
    tif->tif_preencode = JPEGPreEncode;
2837
809k
    tif->tif_postencode = JPEGPostEncode;
2838
809k
    tif->tif_encoderow = JPEGEncode;
2839
809k
    tif->tif_encodestrip = JPEGEncode;
2840
809k
    tif->tif_encodetile = JPEGEncode;
2841
809k
    tif->tif_cleanup = JPEGCleanup;
2842
2843
809k
    tif->tif_defstripsize = JPEGDefaultStripSize;
2844
809k
    tif->tif_deftilesize = JPEGDefaultTileSize;
2845
809k
    tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */
2846
809k
    sp->cinfo_initialized = FALSE;
2847
809k
}
tif_jpeg_12.c:TIFFInitJPEGCommon
Line
Count
Source
2807
122k
{
2808
122k
    JPEGState *sp;
2809
2810
122k
    sp = JState(tif);
2811
122k
    sp->tif = tif; /* back link */
2812
2813
    /* Default values for codec-specific fields */
2814
122k
    sp->otherSettings.jpegtables = NULL;
2815
122k
    sp->otherSettings.jpegtables_length = 0;
2816
122k
    sp->otherSettings.jpegquality = 75; /* Default IJG quality */
2817
122k
    sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW;
2818
122k
    sp->otherSettings.jpegtablesmode =
2819
122k
        JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF;
2820
122k
    sp->otherSettings.ycbcrsampling_fetched = 0;
2821
2822
122k
    tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
2823
122k
    tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
2824
122k
    tif->tif_tagmethods.printdir = JPEGPrintDir;   /* hook for codec tags */
2825
2826
    /*
2827
     * Install codec methods.
2828
     */
2829
122k
    tif->tif_fixuptags = JPEGFixupTags;
2830
122k
    tif->tif_setupdecode = JPEGSetupDecode;
2831
122k
    tif->tif_predecode = JPEGPreDecode;
2832
122k
    tif->tif_decoderow = JPEGDecode;
2833
122k
    tif->tif_decodestrip = JPEGDecode;
2834
122k
    tif->tif_decodetile = JPEGDecode;
2835
122k
    tif->tif_setupencode = JPEGSetupEncode;
2836
122k
    tif->tif_preencode = JPEGPreEncode;
2837
122k
    tif->tif_postencode = JPEGPostEncode;
2838
122k
    tif->tif_encoderow = JPEGEncode;
2839
122k
    tif->tif_encodestrip = JPEGEncode;
2840
122k
    tif->tif_encodetile = JPEGEncode;
2841
122k
    tif->tif_cleanup = JPEGCleanup;
2842
2843
122k
    tif->tif_defstripsize = JPEGDefaultStripSize;
2844
122k
    tif->tif_deftilesize = JPEGDefaultTileSize;
2845
122k
    tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */
2846
122k
    sp->cinfo_initialized = FALSE;
2847
122k
}
2848
2849
int TIFFInitJPEG(TIFF *tif, int scheme)
2850
809k
{
2851
809k
    JPEGState *sp;
2852
2853
809k
    (void)scheme;
2854
809k
    assert(scheme == COMPRESSION_JPEG);
2855
2856
    /*
2857
     * Merge codec-specific tag information.
2858
     */
2859
809k
    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
809k
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(JPEGState));
2870
2871
809k
    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
809k
    _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState));
2877
2878
809k
    sp = JState(tif);
2879
    /*
2880
     * Override parent get/set field methods.
2881
     */
2882
809k
    sp->otherSettings.vgetparent = tif->tif_tagmethods.vgetfield;
2883
809k
    sp->otherSettings.vsetparent = tif->tif_tagmethods.vsetfield;
2884
809k
    sp->otherSettings.printdir = tif->tif_tagmethods.printdir;
2885
2886
809k
    sp->otherSettings.defsparent = tif->tif_defstripsize;
2887
809k
    sp->otherSettings.deftparent = tif->tif_deftilesize;
2888
2889
809k
    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
809k
    if (tif->tif_diroff == 0)
2898
382k
    {
2899
765k
#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
382k
        sp->otherSettings.jpegtables_length = SIZE_OF_JPEGTABLES;
2909
382k
        sp->otherSettings.jpegtables =
2910
382k
            (void *)_TIFFmallocExt(tif, sp->otherSettings.jpegtables_length);
2911
382k
        if (sp->otherSettings.jpegtables)
2912
382k
        {
2913
382k
            _TIFFmemset(sp->otherSettings.jpegtables, 0, SIZE_OF_JPEGTABLES);
2914
382k
        }
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
382k
#undef SIZE_OF_JPEGTABLES
2922
382k
    }
2923
809k
    return 1;
2924
809k
}
gdal_TIFFInitJPEG
Line
Count
Source
2850
809k
{
2851
809k
    JPEGState *sp;
2852
2853
809k
    (void)scheme;
2854
809k
    assert(scheme == COMPRESSION_JPEG);
2855
2856
    /*
2857
     * Merge codec-specific tag information.
2858
     */
2859
809k
    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
809k
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(JPEGState));
2870
2871
809k
    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
809k
    _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState));
2877
2878
809k
    sp = JState(tif);
2879
    /*
2880
     * Override parent get/set field methods.
2881
     */
2882
809k
    sp->otherSettings.vgetparent = tif->tif_tagmethods.vgetfield;
2883
809k
    sp->otherSettings.vsetparent = tif->tif_tagmethods.vsetfield;
2884
809k
    sp->otherSettings.printdir = tif->tif_tagmethods.printdir;
2885
2886
809k
    sp->otherSettings.defsparent = tif->tif_defstripsize;
2887
809k
    sp->otherSettings.deftparent = tif->tif_deftilesize;
2888
2889
809k
    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
809k
    if (tif->tif_diroff == 0)
2898
382k
    {
2899
382k
#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
382k
        sp->otherSettings.jpegtables_length = SIZE_OF_JPEGTABLES;
2909
382k
        sp->otherSettings.jpegtables =
2910
382k
            (void *)_TIFFmallocExt(tif, sp->otherSettings.jpegtables_length);
2911
382k
        if (sp->otherSettings.jpegtables)
2912
382k
        {
2913
382k
            _TIFFmemset(sp->otherSettings.jpegtables, 0, SIZE_OF_JPEGTABLES);
2914
382k
        }
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
382k
#undef SIZE_OF_JPEGTABLES
2922
382k
    }
2923
809k
    return 1;
2924
809k
}
Unexecuted instantiation: gdal_TIFFInitJPEG_12
2925
#endif /* JPEG_SUPPORT */