#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include "tiffiop.h"
#include <stdlib.h>
#ifdef JPEG_SUPPORT
#include <setjmp.h>
typedef struct
{
TIFFVGetMethod vgetparent;
TIFFVSetMethod vsetparent;
TIFFPrintMethod printdir;
TIFFStripMethod defsparent;
TIFFTileMethod deftparent;
void *jpegtables;
uint32_t jpegtables_length;
int jpegquality;
int jpegcolormode;
int jpegtablesmode;
int ycbcrsampling_fetched;
int max_allowed_scan_number;
int has_warned_about_progressive_mode;
} JPEGOtherSettings;
int TIFFFillStrip(TIFF *tif, uint32_t strip);
int TIFFFillTile(TIFF *tif, uint32_t tile);
int TIFFReInitJPEG_12(TIFF *tif, const JPEGOtherSettings *otherSettings,
int scheme, int is_encode);
int TIFFJPEGIsFullStripRequired_12(TIFF *tif);
#include "jerror.h"
#include "jpeglib.h"
#if defined(EXPECTED_JPEG_LIB_VERSION) && !defined(LIBJPEG_12_PATH)
#if EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION
#error EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION
#endif
#endif
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12)
#define JPEG_DUAL_MODE_8_12
#undef BITS_IN_JSAMPLE
#if defined(FROM_TIF_JPEG_12)
#define BITS_IN_JSAMPLE 12
#define TIFF_JSAMPLE J12SAMPLE
#define TIFF_JSAMPARRAY J12SAMPARRAY
#define TIFF_JSAMPIMAGE J12SAMPIMAGE
#define TIFF_JSAMPROW J12SAMPROW
#else
#define BITS_IN_JSAMPLE 8
#define TIFF_JSAMPLE JSAMPLE
#define TIFF_JSAMPARRAY JSAMPARRAY
#define TIFF_JSAMPIMAGE JSAMPIMAGE
#define TIFF_JSAMPROW JSAMPROW
#endif
#else
#define TIFF_JSAMPLE JSAMPLE
#define TIFF_JSAMPARRAY JSAMPARRAY
#define TIFF_JSAMPIMAGE JSAMPIMAGE
#define TIFF_JSAMPROW JSAMPROW
#endif
#if defined(JPEG_LIB_MK1)
#define JPEG_LIB_MK1_OR_12BIT 1
#elif BITS_IN_JSAMPLE == 12
#define JPEG_LIB_MK1_OR_12BIT 1
#endif
#if defined(D_MAX_DATA_UNITS_IN_MCU)
#define width_in_blocks width_in_data_units
#endif
#define SETJMP(jbuf) setjmp(jbuf)
#define LONGJMP(jbuf, code) longjmp(jbuf, code)
#define JMP_BUF jmp_buf
#ifndef TIFF_jpeg_destination_mgr_defined
#define TIFF_jpeg_destination_mgr_defined
typedef struct jpeg_destination_mgr jpeg_destination_mgr;
#endif
#ifndef TIFF_jpeg_source_mgr_defined
#define TIFF_jpeg_source_mgr_defined
typedef struct jpeg_source_mgr jpeg_source_mgr;
#endif
#ifndef TIFF_jpeg_error_mgr_defined
#define TIFF_jpeg_error_mgr_defined
typedef struct jpeg_error_mgr jpeg_error_mgr;
#endif
typedef struct
{
union
{
struct jpeg_compress_struct c;
struct jpeg_decompress_struct d;
struct jpeg_common_struct comm;
} cinfo;
int cinfo_initialized;
jpeg_error_mgr err;
JMP_BUF exit_jmpbuf;
struct jpeg_progress_mgr progress;
jpeg_destination_mgr dest;
jpeg_source_mgr src;
TIFF *tif;
uint16_t photometric;
uint16_t h_sampling;
uint16_t v_sampling;
tmsize_t bytesperline;
TIFF_JSAMPARRAY ds_buffer[MAX_COMPONENTS];
int scancount;
int samplesperclump;
JPEGOtherSettings otherSettings;
} JPEGState;
#define JState(tif) ((JPEGState *)(tif)->tif_data)
static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
static int JPEGInitializeLibJPEG(TIFF *tif, int decode);
static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
#define FIELD_JPEGTABLES (FIELD_CODEC + 0)
static const TIFFField jpegFields[] = {
{TIFFTAG_JPEGTABLES, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8,
TIFF_SETGET_C32_UINT8, FIELD_JPEGTABLES, FALSE, TRUE, "JPEGTables", NULL},
{TIFFTAG_JPEGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL},
{TIFFTAG_JPEGCOLORMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL},
{TIFFTAG_JPEGTABLESMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL}};
static void TIFFjpeg_error_exit(j_common_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
char buffer[JMSG_LENGTH_MAX];
(*cinfo->err->format_message)(cinfo, buffer);
TIFFErrorExtR(sp->tif, "JPEGLib", "%s",
buffer);
jpeg_abort(cinfo);
LONGJMP(sp->exit_jmpbuf, 1);
}
static void TIFFjpeg_output_message(j_common_ptr cinfo)
{
char buffer[JMSG_LENGTH_MAX];
(*cinfo->err->format_message)(cinfo, buffer);
TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer);
}
static void TIFFjpeg_progress_monitor(j_common_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
if (cinfo->is_decompressor)
{
const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number;
if (scan_no >= sp->otherSettings.max_allowed_scan_number)
{
TIFFErrorExtR(
((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor",
"Scan number %d exceeds maximum scans (%d). This limit "
"can be raised through the "
"LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER "
"environment variable.",
scan_no, sp->otherSettings.max_allowed_scan_number);
jpeg_abort(cinfo);
LONGJMP(sp->exit_jmpbuf, 1);
}
}
}
#define CALLJPEG(sp, fail, op) (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op))
#define CALLVJPEG(sp, op) CALLJPEG(sp, 0, ((op), 1))
static int TIFFjpeg_create_compress(JPEGState *sp)
{
sp->cinfo.c.err = jpeg_std_error(&sp->err);
sp->err.error_exit = TIFFjpeg_error_exit;
sp->err.output_message = TIFFjpeg_output_message;
sp->cinfo.c.client_data = NULL;
return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c));
}
static int TIFFjpeg_create_decompress(JPEGState *sp)
{
sp->cinfo.d.err = jpeg_std_error(&sp->err);
sp->err.error_exit = TIFFjpeg_error_exit;
sp->err.output_message = TIFFjpeg_output_message;
sp->cinfo.d.client_data = NULL;
return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d));
}
static int TIFFjpeg_set_defaults(JPEGState *sp)
{
return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c));
}
static int TIFFjpeg_set_colorspace(JPEGState *sp, J_COLOR_SPACE colorspace)
{
return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace));
}
static int TIFFjpeg_set_quality(JPEGState *sp, int quality,
boolean force_baseline)
{
return CALLVJPEG(sp,
jpeg_set_quality(&sp->cinfo.c, quality, force_baseline));
}
static int TIFFjpeg_suppress_tables(JPEGState *sp, boolean suppress)
{
return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress));
}
static int TIFFjpeg_start_compress(JPEGState *sp, boolean write_all_tables)
{
return CALLVJPEG(sp, jpeg_start_compress(&sp->cinfo.c, write_all_tables));
}
static int TIFFjpeg_write_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines,
int num_lines)
{
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
return CALLJPEG(sp, -1,
(int)jpeg12_write_scanlines(&sp->cinfo.c, scanlines,
(JDIMENSION)num_lines));
#else
return CALLJPEG(sp, -1,
(int)jpeg_write_scanlines(&sp->cinfo.c, scanlines,
(JDIMENSION)num_lines));
#endif
}
static int TIFFjpeg_write_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data,
int num_lines)
{
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
return CALLJPEG(
sp, -1,
(int)jpeg12_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines));
#else
return CALLJPEG(
sp, -1,
(int)jpeg_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines));
#endif
}
static int TIFFjpeg_finish_compress(JPEGState *sp)
{
return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c));
}
static int TIFFjpeg_write_tables(JPEGState *sp)
{
return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c));
}
static int TIFFjpeg_read_header(JPEGState *sp, boolean require_image)
{
return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image));
}
static int TIFFjpeg_has_multiple_scans(JPEGState *sp)
{
return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d));
}
static int TIFFjpeg_start_decompress(JPEGState *sp)
{
const char *sz_max_allowed_scan_number;
sp->cinfo.d.progress = &sp->progress;
sp->progress.progress_monitor = TIFFjpeg_progress_monitor;
sp->otherSettings.max_allowed_scan_number = 100;
sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER");
if (sz_max_allowed_scan_number)
sp->otherSettings.max_allowed_scan_number =
atoi(sz_max_allowed_scan_number);
return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d));
}
static int TIFFjpeg_read_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines,
int max_lines)
{
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
return CALLJPEG(sp, -1,
(int)jpeg12_read_scanlines(&sp->cinfo.d, scanlines,
(JDIMENSION)max_lines));
#else
return CALLJPEG(sp, -1,
(int)jpeg_read_scanlines(&sp->cinfo.d, scanlines,
(JDIMENSION)max_lines));
#endif
}
static int TIFFjpeg_read_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data,
int max_lines)
{
#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12
return CALLJPEG(
sp, -1,
(int)jpeg12_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines));
#else
return CALLJPEG(
sp, -1,
(int)jpeg_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines));
#endif
}
static int TIFFjpeg_finish_decompress(JPEGState *sp)
{
return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d));
}
static int TIFFjpeg_abort(JPEGState *sp)
{
return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm));
}
static int TIFFjpeg_destroy(JPEGState *sp)
{
return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm));
}
static JSAMPARRAY TIFFjpeg_alloc_sarray(JPEGState *sp, int pool_id,
JDIMENSION samplesperrow,
JDIMENSION numrows)
{
return CALLJPEG(sp, (JSAMPARRAY)NULL,
(*sp->cinfo.comm.mem->alloc_sarray)(
&sp->cinfo.comm, pool_id, samplesperrow, numrows));
}
static void std_init_destination(j_compress_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
TIFF *tif = sp->tif;
sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
}
static boolean std_empty_output_buffer(j_compress_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
TIFF *tif = sp->tif;
tif->tif_rawcc = tif->tif_rawdatasize;
#ifdef IPPJ_HUFF
if (sp->dest.free_in_buffer >= 0)
{
tif->tif_rawcc = tif->tif_rawdatasize - sp->dest.free_in_buffer;
}
#endif
if (!TIFFFlushData1(tif))
return FALSE;
sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
return (TRUE);
}
static void std_term_destination(j_compress_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
TIFF *tif = sp->tif;
tif->tif_rawcp = (uint8_t *)sp->dest.next_output_byte;
tif->tif_rawcc = tif->tif_rawdatasize - (tmsize_t)sp->dest.free_in_buffer;
}
static void TIFFjpeg_data_dest(JPEGState *sp, TIFF *tif)
{
(void)tif;
sp->cinfo.c.dest = &sp->dest;
sp->dest.init_destination = std_init_destination;
sp->dest.empty_output_buffer = std_empty_output_buffer;
sp->dest.term_destination = std_term_destination;
}
static void tables_init_destination(j_compress_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
sp->dest.next_output_byte = (JOCTET *)sp->otherSettings.jpegtables;
sp->dest.free_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
}
static boolean tables_empty_output_buffer(j_compress_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
void *newbuf;
newbuf =
_TIFFreallocExt(sp->tif, (void *)sp->otherSettings.jpegtables,
(tmsize_t)(sp->otherSettings.jpegtables_length + 1000));
if (newbuf == NULL)
ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 100);
sp->dest.next_output_byte =
(JOCTET *)newbuf + sp->otherSettings.jpegtables_length;
sp->dest.free_in_buffer = (size_t)1000;
sp->otherSettings.jpegtables = newbuf;
sp->otherSettings.jpegtables_length += 1000;
return (TRUE);
}
static void tables_term_destination(j_compress_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
sp->otherSettings.jpegtables_length -= (uint32_t)sp->dest.free_in_buffer;
}
static int TIFFjpeg_tables_dest(JPEGState *sp, TIFF *tif)
{
(void)tif;
if (sp->otherSettings.jpegtables)
_TIFFfreeExt(tif, sp->otherSettings.jpegtables);
sp->otherSettings.jpegtables_length = 1000;
sp->otherSettings.jpegtables = (void *)_TIFFmallocExt(
tif, (tmsize_t)sp->otherSettings.jpegtables_length);
if (sp->otherSettings.jpegtables == NULL)
{
sp->otherSettings.jpegtables_length = 0;
TIFFErrorExtR(sp->tif, "TIFFjpeg_tables_dest",
"No space for JPEGTables");
return (0);
}
sp->cinfo.c.dest = &sp->dest;
sp->dest.init_destination = tables_init_destination;
sp->dest.empty_output_buffer = tables_empty_output_buffer;
sp->dest.term_destination = tables_term_destination;
return (1);
}
static void std_init_source(j_decompress_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
TIFF *tif = sp->tif;
sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata;
sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
}
static boolean std_fill_input_buffer(j_decompress_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
static const JOCTET dummy_EOI[2] = {0xFF, JPEG_EOI};
#ifdef IPPJ_HUFF
if (sp->src.bytes_in_buffer > 0)
{
return (TRUE);
}
#endif
WARNMS(cinfo, JWRN_JPEG_EOF);
sp->src.next_input_byte = dummy_EOI;
sp->src.bytes_in_buffer = 2;
return (TRUE);
}
static void std_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
{
JPEGState *sp = (JPEGState *)cinfo;
if (num_bytes > 0)
{
if ((size_t)num_bytes > sp->src.bytes_in_buffer)
{
(void)std_fill_input_buffer(cinfo);
}
else
{
sp->src.next_input_byte += (size_t)num_bytes;
sp->src.bytes_in_buffer -= (size_t)num_bytes;
}
}
}
static void std_term_source(j_decompress_ptr cinfo)
{
(void)cinfo;
}
static void TIFFjpeg_data_src(JPEGState *sp)
{
sp->cinfo.d.src = &sp->src;
sp->src.init_source = std_init_source;
sp->src.fill_input_buffer = std_fill_input_buffer;
sp->src.skip_input_data = std_skip_input_data;
sp->src.resync_to_restart = jpeg_resync_to_restart;
sp->src.term_source = std_term_source;
sp->src.bytes_in_buffer = 0;
sp->src.next_input_byte = NULL;
}
static void tables_init_source(j_decompress_ptr cinfo)
{
JPEGState *sp = (JPEGState *)cinfo;
sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables;
sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length;
}
static void TIFFjpeg_tables_src(JPEGState *sp)
{
TIFFjpeg_data_src(sp);
sp->src.init_source = tables_init_source;
}
static int alloc_downsampled_buffers(TIFF *tif, jpeg_component_info *comp_info,
int num_components)
{
JPEGState *sp = JState(tif);
int ci;
jpeg_component_info *compptr;
TIFF_JSAMPARRAY buf;
int samples_per_clump = 0;
for (ci = 0, compptr = comp_info; ci < num_components; ci++, compptr++)
{
samples_per_clump += compptr->h_samp_factor * compptr->v_samp_factor;
buf = (TIFF_JSAMPARRAY)TIFFjpeg_alloc_sarray(
sp, JPOOL_IMAGE, compptr->width_in_blocks * DCTSIZE,
(JDIMENSION)(compptr->v_samp_factor * DCTSIZE));
if (buf == NULL)
return (0);
sp->ds_buffer[ci] = buf;
}
sp->samplesperclump = samples_per_clump;
return (1);
}
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
#define JPEG_MARKER_SOF0 0xC0
#define JPEG_MARKER_SOF1 0xC1
#define JPEG_MARKER_SOF2 0xC2
#define JPEG_MARKER_SOF9 0xC9
#define JPEG_MARKER_SOF10 0xCA
#define JPEG_MARKER_DHT 0xC4
#define JPEG_MARKER_SOI 0xD8
#define JPEG_MARKER_SOS 0xDA
#define JPEG_MARKER_DQT 0xDB
#define JPEG_MARKER_DRI 0xDD
#define JPEG_MARKER_APP0 0xE0
#define JPEG_MARKER_COM 0xFE
struct JPEGFixupTagsSubsamplingData
{
TIFF *tif;
void *buffer;
uint32_t buffersize;
uint8_t *buffercurrentbyte;
uint32_t bufferbytesleft;
uint64_t fileoffset;
uint64_t filebytesleft;
uint8_t filepositioned;
};
static void JPEGFixupTagsSubsampling(TIFF *tif);
static int
JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData *data);
static int
JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData *data,
uint8_t *result);
static int
JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData *data,
uint16_t *result);
static void
JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData *data,
uint16_t skiplength);
#endif
static int JPEGFixupTags(TIFF *tif)
{
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
JPEGState *sp = JState(tif);
if ((tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR) &&
(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
(tif->tif_dir.td_samplesperpixel == 3) &&
!sp->otherSettings.ycbcrsampling_fetched)
JPEGFixupTagsSubsampling(tif);
#endif
return (1);
}
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
static void JPEGFixupTagsSubsampling(TIFF *tif)
{
static const char module[] = "JPEGFixupTagsSubsampling";
struct JPEGFixupTagsSubsamplingData m;
uint64_t fileoffset = TIFFGetStrileOffset(tif, 0);
if (fileoffset == 0)
{
return;
}
m.tif = tif;
m.buffersize = 2048;
m.buffer = _TIFFmallocExt(tif, m.buffersize);
if (m.buffer == NULL)
{
TIFFWarningExtR(tif, module,
"Unable to allocate memory for auto-correcting of "
"subsampling values; auto-correcting skipped");
return;
}
m.buffercurrentbyte = NULL;
m.bufferbytesleft = 0;
m.fileoffset = fileoffset;
m.filepositioned = 0;
m.filebytesleft = TIFFGetStrileByteCount(tif, 0);
if (!JPEGFixupTagsSubsamplingSec(&m))
TIFFWarningExtR(
tif, module,
"Unable to auto-correct subsampling values, likely corrupt JPEG "
"compressed data in first strip/tile; auto-correcting skipped");
_TIFFfreeExt(tif, m.buffer);
}
static int
JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData *data)
{
static const char module[] = "JPEGFixupTagsSubsamplingSec";
uint8_t m;
while (1)
{
while (1)
{
if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
return (0);
if (m == 255)
break;
}
while (1)
{
if (!JPEGFixupTagsSubsamplingReadByte(data, &m))
return (0);
if (m != 255)
break;
}
switch (m)
{
case JPEG_MARKER_SOI:
break;
case JPEG_MARKER_COM:
case JPEG_MARKER_APP0:
case JPEG_MARKER_APP0 + 1:
case JPEG_MARKER_APP0 + 2:
case JPEG_MARKER_APP0 + 3:
case JPEG_MARKER_APP0 + 4:
case JPEG_MARKER_APP0 + 5:
case JPEG_MARKER_APP0 + 6:
case JPEG_MARKER_APP0 + 7:
case JPEG_MARKER_APP0 + 8:
case JPEG_MARKER_APP0 + 9:
case JPEG_MARKER_APP0 + 10:
case JPEG_MARKER_APP0 + 11:
case JPEG_MARKER_APP0 + 12:
case JPEG_MARKER_APP0 + 13:
case JPEG_MARKER_APP0 + 14:
case JPEG_MARKER_APP0 + 15:
case JPEG_MARKER_DQT:
case JPEG_MARKER_SOS:
case JPEG_MARKER_DHT:
case JPEG_MARKER_DRI:
{
uint16_t n;
if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
return (0);
if (n < 2)
return (0);
n -= 2;
if (n > 0)
JPEGFixupTagsSubsamplingSkip(data, n);
}
break;
case JPEG_MARKER_SOF0:
case JPEG_MARKER_SOF1:
case JPEG_MARKER_SOF2:
case JPEG_MARKER_SOF9:
case JPEG_MARKER_SOF10:
{
uint16_t n;
uint16_t o;
uint8_t p;
uint8_t ph, pv;
if (!JPEGFixupTagsSubsamplingReadWord(data, &n))
return (0);
if (n != 8 + data->tif->tif_dir.td_samplesperpixel * 3)
return (0);
JPEGFixupTagsSubsamplingSkip(data, 7);
if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
return (0);
ph = (p >> 4);
pv = (p & 15);
JPEGFixupTagsSubsamplingSkip(data, 1);
for (o = 1; o < data->tif->tif_dir.td_samplesperpixel; o++)
{
JPEGFixupTagsSubsamplingSkip(data, 1);
if (!JPEGFixupTagsSubsamplingReadByte(data, &p))
return (0);
if (p != 0x11)
{
TIFFWarningExtR(data->tif, module,
"Subsampling values inside JPEG "
"compressed data "
"have no TIFF equivalent, "
"auto-correction of TIFF "
"subsampling values failed");
return (1);
}
JPEGFixupTagsSubsamplingSkip(data, 1);
}
if (((ph != 1) && (ph != 2) && (ph != 4)) ||
((pv != 1) && (pv != 2) && (pv != 4)))
{
TIFFWarningExtR(data->tif, module,
"Subsampling values inside JPEG "
"compressed data have no TIFF "
"equivalent, auto-correction of TIFF "
"subsampling values failed");
return (1);
}
if ((ph != data->tif->tif_dir.td_ycbcrsubsampling[0]) ||
(pv != data->tif->tif_dir.td_ycbcrsubsampling[1]))
{
TIFFWarningExtR(
data->tif, module,
"Auto-corrected former TIFF subsampling values "
"[%" PRIu16 ",%" PRIu16
"] to match subsampling values inside JPEG "
"compressed data [%" PRIu8 ",%" PRIu8 "]",
data->tif->tif_dir.td_ycbcrsubsampling[0],
data->tif->tif_dir.td_ycbcrsubsampling[1], ph, pv);
data->tif->tif_dir.td_ycbcrsubsampling[0] = ph;
data->tif->tif_dir.td_ycbcrsubsampling[1] = pv;
}
}
return (1);
default:
return (0);
}
}
}
static int
JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData *data,
uint8_t *result)
{
if (data->bufferbytesleft == 0)
{
uint32_t m;
if (data->filebytesleft == 0)
return (0);
if (!data->filepositioned)
{
if (TIFFSeekFile(data->tif, data->fileoffset, SEEK_SET) ==
(toff_t)-1)
{
return 0;
}
data->filepositioned = 1;
}
m = data->buffersize;
if ((uint64_t)m > data->filebytesleft)
m = (uint32_t)data->filebytesleft;
assert(m < 0x80000000UL);
if (TIFFReadFile(data->tif, data->buffer, (tmsize_t)m) != (tmsize_t)m)
return (0);
data->buffercurrentbyte = data->buffer;
data->bufferbytesleft = m;
data->fileoffset += m;
data->filebytesleft -= m;
}
*result = *data->buffercurrentbyte;
data->buffercurrentbyte++;
data->bufferbytesleft--;
return (1);
}
static int
JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData *data,
uint16_t *result)
{
uint8_t ma;
uint8_t mb;
if (!JPEGFixupTagsSubsamplingReadByte(data, &ma))
return (0);
if (!JPEGFixupTagsSubsamplingReadByte(data, &mb))
return (0);
*result = (ma << 8) | mb;
return (1);
}
static void
JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData *data,
uint16_t skiplength)
{
if ((uint32_t)skiplength <= data->bufferbytesleft)
{
data->buffercurrentbyte += skiplength;
data->bufferbytesleft -= skiplength;
}
else
{
uint16_t m;
m = (uint16_t)(skiplength - data->bufferbytesleft);
if (m <= data->filebytesleft)
{
data->bufferbytesleft = 0;
data->fileoffset += m;
data->filebytesleft -= m;
data->filepositioned = 0;
}
else
{
data->bufferbytesleft = 0;
data->filebytesleft = 0;
}
}
}
#endif
static int JPEGSetupDecode(TIFF *tif)
{
JPEGState *sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
if (tif->tif_dir.td_bitspersample == 12)
{
JPEGOtherSettings savedOtherSettings = sp->otherSettings;
return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0);
}
#endif
JPEGInitializeLibJPEG(tif, TRUE);
assert(sp != NULL);
assert(sp->cinfo.comm.is_decompressor);
if (TIFFFieldSet(tif, FIELD_JPEGTABLES))
{
TIFFjpeg_tables_src(sp);
if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY)
{
TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field");
return (0);
}
}
sp->photometric = td->td_photometric;
switch (sp->photometric)
{
case PHOTOMETRIC_YCBCR:
sp->h_sampling = td->td_ycbcrsubsampling[0];
sp->v_sampling = td->td_ycbcrsubsampling[1];
break;
default:
sp->h_sampling = 1;
sp->v_sampling = 1;
break;
}
TIFFjpeg_data_src(sp);
tif->tif_postdecode = _TIFFNoPostDecode;
return (1);
}
int TIFFJPEGIsFullStripRequired(TIFF *tif)
{
int ret;
JPEGState state;
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
if (tif->tif_dir.td_bitspersample == 12)
return TIFFJPEGIsFullStripRequired_12(tif);
#endif
memset(&state, 0, sizeof(JPEGState));
state.tif = tif;
TIFFjpeg_create_decompress(&state);
TIFFjpeg_data_src(&state);
if (TIFFjpeg_read_header(&state, TRUE) != JPEG_HEADER_OK)
{
TIFFjpeg_destroy(&state);
return (0);
}
ret = TIFFjpeg_has_multiple_scans(&state);
TIFFjpeg_destroy(&state);
return ret;
}
static int JPEGPreDecode(TIFF *tif, uint16_t s)
{
JPEGState *sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
static const char module[] = "JPEGPreDecode";
uint32_t segment_width, segment_height;
int downsampled_output;
int ci;
assert(sp != NULL);
if (sp->cinfo.comm.is_decompressor == 0)
{
tif->tif_setupdecode(tif);
}
assert(sp->cinfo.comm.is_decompressor);
if (!TIFFjpeg_abort(sp))
return (0);
if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
return (0);
tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
tif->tif_rawcc = sp->src.bytes_in_buffer;
if (isTiled(tif))
{
segment_width = td->td_tilewidth;
segment_height = td->td_tilelength;
sp->bytesperline = TIFFTileRowSize(tif);
}
else
{
segment_width = td->td_imagewidth;
segment_height = td->td_imagelength - tif->tif_row;
if (segment_height > td->td_rowsperstrip)
segment_height = td->td_rowsperstrip;
sp->bytesperline = TIFFScanlineSize(tif);
}
if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
{
if (sp->h_sampling == 0 || sp->v_sampling == 0)
{
TIFFErrorExtR(tif, module,
"JPEG horizontal or vertical sampling is zero");
return (0);
}
segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
}
if (sp->cinfo.d.image_width < segment_width ||
sp->cinfo.d.image_height < segment_height)
{
TIFFWarningExtR(tif, module,
"Improper JPEG strip/tile size, "
"expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
segment_width, segment_height, sp->cinfo.d.image_width,
sp->cinfo.d.image_height);
}
if (sp->cinfo.d.image_width == segment_width &&
sp->cinfo.d.image_height > segment_height &&
tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif))
{
TIFFWarningExtR(tif, module,
"JPEG strip size exceeds expected dimensions,"
" expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
segment_width, segment_height, sp->cinfo.d.image_width,
sp->cinfo.d.image_height);
}
else if (sp->cinfo.d.image_width > segment_width ||
sp->cinfo.d.image_height > segment_height)
{
TIFFErrorExtR(tif, module,
"JPEG strip/tile size exceeds expected dimensions,"
" expected %" PRIu32 "x%" PRIu32 ", got %ux%u",
segment_width, segment_height, sp->cinfo.d.image_width,
sp->cinfo.d.image_height);
return (0);
}
if (sp->cinfo.d.num_components !=
(td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
: 1))
{
TIFFErrorExtR(tif, module, "Improper JPEG component count");
return (0);
}
#ifdef JPEG_LIB_MK1
if (12 != td->td_bitspersample && 8 != td->td_bitspersample)
{
TIFFErrorExtR(tif, module, "Improper JPEG data precision");
return (0);
}
sp->cinfo.d.data_precision = td->td_bitspersample;
sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
#else
if (sp->cinfo.d.data_precision != td->td_bitspersample)
{
TIFFErrorExtR(tif, module, "Improper JPEG data precision");
return (0);
}
#endif
if (sp->cinfo.d.progressive_mode &&
!sp->otherSettings.has_warned_about_progressive_mode)
{
TIFFWarningExtR(tif, module,
"The JPEG strip/tile is encoded with progressive mode, "
"which is normally not legal for JPEG-in-TIFF.\n"
"libtiff should be able to decode it, but it might "
"cause compatibility issues with other readers");
sp->otherSettings.has_warned_about_progressive_mode = TRUE;
}
if (TIFFjpeg_has_multiple_scans(sp))
{
toff_t nRequiredMemory = 1024 * 1024;
for (ci = 0; ci < sp->cinfo.d.num_components; ci++)
{
const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]);
if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0)
{
nRequiredMemory +=
(toff_t)(((compptr->width_in_blocks +
compptr->h_samp_factor - 1) /
compptr->h_samp_factor)) *
((compptr->height_in_blocks + compptr->v_samp_factor - 1) /
compptr->v_samp_factor) *
sizeof(JBLOCK);
}
}
if (sp->cinfo.d.mem->max_memory_to_use > 0 &&
nRequiredMemory > (toff_t)(sp->cinfo.d.mem->max_memory_to_use) &&
getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL)
{
TIFFErrorExtR(
tif, module,
"Reading this image would require libjpeg to allocate "
"at least %" PRIu64 " bytes. "
"This is disabled since above the %ld threshold. "
"You may override this restriction by defining the "
"LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, "
"or setting the JPEGMEM environment variable to a value "
"greater "
"or equal to '%" PRIu64 "M'",
nRequiredMemory, sp->cinfo.d.mem->max_memory_to_use,
(nRequiredMemory + 1000000u - 1u) / 1000000u);
return 0;
}
}
if (td->td_planarconfig == PLANARCONFIG_CONTIG)
{
if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling)
{
TIFFErrorExtR(tif, module,
"Improper JPEG sampling factors %d,%d\n"
"Apparently should be %" PRIu16 ",%" PRIu16 ".",
sp->cinfo.d.comp_info[0].h_samp_factor,
sp->cinfo.d.comp_info[0].v_samp_factor,
sp->h_sampling, sp->v_sampling);
return (0);
}
for (ci = 1; ci < sp->cinfo.d.num_components; ci++)
{
if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
sp->cinfo.d.comp_info[ci].v_samp_factor != 1)
{
TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
return (0);
}
}
}
else
{
if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
sp->cinfo.d.comp_info[0].v_samp_factor != 1)
{
TIFFErrorExtR(tif, module, "Improper JPEG sampling factors");
return (0);
}
}
downsampled_output = FALSE;
if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
sp->photometric == PHOTOMETRIC_YCBCR &&
sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
{
sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
sp->cinfo.d.out_color_space = JCS_RGB;
}
else
{
sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
sp->cinfo.d.out_color_space = JCS_UNKNOWN;
if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
(sp->h_sampling != 1 || sp->v_sampling != 1))
downsampled_output = TRUE;
}
if (downsampled_output)
{
sp->cinfo.d.raw_data_out = TRUE;
#if JPEG_LIB_VERSION >= 70
sp->cinfo.d.do_fancy_upsampling = FALSE;
#endif
tif->tif_decoderow = DecodeRowError;
tif->tif_decodestrip = JPEGDecodeRaw;
tif->tif_decodetile = JPEGDecodeRaw;
}
else
{
sp->cinfo.d.raw_data_out = FALSE;
tif->tif_decoderow = JPEGDecode;
tif->tif_decodestrip = JPEGDecode;
tif->tif_decodetile = JPEGDecode;
}
if (!TIFFjpeg_start_decompress(sp))
return (0);
if (downsampled_output)
{
if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info,
sp->cinfo.d.num_components))
return (0);
sp->scancount = DCTSIZE;
}
return (1);
}
#if !JPEG_LIB_MK1_OR_12BIT
static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
{
JPEGState *sp = JState(tif);
tmsize_t nrows;
(void)s;
sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp;
sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
if (sp->bytesperline == 0)
{
memset(buf, 0, (size_t)cc);
return 0;
}
nrows = cc / sp->bytesperline;
if (cc % sp->bytesperline)
TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read");
if (nrows > (tmsize_t)sp->cinfo.d.image_height)
nrows = sp->cinfo.d.image_height;
if (nrows)
{
do
{
JSAMPROW bufptr = (JSAMPROW)buf;
if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1)
{
memset(buf, 0, (size_t)cc);
return (0);
}
++tif->tif_row;
buf += sp->bytesperline;
cc -= sp->bytesperline;
} while (--nrows > 0);
}
tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
tif->tif_rawcc = sp->src.bytes_in_buffer;
return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
TIFFjpeg_finish_decompress(sp);
}
#endif
#if JPEG_LIB_MK1_OR_12BIT
static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc,
uint16_t s)
{
JPEGState *sp = JState(tif);
tmsize_t nrows;
(void)s;
sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp;
sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
if (sp->bytesperline == 0)
{
memset(buf, 0, (size_t)cc);
return 0;
}
nrows = cc / sp->bytesperline;
if (cc % sp->bytesperline)
TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read");
if (nrows > (tmsize_t)sp->cinfo.d.image_height)
nrows = sp->cinfo.d.image_height;
if (nrows)
{
TIFF_JSAMPROW line_work_buf = NULL;
if (sp->cinfo.d.data_precision == 12)
{
line_work_buf = (TIFF_JSAMPROW)_TIFFmallocExt(
tif, sizeof(short) * sp->cinfo.d.output_width *
sp->cinfo.d.num_components);
}
do
{
if (line_work_buf != NULL)
{
if (TIFFjpeg_read_scanlines(sp, &line_work_buf, 1) != 1)
{
memset(buf, 0, (size_t)cc);
return (0);
}
if (sp->cinfo.d.data_precision == 12)
{
int value_pairs = (sp->cinfo.d.output_width *
sp->cinfo.d.num_components) /
2;
int iPair;
for (iPair = 0; iPair < value_pairs; iPair++)
{
unsigned char *out_ptr =
((unsigned char *)buf) + iPair * 3;
TIFF_JSAMPLE *in_ptr = line_work_buf + iPair * 2;
out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4);
out_ptr[1] =
(unsigned char)(((in_ptr[0] & 0xf) << 4) |
((in_ptr[1] & 0xf00) >> 8));
out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0));
}
}
else if (sp->cinfo.d.data_precision == 8)
{
int value_count =
(sp->cinfo.d.output_width * sp->cinfo.d.num_components);
int iValue;
for (iValue = 0; iValue < value_count; iValue++)
{
((unsigned char *)buf)[iValue] =
line_work_buf[iValue] & 0xff;
}
}
}
++tif->tif_row;
buf += sp->bytesperline;
cc -= sp->bytesperline;
} while (--nrows > 0);
if (line_work_buf != NULL)
_TIFFfreeExt(tif, line_work_buf);
}
tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte;
tif->tif_rawcc = sp->src.bytes_in_buffer;
return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
TIFFjpeg_finish_decompress(sp);
}
#endif
static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc,
uint16_t s)
{
(void)buf;
(void)cc;
(void)s;
TIFFErrorExtR(
tif, "TIFFReadScanline",
"scanline oriented access is not supported for downsampled JPEG "
"compressed images, consider enabling TIFFTAG_JPEGCOLORMODE as "
"JPEGCOLORMODE_RGB.");
return 0;
}
static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc,
uint16_t s)
{
JPEGState *sp = JState(tif);
tmsize_t nrows;
TIFFDirectory *td = &tif->tif_dir;
(void)s;
nrows = sp->cinfo.d.image_height;
if ((uint32_t)nrows > td->td_imagelength - tif->tif_row && !isTiled(tif))
nrows = td->td_imagelength - tif->tif_row;
#if defined(JPEG_LIB_MK1_OR_12BIT)
unsigned short *tmpbuf = NULL;
#endif
if (nrows != 0)
{
JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width;
int samples_per_clump = sp->samplesperclump;
#if defined(JPEG_LIB_MK1_OR_12BIT)
tmpbuf = _TIFFmallocExt(tif, sizeof(unsigned short) *
sp->cinfo.d.output_width *
sp->cinfo.d.num_components);
if (tmpbuf == NULL)
{
TIFFErrorExtR(tif, "JPEGDecodeRaw", "Out of memory");
return 0;
}
#endif
do
{
jpeg_component_info *compptr;
int ci, clumpoffset;
if (cc < sp->bytesperline)
{
TIFFErrorExtR(
tif, "JPEGDecodeRaw",
"application buffer not large enough for all data.");
goto error;
}
if (sp->scancount >= DCTSIZE)
{
int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE;
if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n) != n)
goto error;
sp->scancount = 0;
}
clumpoffset = 0;
for (ci = 0, compptr = sp->cinfo.d.comp_info;
ci < sp->cinfo.d.num_components; ci++, compptr++)
{
int hsamp = compptr->h_samp_factor;
int vsamp = compptr->v_samp_factor;
int ypos;
for (ypos = 0; ypos < vsamp; ypos++)
{
TIFF_JSAMPLE *inptr =
sp->ds_buffer[ci][sp->scancount * vsamp + ypos];
JDIMENSION nclump;
#if defined(JPEG_LIB_MK1_OR_12BIT)
TIFF_JSAMPLE *outptr = (TIFF_JSAMPLE *)tmpbuf + clumpoffset;
#else
TIFF_JSAMPLE *outptr = (TIFF_JSAMPLE *)buf + clumpoffset;
if (cc < (tmsize_t)(clumpoffset +
(tmsize_t)samples_per_clump *
(clumps_per_line - 1) +
hsamp))
{
TIFFErrorExtR(
tif, "JPEGDecodeRaw",
"application buffer not large enough for all data, "
"possible subsampling issue");
goto error;
}
#endif
if (hsamp == 1)
{
for (nclump = clumps_per_line; nclump-- > 0;)
{
outptr[0] = *inptr++;
outptr += samples_per_clump;
}
}
else
{
int xpos;
for (nclump = clumps_per_line; nclump-- > 0;)
{
for (xpos = 0; xpos < hsamp; xpos++)
outptr[xpos] = *inptr++;
outptr += samples_per_clump;
}
}
clumpoffset += hsamp;
}
}
#if defined(JPEG_LIB_MK1_OR_12BIT)
{
if (sp->cinfo.d.data_precision == 8)
{
int i = 0;
int len =
sp->cinfo.d.output_width * sp->cinfo.d.num_components;
for (i = 0; i < len; i++)
{
((unsigned char *)buf)[i] = tmpbuf[i] & 0xff;
}
}
else
{
int value_pairs = (sp->cinfo.d.output_width *
sp->cinfo.d.num_components) /
2;
int iPair;
for (iPair = 0; iPair < value_pairs; iPair++)
{
unsigned char *out_ptr =
((unsigned char *)buf) + iPair * 3;
JSAMPLE *in_ptr = (JSAMPLE *)(tmpbuf + iPair * 2);
out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4);
out_ptr[1] =
(unsigned char)(((in_ptr[0] & 0xf) << 4) |
((in_ptr[1] & 0xf00) >> 8));
out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0));
}
}
}
#endif
sp->scancount++;
tif->tif_row += sp->v_sampling;
buf += sp->bytesperline;
cc -= sp->bytesperline;
nrows -= sp->v_sampling;
} while (nrows > 0);
#if defined(JPEG_LIB_MK1_OR_12BIT)
_TIFFfreeExt(tif, tmpbuf);
#endif
}
return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height ||
TIFFjpeg_finish_decompress(sp);
error:
#if defined(JPEG_LIB_MK1_OR_12BIT)
_TIFFfreeExt(tif, tmpbuf);
#endif
return 0;
}
static void unsuppress_quant_table(JPEGState *sp, int tblno)
{
JQUANT_TBL *qtbl;
if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
qtbl->sent_table = FALSE;
}
static void suppress_quant_table(JPEGState *sp, int tblno)
{
JQUANT_TBL *qtbl;
if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
qtbl->sent_table = TRUE;
}
static void unsuppress_huff_table(JPEGState *sp, int tblno)
{
JHUFF_TBL *htbl;
if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
htbl->sent_table = FALSE;
if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
htbl->sent_table = FALSE;
}
static void suppress_huff_table(JPEGState *sp, int tblno)
{
JHUFF_TBL *htbl;
if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
htbl->sent_table = TRUE;
if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
htbl->sent_table = TRUE;
}
static int prepare_JPEGTables(TIFF *tif)
{
JPEGState *sp = JState(tif);
if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
return (0);
if (!TIFFjpeg_suppress_tables(sp, TRUE))
return (0);
if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
{
unsuppress_quant_table(sp, 0);
if (sp->photometric == PHOTOMETRIC_YCBCR)
unsuppress_quant_table(sp, 1);
}
if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
{
unsuppress_huff_table(sp, 0);
if (sp->photometric == PHOTOMETRIC_YCBCR)
unsuppress_huff_table(sp, 1);
}
if (!TIFFjpeg_tables_dest(sp, tif))
return (0);
if (!TIFFjpeg_write_tables(sp))
return (0);
return (1);
}
#if defined(JPEG_LIB_VERSION_MAJOR) && \
(JPEG_LIB_VERSION_MAJOR > 9 || \
(JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4))
static void TIFF_std_huff_tables(j_compress_ptr cinfo)
{
if (cinfo->dc_huff_tbl_ptrs[0] == NULL)
{
(void)jpeg_std_huff_table((j_common_ptr)cinfo, TRUE, 0);
}
if (cinfo->ac_huff_tbl_ptrs[0] == NULL)
{
(void)jpeg_std_huff_table((j_common_ptr)cinfo, FALSE, 0);
}
if (cinfo->dc_huff_tbl_ptrs[1] == NULL)
{
(void)jpeg_std_huff_table((j_common_ptr)cinfo, TRUE, 1);
}
if (cinfo->ac_huff_tbl_ptrs[1] == NULL)
{
(void)jpeg_std_huff_table((j_common_ptr)cinfo, FALSE, 1);
}
}
#endif
static int JPEGSetupEncode(TIFF *tif)
{
JPEGState *sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
static const char module[] = "JPEGSetupEncode";
#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12)
if (tif->tif_dir.td_bitspersample == 12)
{
JPEGOtherSettings savedOtherSettings = sp->otherSettings;
return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 1);
}
#endif
JPEGInitializeLibJPEG(tif, FALSE);
assert(sp != NULL);
assert(!sp->cinfo.comm.is_decompressor);
sp->photometric = td->td_photometric;
if (td->td_planarconfig == PLANARCONFIG_CONTIG)
{
sp->cinfo.c.input_components = td->td_samplesperpixel;
if (sp->photometric == PHOTOMETRIC_YCBCR)
{
if (sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
{
sp->cinfo.c.in_color_space = JCS_RGB;
}
else
{
sp->cinfo.c.in_color_space = JCS_YCbCr;
}
}
else
{
if ((td->td_photometric == PHOTOMETRIC_MINISWHITE ||
td->td_photometric == PHOTOMETRIC_MINISBLACK) &&
td->td_samplesperpixel == 1)
sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
else if (td->td_photometric == PHOTOMETRIC_RGB &&
td->td_samplesperpixel == 3)
sp->cinfo.c.in_color_space = JCS_RGB;
else if (td->td_photometric == PHOTOMETRIC_SEPARATED &&
td->td_samplesperpixel == 4)
sp->cinfo.c.in_color_space = JCS_CMYK;
else
sp->cinfo.c.in_color_space = JCS_UNKNOWN;
}
}
else
{
sp->cinfo.c.input_components = 1;
sp->cinfo.c.in_color_space = JCS_UNKNOWN;
}
if (!TIFFjpeg_set_defaults(sp))
return (0);
if (sp->cinfo.c.num_scans != 0 &&
(sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0)
{
TIFFWarningExtR(
tif, module,
"mozjpeg library likely detected. Disable emission of "
"Huffman tables in JpegTables tag, and use optimize_coding "
"to avoid potential issues");
sp->otherSettings.jpegtablesmode &= ~JPEGTABLESMODE_HUFF;
}
sp->cinfo.c.num_scans = 0;
sp->cinfo.c.scan_info = NULL;
switch (sp->photometric)
{
case PHOTOMETRIC_YCBCR:
sp->h_sampling = td->td_ycbcrsubsampling[0];
sp->v_sampling = td->td_ycbcrsubsampling[1];
if (sp->h_sampling == 0 || sp->v_sampling == 0)
{
TIFFErrorExtR(tif, module,
"Invalig horizontal/vertical sampling value");
return (0);
}
if (td->td_bitspersample > 16)
{
TIFFErrorExtR(tif, module,
"BitsPerSample %" PRIu16 " not allowed for JPEG",
td->td_bitspersample);
return (0);
}
{
float *ref;
if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE, &ref))
{
float refbw[6];
long top = 1L << td->td_bitspersample;
refbw[0] = 0;
refbw[1] = (float)(top - 1L);
refbw[2] = (float)(top >> 1);
refbw[3] = refbw[1];
refbw[4] = refbw[2];
refbw[5] = refbw[1];
TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw);
}
}
break;
case PHOTOMETRIC_PALETTE:
case PHOTOMETRIC_MASK:
TIFFErrorExtR(tif, module,
"PhotometricInterpretation %" PRIu16
" not allowed for JPEG",
sp->photometric);
return (0);
default:
sp->h_sampling = 1;
sp->v_sampling = 1;
break;
}
#ifdef JPEG_LIB_MK1
if (td->td_bitspersample != 8 && td->td_bitspersample != 12)
#else
if (td->td_bitspersample != BITS_IN_JSAMPLE)
#endif
{
TIFFErrorExtR(tif, module,
"BitsPerSample %" PRIu16 " not allowed for JPEG",
td->td_bitspersample);
return (0);
}
sp->cinfo.c.data_precision = td->td_bitspersample;
#ifdef JPEG_LIB_MK1
sp->cinfo.c.bits_in_jsample = td->td_bitspersample;
#endif
if (isTiled(tif))
{
if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0)
{
TIFFErrorExtR(tif, module,
"JPEG tile height must be multiple of %" PRIu32,
(uint32_t)(sp->v_sampling * DCTSIZE));
return (0);
}
if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0)
{
TIFFErrorExtR(tif, module,
"JPEG tile width must be multiple of %" PRIu32,
(uint32_t)(sp->h_sampling * DCTSIZE));
return (0);
}
}
else
{
if (td->td_rowsperstrip < td->td_imagelength &&
(td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0)
{
TIFFErrorExtR(tif, module,
"RowsPerStrip must be multiple of %" PRIu32
" for JPEG",
(uint32_t)(sp->v_sampling * DCTSIZE));
return (0);
}
}
if (sp->otherSettings.jpegtablesmode &
(JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF))
{
if (sp->otherSettings.jpegtables == NULL ||
memcmp(sp->otherSettings.jpegtables, "\0\0\0\0\0\0\0\0\0", 8) == 0)
{
#if defined(JPEG_LIB_VERSION_MAJOR) && \
(JPEG_LIB_VERSION_MAJOR > 9 || \
(JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4))
if ((sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0 &&
(sp->cinfo.c.dc_huff_tbl_ptrs[0] == NULL ||
sp->cinfo.c.dc_huff_tbl_ptrs[1] == NULL ||
sp->cinfo.c.ac_huff_tbl_ptrs[0] == NULL ||
sp->cinfo.c.ac_huff_tbl_ptrs[1] == NULL))
{
TIFF_std_huff_tables(&sp->cinfo.c);
}
#endif
if (!prepare_JPEGTables(tif))
return (0);
tif->tif_flags |= TIFF_DIRTYDIRECT;
TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
}
}
else
{
TIFFClrFieldBit(tif, FIELD_JPEGTABLES);
}
TIFFjpeg_data_dest(sp, tif);
return (1);
}
static int JPEGPreEncode(TIFF *tif, uint16_t s)
{
JPEGState *sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
static const char module[] = "JPEGPreEncode";
uint32_t segment_width, segment_height;
int downsampled_input;
assert(sp != NULL);
if (sp->cinfo.comm.is_decompressor == 1)
{
tif->tif_setupencode(tif);
}
assert(!sp->cinfo.comm.is_decompressor);
if (isTiled(tif))
{
segment_width = td->td_tilewidth;
segment_height = td->td_tilelength;
sp->bytesperline = TIFFTileRowSize(tif);
}
else
{
segment_width = td->td_imagewidth;
segment_height = td->td_imagelength - tif->tif_row;
if (segment_height > td->td_rowsperstrip)
segment_height = td->td_rowsperstrip;
sp->bytesperline = TIFFScanlineSize(tif);
}
if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0)
{
if (sp->h_sampling == 0 || sp->v_sampling == 0)
{
TIFFErrorExtR(tif, module,
"JPEG horizontal or vertical sampling is zero");
return (0);
}
segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
}
if (segment_width > 65535 || segment_height > 65535)
{
TIFFErrorExtR(tif, module, "Strip/tile too large for JPEG");
return (0);
}
sp->cinfo.c.image_width = segment_width;
sp->cinfo.c.image_height = segment_height;
downsampled_input = FALSE;
if (td->td_planarconfig == PLANARCONFIG_CONTIG)
{
sp->cinfo.c.input_components = td->td_samplesperpixel;
if (sp->photometric == PHOTOMETRIC_YCBCR)
{
if (sp->otherSettings.jpegcolormode != JPEGCOLORMODE_RGB)
{
if (sp->h_sampling != 1 || sp->v_sampling != 1)
downsampled_input = TRUE;
}
if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr))
return (0);
sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
}
else
{
if (!TIFFjpeg_set_colorspace(sp, sp->cinfo.c.in_color_space))
return (0);
}
}
else
{
if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
return (0);
sp->cinfo.c.comp_info[0].component_id = s;
if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0)
{
sp->cinfo.c.comp_info[0].quant_tbl_no = 1;
sp->cinfo.c.comp_info[0].dc_tbl_no = 1;
sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
}
}
sp->cinfo.c.write_JFIF_header = FALSE;
sp->cinfo.c.write_Adobe_marker = FALSE;
if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE))
return (0);
if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT)
{
suppress_quant_table(sp, 0);
suppress_quant_table(sp, 1);
}
else
{
unsuppress_quant_table(sp, 0);
unsuppress_quant_table(sp, 1);
}
if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF)
{
suppress_huff_table(sp, 0);
suppress_huff_table(sp, 1);
sp->cinfo.c.optimize_coding = FALSE;
}
else
sp->cinfo.c.optimize_coding = TRUE;
if (downsampled_input)
{
sp->cinfo.c.raw_data_in = TRUE;
tif->tif_encoderow = JPEGEncodeRaw;
tif->tif_encodestrip = JPEGEncodeRaw;
tif->tif_encodetile = JPEGEncodeRaw;
}
else
{
sp->cinfo.c.raw_data_in = FALSE;
tif->tif_encoderow = JPEGEncode;
tif->tif_encodestrip = JPEGEncode;
tif->tif_encodetile = JPEGEncode;
}
if (!TIFFjpeg_start_compress(sp, FALSE))
return (0);
if (downsampled_input)
{
if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info,
sp->cinfo.c.num_components))
return (0);
}
sp->scancount = 0;
return (1);
}
static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
{
JPEGState *sp = JState(tif);
tmsize_t nrows;
TIFF_JSAMPROW bufptr[1];
short *line16 = NULL;
int line16_count = 0;
(void)s;
assert(sp != NULL);
nrows = cc / sp->bytesperline;
if (cc % sp->bytesperline)
TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded");
if (!isTiled(tif) && tif->tif_row + nrows > tif->tif_dir.td_imagelength)
nrows = tif->tif_dir.td_imagelength - tif->tif_row;
if (sp->cinfo.c.data_precision == 12)
{
line16_count = (int)((sp->bytesperline * 2) / 3);
line16 = (short *)_TIFFmallocExt(tif, sizeof(short) * line16_count);
if (!line16)
{
TIFFErrorExtR(tif, "JPEGEncode", "Failed to allocate memory");
return 0;
}
}
while (nrows-- > 0)
{
if (sp->cinfo.c.data_precision == 12)
{
int value_pairs = line16_count / 2;
int iPair;
bufptr[0] = (TIFF_JSAMPROW)line16;
for (iPair = 0; iPair < value_pairs; iPair++)
{
unsigned char *in_ptr = ((unsigned char *)buf) + iPair * 3;
TIFF_JSAMPLE *out_ptr = (TIFF_JSAMPLE *)(line16 + iPair * 2);
out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4);
out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2];
}
}
else
{
bufptr[0] = (TIFF_JSAMPROW)buf;
}
if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
return (0);
if (nrows > 0)
tif->tif_row++;
buf += sp->bytesperline;
}
if (sp->cinfo.c.data_precision == 12)
{
_TIFFfreeExt(tif, line16);
}
return (1);
}
static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
{
JPEGState *sp = JState(tif);
TIFF_JSAMPLE *inptr;
TIFF_JSAMPLE *outptr;
tmsize_t nrows;
JDIMENSION clumps_per_line, nclump;
int clumpoffset, ci, xpos, ypos;
jpeg_component_info *compptr;
int samples_per_clump = sp->samplesperclump;
tmsize_t bytesperclumpline;
(void)s;
assert(sp != NULL);
bytesperclumpline =
((((tmsize_t)sp->cinfo.c.image_width + sp->h_sampling - 1) /
sp->h_sampling) *
((tmsize_t)sp->h_sampling * sp->v_sampling + 2) *
sp->cinfo.c.data_precision +
7) /
8;
nrows = (cc / bytesperclumpline) * sp->v_sampling;
if (cc % bytesperclumpline)
TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded");
clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width;
while (nrows > 0)
{
clumpoffset = 0;
for (ci = 0, compptr = sp->cinfo.c.comp_info;
ci < sp->cinfo.c.num_components; ci++, compptr++)
{
int hsamp = compptr->h_samp_factor;
int vsamp = compptr->v_samp_factor;
int padding = (int)(compptr->width_in_blocks * DCTSIZE -
clumps_per_line * hsamp);
for (ypos = 0; ypos < vsamp; ypos++)
{
inptr = ((TIFF_JSAMPLE *)buf) + clumpoffset;
outptr = sp->ds_buffer[ci][sp->scancount * vsamp + ypos];
if (hsamp == 1)
{
for (nclump = clumps_per_line; nclump-- > 0;)
{
*outptr++ = inptr[0];
inptr += samples_per_clump;
}
}
else
{
for (nclump = clumps_per_line; nclump-- > 0;)
{
for (xpos = 0; xpos < hsamp; xpos++)
*outptr++ = inptr[xpos];
inptr += samples_per_clump;
}
}
for (xpos = 0; xpos < padding; xpos++)
{
*outptr = outptr[-1];
outptr++;
}
clumpoffset += hsamp;
}
}
sp->scancount++;
if (sp->scancount >= DCTSIZE)
{
int n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
return (0);
sp->scancount = 0;
}
tif->tif_row += sp->v_sampling;
buf += bytesperclumpline;
nrows -= sp->v_sampling;
}
return (1);
}
static int JPEGPostEncode(TIFF *tif)
{
JPEGState *sp = JState(tif);
if (sp->scancount > 0)
{
int ci, ypos, n;
jpeg_component_info *compptr;
for (ci = 0, compptr = sp->cinfo.c.comp_info;
ci < sp->cinfo.c.num_components; ci++, compptr++)
{
int vsamp = compptr->v_samp_factor;
tmsize_t row_width =
compptr->width_in_blocks * DCTSIZE * sizeof(JSAMPLE);
for (ypos = sp->scancount * vsamp; ypos < DCTSIZE * vsamp; ypos++)
{
_TIFFmemcpy((void *)sp->ds_buffer[ci][ypos],
(void *)sp->ds_buffer[ci][ypos - 1], row_width);
}
}
n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
return (0);
}
return (TIFFjpeg_finish_compress(JState(tif)));
}
static void JPEGCleanup(TIFF *tif)
{
JPEGState *sp = JState(tif);
assert(sp != 0);
tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent;
tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent;
tif->tif_tagmethods.printdir = sp->otherSettings.printdir;
if (sp->cinfo_initialized)
TIFFjpeg_destroy(sp);
if (sp->otherSettings.jpegtables)
_TIFFfreeExt(tif, sp->otherSettings.jpegtables);
_TIFFfreeExt(tif, tif->tif_data);
tif->tif_data = NULL;
_TIFFSetDefaultCompressionState(tif);
}
static void JPEGResetUpsampled(TIFF *tif)
{
JPEGState *sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
tif->tif_flags &= ~TIFF_UPSAMPLED;
if (td->td_planarconfig == PLANARCONFIG_CONTIG)
{
if (td->td_photometric == PHOTOMETRIC_YCBCR &&
sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB)
{
tif->tif_flags |= TIFF_UPSAMPLED;
}
else
{
#ifdef notdef
if (td->td_ycbcrsubsampling[0] != 1 ||
td->td_ycbcrsubsampling[1] != 1)
;
#endif
}
}
if (tif->tif_tilesize > 0)
tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
if (tif->tif_scanlinesize > 0)
tif->tif_scanlinesize = TIFFScanlineSize(tif);
}
static int JPEGVSetField(TIFF *tif, uint32_t tag, va_list ap)
{
JPEGState *sp = JState(tif);
const TIFFField *fip;
uint32_t v32;
assert(sp != NULL);
switch (tag)
{
case TIFFTAG_JPEGTABLES:
v32 = (uint32_t)va_arg(ap, uint32_t);
if (v32 == 0)
{
return (0);
}
_TIFFsetByteArrayExt(tif, &sp->otherSettings.jpegtables,
va_arg(ap, void *), v32);
sp->otherSettings.jpegtables_length = v32;
TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
break;
case TIFFTAG_JPEGQUALITY:
sp->otherSettings.jpegquality = (int)va_arg(ap, int);
return (1);
case TIFFTAG_JPEGCOLORMODE:
sp->otherSettings.jpegcolormode = (int)va_arg(ap, int);
JPEGResetUpsampled(tif);
return (1);
case TIFFTAG_PHOTOMETRIC:
{
int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap);
JPEGResetUpsampled(tif);
return ret_value;
}
case TIFFTAG_JPEGTABLESMODE:
sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int);
return (1);
case TIFFTAG_YCBCRSUBSAMPLING:
sp->otherSettings.ycbcrsampling_fetched = 1;
return (*sp->otherSettings.vsetparent)(tif, tag, ap);
default:
return (*sp->otherSettings.vsetparent)(tif, tag, ap);
}
if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
{
TIFFSetFieldBit(tif, fip->field_bit);
}
else
{
return (0);
}
tif->tif_flags |= TIFF_DIRTYDIRECT;
return (1);
}
static int JPEGVGetField(TIFF *tif, uint32_t tag, va_list ap)
{
JPEGState *sp = JState(tif);
assert(sp != NULL);
switch (tag)
{
case TIFFTAG_JPEGTABLES:
*va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length;
*va_arg(ap, const void **) = sp->otherSettings.jpegtables;
break;
case TIFFTAG_JPEGQUALITY:
*va_arg(ap, int *) = sp->otherSettings.jpegquality;
break;
case TIFFTAG_JPEGCOLORMODE:
*va_arg(ap, int *) = sp->otherSettings.jpegcolormode;
break;
case TIFFTAG_JPEGTABLESMODE:
*va_arg(ap, int *) = sp->otherSettings.jpegtablesmode;
break;
default:
return (*sp->otherSettings.vgetparent)(tif, tag, ap);
}
return (1);
}
static void JPEGPrintDir(TIFF *tif, FILE *fd, long flags)
{
JPEGState *sp = JState(tif);
assert(sp != NULL);
(void)flags;
if (sp != NULL)
{
if (TIFFFieldSet(tif, FIELD_JPEGTABLES))
fprintf(fd, " JPEG Tables: (%" PRIu32 " bytes)\n",
sp->otherSettings.jpegtables_length);
if (sp->otherSettings.printdir)
(*sp->otherSettings.printdir)(tif, fd, flags);
}
}
static uint32_t JPEGDefaultStripSize(TIFF *tif, uint32_t s)
{
JPEGState *sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
s = (*sp->otherSettings.defsparent)(tif, s);
if (s < td->td_imagelength)
s = TIFFroundup_32(s, td->td_ycbcrsubsampling[1] * DCTSIZE);
return (s);
}
static void JPEGDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
{
JPEGState *sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
(*sp->otherSettings.deftparent)(tif, tw, th);
*tw = TIFFroundup_32(*tw, td->td_ycbcrsubsampling[0] * DCTSIZE);
*th = TIFFroundup_32(*th, td->td_ycbcrsubsampling[1] * DCTSIZE);
}
static int JPEGInitializeLibJPEG(TIFF *tif, int decompress)
{
JPEGState *sp = JState(tif);
if (sp->cinfo_initialized)
{
if (!decompress && sp->cinfo.comm.is_decompressor)
TIFFjpeg_destroy(sp);
else if (decompress && !sp->cinfo.comm.is_decompressor)
TIFFjpeg_destroy(sp);
else
return 1;
sp->cinfo_initialized = 0;
}
if (decompress)
{
if (!TIFFjpeg_create_decompress(sp))
return (0);
}
else
{
if (!TIFFjpeg_create_compress(sp))
return (0);
#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE
#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024)
#endif
if (sp->cinfo.c.mem->max_memory_to_use > 0)
{
if (getenv("JPEGMEM") == NULL)
{
if (sp->cinfo.c.mem->max_memory_to_use <
TIFF_JPEG_MAX_MEMORY_TO_USE)
sp->cinfo.c.mem->max_memory_to_use =
TIFF_JPEG_MAX_MEMORY_TO_USE;
}
}
}
sp->cinfo_initialized = TRUE;
return 1;
}
static void TIFFInitJPEGCommon(TIFF *tif)
{
JPEGState *sp;
sp = JState(tif);
sp->tif = tif;
sp->otherSettings.jpegtables = NULL;
sp->otherSettings.jpegtables_length = 0;
sp->otherSettings.jpegquality = 75;
sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW;
sp->otherSettings.jpegtablesmode =
JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF;
sp->otherSettings.ycbcrsampling_fetched = 0;
tif->tif_tagmethods.vgetfield = JPEGVGetField;
tif->tif_tagmethods.vsetfield = JPEGVSetField;
tif->tif_tagmethods.printdir = JPEGPrintDir;
tif->tif_fixuptags = JPEGFixupTags;
tif->tif_setupdecode = JPEGSetupDecode;
tif->tif_predecode = JPEGPreDecode;
tif->tif_decoderow = JPEGDecode;
tif->tif_decodestrip = JPEGDecode;
tif->tif_decodetile = JPEGDecode;
tif->tif_setupencode = JPEGSetupEncode;
tif->tif_preencode = JPEGPreEncode;
tif->tif_postencode = JPEGPostEncode;
tif->tif_encoderow = JPEGEncode;
tif->tif_encodestrip = JPEGEncode;
tif->tif_encodetile = JPEGEncode;
tif->tif_cleanup = JPEGCleanup;
tif->tif_defstripsize = JPEGDefaultStripSize;
tif->tif_deftilesize = JPEGDefaultTileSize;
tif->tif_flags |= TIFF_NOBITREV;
sp->cinfo_initialized = FALSE;
}
int TIFFInitJPEG(TIFF *tif, int scheme)
{
JPEGState *sp;
(void)scheme;
assert(scheme == COMPRESSION_JPEG);
if (!_TIFFMergeFields(tif, jpegFields, TIFFArrayCount(jpegFields)))
{
TIFFErrorExtR(tif, "TIFFInitJPEG",
"Merging JPEG codec-specific tags failed");
return 0;
}
tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(JPEGState));
if (tif->tif_data == NULL)
{
TIFFErrorExtR(tif, "TIFFInitJPEG", "No space for JPEG state block");
return 0;
}
_TIFFmemset(tif->tif_data, 0, sizeof(JPEGState));
sp = JState(tif);
sp->otherSettings.vgetparent = tif->tif_tagmethods.vgetfield;
sp->otherSettings.vsetparent = tif->tif_tagmethods.vsetfield;
sp->otherSettings.printdir = tif->tif_tagmethods.printdir;
sp->otherSettings.defsparent = tif->tif_defstripsize;
sp->otherSettings.deftparent = tif->tif_deftilesize;
TIFFInitJPEGCommon(tif);
if (tif->tif_diroff == 0)
{
#define SIZE_OF_JPEGTABLES 2000
sp->otherSettings.jpegtables_length = SIZE_OF_JPEGTABLES;
sp->otherSettings.jpegtables =
(void *)_TIFFmallocExt(tif, sp->otherSettings.jpegtables_length);
if (sp->otherSettings.jpegtables)
{
_TIFFmemset(sp->otherSettings.jpegtables, 0, SIZE_OF_JPEGTABLES);
}
else
{
TIFFErrorExtR(tif, "TIFFInitJPEG",
"Failed to allocate memory for JPEG tables");
return 0;
}
#undef SIZE_OF_JPEGTABLES
}
return 1;
}
#endif