#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include "tiffiop.h"
#include <stdlib.h>
#ifdef JPEG_SUPPORT
#include <setjmp.h>
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);
#ifdef FAR
#undef FAR
#endif
#if defined(__BORLANDC__) || defined(__MINGW32__)
#define XMD_H …
#endif
#if defined(__WIN32__) && !defined(__MINGW32__)
#ifndef __RPCNDR_H__
typedef unsigned char boolean;
#endif
#define HAVE_BOOLEAN …
#endif
#if defined(USE_SYSTEM_LIBJPEG)
#include <jerror.h>
#include <jpeglib.h>
#elif defined(USE_LIBJPEG_TURBO)
#include "third_party/libjpeg_turbo/jerror.h"
#include "third_party/libjpeg_turbo/jpeglib.h"
#else
#include "third_party/libjpeg/jerror.h"
#include "third_party/libjpeg/jpeglib.h"
#endif
#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 …
#define TIFF_JSAMPLE …
#define TIFF_JSAMPARRAY …
#define TIFF_JSAMPIMAGE …
#define TIFF_JSAMPROW …
#else
#define BITS_IN_JSAMPLE …
#define TIFF_JSAMPLE …
#define TIFF_JSAMPARRAY …
#define TIFF_JSAMPIMAGE …
#define TIFF_JSAMPROW …
#endif
#else
#define TIFF_JSAMPLE …
#define TIFF_JSAMPARRAY …
#define TIFF_JSAMPIMAGE …
#define TIFF_JSAMPROW …
#endif
#if defined(JPEG_LIB_MK1)
#define JPEG_LIB_MK1_OR_12BIT …
#elif BITS_IN_JSAMPLE == 12
#define JPEG_LIB_MK1_OR_12BIT …
#endif
#if defined(D_MAX_DATA_UNITS_IN_MCU)
#define width_in_blocks …
#endif
#define SETJMP(jbuf) …
#define LONGJMP(jbuf, code) …
#define JMP_BUF …
jpeg_destination_mgr;
jpeg_source_mgr;
jpeg_error_mgr;
JPEGState;
#define JState(tif) …
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 …
static const TIFFField jpegFields[] = …;
static void TIFFjpeg_error_exit(j_common_ptr cinfo)
{ … }
static void TIFFjpeg_output_message(j_common_ptr cinfo)
{ … }
static void TIFFjpeg_progress_monitor(j_common_ptr cinfo)
{ … }
#define CALLJPEG(sp, fail, op) …
#define CALLVJPEG(sp, op) …
static int TIFFjpeg_create_compress(JPEGState *sp)
{ … }
static int TIFFjpeg_create_decompress(JPEGState *sp)
{ … }
static int TIFFjpeg_set_defaults(JPEGState *sp)
{ … }
static int TIFFjpeg_set_colorspace(JPEGState *sp, J_COLOR_SPACE colorspace)
{ … }
static int TIFFjpeg_set_quality(JPEGState *sp, int quality,
boolean force_baseline)
{ … }
static int TIFFjpeg_suppress_tables(JPEGState *sp, boolean suppress)
{ … }
static int TIFFjpeg_start_compress(JPEGState *sp, boolean write_all_tables)
{ … }
static int TIFFjpeg_write_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines,
int num_lines)
{ … }
static int TIFFjpeg_write_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data,
int num_lines)
{ … }
static int TIFFjpeg_finish_compress(JPEGState *sp)
{ … }
static int TIFFjpeg_write_tables(JPEGState *sp)
{ … }
static int TIFFjpeg_read_header(JPEGState *sp, boolean require_image)
{ … }
static int TIFFjpeg_has_multiple_scans(JPEGState *sp)
{ … }
static int TIFFjpeg_start_decompress(JPEGState *sp)
{ … }
static int TIFFjpeg_read_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines,
int max_lines)
{ … }
static int TIFFjpeg_read_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data,
int max_lines)
{ … }
static int TIFFjpeg_finish_decompress(JPEGState *sp)
{ … }
static int TIFFjpeg_abort(JPEGState *sp)
{ … }
static int TIFFjpeg_destroy(JPEGState *sp)
{ … }
static JSAMPARRAY TIFFjpeg_alloc_sarray(JPEGState *sp, int pool_id,
JDIMENSION samplesperrow,
JDIMENSION numrows)
{ … }
static void std_init_destination(j_compress_ptr cinfo)
{ … }
static boolean std_empty_output_buffer(j_compress_ptr cinfo)
{ … }
static void std_term_destination(j_compress_ptr cinfo)
{ … }
static void TIFFjpeg_data_dest(JPEGState *sp, TIFF *tif)
{ … }
static void tables_init_destination(j_compress_ptr cinfo)
{ … }
static boolean tables_empty_output_buffer(j_compress_ptr cinfo)
{ … }
static void tables_term_destination(j_compress_ptr cinfo)
{ … }
static int TIFFjpeg_tables_dest(JPEGState *sp, TIFF *tif)
{ … }
static void std_init_source(j_decompress_ptr cinfo)
{ … }
static boolean std_fill_input_buffer(j_decompress_ptr cinfo)
{ … }
static void std_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
{ … }
static void std_term_source(j_decompress_ptr cinfo)
{ … }
static void TIFFjpeg_data_src(JPEGState *sp)
{ … }
static void tables_init_source(j_decompress_ptr cinfo)
{ … }
static void TIFFjpeg_tables_src(JPEGState *sp)
{ … }
static int alloc_downsampled_buffers(TIFF *tif, jpeg_component_info *comp_info,
int num_components)
{ … }
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
#define JPEG_MARKER_SOF0 …
#define JPEG_MARKER_SOF1 …
#define JPEG_MARKER_SOF2 …
#define JPEG_MARKER_SOF9 …
#define JPEG_MARKER_SOF10 …
#define JPEG_MARKER_DHT …
#define JPEG_MARKER_SOI …
#define JPEG_MARKER_SOS …
#define JPEG_MARKER_DQT …
#define JPEG_MARKER_DRI …
#define JPEG_MARKER_APP0 …
#define JPEG_MARKER_COM …
struct JPEGFixupTagsSubsamplingData
{ … };
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
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 JPEGSetupDecode(TIFF *tif)
{ … }
int TIFFJPEGIsFullStripRequired(TIFF *tif)
{ … }
static int JPEGPreDecode(TIFF *tif, uint16_t s)
{ … }
#if !JPEG_LIB_MK1_OR_12BIT
static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
{ … }
#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)
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)
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)
{ … }
static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc,
uint16_t s)
{ … }
static void unsuppress_quant_table(JPEGState *sp, int tblno)
{ … }
static void suppress_quant_table(JPEGState *sp, int tblno)
{ … }
static void unsuppress_huff_table(JPEGState *sp, int tblno)
{ … }
static void suppress_huff_table(JPEGState *sp, int tblno)
{ … }
static int prepare_JPEGTables(TIFF *tif)
{ … }
#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)
{ … }
static int JPEGPreEncode(TIFF *tif, 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 JPEGPostEncode(TIFF *tif)
{ … }
static void JPEGCleanup(TIFF *tif)
{ … }
static void JPEGResetUpsampled(TIFF *tif)
{ … }
static int JPEGVSetField(TIFF *tif, uint32_t tag, va_list ap)
{ … }
static int JPEGVGetField(TIFF *tif, uint32_t tag, va_list ap)
{ … }
static void JPEGPrintDir(TIFF *tif, FILE *fd, long flags)
{ … }
static uint32_t JPEGDefaultStripSize(TIFF *tif, uint32_t s)
{ … }
static void JPEGDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
{ … }
static int JPEGInitializeLibJPEG(TIFF *tif, int decompress)
{ … }
static void TIFFInitJPEGCommon(TIFF *tif)
{ … }
int TIFFInitJPEG(TIFF *tif, int scheme)
{ … }
#endif