25#define WIN32_LEAN_AND_MEAN
49int TIFFReInitJPEG_12(
TIFF *tif,
int scheme,
int is_encode );
50int TIFFJPEGIsFullStripRequired_12(
TIFF* tif);
64#if defined(__BORLANDC__) || defined(__MINGW32__)
82#if defined(__WIN32__) && !defined(__MINGW32__)
97#if defined(JPEG_LIB_MK1)
98# define JPEG_LIB_MK1_OR_12BIT 1
99#elif BITS_IN_JSAMPLE == 12
100# define JPEG_LIB_MK1_OR_12BIT 1
110#if defined(D_MAX_DATA_UNITS_IN_MCU)
111#define width_in_blocks width_in_data_units
118#define SETJMP(jbuf) setjmp(jbuf)
119#define LONGJMP(jbuf,code) longjmp(jbuf,code)
120#define JMP_BUF jmp_buf
145 int cinfo_initialized;
180 int ycbcrsampling_fetched;
181 int max_allowed_scan_number;
184#define JState(tif) ((JPEGState*)(tif)->tif_data)
190static int JPEGInitializeLibJPEG(
TIFF * tif,
int decode );
193#define FIELD_JPEGTABLES (FIELD_CODEC+0)
196 {
TIFFTAG_JPEGTABLES, -3, -3,
TIFF_UNDEFINED, 0,
TIFF_SETGET_C32_UINT8,
TIFF_SETGET_C32_UINT8, FIELD_JPEGTABLES,
FALSE,
TRUE,
"JPEGTables",
NULL },
197 {
TIFFTAG_JPEGQUALITY, 0, 0,
TIFF_ANY, 0,
TIFF_SETGET_INT,
TIFF_SETGET_UNDEFINED,
FIELD_PSEUDO,
TRUE,
FALSE,
"",
NULL },
198 {
TIFFTAG_JPEGCOLORMODE, 0, 0,
TIFF_ANY, 0,
TIFF_SETGET_INT,
TIFF_SETGET_UNDEFINED,
FIELD_PSEUDO,
FALSE,
FALSE,
"",
NULL },
199 {
TIFFTAG_JPEGTABLESMODE, 0, 0,
TIFF_ANY, 0,
TIFF_SETGET_INT,
TIFF_SETGET_UNDEFINED,
FIELD_PSEUDO,
FALSE,
FALSE,
"",
NULL }
219 JPEGState *
sp = (JPEGState *) cinfo;
222 (*cinfo->err->format_message) (cinfo,
buffer);
225 LONGJMP(
sp->exit_jmpbuf, 1);
238 (*cinfo->err->format_message) (cinfo,
buffer);
248 JPEGState *
sp = (JPEGState *) cinfo;
249 if (cinfo->is_decompressor)
253 if (scan_no >=
sp->max_allowed_scan_number)
255 TIFFErrorExt(((JPEGState *) cinfo)->tif->tif_clientdata,
256 "TIFFjpeg_progress_monitor",
257 "Scan number %d exceeds maximum scans (%d). This limit "
258 "can be raised through the LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER "
259 "environment variable.",
260 scan_no,
sp->max_allowed_scan_number);
263 LONGJMP(
sp->exit_jmpbuf, 1);
275#define CALLJPEG(sp, fail, op) (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op))
276#define CALLVJPEG(sp, op) CALLJPEG(sp, 0, ((op),1))
279TIFFjpeg_create_compress(JPEGState*
sp)
283 sp->err.error_exit = TIFFjpeg_error_exit;
284 sp->err.output_message = TIFFjpeg_output_message;
287 sp->cinfo.c.client_data =
NULL;
293TIFFjpeg_create_decompress(JPEGState*
sp)
297 sp->err.error_exit = TIFFjpeg_error_exit;
298 sp->err.output_message = TIFFjpeg_output_message;
301 sp->cinfo.d.client_data =
NULL;
307TIFFjpeg_set_defaults(JPEGState*
sp)
326TIFFjpeg_suppress_tables(JPEGState*
sp,
boolean suppress)
353TIFFjpeg_finish_compress(JPEGState*
sp)
359TIFFjpeg_write_tables(JPEGState*
sp)
371TIFFjpeg_has_multiple_scans(JPEGState*
sp)
377TIFFjpeg_start_decompress(JPEGState*
sp)
379 const char* sz_max_allowed_scan_number;
381 sp->cinfo.d.progress = &
sp->progress;
382 sp->progress.progress_monitor = TIFFjpeg_progress_monitor;
383 sp->max_allowed_scan_number = 100;
384 sz_max_allowed_scan_number =
getenv(
"LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER");
385 if( sz_max_allowed_scan_number )
386 sp->max_allowed_scan_number =
atoi(sz_max_allowed_scan_number);
388 return CALLVJPEG(
sp, jpeg_start_decompress(&
sp->cinfo.d));
406TIFFjpeg_finish_decompress(JPEGState*
sp)
412TIFFjpeg_abort(JPEGState*
sp)
418TIFFjpeg_destroy(JPEGState*
sp)
424TIFFjpeg_alloc_sarray(JPEGState*
sp,
int pool_id,
428 (*
sp->cinfo.comm.mem->alloc_sarray)
429 (&
sp->cinfo.comm, pool_id, samplesperrow, numrows));
441 JPEGState*
sp = (JPEGState*) cinfo;
451 JPEGState*
sp = (JPEGState*) cinfo;
464 if (
sp->dest.free_in_buffer >= 0 ) {
479 JPEGState*
sp = (JPEGState*) cinfo;
489TIFFjpeg_data_dest(JPEGState*
sp,
TIFF* tif)
492 sp->cinfo.c.dest = &
sp->dest;
493 sp->dest.init_destination = std_init_destination;
494 sp->dest.empty_output_buffer = std_empty_output_buffer;
495 sp->dest.term_destination = std_term_destination;
505 JPEGState*
sp = (JPEGState*) cinfo;
508 sp->dest.next_output_byte = (
JOCTET*)
sp->jpegtables;
509 sp->dest.free_in_buffer = (
size_t)
sp->jpegtables_length;
515 JPEGState*
sp = (JPEGState*) cinfo;
522 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 100);
523 sp->dest.next_output_byte = (
JOCTET*) newbuf +
sp->jpegtables_length;
524 sp->dest.free_in_buffer = (
size_t) 1000;
525 sp->jpegtables = newbuf;
526 sp->jpegtables_length += 1000;
533 JPEGState*
sp = (JPEGState*) cinfo;
536 sp->jpegtables_length -= (
uint32)
sp->dest.free_in_buffer;
540TIFFjpeg_tables_dest(JPEGState*
sp,
TIFF* tif)
549 sp->jpegtables_length = 1000;
551 if (
sp->jpegtables ==
NULL) {
552 sp->jpegtables_length = 0;
553 TIFFErrorExt(
sp->tif->tif_clientdata,
"TIFFjpeg_tables_dest",
"No space for JPEGTables");
556 sp->cinfo.c.dest = &
sp->dest;
557 sp->dest.init_destination = tables_init_destination;
558 sp->dest.empty_output_buffer = tables_empty_output_buffer;
559 sp->dest.term_destination = tables_term_destination;
571 JPEGState*
sp = (JPEGState*) cinfo;
581 JPEGState*
sp = (JPEGState* ) cinfo;
594 if(
sp->src.bytes_in_buffer > 0 ) {
607 WARNMS(cinfo, JWRN_JPEG_EOF);
609 sp->src.next_input_byte = dummy_EOI;
610 sp->src.bytes_in_buffer = 2;
617 JPEGState*
sp = (JPEGState*) cinfo;
620 if ((
size_t)num_bytes >
sp->src.bytes_in_buffer) {
622 (
void) std_fill_input_buffer(cinfo);
624 sp->src.next_input_byte += (
size_t) num_bytes;
625 sp->src.bytes_in_buffer -= (
size_t) num_bytes;
638TIFFjpeg_data_src(JPEGState*
sp)
640 sp->cinfo.d.src = &
sp->src;
641 sp->src.init_source = std_init_source;
642 sp->src.fill_input_buffer = std_fill_input_buffer;
643 sp->src.skip_input_data = std_skip_input_data;
645 sp->src.term_source = std_term_source;
646 sp->src.bytes_in_buffer = 0;
647 sp->src.next_input_byte =
NULL;
658 JPEGState*
sp = (JPEGState*) cinfo;
660 sp->src.next_input_byte = (
const JOCTET*)
sp->jpegtables;
661 sp->src.bytes_in_buffer = (
size_t)
sp->jpegtables_length;
665TIFFjpeg_tables_src(JPEGState*
sp)
667 TIFFjpeg_data_src(
sp);
668 sp->src.init_source = tables_init_source;
682 JPEGState*
sp = JState(tif);
686 int samples_per_clump = 0;
688 for (ci = 0,
compptr = comp_info; ci < num_components;
697 sp->ds_buffer[ci] =
buf;
699 sp->samplesperclump = samples_per_clump;
708#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
710#define JPEG_MARKER_SOF0 0xC0
711#define JPEG_MARKER_SOF1 0xC1
712#define JPEG_MARKER_SOF2 0xC2
713#define JPEG_MARKER_SOF9 0xC9
714#define JPEG_MARKER_SOF10 0xCA
715#define JPEG_MARKER_DHT 0xC4
716#define JPEG_MARKER_SOI 0xD8
717#define JPEG_MARKER_SOS 0xDA
718#define JPEG_MARKER_DQT 0xDB
719#define JPEG_MARKER_DRI 0xDD
720#define JPEG_MARKER_APP0 0xE0
721#define JPEG_MARKER_COM 0xFE
722struct JPEGFixupTagsSubsamplingData
727 uint8* buffercurrentbyte;
731 uint8 filepositioned;
733static void JPEGFixupTagsSubsampling(
TIFF* tif);
734static int JPEGFixupTagsSubsamplingSec(
struct JPEGFixupTagsSubsamplingData*
data);
735static int JPEGFixupTagsSubsamplingReadByte(
struct JPEGFixupTagsSubsamplingData*
data,
uint8*
result);
736static int JPEGFixupTagsSubsamplingReadWord(
struct JPEGFixupTagsSubsamplingData*
data,
uint16*
result);
737static void JPEGFixupTagsSubsamplingSkip(
struct JPEGFixupTagsSubsamplingData*
data,
uint16 skiplength);
742JPEGFixupTags(
TIFF* tif)
744#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
745 JPEGState*
sp = JState(tif);
749 !
sp->ycbcrsampling_fetched)
750 JPEGFixupTagsSubsampling(tif);
756#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
759JPEGFixupTagsSubsampling(
TIFF* tif)
781 static const char module[] =
"JPEGFixupTagsSubsampling";
782 struct JPEGFixupTagsSubsamplingData
m;
785 if( fileoffset == 0 )
799 "Unable to allocate memory for auto-correcting of subsampling values; auto-correcting skipped");
802 m.buffercurrentbyte=
NULL;
804 m.fileoffset=fileoffset;
807 if (!JPEGFixupTagsSubsamplingSec(&
m))
809 "Unable to auto-correct subsampling values, likely corrupt JPEG compressed data in first strip/tile; auto-correcting skipped");
814JPEGFixupTagsSubsamplingSec(
struct JPEGFixupTagsSubsamplingData*
data)
816 static const char module[] =
"JPEGFixupTagsSubsamplingSec";
822 if (!JPEGFixupTagsSubsamplingReadByte(
data,&
m))
829 if (!JPEGFixupTagsSubsamplingReadByte(
data,&
m))
836 case JPEG_MARKER_SOI:
839 case JPEG_MARKER_COM:
840 case JPEG_MARKER_APP0:
841 case JPEG_MARKER_APP0+1:
842 case JPEG_MARKER_APP0+2:
843 case JPEG_MARKER_APP0+3:
844 case JPEG_MARKER_APP0+4:
845 case JPEG_MARKER_APP0+5:
846 case JPEG_MARKER_APP0+6:
847 case JPEG_MARKER_APP0+7:
848 case JPEG_MARKER_APP0+8:
849 case JPEG_MARKER_APP0+9:
850 case JPEG_MARKER_APP0+10:
851 case JPEG_MARKER_APP0+11:
852 case JPEG_MARKER_APP0+12:
853 case JPEG_MARKER_APP0+13:
854 case JPEG_MARKER_APP0+14:
855 case JPEG_MARKER_APP0+15:
856 case JPEG_MARKER_DQT:
857 case JPEG_MARKER_SOS:
858 case JPEG_MARKER_DHT:
859 case JPEG_MARKER_DRI:
863 if (!JPEGFixupTagsSubsamplingReadWord(
data,&
n))
869 JPEGFixupTagsSubsamplingSkip(
data,
n);
872 case JPEG_MARKER_SOF0:
873 case JPEG_MARKER_SOF1:
874 case JPEG_MARKER_SOF2:
875 case JPEG_MARKER_SOF9:
876 case JPEG_MARKER_SOF10:
883 if (!JPEGFixupTagsSubsamplingReadWord(
data,&
n))
885 if (
n!=8+
data->tif->tif_dir.td_samplesperpixel*3)
887 JPEGFixupTagsSubsamplingSkip(
data,7);
888 if (!JPEGFixupTagsSubsamplingReadByte(
data,&
p))
892 JPEGFixupTagsSubsamplingSkip(
data,1);
893 for (o=1; o<
data->tif->tif_dir.td_samplesperpixel; o++)
895 JPEGFixupTagsSubsamplingSkip(
data,1);
896 if (!JPEGFixupTagsSubsamplingReadByte(
data,&
p))
901 "Subsampling values inside JPEG compressed data have no TIFF equivalent, auto-correction of TIFF subsampling values failed");
904 JPEGFixupTagsSubsamplingSkip(
data,1);
906 if (((ph!=1)&&(ph!=2)&&(ph!=4))||((pv!=1)&&(pv!=2)&&(pv!=4)))
909 "Subsampling values inside JPEG compressed data have no TIFF equivalent, auto-correction of TIFF subsampling values failed");
912 if ((ph!=
data->tif->tif_dir.td_ycbcrsubsampling[0])||(pv!=
data->tif->tif_dir.td_ycbcrsubsampling[1]))
915 "Auto-corrected former TIFF subsampling values [%d,%d] to match subsampling values inside JPEG compressed data [%d,%d]",
916 (
int)
data->tif->tif_dir.td_ycbcrsubsampling[0],
917 (
int)
data->tif->tif_dir.td_ycbcrsubsampling[1],
919 data->tif->tif_dir.td_ycbcrsubsampling[0]=ph;
920 data->tif->tif_dir.td_ycbcrsubsampling[1]=pv;
931JPEGFixupTagsSubsamplingReadByte(
struct JPEGFixupTagsSubsamplingData*
data,
uint8*
result)
933 if (
data->bufferbytesleft==0)
936 if (
data->filebytesleft==0)
938 if (!
data->filepositioned)
941 data->filepositioned=1;
949 data->buffercurrentbyte=
data->buffer;
950 data->bufferbytesleft=
m;
952 data->filebytesleft-=
m;
955 data->buffercurrentbyte++;
956 data->bufferbytesleft--;
961JPEGFixupTagsSubsamplingReadWord(
struct JPEGFixupTagsSubsamplingData*
data,
uint16*
result)
965 if (!JPEGFixupTagsSubsamplingReadByte(
data,&ma))
967 if (!JPEGFixupTagsSubsamplingReadByte(
data,&mb))
974JPEGFixupTagsSubsamplingSkip(
struct JPEGFixupTagsSubsamplingData*
data,
uint16 skiplength)
976 if ((
uint32)skiplength<=data->bufferbytesleft)
978 data->buffercurrentbyte+=skiplength;
979 data->bufferbytesleft-=skiplength;
987 data->bufferbytesleft=0;
989 data->filebytesleft-=
m;
990 data->filepositioned=0;
994 data->bufferbytesleft=0;
995 data->filebytesleft=0;
1004JPEGSetupDecode(
TIFF* tif)
1006 JPEGState*
sp = JState(tif);
1009#if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFInitJPEG)
1014 JPEGInitializeLibJPEG( tif,
TRUE );
1017 assert(
sp->cinfo.comm.is_decompressor);
1021 TIFFjpeg_tables_src(
sp);
1030 switch (
sp->photometric) {
1043 TIFFjpeg_data_src(
sp);
1055int TIFFJPEGIsFullStripRequired(
TIFF* tif)
1060#if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFJPEGIsFullStripRequired)
1062 return TIFFJPEGIsFullStripRequired_12( tif );
1068 TIFFjpeg_create_decompress(&
state);
1070 TIFFjpeg_data_src(&
state);
1074 TIFFjpeg_destroy(&
state);
1077 ret = TIFFjpeg_has_multiple_scans(&
state);
1079 TIFFjpeg_destroy(&
state);
1090 JPEGState *
sp = JState(tif);
1092 static const char module[] =
"JPEGPreDecode";
1093 uint32 segment_width, segment_height;
1094 int downsampled_output;
1099 if (
sp->cinfo.comm.is_decompressor == 0)
1104 assert(
sp->cinfo.comm.is_decompressor);
1109 if (!TIFFjpeg_abort(
sp))
1143 if (
sp->cinfo.d.image_width < segment_width ||
1144 sp->cinfo.d.image_height < segment_height) {
1146 "Improper JPEG strip/tile size, "
1147 "expected %dx%d, got %dx%d",
1148 segment_width, segment_height,
1149 sp->cinfo.d.image_width,
1150 sp->cinfo.d.image_height);
1152 if(
sp->cinfo.d.image_width == segment_width &&
1153 sp->cinfo.d.image_height > segment_height &&
1161 "JPEG strip size exceeds expected dimensions,"
1162 " expected %dx%d, got %dx%d",
1163 segment_width, segment_height,
1164 sp->cinfo.d.image_width,
sp->cinfo.d.image_height);
1166 else if (
sp->cinfo.d.image_width > segment_width ||
1167 sp->cinfo.d.image_height > segment_height) {
1175 "JPEG strip/tile size exceeds expected dimensions,"
1176 " expected %dx%d, got %dx%d",
1177 segment_width, segment_height,
1178 sp->cinfo.d.image_width,
sp->cinfo.d.image_height);
1181 if (
sp->cinfo.d.num_components !=
1203 if( TIFFjpeg_has_multiple_scans(
sp) )
1210 sp->cinfo.d.image_height *
1211 sp->cinfo.d.num_components *
1215 if(
sp->cinfo.d.progressive_mode )
1216 nRequiredMemory *= 3;
1218#ifndef TIFF_LIBJPEG_LARGEST_MEM_ALLOC
1219#define TIFF_LIBJPEG_LARGEST_MEM_ALLOC (100 * 1024 * 1024)
1222 if( nRequiredMemory > TIFF_LIBJPEG_LARGEST_MEM_ALLOC &&
1223 getenv(
"LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") ==
NULL )
1226 "Reading this strip would require libjpeg to allocate "
1227 "at least %u bytes. "
1228 "This is disabled since above the %u threshold. "
1229 "You may override this restriction by defining the "
1230 "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, "
1231 "or recompile libtiff by defining the "
1232 "TIFF_LIBJPEG_LARGEST_MEM_ALLOC macro to a value greater "
1234 (
unsigned)nRequiredMemory,
1235 (
unsigned)TIFF_LIBJPEG_LARGEST_MEM_ALLOC,
1236 (
unsigned)TIFF_LIBJPEG_LARGEST_MEM_ALLOC);
1243 if (
sp->cinfo.d.comp_info[0].h_samp_factor !=
sp->h_sampling ||
1244 sp->cinfo.d.comp_info[0].v_samp_factor !=
sp->v_sampling) {
1246 "Improper JPEG sampling factors %d,%d\n"
1247 "Apparently should be %d,%d.",
1248 sp->cinfo.d.comp_info[0].h_samp_factor,
1249 sp->cinfo.d.comp_info[0].v_samp_factor,
1250 sp->h_sampling,
sp->v_sampling);
1254 for (ci = 1; ci <
sp->cinfo.d.num_components; ci++) {
1255 if (
sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
1256 sp->cinfo.d.comp_info[ci].v_samp_factor != 1) {
1263 if (
sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
1264 sp->cinfo.d.comp_info[0].v_samp_factor != 1) {
1269 downsampled_output =
FALSE;
1281 (
sp->h_sampling != 1 ||
sp->v_sampling != 1))
1282 downsampled_output =
TRUE;
1285 if (downsampled_output) {
1287 sp->cinfo.d.raw_data_out =
TRUE;
1288#if JPEG_LIB_VERSION >= 70
1289 sp->cinfo.d.do_fancy_upsampling =
FALSE;
1296 sp->cinfo.d.raw_data_out =
FALSE;
1302 if (!TIFFjpeg_start_decompress(
sp))
1305 if (downsampled_output) {
1306 if (!alloc_downsampled_buffers(tif,
sp->cinfo.d.comp_info,
1307 sp->cinfo.d.num_components))
1318#if !JPEG_LIB_MK1_OR_12BIT
1322 JPEGState *
sp = JState(tif);
1331 sp->src.bytes_in_buffer = (
size_t) tif->
tif_rawcc;
1333 if(
sp->bytesperline == 0 )
1336 nrows =
cc /
sp->bytesperline;
1337 if (
cc %
sp->bytesperline)
1339 "fractional scanline not read");
1341 if( nrows > (
tmsize_t)
sp->cinfo.d.image_height )
1342 nrows =
sp->cinfo.d.image_height;
1355 if (TIFFjpeg_read_scanlines(
sp, &
bufptr, 1) != 1)
1359 buf +=
sp->bytesperline;
1360 cc -=
sp->bytesperline;
1361 }
while (--nrows > 0);
1369 return sp->cinfo.d.output_scanline <
sp->cinfo.d.output_height
1370 || TIFFjpeg_finish_decompress(
sp);
1374#if JPEG_LIB_MK1_OR_12BIT
1378 JPEGState *
sp = JState(tif);
1387 sp->src.bytes_in_buffer = (
size_t) tif->
tif_rawcc;
1389 if(
sp->bytesperline == 0 )
1392 nrows =
cc /
sp->bytesperline;
1393 if (
cc %
sp->bytesperline)
1395 "fractional scanline not read");
1397 if( nrows > (
tmsize_t)
sp->cinfo.d.image_height )
1398 nrows =
sp->cinfo.d.image_height;
1409 if(
sp->cinfo.d.data_precision == 12 )
1413 *
sp->cinfo.d.num_components );
1418 if( line_work_buf !=
NULL )
1426 if (TIFFjpeg_read_scanlines(
sp, &line_work_buf, 1) != 1)
1429 if(
sp->cinfo.d.data_precision == 12 )
1431 int value_pairs = (
sp->cinfo.d.output_width
1432 *
sp->cinfo.d.num_components) / 2;
1435 for( iPair = 0; iPair < value_pairs; iPair++ )
1437 unsigned char *out_ptr =
1438 ((
unsigned char *)
buf) + iPair * 3;
1439 JSAMPLE *in_ptr = line_work_buf + iPair * 2;
1441 out_ptr[0] = (
unsigned char)((in_ptr[0] & 0xff0) >> 4);
1442 out_ptr[1] = (
unsigned char)(((in_ptr[0] & 0xf) << 4)
1443 | ((in_ptr[1] & 0xf00) >> 8));
1444 out_ptr[2] = (
unsigned char)(((in_ptr[1] & 0xff) >> 0));
1447 else if(
sp->cinfo.d.data_precision == 8 )
1449 int value_count = (
sp->cinfo.d.output_width
1450 *
sp->cinfo.d.num_components);
1453 for( iValue = 0; iValue < value_count; iValue++ )
1455 ((
unsigned char *)
buf)[iValue] =
1456 line_work_buf[iValue] & 0xff;
1462 buf +=
sp->bytesperline;
1463 cc -=
sp->bytesperline;
1464 }
while (--nrows > 0);
1466 if( line_work_buf !=
NULL )
1475 return sp->cinfo.d.output_scanline <
sp->cinfo.d.output_height
1476 || TIFFjpeg_finish_decompress(
sp);
1489 "scanline oriented access is not supported for downsampled JPEG compressed images, consider enabling TIFF_JPEGCOLORMODE as JPEGCOLORMODE_RGB." );
1500 JPEGState *
sp = JState(tif);
1505 nrows =
sp->cinfo.d.image_height;
1516 JDIMENSION clumps_per_line =
sp->cinfo.d.comp_info[1].downsampled_width;
1517 int samples_per_clump =
sp->samplesperclump;
1519#if defined(JPEG_LIB_MK1_OR_12BIT)
1520 unsigned short* tmpbuf =
_TIFFmalloc(
sizeof(
unsigned short) *
1521 sp->cinfo.d.output_width *
1522 sp->cinfo.d.num_components);
1532 int ci, clumpoffset;
1534 if( cc < sp->bytesperline ) {
1536 "application buffer not large enough for all data.");
1542 int n =
sp->cinfo.d.max_v_samp_factor *
DCTSIZE;
1543 if (TIFFjpeg_read_raw_data(
sp,
sp->ds_buffer,
n) !=
n)
1552 for (ci = 0,
compptr =
sp->cinfo.d.comp_info;
1559 for (ypos = 0; ypos < vsamp; ypos++) {
1560 JSAMPLE *inptr =
sp->ds_buffer[ci][
sp->scancount*vsamp + ypos];
1562#if defined(JPEG_LIB_MK1_OR_12BIT)
1566 if (
cc < (
tmsize_t)(clumpoffset + (
tmsize_t)samples_per_clump*(clumps_per_line-1) + hsamp)) {
1568 "application buffer not large enough for all data, possible subsampling issue");
1575 for (nclump = clumps_per_line; nclump-- > 0; ) {
1576 outptr[0] = *inptr++;
1577 outptr += samples_per_clump;
1583 for (nclump = clumps_per_line; nclump-- > 0; ) {
1584 for (xpos = 0; xpos < hsamp; xpos++)
1585 outptr[xpos] = *inptr++;
1586 outptr += samples_per_clump;
1589 clumpoffset += hsamp;
1593#if defined(JPEG_LIB_MK1_OR_12BIT)
1595 if (
sp->cinfo.d.data_precision == 8)
1598 int len =
sp->cinfo.d.output_width *
sp->cinfo.d.num_components;
1601 ((
unsigned char*)
buf)[
i] = tmpbuf[
i] & 0xff;
1606 int value_pairs = (
sp->cinfo.d.output_width
1607 *
sp->cinfo.d.num_components) / 2;
1609 for( iPair = 0; iPair < value_pairs; iPair++ )
1611 unsigned char *out_ptr = ((
unsigned char *)
buf) + iPair * 3;
1613 out_ptr[0] = (
unsigned char)((in_ptr[0] & 0xff0) >> 4);
1614 out_ptr[1] = (
unsigned char)(((in_ptr[0] & 0xf) << 4)
1615 | ((in_ptr[1] & 0xf00) >> 8));
1616 out_ptr[2] = (
unsigned char)(((in_ptr[1] & 0xff) >> 0));
1625 buf +=
sp->bytesperline;
1626 cc -=
sp->bytesperline;
1628 nrows -=
sp->v_sampling;
1629 }
while (nrows > 0);
1631#if defined(JPEG_LIB_MK1_OR_12BIT)
1638 return sp->cinfo.d.output_scanline <
sp->cinfo.d.output_height
1639 || TIFFjpeg_finish_decompress(
sp);
1648unsuppress_quant_table (JPEGState*
sp,
int tblno)
1652 if ((qtbl =
sp->cinfo.c.quant_tbl_ptrs[
tblno]) !=
NULL)
1657suppress_quant_table (JPEGState*
sp,
int tblno)
1661 if ((qtbl =
sp->cinfo.c.quant_tbl_ptrs[
tblno]) !=
NULL)
1666unsuppress_huff_table (JPEGState*
sp,
int tblno)
1670 if ((htbl =
sp->cinfo.c.dc_huff_tbl_ptrs[
tblno]) !=
NULL)
1672 if ((htbl =
sp->cinfo.c.ac_huff_tbl_ptrs[
tblno]) !=
NULL)
1677suppress_huff_table (JPEGState*
sp,
int tblno)
1681 if ((htbl =
sp->cinfo.c.dc_huff_tbl_ptrs[
tblno]) !=
NULL)
1683 if ((htbl =
sp->cinfo.c.ac_huff_tbl_ptrs[
tblno]) !=
NULL)
1688prepare_JPEGTables(
TIFF* tif)
1690 JPEGState*
sp = JState(tif);
1693 if (!TIFFjpeg_set_quality(
sp,
sp->jpegquality,
FALSE))
1697 if (!TIFFjpeg_suppress_tables(
sp,
TRUE))
1700 unsuppress_quant_table(
sp, 0);
1702 unsuppress_quant_table(
sp, 1);
1705 unsuppress_huff_table(
sp, 0);
1707 unsuppress_huff_table(
sp, 1);
1710 if (!TIFFjpeg_tables_dest(
sp, tif))
1713 if (!TIFFjpeg_write_tables(
sp))
1720JPEGSetupEncode(
TIFF* tif)
1722 JPEGState*
sp = JState(tif);
1724 static const char module[] =
"JPEGSetupEncode";
1726#if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFInitJPEG)
1731 JPEGInitializeLibJPEG( tif,
FALSE );
1734 assert(!
sp->cinfo.comm.is_decompressor);
1762 sp->cinfo.c.input_components = 1;
1765 if (!TIFFjpeg_set_defaults(
sp))
1768 switch (
sp->photometric) {
1772 if(
sp->h_sampling == 0 ||
sp->v_sampling == 0 )
1775 "Invalig horizontal/vertical sampling value");
1781 "BitsPerSample %d not allowed for JPEG",
1800 refbw[3] = refbw[1];
1801 refbw[4] = refbw[2];
1802 refbw[5] = refbw[1];
1811 "PhotometricInterpretation %d not allowed for JPEG",
1812 (
int)
sp->photometric);
1846 "JPEG tile height must be multiple of %d",
1852 "JPEG tile width must be multiple of %d",
1860 "RowsPerStrip must be multiple of %d for JPEG",
1868 if(
sp->jpegtables ==
NULL
1869 ||
memcmp(
sp->jpegtables,
"\0\0\0\0\0\0\0\0\0",8) == 0 )
1871 if (!prepare_JPEGTables(tif))
1885 TIFFjpeg_data_dest(
sp, tif);
1896 JPEGState *
sp = JState(tif);
1898 static const char module[] =
"JPEGPreEncode";
1899 uint32 segment_width, segment_height;
1900 int downsampled_input;
1904 if (
sp->cinfo.comm.is_decompressor == 1)
1909 assert(!
sp->cinfo.comm.is_decompressor);
1931 if (segment_width > 65535 || segment_height > 65535) {
1935 sp->cinfo.c.image_width = segment_width;
1936 sp->cinfo.c.image_height = segment_height;
1937 downsampled_input =
FALSE;
1942 if (
sp->h_sampling != 1 ||
sp->v_sampling != 1)
1943 downsampled_input =
TRUE;
1951 sp->cinfo.c.comp_info[0].h_samp_factor =
sp->h_sampling;
1952 sp->cinfo.c.comp_info[0].v_samp_factor =
sp->v_sampling;
1954 if (!TIFFjpeg_set_colorspace(
sp,
sp->cinfo.c.in_color_space))
1961 sp->cinfo.c.comp_info[0].component_id =
s;
1964 sp->cinfo.c.comp_info[0].quant_tbl_no = 1;
1965 sp->cinfo.c.comp_info[0].dc_tbl_no = 1;
1966 sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
1970 sp->cinfo.c.write_JFIF_header =
FALSE;
1971 sp->cinfo.c.write_Adobe_marker =
FALSE;
1978 if (!TIFFjpeg_set_quality(
sp,
sp->jpegquality,
FALSE))
1981 suppress_quant_table(
sp, 0);
1982 suppress_quant_table(
sp, 1);
1985 unsuppress_quant_table(
sp, 0);
1986 unsuppress_quant_table(
sp, 1);
1993 suppress_huff_table(
sp, 0);
1994 suppress_huff_table(
sp, 1);
1995 sp->cinfo.c.optimize_coding =
FALSE;
1998 sp->cinfo.c.optimize_coding =
TRUE;
1999 if (downsampled_input) {
2001 sp->cinfo.c.raw_data_in =
TRUE;
2007 sp->cinfo.c.raw_data_in =
FALSE;
2013 if (!TIFFjpeg_start_compress(
sp,
FALSE))
2016 if (downsampled_input) {
2017 if (!alloc_downsampled_buffers(tif,
sp->cinfo.c.comp_info,
2018 sp->cinfo.c.num_components))
2033 JPEGState *
sp = JState(tif);
2036 short *line16 =
NULL;
2037 int line16_count = 0;
2042 nrows =
cc /
sp->bytesperline;
2043 if (
cc %
sp->bytesperline)
2045 "fractional scanline discarded");
2051 if(
sp->cinfo.c.data_precision == 12 )
2053 line16_count = (
int)((
sp->bytesperline * 2) / 3);
2054 line16 = (
short *)
_TIFFmalloc(
sizeof(
short) * line16_count);
2059 "Failed to allocate memory");
2065 while (nrows-- > 0) {
2067 if(
sp->cinfo.c.data_precision == 12 )
2070 int value_pairs = line16_count / 2;
2075 for( iPair = 0; iPair < value_pairs; iPair++ )
2077 unsigned char *in_ptr =
2078 ((
unsigned char *)
buf) + iPair * 3;
2081 out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4);
2082 out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2];
2089 if (TIFFjpeg_write_scanlines(
sp,
bufptr, 1) != 1)
2093 buf +=
sp->bytesperline;
2096 if(
sp->cinfo.c.data_precision == 12 )
2111 JPEGState *
sp = JState(tif);
2116 int clumpoffset, ci, xpos, ypos;
2118 int samples_per_clump =
sp->samplesperclump;
2126 bytesperclumpline = ((((
tmsize_t)
sp->cinfo.c.image_width+
sp->h_sampling-1)/
sp->h_sampling)
2127 *((
tmsize_t)
sp->h_sampling*
sp->v_sampling+2)*
sp->cinfo.c.data_precision+7)
2130 nrows = (
cc / bytesperclumpline ) *
sp->v_sampling;
2131 if (
cc % bytesperclumpline)
2135 clumps_per_line =
sp->cinfo.c.comp_info[1].downsampled_width;
2143 for (ci = 0,
compptr =
sp->cinfo.c.comp_info;
2149 clumps_per_line * hsamp);
2150 for (ypos = 0; ypos < vsamp; ypos++) {
2152 outptr =
sp->ds_buffer[ci][
sp->scancount*vsamp + ypos];
2155 for (nclump = clumps_per_line; nclump-- > 0; ) {
2156 *outptr++ = inptr[0];
2157 inptr += samples_per_clump;
2161 for (nclump = clumps_per_line; nclump-- > 0; ) {
2162 for (xpos = 0; xpos < hsamp; xpos++)
2163 *outptr++ = inptr[xpos];
2164 inptr += samples_per_clump;
2168 for (xpos = 0; xpos <
padding; xpos++) {
2169 *outptr = outptr[-1];
2172 clumpoffset += hsamp;
2177 int n =
sp->cinfo.c.max_v_samp_factor *
DCTSIZE;
2178 if (TIFFjpeg_write_raw_data(
sp,
sp->ds_buffer,
n) !=
n)
2183 buf += bytesperclumpline;
2184 nrows -=
sp->v_sampling;
2193JPEGPostEncode(
TIFF* tif)
2195 JPEGState *
sp = JState(tif);
2197 if (
sp->scancount > 0) {
2205 for (ci = 0,
compptr =
sp->cinfo.c.comp_info;
2211 for (ypos =
sp->scancount * vsamp;
2212 ypos <
DCTSIZE * vsamp; ypos++) {
2214 (
void*)
sp->ds_buffer[ci][ypos-1],
2220 if (TIFFjpeg_write_raw_data(
sp,
sp->ds_buffer,
n) !=
n)
2224 return (TIFFjpeg_finish_compress(JState(tif)));
2228JPEGCleanup(
TIFF* tif)
2230 JPEGState *
sp = JState(tif);
2237 if(
sp->cinfo_initialized )
2238 TIFFjpeg_destroy(
sp);
2248JPEGResetUpsampled(
TIFF* tif )
2250 JPEGState*
sp = JState(tif);
2285 JPEGState*
sp = JState(tif);
2299 sp->jpegtables_length = v32;
2307 JPEGResetUpsampled( tif );
2311 int ret_value = (*
sp->vsetparent)(tif,
tag,
ap);
2312 JPEGResetUpsampled( tif );
2320 sp->ycbcrsampling_fetched = 1;
2322 return (*
sp->vsetparent)(tif,
tag,
ap);
2324 return (*
sp->vsetparent)(tif,
tag,
ap);
2340 JPEGState*
sp = JState(tif);
2359 return (*
sp->vgetparent)(tif,
tag,
ap);
2367 JPEGState*
sp = JState(tif);
2374 fprintf(
fd,
" JPEG Tables: (%lu bytes)\n",
2375 (
unsigned long)
sp->jpegtables_length);
2384 JPEGState*
sp = JState(tif);
2387 s = (*
sp->defsparent)(tif,
s);
2388 if (s < td->td_imagelength)
2396 JPEGState*
sp = JState(tif);
2399 (*
sp->deftparent)(tif, tw, th);
2426static int JPEGInitializeLibJPEG(
TIFF * tif,
int decompress )
2428 JPEGState*
sp = JState(tif);
2430 if(
sp->cinfo_initialized)
2432 if( !decompress &&
sp->cinfo.comm.is_decompressor )
2433 TIFFjpeg_destroy(
sp );
2434 else if( decompress && !
sp->cinfo.comm.is_decompressor )
2435 TIFFjpeg_destroy(
sp );
2439 sp->cinfo_initialized = 0;
2446 if (!TIFFjpeg_create_decompress(
sp))
2449 if (!TIFFjpeg_create_compress(
sp))
2451#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE
2452#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024)
2457 if(
sp->cinfo.c.mem->max_memory_to_use > 0 )
2466 if(
sp->cinfo.c.mem->max_memory_to_use < TIFF_JPEG_MAX_MEMORY_TO_USE )
2467 sp->cinfo.c.mem->max_memory_to_use = TIFF_JPEG_MAX_MEMORY_TO_USE;
2472 sp->cinfo_initialized =
TRUE;
2490 "Merging JPEG codec-specific tags failed");
2501 "TIFFInitJPEG",
"No space for JPEG state block");
2521 sp->jpegtables_length = 0;
2522 sp->jpegquality = 75;
2525 sp->ycbcrsampling_fetched = 0;
2549 sp->cinfo_initialized =
FALSE;
2559#define SIZE_OF_JPEGTABLES 2000
2568 sp->jpegtables_length = SIZE_OF_JPEGTABLES;
2578 "Failed to allocate memory for JPEG tables");
2581#undef SIZE_OF_JPEGTABLES
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
static BYTE decode(char c)
char boolean force_baseline
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
GLdouble GLdouble GLdouble GLdouble top
GLenum GLuint GLenum GLsizei const GLchar * buf
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
_Check_return_ char *__cdecl getenv(_In_z_ const char *_VarName)
jpeg_finish_compress(j_compress_ptr cinfo)
jpeg_write_tables(j_compress_ptr cinfo)
jpeg_suppress_tables(j_compress_ptr cinfo, boolean suppress)
jpeg_write_raw_data(j_compress_ptr cinfo, JSAMPIMAGE data, JDIMENSION num_lines)
jpeg_write_scanlines(j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines)
jpeg_start_compress(j_compress_ptr cinfo, boolean write_all_tables)
jpeg_abort(j_common_ptr cinfo)
jpeg_destroy(j_common_ptr cinfo)
jpeg_set_colorspace(j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
jpeg_set_quality(j_compress_ptr cinfo, int quality, boolean force_baseline)
jpeg_set_defaults(j_compress_ptr cinfo)
jpeg_has_multiple_scans(j_decompress_ptr cinfo)
jpeg_finish_decompress(j_decompress_ptr cinfo)
jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines)
jpeg_read_raw_data(j_decompress_ptr cinfo, JSAMPIMAGE data, JDIMENSION max_lines)
jpeg_component_info * compptr
jpeg_resync_to_restart(j_decompress_ptr cinfo, int desired)
jpeg_std_error(struct jpeg_error_mgr *err)
#define ERREXIT1(cinfo, code, p1)
#define WARNMS(cinfo, code)
struct jpeg_decompress_struct * j_decompress_ptr
#define JPEG_HEADER_TABLES_ONLY
#define jpeg_create_decompress(cinfo)
#define jpeg_create_compress(cinfo)
JSAMPARRAY JDIMENSION num_lines
JSAMPARRAY JDIMENSION max_lines
static const DWORD padding[]
static float(__cdecl *square_half_float)(float x
uint16 td_samplesperpixel
uint16 td_ycbcrsubsampling[2]
JDIMENSION width_in_blocks
TIFFTileMethod tif_deftilesize
TIFFCodeMethod tif_encodestrip
TIFFCodeMethod tif_encodetile
TIFFTagMethods tif_tagmethods
tmsize_t tif_scanlinesize
TIFFPreMethod tif_preencode
TIFFBoolMethod tif_fixuptags
TIFFPreMethod tif_predecode
TIFFCodeMethod tif_decodestrip
TIFFPostMethod tif_postdecode
TIFFStripMethod tif_defstripsize
TIFFCodeMethod tif_decoderow
TIFFBoolMethod tif_setupencode
TIFFBoolMethod tif_postencode
TIFFCodeMethod tif_encoderow
TIFFVoidMethod tif_cleanup
TIFFBoolMethod tif_setupdecode
TIFFCodeMethod tif_decodetile
void _TIFFSetDefaultCompressionState(TIFF *tif)
int TIFFSetField(TIFF *tif, uint32 tag,...)
void _TIFFsetByteArray(void **vpp, void *vp, uint32 n)
int TIFFGetField(TIFF *tif, uint32 tag,...)
#define TIFFFieldSet(tif, field)
#define TIFFClrFieldBit(tif, field)
#define TIFFSetFieldBit(tif, field)
const TIFFField * TIFFFieldWithTag(TIFF *tif, uint32 tag)
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32 n)
uint64 TIFFGetStrileOffset(TIFF *tif, uint32 strile)
uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
void _TIFFNoPostDecode(TIFF *tif, uint8 *buf, tmsize_t cc)
int TIFFFillStrip(TIFF *tif, uint32 strip)
int TIFFFillTile(TIFF *tif, uint32 tile)
tmsize_t TIFFScanlineSize(TIFF *tif)
tmsize_t TIFFTileSize(TIFF *tif)
tmsize_t TIFFTileRowSize(TIFF *tif)
void _TIFFmemset(void *p, int v, tmsize_t c)
void * _TIFFmalloc(tmsize_t s)
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
void * _TIFFrealloc(void *p, tmsize_t s)
void TIFFWarningExt(thandle_t fd, const char *module, const char *fmt,...)
int TIFFFlushData1(TIFF *tif)
#define PLANARCONFIG_SEPARATE
#define JPEGTABLESMODE_HUFF
#define JPEGCOLORMODE_RAW
#define TIFFTAG_PHOTOMETRIC
#define PHOTOMETRIC_PALETTE
#define TIFFTAG_JPEGTABLESMODE
#define PHOTOMETRIC_SEPARATED
#define JPEGTABLESMODE_QUANT
#define PHOTOMETRIC_YCBCR
#define PHOTOMETRIC_MINISWHITE
#define TIFFTAG_JPEGQUALITY
#define TIFFTAG_YCBCRSUBSAMPLING
#define TIFFTAG_REFERENCEBLACKWHITE
#define PHOTOMETRIC_MINISBLACK
#define TIFFTAG_JPEGCOLORMODE
#define JPEGCOLORMODE_RGB
#define TIFFTAG_JPEGTABLES
#define PLANARCONFIG_CONTIG
void(* TIFFPrintMethod)(TIFF *, FILE *, long)
int(* TIFFVGetMethod)(TIFF *, uint32, va_list)
int(* TIFFVSetMethod)(TIFF *, uint32, va_list)
#define TIFFReadFile(tif, buf, size)
void(* TIFFTileMethod)(TIFF *, uint32 *, uint32 *)
#define TIFFroundup_32(x, y)
uint32(* TIFFStripMethod)(TIFF *, uint32)
#define TIFFSeekFile(tif, off, whence)
#define TIFFhowmany_32(x, y)
#define TIFFArrayCount(a)
static unsigned int bufptr
void int int ULONGLONG int va_list * ap