summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libtiff/t4.h2
-rw-r--r--src/libtiff/tif_aux.c18
-rw-r--r--src/libtiff/tif_close.c22
-rw-r--r--src/libtiff/tif_codec.c9
-rw-r--r--src/libtiff/tif_color.c2
-rw-r--r--src/libtiff/tif_compress.c24
-rw-r--r--src/libtiff/tif_dir.c229
-rw-r--r--src/libtiff/tif_dir.h77
-rw-r--r--src/libtiff/tif_dirinfo.c162
-rw-r--r--src/libtiff/tif_dirread.c858
-rw-r--r--src/libtiff/tif_dirwrite.c326
-rw-r--r--src/libtiff/tif_dumpmode.c6
-rw-r--r--src/libtiff/tif_error.c2
-rw-r--r--src/libtiff/tif_extension.c2
-rw-r--r--src/libtiff/tif_fax3.c129
-rw-r--r--src/libtiff/tif_fax3.h2
-rw-r--r--src/libtiff/tif_flush.c2
-rw-r--r--src/libtiff/tif_getimage.c1997
-rw-r--r--src/libtiff/tif_jpeg.c391
-rw-r--r--src/libtiff/tif_luv.c38
-rw-r--r--src/libtiff/tif_lzw.c48
-rw-r--r--src/libtiff/tif_next.c39
-rw-r--r--src/libtiff/tif_ojpeg.c4923
-rw-r--r--src/libtiff/tif_open.c19
-rw-r--r--src/libtiff/tif_packbits.c6
-rw-r--r--src/libtiff/tif_pixarlog.c34
-rw-r--r--src/libtiff/tif_predict.c219
-rw-r--r--src/libtiff/tif_predict.h16
-rw-r--r--src/libtiff/tif_print.c6
-rw-r--r--src/libtiff/tif_read.c375
-rw-r--r--src/libtiff/tif_strip.c85
-rw-r--r--src/libtiff/tif_swab.c2
-rw-r--r--src/libtiff/tif_thunder.c2
-rw-r--r--src/libtiff/tif_tile.c2
-rw-r--r--src/libtiff/tif_version.c2
-rw-r--r--src/libtiff/tif_warning.c2
-rw-r--r--src/libtiff/tif_write.c108
-rw-r--r--src/libtiff/tif_zip.c64
-rw-r--r--src/libtiff/tiff.h4
-rw-r--r--src/libtiff/tiffio.h92
-rw-r--r--src/libtiff/tiffiop.h49
-rw-r--r--src/libtiff/tiffvers.h4
42 files changed, 5623 insertions, 4776 deletions
diff --git a/src/libtiff/t4.h b/src/libtiff/t4.h
index b8f08fd..f7bfe1e 100644
--- a/src/libtiff/t4.h
+++ b/src/libtiff/t4.h
@@ -1,4 +1,4 @@
-/* $Id: t4.h,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: t4.h,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
diff --git a/src/libtiff/tif_aux.c b/src/libtiff/tif_aux.c
index d33a144..8aad4eb 100644
--- a/src/libtiff/tif_aux.c
+++ b/src/libtiff/tif_aux.c
@@ -1,4 +1,4 @@
-/* $Id: tif_aux.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_aux.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1991-1997 Sam Leffler
@@ -34,7 +34,8 @@
#include <math.h>
tdata_t
-_TIFFCheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
+_TIFFCheckRealloc(TIFF* tif, tdata_t buffer,
+ size_t nmemb, size_t elem_size, const char* what)
{
tdata_t cp = NULL;
tsize_t bytes = nmemb * elem_size;
@@ -43,12 +44,19 @@ _TIFFCheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
* XXX: Check for integer overflow.
*/
if (nmemb && elem_size && bytes / elem_size == nmemb)
- cp = _TIFFmalloc(bytes);
+ cp = _TIFFrealloc(buffer, bytes);
if (cp == NULL)
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space %s", what);
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "No space %s", what);
+
+ return cp;
+}
- return (cp);
+tdata_t
+_TIFFCheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
+{
+ return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
}
static int
diff --git a/src/libtiff/tif_close.c b/src/libtiff/tif_close.c
index 5047d37..2e8250e 100644
--- a/src/libtiff/tif_close.c
+++ b/src/libtiff/tif_close.c
@@ -1,4 +1,4 @@
-/* $Id: tif_close.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_close.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -54,27 +54,27 @@ TIFFCleanup(TIFF* tif)
TIFFFreeDirectory(tif);
if (tif->tif_dirlist)
- _TIFFfree(tif->tif_dirlist);
-
+ _TIFFfree(tif->tif_dirlist);
+
/* Clean up client info links */
while( tif->tif_clientinfo )
{
- TIFFClientInfoLink *link = tif->tif_clientinfo;
+ TIFFClientInfoLink *link = tif->tif_clientinfo;
- tif->tif_clientinfo = link->next;
- _TIFFfree( link->name );
- _TIFFfree( link );
+ tif->tif_clientinfo = link->next;
+ _TIFFfree( link->name );
+ _TIFFfree( link );
}
if (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))
- _TIFFfree(tif->tif_rawdata);
+ _TIFFfree(tif->tif_rawdata);
if (isMapped(tif))
- TIFFUnmapFileContents(tif, tif->tif_base, tif->tif_size);
+ TIFFUnmapFileContents(tif, tif->tif_base, tif->tif_size);
/* Clean up custom fields */
- if (tif->tif_nfields > 0)
+ if (tif->tif_nfields > 0)
{
- size_t i;
+ size_t i;
for (i = 0; i < tif->tif_nfields; i++)
{
diff --git a/src/libtiff/tif_codec.c b/src/libtiff/tif_codec.c
index 377e083..6f7099f 100644
--- a/src/libtiff/tif_codec.c
+++ b/src/libtiff/tif_codec.c
@@ -1,4 +1,4 @@
-/* $Id: tif_codec.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_codec.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -102,9 +102,12 @@ static int
_notConfigured(TIFF* tif)
{
const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
-
+ char compression_code[20];
+
+ sprintf( compression_code, "%d", tif->tif_dir.td_compression );
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "%s compression support is not configured", c->name);
+ "%s compression support is not configured",
+ c ? c->name : compression_code );
return (0);
}
diff --git a/src/libtiff/tif_color.c b/src/libtiff/tif_color.c
index 9e9481e..1f69d22 100644
--- a/src/libtiff/tif_color.c
+++ b/src/libtiff/tif_color.c
@@ -1,4 +1,4 @@
-/* $Id: tif_color.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_color.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
diff --git a/src/libtiff/tif_compress.c b/src/libtiff/tif_compress.c
index cbbb295..e99dc93 100644
--- a/src/libtiff/tif_compress.c
+++ b/src/libtiff/tif_compress.c
@@ -1,4 +1,4 @@
-/* $Id: tif_compress.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_compress.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -37,12 +37,13 @@ TIFFNoEncode(TIFF* tif, const char* method)
const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
if (c) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "%s %s encoding is not implemented",
- c->name, method);
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%s %s encoding is not implemented",
+ c->name, method);
} else {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "Compression scheme %u %s encoding is not implemented",
- tif->tif_dir.td_compression, method);
+ "Compression scheme %u %s encoding is not implemented",
+ tif->tif_dir.td_compression, method);
}
return (-1);
}
@@ -74,12 +75,13 @@ TIFFNoDecode(TIFF* tif, const char* method)
const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
if (c)
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "%s %s decoding is not implemented",
- c->name, method);
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%s %s decoding is not implemented",
+ c->name, method);
else
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "Compression scheme %u %s decoding is not implemented",
- tif->tif_dir.td_compression, method);
+ "Compression scheme %u %s decoding is not implemented",
+ tif->tif_dir.td_compression, method);
return (-1);
}
@@ -109,7 +111,7 @@ _TIFFNoSeek(TIFF* tif, uint32 off)
{
(void) off;
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "Compression algorithm does not support random access");
+ "Compression algorithm does not support random access");
return (0);
}
@@ -144,7 +146,7 @@ _TIFFSetDefaultCompressionState(TIFF* tif)
tif->tif_cleanup = _TIFFvoid;
tif->tif_defstripsize = _TIFFDefaultStripSize;
tif->tif_deftilesize = _TIFFDefaultTileSize;
- tif->tif_flags &= ~TIFF_NOBITREV;
+ tif->tif_flags &= ~(TIFF_NOBITREV|TIFF_NOREADRAW);
}
int
diff --git a/src/libtiff/tif_dir.c b/src/libtiff/tif_dir.c
index f2d1ee7..f9621ca 100644
--- a/src/libtiff/tif_dir.c
+++ b/src/libtiff/tif_dir.c
@@ -1,4 +1,4 @@
-/* $Id: tif_dir.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_dir.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -74,21 +74,37 @@ void _TIFFsetDoubleArray(double** dpp, double* dp, uint32 n)
static int
setExtraSamples(TIFFDirectory* td, va_list ap, uint32* v)
{
+/* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
+#define EXTRASAMPLE_COREL_UNASSALPHA 999
+
uint16* va;
uint32 i;
*v = va_arg(ap, uint32);
if ((uint16) *v > td->td_samplesperpixel)
- return (0);
+ return 0;
va = va_arg(ap, uint16*);
if (*v > 0 && va == NULL) /* typically missing param */
- return (0);
- for (i = 0; i < *v; i++)
- if (va[i] > EXTRASAMPLE_UNASSALPHA)
- return (0);
+ return 0;
+ for (i = 0; i < *v; i++) {
+ if (va[i] > EXTRASAMPLE_UNASSALPHA) {
+ /*
+ * XXX: Corel Draw is known to produce incorrect
+ * ExtraSamples tags which must be patched here if we
+ * want to be able to open some of the damaged TIFF
+ * files:
+ */
+ if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
+ va[i] = EXTRASAMPLE_UNASSALPHA;
+ else
+ return 0;
+ }
+ }
td->td_extrasamples = (uint16) *v;
_TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
- return (1);
+ return 1;
+
+#undef EXTRASAMPLE_COREL_UNASSALPHA
}
static uint32
@@ -121,7 +137,7 @@ static int
_TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
{
static const char module[] = "_TIFFVSetField";
-
+
TIFFDirectory* td = &tif->tif_dir;
int status = 1;
uint32 v32, i, v;
@@ -192,14 +208,11 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
goto badvalue;
td->td_fillorder = (uint16) v;
break;
- break;
case TIFFTAG_ORIENTATION:
v = va_arg(ap, uint32);
- if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v) {
- TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
- "Bad value %lu for \"%s\" tag ignored",
- v, _TIFFFieldWithTag(tif, tag)->field_name);
- } else
+ if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
+ goto badvalue;
+ else
td->td_orientation = (uint16) v;
break;
case TIFFTAG_SAMPLESPERPIXEL:
@@ -345,8 +358,9 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
_TIFFsetLongArray(&td->td_subifd, va_arg(ap, uint32*),
(long) td->td_nsubifd);
} else {
- TIFFErrorExt(tif->tif_clientdata, module, "%s: Sorry, cannot nest SubIFDs",
- tif->tif_name);
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Sorry, cannot nest SubIFDs",
+ tif->tif_name);
status = 0;
}
break;
@@ -374,9 +388,9 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
}
break;
default: {
- const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
TIFFTagValue *tv;
int tv_size, iCustom;
+ const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
/*
* This can happen if multiple images are open with different
@@ -389,9 +403,9 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
*/
if(fip == NULL || fip->field_bit != FIELD_CUSTOM) {
TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Invalid %stag \"%s\" (not supported by codec)",
- tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
- _TIFFFieldWithTag(tif, tag)->field_name);
+ "%s: Invalid %stag \"%s\" (not supported by codec)",
+ tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
+ fip ? fip->field_name : "Unknown");
status = 0;
break;
}
@@ -400,16 +414,15 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
* Find the existing entry for this custom value.
*/
tv = NULL;
- for(iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
- if(td->td_customValues[iCustom].info == fip) {
- tv = td->td_customValues + iCustom;
- if(tv->value != NULL)
- {
- _TIFFfree(tv->value);
- tv->value = NULL;
- }
- break;
- }
+ for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
+ if (td->td_customValues[iCustom].info->field_tag == tag) {
+ tv = td->td_customValues + iCustom;
+ if (tv->value != NULL) {
+ _TIFFfree(tv->value);
+ tv->value = NULL;
+ }
+ break;
+ }
}
/*
@@ -432,7 +445,7 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
td->td_customValues = new_customValues;
- tv = td->td_customValues + (td->td_customValueCount-1);
+ tv = td->td_customValues + (td->td_customValueCount - 1);
tv->info = fip;
tv->value = NULL;
tv->count = 0;
@@ -468,7 +481,8 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
if (fip->field_type == TIFF_ASCII)
_TIFFsetString((char **)&tv->value, va_arg(ap, char *));
else {
- tv->value = _TIFFmalloc(tv_size * tv->count);
+ tv->value = _TIFFCheckMalloc(tif, tv_size, tv->count,
+ "Tag Value");
if (!tv->value) {
status = 0;
goto end;
@@ -571,13 +585,17 @@ end:
va_end(ap);
return (status);
badvalue:
- TIFFErrorExt(tif->tif_clientdata, module, "%s: Bad value %d for \"%s\"",
- tif->tif_name, v, _TIFFFieldWithTag(tif, tag)->field_name);
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Bad value %d for \"%s\" tag",
+ tif->tif_name, v,
+ _TIFFFieldWithTag(tif, tag)->field_name);
va_end(ap);
return (0);
badvalue32:
- TIFFErrorExt(tif->tif_clientdata, module, "%s: Bad value %ld for \"%s\"",
- tif->tif_name, v32, _TIFFFieldWithTag(tif, tag)->field_name);
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Bad value %u for \"%s\" tag",
+ tif->tif_name, v32,
+ _TIFFFieldWithTag(tif, tag)->field_name);
va_end(ap);
return (0);
}
@@ -806,21 +824,22 @@ _TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
int i;
/*
- * This can happen if multiple images are open with
- * different codecs which have private tags. The
- * global tag information table may then have tags
- * that are valid for one file but not the other.
- * If the client tries to get a tag that is not valid
- * for the image's codec then we'll arrive here.
+ * This can happen if multiple images are open with different
+ * codecs which have private tags. The global tag information
+ * table may then have tags that are valid for one file but not
+ * the other. If the client tries to get a tag that is not valid
+ * for the image's codec then we'll arrive here.
*/
if( fip == NULL || fip->field_bit != FIELD_CUSTOM )
{
- TIFFErrorExt(tif->tif_clientdata, "_TIFFVGetField",
- "%s: Invalid %stag \"%s\" (not supported by codec)",
- tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
- _TIFFFieldWithTag(tif, tag)->field_name);
- ret_val = 0;
- break;
+ TIFFErrorExt(tif->tif_clientdata, "_TIFFVGetField",
+ "%s: Invalid %stag \"%s\" "
+ "(not supported by codec)",
+ tif->tif_name,
+ isPseudoTag(tag) ? "pseudo-" : "",
+ fip ? fip->field_name : "Unknown");
+ ret_val = 0;
+ break;
}
/*
@@ -1032,7 +1051,7 @@ TIFFDefaultDirectory(TIFF* tif)
size_t tiffFieldInfoCount;
const TIFFFieldInfo *tiffFieldInfo =
- _TIFFGetFieldInfo(&tiffFieldInfoCount);
+ _TIFFGetFieldInfo(&tiffFieldInfoCount);
_TIFFSetupFieldInfo(tif, tiffFieldInfo, tiffFieldInfoCount);
_TIFFmemset(td, 0, sizeof (*td));
@@ -1053,7 +1072,7 @@ TIFFDefaultDirectory(TIFF* tif)
td->td_ycbcrsubsampling[1] = 2;
td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
tif->tif_postdecode = _TIFFNoPostDecode;
- tif->tif_foundfield = NULL;
+ tif->tif_foundfield = NULL;
tif->tif_tagmethods.vsetfield = _TIFFVSetField;
tif->tif_tagmethods.vgetfield = _TIFFVGetField;
tif->tif_tagmethods.printdir = NULL;
@@ -1074,12 +1093,12 @@ TIFFDefaultDirectory(TIFF* tif)
*/
tif->tif_flags &= ~TIFF_DIRTYDIRECT;
- /*
- * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
- * we clear the ISTILED flag when setting up a new directory.
- * Should we also be clearing stuff like INSUBIFD?
- */
- tif->tif_flags &= ~TIFF_ISTILED;
+ /*
+ * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
+ * we clear the ISTILED flag when setting up a new directory.
+ * Should we also be clearing stuff like INSUBIFD?
+ */
+ tif->tif_flags &= ~TIFF_ISTILED;
return (1);
}
@@ -1087,59 +1106,59 @@ TIFFDefaultDirectory(TIFF* tif)
static int
TIFFAdvanceDirectory(TIFF* tif, uint32* nextdir, toff_t* off)
{
- static const char module[] = "TIFFAdvanceDirectory";
- uint16 dircount;
- if (isMapped(tif))
- {
- toff_t poff=*nextdir;
- if (poff+sizeof(uint16) > tif->tif_size)
- {
+ static const char module[] = "TIFFAdvanceDirectory";
+ uint16 dircount;
+ if (isMapped(tif))
+ {
+ toff_t poff=*nextdir;
+ if (poff+sizeof(uint16) > tif->tif_size)
+ {
TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
- tif->tif_name);
- return (0);
- }
- _TIFFmemcpy(&dircount, tif->tif_base+poff, sizeof (uint16));
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabShort(&dircount);
- poff+=sizeof (uint16)+dircount*sizeof (TIFFDirEntry);
- if (off != NULL)
- *off = poff;
- if (((toff_t) (poff+sizeof (uint32))) > tif->tif_size)
- {
+ tif->tif_name);
+ return (0);
+ }
+ _TIFFmemcpy(&dircount, tif->tif_base+poff, sizeof (uint16));
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&dircount);
+ poff+=sizeof (uint16)+dircount*sizeof (TIFFDirEntry);
+ if (off != NULL)
+ *off = poff;
+ if (((toff_t) (poff+sizeof (uint32))) > tif->tif_size)
+ {
TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
- tif->tif_name);
- return (0);
- }
- _TIFFmemcpy(nextdir, tif->tif_base+poff, sizeof (uint32));
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabLong(nextdir);
- return (1);
- }
- else
- {
- if (!SeekOK(tif, *nextdir) ||
- !ReadOK(tif, &dircount, sizeof (uint16))) {
+ tif->tif_name);
+ return (0);
+ }
+ _TIFFmemcpy(nextdir, tif->tif_base+poff, sizeof (uint32));
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong(nextdir);
+ return (1);
+ }
+ else
+ {
+ if (!SeekOK(tif, *nextdir) ||
+ !ReadOK(tif, &dircount, sizeof (uint16))) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
- tif->tif_name);
- return (0);
- }
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabShort(&dircount);
- if (off != NULL)
- *off = TIFFSeekFile(tif,
- dircount*sizeof (TIFFDirEntry), SEEK_CUR);
- else
- (void) TIFFSeekFile(tif,
- dircount*sizeof (TIFFDirEntry), SEEK_CUR);
- if (!ReadOK(tif, nextdir, sizeof (uint32))) {
+ tif->tif_name);
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&dircount);
+ if (off != NULL)
+ *off = TIFFSeekFile(tif,
+ dircount*sizeof (TIFFDirEntry), SEEK_CUR);
+ else
+ (void) TIFFSeekFile(tif,
+ dircount*sizeof (TIFFDirEntry), SEEK_CUR);
+ if (!ReadOK(tif, nextdir, sizeof (uint32))) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
- tif->tif_name);
- return (0);
- }
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabLong(nextdir);
- return (1);
- }
+ tif->tif_name);
+ return (0);
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong(nextdir);
+ return (1);
+ }
}
/*
diff --git a/src/libtiff/tif_dir.h b/src/libtiff/tif_dir.h
index 039a9cc..c843b02 100644
--- a/src/libtiff/tif_dir.h
+++ b/src/libtiff/tif_dir.h
@@ -1,4 +1,4 @@
-/* $Id: tif_dir.h,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_dir.h,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -38,44 +38,47 @@ typedef struct {
/* bit vector of fields that are set */
unsigned long td_fieldsset[FIELD_SETLONGS];
- uint32 td_imagewidth, td_imagelength, td_imagedepth;
- uint32 td_tilewidth, td_tilelength, td_tiledepth;
- uint32 td_subfiletype;
- uint16 td_bitspersample;
- uint16 td_sampleformat;
- uint16 td_compression;
- uint16 td_photometric;
- uint16 td_threshholding;
- uint16 td_fillorder;
- uint16 td_orientation;
- uint16 td_samplesperpixel;
- uint32 td_rowsperstrip;
- uint16 td_minsamplevalue, td_maxsamplevalue;
- double td_sminsamplevalue, td_smaxsamplevalue;
- float td_xresolution, td_yresolution;
- uint16 td_resolutionunit;
- uint16 td_planarconfig;
- float td_xposition, td_yposition;
- uint16 td_pagenumber[2];
- uint16* td_colormap[3];
- uint16 td_halftonehints[2];
- uint16 td_extrasamples;
- uint16* td_sampleinfo;
- tstrip_t td_stripsperimage;
- tstrip_t td_nstrips; /* size of offset & bytecount arrays */
- uint32* td_stripoffset;
- uint32* td_stripbytecount;
- int td_stripbytecountsorted; /* is the bytecount array sorted ascending? */
- uint16 td_nsubifd;
- uint32* td_subifd;
+ uint32 td_imagewidth, td_imagelength, td_imagedepth;
+ uint32 td_tilewidth, td_tilelength, td_tiledepth;
+ uint32 td_subfiletype;
+ uint16 td_bitspersample;
+ uint16 td_sampleformat;
+ uint16 td_compression;
+ uint16 td_photometric;
+ uint16 td_threshholding;
+ uint16 td_fillorder;
+ uint16 td_orientation;
+ uint16 td_samplesperpixel;
+ uint32 td_rowsperstrip;
+ uint16 td_minsamplevalue, td_maxsamplevalue;
+ double td_sminsamplevalue, td_smaxsamplevalue;
+ float td_xresolution, td_yresolution;
+ uint16 td_resolutionunit;
+ uint16 td_planarconfig;
+ float td_xposition, td_yposition;
+ uint16 td_pagenumber[2];
+ uint16* td_colormap[3];
+ uint16 td_halftonehints[2];
+ uint16 td_extrasamples;
+ uint16* td_sampleinfo;
+ /* even though the name is misleading, td_stripsperimage is the number
+ * of striles (=strips or tiles) per plane, and td_nstrips the total
+ * number of striles */
+ tstrile_t td_stripsperimage;
+ tstrile_t td_nstrips; /* size of offset & bytecount arrays */
+ toff_t* td_stripoffset;
+ toff_t* td_stripbytecount; /* FIXME: it should be tsize_t array */
+ int td_stripbytecountsorted; /* is the bytecount array sorted ascending? */
+ uint16 td_nsubifd;
+ uint32* td_subifd;
/* YCbCr parameters */
- uint16 td_ycbcrsubsampling[2];
- uint16 td_ycbcrpositioning;
+ uint16 td_ycbcrsubsampling[2];
+ uint16 td_ycbcrpositioning;
/* Colorimetry parameters */
- uint16* td_transferfunction[3];
+ uint16* td_transferfunction[3];
/* CMYK parameters */
- int td_inknameslen;
- char* td_inknames;
+ int td_inknameslen;
+ char* td_inknames;
int td_customValueCount;
TIFFTagValue *td_customValues;
@@ -177,6 +180,7 @@ extern "C" {
extern const TIFFFieldInfo *_TIFFGetFieldInfo(size_t *);
extern const TIFFFieldInfo *_TIFFGetExifFieldInfo(size_t *);
extern void _TIFFSetupFieldInfo(TIFF*, const TIFFFieldInfo[], size_t);
+extern int _TIFFMergeFieldInfo(TIFF*, const TIFFFieldInfo[], int);
extern void _TIFFPrintFieldInfo(TIFF*, FILE*);
extern TIFFDataType _TIFFSampleToTagType(TIFF*);
extern const TIFFFieldInfo* _TIFFFindOrRegisterFieldInfo( TIFF *tif,
@@ -185,7 +189,6 @@ extern const TIFFFieldInfo* _TIFFFindOrRegisterFieldInfo( TIFF *tif,
extern TIFFFieldInfo* _TIFFCreateAnonFieldInfo( TIFF *tif, ttag_t tag,
TIFFDataType dt );
-#define _TIFFMergeFieldInfo TIFFMergeFieldInfo
#define _TIFFFindFieldInfo TIFFFindFieldInfo
#define _TIFFFindFieldInfoByName TIFFFindFieldInfoByName
#define _TIFFFieldWithTag TIFFFieldWithTag
diff --git a/src/libtiff/tif_dirinfo.c b/src/libtiff/tif_dirinfo.c
index 0f9020b..610632d 100644
--- a/src/libtiff/tif_dirinfo.c
+++ b/src/libtiff/tif_dirinfo.c
@@ -1,4 +1,4 @@
-/* $Id: tif_dirinfo.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_dirinfo.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -449,6 +449,8 @@ exifFieldInfo[] = {
1, 0, "SubSecTimeDigitized" },
{ EXIFTAG_FLASHPIXVERSION, 4, 4, TIFF_UNDEFINED, FIELD_CUSTOM,
1, 0, "FlashpixVersion" },
+ { EXIFTAG_COLORSPACE, 1, 1, TIFF_SHORT, FIELD_CUSTOM,
+ 1, 0, "ColorSpace" },
{ EXIFTAG_PIXELXDIMENSION, 1, 1, TIFF_LONG, FIELD_CUSTOM,
1, 0, "PixelXDimension" },
{ EXIFTAG_PIXELXDIMENSION, 1, 1, TIFF_SHORT, FIELD_CUSTOM,
@@ -542,7 +544,11 @@ _TIFFSetupFieldInfo(TIFF* tif, const TIFFFieldInfo info[], size_t n)
_TIFFfree(tif->tif_fieldinfo);
tif->tif_nfields = 0;
}
- _TIFFMergeFieldInfo(tif, info, n);
+ if (!_TIFFMergeFieldInfo(tif, info, n))
+ {
+ TIFFErrorExt(tif->tif_clientdata, "_TIFFSetupFieldInfo",
+ "Setting up field info failed");
+ }
}
static int
@@ -552,9 +558,10 @@ tagCompare(const void* a, const void* b)
const TIFFFieldInfo* tb = *(const TIFFFieldInfo**) b;
/* NB: be careful of return values for 16-bit platforms */
if (ta->field_tag != tb->field_tag)
- return (ta->field_tag < tb->field_tag ? -1 : 1);
+ return (int)ta->field_tag - (int)tb->field_tag;
else
- return ((int)tb->field_type - (int)ta->field_type);
+ return (ta->field_type == TIFF_ANY) ?
+ 0 : ((int)tb->field_type - (int)ta->field_type);
}
static int
@@ -562,13 +569,30 @@ tagNameCompare(const void* a, const void* b)
{
const TIFFFieldInfo* ta = *(const TIFFFieldInfo**) a;
const TIFFFieldInfo* tb = *(const TIFFFieldInfo**) b;
+ int ret = strcmp(ta->field_name, tb->field_name);
- return strcmp(ta->field_name, tb->field_name);
+ if (ret)
+ return ret;
+ else
+ return (ta->field_type == TIFF_ANY) ?
+ 0 : ((int)tb->field_type - (int)ta->field_type);
}
void
+TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], int n)
+{
+ if (_TIFFMergeFieldInfo(tif, info, n) < 0)
+ {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFMergeFieldInfo",
+ "Merging block of %d fields failed", n);
+ }
+}
+
+int
_TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], int n)
{
+ static const char module[] = "_TIFFMergeFieldInfo";
+ static const char reason[] = "for field info array";
TIFFFieldInfo** tp;
int i;
@@ -576,20 +600,37 @@ _TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], int n)
if (tif->tif_nfields > 0) {
tif->tif_fieldinfo = (TIFFFieldInfo**)
- _TIFFrealloc(tif->tif_fieldinfo,
- (tif->tif_nfields+n) * sizeof (TIFFFieldInfo*));
+ _TIFFCheckRealloc(tif, tif->tif_fieldinfo,
+ (tif->tif_nfields + n),
+ sizeof (TIFFFieldInfo*), reason);
} else {
tif->tif_fieldinfo = (TIFFFieldInfo**)
- _TIFFmalloc(n * sizeof (TIFFFieldInfo*));
+ _TIFFCheckMalloc(tif, n, sizeof (TIFFFieldInfo*),
+ reason);
+ }
+ if (!tif->tif_fieldinfo) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Failed to allocate field info array");
+ return 0;
}
- assert(tif->tif_fieldinfo != NULL);
tp = tif->tif_fieldinfo + tif->tif_nfields;
for (i = 0; i < n; i++)
- *tp++ = (TIFFFieldInfo*) (info + i); /* XXX */
+ {
+ const TIFFFieldInfo *fip =
+ _TIFFFindFieldInfo(tif, info[i].field_tag, info[i].field_type);
+
+ /* only add definitions that aren't already present */
+ if (!fip) {
+ *tp++ = (TIFFFieldInfo*) (info + i);
+ tif->tif_nfields++;
+ }
+ }
/* Sort the field info by tag number */
- qsort(tif->tif_fieldinfo, tif->tif_nfields += n,
+ qsort(tif->tif_fieldinfo, tif->tif_nfields,
sizeof (TIFFFieldInfo*), tagCompare);
+
+ return n;
}
void
@@ -704,67 +745,58 @@ _TIFFSampleToTagType(TIFF* tif)
const TIFFFieldInfo*
_TIFFFindFieldInfo(TIFF* tif, ttag_t tag, TIFFDataType dt)
{
- int i, n;
+ TIFFFieldInfo key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0};
+ TIFFFieldInfo* pkey = &key;
+ const TIFFFieldInfo **ret;
if (tif->tif_foundfield && tif->tif_foundfield->field_tag == tag &&
(dt == TIFF_ANY || dt == tif->tif_foundfield->field_type))
- return (tif->tif_foundfield);
- /* NB: use sorted search (e.g. binary search) */
- if(dt != TIFF_ANY) {
- TIFFFieldInfo key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0};
- TIFFFieldInfo* pkey = &key;
- const TIFFFieldInfo **ret;
-
- key.field_tag = tag;
- key.field_type = dt;
-
- ret = (const TIFFFieldInfo **) bsearch(&pkey,
- tif->tif_fieldinfo,
- tif->tif_nfields,
- sizeof(TIFFFieldInfo *),
- tagCompare);
- return (ret) ? (*ret) : NULL;
- } else for (i = 0, n = tif->tif_nfields; i < n; i++) {
- const TIFFFieldInfo* fip = tif->tif_fieldinfo[i];
- if (fip->field_tag == tag &&
- (dt == TIFF_ANY || fip->field_type == dt))
- return (tif->tif_foundfield = fip);
+ return tif->tif_foundfield;
+
+ /* If we are invoked with no field information, then just return. */
+ if ( !tif->tif_fieldinfo ) {
+ return NULL;
}
- return ((const TIFFFieldInfo *)0);
+
+ /* NB: use sorted search (e.g. binary search) */
+ key.field_tag = tag;
+ key.field_type = dt;
+
+ ret = (const TIFFFieldInfo **) bsearch(&pkey,
+ tif->tif_fieldinfo,
+ tif->tif_nfields,
+ sizeof(TIFFFieldInfo *),
+ tagCompare);
+ return tif->tif_foundfield = (ret ? *ret : NULL);
}
const TIFFFieldInfo*
_TIFFFindFieldInfoByName(TIFF* tif, const char *field_name, TIFFDataType dt)
{
- int i, n;
+ TIFFFieldInfo key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0};
+ TIFFFieldInfo* pkey = &key;
+ const TIFFFieldInfo **ret;
if (tif->tif_foundfield
&& streq(tif->tif_foundfield->field_name, field_name)
&& (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type))
return (tif->tif_foundfield);
+
+ /* If we are invoked with no field information, then just return. */
+ if ( !tif->tif_fieldinfo ) {
+ return NULL;
+ }
+
/* NB: use sorted search (e.g. binary search) */
- if(dt != TIFF_ANY) {
- TIFFFieldInfo key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0};
- TIFFFieldInfo* pkey = &key;
- const TIFFFieldInfo **ret;
-
- key.field_name = (char *)field_name;
- key.field_type = dt;
-
- ret = (const TIFFFieldInfo **) lfind(&pkey,
- tif->tif_fieldinfo,
- &tif->tif_nfields,
- sizeof(TIFFFieldInfo *),
- tagNameCompare);
- return (ret) ? (*ret) : NULL;
- } else
- for (i = 0, n = tif->tif_nfields; i < n; i++) {
- const TIFFFieldInfo* fip = tif->tif_fieldinfo[i];
- if (streq(fip->field_name, field_name) &&
- (dt == TIFF_ANY || fip->field_type == dt))
- return (tif->tif_foundfield = fip);
- }
- return ((const TIFFFieldInfo *)0);
+ key.field_name = (char *)field_name;
+ key.field_type = dt;
+
+ ret = (const TIFFFieldInfo **) lfind(&pkey,
+ tif->tif_fieldinfo,
+ &tif->tif_nfields,
+ sizeof(TIFFFieldInfo *),
+ tagNameCompare);
+ return tif->tif_foundfield = (ret ? *ret : NULL);
}
const TIFFFieldInfo*
@@ -773,8 +805,8 @@ _TIFFFieldWithTag(TIFF* tif, ttag_t tag)
const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
if (!fip) {
TIFFErrorExt(tif->tif_clientdata, "TIFFFieldWithTag",
- "Internal error, unknown tag 0x%x",
- (unsigned int) tag);
+ "Internal error, unknown tag 0x%x",
+ (unsigned int) tag);
assert(fip != NULL);
/*NOTREACHED*/
}
@@ -788,7 +820,7 @@ _TIFFFieldWithName(TIFF* tif, const char *field_name)
_TIFFFindFieldInfoByName(tif, field_name, TIFF_ANY);
if (!fip) {
TIFFErrorExt(tif->tif_clientdata, "TIFFFieldWithName",
- "Internal error, unknown tag %s", field_name);
+ "Internal error, unknown tag %s", field_name);
assert(fip != NULL);
/*NOTREACHED*/
}
@@ -805,7 +837,8 @@ _TIFFFindOrRegisterFieldInfo( TIFF *tif, ttag_t tag, TIFFDataType dt )
if( fld == NULL )
{
fld = _TIFFCreateAnonFieldInfo( tif, tag, dt );
- _TIFFMergeFieldInfo( tif, fld, 1 );
+ if (!_TIFFMergeFieldInfo(tif, fld, 1))
+ return NULL;
}
return fld;
@@ -823,8 +856,8 @@ _TIFFCreateAnonFieldInfo(TIFF *tif, ttag_t tag, TIFFDataType field_type)
_TIFFmemset( fld, 0, sizeof(TIFFFieldInfo) );
fld->field_tag = tag;
- fld->field_readcount = TIFF_VARIABLE;
- fld->field_writecount = TIFF_VARIABLE;
+ fld->field_readcount = TIFF_VARIABLE2;
+ fld->field_writecount = TIFF_VARIABLE2;
fld->field_type = field_type;
fld->field_bit = FIELD_CUSTOM;
fld->field_oktochange = TRUE;
@@ -835,7 +868,8 @@ _TIFFCreateAnonFieldInfo(TIFF *tif, ttag_t tag, TIFFDataType field_type)
return NULL;
}
- /* note that this name is a special sign to TIFFClose() and
+ /*
+ * note that this name is a special sign to TIFFClose() and
* _TIFFSetupFieldInfo() to free the field
*/
sprintf(fld->field_name, "Tag %d", (int) tag);
diff --git a/src/libtiff/tif_dirread.c b/src/libtiff/tif_dirread.c
index 5c8c708..22f675a 100644
--- a/src/libtiff/tif_dirread.c
+++ b/src/libtiff/tif_dirread.c
@@ -1,4 +1,4 @@
-/* $Id: tif_dirread.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_dirread.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -41,9 +41,13 @@ extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
#endif
+static TIFFDirEntry* TIFFReadDirectoryFind(TIFFDirEntry* dir,
+ uint16 dircount, uint16 tagid);
static int EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16);
static void MissingRequired(TIFF*, const char*);
+static int TIFFCheckDirOffset(TIFF*, toff_t);
static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
+static uint16 TIFFFetchDirectory(TIFF*, toff_t, TIFFDirEntry**, toff_t *);
static tsize_t TIFFFetchData(TIFF*, TIFFDirEntry*, char*);
static tsize_t TIFFFetchString(TIFF*, TIFFDirEntry*, char*);
static float TIFFFetchRational(TIFF*, TIFFDirEntry*);
@@ -54,6 +58,7 @@ static int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*);
static int TIFFFetchShortArray(TIFF*, TIFFDirEntry*, uint16*);
static int TIFFFetchStripThing(TIFF*, TIFFDirEntry*, long, uint32**);
static int TIFFFetchRefBlackWhite(TIFF*, TIFFDirEntry*);
+static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
static float TIFFFetchFloat(TIFF*, TIFFDirEntry*);
static int TIFFFetchFloatArray(TIFF*, TIFFDirEntry*, float*);
static int TIFFFetchDoubleArray(TIFF*, TIFFDirEntry*, double*);
@@ -62,9 +67,8 @@ static int TIFFFetchShortPair(TIFF*, TIFFDirEntry*);
static void ChopUpSingleUncompressedStrip(TIFF*);
/*
- * Read the next TIFF directory from a file
- * and convert it to the internal format.
- * We read directories sequentially.
+ * Read the next TIFF directory from a file and convert it to the internal
+ * format. We read directories sequentially.
*/
int
TIFFReadDirectory(TIFF* tif)
@@ -79,105 +83,27 @@ TIFFReadDirectory(TIFF* tif)
const TIFFFieldInfo* fip;
size_t fix;
uint16 dircount;
- toff_t nextdiroff;
- int diroutoforderwarning = 0;
- toff_t* new_dirlist;
+ int diroutoforderwarning = 0, compressionknown = 0;
tif->tif_diroff = tif->tif_nextdiroff;
- if (tif->tif_diroff == 0) /* no more directories */
- return (0);
-
/*
- * XXX: Trick to prevent IFD looping. The one can create TIFF file
- * with looped directory pointers. We will maintain a list of already
- * seen directories and check every IFD offset against this list.
+ * Check whether we have the last offset or bad offset (IFD looping).
*/
- for (n = 0; n < tif->tif_dirnumber; n++) {
- if (tif->tif_dirlist[n] == tif->tif_diroff)
- return (0);
- }
- tif->tif_dirnumber++;
- new_dirlist = (toff_t *)_TIFFrealloc(tif->tif_dirlist,
- tif->tif_dirnumber * sizeof(toff_t));
- if (!new_dirlist) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Failed to allocate space for IFD list",
- tif->tif_name);
- return (0);
- }
- tif->tif_dirlist = new_dirlist;
- tif->tif_dirlist[tif->tif_dirnumber - 1] = tif->tif_diroff;
-
+ if (!TIFFCheckDirOffset(tif, tif->tif_nextdiroff))
+ return 0;
/*
* Cleanup any previous compression state.
*/
(*tif->tif_cleanup)(tif);
tif->tif_curdir++;
- nextdiroff = 0;
- if (!isMapped(tif)) {
- if (!SeekOK(tif, tif->tif_diroff)) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Seek error accessing TIFF directory",
- tif->tif_name);
- return (0);
- }
- if (!ReadOK(tif, &dircount, sizeof (uint16))) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Can not read TIFF directory count",
- tif->tif_name);
- return (0);
- }
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabShort(&dircount);
- dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
- sizeof (TIFFDirEntry),
- "to read TIFF directory");
- if (dir == NULL)
- return (0);
- if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%.100s: Can not read TIFF directory",
- tif->tif_name);
- goto bad;
- }
- /*
- * Read offset to next directory for sequential scans.
- */
- (void) ReadOK(tif, &nextdiroff, sizeof (uint32));
- } else {
- toff_t off = tif->tif_diroff;
-
- if (off + sizeof (uint16) > tif->tif_size) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Can not read TIFF directory count",
- tif->tif_name);
- return (0);
- } else
- _TIFFmemcpy(&dircount, tif->tif_base + off, sizeof (uint16));
- off += sizeof (uint16);
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabShort(&dircount);
- dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
- sizeof (TIFFDirEntry),
- "to read TIFF directory");
- if (dir == NULL)
- return (0);
- if (off + dircount*sizeof (TIFFDirEntry) > tif->tif_size) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Can not read TIFF directory",
- tif->tif_name);
- goto bad;
- } else {
- _TIFFmemcpy(dir, tif->tif_base + off,
- dircount*sizeof (TIFFDirEntry));
- }
- off += dircount* sizeof (TIFFDirEntry);
- if (off + sizeof (uint32) <= tif->tif_size)
- _TIFFmemcpy(&nextdiroff, tif->tif_base+off, sizeof (uint32));
+ dircount = TIFFFetchDirectory(tif, tif->tif_nextdiroff,
+ &dir, &tif->tif_nextdiroff);
+ if (!dircount) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Failed to read directory at offset %u",
+ tif->tif_name, tif->tif_nextdiroff);
+ return 0;
}
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabLong(&nextdiroff);
- tif->tif_nextdiroff = nextdiroff;
tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
/*
@@ -216,7 +142,7 @@ TIFFReadDirectory(TIFF* tif)
*
* It sure would have been nice if Aldus had really thought
* this stuff through carefully.
- */
+ */
for (dp = dir, n = dircount; n > 0; n--, dp++) {
if (tif->tif_flags & TIFF_SWAB) {
TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
@@ -236,7 +162,7 @@ TIFFReadDirectory(TIFF* tif)
if (fix >= tif->tif_nfields || dp->tdir_tag == IGNORE)
continue;
-
+
/*
* Silicon Beach (at least) writes unordered
* directory tags (violating the spec). Handle
@@ -246,13 +172,13 @@ TIFFReadDirectory(TIFF* tif)
if (!diroutoforderwarning) {
TIFFWarningExt(tif->tif_clientdata, module,
"%s: invalid TIFF directory; tags are not sorted in ascending order",
- tif->tif_name);
+ tif->tif_name);
diroutoforderwarning = 1;
}
fix = 0; /* O(n^2) */
}
while (fix < tif->tif_nfields &&
- tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
+ tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
fix++;
if (fix >= tif->tif_nfields ||
tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
@@ -262,18 +188,25 @@ TIFFReadDirectory(TIFF* tif)
"%s: unknown field with tag %d (0x%x) encountered",
tif->tif_name,
dp->tdir_tag,
- dp->tdir_tag,
- dp->tdir_type);
+ dp->tdir_tag);
- TIFFMergeFieldInfo(tif,
- _TIFFCreateAnonFieldInfo(tif,
+ if (!_TIFFMergeFieldInfo(tif,
+ _TIFFCreateAnonFieldInfo(tif,
dp->tdir_tag,
(TIFFDataType) dp->tdir_type),
- 1 );
- fix = 0;
- while (fix < tif->tif_nfields &&
- tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
- fix++;
+ 1))
+ {
+ TIFFWarningExt(tif->tif_clientdata,
+ module,
+ "Registering anonymous field with tag %d (0x%x) failed",
+ dp->tdir_tag,
+ dp->tdir_tag);
+ goto ignore;
+ }
+ fix = 0;
+ while (fix < tif->tif_nfields &&
+ tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
+ fix++;
}
/*
* Null out old tags that we ignore.
@@ -288,10 +221,10 @@ TIFFReadDirectory(TIFF* tif)
*/
fip = tif->tif_fieldinfo[fix];
while (dp->tdir_type != (unsigned short) fip->field_type
- && fix < tif->tif_nfields) {
+ && fix < tif->tif_nfields) {
if (fip->field_type == TIFF_ANY) /* wildcard */
break;
- fip = tif->tif_fieldinfo[++fix];
+ fip = tif->tif_fieldinfo[++fix];
if (fix >= tif->tif_nfields ||
fip->field_tag != dp->tdir_tag) {
TIFFWarningExt(tif->tif_clientdata, module,
@@ -326,6 +259,8 @@ TIFFReadDirectory(TIFF* tif)
dp->tdir_type, dp->tdir_offset);
if (!TIFFSetField(tif, dp->tdir_tag, (uint16)v))
goto bad;
+ else
+ compressionknown = 1;
break;
/* XXX: workaround for broken TIFFs */
} else if (dp->tdir_type == TIFF_LONG) {
@@ -362,6 +297,30 @@ TIFFReadDirectory(TIFF* tif)
}
/*
+ * XXX: OJPEG hack.
+ * If a) compression is OJPEG, b) planarconfig tag says it's separate,
+ * c) strip offsets/bytecounts tag are both present and
+ * d) both contain exactly one value, then we consistently find
+ * that the buggy implementation of the buggy compression scheme
+ * matches contig planarconfig best. So we 'fix-up' the tag here
+ */
+ if ((td->td_compression==COMPRESSION_OJPEG) &&
+ (td->td_planarconfig==PLANARCONFIG_SEPARATE)) {
+ dp = TIFFReadDirectoryFind(dir,dircount,TIFFTAG_STRIPOFFSETS);
+ if ((dp!=0) && (dp->tdir_count==1)) {
+ dp = TIFFReadDirectoryFind(dir, dircount,
+ TIFFTAG_STRIPBYTECOUNTS);
+ if ((dp!=0) && (dp->tdir_count==1)) {
+ td->td_planarconfig=PLANARCONFIG_CONTIG;
+ TIFFWarningExt(tif->tif_clientdata,
+ "TIFFReadDirectory",
+ "Planarconfig tag value assumed incorrect, "
+ "assuming data is contig instead of chunky");
+ }
+ }
+ }
+
+ /*
* Allocate directory structure and setup defaults.
*/
if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
@@ -369,7 +328,7 @@ TIFFReadDirectory(TIFF* tif)
goto bad;
}
/*
- * Setup appropriate structures (by strip or by tile)
+ * Setup appropriate structures (by strip or by tile)
*/
if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
td->td_nstrips = TIFFNumberOfStrips(tif);
@@ -391,9 +350,23 @@ TIFFReadDirectory(TIFF* tif)
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
td->td_stripsperimage /= td->td_samplesperpixel;
if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
- MissingRequired(tif,
+ if ((td->td_compression==COMPRESSION_OJPEG) &&
+ (isTiled(tif)==0) &&
+ (td->td_nstrips==1)) {
+ /*
+ * XXX: OJPEG hack.
+ * If a) compression is OJPEG, b) it's not a tiled TIFF,
+ * and c) the number of strips is 1,
+ * then we tolerate the absence of stripoffsets tag,
+ * because, presumably, all required data is in the
+ * JpegInterchangeFormat stream.
+ */
+ TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
+ } else {
+ MissingRequired(tif,
isTiled(tif) ? "TileOffsets" : "StripOffsets");
- goto bad;
+ goto bad;
+ }
}
/*
@@ -414,11 +387,11 @@ TIFFReadDirectory(TIFF* tif)
* one value per sample. Because of this, we
* accept the tag if one value is supplied.
*
- * The MinSampleValue, MaxSampleValue, BitsPerSample
- * DataType and SampleFormat tags are supposed to be
- * written as one value/sample, but some vendors
- * incorrectly write one value only -- so we accept
- * that as well (yech). Other vendors write correct
+ * The MinSampleValue, MaxSampleValue, BitsPerSample
+ * DataType and SampleFormat tags are supposed to be
+ * written as one value/sample, but some vendors
+ * incorrectly write one value only -- so we accept
+ * that as well (yech). Other vendors write correct
* value for NumberOfSamples, but incorrect one for
* BitsPerSample and friends, and we will read this
* too.
@@ -529,6 +502,69 @@ TIFFReadDirectory(TIFF* tif)
}
}
/*
+ * OJPEG hack:
+ * - If a) compression is OJPEG, and b) photometric tag is missing,
+ * then we consistently find that photometric should be YCbCr
+ * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
+ * then we consistently find that the buggy implementation of the
+ * buggy compression scheme matches photometric YCbCr instead.
+ * - If a) compression is OJPEG, and b) bitspersample tag is missing,
+ * then we consistently find bitspersample should be 8.
+ * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
+ * and c) photometric is RGB or YCbCr, then we consistently find
+ * samplesperpixel should be 3
+ * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
+ * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
+ * find samplesperpixel should be 3
+ */
+ if (td->td_compression==COMPRESSION_OJPEG)
+ {
+ if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
+ {
+ TIFFWarningExt(tif->tif_clientdata, "TIFFReadDirectory",
+ "Photometric tag is missing, assuming data is YCbCr");
+ if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
+ goto bad;
+ }
+ else if (td->td_photometric==PHOTOMETRIC_RGB)
+ {
+ td->td_photometric=PHOTOMETRIC_YCBCR;
+ TIFFWarningExt(tif->tif_clientdata, "TIFFReadDirectory",
+ "Photometric tag value assumed incorrect, "
+ "assuming data is YCbCr instead of RGB");
+ }
+ if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
+ {
+ TIFFWarningExt(tif->tif_clientdata,"TIFFReadDirectory",
+ "BitsPerSample tag is missing, assuming 8 bits per sample");
+ if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
+ goto bad;
+ }
+ if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
+ {
+ if ((td->td_photometric==PHOTOMETRIC_RGB)
+ || (td->td_photometric==PHOTOMETRIC_YCBCR))
+ {
+ TIFFWarningExt(tif->tif_clientdata,
+ "TIFFReadDirectory",
+ "SamplesPerPixel tag is missing, "
+ "assuming correct SamplesPerPixel value is 3");
+ if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
+ goto bad;
+ }
+ else if ((td->td_photometric==PHOTOMETRIC_MINISWHITE)
+ || (td->td_photometric==PHOTOMETRIC_MINISBLACK))
+ {
+ TIFFWarningExt(tif->tif_clientdata,
+ "TIFFReadDirectory",
+ "SamplesPerPixel tag is missing, "
+ "assuming correct SamplesPerPixel value is 1");
+ if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
+ goto bad;
+ }
+ }
+ }
+ /*
* Verify Palette image has a Colormap.
*/
if (td->td_photometric == PHOTOMETRIC_PALETTE &&
@@ -537,76 +573,90 @@ TIFFReadDirectory(TIFF* tif)
goto bad;
}
/*
- * Attempt to deal with a missing StripByteCounts tag.
+ * OJPEG hack:
+ * We do no further messing with strip/tile offsets/bytecounts in OJPEG
+ * TIFFs
*/
- if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
+ if (td->td_compression!=COMPRESSION_OJPEG)
+ {
/*
- * Some manufacturers violate the spec by not giving
- * the size of the strips. In this case, assume there
- * is one uncompressed strip of data.
+ * Attempt to deal with a missing StripByteCounts tag.
*/
- if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
- td->td_nstrips > 1) ||
- (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
- td->td_nstrips != td->td_samplesperpixel)) {
- MissingRequired(tif, "StripByteCounts");
- goto bad;
- }
- TIFFWarningExt(tif->tif_clientdata, module,
- "%s: TIFF directory is missing required "
- "\"%s\" field, calculating from imagelength",
- tif->tif_name,
- _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
- if (EstimateStripByteCounts(tif, dir, dircount) < 0)
- goto bad;
-/*
- * Assume we have wrong StripByteCount value (in case of single strip) in
- * following cases:
- * - it is equal to zero along with StripOffset;
- * - it is larger than file itself (in case of uncompressed image);
- * - it is smaller than the size of the bytes per row multiplied on the
- * number of rows. The last case should not be checked in the case of
- * writing new image, because we may do not know the exact strip size
- * until the whole image will be written and directory dumped out.
- */
-#define BYTECOUNTLOOKSBAD \
- ( (td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \
- (td->td_compression == COMPRESSION_NONE && \
- td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0]) || \
- (tif->tif_mode == O_RDONLY && \
- td->td_compression == COMPRESSION_NONE && \
- td->td_stripbytecount[0] < TIFFScanlineSize(tif) * td->td_imagelength) )
-
- } else if (td->td_nstrips == 1
- && td->td_stripoffset[0] != 0
- && BYTECOUNTLOOKSBAD) {
+ if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
+ /*
+ * Some manufacturers violate the spec by not giving
+ * the size of the strips. In this case, assume there
+ * is one uncompressed strip of data.
+ */
+ if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
+ td->td_nstrips > 1) ||
+ (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
+ td->td_nstrips != td->td_samplesperpixel)) {
+ MissingRequired(tif, "StripByteCounts");
+ goto bad;
+ }
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "%s: TIFF directory is missing required "
+ "\"%s\" field, calculating from imagelength",
+ tif->tif_name,
+ _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
+ if (EstimateStripByteCounts(tif, dir, dircount) < 0)
+ goto bad;
/*
- * XXX: Plexus (and others) sometimes give a value of zero for
- * a tag when they don't know what the correct value is! Try
- * and handle the simple case of estimating the size of a one
- * strip image.
+ * Assume we have wrong StripByteCount value (in case
+ * of single strip) in following cases:
+ * - it is equal to zero along with StripOffset;
+ * - it is larger than file itself (in case of uncompressed
+ * image);
+ * - it is smaller than the size of the bytes per row
+ * multiplied on the number of rows. The last case should
+ * not be checked in the case of writing new image,
+ * because we may do not know the exact strip size
+ * until the whole image will be written and directory
+ * dumped out.
*/
- TIFFWarningExt(tif->tif_clientdata, module,
+ #define BYTECOUNTLOOKSBAD \
+ ( (td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \
+ (td->td_compression == COMPRESSION_NONE && \
+ td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0]) || \
+ (tif->tif_mode == O_RDONLY && \
+ td->td_compression == COMPRESSION_NONE && \
+ td->td_stripbytecount[0] < TIFFScanlineSize(tif) * td->td_imagelength) )
+
+ } else if (td->td_nstrips == 1
+ && td->td_stripoffset[0] != 0
+ && BYTECOUNTLOOKSBAD) {
+ /*
+ * XXX: Plexus (and others) sometimes give a value of
+ * zero for a tag when they don't know what the
+ * correct value is! Try and handle the simple case
+ * of estimating the size of a one strip image.
+ */
+ TIFFWarningExt(tif->tif_clientdata, module,
"%s: Bogus \"%s\" field, ignoring and calculating from imagelength",
- tif->tif_name,
- _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
- if(EstimateStripByteCounts(tif, dir, dircount) < 0)
- goto bad;
- } else if (td->td_planarconfig == PLANARCONFIG_CONTIG
- && td->td_nstrips > 2
- && td->td_compression == COMPRESSION_NONE
- && td->td_stripbytecount[0] != td->td_stripbytecount[1]) {
- /*
- * XXX: Some vendors fill StripByteCount array with absolutely
- * wrong values (it can be equal to StripOffset array, for
- * example). Catch this case here.
- */
- TIFFWarningExt(tif->tif_clientdata, module,
+ tif->tif_name,
+ _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
+ if(EstimateStripByteCounts(tif, dir, dircount) < 0)
+ goto bad;
+ } else if (td->td_planarconfig == PLANARCONFIG_CONTIG
+ && td->td_nstrips > 2
+ && td->td_compression == COMPRESSION_NONE
+ && td->td_stripbytecount[0] != td->td_stripbytecount[1]
+ && td->td_stripbytecount[0] != 0
+ && td->td_stripbytecount[1] != 0 ) {
+ /*
+ * XXX: Some vendors fill StripByteCount array with
+ * absolutely wrong values (it can be equal to
+ * StripOffset array, for example). Catch this case
+ * here.
+ */
+ TIFFWarningExt(tif->tif_clientdata, module,
"%s: Wrong \"%s\" field, ignoring and calculating from imagelength",
- tif->tif_name,
- _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
- if (EstimateStripByteCounts(tif, dir, dircount) < 0)
- goto bad;
+ tif->tif_name,
+ _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
+ if (EstimateStripByteCounts(tif, dir, dircount) < 0)
+ goto bad;
+ }
}
if (dir) {
_TIFFfree((char *)dir);
@@ -638,15 +688,15 @@ TIFFReadDirectory(TIFF* tif)
if (!TIFFFieldSet(tif, FIELD_COMPRESSION))
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
- /*
- * Some manufacturers make life difficult by writing
+ /*
+ * Some manufacturers make life difficult by writing
* large amounts of uncompressed data as a single strip.
* This is contrary to the recommendations of the spec.
- * The following makes an attempt at breaking such images
+ * The following makes an attempt at breaking such images
* into strips closer to the recommended 8k bytes. A
* side effect, however, is that the RowsPerStrip tag
* value may be changed.
- */
+ */
if (td->td_nstrips == 1 && td->td_compression == COMPRESSION_NONE &&
(tif->tif_flags & (TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP)
ChopUpSingleUncompressedStrip(tif);
@@ -662,22 +712,25 @@ TIFFReadDirectory(TIFF* tif)
tif->tif_scanlinesize = TIFFScanlineSize(tif);
if (!tif->tif_scanlinesize) {
- TIFFErrorExt(tif->tif_clientdata, module, "%s: cannot handle zero scanline size",
- tif->tif_name);
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: cannot handle zero scanline size",
+ tif->tif_name);
return (0);
}
if (isTiled(tif)) {
tif->tif_tilesize = TIFFTileSize(tif);
if (!tif->tif_tilesize) {
- TIFFErrorExt(tif->tif_clientdata, module, "%s: cannot handle zero tile size",
- tif->tif_name);
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: cannot handle zero tile size",
+ tif->tif_name);
return (0);
}
} else {
if (!TIFFStripSize(tif)) {
- TIFFErrorExt(tif->tif_clientdata, module, "%s: cannot handle zero strip size",
- tif->tif_name);
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: cannot handle zero strip size",
+ tif->tif_name);
return (0);
}
}
@@ -688,7 +741,20 @@ bad:
return (0);
}
-/*
+static TIFFDirEntry*
+TIFFReadDirectoryFind(TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
+{
+ TIFFDirEntry* m;
+ uint16 n;
+ for (m=dir, n=0; n<dircount; m++, n++)
+ {
+ if (m->tdir_tag==tagid)
+ return(m);
+ }
+ return(0);
+}
+
+/*
* Read custom directory from the arbitarry offset.
* The code is very similar to TIFFReadDirectory().
*/
@@ -706,64 +772,16 @@ TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
_TIFFSetupFieldInfo(tif, info, n);
- tif->tif_diroff = diroff;
-
- if (!isMapped(tif)) {
- if (!SeekOK(tif, diroff)) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Seek error accessing TIFF directory",
- tif->tif_name);
- return (0);
- }
- if (!ReadOK(tif, &dircount, sizeof (uint16))) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Can not read TIFF directory count",
- tif->tif_name);
- return (0);
- }
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabShort(&dircount);
- dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
- sizeof (TIFFDirEntry),
- "to read TIFF custom directory");
- if (dir == NULL)
- return (0);
- if (!ReadOK(tif, dir, dircount * sizeof (TIFFDirEntry))) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%.100s: Can not read TIFF directory",
- tif->tif_name);
- goto bad;
- }
- } else {
- toff_t off = diroff;
-
- if (off + sizeof (uint16) > tif->tif_size) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Can not read TIFF directory count",
- tif->tif_name);
- return (0);
- } else
- _TIFFmemcpy(&dircount, tif->tif_base + off, sizeof (uint16));
- off += sizeof (uint16);
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabShort(&dircount);
- dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
- sizeof (TIFFDirEntry),
- "to read TIFF custom directory");
- if (dir == NULL)
- return (0);
- if (off + dircount * sizeof (TIFFDirEntry) > tif->tif_size) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Can not read TIFF directory",
- tif->tif_name);
- goto bad;
- } else {
- _TIFFmemcpy(dir, tif->tif_base + off,
- dircount * sizeof (TIFFDirEntry));
- }
+ dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
+ if (!dircount) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Failed to read custom directory at offset %u",
+ tif->tif_name, diroff);
+ return 0;
}
TIFFFreeDirectory(tif);
+ _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
fix = 0;
for (dp = dir, i = dircount; i > 0; i--, dp++) {
@@ -784,14 +802,18 @@ TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
TIFFWarningExt(tif->tif_clientdata, module,
"%s: unknown field with tag %d (0x%x) encountered",
- tif->tif_name, dp->tdir_tag, dp->tdir_tag,
- dp->tdir_type);
-
- TIFFMergeFieldInfo(tif,
- _TIFFCreateAnonFieldInfo(tif,
- dp->tdir_tag,
- (TIFFDataType)dp->tdir_type),
- 1);
+ tif->tif_name, dp->tdir_tag, dp->tdir_tag);
+ if (!_TIFFMergeFieldInfo(tif,
+ _TIFFCreateAnonFieldInfo(tif,
+ dp->tdir_tag,
+ (TIFFDataType) dp->tdir_type),
+ 1))
+ {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "Registering anonymous field with tag %d (0x%x) failed",
+ dp->tdir_tag, dp->tdir_tag);
+ goto ignore;
+ }
fix = 0;
while (fix < tif->tif_nfields &&
@@ -836,17 +858,22 @@ TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
goto ignore;
}
- (void) TIFFFetchNormalTag(tif, dp);
+ /*
+ * EXIF tags which need to be specifically processed.
+ */
+ switch (dp->tdir_tag) {
+ case EXIFTAG_SUBJECTDISTANCE:
+ (void) TIFFFetchSubjectDistance(tif, dp);
+ break;
+ default:
+ (void) TIFFFetchNormalTag(tif, dp);
+ break;
+ }
}
if (dir)
_TIFFfree(dir);
return 1;
-
-bad:
- if (dir)
- _TIFFfree(dir);
- return 0;
}
/*
@@ -868,15 +895,18 @@ EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
{
static const char module[] = "EstimateStripByteCounts";
- register TIFFDirEntry *dp;
- register TIFFDirectory *td = &tif->tif_dir;
- uint16 i;
+ TIFFDirEntry *dp;
+ TIFFDirectory *td = &tif->tif_dir;
+ uint32 strip;
if (td->td_stripbytecount)
_TIFFfree(td->td_stripbytecount);
td->td_stripbytecount = (uint32*)
_TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint32),
"for \"StripByteCounts\" array");
+ if( td->td_stripbytecount == NULL )
+ return -1;
+
if (td->td_compression != COMPRESSION_NONE) {
uint32 space = (uint32)(sizeof (TIFFHeader)
+ sizeof (uint16)
@@ -902,8 +932,8 @@ EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
space = filesize - space;
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
space /= td->td_samplesperpixel;
- for (i = 0; i < td->td_nstrips; i++)
- td->td_stripbytecount[i] = space;
+ for (strip = 0; strip < td->td_nstrips; strip++)
+ td->td_stripbytecount[strip] = space;
/*
* This gross hack handles the case were the offset to
* the last strip is past the place where we think the strip
@@ -911,16 +941,21 @@ EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
* it's safe to assume that we've overestimated the amount
* of data in the strip and trim this number back accordingly.
*/
- i--;
- if (((toff_t)(td->td_stripoffset[i]+td->td_stripbytecount[i]))
- > filesize)
- td->td_stripbytecount[i] =
- filesize - td->td_stripoffset[i];
+ strip--;
+ if (((toff_t)(td->td_stripoffset[strip]+
+ td->td_stripbytecount[strip])) > filesize)
+ td->td_stripbytecount[strip] =
+ filesize - td->td_stripoffset[strip];
+ } else if (isTiled(tif)) {
+ uint32 bytespertile = TIFFTileSize(tif);
+
+ for (strip = 0; strip < td->td_nstrips; strip++)
+ td->td_stripbytecount[strip] = bytespertile;
} else {
uint32 rowbytes = TIFFScanlineSize(tif);
uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
- for (i = 0; i < td->td_nstrips; i++)
- td->td_stripbytecount[i] = rowbytes*rowsperstrip;
+ for (strip = 0; strip < td->td_nstrips; strip++)
+ td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
}
TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
@@ -939,23 +974,65 @@ MissingRequired(TIFF* tif, const char* tagname)
}
/*
- * Check the count field of a directory
- * entry against a known value. The caller
- * is expected to skip/ignore the tag if
- * there is a mismatch.
+ * Check the directory offset against the list of already seen directory
+ * offsets. This is a trick to prevent IFD looping. The one can create TIFF
+ * file with looped directory pointers. We will maintain a list of already
+ * seen directories and check every IFD offset against that list.
+ */
+static int
+TIFFCheckDirOffset(TIFF* tif, toff_t diroff)
+{
+ uint16 n;
+
+ if (diroff == 0) /* no more directories */
+ return 0;
+
+ for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
+ if (tif->tif_dirlist[n] == diroff)
+ return 0;
+ }
+
+ tif->tif_dirnumber++;
+
+ if (tif->tif_dirnumber > tif->tif_dirlistsize) {
+ toff_t* new_dirlist;
+
+ /*
+ * XXX: Reduce memory allocation granularity of the dirlist
+ * array.
+ */
+ new_dirlist = (toff_t *)_TIFFCheckRealloc(tif,
+ tif->tif_dirlist,
+ tif->tif_dirnumber,
+ 2 * sizeof(toff_t),
+ "for IFD list");
+ if (!new_dirlist)
+ return 0;
+ tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
+ tif->tif_dirlist = new_dirlist;
+ }
+
+ tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
+
+ return 1;
+}
+
+/*
+ * Check the count field of a directory entry against a known value. The
+ * caller is expected to skip/ignore the tag if there is a mismatch.
*/
static int
CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
{
if (count > dir->tdir_count) {
TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
- "incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored",
+ "incorrect count for field \"%s\" (%u, expecting %u); tag ignored",
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
dir->tdir_count, count);
return (0);
} else if (count < dir->tdir_count) {
TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
- "incorrect count for field \"%s\" (%lu, expecting %lu); tag trimmed",
+ "incorrect count for field \"%s\" (%u, expecting %u); tag trimmed",
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
dir->tdir_count, count);
return (1);
@@ -964,16 +1041,128 @@ CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
}
/*
+ * Read IFD structure from the specified offset. If the pointer to
+ * nextdiroff variable has been specified, read it too. Function returns a
+ * number of fields in the directory or 0 if failed.
+ */
+static uint16
+TIFFFetchDirectory(TIFF* tif, toff_t diroff, TIFFDirEntry **pdir,
+ toff_t *nextdiroff)
+{
+ static const char module[] = "TIFFFetchDirectory";
+
+ TIFFDirEntry *dir;
+ uint16 dircount;
+
+ assert(pdir);
+
+ tif->tif_diroff = diroff;
+ if (nextdiroff)
+ *nextdiroff = 0;
+ if (!isMapped(tif)) {
+ if (!SeekOK(tif, tif->tif_diroff)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Seek error accessing TIFF directory",
+ tif->tif_name);
+ return 0;
+ }
+ if (!ReadOK(tif, &dircount, sizeof (uint16))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Can not read TIFF directory count",
+ tif->tif_name);
+ return 0;
+ }
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&dircount);
+ dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
+ sizeof (TIFFDirEntry),
+ "to read TIFF directory");
+ if (dir == NULL)
+ return 0;
+ if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%.100s: Can not read TIFF directory",
+ tif->tif_name);
+ _TIFFfree(dir);
+ return 0;
+ }
+ /*
+ * Read offset to next directory for sequential scans if
+ * needed.
+ */
+ if (nextdiroff)
+ (void) ReadOK(tif, nextdiroff, sizeof(uint32));
+ } else {
+ toff_t off = tif->tif_diroff;
+
+ /*
+ * Check for integer overflow when validating the dir_off,
+ * otherwise a very high offset may cause an OOB read and
+ * crash the client. Make two comparisons instead of
+ *
+ * off + sizeof(uint16) > tif->tif_size
+ *
+ * to avoid overflow.
+ */
+ if (tif->tif_size < sizeof (uint16) ||
+ off > tif->tif_size - sizeof(uint16)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Can not read TIFF directory count",
+ tif->tif_name);
+ return 0;
+ } else {
+ _TIFFmemcpy(&dircount, tif->tif_base + off,
+ sizeof(uint16));
+ }
+ off += sizeof (uint16);
+ if (tif->tif_flags & TIFF_SWAB)
+ TIFFSwabShort(&dircount);
+ dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
+ sizeof(TIFFDirEntry),
+ "to read TIFF directory");
+ if (dir == NULL)
+ return 0;
+ if (off + dircount * sizeof (TIFFDirEntry) > tif->tif_size) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "%s: Can not read TIFF directory",
+ tif->tif_name);
+ _TIFFfree(dir);
+ return 0;
+ } else {
+ _TIFFmemcpy(dir, tif->tif_base + off,
+ dircount * sizeof(TIFFDirEntry));
+ }
+ if (nextdiroff) {
+ off += dircount * sizeof (TIFFDirEntry);
+ if (off + sizeof (uint32) <= tif->tif_size) {
+ _TIFFmemcpy(nextdiroff, tif->tif_base + off,
+ sizeof (uint32));
+ }
+ }
+ }
+ if (nextdiroff && tif->tif_flags & TIFF_SWAB)
+ TIFFSwabLong(nextdiroff);
+ *pdir = dir;
+ return dircount;
+}
+
+/*
* Fetch a contiguous directory item.
*/
static tsize_t
TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
{
- int w = TIFFDataWidth((TIFFDataType) dir->tdir_type);
- tsize_t cc = dir->tdir_count * w;
+ uint32 w = TIFFDataWidth((TIFFDataType) dir->tdir_type);
+ /*
+ * FIXME: butecount should have tsize_t type, but for now libtiff
+ * defines tsize_t as a signed 32-bit integer and we are losing
+ * ability to read arrays larger than 2^31 bytes. So we are using
+ * uint32 instead of tsize_t here.
+ */
+ uint32 cc = dir->tdir_count * w;
/* Check for overflow. */
- if (!dir->tdir_count || !w || cc / w != (tsize_t)dir->tdir_count)
+ if (!dir->tdir_count || !w || cc / w != dir->tdir_count)
goto bad;
if (!isMapped(tif)) {
@@ -983,9 +1172,9 @@ TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
goto bad;
} else {
/* Check for overflow. */
- if ((tsize_t)dir->tdir_offset + cc < (tsize_t)dir->tdir_offset
- || (tsize_t)dir->tdir_offset + cc < cc
- || (tsize_t)dir->tdir_offset + cc > (tsize_t)tif->tif_size)
+ if (dir->tdir_offset + cc < dir->tdir_offset
+ || dir->tdir_offset + cc < cc
+ || dir->tdir_offset + cc > tif->tif_size)
goto bad;
_TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc);
}
@@ -1041,7 +1230,7 @@ cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv)
{
if (denom == 0) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "%s: Rational with zero denominator (num = %lu)",
+ "%s: Rational with zero denominator (num = %u)",
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num);
return (0);
} else {
@@ -1054,9 +1243,8 @@ cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv)
}
/*
- * Fetch a rational item from the file
- * at offset off and return the value
- * as a floating point number.
+ * Fetch a rational item from the file at offset off and return the value as a
+ * floating point number.
*/
static float
TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir)
@@ -1069,9 +1257,8 @@ TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir)
}
/*
- * Fetch a single floating point value
- * from the offset field and return it
- * as a native float.
+ * Fetch a single floating point value from the offset field and return it as
+ * a native float.
*/
static float
TIFFFetchFloat(TIFF* tif, TIFFDirEntry* dir)
@@ -1159,6 +1346,18 @@ TIFFFetchShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
static int
TIFFFetchShortPair(TIFF* tif, TIFFDirEntry* dir)
{
+ /*
+ * Prevent overflowing the v stack arrays below by performing a sanity
+ * check on tdir_count, this should never be greater than two.
+ */
+ if (dir->tdir_count > 2) {
+ TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+ "unexpected count for field \"%s\", %u, expected 2; ignored",
+ _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
+ dir->tdir_count);
+ return 0;
+ }
+
switch (dir->tdir_type) {
case TIFF_BYTE:
case TIFF_SBYTE:
@@ -1251,15 +1450,13 @@ TIFFFetchDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
}
/*
- * Fetch an array of ANY values. The actual values are
- * returned as doubles which should be able hold all the
- * types. Yes, there really should be an tany_t to avoid
- * this potential non-portability ... Note in particular
- * that we assume that the double return value vector is
- * large enough to read in any fundamental type. We use
- * that vector as a buffer to read in the base type vector
- * and then convert it in place to double (from end
- * to front of course).
+ * Fetch an array of ANY values. The actual values are returned as doubles
+ * which should be able hold all the types. Yes, there really should be an
+ * tany_t to avoid this potential non-portability ... Note in particular that
+ * we assume that the double return value vector is large enough to read in
+ * any fundamental type. We use that vector as a buffer to read in the base
+ * type vector and then convert it in place to double (from end to front of
+ * course).
*/
static int
TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v)
@@ -1510,10 +1707,10 @@ TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, uint16* pl)
for (i = 1; i < check_count; i++)
if (v[i] != v[0]) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "Cannot handle different per-sample values for field \"%s\"",
- _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
- goto bad;
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Cannot handle different per-sample values for field \"%s\"",
+ _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
+ goto bad;
}
*pl = v[0];
status = 1;
@@ -1551,10 +1748,10 @@ TIFFFetchPerSampleLongs(TIFF* tif, TIFFDirEntry* dir, uint32* pl)
check_count = samples;
for (i = 1; i < check_count; i++)
if (v[i] != v[0]) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "Cannot handle different per-sample values for field \"%s\"",
- _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
- goto bad;
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Cannot handle different per-sample values for field \"%s\"",
+ _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
+ goto bad;
}
*pl = v[0];
status = 1;
@@ -1591,10 +1788,10 @@ TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* pl)
for (i = 1; i < check_count; i++)
if (v[i] != v[0]) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "Cannot handle different per-sample values for field \"%s\"",
- _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
- goto bad;
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Cannot handle different per-sample values for field \"%s\"",
+ _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
+ goto bad;
}
*pl = v[0];
status = 1;
@@ -1706,11 +1903,34 @@ TIFFFetchRefBlackWhite(TIFF* tif, TIFFDirEntry* dir)
}
/*
- * Replace a single strip (tile) of uncompressed data by
- * multiple strips (tiles), each approximately 8Kbytes.
- * This is useful for dealing with large images or
- * for dealing with machines with a limited amount
- * memory.
+ * Fetch and set the SubjectDistance EXIF tag.
+ */
+static int
+TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
+{
+ uint32 l[2];
+ float v;
+ int ok = 0;
+
+ if (TIFFFetchData(tif, dir, (char *)l)
+ && cvtRational(tif, dir, l[0], l[1], &v)) {
+ /*
+ * XXX: Numerator 0xFFFFFFFF means that we have infinite
+ * distance. Indicate that with a negative floating point
+ * SubjectDistance value.
+ */
+ ok = TIFFSetField(tif, dir->tdir_tag,
+ (l[0] != 0xFFFFFFFF) ? v : -v);
+ }
+
+ return ok;
+}
+
+/*
+ * Replace a single strip (tile) of uncompressed data by multiple strips
+ * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
+ * dealing with large images or for dealing with machines with a limited
+ * amount memory.
*/
static void
ChopUpSingleUncompressedStrip(TIFF* tif)
@@ -1752,8 +1972,8 @@ ChopUpSingleUncompressedStrip(TIFF* tif)
"for chopped \"StripOffsets\" array");
if (newcounts == NULL || newoffsets == NULL) {
/*
- * Unable to allocate new strip information, give
- * up and use the original one strip information.
+ * Unable to allocate new strip information, give up and use
+ * the original one strip information.
*/
if (newcounts != NULL)
_TIFFfree(newcounts);
@@ -1766,7 +1986,7 @@ ChopUpSingleUncompressedStrip(TIFF* tif)
* that reflect the broken-up format.
*/
for (strip = 0; strip < nstrips; strip++) {
- if (stripbytes > (tsize_t) bytecount)
+ if ((uint32)stripbytes > bytecount)
stripbytes = bytecount;
newcounts[strip] = stripbytes;
newoffsets[strip] = offset;
diff --git a/src/libtiff/tif_dirwrite.c b/src/libtiff/tif_dirwrite.c
index d775354..7d5ca71 100644
--- a/src/libtiff/tif_dirwrite.c
+++ b/src/libtiff/tif_dirwrite.c
@@ -1,4 +1,4 @@
-/* $Id: tif_dirwrite.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_dirwrite.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -100,31 +100,39 @@ _TIFFWriteDirectory(TIFF* tif, int done)
*/
if (done)
{
- if (tif->tif_flags & TIFF_POSTENCODE) {
- tif->tif_flags &= ~TIFF_POSTENCODE;
- if (!(*tif->tif_postencode)(tif)) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ tsize_t orig_rawcc = tif->tif_rawcc;
+
+ if (tif->tif_flags & TIFF_POSTENCODE) {
+ tif->tif_flags &= ~TIFF_POSTENCODE;
+ if (!(*tif->tif_postencode)(tif)) {
+ TIFFErrorExt(tif->tif_clientdata,
+ tif->tif_name,
"Error post-encoding before directory write");
- return (0);
- }
- }
- (*tif->tif_close)(tif); /* shutdown encoder */
- /*
- * Flush any data that might have been written
- * by the compression close+cleanup routines.
- */
- if (tif->tif_rawcc > 0 && !TIFFFlushData1(tif)) {
+ return (0);
+ }
+ }
+ (*tif->tif_close)(tif); /* shutdown encoder */
+ /*
+ * Flush any data that might have been written
+ * by the compression close+cleanup routines. But
+ * be careful not to write stuff if we didn't add data
+ * in the previous steps as the "rawcc" data may well be
+ * a previously read tile/strip in mixed read/write mode.
+ */
+ if (tif->tif_rawcc > 0 && tif->tif_rawcc != orig_rawcc
+ && (tif->tif_flags & TIFF_BEENWRITING) != 0
+ && !TIFFFlushData1(tif)) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "Error flushing data before directory write");
- return (0);
- }
- if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
- _TIFFfree(tif->tif_rawdata);
- tif->tif_rawdata = NULL;
- tif->tif_rawcc = 0;
- tif->tif_rawdatasize = 0;
- }
- tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
+ "Error flushing data before directory write");
+ return (0);
+ }
+ if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
+ _TIFFfree(tif->tif_rawdata);
+ tif->tif_rawdata = NULL;
+ tif->tif_rawcc = 0;
+ tif->tif_rawdatasize = 0;
+ }
+ tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
}
td = &tif->tif_dir;
@@ -137,12 +145,12 @@ _TIFFWriteDirectory(TIFF* tif, int done)
for (b = 0; b <= FIELD_LAST; b++)
if (TIFFFieldSet(tif, b) && b != FIELD_CUSTOM)
nfields += (b < FIELD_SUBFILETYPE ? 2 : 1);
- nfields += td->td_customValueCount;
+ nfields += td->td_customValueCount;
dirsize = nfields * sizeof (TIFFDirEntry);
data = (char*) _TIFFmalloc(dirsize);
if (data == NULL) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "Cannot write directory, out of space");
+ "Cannot write directory, out of space");
return (0);
}
/*
@@ -176,30 +184,29 @@ _TIFFWriteDirectory(TIFF* tif, int done)
for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) {
const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi];
- /*
- ** For custom fields, we test to see if the custom field
- ** is set or not. For normal fields, we just use the
- ** FieldSet test.
- */
- if( fip->field_bit == FIELD_CUSTOM )
- {
- int ci, is_set = FALSE;
+ /*
+ * For custom fields, we test to see if the custom field
+ * is set or not. For normal fields, we just use the
+ * FieldSet test.
+ */
+ if( fip->field_bit == FIELD_CUSTOM )
+ {
+ int ci, is_set = FALSE;
- for( ci = 0; ci < td->td_customValueCount; ci++ )
- is_set |= (td->td_customValues[ci].info == fip);
+ for( ci = 0; ci < td->td_customValueCount; ci++ )
+ is_set |= (td->td_customValues[ci].info == fip);
- if( !is_set )
- continue;
- }
+ if( !is_set )
+ continue;
+ }
else if (!FieldSet(fields, fip->field_bit))
- continue;
-
+ continue;
- /*
- ** Handle other fields.
- */
+ /*
+ * Handle other fields.
+ */
switch (fip->field_bit)
- {
+ {
case FIELD_STRIPOFFSETS:
/*
* We use one field bit for both strip and tile
@@ -234,8 +241,7 @@ _TIFFWriteDirectory(TIFF* tif, int done)
dir->tdir_tag = (uint16) tag;
dir->tdir_type = (uint16) TIFF_LONG;
dir->tdir_count = (uint32) td->td_nstrips;
- if (!TIFFWriteLongArray(tif, dir,
- td->td_stripbytecount))
+ if (!TIFFWriteLongArray(tif, dir, td->td_stripbytecount))
goto bad;
break;
case FIELD_ROWSPERSTRIP:
@@ -346,8 +352,8 @@ _TIFFWriteDirectory(TIFF* tif, int done)
}
dir++;
- if( fip->field_bit != FIELD_CUSTOM )
- ResetFieldBit(fields, fip->field_bit);
+ if( fip->field_bit != FIELD_CUSTOM )
+ ResetFieldBit(fields, fip->field_bit);
}
/*
@@ -376,15 +382,18 @@ _TIFFWriteDirectory(TIFF* tif, int done)
}
(void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET);
if (!WriteOK(tif, &dircount, sizeof (dircount))) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing directory count");
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error writing directory count");
goto bad;
}
if (!WriteOK(tif, data, dirsize)) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing directory contents");
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error writing directory contents");
goto bad;
}
- if (!WriteOK(tif, &diroff, sizeof (diroff))) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing directory link");
+ if (!WriteOK(tif, &diroff, sizeof (uint32))) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error writing directory link");
goto bad;
}
if (done) {
@@ -430,6 +439,133 @@ TIFFCheckpointDirectory(TIFF* tif)
return rc;
}
+static int
+_TIFFWriteCustomDirectory(TIFF* tif, toff_t *pdiroff)
+{
+ uint16 dircount;
+ uint32 nfields;
+ tsize_t dirsize;
+ char* data;
+ TIFFDirEntry* dir;
+ TIFFDirectory* td;
+ unsigned long b, fields[FIELD_SETLONGS];
+ int fi, nfi;
+
+ if (tif->tif_mode == O_RDONLY)
+ return (1);
+
+ td = &tif->tif_dir;
+ /*
+ * Size the directory so that we can calculate
+ * offsets for the data items that aren't kept
+ * in-place in each field.
+ */
+ nfields = 0;
+ for (b = 0; b <= FIELD_LAST; b++)
+ if (TIFFFieldSet(tif, b) && b != FIELD_CUSTOM)
+ nfields += (b < FIELD_SUBFILETYPE ? 2 : 1);
+ nfields += td->td_customValueCount;
+ dirsize = nfields * sizeof (TIFFDirEntry);
+ data = (char*) _TIFFmalloc(dirsize);
+ if (data == NULL) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Cannot write directory, out of space");
+ return (0);
+ }
+ /*
+ * Put the directory at the end of the file.
+ */
+ tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1;
+ tif->tif_dataoff = (toff_t)(
+ tif->tif_diroff + sizeof (uint16) + dirsize + sizeof (toff_t));
+ if (tif->tif_dataoff & 1)
+ tif->tif_dataoff++;
+ (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET);
+ dir = (TIFFDirEntry*) data;
+ /*
+ * Setup external form of directory
+ * entries and write data items.
+ */
+ _TIFFmemcpy(fields, td->td_fieldsset, sizeof (fields));
+
+ for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) {
+ const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi];
+
+ /*
+ * For custom fields, we test to see if the custom field
+ * is set or not. For normal fields, we just use the
+ * FieldSet test.
+ */
+ if( fip->field_bit == FIELD_CUSTOM )
+ {
+ int ci, is_set = FALSE;
+
+ for( ci = 0; ci < td->td_customValueCount; ci++ )
+ is_set |= (td->td_customValues[ci].info == fip);
+
+ if( !is_set )
+ continue;
+ }
+ else if (!FieldSet(fields, fip->field_bit))
+ continue;
+
+ if( fip->field_bit != FIELD_CUSTOM )
+ ResetFieldBit(fields, fip->field_bit);
+ }
+
+ /*
+ * Write directory.
+ */
+ dircount = (uint16) nfields;
+ *pdiroff = (uint32) tif->tif_nextdiroff;
+ if (tif->tif_flags & TIFF_SWAB) {
+ /*
+ * The file's byte order is opposite to the
+ * native machine architecture. We overwrite
+ * the directory information with impunity
+ * because it'll be released below after we
+ * write it to the file. Note that all the
+ * other tag construction routines assume that
+ * we do this byte-swapping; i.e. they only
+ * byte-swap indirect data.
+ */
+ for (dir = (TIFFDirEntry*) data; dircount; dir++, dircount--) {
+ TIFFSwabArrayOfShort(&dir->tdir_tag, 2);
+ TIFFSwabArrayOfLong(&dir->tdir_count, 2);
+ }
+ dircount = (uint16) nfields;
+ TIFFSwabShort(&dircount);
+ TIFFSwabLong(pdiroff);
+ }
+ (void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET);
+ if (!WriteOK(tif, &dircount, sizeof (dircount))) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error writing directory count");
+ goto bad;
+ }
+ if (!WriteOK(tif, data, dirsize)) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error writing directory contents");
+ goto bad;
+ }
+ if (!WriteOK(tif, pdiroff, sizeof (uint32))) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error writing directory link");
+ goto bad;
+ }
+ _TIFFfree(data);
+ return (1);
+bad:
+ _TIFFfree(data);
+ return (0);
+}
+
+int
+TIFFWriteCustomDirectory(TIFF* tif, toff_t *pdiroff)
+{
+ return _TIFFWriteCustomDirectory(tif, pdiroff);
+}
+
/*
* Process tags that are not special cased.
*/
@@ -581,7 +717,12 @@ TIFFWriteNormalTag(TIFF* tif, TIFFDirEntry* dir, const TIFFFieldInfo* fip)
{
char* cp;
if (fip->field_passcount)
- TIFFGetField(tif, fip->field_tag, &wc, &cp);
+ {
+ if( wc == (uint16) TIFF_VARIABLE2 )
+ TIFFGetField(tif, fip->field_tag, &wc2, &cp);
+ else
+ TIFFGetField(tif, fip->field_tag, &wc, &cp);
+ }
else
TIFFGetField(tif, fip->field_tag, &cp);
@@ -786,12 +927,27 @@ TIFFWriteShortTable(TIFF* tif,
static int
TIFFWriteByteArray(TIFF* tif, TIFFDirEntry* dir, char* cp)
{
- if (dir->tdir_count > 4) {
- if (!TIFFWriteData(tif, dir, cp))
- return (0);
+ if (dir->tdir_count <= 4) {
+ if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
+ dir->tdir_offset = (uint32)cp[0] << 24;
+ if (dir->tdir_count >= 2)
+ dir->tdir_offset |= (uint32)cp[1] << 16;
+ if (dir->tdir_count >= 3)
+ dir->tdir_offset |= (uint32)cp[2] << 8;
+ if (dir->tdir_count == 4)
+ dir->tdir_offset |= cp[3];
+ } else {
+ dir->tdir_offset = cp[0];
+ if (dir->tdir_count >= 2)
+ dir->tdir_offset |= (uint32) cp[1] << 8;
+ if (dir->tdir_count >= 3)
+ dir->tdir_offset |= (uint32) cp[2] << 16;
+ if (dir->tdir_count == 4)
+ dir->tdir_offset |= (uint32) cp[3] << 24;
+ }
+ return 1;
} else
- _TIFFmemcpy(&dir->tdir_offset, cp, dir->tdir_count);
- return (1);
+ return TIFFWriteData(tif, dir, cp);
}
/*
@@ -803,13 +959,13 @@ TIFFWriteShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
{
if (dir->tdir_count <= 2) {
if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
- dir->tdir_offset = (uint32) ((long) v[0] << 16);
+ dir->tdir_offset = (uint32) v[0] << 16;
if (dir->tdir_count == 2)
dir->tdir_offset |= v[1] & 0xffff;
} else {
dir->tdir_offset = v[0] & 0xffff;
if (dir->tdir_count == 2)
- dir->tdir_offset |= (long) v[1] << 16;
+ dir->tdir_offset |= (uint32) v[1] << 16;
}
return (1);
} else
@@ -854,10 +1010,11 @@ TIFFWriteRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
if (fv < 0) {
if (dir->tdir_type == TIFF_RATIONAL) {
- TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+ TIFFWarningExt(tif->tif_clientdata,
+ tif->tif_name,
"\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL",
_TIFFFieldWithTag(tif,dir->tdir_tag)->field_name,
- fv);
+ fv);
fv = 0;
} else
fv = -fv, sign = -1;
@@ -914,7 +1071,7 @@ TIFFWriteAnyArray(TIFF* tif,
w = (char*) _TIFFmalloc(n * TIFFDataWidth(type));
if (w == NULL) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "No space to write array");
+ "No space to write array");
return (0);
}
}
@@ -988,7 +1145,10 @@ TIFFWriteAnyArray(TIFF* tif,
}
break;
case TIFF_DOUBLE:
- return (TIFFWriteDoubleArray(tif, dir, v));
+ {
+ if( !TIFFWriteDoubleArray(tif, dir, v))
+ goto out;
+ }
default:
/* TIFF_NOTYPE */
/* TIFF_ASCII */
@@ -1073,8 +1233,9 @@ TIFFWriteData(TIFF* tif, TIFFDirEntry* dir, char* cp)
tif->tif_dataoff += (cc + 1) & ~1;
return (1);
}
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing data for field \"%s\"",
- _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error writing data for field \"%s\"",
+ _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
return (0);
}
@@ -1110,7 +1271,8 @@ TIFFRewriteDirectory( TIFF *tif )
if (!WriteOK(tif, &(tif->tif_header.tiff_diroff),
sizeof (tif->tif_diroff)))
{
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error updating TIFF header");
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error updating TIFF header");
return (0);
}
}
@@ -1124,7 +1286,8 @@ TIFFRewriteDirectory( TIFF *tif )
if (!SeekOK(tif, nextdir) ||
!ReadOK(tif, &dircount, sizeof (dircount))) {
- TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory count");
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory count");
return (0);
}
if (tif->tif_flags & TIFF_SWAB)
@@ -1132,7 +1295,8 @@ TIFFRewriteDirectory( TIFF *tif )
(void) TIFFSeekFile(tif,
dircount * sizeof (TIFFDirEntry), SEEK_CUR);
if (!ReadOK(tif, &nextdir, sizeof (nextdir))) {
- TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory link");
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory link");
return (0);
}
if (tif->tif_flags & TIFF_SWAB)
@@ -1142,7 +1306,8 @@ TIFFRewriteDirectory( TIFF *tif )
(void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET);
tif->tif_diroff = 0;
if (!WriteOK(tif, &(tif->tif_diroff), sizeof (nextdir))) {
- TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error writing directory link");
return (0);
}
}
@@ -1156,8 +1321,7 @@ TIFFRewriteDirectory( TIFF *tif )
/*
- * Link the current directory into the
- * directory chain for the file.
+ * Link the current directory into the directory chain for the file.
*/
static int
TIFFLinkDirectory(TIFF* tif)
@@ -1178,8 +1342,8 @@ TIFFLinkDirectory(TIFF* tif)
(void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
if (!WriteOK(tif, &diroff, sizeof (diroff))) {
TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Error writing SubIFD directory link",
- tif->tif_name);
+ "%s: Error writing SubIFD directory link",
+ tif->tif_name);
return (0);
}
/*
@@ -1203,7 +1367,8 @@ TIFFLinkDirectory(TIFF* tif)
(toff_t)(TIFF_MAGIC_SIZE+TIFF_VERSION_SIZE),
SEEK_SET);
if (!WriteOK(tif, &diroff, sizeof (diroff))) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing TIFF header");
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Error writing TIFF header");
return (0);
}
return (1);
@@ -1217,7 +1382,8 @@ TIFFLinkDirectory(TIFF* tif)
if (!SeekOK(tif, nextdir) ||
!ReadOK(tif, &dircount, sizeof (dircount))) {
- TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory count");
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory count");
return (0);
}
if (tif->tif_flags & TIFF_SWAB)
@@ -1225,7 +1391,8 @@ TIFFLinkDirectory(TIFF* tif)
(void) TIFFSeekFile(tif,
dircount * sizeof (TIFFDirEntry), SEEK_CUR);
if (!ReadOK(tif, &nextdir, sizeof (nextdir))) {
- TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory link");
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error fetching directory link");
return (0);
}
if (tif->tif_flags & TIFF_SWAB)
@@ -1234,7 +1401,8 @@ TIFFLinkDirectory(TIFF* tif)
off = TIFFSeekFile(tif, 0, SEEK_CUR); /* get current offset */
(void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET);
if (!WriteOK(tif, &diroff, sizeof (diroff))) {
- TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Error writing directory link");
return (0);
}
return (1);
diff --git a/src/libtiff/tif_dumpmode.c b/src/libtiff/tif_dumpmode.c
index d58a98b..cc9f999 100644
--- a/src/libtiff/tif_dumpmode.c
+++ b/src/libtiff/tif_dumpmode.c
@@ -1,4 +1,4 @@
-/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_dumpmode.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_dumpmode.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -45,7 +45,7 @@ DumpModeEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
if (tif->tif_rawcc + n > tif->tif_rawdatasize)
n = tif->tif_rawdatasize - tif->tif_rawcc;
- assert( n > 0 );
+ assert( n > 0 );
/*
* Avoid copy if client has setup raw
@@ -71,6 +71,8 @@ static int
DumpModeDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
{
(void) s;
+/* fprintf(stderr,"DumpModeDecode: scanline %ld, expected %ld bytes, got %ld bytes\n", */
+/* (long) tif->tif_row, (long) tif->tif_rawcc, (long) cc); */
if (tif->tif_rawcc < cc) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
"DumpModeDecode: Not enough data for scanline %d",
diff --git a/src/libtiff/tif_error.c b/src/libtiff/tif_error.c
index 456e6f9..37dadf0 100644
--- a/src/libtiff/tif_error.c
+++ b/src/libtiff/tif_error.c
@@ -1,4 +1,4 @@
-/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_error.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_error.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
diff --git a/src/libtiff/tif_extension.c b/src/libtiff/tif_extension.c
index 0d32a07..39678d7 100644
--- a/src/libtiff/tif_extension.c
+++ b/src/libtiff/tif_extension.c
@@ -1,4 +1,4 @@
-/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_extension.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_extension.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
diff --git a/src/libtiff/tif_fax3.c b/src/libtiff/tif_fax3.c
index 96f5d13..3116900 100644
--- a/src/libtiff/tif_fax3.c
+++ b/src/libtiff/tif_fax3.c
@@ -1,4 +1,4 @@
-/* $Id: tif_fax3.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_fax3.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1990-1997 Sam Leffler
@@ -63,6 +63,7 @@ typedef struct {
char* faxdcs; /* Table 2/T.30 encoded session params */
TIFFVGetMethod vgetparent; /* super-class method */
TIFFVSetMethod vsetparent; /* super-class method */
+ TIFFPrintMethod printdir; /* super-class method */
} Fax3BaseState;
#define Fax3State(tif) ((Fax3BaseState*) (tif)->tif_data)
@@ -85,6 +86,8 @@ typedef struct {
unsigned char* refline; /* reference line for 2d decoding */
int k; /* #rows left that can be 2d encoded */
int maxk; /* max #rows that can be 2d encoded */
+
+ int line;
} Fax3CodecState;
#define DecoderState(tif) ((Fax3CodecState*) Fax3State(tif))
#define EncoderState(tif) ((Fax3CodecState*) Fax3State(tif))
@@ -167,6 +170,7 @@ Fax3PreDecode(TIFF* tif, tsample_t s)
sp->refruns[0] = (uint32) sp->b.rowpixels;
sp->refruns[1] = 0;
}
+ sp->line = 0;
return (1);
}
@@ -179,46 +183,46 @@ Fax3PreDecode(TIFF* tif, tsample_t s)
static void
Fax3Unexpected(const char* module, TIFF* tif, uint32 line, uint32 a0)
{
- TIFFErrorExt(tif->tif_clientdata, module, "%s: Bad code word at line %lu of %s %lu (x %lu)",
- tif->tif_name, (unsigned long) line, isTiled(tif) ? "tile" : "strip",
- (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
- (unsigned long) a0);
+ TIFFErrorExt(tif->tif_clientdata, module, "%s: Bad code word at line %u of %s %u (x %u)",
+ tif->tif_name, line, isTiled(tif) ? "tile" : "strip",
+ (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+ a0);
}
-#define unexpected(table, a0) Fax3Unexpected(module, tif, line, a0)
+#define unexpected(table, a0) Fax3Unexpected(module, tif, sp->line, a0)
static void
Fax3Extension(const char* module, TIFF* tif, uint32 line, uint32 a0)
{
TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Uncompressed data (not supported) at line %lu of %s %lu (x %lu)",
- tif->tif_name, (unsigned long) line, isTiled(tif) ? "tile" : "strip",
- (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
- (unsigned long) a0);
+ "%s: Uncompressed data (not supported) at line %u of %s %u (x %u)",
+ tif->tif_name, line, isTiled(tif) ? "tile" : "strip",
+ (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+ a0);
}
-#define extension(a0) Fax3Extension(module, tif, line, a0)
+#define extension(a0) Fax3Extension(module, tif, sp->line, a0)
static void
Fax3BadLength(const char* module, TIFF* tif, uint32 line, uint32 a0, uint32 lastx)
{
- TIFFWarningExt(tif->tif_clientdata, module, "%s: %s at line %lu of %s %lu (got %lu, expected %lu)",
- tif->tif_name,
- a0 < lastx ? "Premature EOL" : "Line length mismatch",
- (unsigned long) line, isTiled(tif) ? "tile" : "strip",
- (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
- (unsigned long) a0, lastx);
+ TIFFWarningExt(tif->tif_clientdata, module, "%s: %s at line %u of %s %u (got %u, expected %u)",
+ tif->tif_name,
+ a0 < lastx ? "Premature EOL" : "Line length mismatch",
+ line, isTiled(tif) ? "tile" : "strip",
+ (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+ a0, lastx);
}
-#define badlength(a0,lastx) Fax3BadLength(module, tif, line, a0, lastx)
+#define badlength(a0,lastx) Fax3BadLength(module, tif, sp->line, a0, lastx)
static void
Fax3PrematureEOF(const char* module, TIFF* tif, uint32 line, uint32 a0)
{
- TIFFWarningExt(tif->tif_clientdata, module, "%s: Premature EOF at line %lu of %s %lu (x %lu)",
+ TIFFWarningExt(tif->tif_clientdata, module, "%s: Premature EOF at line %u of %s %u (x %u)",
tif->tif_name,
- (unsigned long) line, isTiled(tif) ? "tile" : "strip",
- (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
- (unsigned long) a0);
+ line, isTiled(tif) ? "tile" : "strip",
+ (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+ a0);
}
-#define prematureEOF(a0) Fax3PrematureEOF(module, tif, line, a0)
+#define prematureEOF(a0) Fax3PrematureEOF(module, tif, sp->line, a0)
#define Nop
@@ -229,7 +233,6 @@ static int
Fax3Decode1D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
{
DECLARE_STATE(tif, sp, "Fax3Decode1D");
- int line = 0;
(void) s;
CACHE_STATE(tif, sp);
@@ -248,7 +251,7 @@ Fax3Decode1D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
(*sp->fill)(buf, thisrun, pa, lastx);
buf += sp->b.rowbytes;
occ -= sp->b.rowbytes;
- line++;
+ sp->line++;
continue;
EOF1D: /* premature EOF */
CLEANUP_RUNS();
@@ -269,7 +272,6 @@ static int
Fax3Decode2D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
{
DECLARE_STATE_2D(tif, sp, "Fax3Decode2D");
- int line = 0;
int is1D; /* current line is 1d/2d-encoded */
(void) s;
@@ -302,7 +304,7 @@ Fax3Decode2D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
SWAP(uint32*, sp->curruns, sp->refruns);
buf += sp->b.rowbytes;
occ -= sp->b.rowbytes;
- line++;
+ sp->line++;
continue;
EOF2D: /* premature EOF */
CLEANUP_RUNS();
@@ -492,14 +494,14 @@ Fax3SetupState(TIFF* tif)
);
nruns = needsRefLine ? 2*TIFFroundup(rowpixels,32) : rowpixels;
-
- dsp->runs = (uint32*) _TIFFCheckMalloc(tif, 2*nruns+3, sizeof (uint32),
+ nruns += 3;
+ dsp->runs = (uint32*) _TIFFCheckMalloc(tif, 2*nruns, sizeof (uint32),
"for Group 3/4 run arrays");
if (dsp->runs == NULL)
return (0);
dsp->curruns = dsp->runs;
if (needsRefLine)
- dsp->refruns = dsp->runs + (nruns>>1);
+ dsp->refruns = dsp->runs + nruns;
else
dsp->refruns = NULL;
if (td->td_compression == COMPRESSION_CCITTFAX3
@@ -718,6 +720,7 @@ Fax3PreEncode(TIFF* tif, tsample_t s)
sp->k = sp->maxk-1;
} else
sp->k = sp->maxk = 0;
+ sp->line = 0;
return (1);
}
@@ -773,7 +776,7 @@ static int32 find1span(unsigned char*, int32, int32);
* table. The ``base'' of the bit string is supplied
* along with the start+end bit indices.
*/
-inline static int32
+static int32
find0span(unsigned char* bp, int32 bs, int32 be)
{
int32 bits = be - bs;
@@ -832,7 +835,7 @@ find0span(unsigned char* bp, int32 bs, int32 be)
return (span);
}
-inline static int32
+static int32
find1span(unsigned char* bp, int32 bs, int32 be)
{
int32 bits = be - bs;
@@ -1074,6 +1077,7 @@ Fax3Cleanup(TIFF* tif)
tif->tif_tagmethods.vgetfield = sp->b.vgetparent;
tif->tif_tagmethods.vsetfield = sp->b.vsetparent;
+ tif->tif_tagmethods.printdir = sp->b.printdir;
if (sp->runs)
_TIFFfree(sp->runs);
@@ -1082,6 +1086,9 @@ Fax3Cleanup(TIFF* tif)
if (Fax3State(tif)->subaddress)
_TIFFfree(Fax3State(tif)->subaddress);
+ if (Fax3State(tif)->faxdcs)
+ _TIFFfree(Fax3State(tif)->faxdcs);
+
_TIFFfree(tif->tif_data);
tif->tif_data = NULL;
@@ -1136,6 +1143,7 @@ static int
Fax3VSetField(TIFF* tif, ttag_t tag, va_list ap)
{
Fax3BaseState* sp = Fax3State(tif);
+ const TIFFFieldInfo* fip;
assert(sp != 0);
assert(sp->vsetparent != 0);
@@ -1143,10 +1151,10 @@ Fax3VSetField(TIFF* tif, ttag_t tag, va_list ap)
switch (tag) {
case TIFFTAG_FAXMODE:
sp->mode = va_arg(ap, int);
- return (1); /* NB: pseudo tag */
+ return 1; /* NB: pseudo tag */
case TIFFTAG_FAXFILLFUNC:
DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc);
- return (1); /* NB: pseudo tag */
+ return 1; /* NB: pseudo tag */
case TIFFTAG_GROUP3OPTIONS:
/* XXX: avoid reading options if compression mismatches. */
if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)
@@ -1181,9 +1189,14 @@ Fax3VSetField(TIFF* tif, ttag_t tag, va_list ap)
default:
return (*sp->vsetparent)(tif, tag, ap);
}
- TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
+
+ if ((fip = _TIFFFieldWithTag(tif, tag)))
+ TIFFSetFieldBit(tif, fip->field_bit);
+ else
+ return 0;
+
tif->tif_flags |= TIFF_DIRTYDIRECT;
- return (1);
+ return 1;
}
static int
@@ -1191,6 +1204,8 @@ Fax3VGetField(TIFF* tif, ttag_t tag, va_list ap)
{
Fax3BaseState* sp = Fax3State(tif);
+ assert(sp != 0);
+
switch (tag) {
case TIFFTAG_FAXMODE:
*va_arg(ap, int*) = sp->mode;
@@ -1234,6 +1249,8 @@ Fax3PrintDir(TIFF* tif, FILE* fd, long flags)
{
Fax3BaseState* sp = Fax3State(tif);
+ assert(sp != 0);
+
(void) flags;
if (TIFFFieldSet(tif,FIELD_OPTIONS)) {
const char* sep = " ";
@@ -1295,6 +1312,15 @@ InitCCITTFax3(TIFF* tif)
Fax3BaseState* sp;
/*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFieldInfo(tif, faxFieldInfo, N(faxFieldInfo))) {
+ TIFFErrorExt(tif->tif_clientdata, "InitCCITTFax3",
+ "Merging common CCITT Fax codec-specific tags failed");
+ return 0;
+ }
+
+ /*
* Allocate state block so tag methods have storage to record values.
*/
tif->tif_data = (tidata_t)
@@ -1310,14 +1336,13 @@ InitCCITTFax3(TIFF* tif)
sp->rw_mode = tif->tif_mode;
/*
- * Merge codec-specific tag information and
- * override parent get/set field methods.
+ * Override parent get/set field methods.
*/
- _TIFFMergeFieldInfo(tif, faxFieldInfo, N(faxFieldInfo));
sp->vgetparent = tif->tif_tagmethods.vgetfield;
tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */
sp->vsetparent = tif->tif_tagmethods.vsetfield;
tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */
+ sp->printdir = tif->tif_tagmethods.printdir;
tif->tif_tagmethods.printdir = Fax3PrintDir; /* hook for codec tags */
sp->groupoptions = 0;
sp->recvparams = 0;
@@ -1355,14 +1380,21 @@ TIFFInitCCITTFax3(TIFF* tif, int scheme)
{
(void) scheme;
if (InitCCITTFax3(tif)) {
- _TIFFMergeFieldInfo(tif, fax3FieldInfo, N(fax3FieldInfo));
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFieldInfo(tif, fax3FieldInfo, N(fax3FieldInfo))) {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax3",
+ "Merging CCITT Fax 3 codec-specific tags failed");
+ return 0;
+ }
/*
* The default format is Class/F-style w/o RTC.
*/
return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
} else
- return (0);
+ return 01;
}
/*
@@ -1378,7 +1410,6 @@ static int
Fax4Decode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
{
DECLARE_STATE_2D(tif, sp, "Fax4Decode");
- int line = 0;
(void) s;
CACHE_STATE(tif, sp);
@@ -1401,7 +1432,7 @@ Fax4Decode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
SWAP(uint32*, sp->curruns, sp->refruns);
buf += sp->b.rowbytes;
occ -= sp->b.rowbytes;
- line++;
+ sp->line++;
continue;
EOFG4:
NeedBits16( 13, BADG4 );
@@ -1457,7 +1488,14 @@ TIFFInitCCITTFax4(TIFF* tif, int scheme)
{
(void) scheme;
if (InitCCITTFax3(tif)) { /* reuse G3 support */
- _TIFFMergeFieldInfo(tif, fax4FieldInfo, N(fax4FieldInfo));
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFieldInfo(tif, fax4FieldInfo, N(fax4FieldInfo))) {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax4",
+ "Merging CCITT Fax 4 codec-specific tags failed");
+ return 0;
+ }
tif->tif_decoderow = Fax4Decode;
tif->tif_decodestrip = Fax4Decode;
@@ -1487,7 +1525,6 @@ Fax3DecodeRLE(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
{
DECLARE_STATE(tif, sp, "Fax3DecodeRLE");
int mode = sp->b.mode;
- int line = 0;
(void) s;
CACHE_STATE(tif, sp);
@@ -1517,7 +1554,7 @@ Fax3DecodeRLE(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
}
buf += sp->b.rowbytes;
occ -= sp->b.rowbytes;
- line++;
+ sp->line++;
continue;
EOFRLE: /* premature EOF */
(*sp->fill)(buf, thisrun, pa, lastx);
diff --git a/src/libtiff/tif_fax3.h b/src/libtiff/tif_fax3.h
index 99ced5f..7fd2205 100644
--- a/src/libtiff/tif_fax3.h
+++ b/src/libtiff/tif_fax3.h
@@ -1,4 +1,4 @@
-/* $Id: tif_fax3.h,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_fax3.h,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1990-1997 Sam Leffler
diff --git a/src/libtiff/tif_flush.c b/src/libtiff/tif_flush.c
index 140a58a..4893b9a 100644
--- a/src/libtiff/tif_flush.c
+++ b/src/libtiff/tif_flush.c
@@ -1,4 +1,4 @@
-/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_flush.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_flush.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
diff --git a/src/libtiff/tif_getimage.c b/src/libtiff/tif_getimage.c
index 251733c..862fdb2 100644
--- a/src/libtiff/tif_getimage.c
+++ b/src/libtiff/tif_getimage.c
@@ -1,4 +1,4 @@
-/* $Id: tif_getimage.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_getimage.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1991-1997 Sam Leffler
@@ -32,14 +32,13 @@
#include "tiffiop.h"
#include <stdio.h>
-static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
-static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
-static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
-static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
-static int pickTileContigCase(TIFFRGBAImage*);
-static int pickTileSeparateCase(TIFFRGBAImage*);
-
-static const char photoTag[] = "PhotometricInterpretation";
+static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int PickContigCase(TIFFRGBAImage*);
+static int PickSeparateCase(TIFFRGBAImage*);
+static const char photoTag[] = "PhotometricInterpretation";
/*
* Helper constants used in Orientation tag handling
@@ -72,118 +71,122 @@ TIFFDisplay display_sRGB = {
int
TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
{
- TIFFDirectory* td = &tif->tif_dir;
- uint16 photometric;
- int colorchannels;
+ TIFFDirectory* td = &tif->tif_dir;
+ uint16 photometric;
+ int colorchannels;
- if (!tif->tif_decodestatus) {
- sprintf(emsg, "Sorry, requested compression method is not configured");
- return (0);
- }
- switch (td->td_bitspersample) {
- case 1: case 2: case 4:
- case 8: case 16:
- break;
- default:
- sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
- td->td_bitspersample);
- return (0);
- }
- colorchannels = td->td_samplesperpixel - td->td_extrasamples;
- if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
- switch (colorchannels) {
- case 1:
- photometric = PHOTOMETRIC_MINISBLACK;
- break;
- case 3:
- photometric = PHOTOMETRIC_RGB;
- break;
- default:
- sprintf(emsg, "Missing needed %s tag", photoTag);
- return (0);
- }
- }
- switch (photometric) {
- case PHOTOMETRIC_MINISWHITE:
- case PHOTOMETRIC_MINISBLACK:
- case PHOTOMETRIC_PALETTE:
- if (td->td_planarconfig == PLANARCONFIG_CONTIG
- && td->td_samplesperpixel != 1
- && td->td_bitspersample < 8 ) {
- sprintf(emsg,
- "Sorry, can not handle contiguous data with %s=%d, "
- "and %s=%d and Bits/Sample=%d",
- photoTag, photometric,
- "Samples/pixel", td->td_samplesperpixel,
- td->td_bitspersample);
- return (0);
- }
- /*
- ** We should likely validate that any extra samples are either
- ** to be ignored, or are alpha, and if alpha we should try to use
- ** them. But for now we won't bother with this.
- */
- break;
- case PHOTOMETRIC_YCBCR:
- if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
- sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
- "Planarconfiguration", td->td_planarconfig);
- return (0);
+ if (!tif->tif_decodestatus) {
+ sprintf(emsg, "Sorry, requested compression method is not configured");
+ return (0);
}
- break;
- case PHOTOMETRIC_RGB:
- if (colorchannels < 3) {
- sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
- "Color channels", colorchannels);
- return (0);
+ switch (td->td_bitspersample) {
+ case 1:
+ case 2:
+ case 4:
+ case 8:
+ case 16:
+ break;
+ default:
+ sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
+ td->td_bitspersample);
+ return (0);
}
- break;
- case PHOTOMETRIC_SEPARATED:
- {
- uint16 inkset;
- TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
- if (inkset != INKSET_CMYK) {
- sprintf(emsg,
- "Sorry, can not handle separated image with %s=%d",
- "InkSet", inkset);
- return 0;
+ colorchannels = td->td_samplesperpixel - td->td_extrasamples;
+ if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
+ switch (colorchannels) {
+ case 1:
+ photometric = PHOTOMETRIC_MINISBLACK;
+ break;
+ case 3:
+ photometric = PHOTOMETRIC_RGB;
+ break;
+ default:
+ sprintf(emsg, "Missing needed %s tag", photoTag);
+ return (0);
}
- if (td->td_samplesperpixel < 4) {
- sprintf(emsg,
- "Sorry, can not handle separated image with %s=%d",
- "Samples/pixel", td->td_samplesperpixel);
- return 0;
- }
- break;
- }
- case PHOTOMETRIC_LOGL:
- if (td->td_compression != COMPRESSION_SGILOG) {
- sprintf(emsg, "Sorry, LogL data must have %s=%d",
- "Compression", COMPRESSION_SGILOG);
- return (0);
- }
- break;
- case PHOTOMETRIC_LOGLUV:
- if (td->td_compression != COMPRESSION_SGILOG &&
- td->td_compression != COMPRESSION_SGILOG24) {
- sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
- "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
- return (0);
}
- if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
- sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
- "Planarconfiguration", td->td_planarconfig);
- return (0);
+ switch (photometric) {
+ case PHOTOMETRIC_MINISWHITE:
+ case PHOTOMETRIC_MINISBLACK:
+ case PHOTOMETRIC_PALETTE:
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG
+ && td->td_samplesperpixel != 1
+ && td->td_bitspersample < 8 ) {
+ sprintf(emsg,
+ "Sorry, can not handle contiguous data with %s=%d, "
+ "and %s=%d and Bits/Sample=%d",
+ photoTag, photometric,
+ "Samples/pixel", td->td_samplesperpixel,
+ td->td_bitspersample);
+ return (0);
+ }
+ /*
+ * We should likely validate that any extra samples are either
+ * to be ignored, or are alpha, and if alpha we should try to use
+ * them. But for now we won't bother with this.
+ */
+ break;
+ case PHOTOMETRIC_YCBCR:
+ /*
+ * TODO: if at all meaningful and useful, make more complete
+ * support check here, or better still, refactor to let supporting
+ * code decide whether there is support and what meaningfull
+ * error to return
+ */
+ break;
+ case PHOTOMETRIC_RGB:
+ if (colorchannels < 3) {
+ sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
+ "Color channels", colorchannels);
+ return (0);
+ }
+ break;
+ case PHOTOMETRIC_SEPARATED:
+ {
+ uint16 inkset;
+ TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
+ if (inkset != INKSET_CMYK) {
+ sprintf(emsg,
+ "Sorry, can not handle separated image with %s=%d",
+ "InkSet", inkset);
+ return 0;
+ }
+ if (td->td_samplesperpixel < 4) {
+ sprintf(emsg,
+ "Sorry, can not handle separated image with %s=%d",
+ "Samples/pixel", td->td_samplesperpixel);
+ return 0;
+ }
+ break;
+ }
+ case PHOTOMETRIC_LOGL:
+ if (td->td_compression != COMPRESSION_SGILOG) {
+ sprintf(emsg, "Sorry, LogL data must have %s=%d",
+ "Compression", COMPRESSION_SGILOG);
+ return (0);
+ }
+ break;
+ case PHOTOMETRIC_LOGLUV:
+ if (td->td_compression != COMPRESSION_SGILOG &&
+ td->td_compression != COMPRESSION_SGILOG24) {
+ sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
+ "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
+ return (0);
+ }
+ if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
+ sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
+ "Planarconfiguration", td->td_planarconfig);
+ return (0);
+ }
+ break;
+ case PHOTOMETRIC_CIELAB:
+ break;
+ default:
+ sprintf(emsg, "Sorry, can not handle image with %s=%d",
+ photoTag, photometric);
+ return (0);
}
- break;
- case PHOTOMETRIC_CIELAB:
- break;
- default:
- sprintf(emsg, "Sorry, can not handle image with %s=%d",
- photoTag, photometric);
- return (0);
- }
- return (1);
+ return (1);
}
void
@@ -199,7 +202,6 @@ TIFFRGBAImageEnd(TIFFRGBAImage* img)
_TIFFfree(img->ycbcr), img->ycbcr = NULL;
if (img->cielab)
_TIFFfree(img->cielab), img->cielab = NULL;
-
if( img->redcmap ) {
_TIFFfree( img->redcmap );
_TIFFfree( img->greencmap );
@@ -221,221 +223,227 @@ isCCITTCompression(TIFF* tif)
int
TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
{
- uint16* sampleinfo;
- uint16 extrasamples;
- uint16 planarconfig;
- uint16 compress;
- int colorchannels;
- uint16 *red_orig, *green_orig, *blue_orig;
- int n_color;
-
- /* Initialize to normal values */
- img->row_offset = 0;
- img->col_offset = 0;
- img->redcmap = NULL;
- img->greencmap = NULL;
- img->bluecmap = NULL;
- img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
-
- img->tif = tif;
- img->stoponerr = stop;
- TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
- switch (img->bitspersample) {
- case 1: case 2: case 4:
- case 8: case 16:
- break;
- default:
- sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
- img->bitspersample);
- return (0);
- }
- img->alpha = 0;
- TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
- TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
- &extrasamples, &sampleinfo);
- if (extrasamples >= 1)
- {
- switch (sampleinfo[0]) {
- case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without */
- if (img->samplesperpixel > 3) /* correct info about alpha channel */
- img->alpha = EXTRASAMPLE_ASSOCALPHA;
- break;
- case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
- case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
- img->alpha = sampleinfo[0];
- break;
+ uint16* sampleinfo;
+ uint16 extrasamples;
+ uint16 planarconfig;
+ uint16 compress;
+ int colorchannels;
+ uint16 *red_orig, *green_orig, *blue_orig;
+ int n_color;
+
+ /* Initialize to normal values */
+ img->row_offset = 0;
+ img->col_offset = 0;
+ img->redcmap = NULL;
+ img->greencmap = NULL;
+ img->bluecmap = NULL;
+ img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
+
+ img->tif = tif;
+ img->stoponerr = stop;
+ TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
+ switch (img->bitspersample) {
+ case 1:
+ case 2:
+ case 4:
+ case 8:
+ case 16:
+ break;
+ default:
+ sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
+ img->bitspersample);
+ return (0);
+ }
+ img->alpha = 0;
+ TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
+ TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
+ &extrasamples, &sampleinfo);
+ if (extrasamples >= 1)
+ {
+ switch (sampleinfo[0]) {
+ case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without */
+ if (img->samplesperpixel > 3) /* correct info about alpha channel */
+ img->alpha = EXTRASAMPLE_ASSOCALPHA;
+ break;
+ case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
+ case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
+ img->alpha = sampleinfo[0];
+ break;
+ }
}
- }
#ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
- if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
- img->photometric = PHOTOMETRIC_MINISWHITE;
-
- if( extrasamples == 0
- && img->samplesperpixel == 4
- && img->photometric == PHOTOMETRIC_RGB )
- {
- img->alpha = EXTRASAMPLE_ASSOCALPHA;
- extrasamples = 1;
- }
-#endif
-
- colorchannels = img->samplesperpixel - extrasamples;
- TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
- TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
- if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
- switch (colorchannels) {
- case 1:
- if (isCCITTCompression(tif))
+ if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
img->photometric = PHOTOMETRIC_MINISWHITE;
- else
- img->photometric = PHOTOMETRIC_MINISBLACK;
- break;
- case 3:
- img->photometric = PHOTOMETRIC_RGB;
- break;
- default:
- sprintf(emsg, "Missing needed %s tag", photoTag);
- return (0);
- }
- }
- switch (img->photometric) {
- case PHOTOMETRIC_PALETTE:
- if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
- &red_orig, &green_orig, &blue_orig)) {
- sprintf(emsg, "Missing required \"Colormap\" tag");
- return (0);
- }
-
- /* copy the colormaps so we can modify them */
- n_color = (1L << img->bitspersample);
- img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
- img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
- img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
- if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
- sprintf(emsg, "Out of memory for colormap copy");
- return (0);
- }
- _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
- _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
- _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
-
- /* fall thru... */
- case PHOTOMETRIC_MINISWHITE:
- case PHOTOMETRIC_MINISBLACK:
- if (planarconfig == PLANARCONFIG_CONTIG
- && img->samplesperpixel != 1
- && img->bitspersample < 8 ) {
- sprintf(emsg,
- "Sorry, can not handle contiguous data with %s=%d, "
- "and %s=%d and Bits/Sample=%d",
- photoTag, img->photometric,
- "Samples/pixel", img->samplesperpixel,
- img->bitspersample);
- return (0);
- }
- break;
- case PHOTOMETRIC_YCBCR:
- if (planarconfig != PLANARCONFIG_CONTIG) {
- sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
- "Planarconfiguration", planarconfig);
- return (0);
+ if( extrasamples == 0
+ && img->samplesperpixel == 4
+ && img->photometric == PHOTOMETRIC_RGB )
+ {
+ img->alpha = EXTRASAMPLE_ASSOCALPHA;
+ extrasamples = 1;
}
- /* It would probably be nice to have a reality check here. */
- if (planarconfig == PLANARCONFIG_CONTIG)
- /* can rely on libjpeg to convert to RGB */
- /* XXX should restore current state on exit */
- switch (compress) {
- case COMPRESSION_OJPEG:
- case COMPRESSION_JPEG:
- TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
- img->photometric = PHOTOMETRIC_RGB;
- break;
+#endif
- default:
- /* do nothing */;
- break;
- }
- break;
- case PHOTOMETRIC_RGB:
- if (colorchannels < 3) {
- sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
- "Color channels", colorchannels);
- return (0);
- }
- break;
- case PHOTOMETRIC_SEPARATED: {
- uint16 inkset;
- TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
- if (inkset != INKSET_CMYK) {
- sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
- "InkSet", inkset);
- return (0);
- }
- if (img->samplesperpixel < 4) {
- sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
- "Samples/pixel", img->samplesperpixel);
- return (0);
- }
- break;
- }
- case PHOTOMETRIC_LOGL:
- if (compress != COMPRESSION_SGILOG) {
- sprintf(emsg, "Sorry, LogL data must have %s=%d",
- "Compression", COMPRESSION_SGILOG);
- return (0);
- }
- TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
- img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
- img->bitspersample = 8;
- break;
- case PHOTOMETRIC_LOGLUV:
- if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
- sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
- "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
- return (0);
- }
- if (planarconfig != PLANARCONFIG_CONTIG) {
- sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
- "Planarconfiguration", planarconfig);
- return (0);
+ colorchannels = img->samplesperpixel - extrasamples;
+ TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
+ TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
+ if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
+ switch (colorchannels) {
+ case 1:
+ if (isCCITTCompression(tif))
+ img->photometric = PHOTOMETRIC_MINISWHITE;
+ else
+ img->photometric = PHOTOMETRIC_MINISBLACK;
+ break;
+ case 3:
+ img->photometric = PHOTOMETRIC_RGB;
+ break;
+ default:
+ sprintf(emsg, "Missing needed %s tag", photoTag);
+ return (0);
+ }
}
- TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
- img->photometric = PHOTOMETRIC_RGB; /* little white lie */
- img->bitspersample = 8;
- break;
- case PHOTOMETRIC_CIELAB:
- break;
- default:
- sprintf(emsg, "Sorry, can not handle image with %s=%d",
- photoTag, img->photometric);
- return (0);
- }
- img->Map = NULL;
- img->BWmap = NULL;
- img->PALmap = NULL;
- img->ycbcr = NULL;
- img->cielab = NULL;
- TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
- TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
- TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
- img->isContig =
- !(planarconfig == PLANARCONFIG_SEPARATE && colorchannels > 1);
- if (img->isContig) {
- img->get = TIFFIsTiled(tif) ? gtTileContig : gtStripContig;
- if (!pickTileContigCase(img)) {
- sprintf(emsg, "Sorry, can not handle image");
- return 0;
+ switch (img->photometric) {
+ case PHOTOMETRIC_PALETTE:
+ if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
+ &red_orig, &green_orig, &blue_orig)) {
+ sprintf(emsg, "Missing required \"Colormap\" tag");
+ return (0);
+ }
+
+ /* copy the colormaps so we can modify them */
+ n_color = (1L << img->bitspersample);
+ img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
+ img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
+ img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
+ if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
+ sprintf(emsg, "Out of memory for colormap copy");
+ return (0);
+ }
+
+ _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
+ _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
+ _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
+
+ /* fall thru... */
+ case PHOTOMETRIC_MINISWHITE:
+ case PHOTOMETRIC_MINISBLACK:
+ if (planarconfig == PLANARCONFIG_CONTIG
+ && img->samplesperpixel != 1
+ && img->bitspersample < 8 ) {
+ sprintf(emsg,
+ "Sorry, can not handle contiguous data with %s=%d, "
+ "and %s=%d and Bits/Sample=%d",
+ photoTag, img->photometric,
+ "Samples/pixel", img->samplesperpixel,
+ img->bitspersample);
+ return (0);
+ }
+ break;
+ case PHOTOMETRIC_YCBCR:
+ /* It would probably be nice to have a reality check here. */
+ if (planarconfig == PLANARCONFIG_CONTIG)
+ /* can rely on libjpeg to convert to RGB */
+ /* XXX should restore current state on exit */
+ switch (compress) {
+ case COMPRESSION_JPEG:
+ /*
+ * TODO: when complete tests verify complete desubsampling
+ * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
+ * favor of tif_getimage.c native handling
+ */
+ TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
+ img->photometric = PHOTOMETRIC_RGB;
+ break;
+ default:
+ /* do nothing */;
+ break;
+ }
+ /*
+ * TODO: if at all meaningful and useful, make more complete
+ * support check here, or better still, refactor to let supporting
+ * code decide whether there is support and what meaningfull
+ * error to return
+ */
+ break;
+ case PHOTOMETRIC_RGB:
+ if (colorchannels < 3) {
+ sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
+ "Color channels", colorchannels);
+ return (0);
+ }
+ break;
+ case PHOTOMETRIC_SEPARATED:
+ {
+ uint16 inkset;
+ TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
+ if (inkset != INKSET_CMYK) {
+ sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
+ "InkSet", inkset);
+ return (0);
+ }
+ if (img->samplesperpixel < 4) {
+ sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
+ "Samples/pixel", img->samplesperpixel);
+ return (0);
+ }
+ }
+ break;
+ case PHOTOMETRIC_LOGL:
+ if (compress != COMPRESSION_SGILOG) {
+ sprintf(emsg, "Sorry, LogL data must have %s=%d",
+ "Compression", COMPRESSION_SGILOG);
+ return (0);
+ }
+ TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
+ img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
+ img->bitspersample = 8;
+ break;
+ case PHOTOMETRIC_LOGLUV:
+ if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
+ sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
+ "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
+ return (0);
+ }
+ if (planarconfig != PLANARCONFIG_CONTIG) {
+ sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
+ "Planarconfiguration", planarconfig);
+ return (0);
+ }
+ TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
+ img->photometric = PHOTOMETRIC_RGB; /* little white lie */
+ img->bitspersample = 8;
+ break;
+ case PHOTOMETRIC_CIELAB:
+ break;
+ default:
+ sprintf(emsg, "Sorry, can not handle image with %s=%d",
+ photoTag, img->photometric);
+ return (0);
}
- } else {
- img->get = TIFFIsTiled(tif) ? gtTileSeparate : gtStripSeparate;
- if (!pickTileSeparateCase(img)) {
- sprintf(emsg, "Sorry, can not handle image");
- return 0;
+ img->Map = NULL;
+ img->BWmap = NULL;
+ img->PALmap = NULL;
+ img->ycbcr = NULL;
+ img->cielab = NULL;
+ TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
+ TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
+ TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
+ img->isContig =
+ !(planarconfig == PLANARCONFIG_SEPARATE && colorchannels > 1);
+ if (img->isContig) {
+ if (!PickContigCase(img)) {
+ sprintf(emsg, "Sorry, can not handle image");
+ return 0;
+ }
+ } else {
+ if (!PickSeparateCase(img)) {
+ sprintf(emsg, "Sorry, can not handle image");
+ return 0;
+ }
}
- }
- return 1;
+ return 1;
}
int
@@ -654,119 +662,120 @@ gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
static int
gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
{
- TIFF* tif = img->tif;
- tileSeparateRoutine put = img->put.separate;
- uint32 col, row, y, rowstoread;
- uint32 pos;
- uint32 tw, th;
- unsigned char* buf;
- unsigned char* r;
- unsigned char* g;
- unsigned char* b;
- unsigned char* a;
- tsize_t tilesize;
- int32 fromskew, toskew;
- int alpha = img->alpha;
- uint32 nrow;
- int ret = 1, flip;
-
- tilesize = TIFFTileSize(tif);
- buf = (unsigned char*) _TIFFmalloc(4*tilesize);
- if (buf == 0) {
+ TIFF* tif = img->tif;
+ tileSeparateRoutine put = img->put.separate;
+ uint32 col, row, y, rowstoread;
+ uint32 pos;
+ uint32 tw, th;
+ unsigned char* buf;
+ unsigned char* p0;
+ unsigned char* p1;
+ unsigned char* p2;
+ unsigned char* pa;
+ tsize_t tilesize;
+ int32 fromskew, toskew;
+ int alpha = img->alpha;
+ uint32 nrow;
+ int ret = 1, flip;
+
+ tilesize = TIFFTileSize(tif);
+ buf = (unsigned char*) _TIFFmalloc((alpha?4:3)*tilesize);
+ if (buf == 0) {
TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
return (0);
- }
- _TIFFmemset(buf, 0, 4*tilesize);
- r = buf;
- g = r + tilesize;
- b = g + tilesize;
- a = b + tilesize;
- if (!alpha)
- _TIFFmemset(a, 0xff, tilesize);
- TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
- TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
-
- flip = setorientation(img);
- if (flip & FLIP_VERTICALLY) {
- y = h - 1;
- toskew = -(int32)(tw + w);
- }
- else {
- y = 0;
- toskew = -(int32)(tw - w);
- }
-
- for (row = 0; row < h; row += nrow)
- {
- rowstoread = th - (row + img->row_offset) % th;
- nrow = (row + rowstoread > h ? h - row : rowstoread);
- for (col = 0; col < w; col += tw)
- {
- if (TIFFReadTile(tif, r, col+img->col_offset,
- row+img->row_offset,0,0) < 0 && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (TIFFReadTile(tif, g, col+img->col_offset,
- row+img->row_offset,0,1) < 0 && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (TIFFReadTile(tif, b, col+img->col_offset,
- row+img->row_offset,0,2) < 0 && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (alpha && TIFFReadTile(tif,a,col+img->col_offset,
- row+img->row_offset,0,3) < 0 && img->stoponerr)
- {
- ret = 0;
- break;
- }
+ }
+ _TIFFmemset(buf, 0, (alpha?4:3)*tilesize);
+ p0 = buf;
+ p1 = p0 + tilesize;
+ p2 = p1 + tilesize;
+ pa = (alpha?(p2+tilesize):NULL);
+ TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
+ TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
+
+ flip = setorientation(img);
+ if (flip & FLIP_VERTICALLY) {
+ y = h - 1;
+ toskew = -(int32)(tw + w);
+ }
+ else {
+ y = 0;
+ toskew = -(int32)(tw - w);
+ }
- pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);
+ for (row = 0; row < h; row += nrow)
+ {
+ rowstoread = th - (row + img->row_offset) % th;
+ nrow = (row + rowstoread > h ? h - row : rowstoread);
+ for (col = 0; col < w; col += tw)
+ {
+ if (TIFFReadTile(tif, p0, col+img->col_offset,
+ row+img->row_offset,0,0) < 0 && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (TIFFReadTile(tif, p1, col+img->col_offset,
+ row+img->row_offset,0,1) < 0 && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (TIFFReadTile(tif, p2, col+img->col_offset,
+ row+img->row_offset,0,2) < 0 && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (alpha)
+ {
+ if (TIFFReadTile(tif,pa,col+img->col_offset,
+ row+img->row_offset,0,3) < 0 && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ }
+
+ pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);
+
+ if (col + tw > w)
+ {
+ /*
+ * Tile is clipped horizontally. Calculate
+ * visible portion and skewing factors.
+ */
+ uint32 npix = w - col;
+ fromskew = tw - npix;
+ (*put)(img, raster+y*w+col, col, y,
+ npix, nrow, fromskew, toskew + fromskew,
+ p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
+ } else {
+ (*put)(img, raster+y*w+col, col, y,
+ tw, nrow, 0, toskew, p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
+ }
+ }
- if (col + tw > w)
- {
- /*
- * Tile is clipped horizontally. Calculate
- * visible portion and skewing factors.
- */
- uint32 npix = w - col;
- fromskew = tw - npix;
- (*put)(img, raster+y*w+col, col, y,
- npix, nrow, fromskew, toskew + fromskew,
- r + pos, g + pos, b + pos, a + pos);
- } else {
- (*put)(img, raster+y*w+col, col, y,
- tw, nrow, 0, toskew, r + pos, g + pos, b + pos, a + pos);
- }
- }
+ y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
+ }
- y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
- }
+ if (flip & FLIP_HORIZONTALLY) {
+ uint32 line;
- if (flip & FLIP_HORIZONTALLY) {
- uint32 line;
+ for (line = 0; line < h; line++) {
+ uint32 *left = raster + (line * w);
+ uint32 *right = left + w - 1;
- for (line = 0; line < h; line++) {
- uint32 *left = raster + (line * w);
- uint32 *right = left + w - 1;
-
- while ( left < right ) {
- uint32 temp = *left;
- *left = *right;
- *right = temp;
- left++, right--;
- }
- }
- }
+ while ( left < right ) {
+ uint32 temp = *left;
+ *left = *right;
+ *right = temp;
+ left++, right--;
+ }
+ }
+ }
- _TIFFfree(buf);
- return (ret);
+ _TIFFfree(buf);
+ return (ret);
}
/*
@@ -778,73 +787,78 @@ gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
static int
gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
{
- TIFF* tif = img->tif;
- tileContigRoutine put = img->put.contig;
- uint32 row, y, nrow, rowstoread;
- uint32 pos;
- unsigned char* buf;
- uint32 rowsperstrip;
- uint32 imagewidth = img->width;
- tsize_t scanline;
- int32 fromskew, toskew;
- int ret = 1, flip;
-
- buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
- if (buf == 0) {
+ TIFF* tif = img->tif;
+ tileContigRoutine put = img->put.contig;
+ uint32 row, y, nrow, nrowsub, rowstoread;
+ uint32 pos;
+ unsigned char* buf;
+ uint32 rowsperstrip;
+ uint16 subsamplinghor,subsamplingver;
+ uint32 imagewidth = img->width;
+ tsize_t scanline;
+ int32 fromskew, toskew;
+ int ret = 1, flip;
+
+ buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
+ if (buf == 0) {
TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
return (0);
- }
- _TIFFmemset(buf, 0, TIFFStripSize(tif));
+ }
+ _TIFFmemset(buf, 0, TIFFStripSize(tif));
- flip = setorientation(img);
- if (flip & FLIP_VERTICALLY) {
- y = h - 1;
- toskew = -(int32)(w + w);
- } else {
- y = 0;
- toskew = -(int32)(w - w);
- }
+ flip = setorientation(img);
+ if (flip & FLIP_VERTICALLY) {
+ y = h - 1;
+ toskew = -(int32)(w + w);
+ } else {
+ y = 0;
+ toskew = -(int32)(w - w);
+ }
- TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
- scanline = TIFFScanlineSize(tif);
- fromskew = (w < imagewidth ? imagewidth - w : 0);
- for (row = 0; row < h; row += nrow)
- {
- rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
- nrow = (row + rowstoread > h ? h - row : rowstoread);
- if (TIFFReadEncodedStrip(tif,
- TIFFComputeStrip(tif,row+img->row_offset, 0),
- buf,
- ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
+ TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+ TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
+ scanline = TIFFNewScanlineSize(tif);
+ fromskew = (w < imagewidth ? imagewidth - w : 0);
+ for (row = 0; row < h; row += nrow)
+ {
+ rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
+ nrow = (row + rowstoread > h ? h - row : rowstoread);
+ nrowsub = nrow;
+ if ((nrowsub%subsamplingver)!=0)
+ nrowsub+=subsamplingver-nrowsub%subsamplingver;
+ if (TIFFReadEncodedStrip(tif,
+ TIFFComputeStrip(tif,row+img->row_offset, 0),
+ buf,
+ ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline) < 0
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
- pos = ((row + img->row_offset) % rowsperstrip) * scanline;
- (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
- y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
- }
+ pos = ((row + img->row_offset) % rowsperstrip) * scanline;
+ (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
+ y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
+ }
- if (flip & FLIP_HORIZONTALLY) {
- uint32 line;
+ if (flip & FLIP_HORIZONTALLY) {
+ uint32 line;
- for (line = 0; line < h; line++) {
- uint32 *left = raster + (line * w);
- uint32 *right = left + w - 1;
-
- while ( left < right ) {
- uint32 temp = *left;
- *left = *right;
- *right = temp;
- left++, right--;
- }
- }
- }
+ for (line = 0; line < h; line++) {
+ uint32 *left = raster + (line * w);
+ uint32 *right = left + w - 1;
- _TIFFfree(buf);
- return (ret);
+ while ( left < right ) {
+ uint32 temp = *left;
+ *left = *right;
+ *right = temp;
+ left++, right--;
+ }
+ }
+ }
+
+ _TIFFfree(buf);
+ return (ret);
}
/*
@@ -856,105 +870,105 @@ gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
static int
gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
{
- TIFF* tif = img->tif;
- tileSeparateRoutine put = img->put.separate;
- unsigned char *buf;
- unsigned char *r, *g, *b, *a;
- uint32 row, y, nrow, rowstoread;
- uint32 pos;
- tsize_t scanline;
- uint32 rowsperstrip, offset_row;
- uint32 imagewidth = img->width;
- tsize_t stripsize;
- int32 fromskew, toskew;
- int alpha = img->alpha;
- int ret = 1, flip;
-
- stripsize = TIFFStripSize(tif);
- r = buf = (unsigned char *)_TIFFmalloc(4*stripsize);
- if (buf == 0) {
+ TIFF* tif = img->tif;
+ tileSeparateRoutine put = img->put.separate;
+ unsigned char *buf;
+ unsigned char *p0, *p1, *p2, *pa;
+ uint32 row, y, nrow, rowstoread;
+ uint32 pos;
+ tsize_t scanline;
+ uint32 rowsperstrip, offset_row;
+ uint32 imagewidth = img->width;
+ tsize_t stripsize;
+ int32 fromskew, toskew;
+ int alpha = img->alpha;
+ int ret = 1, flip;
+
+ stripsize = TIFFStripSize(tif);
+ p0 = buf = (unsigned char *)_TIFFmalloc((alpha?4:3)*stripsize);
+ if (buf == 0) {
TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
return (0);
- }
- _TIFFmemset(buf, 0, 4*stripsize);
- g = r + stripsize;
- b = g + stripsize;
- a = b + stripsize;
- if (!alpha)
- _TIFFmemset(a, 0xff, stripsize);
+ }
+ _TIFFmemset(buf, 0, (alpha?4:3)*stripsize);
+ p1 = p0 + stripsize;
+ p2 = p1 + stripsize;
+ pa = (alpha?(p2+stripsize):NULL);
+
+ flip = setorientation(img);
+ if (flip & FLIP_VERTICALLY) {
+ y = h - 1;
+ toskew = -(int32)(w + w);
+ }
+ else {
+ y = 0;
+ toskew = -(int32)(w - w);
+ }
- flip = setorientation(img);
- if (flip & FLIP_VERTICALLY) {
- y = h - 1;
- toskew = -(int32)(w + w);
- }
- else {
- y = 0;
- toskew = -(int32)(w - w);
- }
+ TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+ scanline = TIFFScanlineSize(tif);
+ fromskew = (w < imagewidth ? imagewidth - w : 0);
+ for (row = 0; row < h; row += nrow)
+ {
+ rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
+ nrow = (row + rowstoread > h ? h - row : rowstoread);
+ offset_row = row + img->row_offset;
+ if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
+ p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
+ p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
+ p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ if (alpha)
+ {
+ if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 3),
+ pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
+ && img->stoponerr)
+ {
+ ret = 0;
+ break;
+ }
+ }
- TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
- scanline = TIFFScanlineSize(tif);
- fromskew = (w < imagewidth ? imagewidth - w : 0);
- for (row = 0; row < h; row += nrow)
- {
- rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
- nrow = (row + rowstoread > h ? h - row : rowstoread);
- offset_row = row + img->row_offset;
- if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
- r, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
- g, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
- b, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (alpha &&
- (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 3),
- a, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
- && img->stoponerr))
- {
- ret = 0;
- break;
- }
+ pos = ((row + img->row_offset) % rowsperstrip) * scanline;
+ (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
+ p2 + pos, (alpha?(pa+pos):NULL));
+ y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
+ }
- pos = ((row + img->row_offset) % rowsperstrip) * scanline;
- (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, r + pos, g + pos,
- b + pos, a + pos);
- y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
- }
+ if (flip & FLIP_HORIZONTALLY) {
+ uint32 line;
- if (flip & FLIP_HORIZONTALLY) {
- uint32 line;
+ for (line = 0; line < h; line++) {
+ uint32 *left = raster + (line * w);
+ uint32 *right = left + w - 1;
- for (line = 0; line < h; line++) {
- uint32 *left = raster + (line * w);
- uint32 *right = left + w - 1;
-
- while ( left < right ) {
- uint32 temp = *left;
- *left = *right;
- *right = temp;
- left++, right--;
- }
- }
- }
+ while ( left < right ) {
+ uint32 temp = *left;
+ *left = *right;
+ *right = temp;
+ left++, right--;
+ }
+ }
+ }
- _TIFFfree(buf);
- return (ret);
+ _TIFFfree(buf);
+ return (ret);
}
/*
@@ -963,9 +977,9 @@ gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
* ABGR pixels (i.e. suitable for passing to lrecwrite.)
*
* The routines have been created according to the most
- * important cases and optimized. pickTileContigCase and
- * pickTileSeparateCase analyze the parameters and select
- * the appropriate "put" routine to use.
+ * important cases and optimized. PickContigCase and
+ * PickSeparateCase analyze the parameters and select
+ * the appropriate "get" and "put" routine to use.
*/
#define REPEAT8(op) REPEAT4(op); REPEAT4(op)
#define REPEAT4(op) REPEAT2(op); REPEAT2(op)
@@ -1223,26 +1237,6 @@ DECLAREContigPutFunc(putRGBcontig8bittile)
}
/*
- * 8-bit packed samples, w/ Map => RGB
- */
-DECLAREContigPutFunc(putRGBcontig8bitMaptile)
-{
- TIFFRGBValue* Map = img->Map;
- int samplesperpixel = img->samplesperpixel;
-
- (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- for (x = w; x-- > 0;) {
- *cp++ = PACK(Map[pp[0]], Map[pp[1]], Map[pp[2]]);
- pp += samplesperpixel;
- }
- pp += fromskew;
- cp += toskew;
- }
-}
-
-/*
* 8-bit packed samples => RGBA w/ associated alpha
* (known to have Map == NULL)
*/
@@ -1267,23 +1261,22 @@ DECLAREContigPutFunc(putRGBAAcontig8bittile)
*/
DECLAREContigPutFunc(putRGBUAcontig8bittile)
{
- int samplesperpixel = img->samplesperpixel;
-
- (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- uint32 r, g, b, a;
- for (x = w; x-- > 0;) {
- a = pp[3];
- r = (pp[0] * a) / 255;
- g = (pp[1] * a) / 255;
- b = (pp[2] * a) / 255;
- *cp++ = PACK4(r,g,b,a);
- pp += samplesperpixel;
+ int samplesperpixel = img->samplesperpixel;
+ (void) y;
+ fromskew *= samplesperpixel;
+ while (h-- > 0) {
+ uint32 r, g, b, a;
+ for (x = w; x-- > 0;) {
+ a = pp[3];
+ r = (a*pp[0] + 127) / 255;
+ g = (a*pp[1] + 127) / 255;
+ b = (a*pp[2] + 127) / 255;
+ *cp++ = PACK4(r,g,b,a);
+ pp += samplesperpixel;
+ }
+ cp += toskew;
+ pp += fromskew;
}
- cp += toskew;
- pp += fromskew;
- }
}
/*
@@ -1291,19 +1284,18 @@ DECLAREContigPutFunc(putRGBUAcontig8bittile)
*/
DECLAREContigPutFunc(putRGBcontig16bittile)
{
- int samplesperpixel = img->samplesperpixel;
- uint16 *wp = (uint16 *)pp;
-
- (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- for (x = w; x-- > 0;) {
- *cp++ = PACKW(wp[0], wp[1], wp[2]);
- wp += samplesperpixel;
+ int samplesperpixel = img->samplesperpixel;
+ uint16 *wp = (uint16 *)pp;
+ (void) y;
+ fromskew *= samplesperpixel;
+ while (h-- > 0) {
+ for (x = w; x-- > 0;) {
+ *cp++ = PACKW(wp[0],wp[1],wp[2]);
+ wp += samplesperpixel;
+ }
+ cp += toskew;
+ wp += fromskew;
}
- cp += toskew;
- wp += fromskew;
- }
}
/*
@@ -1312,19 +1304,18 @@ DECLAREContigPutFunc(putRGBcontig16bittile)
*/
DECLAREContigPutFunc(putRGBAAcontig16bittile)
{
- int samplesperpixel = img->samplesperpixel;
- uint16 *wp = (uint16 *)pp;
-
- (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- for (x = w; x-- > 0;) {
- *cp++ = PACKW4(wp[0], wp[1], wp[2], wp[3]);
- wp += samplesperpixel;
+ int samplesperpixel = img->samplesperpixel;
+ uint16 *wp = (uint16 *)pp;
+ (void) y;
+ fromskew *= samplesperpixel;
+ while (h-- > 0) {
+ for (x = w; x-- > 0;) {
+ *cp++ = PACKW4(wp[0],wp[1],wp[2],wp[3]);
+ wp += samplesperpixel;
+ }
+ cp += toskew;
+ wp += fromskew;
}
- cp += toskew;
- wp += fromskew;
- }
}
/*
@@ -1333,32 +1324,23 @@ DECLAREContigPutFunc(putRGBAAcontig16bittile)
*/
DECLAREContigPutFunc(putRGBUAcontig16bittile)
{
- int samplesperpixel = img->samplesperpixel;
- uint16 *wp = (uint16 *)pp;
-
- (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- uint32 r,g,b,a;
- /*
- * We shift alpha down four bits just in case unsigned
- * arithmetic doesn't handle the full range.
- * We still have plenty of accuracy, since the output is 8 bits.
- * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)
- * Since we want r*a * 0xff for eight bit output,
- * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.
- */
- for (x = w; x-- > 0;) {
- a = wp[3] >> 4;
- r = (wp[0] * a) / 0x10eff;
- g = (wp[1] * a) / 0x10eff;
- b = (wp[2] * a) / 0x10eff;
- *cp++ = PACK4(r,g,b,a);
- wp += samplesperpixel;
+ int samplesperpixel = img->samplesperpixel;
+ uint16 *wp = (uint16 *)pp;
+ (void) y;
+ fromskew *= samplesperpixel;
+ while (h-- > 0) {
+ uint32 r,g,b,a;
+ for (x = w; x-- > 0;) {
+ a = W2B(wp[3]);
+ r = (a*W2B(wp[0]) + 127) / 255;
+ g = (a*W2B(wp[1]) + 127) / 255;
+ b = (a*W2B(wp[2]) + 127) / 255;
+ *cp++ = PACK4(r,g,b,a);
+ wp += samplesperpixel;
+ }
+ cp += toskew;
+ wp += fromskew;
}
- cp += toskew;
- wp += fromskew;
- }
}
/*
@@ -1437,32 +1419,16 @@ DECLARESepPutFunc(putRGBseparate8bittile)
}
/*
- * 8-bit unpacked samples => RGB
- */
-DECLARESepPutFunc(putRGBseparate8bitMaptile)
-{
- TIFFRGBValue* Map = img->Map;
-
- (void) y; (void) a;
- while (h-- > 0) {
- for (x = w; x > 0; x--)
- *cp++ = PACK(Map[*r++], Map[*g++], Map[*b++]);
- SKEW(r, g, b, fromskew);
- cp += toskew;
- }
-}
-
-/*
* 8-bit unpacked samples => RGBA w/ associated alpha
*/
DECLARESepPutFunc(putRGBAAseparate8bittile)
{
- (void) img; (void) x; (void) y;
- while (h-- > 0) {
- UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
- SKEW4(r, g, b, a, fromskew);
- cp += toskew;
- }
+ (void) img; (void) x; (void) y;
+ while (h-- > 0) {
+ UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
+ SKEW4(r, g, b, a, fromskew);
+ cp += toskew;
+ }
}
/*
@@ -1470,19 +1436,19 @@ DECLARESepPutFunc(putRGBAAseparate8bittile)
*/
DECLARESepPutFunc(putRGBUAseparate8bittile)
{
- (void) img; (void) y;
- while (h-- > 0) {
- uint32 rv, gv, bv, av;
- for (x = w; x-- > 0;) {
- av = *a++;
- rv = (*r++ * av) / 255;
- gv = (*g++ * av) / 255;
- bv = (*b++ * av) / 255;
- *cp++ = PACK4(rv,gv,bv,av);
+ (void) img; (void) y;
+ while (h-- > 0) {
+ uint32 rv, gv, bv, av;
+ for (x = w; x-- > 0;) {
+ av = *a++;
+ rv = (av* *r++ + 127) / 255;
+ gv = (av* *g++ + 127) / 255;
+ bv = (av* *b++ + 127) / 255;
+ *cp++ = PACK4(rv,gv,bv,av);
+ }
+ SKEW4(r, g, b, a, fromskew);
+ cp += toskew;
}
- SKEW4(r, g, b, a, fromskew);
- cp += toskew;
- }
}
/*
@@ -1490,17 +1456,16 @@ DECLARESepPutFunc(putRGBUAseparate8bittile)
*/
DECLARESepPutFunc(putRGBseparate16bittile)
{
- uint16 *wr = (uint16*) r;
- uint16 *wg = (uint16*) g;
- uint16 *wb = (uint16*) b;
-
- (void) img; (void) y; (void) a;
- while (h-- > 0) {
- for (x = 0; x < w; x++)
- *cp++ = PACKW(*wr++, *wg++, *wb++);
- SKEW(wr, wg, wb, fromskew);
- cp += toskew;
- }
+ uint16 *wr = (uint16*) r;
+ uint16 *wg = (uint16*) g;
+ uint16 *wb = (uint16*) b;
+ (void) img; (void) y; (void) a;
+ while (h-- > 0) {
+ for (x = 0; x < w; x++)
+ *cp++ = PACKW(*wr++,*wg++,*wb++);
+ SKEW(wr, wg, wb, fromskew);
+ cp += toskew;
+ }
}
/*
@@ -1508,18 +1473,17 @@ DECLARESepPutFunc(putRGBseparate16bittile)
*/
DECLARESepPutFunc(putRGBAAseparate16bittile)
{
- uint16 *wr = (uint16*) r;
- uint16 *wg = (uint16*) g;
- uint16 *wb = (uint16*) b;
- uint16 *wa = (uint16*) a;
-
- (void) img; (void) y;
- while (h-- > 0) {
- for (x = 0; x < w; x++)
- *cp++ = PACKW4(*wr++, *wg++, *wb++, *wa++);
- SKEW4(wr, wg, wb, wa, fromskew);
- cp += toskew;
- }
+ uint16 *wr = (uint16*) r;
+ uint16 *wg = (uint16*) g;
+ uint16 *wb = (uint16*) b;
+ uint16 *wa = (uint16*) a;
+ (void) img; (void) y;
+ while (h-- > 0) {
+ for (x = 0; x < w; x++)
+ *cp++ = PACKW4(*wr++,*wg++,*wb++,*wa++);
+ SKEW4(wr, wg, wb, wa, fromskew);
+ cp += toskew;
+ }
}
/*
@@ -1527,32 +1491,23 @@ DECLARESepPutFunc(putRGBAAseparate16bittile)
*/
DECLARESepPutFunc(putRGBUAseparate16bittile)
{
- uint16 *wr = (uint16*) r;
- uint16 *wg = (uint16*) g;
- uint16 *wb = (uint16*) b;
- uint16 *wa = (uint16*) a;
-
- (void) img; (void) y;
- while (h-- > 0) {
- uint32 r,g,b,a;
- /*
- * We shift alpha down four bits just in case unsigned
- * arithmetic doesn't handle the full range.
- * We still have plenty of accuracy, since the output is 8 bits.
- * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)
- * Since we want r*a * 0xff for eight bit output,
- * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.
- */
- for (x = w; x-- > 0;) {
- a = *wa++ >> 4;
- r = (*wr++ * a) / 0x10eff;
- g = (*wg++ * a) / 0x10eff;
- b = (*wb++ * a) / 0x10eff;
- *cp++ = PACK4(r,g,b,a);
+ uint16 *wr = (uint16*) r;
+ uint16 *wg = (uint16*) g;
+ uint16 *wb = (uint16*) b;
+ uint16 *wa = (uint16*) a;
+ (void) img; (void) y;
+ while (h-- > 0) {
+ uint32 r,g,b,a;
+ for (x = w; x-- > 0;) {
+ a = W2B(*wa++);
+ r = (a*W2B(*wr++) + 127) / 255;
+ g = (a*W2B(*wg++) + 127) / 255;
+ b = (a*W2B(*wb++) + 127) / 255;
+ *cp++ = PACK4(r,g,b,a);
+ }
+ SKEW4(wr, wg, wb, wa, fromskew);
+ cp += toskew;
}
- SKEW4(wr, wg, wb, wa, fromskew);
- cp += toskew;
- }
}
/*
@@ -1890,63 +1845,56 @@ DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
*/
DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
{
- uint32* cp1 = cp+w+toskew;
- int32 incr = 2*toskew+w;
-
- (void) y;
- fromskew = (fromskew * 6) / 2;
- if ((h & 1) == 0 && (w & 1) == 0) {
- for (; h >= 2; h -= 2) {
- x = w>>1;
- do {
- int32 Cb = pp[4];
- int32 Cr = pp[5];
-
- YCbCrtoRGB(cp [0], pp[0]);
- YCbCrtoRGB(cp [1], pp[1]);
- YCbCrtoRGB(cp1[0], pp[2]);
- YCbCrtoRGB(cp1[1], pp[3]);
-
- cp += 2, cp1 += 2;
- pp += 6;
- } while (--x);
- cp += incr, cp1 += incr;
- pp += fromskew;
- }
- } else {
- while (h > 0) {
- for (x = w; x > 0;) {
- int32 Cb = pp[4];
- int32 Cr = pp[5];
- switch (x) {
- default:
- switch (h) {
- default: YCbCrtoRGB(cp1[1], pp[ 3]); /* FALLTHROUGH */
- case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
- } /* FALLTHROUGH */
- case 1:
- switch (h) {
- default: YCbCrtoRGB(cp1[0], pp[ 2]); /* FALLTHROUGH */
- case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
- } /* FALLTHROUGH */
- }
- if (x < 2) {
- cp += x; cp1 += x;
- x = 0;
- }
- else {
- cp += 2; cp1 += 2;
- x -= 2;
- }
- pp += 6;
- }
- if (h <= 2)
- break;
- h -= 2;
- cp += incr, cp1 += incr;
- pp += fromskew;
- }
- }
+ uint32* cp2;
+ (void) y;
+ fromskew = (fromskew / 2) * 6;
+ cp2 = cp+w+toskew;
+ while (h>=2) {
+ x = w;
+ while (x>=2) {
+ uint32 Cb = pp[4];
+ uint32 Cr = pp[5];
+ YCbCrtoRGB(cp[0], pp[0]);
+ YCbCrtoRGB(cp[1], pp[1]);
+ YCbCrtoRGB(cp2[0], pp[2]);
+ YCbCrtoRGB(cp2[1], pp[3]);
+ cp += 2;
+ cp2 += 2;
+ pp += 6;
+ x -= 2;
+ }
+ if (x==1) {
+ uint32 Cb = pp[4];
+ uint32 Cr = pp[5];
+ YCbCrtoRGB(cp[0], pp[0]);
+ YCbCrtoRGB(cp2[0], pp[2]);
+ cp ++ ;
+ cp2 ++ ;
+ pp += 6;
+ }
+ cp += toskew*2+w;
+ cp2 += toskew*2+w;
+ pp += fromskew;
+ h-=2;
+ }
+ if (h==1) {
+ x = w;
+ while (x>=2) {
+ uint32 Cb = pp[4];
+ uint32 Cr = pp[5];
+ YCbCrtoRGB(cp[0], pp[0]);
+ YCbCrtoRGB(cp[1], pp[1]);
+ cp += 2;
+ cp2 += 2;
+ pp += 6;
+ x -= 2;
+ }
+ if (x==1) {
+ uint32 Cb = pp[4];
+ uint32 Cr = pp[5];
+ YCbCrtoRGB(cp[0], pp[0]);
+ }
+ }
}
/*
@@ -1954,35 +1902,72 @@ DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
*/
DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
{
- (void) y;
- fromskew = (fromskew * 4) / 2;
- do {
- x = w>>1;
+ (void) y;
+ fromskew = (fromskew * 4) / 2;
do {
- int32 Cb = pp[2];
- int32 Cr = pp[3];
+ x = w>>1;
+ do {
+ int32 Cb = pp[2];
+ int32 Cr = pp[3];
- YCbCrtoRGB(cp[0], pp[0]);
- YCbCrtoRGB(cp[1], pp[1]);
+ YCbCrtoRGB(cp[0], pp[0]);
+ YCbCrtoRGB(cp[1], pp[1]);
- cp += 2;
- pp += 4;
- } while (--x);
+ cp += 2;
+ pp += 4;
+ } while (--x);
- if( (w&1) != 0 )
- {
- int32 Cb = pp[2];
- int32 Cr = pp[3];
-
- YCbCrtoRGB(cp [0], pp[0]);
+ if( (w&1) != 0 )
+ {
+ int32 Cb = pp[2];
+ int32 Cr = pp[3];
- cp += 1;
- pp += 4;
- }
+ YCbCrtoRGB(cp[0], pp[0]);
- cp += toskew;
- pp += fromskew;
- } while (--h);
+ cp += 1;
+ pp += 4;
+ }
+
+ cp += toskew;
+ pp += fromskew;
+ } while (--h);
+}
+
+/*
+ * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
+ */
+DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
+{
+ uint32* cp2;
+ (void) y;
+ fromskew = (fromskew / 2) * 4;
+ cp2 = cp+w+toskew;
+ while (h>=2) {
+ x = w;
+ do {
+ uint32 Cb = pp[2];
+ uint32 Cr = pp[3];
+ YCbCrtoRGB(cp[0], pp[0]);
+ YCbCrtoRGB(cp2[0], pp[1]);
+ cp ++;
+ cp2 ++;
+ pp += 4;
+ } while (--x);
+ cp += toskew*2+w;
+ cp2 += toskew*2+w;
+ pp += fromskew;
+ h-=2;
+ }
+ if (h==1) {
+ x = w;
+ do {
+ uint32 Cb = pp[2];
+ uint32 Cr = pp[3];
+ YCbCrtoRGB(cp[0], pp[0]);
+ cp ++;
+ pp += 4;
+ } while (--x);
+ }
}
/*
@@ -1990,70 +1975,71 @@ DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
*/
DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
{
- (void) y;
- fromskew *= 3;
- do {
- x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
+ (void) y;
+ fromskew *= 3;
do {
- int32 Cb = pp[1];
- int32 Cr = pp[2];
+ x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
+ do {
+ int32 Cb = pp[1];
+ int32 Cr = pp[2];
- YCbCrtoRGB(*cp++, pp[0]);
+ YCbCrtoRGB(*cp++, pp[0]);
- pp += 3;
- } while (--x);
- cp += toskew;
- pp += fromskew;
- } while (--h);
+ pp += 3;
+ } while (--x);
+ cp += toskew;
+ pp += fromskew;
+ } while (--h);
}
-#undef YCbCrtoRGB
-static tileContigRoutine
+/*
+ * 8-bit packed YCbCr samples w/ no subsampling => RGB
+ */
+DECLARESepPutFunc(putseparate8bitYCbCr11tile)
+{
+ (void) y;
+ (void) a;
+ /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
+ while (h-- > 0) {
+ x = w;
+ do {
+ uint32 dr, dg, db;
+ TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
+ *cp++ = PACK(dr,dg,db);
+ } while (--x);
+ SKEW(r, g, b, fromskew);
+ cp += toskew;
+ }
+}
+#undef YCbCrtoRGB
+
+static int
initYCbCrConversion(TIFFRGBAImage* img)
{
- static char module[] = "initCIELabConversion";
+ static char module[] = "initYCbCrConversion";
float *luma, *refBlackWhite;
- uint16 hs, vs;
if (img->ycbcr == NULL) {
- img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
+ img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
TIFFroundup(sizeof (TIFFYCbCrToRGB), sizeof (long))
+ 4*256*sizeof (TIFFRGBValue)
+ 2*256*sizeof (int)
+ 3*256*sizeof (int32)
- );
- if (img->ycbcr == NULL) {
+ );
+ if (img->ycbcr == NULL) {
TIFFErrorExt(img->tif->tif_clientdata, module,
- "No space for YCbCr->RGB conversion state");
- return (NULL);
- }
+ "No space for YCbCr->RGB conversion state");
+ return (0);
+ }
}
TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
- &refBlackWhite);
+ &refBlackWhite);
if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
- return NULL;
-
- /*
- * The 6.0 spec says that subsampling must be
- * one of 1, 2, or 4, and that vertical subsampling
- * must always be <= horizontal subsampling; so
- * there are only a few possibilities and we just
- * enumerate the cases.
- */
- TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
- switch ((hs<<4)|vs) {
- case 0x44: return (putcontig8bitYCbCr44tile);
- case 0x42: return (putcontig8bitYCbCr42tile);
- case 0x41: return (putcontig8bitYCbCr41tile);
- case 0x22: return (putcontig8bitYCbCr22tile);
- case 0x21: return (putcontig8bitYCbCr21tile);
- case 0x11: return (putcontig8bitYCbCr11tile);
- }
-
- return (NULL);
+ return(0);
+ return (1);
}
static tileContigRoutine
@@ -2327,73 +2313,140 @@ buildMap(TIFFRGBAImage* img)
* Select the appropriate conversion routine for packed data.
*/
static int
-pickTileContigCase(TIFFRGBAImage* img)
+PickContigCase(TIFFRGBAImage* img)
{
- tileContigRoutine put = 0;
-
- if (buildMap(img)) {
+ img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
+ img->put.contig = NULL;
switch (img->photometric) {
- case PHOTOMETRIC_RGB:
- switch (img->bitspersample) {
- case 8:
- if (!img->Map) {
- if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
- put = putRGBAAcontig8bittile;
- else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
- put = putRGBUAcontig8bittile;
- else
- put = putRGBcontig8bittile;
- } else
- put = putRGBcontig8bitMaptile;
- break;
- case 16:
- put = putRGBcontig16bittile;
- if (!img->Map) {
- if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
- put = putRGBAAcontig16bittile;
- else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
- put = putRGBUAcontig16bittile;
- }
- break;
- }
- break;
- case PHOTOMETRIC_SEPARATED:
- if (img->bitspersample == 8) {
- if (!img->Map)
- put = putRGBcontig8bitCMYKtile;
- else
- put = putRGBcontig8bitCMYKMaptile;
- }
- break;
- case PHOTOMETRIC_PALETTE:
- switch (img->bitspersample) {
- case 8: put = put8bitcmaptile; break;
- case 4: put = put4bitcmaptile; break;
- case 2: put = put2bitcmaptile; break;
- case 1: put = put1bitcmaptile; break;
- }
- break;
- case PHOTOMETRIC_MINISWHITE:
- case PHOTOMETRIC_MINISBLACK:
- switch (img->bitspersample) {
- case 16: put = put16bitbwtile; break;
- case 8: put = putgreytile; break;
- case 4: put = put4bitbwtile; break;
- case 2: put = put2bitbwtile; break;
- case 1: put = put1bitbwtile; break;
- }
- break;
- case PHOTOMETRIC_YCBCR:
- if (img->bitspersample == 8)
- put = initYCbCrConversion(img);
- break;
- case PHOTOMETRIC_CIELAB:
- if (img->bitspersample == 8)
- put = initCIELabConversion(img);
- break;
+ case PHOTOMETRIC_RGB:
+ switch (img->bitspersample) {
+ case 8:
+ if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
+ img->put.contig = putRGBAAcontig8bittile;
+ else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
+ {
+ img->put.contig = putRGBUAcontig8bittile;
+ }
+ else
+ img->put.contig = putRGBcontig8bittile;
+ break;
+ case 16:
+ if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
+ {
+ img->put.contig = putRGBAAcontig16bittile;
+ }
+ else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
+ {
+ img->put.contig = putRGBUAcontig16bittile;
+ }
+ else
+ {
+ img->put.contig = putRGBcontig16bittile;
+ }
+ break;
+ }
+ break;
+ case PHOTOMETRIC_SEPARATED:
+ if (buildMap(img)) {
+ if (img->bitspersample == 8) {
+ if (!img->Map)
+ img->put.contig = putRGBcontig8bitCMYKtile;
+ else
+ img->put.contig = putRGBcontig8bitCMYKMaptile;
+ }
+ }
+ break;
+ case PHOTOMETRIC_PALETTE:
+ if (buildMap(img)) {
+ switch (img->bitspersample) {
+ case 8:
+ img->put.contig = put8bitcmaptile;
+ break;
+ case 4:
+ img->put.contig = put4bitcmaptile;
+ break;
+ case 2:
+ img->put.contig = put2bitcmaptile;
+ break;
+ case 1:
+ img->put.contig = put1bitcmaptile;
+ break;
+ }
+ }
+ break;
+ case PHOTOMETRIC_MINISWHITE:
+ case PHOTOMETRIC_MINISBLACK:
+ if (buildMap(img)) {
+ switch (img->bitspersample) {
+ case 16:
+ img->put.contig = put16bitbwtile;
+ break;
+ case 8:
+ img->put.contig = putgreytile;
+ break;
+ case 4:
+ img->put.contig = put4bitbwtile;
+ break;
+ case 2:
+ img->put.contig = put2bitbwtile;
+ break;
+ case 1:
+ img->put.contig = put1bitbwtile;
+ break;
+ }
+ }
+ break;
+ case PHOTOMETRIC_YCBCR:
+ if (img->bitspersample == 8)
+ {
+ if (initYCbCrConversion(img)!=0)
+ {
+ /*
+ * The 6.0 spec says that subsampling must be
+ * one of 1, 2, or 4, and that vertical subsampling
+ * must always be <= horizontal subsampling; so
+ * there are only a few possibilities and we just
+ * enumerate the cases.
+ * Joris: added support for the [1,2] case, nonetheless, to accomodate
+ * some OJPEG files
+ */
+ uint16 SubsamplingHor;
+ uint16 SubsamplingVer;
+ TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
+ switch ((SubsamplingHor<<4)|SubsamplingVer) {
+ case 0x44:
+ img->put.contig = putcontig8bitYCbCr44tile;
+ break;
+ case 0x42:
+ img->put.contig = putcontig8bitYCbCr42tile;
+ break;
+ case 0x41:
+ img->put.contig = putcontig8bitYCbCr41tile;
+ break;
+ case 0x22:
+ img->put.contig = putcontig8bitYCbCr22tile;
+ break;
+ case 0x21:
+ img->put.contig = putcontig8bitYCbCr21tile;
+ break;
+ case 0x12:
+ img->put.contig = putcontig8bitYCbCr12tile;
+ break;
+ case 0x11:
+ img->put.contig = putcontig8bitYCbCr11tile;
+ break;
+ }
+ }
+ }
+ break;
+ case PHOTOMETRIC_CIELAB:
+ if (buildMap(img)) {
+ if (img->bitspersample == 8)
+ img->put.contig = initCIELabConversion(img);
+ break;
+ }
}
- }
- return ((img->put.contig = put) != 0);
+ return ((img->get!=NULL) && (img->put.contig!=NULL));
}
/*
@@ -2403,39 +2456,57 @@ pickTileContigCase(TIFFRGBAImage* img)
* to the "packed routines.
*/
static int
-pickTileSeparateCase(TIFFRGBAImage* img)
+PickSeparateCase(TIFFRGBAImage* img)
{
- tileSeparateRoutine put = 0;
-
- if (buildMap(img)) {
+ img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
+ img->put.separate = NULL;
switch (img->photometric) {
- case PHOTOMETRIC_RGB:
- switch (img->bitspersample) {
- case 8:
- if (!img->Map) {
- if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
- put = putRGBAAseparate8bittile;
- else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
- put = putRGBUAseparate8bittile;
- else
- put = putRGBseparate8bittile;
- } else
- put = putRGBseparate8bitMaptile;
- break;
- case 16:
- put = putRGBseparate16bittile;
- if (!img->Map) {
- if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
- put = putRGBAAseparate16bittile;
- else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
- put = putRGBUAseparate16bittile;
- }
- break;
- }
- break;
+ case PHOTOMETRIC_RGB:
+ switch (img->bitspersample) {
+ case 8:
+ if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
+ img->put.separate = putRGBAAseparate8bittile;
+ else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
+ {
+ img->put.separate = putRGBUAseparate8bittile;
+ }
+ else
+ img->put.separate = putRGBseparate8bittile;
+ break;
+ case 16:
+ if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
+ {
+ img->put.separate = putRGBAAseparate16bittile;
+ }
+ else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
+ {
+ img->put.separate = putRGBUAseparate16bittile;
+ }
+ else
+ {
+ img->put.separate = putRGBseparate16bittile;
+ }
+ break;
+ }
+ break;
+ case PHOTOMETRIC_YCBCR:
+ if ((img->bitspersample==8) && (img->samplesperpixel==3))
+ {
+ if (initYCbCrConversion(img)!=0)
+ {
+ uint16 hs, vs;
+ TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
+ switch ((hs<<4)|vs) {
+ case 0x11:
+ img->put.separate = putseparate8bitYCbCr11tile;
+ break;
+ /* TODO: add other cases here */
+ }
+ }
+ }
+ break;
}
- }
- return ((img->put.separate = put) != 0);
+ return ((img->get!=NULL) && (img->put.separate!=NULL));
}
/*
diff --git a/src/libtiff/tif_jpeg.c b/src/libtiff/tif_jpeg.c
index 63f943e..90e157e 100644
--- a/src/libtiff/tif_jpeg.c
+++ b/src/libtiff/tif_jpeg.c
@@ -1,4 +1,4 @@
-/* $Id: tif_jpeg.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_jpeg.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1994-1997 Sam Leffler
@@ -154,6 +154,7 @@ typedef struct {
TIFFVGetMethod vgetparent; /* super-class method */
TIFFVSetMethod vsetparent; /* super-class method */
+ TIFFPrintMethod printdir; /* super-class method */
TIFFStripMethod defsparent; /* super-class method */
TIFFTileMethod deftparent; /* super-class method */
/* pseudo-tag fields */
@@ -714,7 +715,7 @@ JPEGPreDecode(TIFF* tif, tsample_t s)
} else {
if (segment_height > td->td_rowsperstrip)
segment_height = td->td_rowsperstrip;
- sp->bytesperline = TIFFScanlineSize(tif);
+ sp->bytesperline = TIFFOldScanlineSize(tif);
}
if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) {
/*
@@ -724,14 +725,29 @@ JPEGPreDecode(TIFF* tif, tsample_t s)
segment_width = TIFFhowmany(segment_width, sp->h_sampling);
segment_height = TIFFhowmany(segment_height, sp->v_sampling);
}
- if (sp->cinfo.d.image_width != segment_width ||
- sp->cinfo.d.image_height != segment_height) {
+ if (sp->cinfo.d.image_width < segment_width ||
+ sp->cinfo.d.image_height < segment_height) {
TIFFWarningExt(tif->tif_clientdata, module,
- "Improper JPEG strip/tile size, expected %dx%d, got %dx%d",
- segment_width,
- segment_height,
- sp->cinfo.d.image_width,
- sp->cinfo.d.image_height);
+ "Improper JPEG strip/tile size, "
+ "expected %dx%d, got %dx%d",
+ 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) {
+ /*
+ * This case could be dangerous, if the strip or tile size has
+ * been reported as less than the amount of data jpeg will
+ * return, some potential security issues arise. Catch this
+ * case and error out.
+ */
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "JPEG strip/tile size exceeds expected dimensions,"
+ " expected %dx%d, got %dx%d",
+ 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 ?
@@ -763,6 +779,24 @@ JPEGPreDecode(TIFF* tif, tsample_t s)
sp->cinfo.d.comp_info[0].v_samp_factor,
sp->h_sampling, sp->v_sampling);
+ /*
+ * There are potential security issues here
+ * for decoders that have already allocated
+ * buffers based on the expected sampling
+ * factors. Lets check the sampling factors
+ * dont exceed what we were expecting.
+ */
+ 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) {
+ TIFFErrorExt(tif->tif_clientdata,
+ module,
+ "Cannot honour JPEG sampling factors"
+ " that exceed those specified.");
+ return (0);
+ }
+
/*
* XXX: Files written by the Intergraph software
* has different sampling factors stored in the
@@ -953,119 +987,121 @@ JPEGDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
/*ARGSUSED*/ static int
JPEGDecodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
{
- JPEGState *sp = JState(tif);
- tsize_t nrows;
- (void) s;
+ JPEGState *sp = JState(tif);
+ tsize_t nrows;
+ (void) s;
+
+ /* data is expected to be read in multiples of a scanline */
+ if ( (nrows = sp->cinfo.d.image_height) ) {
+ /* Cb,Cr both have sampling factors 1, so this is correct */
+ JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width;
+ int samples_per_clump = sp->samplesperclump;
- /* data is expected to be read in multiples of a scanline */
- if ( (nrows = sp->cinfo.d.image_height) ) {
- /* Cb,Cr both have sampling factors 1, so this is correct */
- JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width;
- int samples_per_clump = sp->samplesperclump;
-
#ifdef JPEG_LIB_MK1
- unsigned short* tmpbuf = _TIFFmalloc(sizeof(unsigned short) *
- sp->cinfo.d.output_width *
- sp->cinfo.d.num_components);
+ unsigned short* tmpbuf = _TIFFmalloc(sizeof(unsigned short) *
+ sp->cinfo.d.output_width *
+ sp->cinfo.d.num_components);
#endif
-
- do {
- jpeg_component_info *compptr;
- int ci, clumpoffset;
-
- /* Reload downsampled-data buffer if needed */
- 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)
- return (0);
- sp->scancount = 0;
- }
- /*
- * Fastest way to unseparate data is to make one pass
- * over the scanline for each row of each component.
- */
- clumpoffset = 0; /* first sample in clump */
- 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++) {
- JSAMPLE *inptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos];
+
+ do {
+ jpeg_component_info *compptr;
+ int ci, clumpoffset;
+
+ /* Reload downsampled-data buffer if needed */
+ 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)
+ return (0);
+ sp->scancount = 0;
+ }
+ /*
+ * Fastest way to unseparate data is to make one pass
+ * over the scanline for each row of each component.
+ */
+ clumpoffset = 0; /* first sample in clump */
+ 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++) {
+ JSAMPLE *inptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos];
#ifdef JPEG_LIB_MK1
- JSAMPLE *outptr = (JSAMPLE*)tmpbuf + clumpoffset;
+ JSAMPLE *outptr = (JSAMPLE*)tmpbuf + clumpoffset;
#else
- JSAMPLE *outptr = (JSAMPLE*)buf + clumpoffset;
+ JSAMPLE *outptr = (JSAMPLE*)buf + clumpoffset;
#endif
- JDIMENSION nclump;
-
- if (hsamp == 1) {
- /* fast path for at least Cb and Cr */
- for (nclump = clumps_per_line; nclump-- > 0; ) {
- outptr[0] = *inptr++;
- outptr += samples_per_clump;
- }
- } else {
- int xpos;
-
- /* general case */
- for (nclump = clumps_per_line; nclump-- > 0; ) {
- for (xpos = 0; xpos < hsamp; xpos++)
- outptr[xpos] = *inptr++;
- outptr += samples_per_clump;
- }
- }
- clumpoffset += hsamp;
- }
- }
+ JDIMENSION nclump;
+
+ if (hsamp == 1) {
+ /* fast path for at least Cb and Cr */
+ for (nclump = clumps_per_line; nclump-- > 0; ) {
+ outptr[0] = *inptr++;
+ outptr += samples_per_clump;
+ }
+ } else {
+ int xpos;
+
+ /* general case */
+ for (nclump = clumps_per_line; nclump-- > 0; ) {
+ for (xpos = 0; xpos < hsamp; xpos++)
+ outptr[xpos] = *inptr++;
+ outptr += samples_per_clump;
+ }
+ }
+ clumpoffset += hsamp;
+ }
+ }
#ifdef JPEG_LIB_MK1
- {
- 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
- { // 12-bit
- 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 = tmpbuf + iPair * 2;
- out_ptr[0] = (in_ptr[0] & 0xff0) >> 4;
- out_ptr[1] = ((in_ptr[0] & 0xf) << 4)
- | ((in_ptr[1] & 0xf00) >> 8);
- out_ptr[2] = ((in_ptr[1] & 0xff) >> 0);
- }
- }
- }
+ {
+ 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
+ { // 12-bit
+ 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 = tmpbuf + iPair * 2;
+ out_ptr[0] = (in_ptr[0] & 0xff0) >> 4;
+ out_ptr[1] = ((in_ptr[0] & 0xf) << 4)
+ | ((in_ptr[1] & 0xf00) >> 8);
+ out_ptr[2] = ((in_ptr[1] & 0xff) >> 0);
+ }
+ }
+ }
#endif
- ++sp->scancount;
- ++tif->tif_row;
- buf += sp->bytesperline;
- cc -= sp->bytesperline;
- } while (--nrows > 0);
-
+ sp->scancount ++;
+ tif->tif_row += sp->v_sampling;
+ /* increment/decrement of buf and cc is still incorrect, but should not matter
+ * TODO: resolve this */
+ buf += sp->bytesperline;
+ cc -= sp->bytesperline;
+ nrows -= sp->v_sampling;
+ } while (nrows > 0);
+
#ifdef JPEG_LIB_MK1
- _TIFFfree(tmpbuf);
+ _TIFFfree(tmpbuf);
#endif
- }
+ }
- /* Close down the decompressor if done. */
- return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
- || TIFFjpeg_finish_decompress(sp);
+ /* Close down the decompressor if done. */
+ return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
+ || TIFFjpeg_finish_decompress(sp);
}
@@ -1200,9 +1236,9 @@ JPEGSetupEncode(TIFF* tif)
/* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */
if (td->td_bitspersample != 8 && td->td_bitspersample != 12)
#else
- if (td->td_bitspersample != BITS_IN_JSAMPLE )
+ if (td->td_bitspersample != BITS_IN_JSAMPLE )
#endif
- {
+ {
TIFFErrorExt(tif->tif_clientdata, module, "BitsPerSample %d not allowed for JPEG",
(int) td->td_bitspersample);
return (0);
@@ -1280,7 +1316,7 @@ JPEGPreEncode(TIFF* tif, tsample_t s)
segment_height = td->td_imagelength - tif->tif_row;
if (segment_height > td->td_rowsperstrip)
segment_height = td->td_rowsperstrip;
- sp->bytesperline = TIFFScanlineSize(tif);
+ sp->bytesperline = TIFFOldScanlineSize(tif);
}
if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) {
/* for PC 2, scale down the strip/tile size
@@ -1393,6 +1429,10 @@ JPEGEncode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
if (cc % sp->bytesperline)
TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline discarded");
+ /* The last strip will be limited to image size */
+ if( !isTiled(tif) && tif->tif_row+nrows > tif->tif_dir.td_imagelength )
+ nrows = tif->tif_dir.td_imagelength - tif->tif_row;
+
while (nrows-- > 0) {
bufptr[0] = (JSAMPROW) buf;
if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
@@ -1419,18 +1459,25 @@ JPEGEncodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
int clumpoffset, ci, xpos, ypos;
jpeg_component_info* compptr;
int samples_per_clump = sp->samplesperclump;
+ tsize_t bytesperclumpline;
(void) s;
assert(sp != NULL);
- /* data is expected to be supplied in multiples of a scanline */
- nrows = cc / sp->bytesperline;
- if (cc % sp->bytesperline)
+ /* data is expected to be supplied in multiples of a clumpline */
+ /* a clumpline is equivalent to v_sampling desubsampled scanlines */
+ /* TODO: the following calculation of bytesperclumpline, should substitute calculation of sp->bytesperline, except that it is per v_sampling lines */
+ bytesperclumpline = (((sp->cinfo.c.image_width+sp->h_sampling-1)/sp->h_sampling)
+ *(sp->h_sampling*sp->v_sampling+2)*sp->cinfo.c.data_precision+7)
+ /8;
+
+ nrows = ( cc / bytesperclumpline ) * sp->v_sampling;
+ if (cc % bytesperclumpline)
TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline discarded");
/* Cb,Cr both have sampling factors 1, so this is correct */
clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width;
- while (nrows-- > 0) {
+ while (nrows > 0) {
/*
* Fastest way to separate the data is to make one pass
* over the scanline for each row of each component.
@@ -1475,9 +1522,9 @@ JPEGEncodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
return (0);
sp->scancount = 0;
}
- if (nrows > 0)
- tif->tif_row++;
+ tif->tif_row += sp->v_sampling;
buf += sp->bytesperline;
+ nrows -= sp->v_sampling;
}
return (1);
}
@@ -1529,6 +1576,7 @@ JPEGCleanup(TIFF* tif)
tif->tif_tagmethods.vgetfield = sp->vgetparent;
tif->tif_tagmethods.vsetfield = sp->vsetparent;
+ tif->tif_tagmethods.printdir = sp->printdir;
if( sp->cinfo_initialized )
TIFFjpeg_destroy(sp); /* release libjpeg resources */
@@ -1540,11 +1588,43 @@ JPEGCleanup(TIFF* tif)
_TIFFSetDefaultCompressionState(tif);
}
+static void
+JPEGResetUpsampled( TIFF* tif )
+{
+ JPEGState* sp = JState(tif);
+ TIFFDirectory* td = &tif->tif_dir;
+
+ /*
+ * Mark whether returned data is up-sampled or not so TIFFStripSize
+ * and TIFFTileSize return values that reflect the true amount of
+ * data.
+ */
+ tif->tif_flags &= ~TIFF_UPSAMPLED;
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
+ if (td->td_photometric == PHOTOMETRIC_YCBCR &&
+ sp->jpegcolormode == JPEGCOLORMODE_RGB) {
+ tif->tif_flags |= TIFF_UPSAMPLED;
+ } else {
+#ifdef notdef
+ if (td->td_ycbcrsubsampling[0] != 1 ||
+ td->td_ycbcrsubsampling[1] != 1)
+ ; /* XXX what about up-sampling? */
+#endif
+ }
+ }
+
+ /*
+ * Must recalculate cached tile size in case sampling state changed.
+ * Should we really be doing this now if image size isn't set?
+ */
+ tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tsize_t) -1;
+}
+
static int
JPEGVSetField(TIFF* tif, ttag_t tag, va_list ap)
{
JPEGState* sp = JState(tif);
- TIFFDirectory* td = &tif->tif_dir;
+ const TIFFFieldInfo* fip;
uint32 v32;
assert(sp != NULL);
@@ -1566,34 +1646,21 @@ JPEGVSetField(TIFF* tif, ttag_t tag, va_list ap)
return (1); /* pseudo tag */
case TIFFTAG_JPEGCOLORMODE:
sp->jpegcolormode = va_arg(ap, int);
- /*
- * Mark whether returned data is up-sampled or not
- * so TIFFStripSize and TIFFTileSize return values
- * that reflect the true amount of data.
- */
- tif->tif_flags &= ~TIFF_UPSAMPLED;
- if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
- if (td->td_photometric == PHOTOMETRIC_YCBCR &&
- sp->jpegcolormode == JPEGCOLORMODE_RGB) {
- tif->tif_flags |= TIFF_UPSAMPLED;
- } else {
- if (td->td_ycbcrsubsampling[0] != 1 ||
- td->td_ycbcrsubsampling[1] != 1)
- ; /* XXX what about up-sampling? */
- }
- }
- /*
- * Must recalculate cached tile size
- * in case sampling state changed.
- */
- tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tsize_t) -1;
+ JPEGResetUpsampled( tif );
return (1); /* pseudo tag */
+ case TIFFTAG_PHOTOMETRIC:
+ {
+ int ret_value = (*sp->vsetparent)(tif, tag, ap);
+ JPEGResetUpsampled( tif );
+ return ret_value;
+ }
case TIFFTAG_JPEGTABLESMODE:
sp->jpegtablesmode = va_arg(ap, int);
return (1); /* pseudo tag */
case TIFFTAG_YCBCRSUBSAMPLING:
/* mark the fact that we have a real ycbcrsubsampling! */
sp->ycbcrsampling_fetched = 1;
+ /* should we be recomputing upsampling info here? */
return (*sp->vsetparent)(tif, tag, ap);
case TIFFTAG_FAXRECVPARAMS:
sp->recvparams = va_arg(ap, uint32);
@@ -1610,7 +1677,13 @@ JPEGVSetField(TIFF* tif, ttag_t tag, va_list ap)
default:
return (*sp->vsetparent)(tif, tag, ap);
}
- TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
+
+ if ((fip = _TIFFFieldWithTag(tif, tag))) {
+ TIFFSetFieldBit(tif, fip->field_bit);
+ } else {
+ return (0);
+ }
+
tif->tif_flags |= TIFF_DIRTYDIRECT;
return (1);
}
@@ -1706,7 +1779,6 @@ JPEGVGetField(TIFF* tif, ttag_t tag, va_list ap)
case TIFFTAG_YCBCRSUBSAMPLING:
JPEGFixupTestSubsampling( tif );
return (*sp->vgetparent)(tif, tag, ap);
- break;
case TIFFTAG_FAXRECVPARAMS:
*va_arg(ap, uint32*) = sp->recvparams;
break;
@@ -1800,8 +1872,18 @@ static int JPEGInitializeLibJPEG( TIFF * tif, int force_encode, int force_decode
int data_is_empty = TRUE;
int decompress;
- if( sp->cinfo_initialized )
- return 1;
+
+ if(sp->cinfo_initialized)
+ {
+ if( force_encode && sp->cinfo.comm.is_decompressor )
+ TIFFjpeg_destroy( sp );
+ else if( force_decode && !sp->cinfo.comm.is_decompressor )
+ TIFFjpeg_destroy( sp );
+ else
+ return 1;
+
+ sp->cinfo_initialized = 0;
+ }
/*
* Do we have tile data already? Make sure we initialize the
@@ -1857,28 +1939,38 @@ TIFFInitJPEG(TIFF* tif, int scheme)
assert(scheme == COMPRESSION_JPEG);
/*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFieldInfo(tif, jpegFieldInfo, N(jpegFieldInfo))) {
+ TIFFErrorExt(tif->tif_clientdata,
+ "TIFFInitJPEG",
+ "Merging JPEG codec-specific tags failed");
+ return 0;
+ }
+
+ /*
* Allocate state block so tag methods have storage to record values.
*/
tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (JPEGState));
if (tif->tif_data == NULL) {
- TIFFErrorExt(tif->tif_clientdata, "TIFFInitJPEG", "No space for JPEG state block");
- return (0);
+ TIFFErrorExt(tif->tif_clientdata,
+ "TIFFInitJPEG", "No space for JPEG state block");
+ return 0;
}
- _TIFFmemset( tif->tif_data, 0, sizeof(JPEGState));
+ _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState));
sp = JState(tif);
sp->tif = tif; /* back link */
/*
- * Merge codec-specific tag information and override parent get/set
- * field methods.
+ * Override parent get/set field methods.
*/
- _TIFFMergeFieldInfo(tif, jpegFieldInfo, N(jpegFieldInfo));
sp->vgetparent = tif->tif_tagmethods.vgetfield;
tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
sp->vsetparent = tif->tif_tagmethods.vsetfield;
tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
+ sp->printdir = tif->tif_tagmethods.printdir;
tif->tif_tagmethods.printdir = JPEGPrintDir; /* hook for codec tags */
/* Default values for codec-specific fields */
@@ -1944,3 +2036,4 @@ TIFFInitJPEG(TIFF* tif, int scheme)
#endif /* JPEG_SUPPORT */
/* vim: set ts=8 sts=8 sw=8 noet: */
+
diff --git a/src/libtiff/tif_luv.c b/src/libtiff/tif_luv.c
index 484bc4f..db1b746 100644
--- a/src/libtiff/tif_luv.c
+++ b/src/libtiff/tif_luv.c
@@ -1,4 +1,4 @@
-/* $Id: tif_luv.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_luv.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1997 Greg Ward Larson
@@ -451,7 +451,7 @@ LogL16Encode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
tif->tif_rawcp = op;
tif->tif_rawcc = tif->tif_rawdatasize - occ;
- return (0);
+ return (1);
}
/*
@@ -496,7 +496,7 @@ LogLuvEncode24(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
tif->tif_rawcp = op;
tif->tif_rawcc = tif->tif_rawdatasize - occ;
- return (0);
+ return (1);
}
/*
@@ -585,7 +585,7 @@ LogLuvEncode32(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
tif->tif_rawcp = op;
tif->tif_rawcc = tif->tif_rawdatasize - occ;
- return (0);
+ return (1);
}
/*
@@ -598,7 +598,7 @@ LogLuvEncodeStrip(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
tsize_t rowlen = TIFFScanlineSize(tif);
assert(cc%rowlen == 0);
- while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 0)
+ while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
bp += rowlen, cc -= rowlen;
return (cc == 0);
}
@@ -613,7 +613,7 @@ LogLuvEncodeTile(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
tsize_t rowlen = TIFFTileRowSize(tif);
assert(cc%rowlen == 0);
- while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 0)
+ while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
bp += rowlen, cc -= rowlen;
return (cc == 0);
}
@@ -1200,7 +1200,10 @@ LogL16InitState(TIFF* tif)
"No support for converting user data format to LogL");
return (0);
}
- sp->tbuflen = multiply(td->td_imagewidth, td->td_rowsperstrip);
+ if( isTiled(tif) )
+ sp->tbuflen = multiply(td->td_tilewidth, td->td_tilelength);
+ else
+ sp->tbuflen = multiply(td->td_imagewidth, td->td_rowsperstrip);
if (multiply(sp->tbuflen, sizeof (int16)) == 0 ||
(sp->tbuf = (tidata_t*) _TIFFmalloc(sp->tbuflen * sizeof (int16))) == NULL) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: No space for SGILog translation buffer",
@@ -1298,7 +1301,10 @@ LogLuvInitState(TIFF* tif)
"No support for converting user data format to LogLuv");
return (0);
}
- sp->tbuflen = multiply(td->td_imagewidth, td->td_rowsperstrip);
+ if( isTiled(tif) )
+ sp->tbuflen = multiply(td->td_tilewidth, td->td_tilelength);
+ else
+ sp->tbuflen = multiply(td->td_imagewidth, td->td_rowsperstrip);
if (multiply(sp->tbuflen, sizeof (uint32)) == 0 ||
(sp->tbuf = (tidata_t*) _TIFFmalloc(sp->tbuflen * sizeof (uint32))) == NULL) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: No space for SGILog translation buffer",
@@ -1561,6 +1567,16 @@ TIFFInitSGILog(TIFF* tif, int scheme)
assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG);
/*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFieldInfo(tif, LogLuvFieldInfo,
+ TIFFArrayCount(LogLuvFieldInfo))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging SGILog codec-specific tags failed");
+ return 0;
+ }
+
+ /*
* Allocate state block so tag methods have storage to record values.
*/
tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (LogLuvState));
@@ -1587,9 +1603,9 @@ TIFFInitSGILog(TIFF* tif, int scheme)
tif->tif_close = LogLuvClose;
tif->tif_cleanup = LogLuvCleanup;
- /* override SetField so we can handle our private pseudo-tag */
- _TIFFMergeFieldInfo(tif, LogLuvFieldInfo,
- TIFFArrayCount(LogLuvFieldInfo));
+ /*
+ * Override parent get/set field methods.
+ */
sp->vgetparent = tif->tif_tagmethods.vgetfield;
tif->tif_tagmethods.vgetfield = LogLuvVGetField; /* hook for codec tags */
sp->vsetparent = tif->tif_tagmethods.vsetfield;
diff --git a/src/libtiff/tif_lzw.c b/src/libtiff/tif_lzw.c
index 79d4091..ba47c34 100644
--- a/src/libtiff/tif_lzw.c
+++ b/src/libtiff/tif_lzw.c
@@ -1,4 +1,4 @@
-/* $Id: tif_lzw.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_lzw.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -224,7 +224,8 @@ LZWSetupDecode(TIFF* tif)
if (sp->dec_codetab == NULL) {
sp->dec_codetab = (code_t*)_TIFFmalloc(CSIZE*sizeof (code_t));
if (sp->dec_codetab == NULL) {
- TIFFErrorExt(tif->tif_clientdata, module, "No space for LZW code table");
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "No space for LZW code table");
return (0);
}
/*
@@ -237,6 +238,11 @@ LZWSetupDecode(TIFF* tif)
sp->dec_codetab[code].length = 1;
sp->dec_codetab[code].next = NULL;
} while (code--);
+ /*
+ * Zero-out the unused entries
+ */
+ _TIFFmemset(&sp->dec_codetab[CODE_CLEAR], 0,
+ (CODE_FIRST - CODE_CLEAR) * sizeof (code_t));
}
return (1);
}
@@ -251,6 +257,11 @@ LZWPreDecode(TIFF* tif, tsample_t s)
(void) s;
assert(sp != NULL);
+ if( sp->dec_codetab == NULL )
+ {
+ tif->tif_setupdecode( tif );
+ }
+
/*
* Check for old bit-reversed codes.
*/
@@ -350,6 +361,7 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
(void) s;
assert(sp != NULL);
+ assert(sp->dec_codetab != NULL);
/*
* Restart interrupted output operation.
*/
@@ -408,12 +420,20 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
break;
if (code == CODE_CLEAR) {
free_entp = sp->dec_codetab + CODE_FIRST;
+ _TIFFmemset(free_entp, 0,
+ (CSIZE - CODE_FIRST) * sizeof (code_t));
nbits = BITS_MIN;
nbitsmask = MAXCODE(BITS_MIN);
maxcodep = sp->dec_codetab + nbitsmask-1;
NextCode(tif, sp, bp, code, GetNextCode);
if (code == CODE_EOI)
break;
+ if (code == CODE_CLEAR) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "LZWDecode: Corrupted LZW table at scanline %d",
+ tif->tif_row);
+ return (0);
+ }
*op++ = (char)code, occ--;
oldcodep = sp->dec_codetab + code;
continue;
@@ -514,7 +534,7 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
if (occ > 0) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "LZWDecode: Not enough data at scanline %d (short %d bytes)",
+ "LZWDecode: Not enough data at scanline %d (short %ld bytes)",
tif->tif_row, occ);
return (0);
}
@@ -604,12 +624,20 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
break;
if (code == CODE_CLEAR) {
free_entp = sp->dec_codetab + CODE_FIRST;
+ _TIFFmemset(free_entp, 0,
+ (CSIZE - CODE_FIRST) * sizeof (code_t));
nbits = BITS_MIN;
nbitsmask = MAXCODE(BITS_MIN);
maxcodep = sp->dec_codetab + nbitsmask;
NextCode(tif, sp, bp, code, GetNextCodeCompat);
if (code == CODE_EOI)
break;
+ if (code == CODE_CLEAR) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "LZWDecode: Corrupted LZW table at scanline %d",
+ tif->tif_row);
+ return (0);
+ }
*op++ = code, occ--;
oldcodep = sp->dec_codetab + code;
continue;
@@ -647,6 +675,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
}
oldcodep = codep;
if (code >= 256) {
+ char *op_orig = op;
/*
* Code maps to a string, copy string
* value to output (written in reverse).
@@ -681,7 +710,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
tp = op;
do {
*--tp = codep->value;
- } while( (codep = codep->next) != NULL);
+ } while( (codep = codep->next) != NULL && tp > op_orig);
} else
*op++ = code, occ--;
}
@@ -697,7 +726,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
if (occ > 0) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)",
+ "LZWDecodeCompat: Not enough data at scanline %d (short %ld bytes)",
tif->tif_row, occ);
return (0);
}
@@ -734,6 +763,12 @@ LZWPreEncode(TIFF* tif, tsample_t s)
(void) s;
assert(sp != NULL);
+
+ if( sp->enc_hashtab == NULL )
+ {
+ tif->tif_setupencode( tif );
+ }
+
sp->lzw_nbits = BITS_MIN;
sp->lzw_maxcode = MAXCODE(BITS_MIN);
sp->lzw_free_ent = CODE_FIRST;
@@ -803,6 +838,9 @@ LZWEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
(void) s;
if (sp == NULL)
return (0);
+
+ assert(sp->enc_hashtab != NULL);
+
/*
* Load local state.
*/
diff --git a/src/libtiff/tif_next.c b/src/libtiff/tif_next.c
index 7223f7b..3cb0085 100644
--- a/src/libtiff/tif_next.c
+++ b/src/libtiff/tif_next.c
@@ -1,4 +1,4 @@
-/* $Id: tif_next.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_next.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -48,11 +48,10 @@
static int
NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
{
- register unsigned char *bp, *op;
- register tsize_t cc;
- register int n;
+ unsigned char *bp, *op;
+ tsize_t cc;
tidata_t row;
- tsize_t scanline;
+ tsize_t scanline, n;
(void) s;
/*
@@ -66,7 +65,7 @@ NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
bp = (unsigned char *)tif->tif_rawcp;
cc = tif->tif_rawcc;
scanline = tif->tif_scanlinesize;
- for (row = buf; (long)occ > 0; occ -= scanline, row += scanline) {
+ for (row = buf; occ > 0; occ -= scanline, row += scanline) {
n = *bp++, cc--;
switch (n) {
case LITERALROW:
@@ -80,10 +79,10 @@ NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
cc -= scanline;
break;
case LITERALSPAN: {
- int off;
+ tsize_t off;
/*
- * The scanline has a literal span
- * that begins at some offset.
+ * The scanline has a literal span that begins at some
+ * offset.
*/
off = (bp[0] * 256) + bp[1];
n = (bp[2] * 256) + bp[3];
@@ -95,23 +94,27 @@ NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
break;
}
default: {
- register int npixels = 0, grey;
- unsigned long imagewidth = tif->tif_dir.td_imagewidth;
+ uint32 npixels = 0, grey;
+ uint32 imagewidth = tif->tif_dir.td_imagewidth;
/*
- * The scanline is composed of a sequence
- * of constant color ``runs''. We shift
- * into ``run mode'' and interpret bytes
- * as codes of the form <color><npixels>
- * until we've filled the scanline.
+ * The scanline is composed of a sequence of constant
+ * color ``runs''. We shift into ``run mode'' and
+ * interpret bytes as codes of the form
+ * <color><npixels> until we've filled the scanline.
*/
op = row;
for (;;) {
grey = (n>>6) & 0x3;
n &= 0x3f;
- while (n-- > 0)
+ /*
+ * Ensure the run does not exceed the scanline
+ * bounds, potentially resulting in a security
+ * issue.
+ */
+ while (n-- > 0 && npixels < imagewidth)
SETPIXEL(op, grey);
- if (npixels >= (int) imagewidth)
+ if (npixels >= imagewidth)
break;
if (cc == 0)
goto bad;
diff --git a/src/libtiff/tif_ojpeg.c b/src/libtiff/tif_ojpeg.c
index ecc5e08..f9f0508 100644
--- a/src/libtiff/tif_ojpeg.c
+++ b/src/libtiff/tif_ojpeg.c
@@ -1,2629 +1,2424 @@
-/* $Id: tif_ojpeg.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_ojpeg.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
+
+/* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
+ specification is now totally obsolete and deprecated for new applications and
+ images. This file was was created solely in order to read unconverted images
+ still present on some users' computer systems. It will never be extended
+ to write such files. Writing new-style JPEG compressed TIFFs is implemented
+ in tif_jpeg.c.
+
+ The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
+ testfiles, and anticipate as much as possible all other... But still, it may
+ fail on some. If you encounter problems, please report them on the TIFF
+ mailing list and/or to Joris Van Damme <info@awaresystems.be>.
+
+ Please read the file called "TIFF Technical Note #2" if you need to be
+ convinced this compression scheme is bad and breaks TIFF. That document
+ is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
+ and from AWare Systems' TIFF section
+ <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
+ in Adobe's specification supplements, marked "draft" up to this day, but
+ supported by the TIFF community.
+
+ This file interfaces with Release 6B of the JPEG Library written by the
+ Independent JPEG Group. Previous versions of this file required a hack inside
+ the LibJpeg library. This version no longer requires that. Remember to
+ remove the hack if you update from the old version.
+
+ Copyright (c) Joris Van Damme <info@awaresystems.be>
+ Copyright (c) AWare Systems <http://www.awaresystems.be/>
+
+ The licence agreement for this file is the same as the rest of the LibTiff
+ library.
+
+ IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
+ ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ OF THIS SOFTWARE.
+
+ Joris Van Damme and/or AWare Systems may be available for custom
+ developement. If you like what you see, and need anything similar or related,
+ contact <info@awaresystems.be>.
+*/
+
+/* What is what, and what is not?
+
+ This decoder starts with an input stream, that is essentially the JpegInterchangeFormat
+ stream, if any, followed by the strile data, if any. This stream is read in
+ OJPEGReadByte and related functions.
+
+ It analyzes the start of this stream, until it encounters non-marker data, i.e.
+ compressed image data. Some of the header markers it sees have no actual content,
+ like the SOI marker, and APP/COM markers that really shouldn't even be there. Some
+ other markers do have content, and the valuable bits and pieces of information
+ in these markers are saved, checking all to verify that the stream is more or
+ less within expected bounds. This happens inside the OJPEGReadHeaderInfoSecStreamXxx
+ functions.
+
+ Some OJPEG imagery contains no valid JPEG header markers. This situation is picked
+ up on if we've seen no SOF marker when we're at the start of the compressed image
+ data. In this case, the tables are read from JpegXxxTables tags, and the other
+ bits and pieces of information is initialized to its most basic value. This is
+ implemented in the OJPEGReadHeaderInfoSecTablesXxx functions.
+
+ When this is complete, a good and valid JPEG header can be assembled, and this is
+ passed through to LibJpeg. When that's done, the remainder of the input stream, i.e.
+ the compressed image data, can be passed through unchanged. This is done in
+ OJPEGWriteStream functions.
+
+ LibTiff rightly expects to know the subsampling values before decompression. Just like
+ in new-style JPEG-in-TIFF, though, or even more so, actually, the YCbCrsubsampling
+ tag is notoriously unreliable. To correct these tag values with the ones inside
+ the JPEG stream, the first part of the input stream is pre-scanned in
+ OJPEGSubsamplingCorrect, making no note of any other data, reporting no warnings
+ or errors, up to the point where either these values are read, or it's clear they
+ aren't there. This means that some of the data is read twice, but we feel speed
+ in correcting these values is important enough to warrant this sacrifice. Allthough
+ there is currently no define or other configuration mechanism to disable this behaviour,
+ the actual header scanning is build to robustly respond with error report if it
+ should encounter an uncorrected mismatch of subsampling values. See
+ OJPEGReadHeaderInfoSecStreamSof.
+
+ The restart interval and restart markers are the most tricky part... The restart
+ interval can be specified in a tag. It can also be set inside the input JPEG stream.
+ It can be used inside the input JPEG stream. If reading from strile data, we've
+ consistenly discovered the need to insert restart markers in between the different
+ striles, as is also probably the most likely interpretation of the original TIFF 6.0
+ specification. With all this setting of interval, and actual use of markers that is not
+ predictable at the time of valid JPEG header assembly, the restart thing may turn
+ out the Achilles heel of this implementation. Fortunately, most OJPEG writer vendors
+ succeed in reading back what they write, which may be the reason why we've been able
+ to discover ways that seem to work.
+
+ Some special provision is made for planarconfig separate OJPEG files. These seem
+ to consistently contain header info, a SOS marker, a plane, SOS marker, plane, SOS,
+ and plane. This may or may not be a valid JPEG configuration, we don't know and don't
+ care. We want LibTiff to be able to access the planes individually, without huge
+ buffering inside LibJpeg, anyway. So we compose headers to feed to LibJpeg, in this
+ case, that allow us to pass a single plane such that LibJpeg sees a valid
+ single-channel JPEG stream. Locating subsequent SOS markers, and thus subsequent
+ planes, is done inside OJPEGReadSecondarySos.
+
+ The benefit of the scheme is... that it works, basically. We know of no other that
+ does. It works without checking software tag, or otherwise going about things in an
+ OJPEG flavor specific manner. Instead, it is a single scheme, that covers the cases
+ with and without JpegInterchangeFormat, with and without striles, with part of
+ the header in JpegInterchangeFormat and remainder in first strile, etc. It is forgiving
+ and robust, may likely work with OJPEG flavors we've not seen yet, and makes most out
+ of the data.
+
+ Another nice side-effect is that a complete JPEG single valid stream is build if
+ planarconfig is not separate (vast majority). We may one day use that to build
+ converters to JPEG, and/or to new-style JPEG compression inside TIFF.
+
+ A dissadvantage is the lack of random access to the individual striles. This is the
+ reason for much of the complicated restart-and-position stuff inside OJPEGPreDecode.
+ Applications would do well accessing all striles in order, as this will result in
+ a single sequential scan of the input stream, and no restarting of LibJpeg decoding
+ session.
+*/
+
#include "tiffiop.h"
#ifdef OJPEG_SUPPORT
-/* JPEG Compression support, as per the original TIFF 6.0 specification.
-
- WARNING: KLUDGE ALERT! The type of JPEG encapsulation defined by the TIFF
- Version 6.0 specification is now totally obsolete and
- deprecated for new applications and images. This file is an unsupported hack
- that was created solely in order to read (but NOT write!) a few old,
- unconverted images still present on some users' computer systems. The code
- isn't pretty or robust, and it won't read every "old format" JPEG-in-TIFF
- file (see Samuel Leffler's draft "TIFF Technical Note No. 2" for a long and
- incomplete list of known problems), but it seems to work well enough in the
- few cases of practical interest to the author; so, "caveat emptor"! This
- file should NEVER be enhanced to write new images using anything other than
- the latest approved JPEG-in-TIFF encapsulation method, implemented by the
- "tif_jpeg.c" file elsewhere in this library.
-
- This file interfaces with Release 6B of the JPEG Library written by theu
- Independent JPEG Group, which you can find on the Internet at:
- ftp://ftp.uu.net:/graphics/jpeg/.
-
- The "C" Preprocessor macros, "[CD]_LOSSLESS_SUPPORTED", are defined by your
- JPEG Library Version 6B only if you have applied a (massive!) patch by Ken
- Murchison of Oceana Matrix Ltd. <ken@oceana.com> to support lossless Huffman
- encoding (TIFF "JPEGProc" tag value = 14). This patch can be found on the
- Internet at: ftp://ftp.oceana.com/pub/ljpeg-6b.tar.gz.
-
- Some old files produced by the Wang Imaging application for Microsoft Windows
- apparently can be decoded only with a special patch to the JPEG Library,
- which defines a subroutine named "jpeg_reset_huff_decode()" in its "jdhuff.c"
- module (the "jdshuff.c" module, if Ken Murchison's patch has been applied).
- Unfortunately the patch differs slightly in each case, and some TIFF Library
- have reported problems finding the code, so both versions appear below; you
- should carefully extract and apply only the version that applies to your JPEG
- Library!
-
- Contributed by Scott Marovich <marovich@hpl.hp.com> with considerable help
- from Charles Auer <Bumble731@msn.com> to unravel the mysteries of image files
- created by the Wang Imaging application for Microsoft Windows.
-*/
-#if 0 /* Patch for JPEG Library WITHOUT lossless Huffman coding */
-*** jdhuff.c.orig Mon Oct 20 17:51:10 1997
---- jdhuff.c Sun Nov 11 17:33:58 2001
-***************
-*** 648,651 ****
---- 648,683 ----
- for (i = 0; i < NUM_HUFF_TBLS; i++) {
- entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
- }
- }
-+
-+ /*
-+ * BEWARE OF KLUDGE: This subroutine is a hack for decoding illegal JPEG-in-
-+ * TIFF encapsulations produced by Microsoft's Wang Imaging
-+ * for Windows application with the public-domain TIFF Library. Based upon an
-+ * examination of selected output files, this program apparently divides a JPEG
-+ * bit-stream into consecutive horizontal TIFF "strips", such that the JPEG
-+ * encoder's/decoder's DC coefficients for each image component are reset before
-+ * each "strip". Moreover, a "strip" is not necessarily encoded in a multiple
-+ * of 8 bits, so one must sometimes discard 1-7 bits at the end of each "strip"
-+ * for alignment to the next input-Byte storage boundary. IJG JPEG Library
-+ * decoder state is not normally exposed to client applications, so this sub-
-+ * routine provides the TIFF Library with a "hook" to make these corrections.
-+ * It should be called after "jpeg_start_decompress()" and before
-+ * "jpeg_finish_decompress()", just before decoding each "strip" using
-+ * "jpeg_read_raw_data()" or "jpeg_read_scanlines()".
-+ *
-+ * This kludge is not sanctioned or supported by the Independent JPEG Group, and
-+ * future changes to the IJG JPEG Library might invalidate it. Do not send bug
-+ * reports about this code to IJG developers. Instead, contact the author for
-+ * advice: Scott B. Marovich <marovich@hpl.hp.com>, Hewlett-Packard Labs, 6/01.
-+ */
-+ GLOBAL(void)
-+ jpeg_reset_huff_decode (register j_decompress_ptr cinfo)
-+ { register huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
-+ register int ci = 0;
-+
-+ /* Discard encoded input bits, up to the next Byte boundary */
-+ entropy->bitstate.bits_left &= ~7;
-+ /* Re-initialize DC predictions to 0 */
-+ do entropy->saved.last_dc_val[ci] = 0; while (++ci < cinfo->comps_in_scan);
-+ }
-#endif /* Patch for JPEG Library WITHOUT lossless Huffman coding */
-#if 0 /* Patch for JPEG Library WITH lossless Huffman coding */
-*** jdshuff.c.orig Mon Mar 11 16:44:54 2002
---- jdshuff.c Mon Mar 11 16:44:54 2002
-***************
-*** 357,360 ****
---- 357,393 ----
- for (i = 0; i < NUM_HUFF_TBLS; i++) {
- entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
- }
- }
-+
-+ /*
-+ * BEWARE OF KLUDGE: This subroutine is a hack for decoding illegal JPEG-in-
-+ * TIFF encapsulations produced by Microsoft's Wang Imaging
-+ * for Windows application with the public-domain TIFF Library. Based upon an
-+ * examination of selected output files, this program apparently divides a JPEG
-+ * bit-stream into consecutive horizontal TIFF "strips", such that the JPEG
-+ * encoder's/decoder's DC coefficients for each image component are reset before
-+ * each "strip". Moreover, a "strip" is not necessarily encoded in a multiple
-+ * of 8 bits, so one must sometimes discard 1-7 bits at the end of each "strip"
-+ * for alignment to the next input-Byte storage boundary. IJG JPEG Library
-+ * decoder state is not normally exposed to client applications, so this sub-
-+ * routine provides the TIFF Library with a "hook" to make these corrections.
-+ * It should be called after "jpeg_start_decompress()" and before
-+ * "jpeg_finish_decompress()", just before decoding each "strip" using
-+ * "jpeg_read_raw_data()" or "jpeg_read_scanlines()".
-+ *
-+ * This kludge is not sanctioned or supported by the Independent JPEG Group, and
-+ * future changes to the IJG JPEG Library might invalidate it. Do not send bug
-+ * reports about this code to IJG developers. Instead, contact the author for
-+ * advice: Scott B. Marovich <marovich@hpl.hp.com>, Hewlett-Packard Labs, 6/01.
-+ */
-+ GLOBAL(void)
-+ jpeg_reset_huff_decode (register j_decompress_ptr cinfo)
-+ { register shuff_entropy_ptr entropy = (shuff_entropy_ptr)
-+ ((j_lossy_d_ptr)cinfo->codec)->entropy_private;
-+ register int ci = 0;
-+
-+ /* Discard encoded input bits, up to the next Byte boundary */
-+ entropy->bitstate.bits_left &= ~7;
-+ /* Re-initialize DC predictions to 0 */
-+ do entropy->saved.last_dc_val[ci] = 0; while (++ci < cinfo->comps_in_scan);
-+ }
-#endif /* Patch for JPEG Library WITH lossless Huffman coding */
+/* Configuration defines here are:
+ * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some environments,
+ * like eg LibTiffDelphi, this is not possible. For this reason, the actual calls to
+ * libjpeg, with longjump stuff, are encapsulated in dedicated functions. When
+ * JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declared external
+ * to this unit, and can be defined elsewhere to use stuff other then longjump.
+ * The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encapsulators
+ * here, internally, with normal longjump.
+ * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
+ * conviniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
+ * in place of plain setjmp. These macros will make it easier. It is useless
+ * to fiddle with these if you define JPEG_ENCAP_EXTERNAL.
+ * OJPEG_BUFFER: Define the size of the desired buffer here. Should be small enough so as to guarantee
+ * instant processing, optimal streaming and optimal use of processor cache, but also big
+ * enough so as to not result in significant call overhead. It should be at least a few
+ * bytes to accomodate some structures (this is verified in asserts), but it would not be
+ * sensible to make it this small anyway, and it should be at most 64K since it is indexed
+ * with uint16. We recommend 2K.
+ * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used anywhere and has
+ * absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
+ */
+
+/* #define LIBJPEG_ENCAP_EXTERNAL */
+#define SETJMP(jbuf) setjmp(jbuf)
+#define LONGJMP(jbuf,code) longjmp(jbuf,code)
+#define JMP_BUF jmp_buf
+#define OJPEG_BUFFER 2048
+/* define EGYPTIANWALK */
+
+#define JPEG_MARKER_SOF0 0xC0
+#define JPEG_MARKER_SOF1 0xC1
+#define JPEG_MARKER_SOF3 0xC3
+#define JPEG_MARKER_DHT 0xC4
+#define JPEG_MARKER_RST0 0XD0
+#define JPEG_MARKER_SOI 0xD8
+#define JPEG_MARKER_EOI 0xD9
+#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
+
+#define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
+#define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
+#define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
+#define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
+#define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
+#define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
+#define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
+#define FIELD_OJPEG_COUNT 7
+
+static const TIFFFieldInfo ojpeg_field_info[] = {
+ {TIFFTAG_JPEGIFOFFSET,1,1,TIFF_LONG,FIELD_OJPEG_JPEGINTERCHANGEFORMAT,TRUE,FALSE,"JpegInterchangeFormat"},
+ {TIFFTAG_JPEGIFBYTECOUNT,1,1,TIFF_LONG,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH,TRUE,FALSE,"JpegInterchangeFormatLength"},
+ {TIFFTAG_JPEGQTABLES,TIFF_VARIABLE,TIFF_VARIABLE,TIFF_LONG,FIELD_OJPEG_JPEGQTABLES,FALSE,TRUE,"JpegQTables"},
+ {TIFFTAG_JPEGDCTABLES,TIFF_VARIABLE,TIFF_VARIABLE,TIFF_LONG,FIELD_OJPEG_JPEGDCTABLES,FALSE,TRUE,"JpegDcTables"},
+ {TIFFTAG_JPEGACTABLES,TIFF_VARIABLE,TIFF_VARIABLE,TIFF_LONG,FIELD_OJPEG_JPEGACTABLES,FALSE,TRUE,"JpegAcTables"},
+ {TIFFTAG_JPEGPROC,1,1,TIFF_SHORT,FIELD_OJPEG_JPEGPROC,FALSE,FALSE,"JpegProc"},
+ {TIFFTAG_JPEGRESTARTINTERVAL,1,1,TIFF_SHORT,FIELD_OJPEG_JPEGRESTARTINTERVAL,FALSE,FALSE,"JpegRestartInterval"},
+};
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
#include <setjmp.h>
-#include <stdio.h>
-#ifdef FAR
-#undef FAR /* Undefine FAR to avoid conflict with JPEG definition */
#endif
-#define JPEG_INTERNALS /* Include "jpegint.h" for "DSTATE_*" symbols */
-#define JPEG_CJPEG_DJPEG /* Include all Version 6B+ "jconfig.h" options */
-#undef INLINE
+
#include "jpeglib.h"
-#undef JPEG_CJPEG_DJPEG
-#undef JPEG_INTERNALS
+#include "jerror.h"
-/* Hack for files produced by Wang Imaging application on Microsoft Windows */
-extern void jpeg_reset_huff_decode(j_decompress_ptr);
+typedef struct jpeg_error_mgr jpeg_error_mgr;
+typedef struct jpeg_common_struct jpeg_common_struct;
+typedef struct jpeg_decompress_struct jpeg_decompress_struct;
+typedef struct jpeg_source_mgr jpeg_source_mgr;
-/* On some machines, it may be worthwhile to use "_setjmp()" or "sigsetjmp()"
- instead of "setjmp()". These macros make it easier:
-*/
-#define SETJMP(jbuf)setjmp(jbuf)
-#define LONGJMP(jbuf,code)longjmp(jbuf,code)
-#define JMP_BUF jmp_buf
+typedef enum {
+ osibsNotSetYet,
+ osibsJpegInterchangeFormat,
+ osibsStrile,
+ osibsEof
+} OJPEGStateInBufferSource;
+
+typedef enum {
+ ososSoi,
+ ososQTable0,ososQTable1,ososQTable2,ososQTable3,
+ ososDcTable0,ososDcTable1,ososDcTable2,ososDcTable3,
+ ososAcTable0,ososAcTable1,ososAcTable2,ososAcTable3,
+ ososDri,
+ ososSof,
+ ososSos,
+ ososCompressed,
+ ososRst,
+ ososEoi
+} OJPEGStateOutState;
+
+typedef struct {
+ TIFF* tif;
+ #ifndef LIBJPEG_ENCAP_EXTERNAL
+ JMP_BUF exit_jmpbuf;
+ #endif
+ TIFFVGetMethod vgetparent;
+ TIFFVSetMethod vsetparent;
+ toff_t file_size;
+ uint32 image_width;
+ uint32 image_length;
+ uint32 strile_width;
+ uint32 strile_length;
+ uint32 strile_length_total;
+ uint8 samples_per_pixel;
+ uint8 plane_sample_offset;
+ uint8 samples_per_pixel_per_plane;
+ toff_t jpeg_interchange_format;
+ toff_t jpeg_interchange_format_length;
+ uint8 jpeg_proc;
+ uint8 subsamplingcorrect;
+ uint8 subsamplingcorrect_done;
+ uint8 subsampling_tag;
+ uint8 subsampling_hor;
+ uint8 subsampling_ver;
+ uint8 subsampling_force_desubsampling_inside_decompression;
+ uint8 qtable_offset_count;
+ uint8 dctable_offset_count;
+ uint8 actable_offset_count;
+ toff_t qtable_offset[3];
+ toff_t dctable_offset[3];
+ toff_t actable_offset[3];
+ uint8* qtable[4];
+ uint8* dctable[4];
+ uint8* actable[4];
+ uint16 restart_interval;
+ uint8 restart_index;
+ uint8 sof_log;
+ uint8 sof_marker_id;
+ uint32 sof_x;
+ uint32 sof_y;
+ uint8 sof_c[3];
+ uint8 sof_hv[3];
+ uint8 sof_tq[3];
+ uint8 sos_cs[3];
+ uint8 sos_tda[3];
+ struct {
+ uint8 log;
+ OJPEGStateInBufferSource in_buffer_source;
+ tstrile_t in_buffer_next_strile;
+ toff_t in_buffer_file_pos;
+ toff_t in_buffer_file_togo;
+ } sos_end[3];
+ uint8 readheader_done;
+ uint8 writeheader_done;
+ tsample_t write_cursample;
+ tstrile_t write_curstrile;
+ uint8 libjpeg_session_active;
+ uint8 libjpeg_jpeg_query_style;
+ jpeg_error_mgr libjpeg_jpeg_error_mgr;
+ jpeg_decompress_struct libjpeg_jpeg_decompress_struct;
+ jpeg_source_mgr libjpeg_jpeg_source_mgr;
+ uint8 subsampling_convert_log;
+ uint32 subsampling_convert_ylinelen;
+ uint32 subsampling_convert_ylines;
+ uint32 subsampling_convert_clinelen;
+ uint32 subsampling_convert_clines;
+ uint32 subsampling_convert_ybuflen;
+ uint32 subsampling_convert_cbuflen;
+ uint32 subsampling_convert_ycbcrbuflen;
+ uint8* subsampling_convert_ycbcrbuf;
+ uint8* subsampling_convert_ybuf;
+ uint8* subsampling_convert_cbbuf;
+ uint8* subsampling_convert_crbuf;
+ uint32 subsampling_convert_ycbcrimagelen;
+ uint8** subsampling_convert_ycbcrimage;
+ uint32 subsampling_convert_clinelenout;
+ uint32 subsampling_convert_state;
+ uint32 bytes_per_line; /* if the codec outputs subsampled data, a 'line' in bytes_per_line */
+ uint32 lines_per_strile; /* and lines_per_strile means subsampling_ver desubsampled rows */
+ OJPEGStateInBufferSource in_buffer_source;
+ tstrile_t in_buffer_next_strile;
+ tstrile_t in_buffer_strile_count;
+ toff_t in_buffer_file_pos;
+ uint8 in_buffer_file_pos_log;
+ toff_t in_buffer_file_togo;
+ uint16 in_buffer_togo;
+ uint8* in_buffer_cur;
+ uint8 in_buffer[OJPEG_BUFFER];
+ OJPEGStateOutState out_state;
+ uint8 out_buffer[OJPEG_BUFFER];
+ uint8* skip_buffer;
+} OJPEGState;
+
+static int OJPEGVGetField(TIFF* tif, ttag_t tag, va_list ap);
+static int OJPEGVSetField(TIFF* tif, ttag_t tag, va_list ap);
+static void OJPEGPrintDir(TIFF* tif, FILE* fd, long flags);
+
+static int OJPEGSetupDecode(TIFF* tif);
+static int OJPEGPreDecode(TIFF* tif, tsample_t s);
+static int OJPEGPreDecodeSkipRaw(TIFF* tif);
+static int OJPEGPreDecodeSkipScanlines(TIFF* tif);
+static int OJPEGDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s);
+static int OJPEGDecodeRaw(TIFF* tif, tidata_t buf, tsize_t cc);
+static int OJPEGDecodeScanlines(TIFF* tif, tidata_t buf, tsize_t cc);
+static void OJPEGPostDecode(TIFF* tif, tidata_t buf, tsize_t cc);
+static int OJPEGSetupEncode(TIFF* tif);
+static int OJPEGPreEncode(TIFF* tif, tsample_t s);
+static int OJPEGEncode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s);
+static int OJPEGPostEncode(TIFF* tif);
+static void OJPEGCleanup(TIFF* tif);
+
+static void OJPEGSubsamplingCorrect(TIFF* tif);
+static int OJPEGReadHeaderInfo(TIFF* tif);
+static int OJPEGReadSecondarySos(TIFF* tif, tsample_t s);
+static int OJPEGWriteHeaderInfo(TIFF* tif);
+static void OJPEGLibjpegSessionAbort(TIFF* tif);
+
+static int OJPEGReadHeaderInfoSec(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamDri(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamDht(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id);
+static int OJPEGReadHeaderInfoSecStreamSos(TIFF* tif);
+static int OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif);
+static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif);
+static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif);
+
+static int OJPEGReadBufferFill(OJPEGState* sp);
+static int OJPEGReadByte(OJPEGState* sp, uint8* byte);
+static int OJPEGReadBytePeek(OJPEGState* sp, uint8* byte);
+static void OJPEGReadByteAdvance(OJPEGState* sp);
+static int OJPEGReadWord(OJPEGState* sp, uint16* word);
+static int OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem);
+static void OJPEGReadSkip(OJPEGState* sp, uint16 len);
+
+static int OJPEGWriteStream(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
+static void OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
+static void OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
+static void OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len);
+static int OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len);
+
+#ifdef LIBJPEG_ENCAP_EXTERNAL
+extern int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
+extern int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
+extern int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
+extern int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
+extern int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
+extern void jpeg_encap_unwind(TIFF* tif);
+#else
+static int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* j);
+static int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
+static int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
+static int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
+static int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
+static void jpeg_encap_unwind(TIFF* tif);
+#endif
-#define TIFFTAG_WANG_PAGECONTROL 32934
+static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo);
+static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo);
+static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo);
+static boolean OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo);
+static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes);
+static boolean OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired);
+static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo);
-/* Bit-vector offsets for keeping track of TIFF records that we've parsed. */
+int
+TIFFInitOJPEG(TIFF* tif, int scheme)
+{
+ static const char module[]="TIFFInitOJPEG";
+ OJPEGState* sp;
+
+ assert(scheme==COMPRESSION_OJPEG);
+
+ /*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFieldInfo(tif,ojpeg_field_info,FIELD_OJPEG_COUNT)) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging Old JPEG codec-specific tags failed");
+ return 0;
+ }
-#define FIELD_JPEGPROC FIELD_CODEC
-#define FIELD_JPEGIFOFFSET (FIELD_CODEC+1)
-#define FIELD_JPEGIFBYTECOUNT (FIELD_CODEC+2)
-#define FIELD_JPEGRESTARTINTERVAL (FIELD_CODEC+3)
-#define FIELD_JPEGTABLES (FIELD_CODEC+4) /* New, post-6.0 JPEG-in-TIFF tag! */
-#define FIELD_JPEGLOSSLESSPREDICTORS (FIELD_CODEC+5)
-#define FIELD_JPEGPOINTTRANSFORM (FIELD_CODEC+6)
-#define FIELD_JPEGQTABLES (FIELD_CODEC+7)
-#define FIELD_JPEGDCTABLES (FIELD_CODEC+8)
-#define FIELD_JPEGACTABLES (FIELD_CODEC+9)
-#define FIELD_WANG_PAGECONTROL (FIELD_CODEC+10)
-#define FIELD_JPEGCOLORMODE (FIELD_CODEC+11)
+ /* state block */
+ sp=_TIFFmalloc(sizeof(OJPEGState));
+ if (sp==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"No space for OJPEG state block");
+ return(0);
+ }
+ _TIFFmemset(sp,0,sizeof(OJPEGState));
+ sp->tif=tif;
+ sp->jpeg_proc=1;
+ sp->subsampling_hor=2;
+ sp->subsampling_ver=2;
+ TIFFSetField(tif,TIFFTAG_YCBCRSUBSAMPLING,2,2);
+ /* tif codec methods */
+ tif->tif_setupdecode=OJPEGSetupDecode;
+ tif->tif_predecode=OJPEGPreDecode;
+ tif->tif_postdecode=OJPEGPostDecode;
+ tif->tif_decoderow=OJPEGDecode;
+ tif->tif_decodestrip=OJPEGDecode;
+ tif->tif_decodetile=OJPEGDecode;
+ tif->tif_setupencode=OJPEGSetupEncode;
+ tif->tif_preencode=OJPEGPreEncode;
+ tif->tif_postencode=OJPEGPostEncode;
+ tif->tif_encoderow=OJPEGEncode;
+ tif->tif_encodestrip=OJPEGEncode;
+ tif->tif_encodetile=OJPEGEncode;
+ tif->tif_cleanup=OJPEGCleanup;
+ tif->tif_data=(tidata_t)sp;
+ /* tif tag methods */
+ sp->vgetparent=tif->tif_tagmethods.vgetfield;
+ tif->tif_tagmethods.vgetfield=OJPEGVGetField;
+ sp->vsetparent=tif->tif_tagmethods.vsetfield;
+ tif->tif_tagmethods.vsetfield=OJPEGVSetField;
+ tif->tif_tagmethods.printdir=OJPEGPrintDir;
+ /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
+ Some others do, but have totally meaningless or corrupt values
+ in these tags. In these cases, the JpegInterchangeFormat stream is
+ reliable. In any case, this decoder reads the compressed data itself,
+ from the most reliable locations, and we need to notify encapsulating
+ LibTiff not to read raw strips or tiles for us. */
+ tif->tif_flags|=TIFF_NOREADRAW;
+ return(1);
+}
-typedef struct jpeg_destination_mgr jpeg_destination_mgr;
-typedef struct jpeg_source_mgr jpeg_source_mgr;
-typedef struct jpeg_error_mgr jpeg_error_mgr;
+static int
+OJPEGVGetField(TIFF* tif, ttag_t tag, va_list ap)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ switch(tag)
+ {
+ case TIFFTAG_JPEGIFOFFSET:
+ *va_arg(ap,uint32*)=(uint32)sp->jpeg_interchange_format;
+ break;
+ case TIFFTAG_JPEGIFBYTECOUNT:
+ *va_arg(ap,uint32*)=(uint32)sp->jpeg_interchange_format_length;
+ break;
+ case TIFFTAG_YCBCRSUBSAMPLING:
+ if (sp->subsamplingcorrect_done==0)
+ OJPEGSubsamplingCorrect(tif);
+ *va_arg(ap,uint16*)=(uint16)sp->subsampling_hor;
+ *va_arg(ap,uint16*)=(uint16)sp->subsampling_ver;
+ break;
+ case TIFFTAG_JPEGQTABLES:
+ *va_arg(ap,uint32*)=(uint32)sp->qtable_offset_count;
+ *va_arg(ap,void**)=(void*)sp->qtable_offset;
+ break;
+ case TIFFTAG_JPEGDCTABLES:
+ *va_arg(ap,uint32*)=(uint32)sp->dctable_offset_count;
+ *va_arg(ap,void**)=(void*)sp->dctable_offset;
+ break;
+ case TIFFTAG_JPEGACTABLES:
+ *va_arg(ap,uint32*)=(uint32)sp->actable_offset_count;
+ *va_arg(ap,void**)=(void*)sp->actable_offset;
+ break;
+ case TIFFTAG_JPEGPROC:
+ *va_arg(ap,uint16*)=(uint16)sp->jpeg_proc;
+ break;
+ case TIFFTAG_JPEGRESTARTINTERVAL:
+ *va_arg(ap,uint16*)=sp->restart_interval;
+ break;
+ default:
+ return (*sp->vgetparent)(tif,tag,ap);
+ }
+ return (1);
+}
-/* State variable for each open TIFF file that uses "libjpeg" for JPEG
- decompression. (Note: This file should NEVER perform JPEG compression
- except in the manner implemented by the "tif_jpeg.c" file, elsewhere in this
- library; see comments above.) JPEG Library internal state is recorded in a
- "jpeg_{de}compress_struct", while a "jpeg_common_struct" records a few items
- common to both compression and expansion. The "cinfo" field containing JPEG
- Library state MUST be the 1st member of our own state variable, so that we
- can safely "cast" pointers back and forth.
-*/
-typedef struct /* This module's private, per-image state variable */
- {
- union /* JPEG Library state variable; this MUST be our 1st field! */
- {
- struct jpeg_compress_struct c;
- struct jpeg_decompress_struct d;
- struct jpeg_common_struct comm;
- } cinfo;
- jpeg_error_mgr err; /* JPEG Library error manager */
- JMP_BUF exit_jmpbuf; /* ...for catching JPEG Library failures */
-# ifdef never
-
- /* (The following two fields could be a "union", but they're small enough that
- it's not worth the effort.)
- */
- jpeg_destination_mgr dest; /* Destination for compressed data */
-# endif
- jpeg_source_mgr src; /* Source of expanded data */
- JSAMPARRAY ds_buffer[MAX_COMPONENTS]; /* ->Temporary downsampling buffers */
- TIFF *tif; /* Reverse pointer, needed by some code */
- TIFFVGetMethod vgetparent; /* "Super class" methods... */
- TIFFVSetMethod vsetparent;
- TIFFStripMethod defsparent;
- TIFFTileMethod deftparent;
- void *jpegtables; /* ->"New" JPEG tables, if we synthesized any */
- uint32 is_WANG, /* <=> Wang Imaging for Microsoft Windows output file? */
- jpegtables_length; /* Length of "new" JPEG tables, if they exist */
- tsize_t bytesperline; /* No. of decompressed Bytes per scan line */
- int jpegquality, /* Compression quality level */
- jpegtablesmode, /* What to put in JPEGTables */
- samplesperclump,
- scancount; /* No. of scan lines accumulated */
- J_COLOR_SPACE photometric; /* IJG JPEG Library's photometry code */
- unsigned char h_sampling, /* Luminance sampling factors */
- v_sampling,
- jpegcolormode; /* Who performs RGB <-> YCbCr conversion? */
- /* JPEGCOLORMODE_RAW <=> TIFF Library or its client */
- /* JPEGCOLORMODE_RGB <=> JPEG Library */
- /* These fields are added to support TIFFGetField */
- uint16 jpegproc;
- uint32 jpegifoffset;
- uint32 jpegifbytecount;
- uint32 jpegrestartinterval;
- void* jpeglosslesspredictors;
- uint16 jpeglosslesspredictors_length;
- void* jpegpointtransform;
- uint32 jpegpointtransform_length;
- void* jpegqtables;
- uint32 jpegqtables_length;
- void* jpegdctables;
- uint32 jpegdctables_length;
- void* jpegactables;
- uint32 jpegactables_length;
-
- } OJPEGState;
-#define OJState(tif)((OJPEGState*)(tif)->tif_data)
-
-static const TIFFFieldInfo ojpegFieldInfo[]=/* JPEG-specific TIFF-record tags */
- {
-
- /* This is the current JPEG-in-TIFF metadata-encapsulation tag, and its
- treatment in this file is idiosyncratic. It should never appear in a
- "source" image conforming to the TIFF Version 6.0 specification, so we
- arrange to report an error if it appears. But in order to support possible
- future conversion of "old" JPEG-in-TIFF encapsulations to "new" ones, we
- might wish to synthesize an equivalent value to be returned by the TIFF
- Library's "getfield" method. So, this table tells the TIFF Library to pass
- these records to us in order to filter them below.
- */
- {
- TIFFTAG_JPEGTABLES ,TIFF_VARIABLE2,TIFF_VARIABLE2,
- TIFF_UNDEFINED,FIELD_JPEGTABLES ,FALSE,TRUE ,"JPEGTables"
- },
-
- /* These tags are defined by the TIFF Version 6.0 specification and are now
- obsolete. This module reads them from an old "source" image, but it never
- writes them to a new "destination" image.
- */
- {
- TIFFTAG_JPEGPROC ,1 ,1 ,
- TIFF_SHORT ,FIELD_JPEGPROC ,FALSE,FALSE,"JPEGProc"
- },
- {
- TIFFTAG_JPEGIFOFFSET ,1 ,1 ,
- TIFF_LONG ,FIELD_JPEGIFOFFSET ,FALSE,FALSE,"JPEGInterchangeFormat"
- },
- {
- TIFFTAG_JPEGIFBYTECOUNT ,1 ,1 ,
- TIFF_LONG ,FIELD_JPEGIFBYTECOUNT ,FALSE,FALSE,"JPEGInterchangeFormatLength"
- },
- {
- TIFFTAG_JPEGRESTARTINTERVAL ,1 ,1 ,
- TIFF_SHORT ,FIELD_JPEGRESTARTINTERVAL ,FALSE,FALSE,"JPEGRestartInterval"
- },
- {
- TIFFTAG_JPEGLOSSLESSPREDICTORS,TIFF_VARIABLE,TIFF_VARIABLE,
- TIFF_SHORT ,FIELD_JPEGLOSSLESSPREDICTORS,FALSE,TRUE ,"JPEGLosslessPredictors"
- },
- {
- TIFFTAG_JPEGPOINTTRANSFORM ,TIFF_VARIABLE,TIFF_VARIABLE,
- TIFF_SHORT ,FIELD_JPEGPOINTTRANSFORM ,FALSE,TRUE ,"JPEGPointTransforms"
- },
- {
- TIFFTAG_JPEGQTABLES ,TIFF_VARIABLE,TIFF_VARIABLE,
- TIFF_LONG ,FIELD_JPEGQTABLES ,FALSE,TRUE ,"JPEGQTables"
- },
- {
- TIFFTAG_JPEGDCTABLES ,TIFF_VARIABLE,TIFF_VARIABLE,
- TIFF_LONG ,FIELD_JPEGDCTABLES ,FALSE,TRUE ,"JPEGDCTables"
- },
- {
- TIFFTAG_JPEGACTABLES ,TIFF_VARIABLE,TIFF_VARIABLE,
- TIFF_LONG ,FIELD_JPEGACTABLES ,FALSE,TRUE ,"JPEGACTables"
- },
- {
- TIFFTAG_WANG_PAGECONTROL ,TIFF_VARIABLE,1 ,
- TIFF_LONG ,FIELD_WANG_PAGECONTROL ,FALSE,FALSE,"WANG PageControl"
- },
-
- /* This is a pseudo tag intended for internal use only by the TIFF Library and
- its clients, which should never appear in an input/output image file. It
- specifies whether the TIFF Library (or its client) should do YCbCr <-> RGB
- color-space conversion (JPEGCOLORMODE_RAW <=> 0) or whether we should ask
- the JPEG Library to do it (JPEGCOLORMODE_RGB <=> 1).
- */
- {
- TIFFTAG_JPEGCOLORMODE ,0 ,0 ,
- TIFF_ANY ,FIELD_PSEUDO ,FALSE,FALSE,"JPEGColorMode"
- }
- };
-static const char JPEGLib_name[]={"JPEG Library"},
- bad_bps[]={"%u BitsPerSample not allowed for JPEG"},
- bad_photometry[]={"PhotometricInterpretation %u not allowed for JPEG"},
- bad_subsampling[]={"invalid YCbCr subsampling factor(s)"},
-# ifdef never
- no_write_frac[]={"fractional scan line discarded"},
-# endif
- no_read_frac[]={"fractional scan line not read"},
- no_jtable_space[]={"No space for JPEGTables"};
-
-/* The following diagnostic subroutines interface with and replace default
- subroutines in the JPEG Library. Our basic strategy is to use "setjmp()"/
- "longjmp()" in order to return control to the TIFF Library when the JPEG
- library detects an error, and to use TIFF Library subroutines for displaying
- diagnostic messages to a client application.
-*/
-static void
-TIFFojpeg_error_exit(register j_common_ptr cinfo)
+static int
+OJPEGVSetField(TIFF* tif, ttag_t tag, va_list ap)
{
- char buffer[JMSG_LENGTH_MAX];
- int code = cinfo->err->msg_code;
-
- if (((OJPEGState *)cinfo)->is_WANG) {
- if (code == JERR_SOF_DUPLICATE || code == JERR_SOI_DUPLICATE)
- return; /* ignore it */
- }
-
- (*cinfo->err->format_message)(cinfo,buffer);
- TIFFError(JPEGLib_name,buffer); /* Display error message */
- jpeg_abort(cinfo); /* Clean up JPEG Library state */
- LONGJMP(((OJPEGState *)cinfo)->exit_jmpbuf,1); /* Return to TIFF client */
+ static const char module[]="OJPEGVSetField";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint32 ma;
+ uint32* mb;
+ uint32 n;
+ switch(tag)
+ {
+ case TIFFTAG_JPEGIFOFFSET:
+ sp->jpeg_interchange_format=(toff_t)va_arg(ap,uint32);
+ break;
+ case TIFFTAG_JPEGIFBYTECOUNT:
+ sp->jpeg_interchange_format_length=(toff_t)va_arg(ap,uint32);
+ break;
+ case TIFFTAG_YCBCRSUBSAMPLING:
+ sp->subsampling_tag=1;
+ sp->subsampling_hor=(uint8)va_arg(ap,int);
+ sp->subsampling_ver=(uint8)va_arg(ap,int);
+ tif->tif_dir.td_ycbcrsubsampling[0]=sp->subsampling_hor;
+ tif->tif_dir.td_ycbcrsubsampling[1]=sp->subsampling_ver;
+ break;
+ case TIFFTAG_JPEGQTABLES:
+ ma=va_arg(ap,uint32);
+ if (ma!=0)
+ {
+ if (ma>3)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JpegQTables tag has incorrect count");
+ return(0);
+ }
+ sp->qtable_offset_count=(uint8)ma;
+ mb=va_arg(ap,uint32*);
+ for (n=0; n<ma; n++)
+ sp->qtable_offset[n]=(toff_t)mb[n];
+ }
+ break;
+ case TIFFTAG_JPEGDCTABLES:
+ ma=va_arg(ap,uint32);
+ if (ma!=0)
+ {
+ if (ma>3)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JpegDcTables tag has incorrect count");
+ return(0);
+ }
+ sp->dctable_offset_count=(uint8)ma;
+ mb=va_arg(ap,uint32*);
+ for (n=0; n<ma; n++)
+ sp->dctable_offset[n]=(toff_t)mb[n];
+ }
+ break;
+ case TIFFTAG_JPEGACTABLES:
+ ma=va_arg(ap,uint32);
+ if (ma!=0)
+ {
+ if (ma>3)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JpegAcTables tag has incorrect count");
+ return(0);
+ }
+ sp->actable_offset_count=(uint8)ma;
+ mb=va_arg(ap,uint32*);
+ for (n=0; n<ma; n++)
+ sp->actable_offset[n]=(toff_t)mb[n];
+ }
+ break;
+ case TIFFTAG_JPEGPROC:
+ sp->jpeg_proc=(uint8)va_arg(ap,uint32);
+ break;
+ case TIFFTAG_JPEGRESTARTINTERVAL:
+ sp->restart_interval=(uint16)va_arg(ap,uint32);
+ break;
+ default:
+ return (*sp->vsetparent)(tif,tag,ap);
+ }
+ TIFFSetFieldBit(tif,_TIFFFieldWithTag(tif,tag)->field_bit);
+ tif->tif_flags|=TIFF_DIRTYDIRECT;
+ return(1);
}
static void
-TIFFojpeg_output_message(register j_common_ptr cinfo)
- { char buffer[JMSG_LENGTH_MAX];
+OJPEGPrintDir(TIFF* tif, FILE* fd, long flags)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ (void)flags;
+ assert(sp!=NULL);
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMAT))
+ fprintf(fd," JpegInterchangeFormat: %lu\n",(unsigned long)sp->jpeg_interchange_format);
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH))
+ fprintf(fd," JpegInterchangeFormatLength: %lu\n",(unsigned long)sp->jpeg_interchange_format_length);
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGQTABLES))
+ {
+ fprintf(fd," JpegQTables:");
+ for (m=0; m<sp->qtable_offset_count; m++)
+ fprintf(fd," %lu",(unsigned long)sp->qtable_offset[m]);
+ fprintf(fd,"\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGDCTABLES))
+ {
+ fprintf(fd," JpegDcTables:");
+ for (m=0; m<sp->dctable_offset_count; m++)
+ fprintf(fd," %lu",(unsigned long)sp->dctable_offset[m]);
+ fprintf(fd,"\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGACTABLES))
+ {
+ fprintf(fd," JpegAcTables:");
+ for (m=0; m<sp->actable_offset_count; m++)
+ fprintf(fd," %lu",(unsigned long)sp->actable_offset[m]);
+ fprintf(fd,"\n");
+ }
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGPROC))
+ fprintf(fd," JpegProc: %u\n",(unsigned int)sp->jpeg_proc);
+ if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGRESTARTINTERVAL))
+ fprintf(fd," JpegRestartInterval: %u\n",(unsigned int)sp->restart_interval);
+}
- /* This subroutine is invoked only for warning messages, since the JPEG
- Library's "error_exit" method does its own thing and "trace_level" is never
- set > 0.
- */
- (*cinfo->err->format_message)(cinfo,buffer);
- TIFFWarning(JPEGLib_name,buffer);
- }
+static int
+OJPEGSetupDecode(TIFF* tif)
+{
+ static const char module[]="OJPEGSetupDecode";
+ TIFFWarningExt(tif->tif_clientdata,module,"Depreciated and troublesome old-style JPEG compression mode, please convert to new-style JPEG compression and notify vendor of writing software");
+ return(1);
+}
-/* The following subroutines, which also interface with the JPEG Library, exist
- mainly in limit the side effects of "setjmp()" and convert JPEG normal/error
- conditions into TIFF Library return codes.
-*/
-#define CALLJPEG(sp,fail,op)(SETJMP((sp)->exit_jmpbuf)?(fail):(op))
-#define CALLVJPEG(sp,op)CALLJPEG(sp,0,((op),1))
-#ifdef never
+static int
+OJPEGPreDecode(TIFF* tif, tsample_t s)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ tstrile_t m;
+ if (sp->subsamplingcorrect_done==0)
+ OJPEGSubsamplingCorrect(tif);
+ if (sp->readheader_done==0)
+ {
+ if (OJPEGReadHeaderInfo(tif)==0)
+ return(0);
+ }
+ if (sp->sos_end[s].log==0)
+ {
+ if (OJPEGReadSecondarySos(tif,s)==0)
+ return(0);
+ }
+ if isTiled(tif)
+ m=(tstrile_t)tif->tif_curtile;
+ else
+ m=(tstrile_t)tif->tif_curstrip;
+ if ((sp->writeheader_done!=0) && ((sp->write_cursample!=s) || (sp->write_curstrile>m)))
+ {
+ if (sp->libjpeg_session_active!=0)
+ OJPEGLibjpegSessionAbort(tif);
+ sp->writeheader_done=0;
+ }
+ if (sp->writeheader_done==0)
+ {
+ sp->plane_sample_offset=s;
+ sp->write_cursample=s;
+ sp->write_curstrile=s*tif->tif_dir.td_stripsperimage;
+ if ((sp->in_buffer_file_pos_log==0) ||
+ (sp->in_buffer_file_pos-sp->in_buffer_togo!=sp->sos_end[s].in_buffer_file_pos))
+ {
+ sp->in_buffer_source=sp->sos_end[s].in_buffer_source;
+ sp->in_buffer_next_strile=sp->sos_end[s].in_buffer_next_strile;
+ sp->in_buffer_file_pos=sp->sos_end[s].in_buffer_file_pos;
+ sp->in_buffer_file_pos_log=0;
+ sp->in_buffer_file_togo=sp->sos_end[s].in_buffer_file_togo;
+ sp->in_buffer_togo=0;
+ sp->in_buffer_cur=0;
+ }
+ if (OJPEGWriteHeaderInfo(tif)==0)
+ return(0);
+ }
+ while (sp->write_curstrile<m)
+ {
+ if (sp->libjpeg_jpeg_query_style==0)
+ {
+ if (OJPEGPreDecodeSkipRaw(tif)==0)
+ return(0);
+ }
+ else
+ {
+ if (OJPEGPreDecodeSkipScanlines(tif)==0)
+ return(0);
+ }
+ sp->write_curstrile++;
+ }
+ return(1);
+}
static int
-TIFFojpeg_create_compress(register OJPEGState *sp)
- {
- sp->cinfo.c.err = jpeg_std_error(&sp->err); /* Initialize error handling */
- sp->err.error_exit = TIFFojpeg_error_exit;
- sp->err.output_message = TIFFojpeg_output_message;
- return CALLVJPEG(sp,jpeg_create_compress(&sp->cinfo.c));
- }
-
-/* The following subroutines comprise a JPEG Library "destination" data manager
- by directing compressed data from the JPEG Library to a TIFF Library output
- buffer.
-*/
-static void
-std_init_destination(register j_compress_ptr cinfo){} /* "Dummy" stub */
+OJPEGPreDecodeSkipRaw(TIFF* tif)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint32 m;
+ m=sp->lines_per_strile;
+ if (sp->subsampling_convert_state!=0)
+ {
+ if (sp->subsampling_convert_clines-sp->subsampling_convert_state>=m)
+ {
+ sp->subsampling_convert_state+=m;
+ if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
+ sp->subsampling_convert_state=0;
+ return(1);
+ }
+ m-=sp->subsampling_convert_clines-sp->subsampling_convert_state;
+ sp->subsampling_convert_state=0;
+ }
+ while (m>=sp->subsampling_convert_clines)
+ {
+ if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
+ return(0);
+ m-=sp->subsampling_convert_clines;
+ }
+ if (m>0)
+ {
+ if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
+ return(0);
+ sp->subsampling_convert_state=m;
+ }
+ return(1);
+}
-static boolean
-std_empty_output_buffer(register j_compress_ptr cinfo)
- {
-# define sp ((OJPEGState *)cinfo)
- register TIFF *tif = sp->tif;
-
- tif->tif_rawcc = tif->tif_rawdatasize; /* Entire buffer has been filled */
- TIFFFlushData1(tif);
- sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
- sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
- return TRUE;
-# undef sp
- }
+static int
+OJPEGPreDecodeSkipScanlines(TIFF* tif)
+{
+ static const char module[]="OJPEGPreDecodeSkipScanlines";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint32 m;
+ if (sp->skip_buffer==NULL)
+ {
+ sp->skip_buffer=_TIFFmalloc(sp->bytes_per_line);
+ if (sp->skip_buffer==NULL)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ }
+ for (m=0; m<sp->lines_per_strile; m++)
+ {
+ if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&sp->skip_buffer,1)==0)
+ return(0);
+ }
+ return(1);
+}
-static void
-std_term_destination(register j_compress_ptr cinfo)
- {
-# define sp ((OJPEGState *)cinfo)
- register TIFF *tif = sp->tif;
+static int
+OJPEGDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ (void)s;
+ if (sp->libjpeg_jpeg_query_style==0)
+ {
+ if (OJPEGDecodeRaw(tif,buf,cc)==0)
+ return(0);
+ }
+ else
+ {
+ if (OJPEGDecodeScanlines(tif,buf,cc)==0)
+ return(0);
+ }
+ return(1);
+}
- /* NB: The TIFF Library does the final buffer flush. */
- tif->tif_rawcp = (tidata_t)sp->dest.next_output_byte;
- tif->tif_rawcc = tif->tif_rawdatasize - (tsize_t)sp->dest.free_in_buffer;
-# undef sp
- }
+static int
+OJPEGDecodeRaw(TIFF* tif, tidata_t buf, tsize_t cc)
+{
+ static const char module[]="OJPEGDecodeRaw";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8* m;
+ uint32 n;
+ uint8* oy;
+ uint8* ocb;
+ uint8* ocr;
+ uint8* p;
+ uint32 q;
+ uint8* r;
+ uint8 sx,sy;
+ if (cc%sp->bytes_per_line!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
+ return(0);
+ }
+ assert(cc>0);
+ m=buf;
+ n=cc;
+ do
+ {
+ if (sp->subsampling_convert_state==0)
+ {
+ if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
+ return(0);
+ }
+ oy=sp->subsampling_convert_ybuf+sp->subsampling_convert_state*sp->subsampling_ver*sp->subsampling_convert_ylinelen;
+ ocb=sp->subsampling_convert_cbbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
+ ocr=sp->subsampling_convert_crbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
+ p=m;
+ for (q=0; q<sp->subsampling_convert_clinelenout; q++)
+ {
+ r=oy;
+ for (sy=0; sy<sp->subsampling_ver; sy++)
+ {
+ for (sx=0; sx<sp->subsampling_hor; sx++)
+ *p++=*r++;
+ r+=sp->subsampling_convert_ylinelen-sp->subsampling_hor;
+ }
+ oy+=sp->subsampling_hor;
+ *p++=*ocb++;
+ *p++=*ocr++;
+ }
+ sp->subsampling_convert_state++;
+ if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
+ sp->subsampling_convert_state=0;
+ m+=sp->bytes_per_line;
+ n-=sp->bytes_per_line;
+ } while(n>0);
+ return(1);
+}
-/* Alternate destination manager to output JPEGTables field: */
+static int
+OJPEGDecodeScanlines(TIFF* tif, tidata_t buf, tsize_t cc)
+{
+ static const char module[]="OJPEGDecodeScanlines";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8* m;
+ uint32 n;
+ if (cc%sp->bytes_per_line!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
+ return(0);
+ }
+ assert(cc>0);
+ m=buf;
+ n=cc;
+ do
+ {
+ if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&m,1)==0)
+ return(0);
+ m+=sp->bytes_per_line;
+ n-=sp->bytes_per_line;
+ } while(n>0);
+ return(1);
+}
static void
-tables_init_destination(register j_compress_ptr cinfo)
- {
-# define sp ((OJPEGState *)cinfo)
- /* The "jpegtables_length" field is the allocated buffer size while building */
- sp->dest.next_output_byte = (JOCTET *)sp->jpegtables;
- sp->dest.free_in_buffer = (size_t)sp->jpegtables_length;
-# undef sp
- }
+OJPEGPostDecode(TIFF* tif, tidata_t buf, tsize_t cc)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ (void)buf;
+ (void)cc;
+ sp->write_curstrile++;
+ if (sp->write_curstrile%tif->tif_dir.td_stripsperimage==0)
+ {
+ assert(sp->libjpeg_session_active!=0);
+ OJPEGLibjpegSessionAbort(tif);
+ sp->writeheader_done=0;
+ }
+}
-static boolean
-tables_empty_output_buffer(register j_compress_ptr cinfo)
- { void *newbuf;
-# define sp ((OJPEGState *)cinfo)
-
- /* The entire buffer has been filled, so enlarge it by 1000 bytes. */
- if (!( newbuf = _TIFFrealloc( (tdata_t)sp->jpegtables
- , (tsize_t)(sp->jpegtables_length + 1000)
- )
- )
- ) ERREXIT1(cinfo,JERR_OUT_OF_MEMORY,100);
- sp->dest.next_output_byte = (JOCTET *)newbuf + sp->jpegtables_length;
- sp->dest.free_in_buffer = (size_t)1000;
- sp->jpegtables = newbuf;
- sp->jpegtables_length += 1000;
- return TRUE;
-# undef sp
- }
+static int
+OJPEGSetupEncode(TIFF* tif)
+{
+ static const char module[]="OJPEGSetupEncode";
+ TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+ return(0);
+}
-static void
-tables_term_destination(register j_compress_ptr cinfo)
- {
-# define sp ((OJPEGState *)cinfo)
- /* Set tables length to no. of Bytes actually emitted. */
- sp->jpegtables_length -= sp->dest.free_in_buffer;
-# undef sp
- }
-
-/*ARGSUSED*/ static int
-TIFFojpeg_tables_dest(register OJPEGState *sp, TIFF *tif)
- {
-
- /* Allocate a working buffer for building tables. The initial size is 1000
- Bytes, which is usually adequate.
- */
- if (sp->jpegtables) _TIFFfree(sp->jpegtables);
- if (!(sp->jpegtables = (void*)
- _TIFFmalloc((tsize_t)(sp->jpegtables_length = 1000))
- )
- )
- {
- sp->jpegtables_length = 0;
- TIFFError("TIFFojpeg_tables_dest",no_jtable_space);
- 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;
- }
-#else /* well, hardly ever */
+static int
+OJPEGPreEncode(TIFF* tif, tsample_t s)
+{
+ static const char module[]="OJPEGPreEncode";
+ (void)s;
+ TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+ return(0);
+}
static int
-_notSupported(register TIFF *tif)
- { const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression);
+OJPEGEncode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
+{
+ static const char module[]="OJPEGEncode";
+ (void)buf;
+ (void)cc;
+ (void)s;
+ TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+ return(0);
+}
- TIFFError(tif->tif_name,"%s compression not supported",c->name);
- return 0;
- }
-#endif /* never */
+static int
+OJPEGPostEncode(TIFF* tif)
+{
+ static const char module[]="OJPEGPostEncode";
+ TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+ return(0);
+}
-/* The following subroutines comprise a JPEG Library "source" data manager by
- by directing compressed data to the JPEG Library from a TIFF Library input
- buffer.
-*/
static void
-std_init_source(register j_decompress_ptr cinfo)
- {
-# define sp ((OJPEGState *)cinfo)
- register TIFF *tif = sp->tif;
-
- if (sp->src.bytes_in_buffer == 0)
- {
- sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata;
- sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
- };
-# undef sp
- }
+OJPEGCleanup(TIFF* tif)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ if (sp!=0)
+ {
+ tif->tif_tagmethods.vgetfield=sp->vgetparent;
+ tif->tif_tagmethods.vsetfield=sp->vsetparent;
+ if (sp->qtable[0]!=0)
+ _TIFFfree(sp->qtable[0]);
+ if (sp->qtable[1]!=0)
+ _TIFFfree(sp->qtable[1]);
+ if (sp->qtable[2]!=0)
+ _TIFFfree(sp->qtable[2]);
+ if (sp->qtable[3]!=0)
+ _TIFFfree(sp->qtable[3]);
+ if (sp->dctable[0]!=0)
+ _TIFFfree(sp->dctable[0]);
+ if (sp->dctable[1]!=0)
+ _TIFFfree(sp->dctable[1]);
+ if (sp->dctable[2]!=0)
+ _TIFFfree(sp->dctable[2]);
+ if (sp->dctable[3]!=0)
+ _TIFFfree(sp->dctable[3]);
+ if (sp->actable[0]!=0)
+ _TIFFfree(sp->actable[0]);
+ if (sp->actable[1]!=0)
+ _TIFFfree(sp->actable[1]);
+ if (sp->actable[2]!=0)
+ _TIFFfree(sp->actable[2]);
+ if (sp->actable[3]!=0)
+ _TIFFfree(sp->actable[3]);
+ if (sp->libjpeg_session_active!=0)
+ OJPEGLibjpegSessionAbort(tif);
+ if (sp->subsampling_convert_ycbcrbuf!=0)
+ _TIFFfree(sp->subsampling_convert_ycbcrbuf);
+ if (sp->subsampling_convert_ycbcrimage!=0)
+ _TIFFfree(sp->subsampling_convert_ycbcrimage);
+ if (sp->skip_buffer!=0)
+ _TIFFfree(sp->skip_buffer);
+ _TIFFfree(sp);
+ tif->tif_data=NULL;
+ _TIFFSetDefaultCompressionState(tif);
+ }
+}
-static boolean
-std_fill_input_buffer(register j_decompress_ptr cinfo)
- { static const JOCTET dummy_EOI[2]={0xFF,JPEG_EOI};
-# define sp ((OJPEGState *)cinfo)
+static void
+OJPEGSubsamplingCorrect(TIFF* tif)
+{
+ static const char module[]="OJPEGSubsamplingCorrect";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 mh;
+ uint8 mv;
+ assert(sp->subsamplingcorrect_done==0);
+ if ((tif->tif_dir.td_samplesperpixel!=3) || ((tif->tif_dir.td_photometric!=PHOTOMETRIC_YCBCR) &&
+ (tif->tif_dir.td_photometric!=PHOTOMETRIC_ITULAB)))
+ {
+ if (sp->subsampling_tag!=0)
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag not appropriate for this Photometric and/or SamplesPerPixel");
+ sp->subsampling_hor=1;
+ sp->subsampling_ver=1;
+ sp->subsampling_force_desubsampling_inside_decompression=0;
+ }
+ else
+ {
+ sp->subsamplingcorrect_done=1;
+ mh=sp->subsampling_hor;
+ mv=sp->subsampling_ver;
+ sp->subsamplingcorrect=1;
+ OJPEGReadHeaderInfoSec(tif);
+ if (sp->subsampling_force_desubsampling_inside_decompression!=0)
+ {
+ sp->subsampling_hor=1;
+ sp->subsampling_ver=1;
+ }
+ sp->subsamplingcorrect=0;
+ if (((sp->subsampling_hor!=mh) || (sp->subsampling_ver!=mv)) && (sp->subsampling_force_desubsampling_inside_decompression==0))
+ {
+ if (sp->subsampling_tag==0)
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data [%d,%d] does not match default values [2,2]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver);
+ else
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data [%d,%d] does not match subsampling tag values [%d,%d]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver,mh,mv);
+ }
+ if (sp->subsampling_force_desubsampling_inside_decompression!=0)
+ {
+ if (sp->subsampling_tag==0)
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data does not match default values [2,2] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression");
+ else
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data does not match subsampling tag values [%d,%d] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression",mh,mv);
+ }
+ if (sp->subsampling_force_desubsampling_inside_decompression==0)
+ {
+ if (sp->subsampling_hor<sp->subsampling_ver)
+ TIFFWarningExt(tif->tif_clientdata,module,"Subsampling values [%d,%d] are not allowed in TIFF",sp->subsampling_hor,sp->subsampling_ver);
+ }
+ }
+ sp->subsamplingcorrect_done=1;
+}
- /* Control should never get here, since an entire strip/tile is read into
- memory before the decompressor is called; thus, data should have been
- supplied by the "init_source" method. ...But, sometimes things fail.
- */
- WARNMS(cinfo,JWRN_JPEG_EOF);
- sp->src.next_input_byte = dummy_EOI; /* Insert a fake EOI marker */
- sp->src.bytes_in_buffer = sizeof dummy_EOI;
- return TRUE;
-# undef sp
- }
+static int
+OJPEGReadHeaderInfo(TIFF* tif)
+{
+ static const char module[]="OJPEGReadHeaderInfo";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(sp->readheader_done==0);
+ sp->image_width=tif->tif_dir.td_imagewidth;
+ sp->image_length=tif->tif_dir.td_imagelength;
+ if isTiled(tif)
+ {
+ sp->strile_width=tif->tif_dir.td_tilewidth;
+ sp->strile_length=tif->tif_dir.td_tilelength;
+ sp->strile_length_total=((sp->image_length+sp->strile_length-1)/sp->strile_length)*sp->strile_length;
+ }
+ else
+ {
+ sp->strile_width=sp->image_width;
+ sp->strile_length=tif->tif_dir.td_rowsperstrip;
+ sp->strile_length_total=sp->image_length;
+ }
+ sp->samples_per_pixel=tif->tif_dir.td_samplesperpixel;
+ if (sp->samples_per_pixel==1)
+ {
+ sp->plane_sample_offset=0;
+ sp->samples_per_pixel_per_plane=sp->samples_per_pixel;
+ sp->subsampling_hor=1;
+ sp->subsampling_ver=1;
+ }
+ else
+ {
+ if (sp->samples_per_pixel!=3)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"SamplesPerPixel %d not supported for this compression scheme",sp->samples_per_pixel);
+ return(0);
+ }
+ sp->plane_sample_offset=0;
+ if (tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)
+ sp->samples_per_pixel_per_plane=3;
+ else
+ sp->samples_per_pixel_per_plane=1;
+ }
+ if (sp->strile_length<sp->image_length)
+ {
+ if (sp->strile_length%(sp->subsampling_ver*8)!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Incompatible vertical subsampling and image strip/tile length");
+ return(0);
+ }
+ sp->restart_interval=((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8))*(sp->strile_length/(sp->subsampling_ver*8));
+ }
+ if (OJPEGReadHeaderInfoSec(tif)==0)
+ return(0);
+ sp->sos_end[0].log=1;
+ sp->sos_end[0].in_buffer_source=sp->in_buffer_source;
+ sp->sos_end[0].in_buffer_next_strile=sp->in_buffer_next_strile;
+ sp->sos_end[0].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
+ sp->sos_end[0].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
+ sp->readheader_done=1;
+ return(1);
+}
+
+static int
+OJPEGReadSecondarySos(TIFF* tif, tsample_t s)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ assert(s>0);
+ assert(s<3);
+ assert(sp->sos_end[0].log!=0);
+ assert(sp->sos_end[s].log==0);
+ sp->plane_sample_offset=s-1;
+ while(sp->sos_end[sp->plane_sample_offset].log==0)
+ sp->plane_sample_offset--;
+ sp->in_buffer_source=sp->sos_end[sp->plane_sample_offset].in_buffer_source;
+ sp->in_buffer_next_strile=sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile;
+ sp->in_buffer_file_pos=sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos;
+ sp->in_buffer_file_pos_log=0;
+ sp->in_buffer_file_togo=sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo;
+ sp->in_buffer_togo=0;
+ sp->in_buffer_cur=0;
+ while(sp->plane_sample_offset<s)
+ {
+ do
+ {
+ if (OJPEGReadByte(sp,&m)==0)
+ return(0);
+ if (m==255)
+ {
+ do
+ {
+ if (OJPEGReadByte(sp,&m)==0)
+ return(0);
+ if (m!=255)
+ break;
+ } while(1);
+ if (m==JPEG_MARKER_SOS)
+ break;
+ }
+ } while(1);
+ sp->plane_sample_offset++;
+ if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
+ return(0);
+ sp->sos_end[sp->plane_sample_offset].log=1;
+ sp->sos_end[sp->plane_sample_offset].in_buffer_source=sp->in_buffer_source;
+ sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile=sp->in_buffer_next_strile;
+ sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
+ sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
+ }
+ return(1);
+}
+
+static int
+OJPEGWriteHeaderInfo(TIFF* tif)
+{
+ static const char module[]="OJPEGWriteHeaderInfo";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8** m;
+ uint32 n;
+ assert(sp->libjpeg_session_active==0);
+ sp->out_state=ososSoi;
+ sp->restart_index=0;
+ jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr));
+ sp->libjpeg_jpeg_error_mgr.output_message=OJPEGLibjpegJpegErrorMgrOutputMessage;
+ sp->libjpeg_jpeg_error_mgr.error_exit=OJPEGLibjpegJpegErrorMgrErrorExit;
+ sp->libjpeg_jpeg_decompress_struct.err=&(sp->libjpeg_jpeg_error_mgr);
+ sp->libjpeg_jpeg_decompress_struct.client_data=(void*)tif;
+ if (jpeg_create_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
+ return(0);
+ sp->libjpeg_session_active=1;
+ sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=0;
+ sp->libjpeg_jpeg_source_mgr.init_source=OJPEGLibjpegJpegSourceMgrInitSource;
+ sp->libjpeg_jpeg_source_mgr.fill_input_buffer=OJPEGLibjpegJpegSourceMgrFillInputBuffer;
+ sp->libjpeg_jpeg_source_mgr.skip_input_data=OJPEGLibjpegJpegSourceMgrSkipInputData;
+ sp->libjpeg_jpeg_source_mgr.resync_to_restart=OJPEGLibjpegJpegSourceMgrResyncToRestart;
+ sp->libjpeg_jpeg_source_mgr.term_source=OJPEGLibjpegJpegSourceMgrTermSource;
+ sp->libjpeg_jpeg_decompress_struct.src=&(sp->libjpeg_jpeg_source_mgr);
+ if (jpeg_read_header_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),1)==0)
+ return(0);
+ if ((sp->subsampling_force_desubsampling_inside_decompression==0) && (sp->samples_per_pixel_per_plane>1))
+ {
+ sp->libjpeg_jpeg_decompress_struct.raw_data_out=1;
+ sp->libjpeg_jpeg_query_style=0;
+ if (sp->subsampling_convert_log==0)
+ {
+ assert(sp->subsampling_convert_ycbcrbuf==0);
+ assert(sp->subsampling_convert_ycbcrimage==0);
+ sp->subsampling_convert_ylinelen=((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8)*sp->subsampling_hor*8);
+ sp->subsampling_convert_ylines=sp->subsampling_ver*8;
+ sp->subsampling_convert_clinelen=sp->subsampling_convert_ylinelen/sp->subsampling_hor;
+ sp->subsampling_convert_clines=8;
+ sp->subsampling_convert_ybuflen=sp->subsampling_convert_ylinelen*sp->subsampling_convert_ylines;
+ sp->subsampling_convert_cbuflen=sp->subsampling_convert_clinelen*sp->subsampling_convert_clines;
+ sp->subsampling_convert_ycbcrbuflen=sp->subsampling_convert_ybuflen+2*sp->subsampling_convert_cbuflen;
+ sp->subsampling_convert_ycbcrbuf=_TIFFmalloc(sp->subsampling_convert_ycbcrbuflen);
+ if (sp->subsampling_convert_ycbcrbuf==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ sp->subsampling_convert_ybuf=sp->subsampling_convert_ycbcrbuf;
+ sp->subsampling_convert_cbbuf=sp->subsampling_convert_ybuf+sp->subsampling_convert_ybuflen;
+ sp->subsampling_convert_crbuf=sp->subsampling_convert_cbbuf+sp->subsampling_convert_cbuflen;
+ sp->subsampling_convert_ycbcrimagelen=3+sp->subsampling_convert_ylines+2*sp->subsampling_convert_clines;
+ sp->subsampling_convert_ycbcrimage=_TIFFmalloc(sp->subsampling_convert_ycbcrimagelen*sizeof(uint8*));
+ if (sp->subsampling_convert_ycbcrimage==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ m=sp->subsampling_convert_ycbcrimage;
+ *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3);
+ *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines);
+ *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines+sp->subsampling_convert_clines);
+ for (n=0; n<sp->subsampling_convert_ylines; n++)
+ *m++=sp->subsampling_convert_ybuf+n*sp->subsampling_convert_ylinelen;
+ for (n=0; n<sp->subsampling_convert_clines; n++)
+ *m++=sp->subsampling_convert_cbbuf+n*sp->subsampling_convert_clinelen;
+ for (n=0; n<sp->subsampling_convert_clines; n++)
+ *m++=sp->subsampling_convert_crbuf+n*sp->subsampling_convert_clinelen;
+ sp->subsampling_convert_clinelenout=((sp->strile_width+sp->subsampling_hor-1)/sp->subsampling_hor);
+ sp->subsampling_convert_state=0;
+ sp->bytes_per_line=sp->subsampling_convert_clinelenout*(sp->subsampling_ver*sp->subsampling_hor+2);
+ sp->lines_per_strile=((sp->strile_length+sp->subsampling_ver-1)/sp->subsampling_ver);
+ sp->subsampling_convert_log=1;
+ }
+ }
+ else
+ {
+ sp->libjpeg_jpeg_decompress_struct.jpeg_color_space=JCS_UNKNOWN;
+ sp->libjpeg_jpeg_decompress_struct.out_color_space=JCS_UNKNOWN;
+ sp->libjpeg_jpeg_query_style=1;
+ sp->bytes_per_line=sp->samples_per_pixel_per_plane*sp->strile_width;
+ sp->lines_per_strile=sp->strile_length;
+ }
+ if (jpeg_start_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
+ return(0);
+ sp->writeheader_done=1;
+ return(1);
+}
static void
-std_skip_input_data(register j_decompress_ptr cinfo, long num_bytes)
- {
-# define sp ((OJPEGState *)cinfo)
-
- if (num_bytes > 0)
- {
- if (num_bytes > (long)sp->src.bytes_in_buffer) /* oops: buffer overrun */
- (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;
- }
- }
-# undef sp
- }
-
-/*ARGSUSED*/ static void
-std_term_source(register j_decompress_ptr cinfo){} /* "Dummy" stub */
-
-/* Allocate temporary I/O buffers for downsampled data, using values computed in
- "jpeg_start_{de}compress()". We use the JPEG Library's allocator so that
- buffers will be released automatically when done with a strip/tile. This is
- also a handy place to compute samplesperclump, bytesperline, etc.
-*/
+OJPEGLibjpegSessionAbort(TIFF* tif)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(sp->libjpeg_session_active!=0);
+ jpeg_destroy((jpeg_common_struct*)(&(sp->libjpeg_jpeg_decompress_struct)));
+ sp->libjpeg_session_active=0;
+}
+
static int
-alloc_downsampled_buffers(TIFF *tif,jpeg_component_info *comp_info,
- int num_components)
- { register OJPEGState *sp = OJState(tif);
-
- sp->samplesperclump = 0;
- if (num_components > 0)
- { tsize_t size = sp->cinfo.comm.is_decompressor
-# ifdef D_LOSSLESS_SUPPORTED
- ? sp->cinfo.d.min_codec_data_unit
-# else
- ? DCTSIZE
-# endif
-# ifdef C_LOSSLESS_SUPPORTED
- : sp->cinfo.c.data_unit;
-# else
- : DCTSIZE;
-# endif
- int ci = 0;
- register jpeg_component_info *compptr = comp_info;
-
- do
- { JSAMPARRAY buf;
-
- sp->samplesperclump +=
- compptr->h_samp_factor * compptr->v_samp_factor;
-# if defined(C_LOSSLESS_SUPPORTED) || defined(D_LOSSLESS_SUPPORTED)
- if (!(buf = CALLJPEG(sp,0,(*sp->cinfo.comm.mem->alloc_sarray)(&sp->cinfo.comm,JPOOL_IMAGE,compptr->width_in_data_units*size,compptr->v_samp_factor*size))))
-# else
- if (!(buf = CALLJPEG(sp,0,(*sp->cinfo.comm.mem->alloc_sarray)(&sp->cinfo.comm,JPOOL_IMAGE,compptr->width_in_blocks*size,compptr->v_samp_factor*size))))
-# endif
- return 0;
- sp->ds_buffer[ci] = buf;
- }
- while (++compptr,++ci < num_components);
- };
- return 1;
- }
-#ifdef never
-
-/* JPEG Encoding begins here. */
-
-/*ARGSUSED*/ static int
-OJPEGEncode(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s)
- { tsize_t rows; /* No. of unprocessed rows in file */
- register OJPEGState *sp = OJState(tif);
-
- /* Encode a chunk of pixels, where returned data is NOT down-sampled (the
- standard case). The data is expected to be written in scan-line multiples.
- */
- if (cc % sp->bytesperline) TIFFWarning(tif->tif_name,no_write_frac);
- if ( (cc /= bytesperline) /* No. of complete rows in caller's buffer */
- > (rows = sp->cinfo.c.image_height - sp->cinfo.c.next_scanline)
- ) cc = rows;
- while (--cc >= 0)
- {
- if ( CALLJPEG(sp,-1,jpeg_write_scanlines(&sp->cinfo.c,(JSAMPARRAY)&buf,1))
- != 1
- ) return 0;
- ++tif->tif_row;
- buf += sp->bytesperline;
- };
- return 1;
- }
-
-/*ARGSUSED*/ static int
-OJPEGEncodeRaw(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s)
- { tsize_t rows; /* No. of unprocessed rows in file */
- JDIMENSION lines_per_MCU, size;
- register OJPEGState *sp = OJState(tif);
-
- /* Encode a chunk of pixels, where returned data is down-sampled as per the
- sampling factors. The data is expected to be written in scan-line
- multiples.
- */
- cc /= sp->bytesperline;
- if (cc % sp->bytesperline) TIFFWarning(tif->tif_name,no_write_frac);
- if ( (cc /= bytesperline) /* No. of complete rows in caller's buffer */
- > (rows = sp->cinfo.c.image_height - sp->cinfo.c.next_scanline)
- ) cc = rows;
-# ifdef C_LOSSLESS_SUPPORTED
- lines_per_MCU = sp->cinfo.c.max_samp_factor*(size = sp->cinfo.d.data_unit);
-# else
- lines_per_MCU = sp->cinfo.c.max_samp_factor*(size = DCTSIZE);
-# endif
- while (--cc >= 0)
- { int ci = 0, clumpoffset = 0;
- register jpeg_component_info *compptr = sp->cinfo.c.comp_info;
-
- /* The fastest way to separate the data is to make 1 pass over the scan
- line for each row of each component.
- */
- do
- { int ypos = 0;
-
- do
- { int padding;
- register JSAMPLE *inptr = (JSAMPLE*)buf + clumpoffset,
- *outptr =
- sp->ds_buffer[ci][sp->scancount*compptr->v_samp_factor+ypos];
- /* Cb,Cr both have sampling factors 1, so this is correct */
- register int clumps_per_line =
- sp->cinfo.c.comp_info[1].downsampled_width,
- xpos;
-
- padding = (int)
-# ifdef C_LOSSLESS_SUPPORTED
- ( compptr->width_in_data_units * size
-# else
- ( compptr->width_in_blocks * size
-# endif
- - clumps_per_line * compptr->h_samp_factor
- );
- if (compptr->h_samp_factor == 1) /* Cb & Cr fast path */
- do *outptr++ = *inptr;
- while ((inptr += sp->samplesperclump),--clumps_per_line > 0);
- else /* general case */
- do
- {
- xpos = 0;
- do *outptr++ = inptr[xpos];
- while (++xpos < compptr->h_samp_factor);
- }
- while ((inptr += sp->samplesperclump),--clumps_per_line > 0);
- xpos = 0; /* Pad each scan line as needed */
- do outptr[0] = outptr[-1]; while (++outptr,++xpos < padding);
- clumpoffset += compptr->h_samp_factor;
- }
- while (++ypos < compptr->v_samp_factor);
- }
- while (++compptr,++ci < sp->cinfo.c.num_components);
- if (++sp->scancount >= size)
- {
- if ( CALLJPEG(sp,-1,jpeg_write_raw_data(&sp->cinfo.c,sp->ds_buffer,lines_per_MCU))
- != lines_per_MCU
- ) return 0;
- sp->scancount = 0;
- };
- ++tif->tif_row++
- buf += sp->bytesperline;
- };
- return 1;
- }
+OJPEGReadHeaderInfoSec(TIFF* tif)
+{
+ static const char module[]="OJPEGReadHeaderInfoSec";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ uint16 n;
+ uint8 o;
+ if (sp->file_size==0)
+ sp->file_size=TIFFGetFileSize(tif);
+ if (sp->jpeg_interchange_format!=0)
+ {
+ if (sp->jpeg_interchange_format>=sp->file_size)
+ {
+ sp->jpeg_interchange_format=0;
+ sp->jpeg_interchange_format_length=0;
+ }
+ else
+ {
+ if ((sp->jpeg_interchange_format_length==0) || (sp->jpeg_interchange_format+sp->jpeg_interchange_format_length>sp->file_size))
+ sp->jpeg_interchange_format_length=sp->file_size-sp->jpeg_interchange_format;
+ }
+ }
+ sp->in_buffer_source=osibsNotSetYet;
+ sp->in_buffer_next_strile=0;
+ sp->in_buffer_strile_count=tif->tif_dir.td_nstrips;
+ sp->in_buffer_file_togo=0;
+ sp->in_buffer_togo=0;
+ do
+ {
+ if (OJPEGReadBytePeek(sp,&m)==0)
+ return(0);
+ if (m!=255)
+ break;
+ OJPEGReadByteAdvance(sp);
+ do
+ {
+ if (OJPEGReadByte(sp,&m)==0)
+ return(0);
+ } while(m==255);
+ switch(m)
+ {
+ case JPEG_MARKER_SOI:
+ /* this type of marker has no data, and should be skipped */
+ 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:
+ /* this type of marker has data, but it has no use to us (and no place here) and should be skipped */
+ if (OJPEGReadWord(sp,&n)==0)
+ return(0);
+ if (n<2)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
+ return(0);
+ }
+ if (n>2)
+ OJPEGReadSkip(sp,n-2);
+ break;
+ case JPEG_MARKER_DRI:
+ if (OJPEGReadHeaderInfoSecStreamDri(tif)==0)
+ return(0);
+ break;
+ case JPEG_MARKER_DQT:
+ if (OJPEGReadHeaderInfoSecStreamDqt(tif)==0)
+ return(0);
+ break;
+ case JPEG_MARKER_DHT:
+ if (OJPEGReadHeaderInfoSecStreamDht(tif)==0)
+ return(0);
+ break;
+ case JPEG_MARKER_SOF0:
+ case JPEG_MARKER_SOF1:
+ case JPEG_MARKER_SOF3:
+ if (OJPEGReadHeaderInfoSecStreamSof(tif,m)==0)
+ return(0);
+ if (sp->subsamplingcorrect!=0)
+ return(1);
+ break;
+ case JPEG_MARKER_SOS:
+ if (sp->subsamplingcorrect!=0)
+ return(1);
+ assert(sp->plane_sample_offset==0);
+ if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
+ return(0);
+ break;
+ default:
+ TIFFErrorExt(tif->tif_clientdata,module,"Unknown marker type %d in JPEG data",m);
+ return(0);
+ }
+ } while(m!=JPEG_MARKER_SOS);
+ if (sp->subsamplingcorrect)
+ return(1);
+ if (sp->sof_log==0)
+ {
+ if (OJPEGReadHeaderInfoSecTablesQTable(tif)==0)
+ return(0);
+ sp->sof_marker_id=JPEG_MARKER_SOF0;
+ for (o=0; o<sp->samples_per_pixel; o++)
+ sp->sof_c[o]=o;
+ sp->sof_hv[0]=((sp->subsampling_hor<<4)|sp->subsampling_ver);
+ for (o=1; o<sp->samples_per_pixel; o++)
+ sp->sof_hv[o]=17;
+ sp->sof_x=sp->strile_width;
+ sp->sof_y=sp->strile_length_total;
+ sp->sof_log=1;
+ if (OJPEGReadHeaderInfoSecTablesDcTable(tif)==0)
+ return(0);
+ if (OJPEGReadHeaderInfoSecTablesAcTable(tif)==0)
+ return(0);
+ for (o=1; o<sp->samples_per_pixel; o++)
+ sp->sos_cs[o]=o;
+ }
+ return(1);
+}
static int
-OJPEGSetupEncode(register TIFF *tif)
- { static const char module[]={"OJPEGSetupEncode"};
- uint32 segment_height, segment_width;
- int status = 1; /* Assume success by default */
- register OJPEGState *sp = OJState(tif);
-# define td (&tif->tif_dir)
-
- /* Verify miscellaneous parameters. This will need work if the TIFF Library
- ever supports different depths for different components, or if the JPEG
- Library ever supports run-time depth selection. Neither seems imminent.
- */
- if (td->td_bitspersample != 8)
- {
- TIFFError(module,bad_bps,td->td_bitspersample);
- status = 0;
- };
-
- /* The TIFF Version 6.0 specification and IJG JPEG Library accept different
- sets of color spaces, so verify that our image belongs to the common subset
- and map its photometry code, then initialize to handle subsampling and
- optional JPEG Library YCbCr <-> RGB color-space conversion.
- */
- switch (td->td_photometric)
- {
- case PHOTOMETRIC_YCBCR :
-
- /* ISO IS 10918-1 requires that JPEG subsampling factors be 1-4, but
- TIFF Version 6.0 is more restrictive: only 1, 2, and 4 are allowed.
- */
- if ( ( td->td_ycbcrsubsampling[0] == 1
- || td->td_ycbcrsubsampling[0] == 2
- || td->td_ycbcrsubsampling[0] == 4
- )
- && ( td->td_ycbcrsubsampling[1] == 1
- || td->td_ycbcrsubsampling[1] == 2
- || td->td_ycbcrsubsampling[1] == 4
- )
- )
- sp->cinfo.c.raw_data_in =
- ( (sp->h_sampling = td->td_ycbcrsubsampling[0]) << 3
- | (sp->v_sampling = td->td_ycbcrsubsampling[1])
- ) != 011;
- else
- {
- TIFFError(module,bad_subsampling);
- status = 0;
- };
-
- /* A ReferenceBlackWhite field MUST be present, since the default value
- is inapproriate for YCbCr. Fill in the proper value if the
- application didn't set it.
- */
- if (!TIFFFieldSet(tif,FIELD_REFBLACKWHITE))
- { 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);
- };
- sp->cinfo.c.jpeg_color_space = JCS_YCbCr;
- if (sp->jpegcolormode == JPEGCOLORMODE_RGB)
- {
- sp->cinfo.c.raw_data_in = FALSE;
- sp->in_color_space = JCS_RGB;
- break;
- };
- goto L2;
- case PHOTOMETRIC_MINISBLACK:
- sp->cinfo.c.jpeg_color_space = JCS_GRAYSCALE;
- goto L1;
- case PHOTOMETRIC_RGB :
- sp->cinfo.c.jpeg_color_space = JCS_RGB;
- goto L1;
- case PHOTOMETRIC_SEPARATED :
- sp->cinfo.c.jpeg_color_space = JCS_CMYK;
- L1: sp->jpegcolormode = JPEGCOLORMODE_RAW; /* No JPEG Lib. conversion */
- L2: sp->cinfo.d.in_color_space = sp->cinfo.d.jpeg_color-space;
- break;
- default :
- TIFFError(module,bad_photometry,td->td_photometric);
- status = 0;
- };
- tif->tif_encoderow = tif->tif_encodestrip = tif->tif_encodetile =
- sp->cinfo.c.raw_data_in ? OJPEGEncodeRaw : OJPEGEncode;
- if (isTiled(tif))
- { tsize_t size;
-
-# ifdef C_LOSSLESS_SUPPORTED
- if ((size = sp->v_sampling*sp->cinfo.c.data_unit) < 16) size = 16;
-# else
- if ((size = sp->v_sampling*DCTSIZE) < 16) size = 16;
-# endif
- if ((segment_height = td->td_tilelength) % size)
- {
- TIFFError(module,"JPEG tile height must be multiple of %d",size);
- status = 0;
- };
-# ifdef C_LOSSLESS_SUPPORTED
- if ((size = sp->h_sampling*sp->cinfo.c.data_unit) < 16) size = 16;
-# else
- if ((size = sp->h_sampling*DCTSIZE) < 16) size = 16;
-# endif
- if ((segment_width = td->td_tilewidth) % size)
- {
- TIFFError(module,"JPEG tile width must be multiple of %d",size);
- status = 0;
- };
- sp->bytesperline = TIFFTileRowSize(tif);
- }
- else
- { tsize_t size;
-
-# ifdef C_LOSSLESS_SUPPORTED
- if ((size = sp->v_sampling*sp->cinfo.c.data_unit) < 16) size = 16;
-# else
- if ((size = sp->v_sampling*DCTSIZE) < 16) size = 16;
-# endif
- if (td->td_rowsperstrip < (segment_height = td->td_imagelength))
- {
- if (td->td_rowsperstrip % size)
- {
- TIFFError(module,"JPEG RowsPerStrip must be multiple of %d",size);
- status = 0;
- };
- segment_height = td->td_rowsperstrip;
- };
- segment_width = td->td_imagewidth;
- sp->bytesperline = tif->tif_scanlinesize;
- };
- if (segment_width > 65535 || segment_height > 65535)
- {
- TIFFError(module,"Strip/tile too large for JPEG");
- status = 0;
- };
-
- /* Initialize all JPEG parameters to default values. Note that the JPEG
- Library's "jpeg_set_defaults()" method needs legal values for the
- "in_color_space" and "input_components" fields.
- */
- sp->cinfo.c.input_components = 1; /* Default for JCS_UNKNOWN */
- if (!CALLVJPEG(sp,jpeg_set_defaults(&sp->cinfo.c))) status = 0;
- switch (sp->jpegtablesmode & (JPEGTABLESMODE_HUFF|JPEGTABLESMODE_QUANT))
- { register JHUFF_TBL *htbl;
- register JQUANT_TBL *qtbl;
-
- case 0 :
- sp->cinfo.c.optimize_coding = TRUE;
- case JPEGTABLESMODE_HUFF :
- if (!CALLVJPEG(sp,jpeg_set_quality(&sp->cinfo.c,sp->jpegquality,FALSE)))
- return 0;
- if (qtbl = sp->cinfo.c.quant_tbl_ptrs[0]) qtbl->sent_table = FALSE;
- if (qtbl = sp->cinfo.c.quant_tbl_ptrs[1]) qtbl->sent_table = FALSE;
- goto L3;
- case JPEGTABLESMODE_QUANT :
- sp->cinfo.c.optimize_coding = TRUE;
-
- /* We do not support application-supplied JPEG tables, so mark the field
- "not present".
- */
- L3: TIFFClrFieldBit(tif,FIELD_JPEGTABLES);
- break;
- case JPEGTABLESMODE_HUFF|JPEGTABLESMODE_QUANT:
- if ( !CALLVJPEG(sp,jpeg_set_quality(&sp->cinfo.c,sp->jpegquality,FALSE))
- || !CALLVJPEG(sp,jpeg_suppress_tables(&sp->cinfo.c,TRUE))
- )
- {
- status = 0;
- break;
- };
- if (qtbl = sp->cinfo.c.quant_tbl_ptrs[0]) qtbl->sent_table = FALSE;
- if (htbl = sp->cinfo.c.dc_huff_tbl_ptrs[0]) htbl->sent_table = FALSE;
- if (htbl = sp->cinfo.c.ac_huff_tbl_ptrs[0]) htbl->sent_table = FALSE;
- if (sp->cinfo.c.jpeg_color_space == JCS_YCbCr)
- {
- if (qtbl = sp->cinfo.c.quant_tbl_ptrs[1])
- qtbl->sent_table = FALSE;
- if (htbl = sp->cinfo.c.dc_huff_tbl_ptrs[1])
- htbl->sent_table = FALSE;
- if (htbl = sp->cinfo.c.ac_huff_tbl_ptrs[1])
- htbl->sent_table = FALSE;
- };
- if ( TIFFojpeg_tables_dest(sp,tif)
- && CALLVJPEG(sp,jpeg_write_tables(&sp->cinfo.c))
- )
- {
-
- /* Mark the field "present". We can't use "TIFFSetField()" because
- "BEENWRITING" is already set!
- */
- TIFFSetFieldBit(tif,FIELD_JPEGTABLES);
- tif->tif_flags |= TIFF_DIRTYDIRECT;
- }
- else status = 0;
- };
- if ( sp->cinfo.c.raw_data_in
- && !alloc_downsampled_buffers(tif,sp->cinfo.c.comp_info,
- sp->cinfo.c.num_components)
- ) status = 0;
- if (status == 0) return 0; /* If TIFF errors, don't bother to continue */
- /* Grab parameters that are same for all strips/tiles. */
-
- sp->dest.init_destination = std_init_destination;
- sp->dest.empty_output_buffer = std_empty_output_buffer;
- sp->dest.term_destination = std_term_destination;
- sp->cinfo.c.dest = &sp->dest;
- sp->cinfo.c.data_precision = td->td_bitspersample;
- sp->cinfo.c.write_JFIF_header = /* Don't write extraneous markers */
- sp->cinfo.c.write_Adobe_marker = FALSE;
- sp->cinfo.c.image_width = segment_width;
- sp->cinfo.c.image_height = segment_height;
- sp->cinfo.c.comp_info[0].h_samp_factor =
- sp->cinfo.c.comp_info[0].v_samp_factor = 1;
- return CALLVJPEG(sp,jpeg_start_compress(&sp->cinfo.c,FALSE));
-# undef td
- }
+OJPEGReadHeaderInfoSecStreamDri(TIFF* tif)
+{
+ /* this could easilly cause trouble in some cases... but no such cases have occured sofar */
+ static const char module[]="OJPEGReadHeaderInfoSecStreamDri";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint16 m;
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ if (m!=4)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DRI marker in JPEG data");
+ return(0);
+ }
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ sp->restart_interval=m;
+ return(1);
+}
static int
-OJPEGPreEncode(register TIFF *tif,tsample_t s)
- { register OJPEGState *sp = OJState(tif);
-# define td (&tif->tif_dir)
-
- /* If we are about to write the first row of an image plane, which should
- coincide with a JPEG "scan", reset the JPEG Library's compressor. Otherwise
- let the compressor run "as is" and return a "success" status without further
- ado.
- */
- if ( (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip)
- % td->td_stripsperimage
- == 0
- )
- {
- if ( (sp->cinfo.c.comp_info[0].component_id = s) == 1)
- && sp->cinfo.c.jpeg_color_space == JCS_YCbCr
- )
- {
- sp->cinfo.c.comp_info[0].quant_tbl_no =
- sp->cinfo.c.comp_info[0].dc_tbl_no =
- sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
- sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
- sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
-
- /* Scale expected strip/tile size to match a downsampled component. */
-
- sp->cinfo.c.image_width = TIFFhowmany(segment_width,sp->h_sampling);
- sp->cinfo.c.image_height=TIFFhowmany(segment_height,sp->v_sampling);
- };
- sp->scancount = 0; /* Mark subsampling buffer(s) empty */
- };
- return 1;
-# undef td
- }
+OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif)
+{
+ /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
+ static const char module[]="OJPEGReadHeaderInfoSecStreamDqt";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint16 m;
+ uint32 na;
+ uint8* nb;
+ uint8 o;
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ if (m<=2)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
+ return(0);
+ }
+ if (sp->subsamplingcorrect!=0)
+ OJPEGReadSkip(sp,m-2);
+ else
+ {
+ m-=2;
+ do
+ {
+ if (m<65)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
+ return(0);
+ }
+ na=sizeof(uint32)+69;
+ nb=_TIFFmalloc(na);
+ if (nb==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ *(uint32*)nb=na;
+ nb[sizeof(uint32)]=255;
+ nb[sizeof(uint32)+1]=JPEG_MARKER_DQT;
+ nb[sizeof(uint32)+2]=0;
+ nb[sizeof(uint32)+3]=67;
+ if (OJPEGReadBlock(sp,65,&nb[sizeof(uint32)+4])==0)
+ return(0);
+ o=nb[sizeof(uint32)+4]&15;
+ if (3<o)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
+ return(0);
+ }
+ if (sp->qtable[o]!=0)
+ _TIFFfree(sp->qtable[o]);
+ sp->qtable[o]=nb;
+ m-=65;
+ } while(m>0);
+ }
+ return(1);
+}
static int
-OJPEGPostEncode(register TIFF *tif)
- { register OJPEGState *sp = OJState(tif);
-
- /* Finish up at the end of a strip or tile. */
-
- if (sp->scancount > 0) /* emit partial buffer of down-sampled data */
- { JDIMENSION n;
-
-# ifdef C_LOSSLESS_SUPPORTED
- if ( sp->scancount < sp->cinfo.c.data_unit
- && sp->cinfo.c.num_components > 0
- )
-# else
- if (sp->scancount < DCTSIZE && sp->cinfo.c.num_components > 0)
-# endif
- { int ci = 0, /* Pad the data vertically */
-# ifdef C_LOSSLESS_SUPPORTED
- size = sp->cinfo.c.data_unit;
-# else
- size = DCTSIZE;
-# endif
- register jpeg_component_info *compptr = sp->cinfo.c.comp_info;
-
- do
-# ifdef C_LOSSLESS_SUPPORTED
- { tsize_t row_width = compptr->width_in_data_units
-# else
- tsize_t row_width = compptr->width_in_blocks
-# endif
- *size*sizeof(JSAMPLE);
- int ypos = sp->scancount*compptr->v_samp_factor;
-
- do _TIFFmemcpy( (tdata_t)sp->ds_buffer[ci][ypos]
- , (tdata_t)sp->ds_buffer[ci][ypos-1]
- , row_width
- );
- while (++ypos < compptr->v_samp_factor*size);
- }
- while (++compptr,++ci < sp->cinfo.c.num_components);
- };
- n = sp->cinfo.c.max_v_samp_factor*size;
- if (CALLJPEG(sp,-1,jpeg_write_raw_data(&sp->cinfo.c,sp->ds_buffer,n)) != n)
- return 0;
- };
- return CALLVJPEG(sp,jpeg_finish_compress(&sp->cinfo.c));
- }
-#endif /* never */
-
-/* JPEG Decoding begins here. */
-
-/*ARGSUSED*/ static int
-OJPEGDecode(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s)
- { tsize_t bytesperline = isTiled(tif)
- ? TIFFTileRowSize(tif)
- : tif->tif_scanlinesize,
- rows; /* No. of unprocessed rows in file */
- register OJPEGState *sp = OJState(tif);
-
- /* Decode a chunk of pixels, where the input data has not NOT been down-
- sampled, or else the TIFF Library's client has used the "JPEGColorMode" TIFF
- pseudo-tag to request that the JPEG Library do color-space conversion; this
- is the normal case. The data is expected to be read in scan-line multiples,
- and this subroutine is called for both pixel-interleaved and separate color
- planes.
-
- WARNING: Unlike "OJPEGDecodeRawContig()", below, the no. of Bytes in each
- decoded row is calculated here as "bytesperline" instead of
- using "sp->bytesperline", which might be a little smaller. This can
- occur for an old tiled image whose width isn't a multiple of 8 pixels.
- That's illegal according to the TIFF Version 6 specification, but some
- test files, like "zackthecat.tif", were built that way. In those cases,
- we want to embed the image's true width in our caller's buffer (which is
- presumably allocated according to the expected tile width) by
- effectively "padding" it with unused Bytes at the end of each row.
- */
- if ( (cc /= bytesperline) /* No. of complete rows in caller's buffer */
- > (rows = sp->cinfo.d.output_height - sp->cinfo.d.output_scanline)
- ) cc = rows;
- while (--cc >= 0)
- {
- if ( CALLJPEG(sp,-1,jpeg_read_scanlines(&sp->cinfo.d,(JSAMPARRAY)&buf,1))
- != 1
- ) return 0;
- buf += bytesperline;
- ++tif->tif_row;
- };
-
- /* BEWARE OF KLUDGE: If our input file was produced by Microsoft's Wang
- Imaging for Windows application, the DC coefficients of
- each JPEG image component (Y,Cb,Cr) must be reset at the end of each TIFF
- "strip", and any JPEG data bits remaining in the current Byte of the
- decoder's input buffer must be discarded. To do so, we create an "ad hoc"
- interface in the "jdhuff.c" module of IJG JPEG Library Version 6 (module
- "jdshuff.c", if Ken Murchison's lossless-Huffman patch is applied), and we
- invoke that interface here after decoding each "strip".
- */
- if (sp->is_WANG) jpeg_reset_huff_decode(&sp->cinfo.d);
- return 1;
- }
-
-/*ARGSUSED*/ static int
-OJPEGDecodeRawContig(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s)
- { tsize_t rows; /* No. of unprocessed rows in file */
- JDIMENSION lines_per_MCU, size;
- register OJPEGState *sp = OJState(tif);
-
- /* Decode a chunk of pixels, where the input data has pixel-interleaved color
- planes, some of which have been down-sampled, but the TIFF Library's client
- has NOT used the "JPEGColorMode" TIFF pseudo-tag to request that the JPEG
- Library do color-space conversion. In other words, we must up-sample/
- expand/duplicate image components according to the image's sampling factors,
- without changing its color space. The data is expected to be read in scan-
- line multiples.
- */
- if ( (cc /= sp->bytesperline) /* No. of complete rows in caller's buffer */
- > (rows = sp->cinfo.d.output_height - sp->cinfo.d.output_scanline)
- ) cc = rows;
- lines_per_MCU = sp->cinfo.d.max_v_samp_factor
-# ifdef D_LOSSLESS_SUPPORTED
- * (size = sp->cinfo.d.min_codec_data_unit);
-# else
- * (size = DCTSIZE);
-# endif
- while (--cc >= 0)
- { int clumpoffset, ci;
- register jpeg_component_info *compptr;
-
- if (sp->scancount >= size) /* reload downsampled-data buffers */
- {
- if ( CALLJPEG(sp,-1,jpeg_read_raw_data(&sp->cinfo.d,sp->ds_buffer,lines_per_MCU))
- != lines_per_MCU
- ) return 0;
- sp->scancount = 0;
- };
-
- /* The fastest way to separate the data is: make 1 pass over the scan
- line for each row of each component.
- */
- clumpoffset = ci = 0;
- compptr = sp->cinfo.d.comp_info;
- do
- { int ypos = 0;
-
- if (compptr->h_samp_factor == 1) /* fast path */
- do
- { register JSAMPLE *inptr =
- sp->ds_buffer[ci][sp->scancount*compptr->v_samp_factor+ypos],
- *outptr = (JSAMPLE *)buf + clumpoffset;
- register int clumps_per_line = compptr->downsampled_width;
-
- do *outptr = *inptr++;
- while ((outptr += sp->samplesperclump),--clumps_per_line > 0);
- }
- while ( (clumpoffset += compptr->h_samp_factor)
- , ++ypos < compptr->v_samp_factor
- );
- else /* general case */
- do
- { register JSAMPLE *inptr =
- sp->ds_buffer[ci][sp->scancount*compptr->v_samp_factor+ypos],
- *outptr = (JSAMPLE *)buf + clumpoffset;
- register int clumps_per_line = compptr->downsampled_width;
-
- do
- { register int xpos = 0;
-
- do outptr[xpos] = *inptr++;
- while (++xpos < compptr->h_samp_factor);
- }
- while ((outptr += sp->samplesperclump),--clumps_per_line > 0);
- }
- while ( (clumpoffset += compptr->h_samp_factor)
- , ++ypos < compptr->v_samp_factor
- );
- }
- while (++compptr,++ci < sp->cinfo.d.num_components);
- ++sp->scancount;
- buf += sp->bytesperline;
- ++tif->tif_row;
- };
-
- /* BEWARE OF KLUDGE: If our input file was produced by Microsoft's Wang
- Imaging for Windows application, the DC coefficients of
- each JPEG image component (Y,Cb,Cr) must be reset at the end of each TIFF
- "strip", and any JPEG data bits remaining in the current Byte of the
- decoder's input buffer must be discarded. To do so, we create an "ad hoc"
- interface in the "jdhuff.c" module of IJG JPEG Library Version 6 (module
- "jdshuff.c", if Ken Murchison's lossless-Huffman patch is applied), and we
- invoke that interface here after decoding each "strip".
- */
- if (sp->is_WANG) jpeg_reset_huff_decode(&sp->cinfo.d);
- return 1;
- }
-
-/*ARGSUSED*/ static int
-OJPEGDecodeRawSeparate(TIFF *tif,register tidata_t buf,tsize_t cc,tsample_t s)
- { tsize_t rows; /* No. of unprocessed rows in file */
- JDIMENSION lines_per_MCU,
- size, /* ...of MCU */
- v; /* Component's vertical up-sampling ratio */
- register OJPEGState *sp = OJState(tif);
- register jpeg_component_info *compptr = sp->cinfo.d.comp_info + s;
-
- /* Decode a chunk of pixels, where the input data has separate color planes,
- some of which have been down-sampled, but the TIFF Library's client has NOT
- used the "JPEGColorMode" TIFF pseudo-tag to request that the JPEG Library
- do color-space conversion. The data is expected to be read in scan-line
- multiples.
- */
- v = sp->cinfo.d.max_v_samp_factor/compptr->v_samp_factor;
- if ( (cc /= compptr->downsampled_width) /* No. of rows in caller's buffer */
- > (rows = (sp->cinfo.d.output_height-sp->cinfo.d.output_scanline+v-1)/v)
- ) cc = rows; /* No. of rows of "clumps" to read */
- lines_per_MCU = sp->cinfo.d.max_v_samp_factor
-# ifdef D_LOSSLESS_SUPPORTED
- * (size = sp->cinfo.d.min_codec_data_unit);
-# else
- * (size = DCTSIZE);
-# endif
- L: if (sp->scancount >= size) /* reload downsampled-data buffers */
- {
- if ( CALLJPEG(sp,-1,jpeg_read_raw_data(&sp->cinfo.d,sp->ds_buffer,lines_per_MCU))
- != lines_per_MCU
- ) return 0;
- sp->scancount = 0;
- };
- rows = 0;
- do
- { register JSAMPLE *inptr =
- sp->ds_buffer[s][sp->scancount*compptr->v_samp_factor + rows];
- register int clumps_per_line = compptr->downsampled_width;
-
- do *buf++ = *inptr++; while (--clumps_per_line > 0); /* Copy scanline */
- tif->tif_row += v;
- if (--cc <= 0) return 1; /* End of caller's buffer? */
- }
- while (++rows < compptr->v_samp_factor);
- ++sp->scancount;
- goto L;
- }
-
-/* "OJPEGSetupDecode()" temporarily forces the JPEG Library to use the following
- subroutine as a "dummy" input reader in order to fool the library into
- thinking that it has read the image's first "Start of Scan" (SOS) marker, so
- that it initializes accordingly.
-*/
-/*ARGSUSED*/ METHODDEF(int)
-fake_SOS_marker(j_decompress_ptr cinfo){return JPEG_REACHED_SOS;}
-
-/*ARGSUSED*/ METHODDEF(int)
-suspend(j_decompress_ptr cinfo){return JPEG_SUSPENDED;}
-
-/* The JPEG Library's "null" color-space converter actually re-packs separate
- color planes (it's native image representation) into a pixel-interleaved,
- contiguous plane. But if our TIFF Library client is tryng to process a
- PLANARCONFIG_SEPARATE image, we don't want that; so here are modifications of
- code in the JPEG Library's "jdcolor.c" file, which simply copy Bytes to a
- color plane specified by the current JPEG "scan".
-*/
-METHODDEF(void)
-ycc_rgb_convert(register j_decompress_ptr cinfo,JSAMPIMAGE in,JDIMENSION row,
- register JSAMPARRAY out,register int nrows)
- { typedef struct /* "jdcolor.c" color-space conversion state */
- {
-
- /* WARNING: This declaration is ugly and dangerous! It's supposed to be
- private to the JPEG Library's "jdcolor.c" module, but we also
- need it here. Since the library's copy might change without notice, be
- sure to keep this one synchronized or the following code will break!
- */
- struct jpeg_color_deconverter pub; /* Public fields */
- /* Private state for YCC->RGB conversion */
- int *Cr_r_tab, /* ->Cr to R conversion table */
- *Cb_b_tab; /* ->Cb to B conversion table */
- INT32 *Cr_g_tab, /* ->Cr to G conversion table */
- *Cb_g_tab; /* ->Cb to G conversion table */
- } *my_cconvert_ptr;
- my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
- JSAMPARRAY irow0p = in[0] + row;
- register JSAMPLE *range_limit = cinfo->sample_range_limit;
- register JSAMPROW outp, Y;
-
- switch (cinfo->output_scan_number - 1)
- { JSAMPARRAY irow1p, irow2p;
- register INT32 *table0, *table1;
- SHIFT_TEMPS
-
- case RGB_RED : irow2p = in[2] + row;
- table0 = (INT32 *)cconvert->Cr_r_tab;
- while (--nrows >= 0)
- { register JSAMPROW Cr = *irow2p++;
- register int i = cinfo->output_width;
-
- Y = *irow0p++;
- outp = *out++;
- while (--i >= 0)
- *outp++ = range_limit[*Y++ + table0[*Cr++]];
- };
- return;
- case RGB_GREEN: irow1p = in[1] + row;
- irow2p = in[2] + row;
- table0 = cconvert->Cb_g_tab;
- table1 = cconvert->Cr_g_tab;
- while (--nrows >= 0)
- { register JSAMPROW Cb = *irow1p++,
- Cr = *irow2p++;
- register int i = cinfo->output_width;
-
- Y = *irow0p++;
- outp = *out++;
- while (--i >= 0)
- *outp++ =
- range_limit[ *Y++
- + RIGHT_SHIFT(table0[*Cb++]+table1[*Cr++],16)
- ];
- };
- return;
- case RGB_BLUE : irow1p = in[1] + row;
- table0 = (INT32 *)cconvert->Cb_b_tab;
- while (--nrows >= 0)
- { register JSAMPROW Cb = *irow1p++;
- register int i = cinfo->output_width;
-
- Y = *irow0p++;
- outp = *out++;
- while (--i >= 0)
- *outp++ = range_limit[*Y++ + table0[*Cb++]];
- }
- }
- }
-
-METHODDEF(void)
-null_convert(register j_decompress_ptr cinfo,JSAMPIMAGE in,JDIMENSION row,
- register JSAMPARRAY out,register int nrows)
- { register JSAMPARRAY irowp = in[cinfo->output_scan_number - 1] + row;
-
- while (--nrows >= 0) _TIFFmemcpy(*out++,*irowp++,cinfo->output_width);
- }
+OJPEGReadHeaderInfoSecStreamDht(TIFF* tif)
+{
+ /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
+ /* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
+ static const char module[]="OJPEGReadHeaderInfoSecStreamDht";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint16 m;
+ uint32 na;
+ uint8* nb;
+ uint8 o;
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ if (m<=2)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+ return(0);
+ }
+ if (sp->subsamplingcorrect!=0)
+ {
+ OJPEGReadSkip(sp,m-2);
+ }
+ else
+ {
+ na=sizeof(uint32)+2+m;
+ nb=_TIFFmalloc(na);
+ if (nb==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ *(uint32*)nb=na;
+ nb[sizeof(uint32)]=255;
+ nb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
+ nb[sizeof(uint32)+2]=(m>>8);
+ nb[sizeof(uint32)+3]=(m&255);
+ if (OJPEGReadBlock(sp,m-2,&nb[sizeof(uint32)+4])==0)
+ return(0);
+ o=nb[sizeof(uint32)+4];
+ if ((o&240)==0)
+ {
+ if (3<o)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+ return(0);
+ }
+ if (sp->dctable[o]!=0)
+ _TIFFfree(sp->dctable[o]);
+ sp->dctable[o]=nb;
+ }
+ else
+ {
+ if ((o&240)!=16)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+ return(0);
+ }
+ o&=15;
+ if (3<o)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+ return(0);
+ }
+ if (sp->actable[o]!=0)
+ _TIFFfree(sp->actable[o]);
+ sp->actable[o]=nb;
+ }
+ }
+ return(1);
+}
static int
-OJPEGSetupDecode(register TIFF *tif)
- { static char module[]={"OJPEGSetupDecode"};
- J_COLOR_SPACE jpeg_color_space, /* Color space of JPEG-compressed image */
- out_color_space; /* Color space of decompressed image */
- uint32 segment_width;
- int status = 1; /* Assume success by default */
- boolean downsampled_output=FALSE, /* <=> Want JPEG Library's "raw" image? */
- is_JFIF; /* <=> JFIF image? */
- register OJPEGState *sp = OJState(tif);
-# define td (&tif->tif_dir)
-
- /* Verify miscellaneous parameters. This will need work if the TIFF Library
- ever supports different depths for different components, or if the JPEG
- Library ever supports run-time depth selection. Neither seems imminent.
- */
- if (td->td_bitspersample != sp->cinfo.d.data_precision)
- {
- TIFFError(module,bad_bps,td->td_bitspersample);
- status = 0;
- };
-
- /* The TIFF Version 6.0 specification and IJG JPEG Library accept different
- sets of color spaces, so verify that our image belongs to the common subset
- and map its photometry code, then initialize to handle subsampling and
- optional JPEG Library YCbCr <-> RGB color-space conversion.
- */
- switch (td->td_photometric)
- {
- case PHOTOMETRIC_YCBCR :
-
- /* ISO IS 10918-1 requires that JPEG subsampling factors be 1-4, but
- TIFF Version 6.0 is more restrictive: only 1, 2, and 4 are allowed.
- */
- if ( ( td->td_ycbcrsubsampling[0] == 1
- || td->td_ycbcrsubsampling[0] == 2
- || td->td_ycbcrsubsampling[0] == 4
- )
- && ( td->td_ycbcrsubsampling[1] == 1
- || td->td_ycbcrsubsampling[1] == 2
- || td->td_ycbcrsubsampling[1] == 4
- )
- )
- downsampled_output =
- (
- (sp->h_sampling = td->td_ycbcrsubsampling[0]) << 3
- | (sp->v_sampling = td->td_ycbcrsubsampling[1])
- ) != 011;
- else
- {
- TIFFError(module,bad_subsampling);
- status = 0;
- };
- jpeg_color_space = JCS_YCbCr;
- if (sp->jpegcolormode == JPEGCOLORMODE_RGB)
- {
- downsampled_output = FALSE;
- out_color_space = JCS_RGB;
- break;
- };
- goto L2;
- case PHOTOMETRIC_MINISBLACK:
- jpeg_color_space = JCS_GRAYSCALE;
- goto L1;
- case PHOTOMETRIC_RGB :
- jpeg_color_space = JCS_RGB;
- goto L1;
- case PHOTOMETRIC_SEPARATED :
- jpeg_color_space = JCS_CMYK;
- L1: sp->jpegcolormode = JPEGCOLORMODE_RAW; /* No JPEG Lib. conversion */
- L2: out_color_space = jpeg_color_space;
- break;
- default :
- TIFFError(module,bad_photometry,td->td_photometric);
- status = 0;
- };
- if (status == 0) return 0; /* If TIFF errors, don't bother to continue */
-
- /* Set parameters that are same for all strips/tiles. */
-
- 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;
-
- /* BOGOSITY ALERT! The Wang Imaging application for Microsoft Windows produces
- images containing "JPEGInterchangeFormat[Length]" TIFF
- records that resemble JFIF-in-TIFF encapsulations but, in fact, violate the
- TIFF Version 6 specification in several ways; nevertheless, we try to handle
- them gracefully because there are apparently a lot of them around. The
- purported "JFIF" data stream in one of these files vaguely resembles a JPEG
- "tables only" data stream, except that there's no trailing EOI marker. The
- rest of the JPEG data stream lies in a discontiguous file region, identified
- by the 0th Strip offset (which is *also* illegal!), where it begins with an
- SOS marker and apparently continues to the end of the file. There is no
- trailing EOI marker here, either.
- */
- is_JFIF = !sp->is_WANG && TIFFFieldSet(tif,FIELD_JPEGIFOFFSET);
+OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id)
+{
+ /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
+ static const char module[]="OJPEGReadHeaderInfoSecStreamSof";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint16 m;
+ uint16 n;
+ uint8 o;
+ uint16 p;
+ uint16 q;
+ if (sp->sof_log!=0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
+ return(0);
+ }
+ if (sp->subsamplingcorrect==0)
+ sp->sof_marker_id=marker_id;
+ /* Lf: data length */
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ if (m<11)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
+ return(0);
+ }
+ m-=8;
+ if (m%3!=0)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
+ return(0);
+ }
+ n=m/3;
+ if (sp->subsamplingcorrect==0)
+ {
+ if (n!=sp->samples_per_pixel)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of samples");
+ return(0);
+ }
+ }
+ /* P: Sample precision */
+ if (OJPEGReadByte(sp,&o)==0)
+ return(0);
+ if (o!=8)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of bits per sample");
+ return(0);
+ }
+ /* Y: Number of lines, X: Number of samples per line */
+ if (sp->subsamplingcorrect)
+ OJPEGReadSkip(sp,4);
+ else
+ {
+ /* TODO: probably best to also add check on allowed upper bound, especially x, may cause buffer overflow otherwise i think */
+ /* Y: Number of lines */
+ if (OJPEGReadWord(sp,&p)==0)
+ return(0);
+ if ((p<sp->image_length) && (p<sp->strile_length_total))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected height");
+ return(0);
+ }
+ sp->sof_y=p;
+ /* X: Number of samples per line */
+ if (OJPEGReadWord(sp,&p)==0)
+ return(0);
+ if ((p<sp->image_width) && (p<sp->strile_width))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected width");
+ return(0);
+ }
+ sp->sof_x=p;
+ }
+ /* Nf: Number of image components in frame */
+ if (OJPEGReadByte(sp,&o)==0)
+ return(0);
+ if (o!=n)
+ {
+ if (sp->subsamplingcorrect==0)
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
+ return(0);
+ }
+ /* per component stuff */
+ /* TODO: double-check that flow implies that n cannot be as big as to make us overflow sof_c, sof_hv and sof_tq arrays */
+ for (q=0; q<n; q++)
+ {
+ /* C: Component identifier */
+ if (OJPEGReadByte(sp,&o)==0)
+ return(0);
+ if (sp->subsamplingcorrect==0)
+ sp->sof_c[q]=o;
+ /* H: Horizontal sampling factor, and V: Vertical sampling factor */
+ if (OJPEGReadByte(sp,&o)==0)
+ return(0);
+ if (sp->subsamplingcorrect!=0)
+ {
+ if (q==0)
+ {
+ sp->subsampling_hor=(o>>4);
+ sp->subsampling_ver=(o&15);
+ if (((sp->subsampling_hor!=1) && (sp->subsampling_hor!=2) && (sp->subsampling_hor!=4)) ||
+ ((sp->subsampling_ver!=1) && (sp->subsampling_ver!=2) && (sp->subsampling_ver!=4)))
+ sp->subsampling_force_desubsampling_inside_decompression=1;
+ }
+ else
+ {
+ if (o!=17)
+ sp->subsampling_force_desubsampling_inside_decompression=1;
+ }
+ }
+ else
+ {
+ sp->sof_hv[q]=o;
+ if (sp->subsampling_force_desubsampling_inside_decompression==0)
+ {
+ if (q==0)
+ {
+ if (o!=((sp->subsampling_hor<<4)|sp->subsampling_ver))
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
+ return(0);
+ }
+ }
+ else
+ {
+ if (o!=17)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
+ return(0);
+ }
+ }
+ }
+ }
+ /* Tq: Quantization table destination selector */
+ if (OJPEGReadByte(sp,&o)==0)
+ return(0);
+ if (sp->subsamplingcorrect==0)
+ sp->sof_tq[q]=o;
+ }
+ if (sp->subsamplingcorrect==0)
+ sp->sof_log=1;
+ return(1);
+}
- /* Initialize decompression parameters that won't be overridden by JPEG Library
- defaults set during the "jpeg_read_header()" call, below.
- */
- segment_width = td->td_imagewidth;
- if (isTiled(tif))
- {
- if (sp->is_WANG) /* we don't know how to handle it */
- {
- TIFFError(module,"Tiled Wang image not supported");
- return 0;
- };
-
- /* BOGOSITY ALERT! "TIFFTileRowSize()" seems to work fine for modern JPEG-
- in-TIFF encapsulations where the image width--like the
- tile width--is a multiple of 8 or 16 pixels. But image widths and
- heights are aren't restricted to 8- or 16-bit multiples, and we need
- the exact Byte count of decompressed scan lines when we call the JPEG
- Library. At least one old file ("zackthecat.tif") in the TIFF Library
- test suite has widths and heights slightly less than the tile sizes, and
- it apparently used the bogus computation below to determine the number
- of Bytes per scan line (was this due to an old, broken version of
- "TIFFhowmany()"?). Before we get here, "OJPEGSetupDecode()" verified
- that our image uses 8-bit samples, so the following check appears to
- return the correct answer in all known cases tested to date.
- */
- if (is_JFIF || (segment_width & 7) == 0)
- sp->bytesperline = TIFFTileRowSize(tif); /* Normal case */
- else
- {
- /* Was the file-encoder's segment-width calculation bogus? */
- segment_width = (segment_width/sp->h_sampling + 1) * sp->h_sampling;
- sp->bytesperline = segment_width * td->td_samplesperpixel;
- }
- }
- else sp->bytesperline = TIFFVStripSize(tif,1);
-
- /* BEWARE OF KLUDGE: If we have JPEG Interchange File Format (JFIF) image,
- then we want to read "metadata" in the bit-stream's
- header and validate it against corresponding information in TIFF records.
- But if we have a *really old* JPEG file that's not JFIF, then we simply
- assign TIFF-record values to JPEG Library variables without checking.
- */
- if (is_JFIF) /* JFIF image */
- { unsigned char *end_of_data;
- int subsampling_factors;
- register unsigned char *p;
- register int i;
-
- /* WARNING: Although the image file contains a JFIF bit stream, it might
- also contain some old TIFF records causing "OJPEGVSetField()"
- to have allocated quantization or Huffman decoding tables. But when the
- JPEG Library reads and parses the JFIF header below, it reallocate these
- tables anew without checking for "dangling" pointers, thereby causing a
- memory "leak". We have enough information to potentially deallocate the
- old tables here, but unfortunately JPEG Library Version 6B uses a "pool"
- allocator for small objects, with no deallocation procedure; instead, it
- reclaims a whole pool when an image is closed/destroyed, so well-behaved
- TIFF client applications (i.e., those which close their JPEG images as
- soon as they're no longer needed) will waste memory for a short time but
- recover it eventually. But ill-behaved TIFF clients (i.e., those which
- keep many JPEG images open gratuitously) can exhaust memory prematurely.
- If the JPEG Library ever implements a deallocation procedure, insert
- this clean-up code:
- */
-# ifdef someday
- if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) /* free quant. tables */
- { register int i = 0;
-
- do
- { register JQUANT_TBL *q;
-
- if (q = sp->cinfo.d.quant_tbl_ptrs[i])
- {
- jpeg_free_small(&sp->cinfo.comm,q,sizeof *q);
- sp->cinfo.d.quant_tbl_ptrs[i] = 0;
- }
- }
- while (++i < NUM_QUANT_TBLS);
- };
- if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) /* free Huffman tables */
- { register int i = 0;
-
- do
- { register JHUFF_TBL *h;
-
- if (h = sp->cinfo.d.dc_huff_tbl_ptrs[i])
- {
- jpeg_free_small(&sp->cinfo.comm,h,sizeof *h);
- sp->cinfo.d.dc_huff_tbl_ptrs[i] = 0;
- };
- if (h = sp->cinfo.d.ac_huff_tbl_ptrs[i])
- {
- jpeg_free_small(&sp->cinfo.comm,h,sizeof *h);
- sp->cinfo.d.ac_huff_tbl_ptrs[i] = 0;
- }
- }
- while (++i < NUM_HUFF_TBLS);
- };
-# endif /* someday */
-
- /* Since we might someday wish to try rewriting "old format" JPEG-in-TIFF
- encapsulations in "new format" files, try to synthesize the value of a
- modern "JPEGTables" TIFF record by scanning the JPEG data from just past
- the "Start of Information" (SOI) marker until something other than a
- legitimate "table" marker is found, as defined in ISO IS 10918-1
- Appending B.2.4; namely:
-
- -- Define Quantization Table (DQT)
- -- Define Huffman Table (DHT)
- -- Define Arithmetic Coding table (DAC)
- -- Define Restart Interval (DRI)
- -- Comment (COM)
- -- Application data (APPn)
-
- For convenience, we also accept "Expansion" (EXP) markers, although they
- are apparently not a part of normal "table" data.
- */
- sp->jpegtables = p = (unsigned char *)sp->src.next_input_byte;
- end_of_data = p + sp->src.bytes_in_buffer;
- p += 2;
- while (p < end_of_data && p[0] == 0xFF)
- switch (p[1])
- {
- default : goto L;
- case 0xC0: /* SOF0 */
- case 0xC1: /* SOF1 */
- case 0xC2: /* SOF2 */
- case 0xC3: /* SOF3 */
- case 0xC4: /* DHT */
- case 0xC5: /* SOF5 */
- case 0xC6: /* SOF6 */
- case 0xC7: /* SOF7 */
- case 0xC9: /* SOF9 */
- case 0xCA: /* SOF10 */
- case 0xCB: /* SOF11 */
- case 0xCC: /* DAC */
- case 0xCD: /* SOF13 */
- case 0xCE: /* SOF14 */
- case 0xCF: /* SOF15 */
- case 0xDB: /* DQT */
- case 0xDD: /* DRI */
- case 0xDF: /* EXP */
- case 0xE0: /* APP0 */
- case 0xE1: /* APP1 */
- case 0xE2: /* APP2 */
- case 0xE3: /* APP3 */
- case 0xE4: /* APP4 */
- case 0xE5: /* APP5 */
- case 0xE6: /* APP6 */
- case 0xE7: /* APP7 */
- case 0xE8: /* APP8 */
- case 0xE9: /* APP9 */
- case 0xEA: /* APP10 */
- case 0xEB: /* APP11 */
- case 0xEC: /* APP12 */
- case 0xED: /* APP13 */
- case 0xEE: /* APP14 */
- case 0xEF: /* APP15 */
- case 0xFE: /* COM */
- p += (p[2] << 8 | p[3]) + 2;
- };
- L: if (p - (unsigned char *)sp->jpegtables > 2) /* fake "JPEGTables" */
- {
-
- /* In case our client application asks, pretend that this image file
- contains a modern "JPEGTables" TIFF record by copying to a buffer
- the initial part of the JFIF bit-stream that we just scanned, from
- the SOI marker through the "metadata" tables, then append an EOI
- marker and flag the "JPEGTables" TIFF record as "present".
- */
- sp->jpegtables_length = p - (unsigned char*)sp->jpegtables + 2;
- p = sp->jpegtables;
- if (!(sp->jpegtables = _TIFFmalloc(sp->jpegtables_length)))
- {
- TIFFError(module,no_jtable_space);
- return 0;
- };
- _TIFFmemcpy(sp->jpegtables,p,sp->jpegtables_length-2);
- p = (unsigned char *)sp->jpegtables + sp->jpegtables_length;
- p[-2] = 0xFF; p[-1] = JPEG_EOI; /* Append EOI marker */
- TIFFSetFieldBit(tif,FIELD_JPEGTABLES);
- tif->tif_flags |= TIFF_DIRTYDIRECT;
- }
- else sp->jpegtables = 0; /* Don't simulate "JPEGTables" */
- if ( CALLJPEG(sp,-1,jpeg_read_header(&sp->cinfo.d,TRUE))
- != JPEG_HEADER_OK
- ) return 0;
- if ( sp->cinfo.d.image_width != segment_width
- || sp->cinfo.d.image_height != td->td_imagelength
- )
- {
- TIFFError(module,"Improper JPEG strip/tile size");
- return 0;
- };
- if (sp->cinfo.d.num_components != td->td_samplesperpixel)
- {
- TIFFError(module,"Improper JPEG component count");
- return 0;
- };
- if (sp->cinfo.d.data_precision != td->td_bitspersample)
- {
- TIFFError(module,"Improper JPEG data precision");
- return 0;
- };
-
- /* Check that JPEG image components all have the same subsampling factors
- declared (or defaulted) in the TIFF file, since TIFF Version 6.0 is more
- restrictive than JPEG: Only the 0th component may have horizontal and
- vertical subsampling factors other than <1,1>.
- */
- subsampling_factors = sp->h_sampling << 3 | sp->v_sampling;
- i = 0;
- do
- {
- if ( ( sp->cinfo.d.comp_info[i].h_samp_factor << 3
- | sp->cinfo.d.comp_info[i].v_samp_factor
- )
- != subsampling_factors
- )
- {
- TIFFError(module,"Improper JPEG subsampling factors");
- return 0;
- };
- subsampling_factors = 011; /* Required for image components > 0 */
- }
- while (++i < sp->cinfo.d.num_components);
- }
- else /* not JFIF image */
- { int (*save)(j_decompress_ptr cinfo) = sp->cinfo.d.marker->read_markers;
- register int i;
-
- /* We're not assuming that this file's JPEG bit stream has any header
- "metadata", so fool the JPEG Library into thinking that we read a
- "Start of Input" (SOI) marker and a "Start of Frame" (SOFx) marker, then
- force it to read a simulated "Start of Scan" (SOS) marker when we call
- "jpeg_read_header()" below. This should cause the JPEG Library to
- establish reasonable defaults.
- */
- sp->cinfo.d.marker->saw_SOI = /* Pretend we saw SOI marker */
- sp->cinfo.d.marker->saw_SOF = TRUE; /* Pretend we saw SOF marker */
- sp->cinfo.d.marker->read_markers =
- sp->is_WANG ? suspend : fake_SOS_marker;
- sp->cinfo.d.global_state = DSTATE_INHEADER;
- sp->cinfo.d.Se = DCTSIZE2-1; /* Suppress JPEG Library warning */
- sp->cinfo.d.image_width = segment_width;
- sp->cinfo.d.image_height = td->td_imagelength;
-
- /* The following color-space initialization, including the complicated
- "switch"-statement below, essentially duplicates the logic used by the
- JPEG Library's "jpeg_init_colorspace()" subroutine during compression.
- */
- sp->cinfo.d.num_components = td->td_samplesperpixel;
- sp->cinfo.d.comp_info = (jpeg_component_info *)
- (*sp->cinfo.d.mem->alloc_small)
- ( &sp->cinfo.comm
- , JPOOL_IMAGE
- , sp->cinfo.d.num_components * sizeof *sp->cinfo.d.comp_info
- );
- i = 0;
- do
- {
- sp->cinfo.d.comp_info[i].component_index = i;
- sp->cinfo.d.comp_info[i].component_needed = TRUE;
- sp->cinfo.d.cur_comp_info[i] = &sp->cinfo.d.comp_info[i];
- }
- while (++i < sp->cinfo.d.num_components);
- switch (jpeg_color_space)
- {
- case JCS_UNKNOWN :
- i = 0;
- do
- {
- sp->cinfo.d.comp_info[i].component_id = i;
- sp->cinfo.d.comp_info[i].h_samp_factor =
- sp->cinfo.d.comp_info[i].v_samp_factor = 1;
- }
- while (++i < sp->cinfo.d.num_components);
- break;
- case JCS_GRAYSCALE:
- sp->cinfo.d.comp_info[0].component_id =
- sp->cinfo.d.comp_info[0].h_samp_factor =
- sp->cinfo.d.comp_info[0].v_samp_factor = 1;
- break;
- case JCS_RGB :
- sp->cinfo.d.comp_info[0].component_id = 'R';
- sp->cinfo.d.comp_info[1].component_id = 'G';
- sp->cinfo.d.comp_info[2].component_id = 'B';
- i = 0;
- do sp->cinfo.d.comp_info[i].h_samp_factor =
- sp->cinfo.d.comp_info[i].v_samp_factor = 1;
- while (++i < sp->cinfo.d.num_components);
- break;
- case JCS_CMYK :
- sp->cinfo.d.comp_info[0].component_id = 'C';
- sp->cinfo.d.comp_info[1].component_id = 'M';
- sp->cinfo.d.comp_info[2].component_id = 'Y';
- sp->cinfo.d.comp_info[3].component_id = 'K';
- i = 0;
- do sp->cinfo.d.comp_info[i].h_samp_factor =
- sp->cinfo.d.comp_info[i].v_samp_factor = 1;
- while (++i < sp->cinfo.d.num_components);
- break;
- case JCS_YCbCr :
- i = 0;
- do
- {
- sp->cinfo.d.comp_info[i].component_id = i+1;
- sp->cinfo.d.comp_info[i].h_samp_factor =
- sp->cinfo.d.comp_info[i].v_samp_factor = 1;
- sp->cinfo.d.comp_info[i].quant_tbl_no =
- sp->cinfo.d.comp_info[i].dc_tbl_no =
- sp->cinfo.d.comp_info[i].ac_tbl_no = i > 0;
- }
- while (++i < sp->cinfo.d.num_components);
- sp->cinfo.d.comp_info[0].h_samp_factor = sp->h_sampling;
- sp->cinfo.d.comp_info[0].v_samp_factor = sp->v_sampling;
- };
- sp->cinfo.d.comps_in_scan = td->td_planarconfig == PLANARCONFIG_CONTIG
- ? sp->cinfo.d.num_components
- : 1;
- i = CALLJPEG(sp,-1,jpeg_read_header(&sp->cinfo.d,!sp->is_WANG));
- sp->cinfo.d.marker->read_markers = save; /* Restore input method */
- if (sp->is_WANG) /* produced by Wang Imaging on Microsoft Windows */
- {
- if (i != JPEG_SUSPENDED) return 0;
-
- /* BOGOSITY ALERT! Files prooduced by the Wang Imaging application for
- Microsoft Windows are a special--and, technically
- illegal--case. A JPEG SOS marker and rest of the data stream should
- be located at the end of the file, in a position identified by the
- 0th Strip offset.
- */
- i = td->td_nstrips - 1;
- sp->src.next_input_byte = tif->tif_base + td->td_stripoffset[0];
- sp->src.bytes_in_buffer = td->td_stripoffset[i] -
- td->td_stripoffset[0] + td->td_stripbytecount[i];
- i = CALLJPEG(sp,-1,jpeg_read_header(&sp->cinfo.d,TRUE));
- };
- if (i != JPEG_HEADER_OK) return 0;
- };
-
- /* Some of our initialization must wait until the JPEG Library is initialized
- above, in order to override its defaults.
- */
- if ( (sp->cinfo.d.raw_data_out = downsampled_output)
- && !alloc_downsampled_buffers(tif,sp->cinfo.d.comp_info,
- sp->cinfo.d.num_components)
- ) return 0;
- sp->cinfo.d.jpeg_color_space = jpeg_color_space;
- sp->cinfo.d.out_color_space = out_color_space;
- sp->cinfo.d.dither_mode = JDITHER_NONE; /* Reduce image "noise" */
- sp->cinfo.d.two_pass_quantize = FALSE;
-
- /* If the image consists of separate, discontiguous TIFF "samples" (= color
- planes, hopefully = JPEG "scans"), then we must use the JPEG Library's
- "buffered image" mode to decompress the entire image into temporary buffers,
- because the JPEG Library must parse the entire JPEG bit-stream in order to
- be satsified that it has a complete set of color components for each pixel,
- but the TIFF Library must allow our client to extract 1 component at a time.
- Initializing the JPEG Library's "buffered image" mode is tricky: First, we
- start its decompressor, then we tell the decompressor to "consume" (i.e.,
- buffer) the entire bit-stream.
-
- WARNING: Disabling "fancy" up-sampling seems to slightly reduce "noise" for
- certain old Wang Imaging files, but it absolutely *must* be
- enabled if the image has separate color planes, since in that case, the JPEG
- Library doesn't use an "sp->cinfo.d.cconvert" structure (so de-referencing
- this pointer below will cause a fatal crash) but writing our own code to up-
- sample separate color planes is too much work for right now. Maybe someday?
- */
- sp->cinfo.d.do_fancy_upsampling = /* Always let this default (to TRUE)? */
- sp->cinfo.d.buffered_image = td->td_planarconfig == PLANARCONFIG_SEPARATE;
- if (!CALLJPEG(sp,0,jpeg_start_decompress(&sp->cinfo.d))) return 0;
- if (sp->cinfo.d.buffered_image) /* separate color planes */
- {
- if (sp->cinfo.d.raw_data_out)
- tif->tif_decoderow = tif->tif_decodestrip = tif->tif_decodetile =
- OJPEGDecodeRawSeparate;
- else
- {
- tif->tif_decoderow = tif->tif_decodestrip = tif->tif_decodetile =
- OJPEGDecode;
-
- /* In JPEG Library Version 6B, color-space conversion isn't implemented
- for separate color planes, so we must do it ourself if our TIFF
- client doesn't want to:
- */
- sp->cinfo.d.cconvert->color_convert =
- sp->cinfo.d.jpeg_color_space == sp->cinfo.d.out_color_space
- ? null_convert : ycc_rgb_convert;
- };
- L3: switch (CALLJPEG(sp,0,jpeg_consume_input(&sp->cinfo.d)))
- {
- default : goto L3;
-
- /* If no JPEG "End of Information" (EOI) marker is found when bit-
- stream parsing ends, check whether we have enough data to proceed
- before reporting an error.
- */
- case JPEG_SUSPENDED : if ( sp->cinfo.d.input_scan_number
- *sp->cinfo.d.image_height
- + sp->cinfo.d.input_iMCU_row
- *sp->cinfo.d.max_v_samp_factor
-# ifdef D_LOSSLESS_SUPPORTED
- *sp->cinfo.d.data_units_in_MCU
- *sp->cinfo.d.min_codec_data_unit
-# else
- *sp->cinfo.d.blocks_in_MCU
- *DCTSIZE
-# endif
- < td->td_samplesperpixel
- *sp->cinfo.d.image_height
- )
- {
- TIFFError(tif->tif_name,
- "Premature end of JPEG bit-stream");
- return 0;
- }
- case JPEG_REACHED_EOI: ;
- }
- }
- else /* pixel-interleaved color planes */
- tif->tif_decoderow = tif->tif_decodestrip = tif->tif_decodetile =
- downsampled_output ? OJPEGDecodeRawContig : OJPEGDecode;
- return 1;
-# undef td
- }
+static int
+OJPEGReadHeaderInfoSecStreamSos(TIFF* tif)
+{
+ /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
+ static const char module[]="OJPEGReadHeaderInfoSecStreamSos";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint16 m;
+ uint8 n;
+ uint8 o;
+ assert(sp->subsamplingcorrect==0);
+ if (sp->sof_log==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
+ return(0);
+ }
+ /* Ls */
+ if (OJPEGReadWord(sp,&m)==0)
+ return(0);
+ if (m!=6+sp->samples_per_pixel_per_plane*2)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
+ return(0);
+ }
+ /* Ns */
+ if (OJPEGReadByte(sp,&n)==0)
+ return(0);
+ if (n!=sp->samples_per_pixel_per_plane)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
+ return(0);
+ }
+ /* Cs, Td, and Ta */
+ for (o=0; o<sp->samples_per_pixel_per_plane; o++)
+ {
+ /* Cs */
+ if (OJPEGReadByte(sp,&n)==0)
+ return(0);
+ sp->sos_cs[sp->plane_sample_offset+o]=n;
+ /* Td and Ta */
+ if (OJPEGReadByte(sp,&n)==0)
+ return(0);
+ sp->sos_tda[sp->plane_sample_offset+o]=n;
+ }
+ /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as per LibJpeg source */
+ OJPEGReadSkip(sp,3);
+ return(1);
+}
static int
-OJPEGPreDecode(register TIFF *tif,tsample_t s)
- { register OJPEGState *sp = OJState(tif);
-# define td (&tif->tif_dir)
-
- /* If we are about to read the first row of an image plane (hopefully, these
- are coincident with JPEG "scans"!), reset the JPEG Library's decompressor
- appropriately. Otherwise, let the decompressor run "as is" and return a
- "success" status without further ado.
- */
- if ( (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip)
- % td->td_stripsperimage
- == 0
- )
- {
- if ( sp->cinfo.d.buffered_image
- && !CALLJPEG(sp,0,jpeg_start_output(&sp->cinfo.d,s+1))
- ) return 0;
- sp->cinfo.d.output_scanline = 0;
-
- /* Mark subsampling buffers "empty". */
-
-# ifdef D_LOSSLESS_SUPPORTED
- sp->scancount = sp->cinfo.d.min_codec_data_unit;
-# else
- sp->scancount = DCTSIZE;
-# endif
- };
- return 1;
-# undef td
- }
-
-/*ARGSUSED*/ static void
-OJPEGPostDecode(register TIFF *tif,tidata_t buf,tsize_t cc)
- { register OJPEGState *sp = OJState(tif);
-# define td (&tif->tif_dir)
-
- /* The JPEG Library decompressor has reached the end of a strip/tile. If this
- is the end of a TIFF image "sample" (= JPEG "scan") in a file with separate
- components (color planes), then end the "scan". If it ends the image's last
- sample/scan, then also stop the JPEG Library's decompressor.
- */
- if (sp->cinfo.d.output_scanline >= sp->cinfo.d.output_height)
- {
- if (sp->cinfo.d.buffered_image)
- CALLJPEG(sp,-1,jpeg_finish_output(&sp->cinfo.d)); /* End JPEG scan */
- if ( (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip)
- >= td->td_nstrips-1
- ) CALLJPEG(sp,0,jpeg_finish_decompress(&sp->cinfo.d));
- }
-# undef td
- }
+OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif)
+{
+ static const char module[]="OJPEGReadHeaderInfoSecTablesQTable";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ uint8 n;
+ uint32 oa;
+ uint8* ob;
+ uint32 p;
+ if (sp->qtable_offset[0]==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
+ return(0);
+ }
+ sp->in_buffer_file_pos_log=0;
+ for (m=0; m<sp->samples_per_pixel; m++)
+ {
+ if ((sp->qtable_offset[m]!=0) && ((m==0) || (sp->qtable_offset[m]!=sp->qtable_offset[m-1])))
+ {
+ for (n=0; n<m-1; n++)
+ {
+ if (sp->qtable_offset[m]==sp->qtable_offset[n])
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegQTables tag value");
+ return(0);
+ }
+ }
+ oa=sizeof(uint32)+69;
+ ob=_TIFFmalloc(oa);
+ if (ob==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ *(uint32*)ob=oa;
+ ob[sizeof(uint32)]=255;
+ ob[sizeof(uint32)+1]=JPEG_MARKER_DQT;
+ ob[sizeof(uint32)+2]=0;
+ ob[sizeof(uint32)+3]=67;
+ ob[sizeof(uint32)+4]=m;
+ TIFFSeekFile(tif,sp->qtable_offset[m],SEEK_SET);
+ p=TIFFReadFile(tif,&ob[sizeof(uint32)+5],64);
+ if (p!=64)
+ return(0);
+ sp->qtable[m]=ob;
+ sp->sof_tq[m]=m;
+ }
+ else
+ sp->sof_tq[m]=sp->sof_tq[m-1];
+ }
+ return(1);
+}
static int
-OJPEGVSetField(register TIFF *tif,ttag_t tag,va_list ap)
+OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif)
{
- uint32 v32;
- register OJPEGState *sp = OJState(tif);
-# define td (&tif->tif_dir)
- toff_t tiffoff=0;
- uint32 bufoff=0;
- uint32 code_count=0;
- int i2=0;
- int k2=0;
-
- switch (tag)
- {
- default : return
- (*sp->vsetparent)(tif,tag,ap);
-
- /* BEWARE OF KLUDGE: Some old-format JPEG-in-TIFF files, including those
- produced by the Wang Imaging application for Micro-
- soft Windows, illegally omit a "ReferenceBlackWhite" TIFF tag, even
- though the TIFF specification's default is intended for the RGB color
- space and is inappropriate for the YCbCr color space ordinarily used for
- JPEG images. Since many TIFF client applications request the value of
- this tag immediately after a TIFF image directory is parsed, and before
- any other code in this module receives control, we are forced to fix
- this problem very early in image-file processing. Fortunately, legal
- TIFF files are supposed to store their tags in numeric order, so a
- mandatory "PhotometricInterpretation" tag should always appear before
- an optional "ReferenceBlackWhite" tag. Hence, we slyly peek ahead when
- we discover the desired photometry, by installing modified black and
- white reference levels.
- */
- case TIFFTAG_PHOTOMETRIC :
- if ( (v32 = (*sp->vsetparent)(tif,tag,ap))
- && td->td_photometric == PHOTOMETRIC_YCBCR
- )
- {
- 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);
+ static const char module[]="OJPEGReadHeaderInfoSecTablesDcTable";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ uint8 n;
+ uint8 o[16];
+ uint32 p;
+ uint32 q;
+ uint32 ra;
+ uint8* rb;
+ if (sp->dctable_offset[0]==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
+ return(0);
+ }
+ sp->in_buffer_file_pos_log=0;
+ for (m=0; m<sp->samples_per_pixel; m++)
+ {
+ if ((sp->dctable_offset[m]!=0) && ((m==0) || (sp->dctable_offset[m]!=sp->dctable_offset[m-1])))
+ {
+ for (n=0; n<m-1; n++)
+ {
+ if (sp->dctable_offset[m]==sp->dctable_offset[n])
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegDcTables tag value");
+ return(0);
+ }
+ }
+ TIFFSeekFile(tif,sp->dctable_offset[m],SEEK_SET);
+ p=TIFFReadFile(tif,o,16);
+ if (p!=16)
+ return(0);
+ q=0;
+ for (n=0; n<16; n++)
+ q+=o[n];
+ ra=sizeof(uint32)+21+q;
+ rb=_TIFFmalloc(ra);
+ if (rb==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ *(uint32*)rb=ra;
+ rb[sizeof(uint32)]=255;
+ rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
+ rb[sizeof(uint32)+2]=((19+q)>>8);
+ rb[sizeof(uint32)+3]=((19+q)&255);
+ rb[sizeof(uint32)+4]=m;
+ for (n=0; n<16; n++)
+ rb[sizeof(uint32)+5+n]=o[n];
+ p=TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
+ if (p!=q)
+ return(0);
+ sp->dctable[m]=rb;
+ sp->sos_tda[m]=(m<<4);
}
- }
- return v32;
-
- /* BEWARE OF KLUDGE: According to Charles Auer <Bumble731@msn.com>, if our
- input is a multi-image (multi-directory) JPEG-in-TIFF
- file is produced by the Wang Imaging application on Microsoft Windows,
- for some reason the first directory excludes the vendor-specific "WANG
- PageControl" tag (32934) that we check below, so the only other way to
- identify these directories is apparently to look for a software-
- identification tag with the substring, "Wang Labs". Single-image files
- can apparently pass both tests, which causes no harm here, but what a
- mess this is!
- */
- case TIFFTAG_SOFTWARE :
- {
- char *software;
-
- v32 = (*sp->vsetparent)(tif,tag,ap);
- if( TIFFGetField( tif, TIFFTAG_SOFTWARE, &software )
- && strstr( software, "Wang Labs" ) )
- sp->is_WANG = 1;
- return v32;
- }
-
- case TIFFTAG_JPEGPROC :
- case TIFFTAG_JPEGIFOFFSET :
- case TIFFTAG_JPEGIFBYTECOUNT :
- case TIFFTAG_JPEGRESTARTINTERVAL :
- case TIFFTAG_JPEGLOSSLESSPREDICTORS:
- case TIFFTAG_JPEGPOINTTRANSFORM :
- case TIFFTAG_JPEGQTABLES :
- case TIFFTAG_JPEGDCTABLES :
- case TIFFTAG_JPEGACTABLES :
- case TIFFTAG_WANG_PAGECONTROL :
- case TIFFTAG_JPEGCOLORMODE : ;
- };
- v32 = va_arg(ap,uint32); /* No. of values in this TIFF record */
-
- /* This switch statement is added for OJPEGVSetField */
- if(v32 !=0){
- switch(tag){
- case TIFFTAG_JPEGPROC:
- sp->jpegproc=v32;
- break;
- case TIFFTAG_JPEGIFOFFSET:
- sp->jpegifoffset=v32;
- break;
- case TIFFTAG_JPEGIFBYTECOUNT:
- sp->jpegifbytecount=v32;
- break;
- case TIFFTAG_JPEGRESTARTINTERVAL:
- sp->jpegrestartinterval=v32;
- break;
- case TIFFTAG_JPEGLOSSLESSPREDICTORS:
- sp->jpeglosslesspredictors_length=v32;
- break;
- case TIFFTAG_JPEGPOINTTRANSFORM:
- sp->jpegpointtransform_length=v32;
- break;
- case TIFFTAG_JPEGQTABLES:
- sp->jpegqtables_length=v32;
- break;
- case TIFFTAG_JPEGACTABLES:
- sp->jpegactables_length=v32;
- break;
- case TIFFTAG_JPEGDCTABLES:
- sp->jpegdctables_length=v32;
- break;
- default:
- break;
- }
- }
-
- /* BEWARE: The following actions apply only if we are reading a "source" TIFF
- image to be decompressed for a client application program. If we
- ever enhance this file's CODEC to write "destination" JPEG-in-TIFF images,
- we'll need an "if"- and another "switch"-statement below, because we'll
- probably want to store these records' values in some different places. Most
- of these need not be parsed here in order to decode JPEG bit stream, so we
- set boolean flags to note that they have been seen, but we otherwise ignore
- them.
- */
- switch (tag)
- { JHUFF_TBL **h;
-
- /* Validate the JPEG-process code. */
-
- case TIFFTAG_JPEGPROC :
- switch (v32)
- {
- default : TIFFError(tif->tif_name,
- "Unknown JPEG process");
- return 0;
-# ifdef C_LOSSLESS_SUPPORTED
-
- /* Image uses (lossy) baseline sequential coding. */
-
- case JPEGPROC_BASELINE: sp->cinfo.d.process = JPROC_SEQUENTIAL;
- sp->cinfo.d.data_unit = DCTSIZE;
- break;
-
- /* Image uses (lossless) Huffman coding. */
-
- case JPEGPROC_LOSSLESS: sp->cinfo.d.process = JPROC_LOSSLESS;
- sp->cinfo.d.data_unit = 1;
-# else /* not C_LOSSLESS_SUPPORTED */
- case JPEGPROC_LOSSLESS: TIFFError(JPEGLib_name,
- "Does not support lossless Huffman coding");
- return 0;
- case JPEGPROC_BASELINE: ;
-# endif /* C_LOSSLESS_SUPPORTED */
- };
- break;
-
- /* The TIFF Version 6.0 specification says that if the value of a TIFF
- "JPEGInterchangeFormat" record is 0, then we are to behave as if this
- record were absent; i.e., the data does *not* represent a JPEG Inter-
- change Format File (JFIF), so don't even set the boolean "I've been
- here" flag below. Otherwise, the field's value represents the file
- offset of the JPEG SOI marker.
- */
- case TIFFTAG_JPEGIFOFFSET :
- if (v32)
- {
- sp->src.next_input_byte = tif->tif_base + v32;
- break;
- };
- return 1;
- case TIFFTAG_JPEGIFBYTECOUNT :
- sp->src.bytes_in_buffer = v32;
- break;
-
- /* The TIFF Version 6.0 specification says that if the JPEG "Restart"
- marker interval is 0, then the data has no "Restart" markers; i.e., we
- must behave as if this TIFF record were absent. So, don't even set the
- boolean "I've been here" flag below.
- */
- /*
- * Instead, set the field bit so TIFFGetField can get whether or not
- * it was set.
- */
- case TIFFTAG_JPEGRESTARTINTERVAL :
- if (v32)
- sp->cinfo.d.restart_interval = v32;
- break;
- /* The TIFF Version 6.0 specification says that this tag is supposed to be
- a vector containing a value for each image component, but for lossless
- Huffman coding (the only JPEG process defined by the specification for
- which this tag should be needed), ISO IS 10918-1 uses only a single
- value, equivalent to the "Ss" field in a JPEG bit-stream's "Start of
- Scan" (SOS) marker. So, we extract the first vector element and ignore
- the rest. (I hope this is correct!)
- */
- case TIFFTAG_JPEGLOSSLESSPREDICTORS:
- if (v32)
- {
- sp->cinfo.d.Ss = *va_arg(ap,uint16 *);
- sp->jpeglosslesspredictors =
- _TIFFmalloc(sp->jpeglosslesspredictors_length
- * sizeof(uint16));
- if(sp->jpeglosslesspredictors==NULL){return(0);}
- for(i2=0;i2<sp->jpeglosslesspredictors_length;i2++){
- ((uint16*)sp->jpeglosslesspredictors)[i2] =
- ((uint16*)sp->cinfo.d.Ss)[i2];
- }
- sp->jpeglosslesspredictors_length*=sizeof(uint16);
- break;
- };
- return v32;
-
- /* The TIFF Version 6.0 specification says that this tag is supposed to be
- a vector containing a value for each image component, but for lossless
- Huffman coding (the only JPEG process defined by the specification for
- which this tag should be needed), ISO IS 10918-1 uses only a single
- value, equivalent to the "Al" field in a JPEG bit-stream's "Start of
- Scan" (SOS) marker. So, we extract the first vector element and ignore
- the rest. (I hope this is correct!)
- */
- case TIFFTAG_JPEGPOINTTRANSFORM :
- if (v32)
- {
- sp->cinfo.d.Al = *va_arg(ap,uint16 *);
- sp->jpegpointtransform =
- _TIFFmalloc(sp->jpegpointtransform_length*sizeof(uint16));
- if(sp->jpegpointtransform==NULL){return(0);}
- for(i2=0;i2<sp->jpegpointtransform_length;i2++) {
- ((uint16*)sp->jpegpointtransform)[i2] =
- ((uint16*)sp->cinfo.d.Al)[i2];
- }
- sp->jpegpointtransform_length*=sizeof(uint16);
- break;
- }
- return v32;
-
- /* We have a vector of offsets to quantization tables, so load 'em! */
-
- case TIFFTAG_JPEGQTABLES :
- if (v32)
- { uint32 *v;
- int i;
- if (v32 > NUM_QUANT_TBLS)
- {
- TIFFError(tif->tif_name,"Too many quantization tables");
- return 0;
- };
- i = 0;
- v = va_arg(ap,uint32 *);
- sp->jpegqtables=_TIFFmalloc(64*sp->jpegqtables_length);
- if(sp->jpegqtables==NULL){return(0);}
- tiffoff = TIFFSeekFile(tif, 0, SEEK_CUR);
- bufoff=0;
- for(i2=0;i2<sp->jpegqtables_length;i2++){
- TIFFSeekFile(tif, v[i2], SEEK_SET);
- TIFFReadFile(tif, &(((unsigned char*)(sp->jpegqtables))[bufoff]),
- 64);
- bufoff+=64;
- }
- sp->jpegqtables_length=bufoff;
- TIFFSeekFile(tif, tiffoff, SEEK_SET);
-
- do /* read quantization table */
- { register UINT8 *from = tif->tif_base + *v++;
- register UINT16 *to;
- register int j = DCTSIZE2;
-
- if (!( sp->cinfo.d.quant_tbl_ptrs[i]
- = CALLJPEG(sp,0,jpeg_alloc_quant_table(&sp->cinfo.comm))
- )
- )
- {
- TIFFError(JPEGLib_name,"No space for quantization table");
- return 0;
- };
- to = sp->cinfo.d.quant_tbl_ptrs[i]->quantval;
- do *to++ = *from++; while (--j > 0);
- }
- while (++i < v32);
- sp->jpegtablesmode |= JPEGTABLESMODE_QUANT;
- };
- break;
-
- /* We have a vector of offsets to DC Huffman tables, so load 'em! */
-
- case TIFFTAG_JPEGDCTABLES :
- h = sp->cinfo.d.dc_huff_tbl_ptrs;
- goto L;
-
- /* We have a vector of offsets to AC Huffman tables, so load 'em! */
-
- case TIFFTAG_JPEGACTABLES :
- h = sp->cinfo.d.ac_huff_tbl_ptrs;
- L: if (v32)
- { uint32 *v;
- int i;
- if (v32 > NUM_HUFF_TBLS)
- {
- TIFFError(tif->tif_name,"Too many Huffman tables");
- return 0;
- };
- v = va_arg(ap,uint32 *);
- if(tag == TIFFTAG_JPEGDCTABLES) {
- sp->jpegdctables=_TIFFmalloc(272*sp->jpegdctables_length);
- if(sp->jpegdctables==NULL){return(0);}
- tiffoff = TIFFSeekFile(tif, 0, SEEK_CUR);
- bufoff=0;
- code_count=0;
- for(i2=0;i2<sp->jpegdctables_length;i2++){
- TIFFSeekFile(tif, v[i2], SEEK_SET);
- TIFFReadFile(tif,
- &(((unsigned char*)(sp->jpegdctables))[bufoff]),
- 16);
- code_count=0;
- for(k2=0;k2<16;k2++){
- code_count+=((unsigned char*)(sp->jpegdctables))[k2+bufoff];
- }
- TIFFReadFile(tif,
- &(((unsigned char*)(sp->jpegdctables))[bufoff+16]),
- code_count);
- bufoff+=16;
- bufoff+=code_count;
- }
- sp->jpegdctables_length=bufoff;
- TIFFSeekFile(tif, tiffoff, SEEK_SET);
- }
- if(tag==TIFFTAG_JPEGACTABLES){
- sp->jpegactables=_TIFFmalloc(272*sp->jpegactables_length);
- if(sp->jpegactables==NULL){return(0);}
- tiffoff = TIFFSeekFile(tif, 0, SEEK_CUR);
- bufoff=0;
- code_count=0;
- for(i2=0;i2<sp->jpegactables_length;i2++){
- TIFFSeekFile(tif, v[i2], SEEK_SET);
- TIFFReadFile(tif, &(((unsigned char*)(sp->jpegactables))[bufoff]), 16);
- code_count=0;
- for(k2=0;k2<16;k2++){
- code_count+=((unsigned char*)(sp->jpegactables))[k2+bufoff];
- }
- TIFFReadFile(tif, &(((unsigned char*)(sp->jpegactables))[bufoff+16]), code_count);
- bufoff+=16;
- bufoff+=code_count;
- }
- sp->jpegactables_length=bufoff;
- TIFFSeekFile(tif, tiffoff, SEEK_SET);
- }
- i = 0;
- do /* copy each Huffman table */
- { int size = 0;
- register UINT8 *from = tif->tif_base + *v++, *to;
- register int j = sizeof (*h)->bits;
-
- /* WARNING: This code relies on the fact that an image file not
- "memory mapped" was read entirely into a single
- buffer by "TIFFInitOJPEG()", so we can do a fast memory-to-
- memory copy here. Each table consists of 16 Bytes, which are
- suffixed to a 0 Byte when copied, followed by a variable
- number of Bytes whose length is the sum of the first 16.
- */
- if (!( *h
- = CALLJPEG(sp,0,jpeg_alloc_huff_table(&sp->cinfo.comm))
- )
- )
- {
- TIFFError(JPEGLib_name,"No space for Huffman table");
- return 0;
- };
- to = (*h++)->bits;
- *to++ = 0;
- while (--j > 0) size += *to++ = *from++; /* Copy 16 Bytes */
- if (size > sizeof (*h)->huffval/sizeof *(*h)->huffval)
- {
- TIFFError(tif->tif_name,"Huffman table too big");
- return 0;
- };
- if ((j = size) > 0) do *to++ = *from++; while (--j > 0);
- while (++size <= sizeof (*h)->huffval/sizeof *(*h)->huffval)
- *to++ = 0; /* Zero the rest of the table for cleanliness */
- }
- while (++i < v32);
- sp->jpegtablesmode |= JPEGTABLESMODE_HUFF;
- };
- break;
-
- /* The following vendor-specific TIFF tag occurs in (highly illegal) files
- produced by the Wang Imaging application for Microsoft Windows. These
- can apparently have several "pages", in which case this tag specifies
- the offset of a "page control" structure, which we don't currently know
- how to handle. 0 indicates a 1-page image with no "page control", which
- we make a feeble effort to handle.
- */
- case TIFFTAG_WANG_PAGECONTROL :
- if (v32 == 0) v32 = -1;
- sp->is_WANG = v32;
- tag = TIFFTAG_JPEGPROC+FIELD_WANG_PAGECONTROL-FIELD_JPEGPROC;
- break;
-
- /* This pseudo tag indicates whether our caller is expected to do YCbCr <->
- RGB color-space conversion (JPEGCOLORMODE_RAW <=> 0) or whether we must
- ask the JPEG Library to do it (JPEGCOLORMODE_RGB <=> 1).
- */
- case TIFFTAG_JPEGCOLORMODE :
- sp->jpegcolormode = v32;
-
- /* Mark the image to indicate whether returned data is up-sampled, so
- that "TIFF{Strip,Tile}Size()" reflect the true amount of data present.
- */
- v32 = tif->tif_flags; /* Save flags temporarily */
- tif->tif_flags &= ~TIFF_UPSAMPLED;
- if ( td->td_photometric == PHOTOMETRIC_YCBCR
- && (td->td_ycbcrsubsampling[0]<<3 | td->td_ycbcrsubsampling[1])
- != 011
- && sp->jpegcolormode == JPEGCOLORMODE_RGB
- ) tif->tif_flags |= TIFF_UPSAMPLED;
-
- /* If the up-sampling state changed, re-calculate tile size. */
-
- if ((tif->tif_flags ^ v32) & TIFF_UPSAMPLED)
- {
- tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tsize_t) -1;
- tif->tif_flags |= TIFF_DIRTYDIRECT;
- };
- return 1;
- };
- TIFFSetFieldBit(tif,tag-TIFFTAG_JPEGPROC+FIELD_JPEGPROC);
- return 1;
-# undef td
- }
+ else
+ sp->sos_tda[m]=sp->sos_tda[m-1];
+ }
+ return(1);
+}
static int
-OJPEGVGetField(register TIFF *tif,ttag_t tag,va_list ap)
- { register OJPEGState *sp = OJState(tif);
-
- switch (tag)
- {
-
- /* If this file has managed to synthesize a set of consolidated "metadata"
- tables for the current (post-TIFF Version 6.0 specification) JPEG-in-
- TIFF encapsulation strategy, then tell our caller about them; otherwise,
- keep mum.
- */
- case TIFFTAG_JPEGTABLES :
- if (sp->jpegtables_length) /* we have "new"-style JPEG tables */
- {
- *va_arg(ap,uint32 *) = sp->jpegtables_length;
- *va_arg(ap,char **) = sp->jpegtables;
- return 1;
- };
-
- /* This pseudo tag indicates whether our caller is expected to do YCbCr <->
- RGB color-space conversion (JPEGCOLORMODE_RAW <=> 0) or whether we must
- ask the JPEG Library to do it (JPEGCOLORMODE_RGB <=> 1).
- */
- case TIFFTAG_JPEGCOLORMODE :
- *va_arg(ap,uint32 *) = sp->jpegcolormode;
- return 1;
-
- /* The following tags are defined by the TIFF Version 6.0 specification
- and are obsolete. If our caller asks for information about them, do not
- return anything, even if we parsed them in an old-format "source" image.
- */
- case TIFFTAG_JPEGPROC :
- *va_arg(ap, uint16*)=sp->jpegproc;
- return(1);
- break;
- case TIFFTAG_JPEGIFOFFSET :
- *va_arg(ap, uint32*)=sp->jpegifoffset;
- return(1);
- break;
- case TIFFTAG_JPEGIFBYTECOUNT :
- *va_arg(ap, uint32*)=sp->jpegifbytecount;
- return(1);
- break;
- case TIFFTAG_JPEGRESTARTINTERVAL :
- *va_arg(ap, uint32*)=sp->jpegrestartinterval;
- return(1);
- break;
- case TIFFTAG_JPEGLOSSLESSPREDICTORS:
- *va_arg(ap, uint32*)=sp->jpeglosslesspredictors_length;
- *va_arg(ap, void**)=sp->jpeglosslesspredictors;
- return(1);
- break;
- case TIFFTAG_JPEGPOINTTRANSFORM :
- *va_arg(ap, uint32*)=sp->jpegpointtransform_length;
- *va_arg(ap, void**)=sp->jpegpointtransform;
- return(1);
- break;
- case TIFFTAG_JPEGQTABLES :
- *va_arg(ap, uint32*)=sp->jpegqtables_length;
- *va_arg(ap, void**)=sp->jpegqtables;
- return(1);
- break;
- case TIFFTAG_JPEGDCTABLES :
- *va_arg(ap, uint32*)=sp->jpegdctables_length;
- *va_arg(ap, void**)=sp->jpegdctables;
- return(1);
- break;
- case TIFFTAG_JPEGACTABLES :
- *va_arg(ap, uint32*)=sp->jpegactables_length;
- *va_arg(ap, void**)=sp->jpegactables;
- return(1);
- break;
- };
- return (*sp->vgetparent)(tif,tag,ap);
- }
+OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif)
+{
+ static const char module[]="OJPEGReadHeaderInfoSecTablesAcTable";
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ uint8 n;
+ uint8 o[16];
+ uint32 p;
+ uint32 q;
+ uint32 ra;
+ uint8* rb;
+ if (sp->actable_offset[0]==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
+ return(0);
+ }
+ sp->in_buffer_file_pos_log=0;
+ for (m=0; m<sp->samples_per_pixel; m++)
+ {
+ if ((sp->actable_offset[m]!=0) && ((m==0) || (sp->actable_offset[m]!=sp->actable_offset[m-1])))
+ {
+ for (n=0; n<m-1; n++)
+ {
+ if (sp->actable_offset[m]==sp->actable_offset[n])
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegAcTables tag value");
+ return(0);
+ }
+ }
+ TIFFSeekFile(tif,sp->actable_offset[m],SEEK_SET);
+ p=TIFFReadFile(tif,o,16);
+ if (p!=16)
+ return(0);
+ q=0;
+ for (n=0; n<16; n++)
+ q+=o[n];
+ ra=sizeof(uint32)+21+q;
+ rb=_TIFFmalloc(ra);
+ if (rb==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+ return(0);
+ }
+ *(uint32*)rb=ra;
+ rb[sizeof(uint32)]=255;
+ rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
+ rb[sizeof(uint32)+2]=((19+q)>>8);
+ rb[sizeof(uint32)+3]=((19+q)&255);
+ rb[sizeof(uint32)+4]=(16|m);
+ for (n=0; n<16; n++)
+ rb[sizeof(uint32)+5+n]=o[n];
+ p=TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
+ if (p!=q)
+ return(0);
+ sp->actable[m]=rb;
+ sp->sos_tda[m]=(sp->sos_tda[m]|m);
+ }
+ else
+ sp->sos_tda[m]=(sp->sos_tda[m]|(sp->sos_tda[m-1]&15));
+ }
+ return(1);
+}
+
+static int
+OJPEGReadBufferFill(OJPEGState* sp)
+{
+ uint16 m;
+ tsize_t n;
+ /* TODO: double-check: when subsamplingcorrect is set, no call to TIFFErrorExt or TIFFWarningExt should be made
+ * in any other case, seek or read errors should be passed through */
+ do
+ {
+ if (sp->in_buffer_file_togo!=0)
+ {
+ if (sp->in_buffer_file_pos_log==0)
+ {
+ TIFFSeekFile(sp->tif,sp->in_buffer_file_pos,SEEK_SET);
+ sp->in_buffer_file_pos_log=1;
+ }
+ m=OJPEG_BUFFER;
+ if (m>sp->in_buffer_file_togo)
+ m=(uint16)sp->in_buffer_file_togo;
+ n=TIFFReadFile(sp->tif,sp->in_buffer,(tsize_t)m);
+ if (n==0)
+ return(0);
+ assert(n>0);
+ assert(n<=OJPEG_BUFFER);
+ assert(n<65536);
+ assert((uint16)n<=sp->in_buffer_file_togo);
+ m=(uint16)n;
+ sp->in_buffer_togo=m;
+ sp->in_buffer_cur=sp->in_buffer;
+ sp->in_buffer_file_togo-=m;
+ sp->in_buffer_file_pos+=m;
+ break;
+ }
+ sp->in_buffer_file_pos_log=0;
+ switch(sp->in_buffer_source)
+ {
+ case osibsNotSetYet:
+ if (sp->jpeg_interchange_format!=0)
+ {
+ sp->in_buffer_file_pos=sp->jpeg_interchange_format;
+ sp->in_buffer_file_togo=sp->jpeg_interchange_format_length;
+ }
+ sp->in_buffer_source=osibsJpegInterchangeFormat;
+ break;
+ case osibsJpegInterchangeFormat:
+ sp->in_buffer_source=osibsStrile;
+ case osibsStrile:
+ if (sp->in_buffer_next_strile==sp->in_buffer_strile_count)
+ sp->in_buffer_source=osibsEof;
+ else
+ {
+ sp->in_buffer_file_pos=sp->tif->tif_dir.td_stripoffset[sp->in_buffer_next_strile];
+ if (sp->in_buffer_file_pos!=0)
+ {
+ if (sp->in_buffer_file_pos>=sp->file_size)
+ sp->in_buffer_file_pos=0;
+ else
+ {
+ sp->in_buffer_file_togo=sp->tif->tif_dir.td_stripbytecount[sp->in_buffer_next_strile];
+ if (sp->in_buffer_file_togo==0)
+ sp->in_buffer_file_pos=0;
+ else if (sp->in_buffer_file_pos+sp->in_buffer_file_togo>sp->file_size)
+ sp->in_buffer_file_togo=sp->file_size-sp->in_buffer_file_pos;
+ }
+ }
+ sp->in_buffer_next_strile++;
+ }
+ break;
+ default:
+ return(0);
+ }
+ } while (1);
+ return(1);
+}
+
+static int
+OJPEGReadByte(OJPEGState* sp, uint8* byte)
+{
+ if (sp->in_buffer_togo==0)
+ {
+ if (OJPEGReadBufferFill(sp)==0)
+ return(0);
+ assert(sp->in_buffer_togo>0);
+ }
+ *byte=*(sp->in_buffer_cur);
+ sp->in_buffer_cur++;
+ sp->in_buffer_togo--;
+ return(1);
+}
+
+static int
+OJPEGReadBytePeek(OJPEGState* sp, uint8* byte)
+{
+ if (sp->in_buffer_togo==0)
+ {
+ if (OJPEGReadBufferFill(sp)==0)
+ return(0);
+ assert(sp->in_buffer_togo>0);
+ }
+ *byte=*(sp->in_buffer_cur);
+ return(1);
+}
static void
-OJPEGPrintDir(register TIFF *tif,FILE *fd,long flags)
- { register OJPEGState *sp = OJState(tif);
-
- if ( ( flags
- & (TIFFPRINT_JPEGQTABLES|TIFFPRINT_JPEGDCTABLES|TIFFPRINT_JPEGACTABLES)
- )
- && sp->jpegtables_length
- )
- fprintf(fd," JPEG Table Data: <present>, %lu bytes\n",
- sp->jpegtables_length);
- }
-
-static uint32
-OJPEGDefaultStripSize(register TIFF *tif,register uint32 s)
- { register OJPEGState *sp = OJState(tif);
-# define td (&tif->tif_dir)
-
- if ((s = (*sp->defsparent)(tif,s)) < td->td_imagelength)
- { register tsize_t size = sp->cinfo.comm.is_decompressor
-# ifdef D_LOSSLESS_SUPPORTED
- ? sp->cinfo.d.min_codec_data_unit
-# else
- ? DCTSIZE
-# endif
-# ifdef C_LOSSLESS_SUPPORTED
- : sp->cinfo.c.data_unit;
-# else
- : DCTSIZE;
-# endif
-
- size = TIFFroundup(size,16);
- s = TIFFroundup(s,td->td_ycbcrsubsampling[1]*size);
- };
- return s;
-# undef td
- }
+OJPEGReadByteAdvance(OJPEGState* sp)
+{
+ assert(sp->in_buffer_togo>0);
+ sp->in_buffer_cur++;
+ sp->in_buffer_togo--;
+}
+
+static int
+OJPEGReadWord(OJPEGState* sp, uint16* word)
+{
+ uint8 m;
+ if (OJPEGReadByte(sp,&m)==0)
+ return(0);
+ *word=(m<<8);
+ if (OJPEGReadByte(sp,&m)==0)
+ return(0);
+ *word|=m;
+ return(1);
+}
+
+static int
+OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem)
+{
+ uint16 mlen;
+ uint8* mmem;
+ uint16 n;
+ assert(len>0);
+ mlen=len;
+ mmem=mem;
+ do
+ {
+ if (sp->in_buffer_togo==0)
+ {
+ if (OJPEGReadBufferFill(sp)==0)
+ return(0);
+ assert(sp->in_buffer_togo>0);
+ }
+ n=mlen;
+ if (n>sp->in_buffer_togo)
+ n=sp->in_buffer_togo;
+ _TIFFmemcpy(mmem,sp->in_buffer_cur,n);
+ sp->in_buffer_cur+=n;
+ sp->in_buffer_togo-=n;
+ mlen-=n;
+ mmem+=n;
+ } while(mlen>0);
+ return(1);
+}
static void
-OJPEGDefaultTileSize(register TIFF *tif,register uint32 *tw,register uint32 *th)
- { register OJPEGState *sp = OJState(tif);
- register tsize_t size;
-# define td (&tif->tif_dir)
-
- size = sp->cinfo.comm.is_decompressor
-# ifdef D_LOSSLESS_SUPPORTED
- ? sp->cinfo.d.min_codec_data_unit
-# else
- ? DCTSIZE
-# endif
-# ifdef C_LOSSLESS_SUPPORTED
- : sp->cinfo.c.data_unit;
-# else
- : DCTSIZE;
-# endif
- size = TIFFroundup(size,16);
- (*sp->deftparent)(tif,tw,th);
- *tw = TIFFroundup(*tw,td->td_ycbcrsubsampling[0]*size);
- *th = TIFFroundup(*th,td->td_ycbcrsubsampling[1]*size);
-# undef td
- }
+OJPEGReadSkip(OJPEGState* sp, uint16 len)
+{
+ uint16 m;
+ uint16 n;
+ m=len;
+ n=m;
+ if (n>sp->in_buffer_togo)
+ n=sp->in_buffer_togo;
+ sp->in_buffer_cur+=n;
+ sp->in_buffer_togo-=n;
+ m-=n;
+ if (m>0)
+ {
+ assert(sp->in_buffer_togo==0);
+ n=m;
+ if (n>sp->in_buffer_file_togo)
+ n=sp->in_buffer_file_togo;
+ sp->in_buffer_file_pos+=n;
+ sp->in_buffer_file_togo-=n;
+ sp->in_buffer_file_pos_log=0;
+ /* we don't skip past jpeginterchangeformat/strile block...
+ * if that is asked from us, we're dealing with totally bazurk
+ * data anyway, and we've not seen this happening on any
+ * testfile, so we might as well likely cause some other
+ * meaningless error to be passed at some later time
+ */
+ }
+}
+
+static int
+OJPEGWriteStream(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ *len=0;
+ do
+ {
+ assert(sp->out_state<=ososEoi);
+ switch(sp->out_state)
+ {
+ case ososSoi:
+ OJPEGWriteStreamSoi(tif,mem,len);
+ break;
+ case ososQTable0:
+ OJPEGWriteStreamQTable(tif,0,mem,len);
+ break;
+ case ososQTable1:
+ OJPEGWriteStreamQTable(tif,1,mem,len);
+ break;
+ case ososQTable2:
+ OJPEGWriteStreamQTable(tif,2,mem,len);
+ break;
+ case ososQTable3:
+ OJPEGWriteStreamQTable(tif,3,mem,len);
+ break;
+ case ososDcTable0:
+ OJPEGWriteStreamDcTable(tif,0,mem,len);
+ break;
+ case ososDcTable1:
+ OJPEGWriteStreamDcTable(tif,1,mem,len);
+ break;
+ case ososDcTable2:
+ OJPEGWriteStreamDcTable(tif,2,mem,len);
+ break;
+ case ososDcTable3:
+ OJPEGWriteStreamDcTable(tif,3,mem,len);
+ break;
+ case ososAcTable0:
+ OJPEGWriteStreamAcTable(tif,0,mem,len);
+ break;
+ case ososAcTable1:
+ OJPEGWriteStreamAcTable(tif,1,mem,len);
+ break;
+ case ososAcTable2:
+ OJPEGWriteStreamAcTable(tif,2,mem,len);
+ break;
+ case ososAcTable3:
+ OJPEGWriteStreamAcTable(tif,3,mem,len);
+ break;
+ case ososDri:
+ OJPEGWriteStreamDri(tif,mem,len);
+ break;
+ case ososSof:
+ OJPEGWriteStreamSof(tif,mem,len);
+ break;
+ case ososSos:
+ OJPEGWriteStreamSos(tif,mem,len);
+ break;
+ case ososCompressed:
+ if (OJPEGWriteStreamCompressed(tif,mem,len)==0)
+ return(0);
+ break;
+ case ososRst:
+ OJPEGWriteStreamRst(tif,mem,len);
+ break;
+ case ososEoi:
+ OJPEGWriteStreamEoi(tif,mem,len);
+ break;
+ }
+ } while (*len==0);
+ return(1);
+}
static void
-OJPEGCleanUp(register TIFF *tif)
- { register OJPEGState *sp;
-
- if ( (sp = OJState(tif)) )
- {
- CALLVJPEG(sp,jpeg_destroy(&sp->cinfo.comm)); /* Free JPEG Lib. vars. */
- if (sp->jpegtables) {_TIFFfree(sp->jpegtables);sp->jpegtables=0;}
- if (sp->jpeglosslesspredictors) {
- _TIFFfree(sp->jpeglosslesspredictors);
- sp->jpeglosslesspredictors = 0;
- }
- if (sp->jpegpointtransform) {
- _TIFFfree(sp->jpegpointtransform);
- sp->jpegpointtransform=0;
- }
- if (sp->jpegqtables) {_TIFFfree(sp->jpegqtables);sp->jpegqtables=0;}
- if (sp->jpegactables) {_TIFFfree(sp->jpegactables);sp->jpegactables=0;}
- if (sp->jpegdctables) {_TIFFfree(sp->jpegdctables);sp->jpegdctables=0;}
- /* If the image file isn't "memory mapped" and we read it all into a
- single, large memory buffer, free the buffer now.
- */
- if (!isMapped(tif) && tif->tif_base) /* free whole-file buffer */
- {
- _TIFFfree(tif->tif_base);
- tif->tif_base = 0;
- tif->tif_size = 0;
- };
- _TIFFfree(sp); /* Release local variables */
- tif->tif_data = 0;
- }
- }
+OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(OJPEG_BUFFER>=2);
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=JPEG_MARKER_SOI;
+ *len=2;
+ *mem=(void*)sp->out_buffer;
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ if (sp->qtable[table_index]!=0)
+ {
+ *mem=(void*)(sp->qtable[table_index]+sizeof(uint32));
+ *len=*((uint32*)sp->qtable[table_index])-sizeof(uint32);
+ }
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ if (sp->dctable[table_index]!=0)
+ {
+ *mem=(void*)(sp->dctable[table_index]+sizeof(uint32));
+ *len=*((uint32*)sp->dctable[table_index])-sizeof(uint32);
+ }
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ if (sp->actable[table_index]!=0)
+ {
+ *mem=(void*)(sp->actable[table_index]+sizeof(uint32));
+ *len=*((uint32*)sp->actable[table_index])-sizeof(uint32);
+ }
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(OJPEG_BUFFER>=6);
+ if (sp->restart_interval!=0)
+ {
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=JPEG_MARKER_DRI;
+ sp->out_buffer[2]=0;
+ sp->out_buffer[3]=4;
+ sp->out_buffer[4]=(sp->restart_interval>>8);
+ sp->out_buffer[5]=(sp->restart_interval&255);
+ *len=6;
+ *mem=(void*)sp->out_buffer;
+ }
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ assert(OJPEG_BUFFER>=2+8+sp->samples_per_pixel_per_plane*3);
+ assert(255>=8+sp->samples_per_pixel_per_plane*3);
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=sp->sof_marker_id;
+ /* Lf */
+ sp->out_buffer[2]=0;
+ sp->out_buffer[3]=8+sp->samples_per_pixel_per_plane*3;
+ /* P */
+ sp->out_buffer[4]=8;
+ /* Y */
+ sp->out_buffer[5]=(sp->sof_y>>8);
+ sp->out_buffer[6]=(sp->sof_y&255);
+ /* X */
+ sp->out_buffer[7]=(sp->sof_x>>8);
+ sp->out_buffer[8]=(sp->sof_x&255);
+ /* Nf */
+ sp->out_buffer[9]=sp->samples_per_pixel_per_plane;
+ for (m=0; m<sp->samples_per_pixel_per_plane; m++)
+ {
+ /* C */
+ sp->out_buffer[10+m*3]=sp->sof_c[sp->plane_sample_offset+m];
+ /* H and V */
+ sp->out_buffer[10+m*3+1]=sp->sof_hv[sp->plane_sample_offset+m];
+ /* Tq */
+ sp->out_buffer[10+m*3+2]=sp->sof_tq[sp->plane_sample_offset+m];
+ }
+ *len=10+sp->samples_per_pixel_per_plane*3;
+ *mem=(void*)sp->out_buffer;
+ sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ uint8 m;
+ assert(OJPEG_BUFFER>=2+6+sp->samples_per_pixel_per_plane*2);
+ assert(255>=6+sp->samples_per_pixel_per_plane*2);
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=JPEG_MARKER_SOS;
+ /* Ls */
+ sp->out_buffer[2]=0;
+ sp->out_buffer[3]=6+sp->samples_per_pixel_per_plane*2;
+ /* Ns */
+ sp->out_buffer[4]=sp->samples_per_pixel_per_plane;
+ for (m=0; m<sp->samples_per_pixel_per_plane; m++)
+ {
+ /* Cs */
+ sp->out_buffer[5+m*2]=sp->sos_cs[sp->plane_sample_offset+m];
+ /* Td and Ta */
+ sp->out_buffer[5+m*2+1]=sp->sos_tda[sp->plane_sample_offset+m];
+ }
+ /* Ss */
+ sp->out_buffer[5+sp->samples_per_pixel_per_plane*2]=0;
+ /* Se */
+ sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+1]=63;
+ /* Ah and Al */
+ sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+2]=0;
+ *len=8+sp->samples_per_pixel_per_plane*2;
+ *mem=(void*)sp->out_buffer;
+ sp->out_state++;
+}
+
+static int
+OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ if (sp->in_buffer_togo==0)
+ {
+ if (OJPEGReadBufferFill(sp)==0)
+ return(0);
+ assert(sp->in_buffer_togo>0);
+ }
+ *len=sp->in_buffer_togo;
+ *mem=(void*)sp->in_buffer_cur;
+ sp->in_buffer_togo=0;
+ if (sp->in_buffer_file_togo==0)
+ {
+ switch(sp->in_buffer_source)
+ {
+ case osibsStrile:
+ if (sp->in_buffer_next_strile<sp->in_buffer_strile_count)
+ sp->out_state=ososRst;
+ else
+ sp->out_state=ososEoi;
+ break;
+ case osibsEof:
+ sp->out_state=ososEoi;
+ break;
+ default:
+ break;
+ }
+ }
+ return(1);
+}
+
+static void
+OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(OJPEG_BUFFER>=2);
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=JPEG_MARKER_RST0+sp->restart_index;
+ sp->restart_index++;
+ if (sp->restart_index==8)
+ sp->restart_index=0;
+ *len=2;
+ *mem=(void*)sp->out_buffer;
+ sp->out_state=ososCompressed;
+}
+
+static void
+OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ assert(OJPEG_BUFFER>=2);
+ sp->out_buffer[0]=255;
+ sp->out_buffer[1]=JPEG_MARKER_EOI;
+ *len=2;
+ *mem=(void*)sp->out_buffer;
+}
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
+{
+ return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_create_decompress(cinfo),1));
+}
+#endif
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image)
+{
+ return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_read_header(cinfo,require_image),1));
+}
+#endif
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
+{
+ return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_start_decompress(cinfo),1));
+}
+#endif
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines)
+{
+ return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_read_scanlines(cinfo,scanlines,max_lines),1));
+}
+#endif
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines)
+{
+ return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_read_raw_data(cinfo,data,max_lines),1));
+}
+#endif
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static void
+jpeg_encap_unwind(TIFF* tif)
+{
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ LONGJMP(sp->exit_jmpbuf,1);
+}
+#endif
+
+static void
+OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo)
+{
+ char buffer[JMSG_LENGTH_MAX];
+ (*cinfo->err->format_message)(cinfo,buffer);
+ TIFFWarningExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg",buffer);
+}
+
+static void
+OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo)
+{
+ char buffer[JMSG_LENGTH_MAX];
+ (*cinfo->err->format_message)(cinfo,buffer);
+ TIFFErrorExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg",buffer);
+ jpeg_encap_unwind((TIFF*)(cinfo->client_data));
+}
+
+static void
+OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo)
+{
+ (void)cinfo;
+}
+
+static boolean
+OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo)
+{
+ TIFF* tif=(TIFF*)cinfo->client_data;
+ OJPEGState* sp=(OJPEGState*)tif->tif_data;
+ void* mem=0;
+ uint32 len=0;
+ if (OJPEGWriteStream(tif,&mem,&len)==0)
+ {
+ TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Premature end of JPEG data");
+ jpeg_encap_unwind(tif);
+ }
+ sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=len;
+ sp->libjpeg_jpeg_source_mgr.next_input_byte=mem;
+ return(1);
+}
+
+static void
+OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes)
+{
+ TIFF* tif=(TIFF*)cinfo->client_data;
+ (void)num_bytes;
+ TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
+ jpeg_encap_unwind(tif);
+}
+
+static boolean
+OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired)
+{
+ TIFF* tif=(TIFF*)cinfo->client_data;
+ (void)desired;
+ TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
+ jpeg_encap_unwind(tif);
+ return(0);
+}
+
+static void
+OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo)
+{
+ (void)cinfo;
+}
+
+#endif
-int
-TIFFInitOJPEG(register TIFF *tif,int scheme)
- { register OJPEGState *sp;
-# define td (&tif->tif_dir)
-# ifndef never
-
- /* This module supports a decompression-only CODEC, which is intended strictly
- for viewing old image files using the obsolete JPEG-in-TIFF encapsulation
- specified by the TIFF Version 6.0 specification. It does not, and never
- should, support compression for new images. If a client application asks us
- to, refuse and complain loudly!
- */
- if (tif->tif_mode != O_RDONLY) return _notSupported(tif);
-# endif /* never */
- if (!isMapped(tif))
- {
-
- /* BEWARE OF KLUDGE: If our host operating-system doesn't let an image
- file be "memory mapped", then we want to read the
- entire file into a single (possibly large) memory buffer as if it had
- been "memory mapped". Although this is likely to waste space, because
- analysis of the file's content might cause parts of it to be read into
- smaller buffers duplicatively, it appears to be the lesser of several
- evils. Very old JPEG-in-TIFF encapsulations aren't guaranteed to be
- JFIF bit streams, or to have a TIFF "JPEGTables" record or much other
- "metadata" to help us locate the decoding tables and entropy-coded data,
- so we're likely do a lot of random-access grokking around, and we must
- ultimately tell the JPEG Library to sequentially scan much of the file
- anyway. This is all likely to be easier if we use "brute force" to
- read the entire file, once, and don't use incremental disc I/O. If our
- client application tries to process a file so big that we can't buffer
- it entirely, then tough shit: we'll give up and exit!
- */
- if (!(tif->tif_base = _TIFFmalloc(tif->tif_size=TIFFGetFileSize(tif))))
- {
- TIFFError(tif->tif_name,"Cannot allocate file buffer");
- return 0;
- };
- if (!SeekOK(tif,0) || !ReadOK(tif,tif->tif_base,tif->tif_size))
- {
- TIFFError(tif->tif_name,"Cannot read file");
- return 0;
- }
- };
-
- /* Allocate storage for this module's per-file variables. */
-
- if (!(tif->tif_data = (tidata_t)_TIFFmalloc(sizeof *sp)))
- {
- TIFFError("TIFFInitOJPEG","No space for JPEG state block");
- return 0;
- };
- (sp = OJState(tif))->tif = tif; /* Initialize reverse pointer */
- sp->cinfo.d.err = jpeg_std_error(&sp->err); /* Initialize error handling */
- sp->err.error_exit = TIFFojpeg_error_exit;
- sp->err.output_message = TIFFojpeg_output_message;
- if (!CALLVJPEG(sp,jpeg_create_decompress(&sp->cinfo.d))) return 0;
-
- /* Install CODEC-specific tag information and override default TIFF Library
- "method" subroutines with our own, CODEC-specific methods. Like all good
- members of an object-class, we save some of these subroutine pointers for
- "fall back" in case our own methods fail.
- */
- _TIFFMergeFieldInfo(tif,ojpegFieldInfo,
- sizeof ojpegFieldInfo/sizeof *ojpegFieldInfo);
- sp->defsparent = tif->tif_defstripsize;
- sp->deftparent = tif->tif_deftilesize;
- sp->vgetparent = tif->tif_tagmethods.vgetfield;
- sp->vsetparent = tif->tif_tagmethods.vsetfield;
- tif->tif_defstripsize = OJPEGDefaultStripSize;
- tif->tif_deftilesize = OJPEGDefaultTileSize;
- tif->tif_tagmethods.vgetfield = OJPEGVGetField;
- tif->tif_tagmethods.vsetfield = OJPEGVSetField;
- tif->tif_tagmethods.printdir = OJPEGPrintDir;
-# ifdef never
- tif->tif_setupencode = OJPEGSetupEncode;
- tif->tif_preencode = OJPEGPreEncode;
- tif->tif_postencode = OJPEGPostEncode;
-# else /* well, hardly ever */
- tif->tif_setupencode = tif->tif_postencode = _notSupported;
- tif->tif_preencode = (TIFFPreMethod)_notSupported;
-# endif /* never */
- tif->tif_setupdecode = OJPEGSetupDecode;
- tif->tif_predecode = OJPEGPreDecode;
- tif->tif_postdecode = OJPEGPostDecode;
- tif->tif_cleanup = OJPEGCleanUp;
-
- /* If the image file doesn't have "JPEGInterchangeFormat[Length]" TIFF records
- to guide us, we have few clues about where its encapsulated JPEG bit stream
- is located, so establish intelligent defaults: If the Image File Directory
- doesn't immediately follow the TIFF header, assume that the JPEG data lies
- in between; otherwise, assume that it follows the Image File Directory.
- */
- if (tif->tif_header.tiff_diroff > sizeof tif->tif_header)
- {
- sp->src.next_input_byte = tif->tif_base + sizeof tif->tif_header;
- sp->src.bytes_in_buffer = tif->tif_header.tiff_diroff
- - sizeof tif->tif_header;
- }
- else /* this case is ugly! */
- { uint32 maxoffset = tif->tif_size;
- uint16 dircount;
-
- /* Calculate the offset to the next Image File Directory, if there is one,
- or to the end of the file, if not. Then arrange to read the file from
- the end of the Image File Directory to that offset.
- */
- if (tif->tif_nextdiroff) maxoffset = tif->tif_nextdiroff; /* Not EOF */
- _TIFFmemcpy(&dircount,(const tdata_t)
- (sp->src.next_input_byte = tif->tif_base+tif->tif_header.tiff_diroff),
- sizeof dircount);
- if (tif->tif_flags & TIFF_SWAB) TIFFSwabShort(&dircount);
- sp->src.next_input_byte += dircount*sizeof(TIFFDirEntry)
- + sizeof maxoffset + sizeof dircount;
- sp->src.bytes_in_buffer = tif->tif_base - sp->src.next_input_byte
- + maxoffset;
- };
-
- /* IJG JPEG Library Version 6B can be configured for either 8- or 12-bit sample
- precision, but we assume that "old JPEG" TIFF clients only need 8 bits.
- */
- sp->cinfo.d.data_precision = 8;
-# ifdef C_LOSSLESS_SUPPORTED
- /* If the "JPEGProc" TIFF tag is missing from the Image File Dictionary, the
- JPEG Library will use its (lossy) baseline sequential process by default.
- */
- sp->cinfo.d.data_unit = DCTSIZE;
-# endif /* C_LOSSLESS_SUPPORTED */
-
- /* Initialize other CODEC-specific variables requiring default values. */
-
- tif->tif_flags |= TIFF_NOBITREV; /* No bit-reversal within data bytes */
- sp->h_sampling = sp->v_sampling = 1; /* No subsampling by default */
- sp->is_WANG = 0; /* Assume not a MS Windows Wang Imaging file by default */
- sp->jpegtables = 0; /* No "new"-style JPEG tables synthesized yet */
- sp->jpegtables_length = 0;
- sp->jpegquality = 75; /* Default IJG quality */
- sp->jpegcolormode = JPEGCOLORMODE_RAW;
- sp->jpegtablesmode = 0; /* No tables found yet */
- sp->jpeglosslesspredictors=0;
- sp->jpeglosslesspredictors_length=0;
- sp->jpegpointtransform=0;
- sp->jpegpointtransform_length=0;
- sp->jpegqtables=0;
- sp->jpegqtables_length=0;
- sp->jpegdctables=0;
- sp->jpegdctables_length=0;
- sp->jpegactables=0;
- sp->jpegactables_length=0;
- return 1;
-# undef td
- }
-#endif /* OJPEG_SUPPORT */
-
-/* vim: set ts=8 sts=8 sw=8 noet: */
diff --git a/src/libtiff/tif_open.c b/src/libtiff/tif_open.c
index 98b30fa..9020973 100644
--- a/src/libtiff/tif_open.c
+++ b/src/libtiff/tif_open.c
@@ -1,4 +1,4 @@
-/* $Id: tif_open.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_open.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -198,7 +198,7 @@ TIFFClientOpen(
*/
tif->tif_flags = FILLORDER_MSB2LSB;
if (m == O_RDONLY )
- tif->tif_flags |= TIFF_MAPPED;
+ tif->tif_flags |= TIFF_MAPPED;
#ifdef STRIPCHOP_DEFAULT
if (m == O_RDONLY || m == O_RDWR)
@@ -307,7 +307,8 @@ TIFFClientOpen(
if (tif->tif_mode & O_TRUNC ||
!ReadOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
if (tif->tif_mode == O_RDONLY) {
- TIFFErrorExt(tif->tif_clientdata, name, "Cannot read TIFF header");
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Cannot read TIFF header");
goto bad;
}
/*
@@ -336,7 +337,8 @@ TIFFClientOpen(
TIFFSeekFile( tif, 0, SEEK_SET );
if (!WriteOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
- TIFFErrorExt(tif->tif_clientdata, name, "Error writing TIFF header");
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Error writing TIFF header");
goto bad;
}
/*
@@ -350,6 +352,7 @@ TIFFClientOpen(
goto bad;
tif->tif_diroff = 0;
tif->tif_dirlist = NULL;
+ tif->tif_dirlistsize = 0;
tif->tif_dirnumber = 0;
return (tif);
}
@@ -366,10 +369,12 @@ TIFFClientOpen(
tif->tif_header.tiff_magic != MDI_LITTLEENDIAN
#endif
) {
- TIFFErrorExt(tif->tif_clientdata, name, "Not a TIFF or MDI file, bad magic number %d (0x%x)",
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Not a TIFF or MDI file, bad magic number %d (0x%x)",
#else
) {
- TIFFErrorExt(tif->tif_clientdata, name, "Not a TIFF file, bad magic number %d (0x%x)",
+ TIFFErrorExt(tif->tif_clientdata, name,
+ "Not a TIFF file, bad magic number %d (0x%x)",
#endif
tif->tif_header.tiff_magic,
tif->tif_header.tiff_magic);
@@ -398,7 +403,7 @@ TIFFClientOpen(
TIFFErrorExt(tif->tif_clientdata, name,
"Not a TIFF file, bad version number %d (0x%x)",
tif->tif_header.tiff_version,
- tif->tif_header.tiff_version);
+ tif->tif_header.tiff_version);
goto bad;
}
tif->tif_flags |= TIFF_MYBUFFER;
diff --git a/src/libtiff/tif_packbits.c b/src/libtiff/tif_packbits.c
index 644d0e5..8c40b60 100644
--- a/src/libtiff/tif_packbits.c
+++ b/src/libtiff/tif_packbits.c
@@ -1,4 +1,4 @@
-/* $Id: tif_packbits.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_packbits.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -240,7 +240,7 @@ PackBitsDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
if( occ < n )
{
TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
- "PackBitsDecode: discarding %d bytes "
+ "PackBitsDecode: discarding %ld bytes "
"to avoid buffer overrun",
n - occ);
n = occ;
@@ -253,7 +253,7 @@ PackBitsDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
if (occ < n + 1)
{
TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
- "PackBitsDecode: discarding %d bytes "
+ "PackBitsDecode: discarding %ld bytes "
"to avoid buffer overrun",
n - occ + 1);
n = occ - 1;
diff --git a/src/libtiff/tif_pixarlog.c b/src/libtiff/tif_pixarlog.c
index b7acbaa..b05b6da 100644
--- a/src/libtiff/tif_pixarlog.c
+++ b/src/libtiff/tif_pixarlog.c
@@ -1,4 +1,4 @@
-/* $Id: tif_pixarlog.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_pixarlog.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1996-1997 Sam Leffler
@@ -327,7 +327,7 @@ horizontalAccumulate11(uint16 *wp, int n, int stride, uint16 *op)
while (n > 0) {
REPEAT(stride,
wp[stride] += *wp; *op = *wp&mask; wp++; op++)
- n -= stride;
+ n -= stride;
}
}
}
@@ -593,7 +593,6 @@ PixarLogMakeTables(PixarLogState *sp)
static int PixarLogEncode(TIFF*, tidata_t, tsize_t, tsample_t);
static int PixarLogDecode(TIFF*, tidata_t, tsize_t, tsample_t);
-#define N(a) (sizeof(a)/sizeof(a[0]))
#define PIXARLOGDATAFMT_UNKNOWN -1
static int
@@ -768,6 +767,18 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
if (tif->tif_flags & TIFF_SWAB)
TIFFSwabArrayOfShort(up, nsamples);
+ /*
+ * if llen is not an exact multiple of nsamples, the decode operation
+ * may overflow the output buffer, so truncate it enough to prevent
+ * that but still salvage as much data as possible.
+ */
+ if (nsamples % llen) {
+ TIFFWarningExt(tif->tif_clientdata, module,
+ "%s: stride %d is not a multiple of sample count, "
+ "%d, data truncated.", tif->tif_name, llen, nsamples);
+ nsamples -= nsamples % llen;
+ }
+
for (i = 0; i < nsamples; i += llen, up += llen) {
switch (sp->user_datafmt) {
case PIXARLOGDATAFMT_FLOAT:
@@ -1036,7 +1047,7 @@ PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
TIFFDirectory *td = &tif->tif_dir;
PixarLogState *sp = EncoderState(tif);
static const char module[] = "PixarLogEncode";
- int i, n, llen;
+ int i, n, llen;
unsigned short * up;
(void) s;
@@ -1278,11 +1289,23 @@ static const TIFFFieldInfo pixarlogFieldInfo[] = {
int
TIFFInitPixarLog(TIFF* tif, int scheme)
{
+ static const char module[] = "TIFFInitPixarLog";
+
PixarLogState* sp;
assert(scheme == COMPRESSION_PIXARLOG);
/*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFieldInfo(tif, pixarlogFieldInfo,
+ TIFFArrayCount(pixarlogFieldInfo))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging PixarLog codec-specific tags failed");
+ return 0;
+ }
+
+ /*
* Allocate state block so tag methods have storage to record values.
*/
tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (PixarLogState));
@@ -1311,7 +1334,6 @@ TIFFInitPixarLog(TIFF* tif, int scheme)
tif->tif_cleanup = PixarLogCleanup;
/* Override SetField so we can handle our private pseudo-tag */
- _TIFFMergeFieldInfo(tif, pixarlogFieldInfo, N(pixarlogFieldInfo));
sp->vgetparent = tif->tif_tagmethods.vgetfield;
tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */
sp->vsetparent = tif->tif_tagmethods.vsetfield;
@@ -1333,7 +1355,7 @@ TIFFInitPixarLog(TIFF* tif, int scheme)
return (1);
bad:
- TIFFErrorExt(tif->tif_clientdata, "TIFFInitPixarLog",
+ TIFFErrorExt(tif->tif_clientdata, module,
"No space for PixarLog state block");
return (0);
}
diff --git a/src/libtiff/tif_predict.c b/src/libtiff/tif_predict.c
index e738ca3..ca5c091 100644
--- a/src/libtiff/tif_predict.c
+++ b/src/libtiff/tif_predict.c
@@ -1,4 +1,4 @@
-/* $Id: tif_predict.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_predict.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -36,9 +36,12 @@
static void horAcc8(TIFF*, tidata_t, tsize_t);
static void horAcc16(TIFF*, tidata_t, tsize_t);
+static void horAcc32(TIFF*, tidata_t, tsize_t);
static void swabHorAcc16(TIFF*, tidata_t, tsize_t);
+static void swabHorAcc32(TIFF*, tidata_t, tsize_t);
static void horDiff8(TIFF*, tidata_t, tsize_t);
static void horDiff16(TIFF*, tidata_t, tsize_t);
+static void horDiff32(TIFF*, tidata_t, tsize_t);
static void fpAcc(TIFF*, tidata_t, tsize_t);
static void fpDiff(TIFF*, tidata_t, tsize_t);
static int PredictorDecodeRow(TIFF*, tidata_t, tsize_t, tsample_t);
@@ -60,7 +63,8 @@ PredictorSetup(TIFF* tif)
return 1;
case PREDICTOR_HORIZONTAL:
if (td->td_bitspersample != 8
- && td->td_bitspersample != 16) {
+ && td->td_bitspersample != 16
+ && td->td_bitspersample != 32) {
TIFFErrorExt(tif->tif_clientdata, module,
"Horizontal differencing \"Predictor\" not supported with %d-bit samples",
td->td_bitspersample);
@@ -105,19 +109,23 @@ PredictorSetupDecode(TIFF* tif)
if (sp->predictor == 2) {
switch (td->td_bitspersample) {
- case 8: sp->pfunc = horAcc8; break;
- case 16: sp->pfunc = horAcc16; break;
+ case 8: sp->decodepfunc = horAcc8; break;
+ case 16: sp->decodepfunc = horAcc16; break;
+ case 32: sp->decodepfunc = horAcc32; break;
}
/*
* Override default decoding method with one that does the
* predictor stuff.
*/
- sp->coderow = tif->tif_decoderow;
- tif->tif_decoderow = PredictorDecodeRow;
- sp->codestrip = tif->tif_decodestrip;
- tif->tif_decodestrip = PredictorDecodeTile;
- sp->codetile = tif->tif_decodetile;
- tif->tif_decodetile = PredictorDecodeTile;
+ if( tif->tif_decoderow != PredictorDecodeRow )
+ {
+ sp->decoderow = tif->tif_decoderow;
+ tif->tif_decoderow = PredictorDecodeRow;
+ sp->decodestrip = tif->tif_decodestrip;
+ tif->tif_decodestrip = PredictorDecodeTile;
+ sp->decodetile = tif->tif_decodetile;
+ tif->tif_decodetile = PredictorDecodeTile;
+ }
/*
* If the data is horizontally differenced 16-bit data that
* requires byte-swapping, then it must be byte swapped before
@@ -126,25 +134,31 @@ PredictorSetupDecode(TIFF* tif)
* the library setup when the directory was read.
*/
if (tif->tif_flags & TIFF_SWAB) {
- if (sp->pfunc == horAcc16) {
- sp->pfunc = swabHorAcc16;
+ if (sp->decodepfunc == horAcc16) {
+ sp->decodepfunc = swabHorAcc16;
tif->tif_postdecode = _TIFFNoPostDecode;
- } /* else handle 32-bit case... */
+ } else if (sp->decodepfunc == horAcc32) {
+ sp->decodepfunc = swabHorAcc32;
+ tif->tif_postdecode = _TIFFNoPostDecode;
+ }
}
}
else if (sp->predictor == 3) {
- sp->pfunc = fpAcc;
+ sp->decodepfunc = fpAcc;
/*
* Override default decoding method with one that does the
* predictor stuff.
*/
- sp->coderow = tif->tif_decoderow;
- tif->tif_decoderow = PredictorDecodeRow;
- sp->codestrip = tif->tif_decodestrip;
- tif->tif_decodestrip = PredictorDecodeTile;
- sp->codetile = tif->tif_decodetile;
- tif->tif_decodetile = PredictorDecodeTile;
+ if( tif->tif_decoderow != PredictorDecodeRow )
+ {
+ sp->decoderow = tif->tif_decoderow;
+ tif->tif_decoderow = PredictorDecodeRow;
+ sp->decodestrip = tif->tif_decodestrip;
+ tif->tif_decodestrip = PredictorDecodeTile;
+ sp->decodetile = tif->tif_decodetile;
+ tif->tif_decodetile = PredictorDecodeTile;
+ }
/*
* The data should not be swapped outside of the floating
* point predictor, the accumulation routine should return
@@ -173,33 +187,40 @@ PredictorSetupEncode(TIFF* tif)
if (sp->predictor == 2) {
switch (td->td_bitspersample) {
- case 8: sp->pfunc = horDiff8; break;
- case 16: sp->pfunc = horDiff16; break;
+ case 8: sp->encodepfunc = horDiff8; break;
+ case 16: sp->encodepfunc = horDiff16; break;
+ case 32: sp->encodepfunc = horDiff32; break;
}
/*
* Override default encoding method with one that does the
* predictor stuff.
*/
- sp->coderow = tif->tif_encoderow;
- tif->tif_encoderow = PredictorEncodeRow;
- sp->codestrip = tif->tif_encodestrip;
- tif->tif_encodestrip = PredictorEncodeTile;
- sp->codetile = tif->tif_encodetile;
- tif->tif_encodetile = PredictorEncodeTile;
+ if( tif->tif_encoderow != PredictorEncodeRow )
+ {
+ sp->encoderow = tif->tif_encoderow;
+ tif->tif_encoderow = PredictorEncodeRow;
+ sp->encodestrip = tif->tif_encodestrip;
+ tif->tif_encodestrip = PredictorEncodeTile;
+ sp->encodetile = tif->tif_encodetile;
+ tif->tif_encodetile = PredictorEncodeTile;
+ }
}
else if (sp->predictor == 3) {
- sp->pfunc = fpDiff;
+ sp->encodepfunc = fpDiff;
/*
* Override default encoding method with one that does the
* predictor stuff.
*/
- sp->coderow = tif->tif_encoderow;
- tif->tif_encoderow = PredictorEncodeRow;
- sp->codestrip = tif->tif_encodestrip;
- tif->tif_encodestrip = PredictorEncodeTile;
- sp->codetile = tif->tif_encodetile;
- tif->tif_encodetile = PredictorEncodeTile;
+ if( tif->tif_encoderow != PredictorEncodeRow )
+ {
+ sp->encoderow = tif->tif_encoderow;
+ tif->tif_encoderow = PredictorEncodeRow;
+ sp->encodestrip = tif->tif_encodestrip;
+ tif->tif_encodestrip = PredictorEncodeTile;
+ sp->encodetile = tif->tif_encodetile;
+ tif->tif_encodetile = PredictorEncodeTile;
+ }
}
return 1;
@@ -291,6 +312,39 @@ horAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
}
}
+static void
+swabHorAcc32(TIFF* tif, tidata_t cp0, tsize_t cc)
+{
+ tsize_t stride = PredictorState(tif)->stride;
+ uint32* wp = (uint32*) cp0;
+ tsize_t wc = cc / 4;
+
+ if (wc > stride) {
+ TIFFSwabArrayOfLong(wp, wc);
+ wc -= stride;
+ do {
+ REPEAT4(stride, wp[stride] += wp[0]; wp++)
+ wc -= stride;
+ } while ((int32) wc > 0);
+ }
+}
+
+static void
+horAcc32(TIFF* tif, tidata_t cp0, tsize_t cc)
+{
+ tsize_t stride = PredictorState(tif)->stride;
+ uint32* wp = (uint32*) cp0;
+ tsize_t wc = cc / 4;
+
+ if (wc > stride) {
+ wc -= stride;
+ do {
+ REPEAT4(stride, wp[stride] += wp[0]; wp++)
+ wc -= stride;
+ } while ((int32) wc > 0);
+ }
+}
+
/*
* Floating point predictor accumulation routine.
*/
@@ -337,11 +391,11 @@ PredictorDecodeRow(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
TIFFPredictorState *sp = PredictorState(tif);
assert(sp != NULL);
- assert(sp->coderow != NULL);
- assert(sp->pfunc != NULL);
+ assert(sp->decoderow != NULL);
+ assert(sp->decodepfunc != NULL);
- if ((*sp->coderow)(tif, op0, occ0, s)) {
- (*sp->pfunc)(tif, op0, occ0);
+ if ((*sp->decoderow)(tif, op0, occ0, s)) {
+ (*sp->decodepfunc)(tif, op0, occ0);
return 1;
} else
return 0;
@@ -360,14 +414,14 @@ PredictorDecodeTile(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
TIFFPredictorState *sp = PredictorState(tif);
assert(sp != NULL);
- assert(sp->codetile != NULL);
+ assert(sp->decodetile != NULL);
- if ((*sp->codetile)(tif, op0, occ0, s)) {
+ if ((*sp->decodetile)(tif, op0, occ0, s)) {
tsize_t rowsize = sp->rowsize;
assert(rowsize > 0);
- assert(sp->pfunc != NULL);
+ assert(sp->decodepfunc != NULL);
while ((long)occ0 > 0) {
- (*sp->pfunc)(tif, op0, (tsize_t) rowsize);
+ (*sp->decodepfunc)(tif, op0, (tsize_t) rowsize);
occ0 -= rowsize;
op0 += rowsize;
}
@@ -439,6 +493,24 @@ horDiff16(TIFF* tif, tidata_t cp0, tsize_t cc)
}
}
+static void
+horDiff32(TIFF* tif, tidata_t cp0, tsize_t cc)
+{
+ TIFFPredictorState* sp = PredictorState(tif);
+ tsize_t stride = sp->stride;
+ int32 *wp = (int32*) cp0;
+ tsize_t wc = cc/4;
+
+ if (wc > stride) {
+ wc -= stride;
+ wp += wc - 1;
+ do {
+ REPEAT4(stride, wp[stride] -= wp[0]; wp--)
+ wc -= stride;
+ } while ((int32) wc > 0);
+ }
+}
+
/*
* Floating point predictor differencing routine.
*/
@@ -481,33 +553,56 @@ PredictorEncodeRow(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
TIFFPredictorState *sp = PredictorState(tif);
assert(sp != NULL);
- assert(sp->pfunc != NULL);
- assert(sp->coderow != NULL);
+ assert(sp->encodepfunc != NULL);
+ assert(sp->encoderow != NULL);
/* XXX horizontal differencing alters user's data XXX */
- (*sp->pfunc)(tif, bp, cc);
- return (*sp->coderow)(tif, bp, cc, s);
+ (*sp->encodepfunc)(tif, bp, cc);
+ return (*sp->encoderow)(tif, bp, cc, s);
}
static int
PredictorEncodeTile(TIFF* tif, tidata_t bp0, tsize_t cc0, tsample_t s)
{
+ static const char module[] = "PredictorEncodeTile";
TIFFPredictorState *sp = PredictorState(tif);
+ uint8 *working_copy;
tsize_t cc = cc0, rowsize;
- unsigned char* bp = bp0;
+ unsigned char* bp;
+ int result_code;
assert(sp != NULL);
- assert(sp->pfunc != NULL);
- assert(sp->codetile != NULL);
+ assert(sp->encodepfunc != NULL);
+ assert(sp->encodetile != NULL);
+
+ /*
+ * Do predictor manipulation in a working buffer to avoid altering
+ * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
+ */
+ working_copy = (uint8*) _TIFFmalloc(cc0);
+ if( working_copy == NULL )
+ {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Out of memory allocating %d byte temp buffer.",
+ cc0 );
+ return 0;
+ }
+ memcpy( working_copy, bp0, cc0 );
+ bp = working_copy;
rowsize = sp->rowsize;
assert(rowsize > 0);
- while ((long)cc > 0) {
- (*sp->pfunc)(tif, bp, (tsize_t) rowsize);
+ assert((cc0%rowsize)==0);
+ while (cc > 0) {
+ (*sp->encodepfunc)(tif, bp, rowsize);
cc -= rowsize;
bp += rowsize;
}
- return (*sp->codetile)(tif, bp0, cc0, s);
+ result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
+
+ _TIFFfree( working_copy );
+
+ return result_code;
}
#define FIELD_PREDICTOR (FIELD_CODEC+0) /* XXX */
@@ -516,7 +611,6 @@ static const TIFFFieldInfo predictFieldInfo[] = {
{ TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, FIELD_PREDICTOR,
FALSE, FALSE, "Predictor" },
};
-#define N(a) (sizeof (a) / sizeof (a[0]))
static int
PredictorVSetField(TIFF* tif, ttag_t tag, va_list ap)
@@ -583,10 +677,18 @@ TIFFPredictorInit(TIFF* tif)
assert(sp != 0);
/*
- * Merge codec-specific tag information and
- * override parent get/set field methods.
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFieldInfo(tif, predictFieldInfo,
+ TIFFArrayCount(predictFieldInfo))) {
+ TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit",
+ "Merging Predictor codec-specific tags failed");
+ return 0;
+ }
+
+ /*
+ * Override parent get/set field methods.
*/
- _TIFFMergeFieldInfo(tif, predictFieldInfo, N(predictFieldInfo));
sp->vgetparent = tif->tif_tagmethods.vgetfield;
tif->tif_tagmethods.vgetfield =
PredictorVGetField;/* hook for predictor tag */
@@ -603,7 +705,8 @@ TIFFPredictorInit(TIFF* tif)
tif->tif_setupencode = PredictorSetupEncode;
sp->predictor = 1; /* default value */
- sp->pfunc = NULL; /* no predictor routine */
+ sp->encodepfunc = NULL; /* no predictor routine */
+ sp->decodepfunc = NULL; /* no predictor routine */
return 1;
}
diff --git a/src/libtiff/tif_predict.h b/src/libtiff/tif_predict.h
index 594f973..00dacde 100644
--- a/src/libtiff/tif_predict.h
+++ b/src/libtiff/tif_predict.h
@@ -1,4 +1,4 @@
-/* $Id: tif_predict.h,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_predict.h,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1995-1997 Sam Leffler
@@ -40,10 +40,16 @@ typedef struct {
int stride; /* sample stride over data */
tsize_t rowsize; /* tile/strip row size */
- TIFFPostMethod pfunc; /* horizontal differencer/accumulator */
- TIFFCodeMethod coderow; /* parent codec encode/decode row */
- TIFFCodeMethod codestrip; /* parent codec encode/decode strip */
- TIFFCodeMethod codetile; /* parent codec encode/decode tile */
+ TIFFCodeMethod encoderow; /* parent codec encode/decode row */
+ TIFFCodeMethod encodestrip; /* parent codec encode/decode strip */
+ TIFFCodeMethod encodetile; /* parent codec encode/decode tile */
+ TIFFPostMethod encodepfunc; /* horizontal differencer */
+
+ TIFFCodeMethod decoderow; /* parent codec encode/decode row */
+ TIFFCodeMethod decodestrip; /* parent codec encode/decode strip */
+ TIFFCodeMethod decodetile; /* parent codec encode/decode tile */
+ TIFFPostMethod decodepfunc; /* horizontal accumulator */
+
TIFFVGetMethod vgetparent; /* super-class method */
TIFFVSetMethod vsetparent; /* super-class method */
TIFFPrintMethod printdir; /* super-class method */
diff --git a/src/libtiff/tif_print.c b/src/libtiff/tif_print.c
index 01429be..25bdda9 100644
--- a/src/libtiff/tif_print.c
+++ b/src/libtiff/tif_print.c
@@ -1,4 +1,4 @@
-/* $Id: tif_print.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_print.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -491,7 +491,7 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
} else
fprintf(fd, "(present)\n");
}
- if (TIFFFieldSet(tif, FIELD_SUBIFD)) {
+ if (TIFFFieldSet(tif, FIELD_SUBIFD) && (td->td_subifd)) {
fprintf(fd, " SubIFD Offsets:");
for (i = 0; i < td->td_nsubifd; i++)
fprintf(fd, " %5lu", (long) td->td_subifd[i]);
@@ -509,7 +509,7 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
for(i = 0; i < count; i++) {
ttag_t tag = TIFFGetTagListEntry(tif, i);
const TIFFFieldInfo *fip;
- uint16 value_count;
+ uint32 value_count;
int mem_alloc = 0;
void *raw_data;
diff --git a/src/libtiff/tif_read.c b/src/libtiff/tif_read.c
index 7bc580e..888e4d7 100644
--- a/src/libtiff/tif_read.c
+++ b/src/libtiff/tif_read.c
@@ -1,4 +1,4 @@
-/* $Id: tif_read.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_read.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -50,8 +50,10 @@ TIFFSeek(TIFF* tif, uint32 row, tsample_t sample)
tstrip_t strip;
if (row >= td->td_imagelength) { /* out of range */
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "%lu: Row out of range, max %lu",
- (unsigned long) row, (unsigned long) td->td_imagelength);
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%lu: Row out of range, max %lu",
+ (unsigned long) row,
+ (unsigned long) td->td_imagelength);
return (0);
}
if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
@@ -64,7 +66,7 @@ TIFFSeek(TIFF* tif, uint32 row, tsample_t sample)
strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
} else
strip = row / td->td_rowsperstrip;
- if (strip != tif->tif_curstrip) { /* different strip, refill */
+ if (strip != tif->tif_curstrip) { /* different strip, refill */
if (!TIFFFillStrip(tif, strip))
return (0);
} else if (row < tif->tif_row) {
@@ -104,8 +106,8 @@ TIFFReadScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
e = (*tif->tif_decoderow)
(tif, (tidata_t) buf, tif->tif_scanlinesize, sample);
- /* we are now poised at the beginning of the next row */
- tif->tif_row = row + 1;
+ /* we are now poised at the beginning of the next row */
+ tif->tif_row = row + 1;
if (e)
(*tif->tif_postdecode)(tif, (tidata_t) buf,
@@ -129,22 +131,23 @@ TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
if (!TIFFCheckRead(tif, 0))
return (-1);
if (strip >= td->td_nstrips) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "%ld: Strip out of range, max %ld",
- (long) strip, (long) td->td_nstrips);
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%ld: Strip out of range, max %ld",
+ (long) strip, (long) td->td_nstrips);
return (-1);
}
/*
* Calculate the strip size according to the number of
* rows in the strip (check for truncated last strip on any
- * of the separations).
+ * of the separations).
*/
- if( td->td_rowsperstrip >= td->td_imagelength )
- strips_per_sep = 1;
- else
- strips_per_sep = (td->td_imagelength+td->td_rowsperstrip-1)
- / td->td_rowsperstrip;
+ if( td->td_rowsperstrip >= td->td_imagelength )
+ strips_per_sep = 1;
+ else
+ strips_per_sep = (td->td_imagelength+td->td_rowsperstrip-1)
+ / td->td_rowsperstrip;
- sep_strip = strip % strips_per_sep;
+ sep_strip = strip % strips_per_sep;
if (sep_strip != strips_per_sep-1 ||
(nrows = td->td_imagelength % td->td_rowsperstrip) == 0)
@@ -155,9 +158,9 @@ TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
size = stripsize;
else if (size > stripsize)
size = stripsize;
- if (TIFFFillStrip(tif, strip)
- && (*tif->tif_decodestrip)(tif, (tidata_t) buf, size,
- (tsample_t)(strip / td->td_stripsperimage)) > 0 ) {
+ if (TIFFFillStrip(tif, strip)
+ && (*tif->tif_decodestrip)(tif, (tidata_t) buf, size,
+ (tsample_t)(strip / td->td_stripsperimage)) > 0 ) {
(*tif->tif_postdecode)(tif, (tidata_t) buf, size);
return (size);
} else
@@ -170,6 +173,7 @@ TIFFReadRawStrip1(TIFF* tif,
{
TIFFDirectory *td = &tif->tif_dir;
+ assert((tif->tif_flags&TIFF_NOREADRAW)==0);
if (!isMapped(tif)) {
tsize_t cc;
@@ -215,13 +219,27 @@ TIFFReadRawStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
{
static const char module[] = "TIFFReadRawStrip";
TIFFDirectory *td = &tif->tif_dir;
- tsize_t bytecount;
+ /*
+ * FIXME: butecount should have tsize_t type, but for now libtiff
+ * defines tsize_t as a signed 32-bit integer and we are losing
+ * ability to read arrays larger than 2^31 bytes. So we are using
+ * uint32 instead of tsize_t here.
+ */
+ uint32 bytecount;
if (!TIFFCheckRead(tif, 0))
return ((tsize_t) -1);
if (strip >= td->td_nstrips) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "%lu: Strip out of range, max %lu",
- (unsigned long) strip, (unsigned long) td->td_nstrips);
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%lu: Strip out of range, max %lu",
+ (unsigned long) strip,
+ (unsigned long) td->td_nstrips);
+ return ((tsize_t) -1);
+ }
+ if (tif->tif_flags&TIFF_NOREADRAW)
+ {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Compression scheme does not support access to raw uncompressed data");
return ((tsize_t) -1);
}
bytecount = td->td_stripbytecount[strip];
@@ -231,88 +249,110 @@ TIFFReadRawStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
(unsigned long) bytecount, (unsigned long) strip);
return ((tsize_t) -1);
}
- if (size != (tsize_t)-1 && size < bytecount)
+ if (size != (tsize_t)-1 && (uint32)size < bytecount)
bytecount = size;
return (TIFFReadRawStrip1(tif, strip, buf, bytecount, module));
}
/*
- * Read the specified strip and setup for decoding.
- * The data buffer is expanded, as necessary, to
- * hold the strip's data.
+ * Read the specified strip and setup for decoding. The data buffer is
+ * expanded, as necessary, to hold the strip's data.
*/
int
TIFFFillStrip(TIFF* tif, tstrip_t strip)
{
static const char module[] = "TIFFFillStrip";
TIFFDirectory *td = &tif->tif_dir;
- tsize_t bytecount;
- bytecount = td->td_stripbytecount[strip];
- if (bytecount <= 0) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "%lu: Invalid strip byte count, strip %lu",
- (unsigned long) bytecount, (unsigned long) strip);
- return (0);
- }
- if (isMapped(tif) &&
- (isFillOrder(tif, td->td_fillorder)
- || (tif->tif_flags & TIFF_NOBITREV))) {
+ if ((tif->tif_flags&TIFF_NOREADRAW)==0)
+ {
/*
- * The image is mapped into memory and we either don't
- * need to flip bits or the compression routine is going
- * to handle this operation itself. In this case, avoid
- * copying the raw data and instead just reference the
- * data from the memory mapped file image. This assumes
- * that the decompression routines do not modify the
- * contents of the raw data buffer (if they try to,
- * the application will get a fault since the file is
- * mapped read-only).
+ * FIXME: butecount should have tsize_t type, but for now
+ * libtiff defines tsize_t as a signed 32-bit integer and we
+ * are losing ability to read arrays larger than 2^31 bytes.
+ * So we are using uint32 instead of tsize_t here.
*/
- if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
- _TIFFfree(tif->tif_rawdata);
- tif->tif_flags &= ~TIFF_MYBUFFER;
- if ( td->td_stripoffset[strip] + bytecount > tif->tif_size) {
- /*
- * This error message might seem strange, but it's
- * what would happen if a read were done instead.
- */
+ uint32 bytecount = td->td_stripbytecount[strip];
+ if (bytecount <= 0) {
TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Read error on strip %lu; got %lu bytes, expected %lu",
- tif->tif_name,
- (unsigned long) strip,
- (unsigned long) tif->tif_size - td->td_stripoffset[strip],
- (unsigned long) bytecount);
- tif->tif_curstrip = NOSTRIP;
+ "%s: Invalid strip byte count %lu, strip %lu",
+ tif->tif_name, (unsigned long) bytecount,
+ (unsigned long) strip);
return (0);
}
- tif->tif_rawdatasize = bytecount;
- tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
- } else {
- /*
- * Expand raw data buffer, if needed, to
- * hold data strip coming from file
- * (perhaps should set upper bound on
- * the size of a buffer we'll use?).
- */
- if (bytecount > tif->tif_rawdatasize) {
- tif->tif_curstrip = NOSTRIP;
- if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
+ if (isMapped(tif) &&
+ (isFillOrder(tif, td->td_fillorder)
+ || (tif->tif_flags & TIFF_NOBITREV))) {
+ /*
+ * The image is mapped into memory and we either don't
+ * need to flip bits or the compression routine is
+ * going to handle this operation itself. In this
+ * case, avoid copying the raw data and instead just
+ * reference the data from the memory mapped file
+ * image. This assumes that the decompression
+ * routines do not modify the contents of the raw data
+ * buffer (if they try to, the application will get a
+ * fault since the file is mapped read-only).
+ */
+ if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
+ _TIFFfree(tif->tif_rawdata);
+ tif->tif_flags &= ~TIFF_MYBUFFER;
+ /*
+ * We must check for overflow, potentially causing
+ * an OOB read. Instead of simple
+ *
+ * td->td_stripoffset[strip]+bytecount > tif->tif_size
+ *
+ * comparison (which can overflow) we do the following
+ * two comparisons:
+ */
+ if (bytecount > tif->tif_size ||
+ td->td_stripoffset[strip] > tif->tif_size - bytecount) {
+ /*
+ * This error message might seem strange, but
+ * it's what would happen if a read were done
+ * instead.
+ */
TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Data buffer too small to hold strip %lu",
- tif->tif_name, (unsigned long) strip);
+
+ "%s: Read error on strip %lu; "
+ "got %lu bytes, expected %lu",
+ tif->tif_name, (unsigned long) strip,
+ (unsigned long) tif->tif_size - td->td_stripoffset[strip],
+ (unsigned long) bytecount);
+ tif->tif_curstrip = NOSTRIP;
return (0);
}
- if (!TIFFReadBufferSetup(tif, 0,
- TIFFroundup(bytecount, 1024)))
+ tif->tif_rawdatasize = bytecount;
+ tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
+ } else {
+ /*
+ * Expand raw data buffer, if needed, to hold data
+ * strip coming from file (perhaps should set upper
+ * bound on the size of a buffer we'll use?).
+ */
+ if (bytecount > (uint32)tif->tif_rawdatasize) {
+ tif->tif_curstrip = NOSTRIP;
+ if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
+ TIFFErrorExt(tif->tif_clientdata,
+ module,
+ "%s: Data buffer too small to hold strip %lu",
+ tif->tif_name,
+ (unsigned long) strip);
+ return (0);
+ }
+ if (!TIFFReadBufferSetup(tif, 0,
+ TIFFroundup(bytecount, 1024)))
+ return (0);
+ }
+ if ((uint32)TIFFReadRawStrip1(tif, strip,
+ (unsigned char *)tif->tif_rawdata,
+ bytecount, module) != bytecount)
return (0);
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits(tif->tif_rawdata, bytecount);
}
- if (TIFFReadRawStrip1(tif, strip, (unsigned char *)tif->tif_rawdata,
- bytecount, module) != bytecount)
- return (0);
- if (!isFillOrder(tif, td->td_fillorder) &&
- (tif->tif_flags & TIFF_NOBITREV) == 0)
- TIFFReverseBits(tif->tif_rawdata, bytecount);
}
return (TIFFStartStrip(tif, strip));
}
@@ -349,8 +389,9 @@ TIFFReadEncodedTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
if (!TIFFCheckRead(tif, 1))
return (-1);
if (tile >= td->td_nstrips) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "%ld: Tile out of range, max %ld",
- (long) tile, (unsigned long) td->td_nstrips);
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%ld: Tile out of range, max %ld",
+ (long) tile, (unsigned long) td->td_nstrips);
return (-1);
}
if (size == (tsize_t) -1)
@@ -371,6 +412,7 @@ TIFFReadRawTile1(TIFF* tif,
{
TIFFDirectory *td = &tif->tif_dir;
+ assert((tif->tif_flags&TIFF_NOREADRAW)==0);
if (!isMapped(tif)) {
tsize_t cc;
@@ -419,89 +461,121 @@ TIFFReadRawTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
{
static const char module[] = "TIFFReadRawTile";
TIFFDirectory *td = &tif->tif_dir;
- tsize_t bytecount;
+ /*
+ * FIXME: butecount should have tsize_t type, but for now libtiff
+ * defines tsize_t as a signed 32-bit integer and we are losing
+ * ability to read arrays larger than 2^31 bytes. So we are using
+ * uint32 instead of tsize_t here.
+ */
+ uint32 bytecount;
if (!TIFFCheckRead(tif, 1))
return ((tsize_t) -1);
if (tile >= td->td_nstrips) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "%lu: Tile out of range, max %lu",
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%lu: Tile out of range, max %lu",
(unsigned long) tile, (unsigned long) td->td_nstrips);
return ((tsize_t) -1);
}
+ if (tif->tif_flags&TIFF_NOREADRAW)
+ {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Compression scheme does not support access to raw uncompressed data");
+ return ((tsize_t) -1);
+ }
bytecount = td->td_stripbytecount[tile];
- if (size != (tsize_t) -1 && size < bytecount)
+ if (size != (tsize_t) -1 && (uint32)size < bytecount)
bytecount = size;
return (TIFFReadRawTile1(tif, tile, buf, bytecount, module));
}
/*
- * Read the specified tile and setup for decoding.
- * The data buffer is expanded, as necessary, to
- * hold the tile's data.
+ * Read the specified tile and setup for decoding. The data buffer is
+ * expanded, as necessary, to hold the tile's data.
*/
int
TIFFFillTile(TIFF* tif, ttile_t tile)
{
static const char module[] = "TIFFFillTile";
TIFFDirectory *td = &tif->tif_dir;
- tsize_t bytecount;
- bytecount = td->td_stripbytecount[tile];
- if (bytecount <= 0) {
- TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
- "%lu: Invalid tile byte count, tile %lu",
- (unsigned long) bytecount, (unsigned long) tile);
- return (0);
- }
- if (isMapped(tif) &&
- (isFillOrder(tif, td->td_fillorder)
- || (tif->tif_flags & TIFF_NOBITREV))) {
+ if ((tif->tif_flags&TIFF_NOREADRAW)==0)
+ {
/*
- * The image is mapped into memory and we either don't
- * need to flip bits or the compression routine is going
- * to handle this operation itself. In this case, avoid
- * copying the raw data and instead just reference the
- * data from the memory mapped file image. This assumes
- * that the decompression routines do not modify the
- * contents of the raw data buffer (if they try to,
- * the application will get a fault since the file is
- * mapped read-only).
+ * FIXME: butecount should have tsize_t type, but for now
+ * libtiff defines tsize_t as a signed 32-bit integer and we
+ * are losing ability to read arrays larger than 2^31 bytes.
+ * So we are using uint32 instead of tsize_t here.
*/
- if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
- _TIFFfree(tif->tif_rawdata);
- tif->tif_flags &= ~TIFF_MYBUFFER;
- if ( td->td_stripoffset[tile] + bytecount > tif->tif_size) {
- tif->tif_curtile = NOTILE;
+ uint32 bytecount = td->td_stripbytecount[tile];
+ if (bytecount <= 0) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "%lu: Invalid tile byte count, tile %lu",
+ (unsigned long) bytecount, (unsigned long) tile);
return (0);
}
- tif->tif_rawdatasize = bytecount;
- tif->tif_rawdata = tif->tif_base + td->td_stripoffset[tile];
- } else {
- /*
- * Expand raw data buffer, if needed, to
- * hold data tile coming from file
- * (perhaps should set upper bound on
- * the size of a buffer we'll use?).
- */
- if (bytecount > tif->tif_rawdatasize) {
- tif->tif_curtile = NOTILE;
- if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Data buffer too small to hold tile %ld",
- tif->tif_name, (long) tile);
+ if (isMapped(tif) &&
+ (isFillOrder(tif, td->td_fillorder)
+ || (tif->tif_flags & TIFF_NOBITREV))) {
+ /*
+ * The image is mapped into memory and we either don't
+ * need to flip bits or the compression routine is
+ * going to handle this operation itself. In this
+ * case, avoid copying the raw data and instead just
+ * reference the data from the memory mapped file
+ * image. This assumes that the decompression
+ * routines do not modify the contents of the raw data
+ * buffer (if they try to, the application will get a
+ * fault since the file is mapped read-only).
+ */
+ if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
+ _TIFFfree(tif->tif_rawdata);
+ tif->tif_flags &= ~TIFF_MYBUFFER;
+ /*
+ * We must check for overflow, potentially causing
+ * an OOB read. Instead of simple
+ *
+ * td->td_stripoffset[tile]+bytecount > tif->tif_size
+ *
+ * comparison (which can overflow) we do the following
+ * two comparisons:
+ */
+ if (bytecount > tif->tif_size ||
+ td->td_stripoffset[tile] > tif->tif_size - bytecount) {
+ tif->tif_curtile = NOTILE;
return (0);
}
- if (!TIFFReadBufferSetup(tif, 0,
- TIFFroundup(bytecount, 1024)))
+ tif->tif_rawdatasize = bytecount;
+ tif->tif_rawdata =
+ tif->tif_base + td->td_stripoffset[tile];
+ } else {
+ /*
+ * Expand raw data buffer, if needed, to hold data
+ * tile coming from file (perhaps should set upper
+ * bound on the size of a buffer we'll use?).
+ */
+ if (bytecount > (uint32)tif->tif_rawdatasize) {
+ tif->tif_curtile = NOTILE;
+ if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
+ TIFFErrorExt(tif->tif_clientdata,
+ module,
+ "%s: Data buffer too small to hold tile %ld",
+ tif->tif_name,
+ (long) tile);
+ return (0);
+ }
+ if (!TIFFReadBufferSetup(tif, 0,
+ TIFFroundup(bytecount, 1024)))
+ return (0);
+ }
+ if ((uint32)TIFFReadRawTile1(tif, tile,
+ (unsigned char *)tif->tif_rawdata,
+ bytecount, module) != bytecount)
return (0);
+ if (!isFillOrder(tif, td->td_fillorder) &&
+ (tif->tif_flags & TIFF_NOBITREV) == 0)
+ TIFFReverseBits(tif->tif_rawdata, bytecount);
}
- if (TIFFReadRawTile1(tif, tile,
- (unsigned char *)tif->tif_rawdata,
- bytecount, module) != bytecount)
- return (0);
- if (!isFillOrder(tif, td->td_fillorder) &&
- (tif->tif_flags & TIFF_NOBITREV) == 0)
- TIFFReverseBits(tif->tif_rawdata, bytecount);
}
return (TIFFStartTile(tif, tile));
}
@@ -520,6 +594,7 @@ TIFFReadBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
{
static const char module[] = "TIFFReadBufferSetup";
+ assert((tif->tif_flags&TIFF_NOREADRAW)==0);
if (tif->tif_rawdata) {
if (tif->tif_flags & TIFF_MYBUFFER)
_TIFFfree(tif->tif_rawdata);
@@ -560,8 +635,16 @@ TIFFStartStrip(TIFF* tif, tstrip_t strip)
}
tif->tif_curstrip = strip;
tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
- tif->tif_rawcp = tif->tif_rawdata;
- tif->tif_rawcc = td->td_stripbytecount[strip];
+ if (tif->tif_flags&TIFF_NOREADRAW)
+ {
+ tif->tif_rawcp = NULL;
+ tif->tif_rawcc = 0;
+ }
+ else
+ {
+ tif->tif_rawcp = tif->tif_rawdata;
+ tif->tif_rawcc = td->td_stripbytecount[strip];
+ }
return ((*tif->tif_predecode)(tif,
(tsample_t)(strip / td->td_stripsperimage)));
}
@@ -587,8 +670,16 @@ TIFFStartTile(TIFF* tif, ttile_t tile)
tif->tif_col =
(tile % TIFFhowmany(td->td_imagelength, td->td_tilelength)) *
td->td_tilewidth;
- tif->tif_rawcp = tif->tif_rawdata;
- tif->tif_rawcc = td->td_stripbytecount[tile];
+ if (tif->tif_flags&TIFF_NOREADRAW)
+ {
+ tif->tif_rawcp = NULL;
+ tif->tif_rawcc = 0;
+ }
+ else
+ {
+ tif->tif_rawcp = tif->tif_rawdata;
+ tif->tif_rawcc = td->td_stripbytecount[tile];
+ }
return ((*tif->tif_predecode)(tif,
(tsample_t)(tile/td->td_stripsperimage)));
}
diff --git a/src/libtiff/tif_strip.c b/src/libtiff/tif_strip.c
index e6a0fb7..9eaa98e 100644
--- a/src/libtiff/tif_strip.c
+++ b/src/libtiff/tif_strip.c
@@ -1,4 +1,4 @@
-/* $Id: tif_strip.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_strip.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1991-1997 Sam Leffler
@@ -121,12 +121,12 @@ TIFFVStripSize(TIFF* tif, uint32 nrows)
* horizontal/vertical subsampling area include
* YCbCr data for the extended image.
*/
- uint16 ycbcrsubsampling[2];
- tsize_t w, scanline, samplingarea;
+ uint16 ycbcrsubsampling[2];
+ tsize_t w, scanline, samplingarea;
- TIFFGetField( tif, TIFFTAG_YCBCRSUBSAMPLING,
- ycbcrsubsampling + 0,
- ycbcrsubsampling + 1 );
+ TIFFGetField( tif, TIFFTAG_YCBCRSUBSAMPLING,
+ ycbcrsubsampling + 0,
+ ycbcrsubsampling + 1 );
samplingarea = ycbcrsubsampling[0]*ycbcrsubsampling[1];
if (samplingarea == 0) {
@@ -228,13 +228,13 @@ TIFFScanlineSize(TIFF* tif)
{
TIFFDirectory *td = &tif->tif_dir;
tsize_t scanline;
-
+
if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
if (td->td_photometric == PHOTOMETRIC_YCBCR
&& !isUpSampled(tif)) {
uint16 ycbcrsubsampling[2];
- TIFFGetField(tif, TIFFTAG_YCBCRSUBSAMPLING,
+ TIFFGetField(tif, TIFFTAG_YCBCRSUBSAMPLING,
ycbcrsubsampling + 0,
ycbcrsubsampling + 1);
@@ -268,6 +268,75 @@ TIFFScanlineSize(TIFF* tif)
}
/*
+ * Some stuff depends on this older version of TIFFScanlineSize
+ * TODO: resolve this
+ */
+tsize_t
+TIFFOldScanlineSize(TIFF* tif)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+ tsize_t scanline;
+
+ scanline = multiply (tif, td->td_bitspersample, td->td_imagewidth,
+ "TIFFScanlineSize");
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG)
+ scanline = multiply (tif, scanline, td->td_samplesperpixel,
+ "TIFFScanlineSize");
+ return ((tsize_t) TIFFhowmany8(scanline));
+}
+
+/*
+ * Return the number of bytes to read/write in a call to
+ * one of the scanline-oriented i/o routines. Note that
+ * this number may be 1/samples-per-pixel if data is
+ * stored as separate planes.
+ * The ScanlineSize in case of YCbCrSubsampling is defined as the
+ * strip size divided by the strip height, i.e. the size of a pack of vertical
+ * subsampling lines divided by vertical subsampling. It should thus make
+ * sense when multiplied by a multiple of vertical subsampling.
+ * Some stuff depends on this newer version of TIFFScanlineSize
+ * TODO: resolve this
+ */
+tsize_t
+TIFFNewScanlineSize(TIFF* tif)
+{
+ TIFFDirectory *td = &tif->tif_dir;
+ tsize_t scanline;
+
+ if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
+ if (td->td_photometric == PHOTOMETRIC_YCBCR
+ && !isUpSampled(tif)) {
+ uint16 ycbcrsubsampling[2];
+
+ TIFFGetField(tif, TIFFTAG_YCBCRSUBSAMPLING,
+ ycbcrsubsampling + 0,
+ ycbcrsubsampling + 1);
+
+ if (ycbcrsubsampling[0]*ycbcrsubsampling[1] == 0) {
+ TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+ "Invalid YCbCr subsampling");
+ return 0;
+ }
+
+ return((tsize_t) ((((td->td_imagewidth+ycbcrsubsampling[0]-1)
+ /ycbcrsubsampling[0])
+ *(ycbcrsubsampling[0]*ycbcrsubsampling[1]+2)
+ *td->td_bitspersample+7)
+ /8)/ycbcrsubsampling[1]);
+
+ } else {
+ scanline = multiply(tif, td->td_imagewidth,
+ td->td_samplesperpixel,
+ "TIFFScanlineSize");
+ }
+ } else
+ scanline = td->td_imagewidth;
+ return ((tsize_t) TIFFhowmany8(multiply(tif, scanline,
+ td->td_bitspersample,
+ "TIFFScanlineSize")));
+}
+
+/*
* Return the number of bytes required to store a complete
* decoded and packed raster scanline (as opposed to the
* I/O size returned by TIFFScanlineSize which may be less
diff --git a/src/libtiff/tif_swab.c b/src/libtiff/tif_swab.c
index f8e977e..7cf2e2f 100644
--- a/src/libtiff/tif_swab.c
+++ b/src/libtiff/tif_swab.c
@@ -1,4 +1,4 @@
-/* $Id: tif_swab.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_swab.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
diff --git a/src/libtiff/tif_thunder.c b/src/libtiff/tif_thunder.c
index 40f0618..8befaf3 100644
--- a/src/libtiff/tif_thunder.c
+++ b/src/libtiff/tif_thunder.c
@@ -1,4 +1,4 @@
-/* $Id: tif_thunder.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_thunder.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
diff --git a/src/libtiff/tif_tile.c b/src/libtiff/tif_tile.c
index 4d90e11..c11fb49 100644
--- a/src/libtiff/tif_tile.c
+++ b/src/libtiff/tif_tile.c
@@ -1,4 +1,4 @@
-/* $Id: tif_tile.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_tile.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1991-1997 Sam Leffler
diff --git a/src/libtiff/tif_version.c b/src/libtiff/tif_version.c
index c0f25bf..e4b210a 100644
--- a/src/libtiff/tif_version.c
+++ b/src/libtiff/tif_version.c
@@ -1,4 +1,4 @@
-/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_version.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_version.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1992-1997 Sam Leffler
* Copyright (c) 1992-1997 Silicon Graphics, Inc.
diff --git a/src/libtiff/tif_warning.c b/src/libtiff/tif_warning.c
index cb9d53f..30f7c40 100644
--- a/src/libtiff/tif_warning.c
+++ b/src/libtiff/tif_warning.c
@@ -1,4 +1,4 @@
-/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_warning.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Header: /cvsroot/imtoolkit/im/src/libtiff/tif_warning.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
diff --git a/src/libtiff/tif_write.c b/src/libtiff/tif_write.c
index a07606f..0d589b5 100644
--- a/src/libtiff/tif_write.c
+++ b/src/libtiff/tif_write.c
@@ -1,4 +1,4 @@
-/* $Id: tif_write.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_write.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -227,10 +227,8 @@ TIFFWriteEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t data, tsize_t cc)
if( td->td_stripbytecount[strip] > 0 )
{
- /* if we are writing over existing tiles, zero length. */
- td->td_stripbytecount[strip] = 0;
-
- /* this forces TIFFAppendToStrip() to do a seek */
+ /* Force TIFFAppendToStrip() to consider placing data at end
+ of file. */
tif->tif_curoff = 0;
}
@@ -363,10 +361,8 @@ TIFFWriteEncodedTile(TIFF* tif, ttile_t tile, tdata_t data, tsize_t cc)
if( td->td_stripbytecount[tile] > 0 )
{
- /* if we are writing over existing tiles, zero length. */
- td->td_stripbytecount[tile] = 0;
-
- /* this forces TIFFAppendToStrip() to do a seek */
+ /* Force TIFFAppendToStrip() to consider placing data at end
+ of file. */
tif->tif_curoff = 0;
}
@@ -521,7 +517,8 @@ TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
* because this field is used in other parts of library even
* in the single band case.
*/
- tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
+ if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))
+ tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
} else {
if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
TIFFErrorExt(tif->tif_clientdata, module,
@@ -625,64 +622,53 @@ TIFFGrowStrips(TIFF* tif, int delta, const char* module)
static int
TIFFAppendToStrip(TIFF* tif, tstrip_t strip, tidata_t data, tsize_t cc)
{
- TIFFDirectory *td = &tif->tif_dir;
static const char module[] = "TIFFAppendToStrip";
+ TIFFDirectory *td = &tif->tif_dir;
if (td->td_stripoffset[strip] == 0 || tif->tif_curoff == 0) {
- /*
- * No current offset, set the current strip.
- */
- assert(td->td_nstrips > 0);
- if (td->td_stripoffset[strip] != 0) {
- /*
- * Prevent overlapping of the data chunks. We need
- * this to enable in place updating of the compressed
- * images. Larger blocks will be moved at the end of
- * the file without any optimization of the spare
- * space, so such scheme is not too much effective.
- */
- if (td->td_stripbytecountsorted) {
- if (strip == td->td_nstrips - 1
- || td->td_stripoffset[strip + 1] <
- td->td_stripoffset[strip] + cc) {
- td->td_stripoffset[strip] =
- TIFFSeekFile(tif, (toff_t)0,
- SEEK_END);
- }
- } else {
- tstrip_t i;
- for (i = 0; i < td->td_nstrips; i++) {
- if (td->td_stripoffset[i] >
- td->td_stripoffset[strip]
- && td->td_stripoffset[i] <
- td->td_stripoffset[strip] + cc) {
- td->td_stripoffset[strip] =
- TIFFSeekFile(tif,
- (toff_t)0,
- SEEK_END);
- }
- }
- }
-
- if (!SeekOK(tif, td->td_stripoffset[strip])) {
- TIFFErrorExt(tif->tif_clientdata, module,
- "%s: Seek error at scanline %lu",
- tif->tif_name,
- (unsigned long)tif->tif_row);
- return (0);
- }
- } else
- td->td_stripoffset[strip] =
- TIFFSeekFile(tif, (toff_t) 0, SEEK_END);
- tif->tif_curoff = td->td_stripoffset[strip];
+ assert(td->td_nstrips > 0);
+
+ if( td->td_stripbytecount[strip] != 0
+ && td->td_stripoffset[strip] != 0
+ && (tsize_t) td->td_stripbytecount[strip] >= cc )
+ {
+ /*
+ * There is already tile data on disk, and the new tile
+ * data we have to will fit in the same space. The only
+ * aspect of this that is risky is that there could be
+ * more data to append to this strip before we are done
+ * depending on how we are getting called.
+ */
+ if (!SeekOK(tif, td->td_stripoffset[strip])) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Seek error at scanline %lu",
+ (unsigned long)tif->tif_row);
+ return (0);
+ }
+ }
+ else
+ {
+ /*
+ * Seek to end of file, and set that as our location to
+ * write this strip.
+ */
+ td->td_stripoffset[strip] = TIFFSeekFile(tif, 0, SEEK_END);
+ }
+
+ tif->tif_curoff = td->td_stripoffset[strip];
+
+ /*
+ * We are starting a fresh strip/tile, so set the size to zero.
+ */
+ td->td_stripbytecount[strip] = 0;
}
if (!WriteOK(tif, data, cc)) {
- TIFFErrorExt(tif->tif_clientdata, module, "%s: Write error at scanline %lu",
- tif->tif_name, (unsigned long) tif->tif_row);
- return (0);
+ TIFFErrorExt(tif->tif_clientdata, module, "Write error at scanline %lu",
+ (unsigned long) tif->tif_row);
+ return (0);
}
- tif->tif_curoff += cc;
+ tif->tif_curoff = tif->tif_curoff+cc;
td->td_stripbytecount[strip] += cc;
return (1);
}
diff --git a/src/libtiff/tif_zip.c b/src/libtiff/tif_zip.c
index 41781f8..a55e73f 100644
--- a/src/libtiff/tif_zip.c
+++ b/src/libtiff/tif_zip.c
@@ -1,4 +1,4 @@
-/* $Id: tif_zip.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tif_zip.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1995-1997 Sam Leffler
@@ -70,7 +70,8 @@ typedef struct {
z_stream stream;
int zipquality; /* compression level */
int state; /* state flags */
-#define ZSTATE_INIT 0x1 /* zlib setup successfully */
+#define ZSTATE_INIT_DECODE 0x01
+#define ZSTATE_INIT_ENCODE 0x02
TIFFVGetMethod vgetparent; /* super-class method */
TIFFVSetMethod vsetparent; /* super-class method */
@@ -90,11 +91,18 @@ ZIPSetupDecode(TIFF* tif)
static const char module[] = "ZIPSetupDecode";
assert(sp != NULL);
+
+ /* if we were last encoding, terminate this mode */
+ if (sp->state & ZSTATE_INIT_ENCODE) {
+ deflateEnd(&sp->stream);
+ sp->state = 0;
+ }
+
if (inflateInit(&sp->stream) != Z_OK) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
return (0);
} else {
- sp->state |= ZSTATE_INIT;
+ sp->state |= ZSTATE_INIT_DECODE;
return (1);
}
}
@@ -109,6 +117,10 @@ ZIPPreDecode(TIFF* tif, tsample_t s)
(void) s;
assert(sp != NULL);
+
+ if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
+ tif->tif_setupdecode( tif );
+
sp->stream.next_in = tif->tif_rawdata;
sp->stream.avail_in = tif->tif_rawcc;
return (inflateReset(&sp->stream) == Z_OK);
@@ -122,6 +134,8 @@ ZIPDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
(void) s;
assert(sp != NULL);
+ assert(sp->state == ZSTATE_INIT_DECODE);
+
sp->stream.next_out = op;
sp->stream.avail_out = occ;
do {
@@ -158,11 +172,16 @@ ZIPSetupEncode(TIFF* tif)
static const char module[] = "ZIPSetupEncode";
assert(sp != NULL);
+ if (sp->state & ZSTATE_INIT_DECODE) {
+ inflateEnd(&sp->stream);
+ sp->state = 0;
+ }
+
if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
return (0);
} else {
- sp->state |= ZSTATE_INIT;
+ sp->state |= ZSTATE_INIT_ENCODE;
return (1);
}
}
@@ -177,6 +196,9 @@ ZIPPreEncode(TIFF* tif, tsample_t s)
(void) s;
assert(sp != NULL);
+ if( sp->state != ZSTATE_INIT_ENCODE )
+ tif->tif_setupencode( tif );
+
sp->stream.next_out = tif->tif_rawdata;
sp->stream.avail_out = tif->tif_rawdatasize;
return (deflateReset(&sp->stream) == Z_OK);
@@ -191,6 +213,9 @@ ZIPEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
ZIPState *sp = EncoderState(tif);
static const char module[] = "ZIPEncode";
+ assert(sp != NULL);
+ assert(sp->state == ZSTATE_INIT_ENCODE);
+
(void) s;
sp->stream.next_in = bp;
sp->stream.avail_in = cc;
@@ -257,12 +282,12 @@ ZIPCleanup(TIFF* tif)
tif->tif_tagmethods.vgetfield = sp->vgetparent;
tif->tif_tagmethods.vsetfield = sp->vsetparent;
- if (sp->state&ZSTATE_INIT) {
- /* NB: avoid problems in the library */
- if (tif->tif_mode == O_RDONLY)
- inflateEnd(&sp->stream);
- else
- deflateEnd(&sp->stream);
+ if (sp->state & ZSTATE_INIT_ENCODE) {
+ deflateEnd(&sp->stream);
+ sp->state = 0;
+ } else if( sp->state & ZSTATE_INIT_DECODE) {
+ inflateEnd(&sp->stream);
+ sp->state = 0;
}
_TIFFfree(sp);
tif->tif_data = NULL;
@@ -279,7 +304,7 @@ ZIPVSetField(TIFF* tif, ttag_t tag, va_list ap)
switch (tag) {
case TIFFTAG_ZIPQUALITY:
sp->zipquality = va_arg(ap, int);
- if (tif->tif_mode != O_RDONLY && (sp->state&ZSTATE_INIT)) {
+ if ( sp->state&ZSTATE_INIT_ENCODE ) {
if (deflateParams(&sp->stream,
sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
@@ -317,12 +342,23 @@ static const TIFFFieldInfo zipFieldInfo[] = {
int
TIFFInitZIP(TIFF* tif, int scheme)
{
+ static const char module[] = "TIFFInitZIP";
ZIPState* sp;
assert( (scheme == COMPRESSION_DEFLATE)
|| (scheme == COMPRESSION_ADOBE_DEFLATE));
/*
+ * Merge codec-specific tag information.
+ */
+ if (!_TIFFMergeFieldInfo(tif, zipFieldInfo,
+ TIFFArrayCount(zipFieldInfo))) {
+ TIFFErrorExt(tif->tif_clientdata, module,
+ "Merging Deflate codec-specific tags failed");
+ return 0;
+ }
+
+ /*
* Allocate state block so tag methods have storage to record values.
*/
tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (ZIPState));
@@ -335,10 +371,8 @@ TIFFInitZIP(TIFF* tif, int scheme)
sp->stream.data_type = Z_BINARY;
/*
- * Merge codec-specific tag information and
- * override parent get/set field methods.
+ * Override parent get/set field methods.
*/
- _TIFFMergeFieldInfo(tif, zipFieldInfo, TIFFArrayCount(zipFieldInfo));
sp->vgetparent = tif->tif_tagmethods.vgetfield;
tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
sp->vsetparent = tif->tif_tagmethods.vsetfield;
@@ -369,7 +403,7 @@ TIFFInitZIP(TIFF* tif, int scheme)
(void) TIFFPredictorInit(tif);
return (1);
bad:
- TIFFErrorExt(tif->tif_clientdata, "TIFFInitZIP",
+ TIFFErrorExt(tif->tif_clientdata, module,
"No space for ZIP state block");
return (0);
}
diff --git a/src/libtiff/tiff.h b/src/libtiff/tiff.h
index 0697268..b258f25 100644
--- a/src/libtiff/tiff.h
+++ b/src/libtiff/tiff.h
@@ -1,4 +1,4 @@
-/* $Id: tiff.h,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tiff.h,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -43,7 +43,7 @@
* (http://partners.adobe.com/asn/developer/PDFS/TN/TIFF6.pdf)
*
* For Big TIFF design notes see the following link
- * http://gdal.maptools.org/twiki/bin/view/libtiff/BigTIFFDesign
+ * http://www.remotesensing.org/libtiff/bigtiffdesign.html
*/
#define TIFF_VERSION 42
#define TIFF_BIGTIFF_VERSION 43
diff --git a/src/libtiff/tiffio.h b/src/libtiff/tiffio.h
index 3a5eebd..3cfcb22 100644
--- a/src/libtiff/tiffio.h
+++ b/src/libtiff/tiffio.h
@@ -1,4 +1,4 @@
-/* $Id: tiffio.h,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tiffio.h,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -58,14 +58,15 @@ typedef struct tiff TIFF;
* 32-bit file offsets being the most important, and to ensure
* that it is unsigned, rather than signed.
*/
-typedef uint32 ttag_t; /* directory tag */
-typedef uint16 tdir_t; /* directory index */
-typedef uint16 tsample_t; /* sample number */
-typedef uint32 tstrip_t; /* strip number */
-typedef uint32 ttile_t; /* tile number */
-typedef int32 tsize_t; /* i/o size in bytes */
-typedef void* tdata_t; /* image data ref */
-typedef uint32 toff_t; /* file offset */
+typedef uint32 ttag_t; /* directory tag */
+typedef uint16 tdir_t; /* directory index */
+typedef uint16 tsample_t; /* sample number */
+typedef uint32 tstrile_t; /* strip or tile number */
+typedef tstrile_t tstrip_t; /* strip number */
+typedef tstrile_t ttile_t; /* tile number */
+typedef int32 tsize_t; /* i/o size in bytes */
+typedef void* tdata_t; /* image data ref */
+typedef uint32 toff_t; /* file offset */
#if !defined(__WIN32__) && (defined(_WIN32) || defined(WIN32))
#define __WIN32__
@@ -96,10 +97,6 @@ typedef HFILE thandle_t; /* client data handle */
typedef void* thandle_t; /* client data handle */
#endif /* USE_WIN32_FILEIO */
-#ifndef NULL
-# define NULL (void *)0
-#endif
-
/*
* Flags to pass to TIFFPrintDirectory to control
* printing of data structures that are potentially
@@ -191,35 +188,36 @@ typedef void (*tileSeparateRoutine)
* RGBA-reader state.
*/
struct _TIFFRGBAImage {
- TIFF* tif; /* image handle */
- int stoponerr; /* stop on read error */
- int isContig; /* data is packed/separate */
- int alpha; /* type of alpha data present */
- uint32 width; /* image width */
- uint32 height; /* image height */
- uint16 bitspersample; /* image bits/sample */
- uint16 samplesperpixel; /* image samples/pixel */
- uint16 orientation; /* image orientation */
- uint16 req_orientation; /* requested orientation */
- uint16 photometric; /* image photometric interp */
- uint16* redcmap; /* colormap pallete */
- uint16* greencmap;
- uint16* bluecmap;
- /* get image data routine */
- int (*get)(TIFFRGBAImage*, uint32*, uint32, uint32);
+ TIFF* tif; /* image handle */
+ int stoponerr; /* stop on read error */
+ int isContig; /* data is packed/separate */
+ int alpha; /* type of alpha data present */
+ uint32 width; /* image width */
+ uint32 height; /* image height */
+ uint16 bitspersample; /* image bits/sample */
+ uint16 samplesperpixel; /* image samples/pixel */
+ uint16 orientation; /* image orientation */
+ uint16 req_orientation; /* requested orientation */
+ uint16 photometric; /* image photometric interp */
+ uint16* redcmap; /* colormap pallete */
+ uint16* greencmap;
+ uint16* bluecmap;
+ /* get image data routine */
+ int (*get)(TIFFRGBAImage*, uint32*, uint32, uint32);
+ /* put decoded strip/tile */
union {
void (*any)(TIFFRGBAImage*);
- tileContigRoutine contig;
- tileSeparateRoutine separate;
- } put; /* put decoded strip/tile */
- TIFFRGBValue* Map; /* sample mapping array */
- uint32** BWmap; /* black&white map */
- uint32** PALmap; /* palette image map */
- TIFFYCbCrToRGB* ycbcr; /* YCbCr conversion state */
- TIFFCIELabToRGB* cielab; /* CIE L*a*b conversion state */
-
- int row_offset;
- int col_offset;
+ tileContigRoutine contig;
+ tileSeparateRoutine separate;
+ } put;
+ TIFFRGBValue* Map; /* sample mapping array */
+ uint32** BWmap; /* black&white map */
+ uint32** PALmap; /* palette image map */
+ TIFFYCbCrToRGB* ycbcr; /* YCbCr conversion state */
+ TIFFCIELabToRGB* cielab; /* CIE L*a*b conversion state */
+
+ int row_offset;
+ int col_offset;
};
/*
@@ -253,6 +251,10 @@ typedef struct {
#define LOGLUV_PUBLIC 1
#endif
+#if !defined(__GNUC__) && !defined(__attribute__)
+# define __attribute__(x) /*nothing*/
+#endif
+
#if defined(c_plusplus) || defined(__cplusplus)
extern "C" {
#endif
@@ -349,6 +351,8 @@ extern int TIFFReadCustomDirectory(TIFF*, toff_t, const TIFFFieldInfo[],
size_t);
extern int TIFFReadEXIFDirectory(TIFF*, toff_t);
extern tsize_t TIFFScanlineSize(TIFF*);
+extern tsize_t TIFFOldScanlineSize(TIFF*);
+extern tsize_t TIFFNewScanlineSize(TIFF*);
extern tsize_t TIFFRasterScanlineSize(TIFF*);
extern tsize_t TIFFStripSize(TIFF*);
extern tsize_t TIFFRawStripSize(TIFF*, tstrip_t);
@@ -433,10 +437,10 @@ extern TIFF* TIFFClientOpen(const char*, const char*,
TIFFMapFileProc, TIFFUnmapFileProc);
extern const char* TIFFFileName(TIFF*);
extern const char* TIFFSetFileName(TIFF*, const char *);
-extern void TIFFError(const char*, const char*, ...);
-extern void TIFFErrorExt(thandle_t, const char*, const char*, ...);
-extern void TIFFWarning(const char*, const char*, ...);
-extern void TIFFWarningExt(thandle_t, const char*, const char*, ...);
+extern void TIFFError(const char*, const char*, ...) __attribute__((format (printf,2,3)));
+extern void TIFFErrorExt(thandle_t, const char*, const char*, ...) __attribute__((format (printf,3,4)));
+extern void TIFFWarning(const char*, const char*, ...) __attribute__((format (printf,2,3)));
+extern void TIFFWarningExt(thandle_t, const char*, const char*, ...) __attribute__((format (printf,3,4)));
extern TIFFErrorHandler TIFFSetErrorHandler(TIFFErrorHandler);
extern TIFFErrorHandlerExt TIFFSetErrorHandlerExt(TIFFErrorHandlerExt);
extern TIFFErrorHandler TIFFSetWarningHandler(TIFFErrorHandler);
diff --git a/src/libtiff/tiffiop.h b/src/libtiff/tiffiop.h
index a0e5856..dcf9a63 100644
--- a/src/libtiff/tiffiop.h
+++ b/src/libtiff/tiffiop.h
@@ -1,4 +1,4 @@
-/* $Id: tiffiop.h,v 1.1 2008/10/17 06:16:07 scuri Exp $ */
+/* $Id: tiffiop.h,v 1.2 2009/08/21 04:01:59 scuri Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -57,6 +57,13 @@ extern void *lfind(const void *, const void *, size_t *, size_t,
int (*)(const void *, const void *));
#endif
+/*
+ Libtiff itself does not require a 64-bit type, but bundled TIFF
+ utilities may use it.
+*/
+typedef TIFF_INT64_T int64;
+typedef TIFF_UINT64_T uint64;
+
#include "tiffio.h"
#include "tif_dir.h"
@@ -97,29 +104,35 @@ struct tiff {
int tif_fd; /* open file descriptor */
int tif_mode; /* open mode (O_*) */
uint32 tif_flags;
-#define TIFF_FILLORDER 0x0003 /* natural bit fill order for machine */
-#define TIFF_DIRTYHEADER 0x0004 /* header must be written on close */
-#define TIFF_DIRTYDIRECT 0x0008 /* current directory must be written */
-#define TIFF_BUFFERSETUP 0x0010 /* data buffers setup */
-#define TIFF_CODERSETUP 0x0020 /* encoder/decoder setup done */
-#define TIFF_BEENWRITING 0x0040 /* written 1+ scanlines to file */
-#define TIFF_SWAB 0x0080 /* byte swap file information */
-#define TIFF_NOBITREV 0x0100 /* inhibit bit reversal logic */
-#define TIFF_MYBUFFER 0x0200 /* my raw data buffer; free on close */
-#define TIFF_ISTILED 0x0400 /* file is tile, not strip- based */
-#define TIFF_MAPPED 0x0800 /* file is mapped into memory */
-#define TIFF_POSTENCODE 0x1000 /* need call to postencode routine */
-#define TIFF_INSUBIFD 0x2000 /* currently writing a subifd */
-#define TIFF_UPSAMPLED 0x4000 /* library is doing data up-sampling */
-#define TIFF_STRIPCHOP 0x8000 /* enable strip chopping support */
+#define TIFF_FILLORDER 0x00003 /* natural bit fill order for machine */
+#define TIFF_DIRTYHEADER 0x00004 /* header must be written on close */
+#define TIFF_DIRTYDIRECT 0x00008 /* current directory must be written */
+#define TIFF_BUFFERSETUP 0x00010 /* data buffers setup */
+#define TIFF_CODERSETUP 0x00020 /* encoder/decoder setup done */
+#define TIFF_BEENWRITING 0x00040 /* written 1+ scanlines to file */
+#define TIFF_SWAB 0x00080 /* byte swap file information */
+#define TIFF_NOBITREV 0x00100 /* inhibit bit reversal logic */
+#define TIFF_MYBUFFER 0x00200 /* my raw data buffer; free on close */
+#define TIFF_ISTILED 0x00400 /* file is tile, not strip- based */
+#define TIFF_MAPPED 0x00800 /* file is mapped into memory */
+#define TIFF_POSTENCODE 0x01000 /* need call to postencode routine */
+#define TIFF_INSUBIFD 0x02000 /* currently writing a subifd */
+#define TIFF_UPSAMPLED 0x04000 /* library is doing data up-sampling */
+#define TIFF_STRIPCHOP 0x08000 /* enable strip chopping support */
#define TIFF_HEADERONLY 0x10000 /* read header only, do not process */
/* the first directory */
+#define TIFF_NOREADRAW 0x20000 /* skip reading of raw uncompressed */
+ /* image data */
+#define TIFF_INCUSTOMIFD 0x40000 /* currently writing a custom IFD */
toff_t tif_diroff; /* file offset of current directory */
toff_t tif_nextdiroff; /* file offset of following directory */
toff_t* tif_dirlist; /* list of offsets to already seen */
/* directories to prevent IFD looping */
+ tsize_t tif_dirlistsize;/* number of entires in offset list */
uint16 tif_dirnumber; /* number of already seen directories */
TIFFDirectory tif_dir; /* internal rep of current directory */
+ TIFFDirectory tif_customdir; /* custom IFDs are separated from
+ the main ones */
TIFFHeader tif_header; /* file's header block */
const int* tif_typeshift; /* data type shift counts */
const long* tif_typemask; /* data type masks */
@@ -164,7 +177,8 @@ struct tiff {
tsize_t tif_rawcc; /* bytes unread from raw buffer */
/* memory-mapped file support */
tidata_t tif_base; /* base of mapped file */
- toff_t tif_size; /* size of mapped file region (bytes) */
+ toff_t tif_size; /* size of mapped file region (bytes)
+ FIXME: it should be tsize_t */
TIFFMapFileProc tif_mapproc; /* map file method */
TIFFUnmapFileProc tif_unmapproc;/* unmap file method */
/* input/output callback methods */
@@ -273,6 +287,7 @@ extern TIFFErrorHandlerExt _TIFFwarningHandlerExt;
extern TIFFErrorHandlerExt _TIFFerrorHandlerExt;
extern tdata_t _TIFFCheckMalloc(TIFF*, size_t, size_t, const char*);
+extern tdata_t _TIFFCheckRealloc(TIFF*, tdata_t, size_t, size_t, const char*);
extern int TIFFInitDumpMode(TIFF*, int);
#ifdef PACKBITS_SUPPORT
diff --git a/src/libtiff/tiffvers.h b/src/libtiff/tiffvers.h
index 9744f8d..aec110d 100644
--- a/src/libtiff/tiffvers.h
+++ b/src/libtiff/tiffvers.h
@@ -1,4 +1,4 @@
-#define TIFFLIB_VERSION_STR "LIBTIFF, Version 3.8.2\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
+#define TIFFLIB_VERSION_STR "LIBTIFF, Version 3.9.0\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
/*
* This define can be used in code that requires
* compilation-related definitions specific to a
@@ -6,4 +6,4 @@
* version checking should be done based on the
* string returned by TIFFGetVersion.
*/
-#define TIFFLIB_VERSION 20060323
+#define TIFFLIB_VERSION 20090820