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