Merge pull request #13808 from tailsu:sd/libtiff-4.0.10

pull/13726/head
Alexander Alekhin 6 years ago
commit bd00471a89
  1. 9
      3rdparty/libtiff/CMakeLists.txt
  2. 3913
      3rdparty/libtiff/ChangeLog
  3. 67
      3rdparty/libtiff/libport.h
  4. 2
      3rdparty/libtiff/snprintf.c
  5. 2
      3rdparty/libtiff/t4.h
  6. 2
      3rdparty/libtiff/tif_aux.c
  7. 2
      3rdparty/libtiff/tif_close.c
  8. 10
      3rdparty/libtiff/tif_codec.c
  9. 4
      3rdparty/libtiff/tif_color.c
  10. 2
      3rdparty/libtiff/tif_compress.c
  11. 51
      3rdparty/libtiff/tif_config.h.cmake.in
  12. 29
      3rdparty/libtiff/tif_dir.c
  13. 6
      3rdparty/libtiff/tif_dir.h
  14. 17
      3rdparty/libtiff/tif_dirinfo.c
  15. 117
      3rdparty/libtiff/tif_dirread.c
  16. 9
      3rdparty/libtiff/tif_dirwrite.c
  17. 2
      3rdparty/libtiff/tif_dumpmode.c
  18. 2
      3rdparty/libtiff/tif_error.c
  19. 2
      3rdparty/libtiff/tif_extension.c
  20. 2
      3rdparty/libtiff/tif_fax3.c
  21. 2
      3rdparty/libtiff/tif_fax3.h
  22. 2
      3rdparty/libtiff/tif_flush.c
  23. 2
      3rdparty/libtiff/tif_getimage.c
  24. 34
      3rdparty/libtiff/tif_jbig.c
  25. 6
      3rdparty/libtiff/tif_jpeg.c
  26. 12
      3rdparty/libtiff/tif_luv.c
  27. 11
      3rdparty/libtiff/tif_lzma.c
  28. 32
      3rdparty/libtiff/tif_lzw.c
  29. 2
      3rdparty/libtiff/tif_next.c
  30. 2
      3rdparty/libtiff/tif_ojpeg.c
  31. 2
      3rdparty/libtiff/tif_open.c
  32. 2
      3rdparty/libtiff/tif_packbits.c
  33. 2
      3rdparty/libtiff/tif_pixarlog.c
  34. 2
      3rdparty/libtiff/tif_predict.c
  35. 6
      3rdparty/libtiff/tif_predict.h
  36. 12
      3rdparty/libtiff/tif_print.c
  37. 13
      3rdparty/libtiff/tif_read.c
  38. 460
      3rdparty/libtiff/tif_stream.cxx
  39. 2
      3rdparty/libtiff/tif_strip.c
  40. 2
      3rdparty/libtiff/tif_swab.c
  41. 2
      3rdparty/libtiff/tif_thunder.c
  42. 2
      3rdparty/libtiff/tif_tile.c
  43. 2
      3rdparty/libtiff/tif_unix.c
  44. 1
      3rdparty/libtiff/tif_version.c
  45. 2
      3rdparty/libtiff/tif_warning.c
  46. 684
      3rdparty/libtiff/tif_webp.c
  47. 41
      3rdparty/libtiff/tif_win32.c
  48. 8
      3rdparty/libtiff/tif_write.c
  49. 2
      3rdparty/libtiff/tif_zip.c
  50. 440
      3rdparty/libtiff/tif_zstd.c
  51. 18
      3rdparty/libtiff/tiff.h
  52. 9
      3rdparty/libtiff/tiffconf.h.cmake.in
  53. 4
      3rdparty/libtiff/tiffio.h
  54. 3
      3rdparty/libtiff/tiffio.hxx
  55. 15
      3rdparty/libtiff/tiffiop.h
  56. 4
      3rdparty/libtiff/tiffvers.h

@ -414,10 +414,11 @@ set(lib_srcs
tif_tile.c
tif_version.c
tif_warning.c
tif_webp.c
tif_write.c
tif_zip.c
tif_zstd.c
tif_stream.cxx
snprintf.c
t4.h
tif_dir.h
tif_fax3.h
@ -432,6 +433,10 @@ set(lib_srcs
"${CMAKE_CURRENT_BINARY_DIR}/tiffconf.h"
)
if(WIN32 AND NOT HAVE_SNPRINTF)
list(APPEND lib_srcs snprintf.c libport.h)
endif()
if(WIN32 AND NOT WINRT)
list(APPEND lib_srcs tif_win32.c)
else()
@ -444,7 +449,7 @@ ocv_warnings_disable(CMAKE_C_FLAGS -Wno-unused-but-set-variable -Wmissing-protot
-Wimplicit-fallthrough
)
ocv_warnings_disable(CMAKE_C_FLAGS -Wunused-parameter) # clang
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wmissing-declarations -Wunused-parameter
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wmissing-declarations -Wunused-parameter -Wmissing-prototypes
-Wundef # tiffiop.h: #if __clang_major__ >= 4
)
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4018 /wd4100 /wd4127 /wd4311 /wd4701 /wd4706) # vs2005

File diff suppressed because it is too large Load Diff

@ -0,0 +1,67 @@
/*
* Copyright (c) 2009 Frank Warmerdam
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS 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.
*/
#ifndef _LIBPORT_
#define _LIBPORT_
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int opterr;
extern int optind;
extern int optopt;
int strcasecmp(const char *s1, const char *s2);
#ifndef HAVE_GETOPT
# define HAVE_GETOPT 1
#endif
#if HAVE_STRTOL
long strtol(const char *nptr, char **endptr, int base);
#endif
#if HAVE_STRTOLL
long long strtoll(const char *nptr, char **endptr, int base);
#endif
#if HAVE_STRTOUL
unsigned long strtoul(const char *nptr, char **endptr, int base);
#endif
#if HAVE_STRTOULL
unsigned long long strtoull(const char *nptr, char **endptr, int base);
#endif
#if 0
void *
lfind(const void *key, const void *base, size_t *nmemb, size_t size,
int(*compar)(const void *, const void *));
#endif
#if !defined(HAVE_SNPRINTF)
#undef vsnprintf
#define vsnprintf _TIFF_vsnprintf_f
#undef snprintf
#define snprintf _TIFF_snprintf_f
int snprintf(char* str, size_t size, const char* format, ...);
#endif
#endif /* ndef _LIBPORT_ */

@ -9,6 +9,7 @@
#include <stdio.h>
#include <stdarg.h>
#include "libport.h"
int _TIFF_vsnprintf_f(char* str, size_t size, const char* format, va_list ap)
{
@ -35,4 +36,3 @@ int _TIFF_snprintf_f(char* str, size_t size, const char* format, ...)
}
#endif // _MSC_VER

@ -1,5 +1,3 @@
/* $Id: t4.h,v 1.3 2010-03-10 18:56:48 bfriesen Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_aux.c,v 1.31 2017-11-17 20:21:00 erouault Exp $ */
/*
* Copyright (c) 1991-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_close.c,v 1.21 2016-01-23 21:20:34 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_codec.c,v 1.17 2015-08-19 02:31:04 bfriesen Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -72,6 +70,12 @@ static int NotConfigured(TIFF*, int);
#ifndef LZMA_SUPPORT
#define TIFFInitLZMA NotConfigured
#endif
#ifndef ZSTD_SUPPORT
#define TIFFInitZSTD NotConfigured
#endif
#ifndef WEBP_SUPPORT
#define TIFFInitWebP NotConfigured
#endif
/*
* Compression schemes statically built into the library.
@ -99,6 +103,8 @@ TIFFCodec _TIFFBuiltinCODECS[] = {
{ "SGILog", COMPRESSION_SGILOG, TIFFInitSGILog },
{ "SGILog24", COMPRESSION_SGILOG24, TIFFInitSGILog },
{ "LZMA", COMPRESSION_LZMA, TIFFInitLZMA },
{ "ZSTD", COMPRESSION_ZSTD, TIFFInitZSTD },
{ "WEBP", COMPRESSION_WEBP, TIFFInitWebP },
{ NULL, 0, NULL }
};

@ -1,5 +1,3 @@
/* $Id: tif_color.c,v 1.24 2017-05-29 10:12:54 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -168,7 +166,7 @@ TIFFCIELabToRGBInit(TIFFCIELabToRGB* cielab,
}
/*
* Convert color value from the YCbCr space to CIE XYZ.
* Convert color value from the YCbCr space to RGB.
* The colorspace conversion algorithm comes from the IJG v5a code;
* see below for more information on how it works.
*/

@ -1,5 +1,3 @@
/* $Id: tif_compress.c,v 1.25 2016-10-25 20:04:22 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -26,9 +26,6 @@
/* Define to 1 if you have the <fcntl.h> header file. */
#cmakedefine HAVE_FCNTL_H 1
/* Define to 1 if you have the `floor' function. */
#cmakedefine HAVE_FLOOR 1
/* Define to 1 if you have the `getopt' function. */
#cmakedefine HAVE_GETOPT 1
@ -50,30 +47,12 @@
/* Define to 1 if you have the <io.h> header file. */
#cmakedefine HAVE_IO_H 1
/* Define to 1 if you have the `isascii' function. */
#cmakedefine HAVE_ISASCII 1
/* Define to 1 if you have the `jbg_newlen' function. */
#cmakedefine HAVE_JBG_NEWLEN 1
/* Define to 1 if you have the `lfind' function. */
#cmakedefine HAVE_LFIND 1
/* Define to 1 if you have the <limits.h> header file. */
#cmakedefine HAVE_LIMITS_H 1
/* Define to 1 if you have the <malloc.h> header file. */
#cmakedefine HAVE_MALLOC_H 1
/* Define to 1 if you have the `memmove' function. */
#cmakedefine HAVE_MEMMOVE 1
/* Define to 1 if you have the <memory.h> header file. */
#cmakedefine HAVE_MEMORY_H 1
/* Define to 1 if you have the `memset' function. */
#cmakedefine HAVE_MEMSET 1
/* Define to 1 if you have the `mmap' function. */
#cmakedefine HAVE_MMAP 1
@ -83,9 +62,6 @@
/* Define to 1 if you have the <OpenGL/gl.h> header file. */
#cmakedefine HAVE_OPENGL_GL_H 1
/* Define to 1 if you have the `pow' function. */
#cmakedefine HAVE_POW 1
/* Define to 1 if you have the <search.h> header file. */
#cmakedefine HAVE_SEARCH_H 1
@ -95,33 +71,24 @@
/* Define to 1 if you have the `snprintf' function. */
#cmakedefine HAVE_SNPRINTF 1
/* Define to 1 if you have the `sqrt' function. */
#cmakedefine HAVE_SQRT 1
/* Define to 1 if you have the <stdint.h> header file. */
#cmakedefine HAVE_STDINT_H 1
/* Define to 1 if you have the `strcasecmp' function. */
#cmakedefine HAVE_STRCASECMP 1
/* Define to 1 if you have the `strchr' function. */
#cmakedefine HAVE_STRCHR 1
/* Define to 1 if you have the <strings.h> header file. */
#cmakedefine HAVE_STRINGS_H 1
/* Define to 1 if you have the <string.h> header file. */
#cmakedefine HAVE_STRING_H 1
/* Define to 1 if you have the `strrchr' function. */
#cmakedefine HAVE_STRRCHR 1
/* Define to 1 if you have the `strstr' function. */
#cmakedefine HAVE_STRSTR 1
/* Define to 1 if you have the `strtol' function. */
#cmakedefine HAVE_STRTOL 1
/* Define to 1 if you have the `strtoll' function. */
#cmakedefine HAVE_STRTOLL 1
/* Define to 1 if you have the `strtoul' function. */
#cmakedefine HAVE_STRTOUL 1
@ -146,6 +113,12 @@
/* Support LZMA2 compression */
#cmakedefine LZMA_SUPPORT 1
/* Support ZSTD compression */
#cmakedefine ZSTD_SUPPORT 1
/* Support WEBP compression */
#cmakedefine WEBP_SUPPORT 1
/* Name of package */
#define PACKAGE "@PACKAGE_NAME@"
@ -176,9 +149,6 @@
/* The size of `signed long long', as computed by sizeof. */
#define SIZEOF_SIGNED_LONG_LONG @SIZEOF_SIGNED_LONG_LONG@
/* The size of `signed short', as computed by sizeof. */
#define SIZEOF_SIGNED_SHORT @SIZEOF_SIGNED_SHORT@
/* The size of `unsigned char *', as computed by sizeof. */
#define SIZEOF_UNSIGNED_CHAR_P @SIZEOF_UNSIGNED_CHAR_P@
@ -245,6 +215,9 @@
# endif
#endif
/* Number of bits in a file offset, on hosts where this is settable. */
#define _FILE_OFFSET_BITS @FILE_OFFSET_BITS@
/* Define to `__inline__' or `__inline' if that's what the C compiler
calls it, or to nothing if 'inline' is not supported under any name. */
#ifndef __cplusplus

@ -1,5 +1,3 @@
/* $Id: tif_dir.c,v 1.131 2017-07-11 21:38:04 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -863,14 +861,24 @@ _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
if( fip == NULL ) /* cannot happen since TIFFGetField() already checks it */
return 0;
/*
* We want to force the custom code to be used for custom
* fields even if the tag happens to match a well known
* one - important for reinterpreted handling of standard
* tag values in custom directories (i.e. EXIF)
*/
if (fip->field_bit == FIELD_CUSTOM) {
standard_tag = 0;
}
if( tag == TIFFTAG_NUMBEROFINKS )
if( standard_tag == TIFFTAG_NUMBEROFINKS )
{
int i;
for (i = 0; i < td->td_customValueCount; i++) {
uint16 val;
TIFFTagValue *tv = td->td_customValues + i;
if (tv->info->field_tag != tag)
if (tv->info->field_tag != standard_tag)
continue;
if( tv->value == NULL )
return 0;
@ -892,16 +900,6 @@ _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
return 0;
}
/*
* We want to force the custom code to be used for custom
* fields even if the tag happens to match a well known
* one - important for reinterpreted handling of standard
* tag values in custom directories (i.e. EXIF)
*/
if (fip->field_bit == FIELD_CUSTOM) {
standard_tag = 0;
}
switch (standard_tag) {
case TIFFTAG_SUBFILETYPE:
*va_arg(ap, uint32*) = td->td_subfiletype;
@ -1067,6 +1065,9 @@ _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
if (td->td_samplesperpixel - td->td_extrasamples > 1) {
*va_arg(ap, uint16**) = td->td_transferfunction[1];
*va_arg(ap, uint16**) = td->td_transferfunction[2];
} else {
*va_arg(ap, uint16**) = NULL;
*va_arg(ap, uint16**) = NULL;
}
break;
case TIFFTAG_REFERENCEBLACKWHITE:

@ -1,5 +1,3 @@
/* $Id: tif_dir.h,v 1.55 2017-06-01 12:44:04 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -26,6 +24,10 @@
#ifndef _TIFFDIR_
#define _TIFFDIR_
#include "tiff.h"
#include "tiffio.h"
/*
* ``Library-private'' Directory-related Definitions.
*/

@ -1,5 +1,3 @@
/* $Id: tif_dirinfo.c,v 1.127 2017-06-01 12:44:04 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -979,6 +977,8 @@ _TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag)
case TIFFTAG_CONSECUTIVEBADFAXLINES:
case TIFFTAG_GROUP3OPTIONS:
case TIFFTAG_GROUP4OPTIONS:
/* LERC */
case TIFFTAG_LERC_PARAMETERS:
break;
default:
return 1;
@ -1054,7 +1054,18 @@ _TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag)
if (tag == TIFFTAG_PREDICTOR)
return 1;
break;
case COMPRESSION_ZSTD:
if (tag == TIFFTAG_PREDICTOR)
return 1;
break;
case COMPRESSION_LERC:
if (tag == TIFFTAG_LERC_PARAMETERS)
return 1;
break;
case COMPRESSION_WEBP:
if (tag == TIFFTAG_PREDICTOR)
return 1;
break;
}
return 0;
}

@ -1,5 +1,3 @@
/* $Id: tif_dirread.c,v 1.218 2017-09-09 21:44:42 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -167,6 +165,7 @@ static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uin
static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
static void ChopUpSingleUncompressedStrip(TIFF*);
static uint64 TIFFReadUInt64(const uint8 *value);
static int _TIFFGetMaxColorChannels(uint16 photometric);
static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount );
@ -776,7 +775,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* di
static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(
TIFF* tif, uint64 offset, tmsize_t size, void** pdest)
{
#if SIZEOF_VOIDP == 8 || SIZEOF_SIZE_T == 8
#if SIZEOF_SIZE_T == 8
tmsize_t threshold = INITIAL_THRESHOLD;
#endif
tmsize_t already_read = 0;
@ -797,7 +796,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(
void* new_dest;
tmsize_t bytes_read;
tmsize_t to_read = size - already_read;
#if SIZEOF_VOIDP == 8 || SIZEOF_SIZE_T == 8
#if SIZEOF_SIZE_T == 8
if( to_read >= threshold && threshold < MAX_THRESHOLD )
{
to_read = threshold;
@ -3506,6 +3505,35 @@ static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, c
}
}
/*
* Return the maximum number of color channels specified for a given photometric
* type. 0 is returned if photometric type isn't supported or no default value
* is defined by the specification.
*/
static int _TIFFGetMaxColorChannels( uint16 photometric )
{
switch (photometric) {
case PHOTOMETRIC_PALETTE:
case PHOTOMETRIC_MINISWHITE:
case PHOTOMETRIC_MINISBLACK:
return 1;
case PHOTOMETRIC_YCBCR:
case PHOTOMETRIC_RGB:
case PHOTOMETRIC_CIELAB:
case PHOTOMETRIC_LOGLUV:
case PHOTOMETRIC_ITULAB:
case PHOTOMETRIC_ICCLAB:
return 3;
case PHOTOMETRIC_SEPARATED:
case PHOTOMETRIC_MASK:
return 4;
case PHOTOMETRIC_LOGL:
case PHOTOMETRIC_CFA:
default:
return 0;
}
}
/*
* Read the next TIFF directory from a file and convert it to the internal
* format. We read directories sequentially.
@ -3522,6 +3550,7 @@ TIFFReadDirectory(TIFF* tif)
uint32 fii=FAILED_FII;
toff_t nextdiroff;
int bitspersample_read = FALSE;
int color_channels;
tif->tif_diroff=tif->tif_nextdiroff;
if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
@ -4026,6 +4055,37 @@ TIFFReadDirectory(TIFF* tif)
}
}
}
/*
* Make sure all non-color channels are extrasamples.
* If it's not the case, define them as such.
*/
color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
if (color_channels && tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > color_channels) {
uint16 old_extrasamples;
uint16 *new_sampleinfo;
TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric type-related "
"color channels and ExtraSamples doesn't match SamplesPerPixel. "
"Defining non-color channels as ExtraSamples.");
old_extrasamples = tif->tif_dir.td_extrasamples;
tif->tif_dir.td_extrasamples = (uint16) (tif->tif_dir.td_samplesperpixel - color_channels);
// sampleinfo should contain information relative to these new extra samples
new_sampleinfo = (uint16*) _TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16));
if (!new_sampleinfo) {
TIFFErrorExt(tif->tif_clientdata, module, "Failed to allocate memory for "
"temporary new sampleinfo array (%d 16 bit elements)",
tif->tif_dir.td_extrasamples);
goto bad;
}
memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, old_extrasamples * sizeof(uint16));
_TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, new_sampleinfo, tif->tif_dir.td_extrasamples);
_TIFFfree(new_sampleinfo);
}
/*
* Verify Palette image has a Colormap.
*/
@ -4881,17 +4941,18 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
err=TIFFReadDirEntryByteArray(tif,dp,&data);
if (err==TIFFReadDirEntryErrOk)
{
uint8* ma;
uint32 mb;
uint32 mb = 0;
int n;
ma=data;
mb=0;
while (mb<(uint32)dp->tdir_count)
if (data != NULL)
{
if (*ma==0)
break;
ma++;
mb++;
uint8* ma = data;
while (mb<(uint32)dp->tdir_count)
{
if (*ma==0)
break;
ma++;
mb++;
}
}
if (mb+1<(uint32)dp->tdir_count)
TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" contains null byte in value; value incorrectly truncated during reading due to implementation limitations",fip->field_name);
@ -5141,11 +5202,11 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
if (err==TIFFReadDirEntryErrOk)
{
int m;
if( dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
{
TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
data[dp->tdir_count-1] = '\0';
}
if( data != 0 && dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
{
TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
data[dp->tdir_count-1] = '\0';
}
m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
if (data!=0)
_TIFFfree(data);
@ -5318,11 +5379,11 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
if (err==TIFFReadDirEntryErrOk)
{
int m;
if( dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
{
TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
data[dp->tdir_count-1] = '\0';
}
if( data != 0 && dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
{
TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
data[dp->tdir_count-1] = '\0';
}
m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
if (data!=0)
_TIFFfree(data);
@ -5698,6 +5759,16 @@ ChopUpSingleUncompressedStrip(TIFF* tif)
if( nstrips == 0 )
return;
/* If we are going to allocate a lot of memory, make sure that the */
/* file is as big as needed */
if( tif->tif_mode == O_RDONLY &&
nstrips > 1000000 &&
(offset >= TIFFGetFileSize(tif) ||
stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)) )
{
return;
}
newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
"for chopped \"StripByteCounts\" array");
newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),

@ -1,5 +1,3 @@
/* $Id: tif_dirwrite.c,v 1.89 2017-08-23 13:33:42 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -697,8 +695,11 @@ TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
}
break;
default:
assert(0); /* we should never get here */
break;
TIFFErrorExt(tif->tif_clientdata,module,
"Cannot write tag %d (%s)",
TIFFFieldTag(o),
o->field_name ? o->field_name : "unknown");
goto bad;
}
}
}

@ -1,5 +1,3 @@
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dumpmode.c,v 1.15 2015-12-12 18:04:26 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_error.c,v 1.6 2017-07-04 12:54:42 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_extension.c,v 1.8 2015-12-06 11:13:43 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_fax3.c,v 1.81 2017-06-18 10:31:50 erouault Exp $ */
/*
* Copyright (c) 1990-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_fax3.h,v 1.13 2016-12-14 18:36:27 faxguy Exp $ */
/*
* Copyright (c) 1990-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_flush.c,v 1.9 2010-03-31 06:40:10 fwarmerdam Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_getimage.c,v 1.114 2017-11-17 20:21:00 erouault Exp $ */
/*
* Copyright (c) 1991-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_jbig.c,v 1.16 2017-06-26 15:20:00 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -53,17 +51,18 @@ static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
struct jbg_dec_state decoder;
int decodeStatus = 0;
unsigned char* pImage = NULL;
(void) size, (void) s;
unsigned long decodedSize;
(void) s;
if (isFillOrder(tif, tif->tif_dir.td_fillorder))
{
TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
TIFFReverseBits(tif->tif_rawcp, tif->tif_rawcc);
}
jbg_dec_init(&decoder);
#if defined(HAVE_JBG_NEWLEN)
jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
jbg_newlen(tif->tif_rawcp, (size_t)tif->tif_rawcc);
/*
* I do not check the return status of jbg_newlen because even if this
* function fails it does not necessarily mean that decoding the image
@ -76,8 +75,8 @@ static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
*/
#endif /* HAVE_JBG_NEWLEN */
decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawdata,
(size_t)tif->tif_rawdatasize, NULL);
decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawcp,
(size_t)tif->tif_rawcc, NULL);
if (JBG_EOK != decodeStatus)
{
/*
@ -98,9 +97,28 @@ static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
return 0;
}
decodedSize = jbg_dec_getsize(&decoder);
if( (tmsize_t)decodedSize < size )
{
TIFFWarningExt(tif->tif_clientdata, "JBIG",
"Only decoded %lu bytes, whereas %lu requested",
decodedSize, (unsigned long)size);
}
else if( (tmsize_t)decodedSize > size )
{
TIFFErrorExt(tif->tif_clientdata, "JBIG",
"Decoded %lu bytes, whereas %lu were requested",
decodedSize, (unsigned long)size);
jbg_dec_free(&decoder);
return 0;
}
pImage = jbg_dec_getimage(&decoder, 0);
_TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
_TIFFmemcpy(buffer, pImage, decodedSize);
jbg_dec_free(&decoder);
tif->tif_rawcp += tif->tif_rawcc;
tif->tif_rawcc = 0;
return 1;
}

@ -1,5 +1,3 @@
/* $Id: tif_jpeg.c,v 1.134 2017-10-17 19:04:47 erouault Exp $ */
/*
* Copyright (c) 1994-1997 Sam Leffler
* Copyright (c) 1994-1997 Silicon Graphics, Inc.
@ -27,9 +25,9 @@
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include "tiffiop.h"
#include <stdlib.h>
#include "tiffiop.h"
#ifdef JPEG_SUPPORT
/*
@ -76,7 +74,7 @@ int TIFFJPEGIsFullStripRequired_12(TIFF* tif);
"JPEGLib: JPEG parameter struct mismatch: library thinks size is 432,
caller expects 464"
For such users we wil fix the problem here. See install.doc file from
For such users we will fix the problem here. See install.doc file from
the JPEG library distribution for details.
*/

@ -1,5 +1,3 @@
/* $Id: tif_luv.c,v 1.49 2017-07-24 12:47:30 erouault Exp $ */
/*
* Copyright (c) 1997 Greg Ward Larson
* Copyright (c) 1997 Silicon Graphics, Inc.
@ -215,7 +213,7 @@ LogL16Decode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
bp = (unsigned char*) tif->tif_rawcp;
cc = tif->tif_rawcc;
/* get each byte string */
for (shft = 2*8; (shft -= 8) >= 0; ) {
for (shft = 8; shft >= 0; shft -=8) {
for (i = 0; i < npixels && cc > 0; ) {
if (*bp >= 128) { /* run */
if( cc < 2 )
@ -349,7 +347,7 @@ LogLuvDecode32(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
bp = (unsigned char*) tif->tif_rawcp;
cc = tif->tif_rawcc;
/* get each byte string */
for (shft = 4*8; (shft -= 8) >= 0; ) {
for (shft = 24; shft >= 0; shft -=8) {
for (i = 0; i < npixels && cc > 0; ) {
if (*bp >= 128) { /* run */
if( cc < 2 )
@ -467,7 +465,7 @@ LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
/* compress each byte string */
op = tif->tif_rawcp;
occ = tif->tif_rawdatasize - tif->tif_rawcc;
for (shft = 2*8; (shft -= 8) >= 0; )
for (shft = 8; shft >= 0; shft -=8) {
for (i = 0; i < npixels; i += rc) {
if (occ < 4) {
tif->tif_rawcp = op;
@ -522,6 +520,7 @@ LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
} else
rc = 0;
}
}
tif->tif_rawcp = op;
tif->tif_rawcc = tif->tif_rawdatasize - occ;
@ -618,7 +617,7 @@ LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
/* compress each byte string */
op = tif->tif_rawcp;
occ = tif->tif_rawdatasize - tif->tif_rawcc;
for (shft = 4*8; (shft -= 8) >= 0; )
for (shft = 24; shft >= 0; shft -=8) {
for (i = 0; i < npixels; i += rc) {
if (occ < 4) {
tif->tif_rawcp = op;
@ -673,6 +672,7 @@ LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
} else
rc = 0;
}
}
tif->tif_rawcp = op;
tif->tif_rawcc = tif->tif_rawdatasize - occ;

@ -1,5 +1,3 @@
/* $Id: tif_lzma.c,v 1.6 2016-09-17 09:18:59 erouault Exp $ */
/*
* Copyright (c) 2010, Andrey Kiselev <dron@ak4719.spb.edu>
*
@ -249,6 +247,7 @@ LZMAPreEncode(TIFF* tif, uint16 s)
{
static const char module[] = "LZMAPreEncode";
LZMAState *sp = EncoderState(tif);
lzma_ret ret;
(void) s;
assert(sp != NULL);
@ -262,7 +261,13 @@ LZMAPreEncode(TIFF* tif, uint16 s)
"Liblzma cannot deal with buffers this size");
return 0;
}
return (lzma_stream_encoder(&sp->stream, sp->filters, sp->check) == LZMA_OK);
ret = lzma_stream_encoder(&sp->stream, sp->filters, sp->check);
if (ret != LZMA_OK) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error in lzma_stream_encoder(): %s", LZMAStrerror(ret));
return 0;
}
return 1;
}
/*

@ -1,5 +1,3 @@
/* $Id: tif_lzw.c,v 1.57 2017-07-11 10:54:29 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -135,6 +133,7 @@ typedef struct {
long dec_restart; /* restart count */
#ifdef LZW_CHECKEOS
uint64 dec_bitsleft; /* available bits in raw data */
tmsize_t old_tif_rawcc; /* value of tif_rawcc at the end of the previous TIFLZWDecode() call */
#endif
decodeFunc dec_decode; /* regular or backwards compatible */
code_t* dec_codep; /* current recognized code */
@ -320,6 +319,7 @@ LZWPreDecode(TIFF* tif, uint16 s)
sp->dec_nbitsmask = MAXCODE(BITS_MIN);
#ifdef LZW_CHECKEOS
sp->dec_bitsleft = 0;
sp->old_tif_rawcc = 0;
#endif
sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
/*
@ -427,7 +427,7 @@ LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
bp = (unsigned char *)tif->tif_rawcp;
#ifdef LZW_CHECKEOS
sp->dec_bitsleft = (((uint64)tif->tif_rawcc) << 3);
sp->dec_bitsleft += (((uint64)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
#endif
nbits = sp->lzw_nbits;
nextdata = sp->lzw_nextdata;
@ -555,6 +555,9 @@ LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
tif->tif_rawcp = (uint8*) bp;
#ifdef LZW_CHECKEOS
sp->old_tif_rawcc = tif->tif_rawcc;
#endif
sp->lzw_nbits = (unsigned short) nbits;
sp->lzw_nextdata = nextdata;
sp->lzw_nextbits = nextbits;
@ -604,6 +607,7 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
char *tp;
unsigned char *bp;
int code, nbits;
int len;
long nextbits, nextdata, nbitsmask;
code_t *codep, *free_entp, *maxcodep, *oldcodep;
@ -657,7 +661,7 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
bp = (unsigned char *)tif->tif_rawcp;
#ifdef LZW_CHECKEOS
sp->dec_bitsleft = (((uint64)tif->tif_rawcc) << 3);
sp->dec_bitsleft += (((uint64)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
#endif
nbits = sp->lzw_nbits;
nextdata = sp->lzw_nextdata;
@ -755,13 +759,18 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
} while (--occ);
break;
}
assert(occ >= codep->length);
op += codep->length;
occ -= codep->length;
tp = op;
len = codep->length;
tp = op + len;
do {
*--tp = codep->value;
} while( (codep = codep->next) != NULL );
int t;
--tp;
t = codep->value;
codep = codep->next;
*tp = (char)t;
} while (codep && tp > op);
assert(occ >= len);
op += len;
occ -= len;
} else {
*op++ = (char)code;
occ--;
@ -770,6 +779,9 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
tif->tif_rawcp = (uint8*) bp;
#ifdef LZW_CHECKEOS
sp->old_tif_rawcc = tif->tif_rawcc;
#endif
sp->lzw_nbits = (unsigned short)nbits;
sp->lzw_nextdata = nextdata;
sp->lzw_nextbits = nextbits;

@ -1,5 +1,3 @@
/* $Id: tif_next.c,v 1.19 2016-09-04 21:32:56 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_ojpeg.c,v 1.69 2017-04-27 17:29:26 erouault 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

@ -1,5 +1,3 @@
/* $Id: tif_open.c,v 1.48 2016-11-20 22:29:47 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_packbits.c,v 1.26 2017-05-14 02:26:07 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_pixarlog.c,v 1.54 2017-07-10 10:40:28 erouault Exp $ */
/*
* Copyright (c) 1996-1997 Sam Leffler
* Copyright (c) 1996 Pixar

@ -1,5 +1,3 @@
/* $Id: tif_predict.c,v 1.44 2017-06-18 10:31:50 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_predict.h,v 1.9 2016-10-31 17:24:26 erouault Exp $ */
/*
* Copyright (c) 1995-1997 Sam Leffler
* Copyright (c) 1995-1997 Silicon Graphics, Inc.
@ -26,6 +24,10 @@
#ifndef _TIFFPREDICT_
#define _TIFFPREDICT_
#include "tiffio.h"
#include "tiffiop.h"
/*
* ``Library-private'' Support for the Predictor Tag
*/

@ -1,5 +1,3 @@
/* $Id: tif_print.c,v 1.65 2016-11-20 22:31:22 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -546,7 +544,7 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
uint16 i;
fprintf(fd, " %2ld: %5u",
l, td->td_transferfunction[0][l]);
for (i = 1; i < td->td_samplesperpixel; i++)
for (i = 1; i < td->td_samplesperpixel - td->td_extrasamples && i < 3; i++)
fprintf(fd, " %5u",
td->td_transferfunction[i][l]);
fputc('\n', fd);
@ -667,13 +665,13 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
fprintf(fd, " %3lu: [%8I64u, %8I64u]\n",
(unsigned long) s,
(unsigned __int64) td->td_stripoffset[s],
(unsigned __int64) td->td_stripbytecount[s]);
td->td_stripoffset ? (unsigned __int64) td->td_stripoffset[s] : 0,
td->td_stripbytecount ? (unsigned __int64) td->td_stripbytecount[s] : 0);
#else
fprintf(fd, " %3lu: [%8llu, %8llu]\n",
(unsigned long) s,
(unsigned long long) td->td_stripoffset[s],
(unsigned long long) td->td_stripbytecount[s]);
td->td_stripoffset ? (unsigned long long) td->td_stripoffset[s] : 0,
td->td_stripbytecount ? (unsigned long long) td->td_stripbytecount[s] : 0);
#endif
}
}

@ -1,5 +1,3 @@
/* $Id: tif_read.c,v 1.66 2017-11-17 20:21:00 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -58,7 +56,7 @@ static int TIFFReadAndRealloc( TIFF* tif, tmsize_t size,
int is_strip, uint32 strip_or_tile,
const char* module )
{
#if SIZEOF_VOIDP == 8 || SIZEOF_SIZE_T == 8
#if SIZEOF_SIZE_T == 8
tmsize_t threshold = INITIAL_THRESHOLD;
#endif
tmsize_t already_read = 0;
@ -73,7 +71,7 @@ static int TIFFReadAndRealloc( TIFF* tif, tmsize_t size,
{
tmsize_t bytes_read;
tmsize_t to_read = size - already_read;
#if SIZEOF_VOIDP == 8 || SIZEOF_SIZE_T == 8
#if SIZEOF_SIZE_T == 8
if( to_read >= threshold && threshold < MAX_THRESHOLD &&
already_read + to_read + rawdata_offset > tif->tif_rawdatasize )
{
@ -348,6 +346,13 @@ TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
return 0;
whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
|| isMapped(tif);
if( td->td_compression == COMPRESSION_LERC ||
td->td_compression == COMPRESSION_JBIG )
{
/* Ideally plugins should have a way to declare they don't support
* chunk strip */
whole_strip = 1;
}
#else
whole_strip = 1;
#endif

@ -1,26 +1,24 @@
/* $Id: tif_stream.cxx,v 1.11 2010-12-11 23:12:29 faxguy Exp $ */
/*
* Copyright (c) 1988-1996 Sam Leffler
* Copyright (c) 1991-1996 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS 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
* 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.
*/
@ -28,7 +26,6 @@
* TIFF Library UNIX-specific Routines.
*/
#include "tiffiop.h"
#include "tiffio.hxx"
#include <iostream>
#ifndef __VMS
@ -81,30 +78,30 @@ struct tiffos_data;
extern "C" {
static tmsize_t _tiffosReadProc(thandle_t, void*, tmsize_t);
static tmsize_t _tiffisReadProc(thandle_t fd, void* buf, tmsize_t size);
static tmsize_t _tiffosWriteProc(thandle_t fd, void* buf, tmsize_t size);
static tmsize_t _tiffisWriteProc(thandle_t, void*, tmsize_t);
static uint64 _tiffosSeekProc(thandle_t fd, uint64 off, int whence);
static uint64 _tiffisSeekProc(thandle_t fd, uint64 off, int whence);
static uint64 _tiffosSizeProc(thandle_t fd);
static uint64 _tiffisSizeProc(thandle_t fd);
static int _tiffosCloseProc(thandle_t fd);
static int _tiffisCloseProc(thandle_t fd);
static int _tiffDummyMapProc(thandle_t , void** base, toff_t* size );
static void _tiffDummyUnmapProc(thandle_t , void* base, toff_t size );
static TIFF* _tiffStreamOpen(const char* name, const char* mode, void *fd);
static tmsize_t _tiffosReadProc(thandle_t, void*, tmsize_t);
static tmsize_t _tiffisReadProc(thandle_t fd, void* buf, tmsize_t size);
static tmsize_t _tiffosWriteProc(thandle_t fd, void* buf, tmsize_t size);
static tmsize_t _tiffisWriteProc(thandle_t, void*, tmsize_t);
static uint64 _tiffosSeekProc(thandle_t fd, uint64 off, int whence);
static uint64 _tiffisSeekProc(thandle_t fd, uint64 off, int whence);
static uint64 _tiffosSizeProc(thandle_t fd);
static uint64 _tiffisSizeProc(thandle_t fd);
static int _tiffosCloseProc(thandle_t fd);
static int _tiffisCloseProc(thandle_t fd);
static int _tiffDummyMapProc(thandle_t , void** base, toff_t* size );
static void _tiffDummyUnmapProc(thandle_t , void* base, toff_t size );
static TIFF* _tiffStreamOpen(const char* name, const char* mode, void *fd);
struct tiffis_data
{
istream *stream;
istream *stream;
ios::pos_type start_pos;
};
struct tiffos_data
{
ostream *stream;
ios::pos_type start_pos;
ostream *stream;
ios::pos_type start_pos;
};
static tmsize_t
@ -131,222 +128,226 @@ _tiffisReadProc(thandle_t fd, void* buf, tmsize_t size)
static tmsize_t
_tiffosWriteProc(thandle_t fd, void* buf, tmsize_t size)
{
tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
ostream *os = data->stream;
ios::pos_type pos = os->tellp();
tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
ostream *os = data->stream;
ios::pos_type pos = os->tellp();
// Verify that type does not overflow.
streamsize request_size = size;
if (static_cast<tmsize_t>(request_size) != size)
return static_cast<tmsize_t>(-1);
os->write(reinterpret_cast<const char *>(buf), request_size);
os->write(reinterpret_cast<const char *>(buf), request_size);
return static_cast<tmsize_t>(os->tellp() - pos);
return static_cast<tmsize_t>(os->tellp() - pos);
}
static tmsize_t
_tiffisWriteProc(thandle_t, void*, tmsize_t)
{
return 0;
return 0;
}
static uint64
_tiffosSeekProc(thandle_t fd, uint64 off, int whence)
{
tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
ostream *os = data->stream;
// if the stream has already failed, don't do anything
if( os->fail() )
return static_cast<uint64>(-1);
switch(whence) {
case SEEK_SET:
{
// Compute 64-bit offset
uint64 new_offset = static_cast<uint64>(data->start_pos) + off;
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(new_offset);
if (static_cast<uint64>(offset) != new_offset)
return static_cast<uint64>(-1);
os->seekp(offset, ios::beg);
break;
}
case SEEK_CUR:
{
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
os->seekp(offset, ios::cur);
break;
}
case SEEK_END:
{
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
os->seekp(offset, ios::end);
break;
}
}
// Attempt to workaround problems with seeking past the end of the
// stream. ofstream doesn't have a problem with this but
// ostrstream/ostringstream does. In that situation, add intermediate
// '\0' characters.
if( os->fail() ) {
tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
ostream *os = data->stream;
// if the stream has already failed, don't do anything
if( os->fail() )
return static_cast<uint64>(-1);
switch(whence) {
case SEEK_SET:
{
// Compute 64-bit offset
uint64 new_offset = static_cast<uint64>(data->start_pos) + off;
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(new_offset);
if (static_cast<uint64>(offset) != new_offset)
return static_cast<uint64>(-1);
os->seekp(offset, ios::beg);
break;
}
case SEEK_CUR:
{
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
os->seekp(offset, ios::cur);
break;
}
case SEEK_END:
{
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
os->seekp(offset, ios::end);
break;
}
}
// Attempt to workaround problems with seeking past the end of the
// stream. ofstream doesn't have a problem with this but
// ostrstream/ostringstream does. In that situation, add intermediate
// '\0' characters.
if( os->fail() ) {
#ifdef __VMS
int old_state;
int old_state;
#else
ios::iostate old_state;
ios::iostate old_state;
#endif
ios::pos_type origin;
ios::pos_type origin;
old_state = os->rdstate();
// reset the fail bit or else tellp() won't work below
os->clear(os->rdstate() & ~ios::failbit);
switch( whence ) {
case SEEK_SET:
old_state = os->rdstate();
// reset the fail bit or else tellp() won't work below
os->clear(os->rdstate() & ~ios::failbit);
switch( whence ) {
case SEEK_SET:
default:
origin = data->start_pos;
break;
case SEEK_CUR:
origin = os->tellp();
break;
case SEEK_END:
os->seekp(0, ios::end);
origin = os->tellp();
break;
}
// restore original stream state
os->clear(old_state);
// only do something if desired seek position is valid
if( (static_cast<uint64>(origin) + off) > static_cast<uint64>(data->start_pos) ) {
uint64 num_fill;
// clear the fail bit
os->clear(os->rdstate() & ~ios::failbit);
// extend the stream to the expected size
os->seekp(0, ios::end);
num_fill = (static_cast<uint64>(origin)) + off - os->tellp();
for( uint64 i = 0; i < num_fill; i++ )
os->put('\0');
// retry the seek
os->seekp(static_cast<ios::off_type>(static_cast<uint64>(origin) + off), ios::beg);
}
}
return static_cast<uint64>(os->tellp());
origin = data->start_pos;
break;
case SEEK_CUR:
origin = os->tellp();
break;
case SEEK_END:
os->seekp(0, ios::end);
origin = os->tellp();
break;
}
// restore original stream state
os->clear(old_state);
// only do something if desired seek position is valid
if( (static_cast<uint64>(origin) + off) > static_cast<uint64>(data->start_pos) ) {
uint64 num_fill;
// clear the fail bit
os->clear(os->rdstate() & ~ios::failbit);
// extend the stream to the expected size
os->seekp(0, ios::end);
num_fill = (static_cast<uint64>(origin)) + off - os->tellp();
for( uint64 i = 0; i < num_fill; i++ )
os->put('\0');
// retry the seek
os->seekp(static_cast<ios::off_type>(static_cast<uint64>(origin) + off), ios::beg);
}
}
return static_cast<uint64>(os->tellp());
}
static uint64
_tiffisSeekProc(thandle_t fd, uint64 off, int whence)
{
tiffis_data *data = reinterpret_cast<tiffis_data *>(fd);
switch(whence) {
case SEEK_SET:
{
// Compute 64-bit offset
uint64 new_offset = static_cast<uint64>(data->start_pos) + off;
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(new_offset);
if (static_cast<uint64>(offset) != new_offset)
return static_cast<uint64>(-1);
data->stream->seekg(offset, ios::beg);
break;
}
case SEEK_CUR:
{
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
data->stream->seekg(offset, ios::cur);
break;
}
case SEEK_END:
{
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
data->stream->seekg(offset, ios::end);
break;
}
}
return (uint64) (data->stream->tellg() - data->start_pos);
tiffis_data *data = reinterpret_cast<tiffis_data *>(fd);
switch(whence) {
case SEEK_SET:
{
// Compute 64-bit offset
uint64 new_offset = static_cast<uint64>(data->start_pos) + off;
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(new_offset);
if (static_cast<uint64>(offset) != new_offset)
return static_cast<uint64>(-1);
data->stream->seekg(offset, ios::beg);
break;
}
case SEEK_CUR:
{
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
data->stream->seekg(offset, ios::cur);
break;
}
case SEEK_END:
{
// Verify that value does not overflow
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
data->stream->seekg(offset, ios::end);
break;
}
}
return (uint64) (data->stream->tellg() - data->start_pos);
}
static uint64
_tiffosSizeProc(thandle_t fd)
{
tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
ostream *os = data->stream;
ios::pos_type pos = os->tellp();
ios::pos_type len;
tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
ostream *os = data->stream;
ios::pos_type pos = os->tellp();
ios::pos_type len;
os->seekp(0, ios::end);
len = os->tellp();
os->seekp(pos);
os->seekp(0, ios::end);
len = os->tellp();
os->seekp(pos);
return (uint64) len;
return (uint64) len;
}
static uint64
_tiffisSizeProc(thandle_t fd)
{
tiffis_data *data = reinterpret_cast<tiffis_data *>(fd);
ios::pos_type pos = data->stream->tellg();
ios::pos_type len;
tiffis_data *data = reinterpret_cast<tiffis_data *>(fd);
ios::pos_type pos = data->stream->tellg();
ios::pos_type len;
data->stream->seekg(0, ios::end);
len = data->stream->tellg();
data->stream->seekg(pos);
data->stream->seekg(0, ios::end);
len = data->stream->tellg();
data->stream->seekg(pos);
return (uint64) len;
return (uint64) len;
}
static int
_tiffosCloseProc(thandle_t fd)
{
// Our stream was not allocated by us, so it shouldn't be closed by us.
delete reinterpret_cast<tiffos_data *>(fd);
return 0;
// Our stream was not allocated by us, so it shouldn't be closed by us.
delete reinterpret_cast<tiffos_data *>(fd);
return 0;
}
static int
_tiffisCloseProc(thandle_t fd)
{
// Our stream was not allocated by us, so it shouldn't be closed by us.
delete reinterpret_cast<tiffis_data *>(fd);
return 0;
// Our stream was not allocated by us, so it shouldn't be closed by us.
delete reinterpret_cast<tiffis_data *>(fd);
return 0;
}
static int
_tiffDummyMapProc(thandle_t , void** base, toff_t* size )
{
return (0);
(void) base;
(void) size;
return (0);
}
static void
_tiffDummyUnmapProc(thandle_t , void* base, toff_t size )
{
(void) base;
(void) size;
}
/*
@ -355,40 +356,46 @@ _tiffDummyUnmapProc(thandle_t , void* base, toff_t size )
static TIFF*
_tiffStreamOpen(const char* name, const char* mode, void *fd)
{
TIFF* tif;
TIFF* tif;
if( strchr(mode, 'w') ) {
tiffos_data *data = new tiffos_data;
data->stream = reinterpret_cast<ostream *>(fd);
data->start_pos = data->stream->tellp();
if( strchr(mode, 'w') ) {
tiffos_data *data = new tiffos_data;
data->stream = reinterpret_cast<ostream *>(fd);
data->start_pos = data->stream->tellp();
// Open for writing.
tif = TIFFClientOpen(name, mode,
reinterpret_cast<thandle_t>(data),
_tiffosReadProc,
// Open for writing.
tif = TIFFClientOpen(name, mode,
reinterpret_cast<thandle_t>(data),
_tiffosReadProc,
_tiffosWriteProc,
_tiffosSeekProc,
_tiffosSeekProc,
_tiffosCloseProc,
_tiffosSizeProc,
_tiffDummyMapProc,
_tiffosSizeProc,
_tiffDummyMapProc,
_tiffDummyUnmapProc);
} else {
tiffis_data *data = new tiffis_data;
data->stream = reinterpret_cast<istream *>(fd);
data->start_pos = data->stream->tellg();
// Open for reading.
tif = TIFFClientOpen(name, mode,
reinterpret_cast<thandle_t>(data),
_tiffisReadProc,
if (!tif) {
delete data;
}
} else {
tiffis_data *data = new tiffis_data;
data->stream = reinterpret_cast<istream *>(fd);
data->start_pos = data->stream->tellg();
// Open for reading.
tif = TIFFClientOpen(name, mode,
reinterpret_cast<thandle_t>(data),
_tiffisReadProc,
_tiffisWriteProc,
_tiffisSeekProc,
_tiffisSeekProc,
_tiffisCloseProc,
_tiffisSizeProc,
_tiffDummyMapProc,
_tiffisSizeProc,
_tiffDummyMapProc,
_tiffDummyUnmapProc);
}
if (!tif) {
delete data;
}
}
return (tif);
return (tif);
}
} /* extern "C" */
@ -396,31 +403,32 @@ _tiffStreamOpen(const char* name, const char* mode, void *fd)
TIFF*
TIFFStreamOpen(const char* name, ostream *os)
{
// If os is either a ostrstream or ostringstream, and has no data
// written to it yet, then tellp() will return -1 which will break us.
// We workaround this by writing out a dummy character and
// then seek back to the beginning.
if( !os->fail() && static_cast<int>(os->tellp()) < 0 ) {
*os << '\0';
os->seekp(0);
}
// NB: We don't support mapped files with streams so add 'm'
return _tiffStreamOpen(name, "wm", os);
// If os is either a ostrstream or ostringstream, and has no data
// written to it yet, then tellp() will return -1 which will break us.
// We workaround this by writing out a dummy character and
// then seek back to the beginning.
if( !os->fail() && static_cast<int>(os->tellp()) < 0 ) {
*os << '\0';
os->seekp(0);
}
// NB: We don't support mapped files with streams so add 'm'
return _tiffStreamOpen(name, "wm", os);
}
TIFF*
TIFFStreamOpen(const char* name, istream *is)
{
// NB: We don't support mapped files with streams so add 'm'
return _tiffStreamOpen(name, "rm", is);
// NB: We don't support mapped files with streams so add 'm'
return _tiffStreamOpen(name, "rm", is);
}
/* vim: set ts=8 sts=8 sw=8 noet: */
/*
Local Variables:
mode: c
indent-tabs-mode: true
c-basic-offset: 8
End:
*/
* Local Variables:
* mode: c
* c-basic-offset: 8
* fill-column: 78
* End:
*/

@ -1,5 +1,3 @@
/* $Id: tif_strip.c,v 1.38 2016-12-03 11:02:15 erouault Exp $ */
/*
* Copyright (c) 1991-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_swab.c,v 1.15 2017-06-08 16:39:50 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_thunder.c,v 1.13 2016-09-04 21:32:56 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_tile.c,v 1.24 2015-06-07 22:35:40 bfriesen Exp $ */
/*
* Copyright (c) 1991-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Id: tif_unix.c,v 1.28 2017-01-11 19:02:49 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -1,4 +1,3 @@
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_version.c,v 1.3 2010-03-10 18:56:49 bfriesen Exp $ */
/*
* Copyright (c) 1992-1997 Sam Leffler
* Copyright (c) 1992-1997 Silicon Graphics, Inc.

@ -1,5 +1,3 @@
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_warning.c,v 1.4 2017-07-04 12:54:42 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.

@ -0,0 +1,684 @@
/*
* Copyright (c) 2018, Mapbox
* Author: <norman.barker at mapbox.com>
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS 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.
*/
#include "tiffiop.h"
#ifdef WEBP_SUPPORT
/*
* TIFF Library.
*
* WEBP Compression Support
*
*/
#include "webp/decode.h"
#include "webp/encode.h"
#include <stdio.h>
#define LSTATE_INIT_DECODE 0x01
#define LSTATE_INIT_ENCODE 0x02
/*
* State block for each open TIFF
* file using WEBP compression/decompression.
*/
typedef struct {
uint16 nSamples; /* number of samples per pixel */
int lossless; /* lossy/lossless compression */
int quality_level; /* compression level */
WebPPicture sPicture; /* WebP Picture */
WebPConfig sEncoderConfig; /* WebP encoder config */
uint8* pBuffer; /* buffer to hold raw data on encoding */
unsigned int buffer_offset; /* current offset into the buffer */
unsigned int buffer_size;
WebPIDecoder* psDecoder; /* WebPIDecoder */
WebPDecBuffer sDecBuffer; /* Decoder buffer */
int last_y; /* Last row decoded */
int state; /* state flags */
TIFFVGetMethod vgetparent; /* super-class method */
TIFFVSetMethod vsetparent; /* super-class method */
} WebPState;
#define LState(tif) ((WebPState*) (tif)->tif_data)
#define DecoderState(tif) LState(tif)
#define EncoderState(tif) LState(tif)
static int TWebPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
static int TWebPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
static
int TWebPDatasetWriter(const uint8_t* data, size_t data_size,
const WebPPicture* const picture)
{
static const char module[] = "TWebPDatasetWriter";
TIFF* tif = (TIFF*)(picture->custom_ptr);
if ( (tif->tif_rawcc + (tmsize_t)data_size) > tif->tif_rawdatasize ) {
TIFFErrorExt(tif->tif_clientdata, module,
"Buffer too small by " TIFF_SIZE_FORMAT " bytes.",
(size_t) (tif->tif_rawcc + data_size - tif->tif_rawdatasize));
return 0;
} else {
_TIFFmemcpy(tif->tif_rawcp, data, data_size);
tif->tif_rawcc += data_size;
tif->tif_rawcp += data_size;
return 1;
}
}
/*
* Encode a chunk of pixels.
*/
static int
TWebPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
{
static const char module[] = "TWebPEncode";
WebPState *sp = EncoderState(tif);
(void) s;
assert(sp != NULL);
assert(sp->state == LSTATE_INIT_ENCODE);
if( (uint64)sp->buffer_offset +
(uint64)cc > sp->buffer_size )
{
TIFFErrorExt(tif->tif_clientdata, module,
"Too many bytes to be written");
return 0;
}
memcpy(sp->pBuffer + sp->buffer_offset,
bp, cc);
sp->buffer_offset += (unsigned)cc;
return 1;
}
static int
TWebPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
{
static const char module[] = "WebPDecode";
VP8StatusCode status = VP8_STATUS_OK;
WebPState *sp = DecoderState(tif);
(void) s;
assert(sp != NULL);
assert(sp->state == LSTATE_INIT_DECODE);
if (occ % sp->sDecBuffer.u.RGBA.stride)
{
TIFFErrorExt(tif->tif_clientdata, module,
"Fractional scanlines cannot be read");
return 0;
}
status = WebPIAppend(sp->psDecoder, tif->tif_rawcp, tif->tif_rawcc);
if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) {
if (status == VP8_STATUS_INVALID_PARAM) {
TIFFErrorExt(tif->tif_clientdata, module,
"Invalid parameter used.");
} else if (status == VP8_STATUS_OUT_OF_MEMORY) {
TIFFErrorExt(tif->tif_clientdata, module,
"Out of memory.");
} else {
TIFFErrorExt(tif->tif_clientdata, module,
"Unrecognized error.");
}
return 0;
} else {
int current_y, stride;
uint8_t* buf;
/* Returns the RGB/A image decoded so far */
buf = WebPIDecGetRGB(sp->psDecoder, &current_y, NULL, NULL, &stride);
if ((buf != NULL) &&
(occ <= stride * (current_y - sp->last_y))) {
memcpy(op,
buf + (sp->last_y * stride),
occ);
tif->tif_rawcp += tif->tif_rawcc;
tif->tif_rawcc = 0;
sp->last_y += occ / sp->sDecBuffer.u.RGBA.stride;
return 1;
} else {
TIFFErrorExt(tif->tif_clientdata, module, "Unable to decode WebP data.");
return 0;
}
}
}
static int
TWebPFixupTags(TIFF* tif)
{
(void) tif;
if (tif->tif_dir.td_planarconfig != PLANARCONFIG_CONTIG) {
static const char module[] = "TWebPFixupTags";
TIFFErrorExt(tif->tif_clientdata, module,
"TIFF WEBP requires data to be stored contiguously in RGB e.g. RGBRGBRGB "
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
"or RGBARGBARGBA"
#endif
);
return 0;
}
return 1;
}
static int
TWebPSetupDecode(TIFF* tif)
{
static const char module[] = "WebPSetupDecode";
uint16 nBitsPerSample = tif->tif_dir.td_bitspersample;
uint16 sampleFormat = tif->tif_dir.td_sampleformat;
WebPState* sp = DecoderState(tif);
assert(sp != NULL);
sp->nSamples = tif->tif_dir.td_samplesperpixel;
/* check band count */
if ( sp->nSamples != 3
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
&& sp->nSamples != 4
#endif
)
{
TIFFErrorExt(tif->tif_clientdata, module,
"WEBP driver doesn't support %d bands. Must be 3 (RGB) "
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
"or 4 (RGBA) "
#endif
"bands.",
sp->nSamples );
return 0;
}
/* check bits per sample and data type */
if ((nBitsPerSample != 8) && (sampleFormat != 1)) {
TIFFErrorExt(tif->tif_clientdata, module,
"WEBP driver requires 8 bit unsigned data");
return 0;
}
/* if we were last encoding, terminate this mode */
if (sp->state & LSTATE_INIT_ENCODE) {
WebPPictureFree(&sp->sPicture);
if (sp->pBuffer != NULL) {
_TIFFfree(sp->pBuffer);
sp->pBuffer = NULL;
}
sp->buffer_offset = 0;
sp->state = 0;
}
sp->state |= LSTATE_INIT_DECODE;
return 1;
}
/*
* Setup state for decoding a strip.
*/
static int
TWebPPreDecode(TIFF* tif, uint16 s)
{
static const char module[] = "TWebPPreDecode";
uint32 segment_width, segment_height;
WebPState* sp = DecoderState(tif);
TIFFDirectory* td = &tif->tif_dir;
(void) s;
assert(sp != NULL);
if (isTiled(tif)) {
segment_width = td->td_tilewidth;
segment_height = td->td_tilelength;
} else {
segment_width = td->td_imagewidth;
segment_height = td->td_imagelength - tif->tif_row;
if (segment_height > td->td_rowsperstrip)
segment_height = td->td_rowsperstrip;
}
if( (sp->state & LSTATE_INIT_DECODE) == 0 )
tif->tif_setupdecode(tif);
if (sp->psDecoder != NULL) {
WebPIDelete(sp->psDecoder);
WebPFreeDecBuffer(&sp->sDecBuffer);
sp->psDecoder = NULL;
}
sp->last_y = 0;
WebPInitDecBuffer(&sp->sDecBuffer);
sp->sDecBuffer.is_external_memory = 0;
sp->sDecBuffer.width = segment_width;
sp->sDecBuffer.height = segment_height;
sp->sDecBuffer.u.RGBA.stride = segment_width * sp->nSamples;
sp->sDecBuffer.u.RGBA.size = segment_width * sp->nSamples * segment_height;
if (sp->nSamples > 3) {
sp->sDecBuffer.colorspace = MODE_RGBA;
} else {
sp->sDecBuffer.colorspace = MODE_RGB;
}
sp->psDecoder = WebPINewDecoder(&sp->sDecBuffer);
if (sp->psDecoder == NULL) {
TIFFErrorExt(tif->tif_clientdata, module,
"Unable to allocate WebP decoder.");
return 0;
}
return 1;
}
static int
TWebPSetupEncode(TIFF* tif)
{
static const char module[] = "WebPSetupEncode";
uint16 nBitsPerSample = tif->tif_dir.td_bitspersample;
uint16 sampleFormat = tif->tif_dir.td_sampleformat;
WebPState* sp = EncoderState(tif);
assert(sp != NULL);
sp->nSamples = tif->tif_dir.td_samplesperpixel;
/* check band count */
if ( sp->nSamples != 3
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
&& sp->nSamples != 4
#endif
)
{
TIFFErrorExt(tif->tif_clientdata, module,
"WEBP driver doesn't support %d bands. Must be 3 (RGB) "
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
"or 4 (RGBA) "
#endif
"bands.",
sp->nSamples );
return 0;
}
/* check bits per sample and data type */
if ((nBitsPerSample != 8) && (sampleFormat != 1)) {
TIFFErrorExt(tif->tif_clientdata, module,
"WEBP driver requires 8 bit unsigned data");
return 0;
}
if (sp->state & LSTATE_INIT_DECODE) {
WebPIDelete(sp->psDecoder);
WebPFreeDecBuffer(&sp->sDecBuffer);
sp->psDecoder = NULL;
sp->last_y = 0;
sp->state = 0;
}
sp->state |= LSTATE_INIT_ENCODE;
if (!WebPConfigInitInternal(&sp->sEncoderConfig, WEBP_PRESET_DEFAULT,
sp->quality_level,
WEBP_ENCODER_ABI_VERSION)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error creating WebP encoder configuration.");
return 0;
}
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
sp->sEncoderConfig.lossless = sp->lossless;
#endif
if (!WebPValidateConfig(&sp->sEncoderConfig)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error with WebP encoder configuration.");
return 0;
}
if (!WebPPictureInit(&sp->sPicture)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error initializing WebP picture.");
return 0;
}
return 1;
}
/*
* Reset encoding state at the start of a strip.
*/
static int
TWebPPreEncode(TIFF* tif, uint16 s)
{
static const char module[] = "TWebPPreEncode";
uint32 segment_width, segment_height;
WebPState *sp = EncoderState(tif);
TIFFDirectory* td = &tif->tif_dir;
(void) s;
assert(sp != NULL);
if( sp->state != LSTATE_INIT_ENCODE )
tif->tif_setupencode(tif);
/*
* Set encoding parameters for this strip/tile.
*/
if (isTiled(tif)) {
segment_width = td->td_tilewidth;
segment_height = td->td_tilelength;
} else {
segment_width = td->td_imagewidth;
segment_height = td->td_imagelength - tif->tif_row;
if (segment_height > td->td_rowsperstrip)
segment_height = td->td_rowsperstrip;
}
if( segment_width > 16383 || segment_height > 16383 ) {
TIFFErrorExt(tif->tif_clientdata, module,
"WEBP maximum image dimensions are 16383 x 16383.");
return 0;
}
/* set up buffer for raw data */
/* given above check and that nSamples <= 4, buffer_size is <= 1 GB */
sp->buffer_size = segment_width * segment_height * sp->nSamples;
sp->pBuffer = _TIFFmalloc(sp->buffer_size);
if( !sp->pBuffer) {
TIFFErrorExt(tif->tif_clientdata, module, "Cannot allocate buffer");
return 0;
}
sp->buffer_offset = 0;
sp->sPicture.width = segment_width;
sp->sPicture.height = segment_height;
sp->sPicture.writer = TWebPDatasetWriter;
sp->sPicture.custom_ptr = tif;
return 1;
}
/*
* Finish off an encoded strip by flushing it.
*/
static int
TWebPPostEncode(TIFF* tif)
{
static const char module[] = "WebPPostEncode";
int64_t stride;
WebPState *sp = EncoderState(tif);
assert(sp != NULL);
assert(sp->state == LSTATE_INIT_ENCODE);
stride = (int64_t)sp->sPicture.width * sp->nSamples;
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
if (sp->nSamples == 4) {
if (!WebPPictureImportRGBA(&sp->sPicture, sp->pBuffer, (int)stride)) {
TIFFErrorExt(tif->tif_clientdata, module,
"WebPPictureImportRGBA() failed" );
return 0;
}
}
else
#endif
if (!WebPPictureImportRGB(&sp->sPicture, sp->pBuffer, (int)stride)) {
TIFFErrorExt(tif->tif_clientdata, module,
"WebPPictureImportRGB() failed");
return 0;
}
if (!WebPEncode(&sp->sEncoderConfig, &sp->sPicture)) {
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
const char* pszErrorMsg = NULL;
switch(sp->sPicture.error_code) {
case VP8_ENC_ERROR_OUT_OF_MEMORY:
pszErrorMsg = "Out of memory"; break;
case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
pszErrorMsg = "Out of memory while flushing bits"; break;
case VP8_ENC_ERROR_NULL_PARAMETER:
pszErrorMsg = "A pointer parameter is NULL"; break;
case VP8_ENC_ERROR_INVALID_CONFIGURATION:
pszErrorMsg = "Configuration is invalid"; break;
case VP8_ENC_ERROR_BAD_DIMENSION:
pszErrorMsg = "Picture has invalid width/height"; break;
case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
pszErrorMsg = "Partition is bigger than 512k. Try using less "
"SEGMENTS, or increase PARTITION_LIMIT value";
break;
case VP8_ENC_ERROR_PARTITION_OVERFLOW:
pszErrorMsg = "Partition is bigger than 16M";
break;
case VP8_ENC_ERROR_BAD_WRITE:
pszErrorMsg = "Error while fludshing bytes"; break;
case VP8_ENC_ERROR_FILE_TOO_BIG:
pszErrorMsg = "File is bigger than 4G"; break;
case VP8_ENC_ERROR_USER_ABORT:
pszErrorMsg = "User interrupted";
break;
default:
TIFFErrorExt(tif->tif_clientdata, module,
"WebPEncode returned an unknown error code: %d",
sp->sPicture.error_code);
pszErrorMsg = "Unknown WebP error type.";
break;
}
TIFFErrorExt(tif->tif_clientdata, module,
"WebPEncode() failed : %s", pszErrorMsg);
#else
TIFFErrorExt(tif->tif_clientdata, module,
"Error in WebPEncode()");
#endif
return 0;
}
sp->sPicture.custom_ptr = NULL;
if (!TIFFFlushData1(tif))
{
TIFFErrorExt(tif->tif_clientdata, module,
"Error flushing TIFF WebP encoder.");
return 0;
}
return 1;
}
static void
TWebPCleanup(TIFF* tif)
{
WebPState* sp = LState(tif);
assert(sp != 0);
tif->tif_tagmethods.vgetfield = sp->vgetparent;
tif->tif_tagmethods.vsetfield = sp->vsetparent;
if (sp->state & LSTATE_INIT_ENCODE) {
WebPPictureFree(&sp->sPicture);
}
if (sp->psDecoder != NULL) {
WebPIDelete(sp->psDecoder);
WebPFreeDecBuffer(&sp->sDecBuffer);
sp->psDecoder = NULL;
sp->last_y = 0;
}
if (sp->pBuffer != NULL) {
_TIFFfree(sp->pBuffer);
sp->pBuffer = NULL;
}
if (tif->tif_data) {
_TIFFfree(tif->tif_data);
tif->tif_data = NULL;
}
_TIFFSetDefaultCompressionState(tif);
}
static int
TWebPVSetField(TIFF* tif, uint32 tag, va_list ap)
{
static const char module[] = "WebPVSetField";
WebPState* sp = LState(tif);
switch (tag) {
case TIFFTAG_WEBP_LEVEL:
sp->quality_level = (int) va_arg(ap, int);
if( sp->quality_level <= 0 ||
sp->quality_level > 100.0f ) {
TIFFWarningExt(tif->tif_clientdata, module,
"WEBP_LEVEL should be between 1 and 100");
}
return 1;
case TIFFTAG_WEBP_LOSSLESS:
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
sp->lossless = va_arg(ap, int);
return 1;
#else
TIFFErrorExt(tif->tif_clientdata, module,
"Need to upgrade WEBP driver, this version doesn't support "
"lossless compression.");
return 0;
#endif
default:
return (*sp->vsetparent)(tif, tag, ap);
}
/*NOTREACHED*/
}
static int
TWebPVGetField(TIFF* tif, uint32 tag, va_list ap)
{
WebPState* sp = LState(tif);
switch (tag) {
case TIFFTAG_WEBP_LEVEL:
*va_arg(ap, int*) = sp->quality_level;
break;
case TIFFTAG_WEBP_LOSSLESS:
*va_arg(ap, int*) = sp->lossless;
break;
default:
return (*sp->vgetparent)(tif, tag, ap);
}
return 1;
}
static const TIFFField TWebPFields[] = {
{ TIFFTAG_WEBP_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
TIFF_SETGET_UNDEFINED,
FIELD_PSEUDO, TRUE, FALSE, "WEBP quality", NULL },
{ TIFFTAG_WEBP_LOSSLESS, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
TIFF_SETGET_UNDEFINED,
FIELD_PSEUDO, TRUE, FALSE, "WEBP lossless/lossy", NULL
},
};
int
TIFFInitWebP(TIFF* tif, int scheme)
{
static const char module[] = "TIFFInitWebP";
WebPState* sp;
assert( scheme == COMPRESSION_WEBP );
/*
* Merge codec-specific tag information.
*/
if ( !_TIFFMergeFields(tif, TWebPFields, TIFFArrayCount(TWebPFields)) ) {
TIFFErrorExt(tif->tif_clientdata, module,
"Merging WebP codec-specific tags failed");
return 0;
}
/*
* Allocate state block so tag methods have storage to record values.
*/
tif->tif_data = (uint8*) _TIFFmalloc(sizeof(WebPState));
if (tif->tif_data == NULL)
goto bad;
sp = LState(tif);
/*
* Override parent get/set field methods.
*/
sp->vgetparent = tif->tif_tagmethods.vgetfield;
tif->tif_tagmethods.vgetfield = TWebPVGetField; /* hook for codec tags */
sp->vsetparent = tif->tif_tagmethods.vsetfield;
tif->tif_tagmethods.vsetfield = TWebPVSetField; /* hook for codec tags */
/* Default values for codec-specific fields */
sp->quality_level = 75.0f; /* default comp. level */
sp->lossless = 0; /* default to false */
sp->state = 0;
sp->nSamples = 0;
sp->psDecoder = NULL;
sp->last_y = 0;
sp->buffer_offset = 0;
sp->pBuffer = NULL;
/*
* Install codec methods.
* Notes:
* encoderow is not supported
*/
tif->tif_fixuptags = TWebPFixupTags;
tif->tif_setupdecode = TWebPSetupDecode;
tif->tif_predecode = TWebPPreDecode;
tif->tif_decoderow = TWebPDecode;
tif->tif_decodestrip = TWebPDecode;
tif->tif_decodetile = TWebPDecode;
tif->tif_setupencode = TWebPSetupEncode;
tif->tif_preencode = TWebPPreEncode;
tif->tif_postencode = TWebPPostEncode;
tif->tif_encoderow = TWebPEncode;
tif->tif_encodestrip = TWebPEncode;
tif->tif_encodetile = TWebPEncode;
tif->tif_cleanup = TWebPCleanup;
return 1;
bad:
TIFFErrorExt(tif->tif_clientdata, module,
"No space for WebP state block");
return 0;
}
#endif /* WEBP_SUPPORT */

@ -1,5 +1,3 @@
/* $Id: tif_win32.c,v 1.42 2017-01-11 19:02:49 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -407,60 +405,21 @@ _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
static void
Win32WarningHandler(const char* module, const char* fmt, va_list ap)
{
#ifndef TIF_PLATFORM_CONSOLE
LPTSTR szTitle;
LPTSTR szTmp;
LPCTSTR szTitleText = "%s Warning";
LPCTSTR szDefaultModule = "LIBTIFF";
LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
SIZE_T nBufSize = (strlen(szTmpModule) +
strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
return;
sprintf(szTitle, szTitleText, szTmpModule);
szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
LocalFree(szTitle);
return;
#else
if (module != NULL)
fprintf(stderr, "%s: ", module);
fprintf(stderr, "Warning, ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, ".\n");
#endif
}
TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
static void
Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
{
#ifndef TIF_PLATFORM_CONSOLE
LPTSTR szTitle;
LPTSTR szTmp;
LPCTSTR szTitleText = "%s Error";
LPCTSTR szDefaultModule = "LIBTIFF";
LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
SIZE_T nBufSize = (strlen(szTmpModule) +
strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
return;
sprintf(szTitle, szTitleText, szTmpModule);
szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
LocalFree(szTitle);
return;
#else
if (module != NULL)
fprintf(stderr, "%s: ", module);
vfprintf(stderr, fmt, ap);
fprintf(stderr, ".\n");
#endif
}
TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;

@ -1,5 +1,3 @@
/* $Id: tif_write.c,v 1.46 2016-12-03 21:57:44 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -540,9 +538,11 @@ TIFFSetupStrips(TIFF* tif)
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
td->td_stripsperimage /= td->td_samplesperpixel;
td->td_stripoffset = (uint64 *)
_TIFFmalloc(td->td_nstrips * sizeof (uint64));
_TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
"for \"StripOffsets\" array");
td->td_stripbytecount = (uint64 *)
_TIFFmalloc(td->td_nstrips * sizeof (uint64));
_TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
"for \"StripByteCounts\" array");
if (td->td_stripoffset == NULL || td->td_stripbytecount == NULL)
return (0);
/*

@ -1,5 +1,3 @@
/* $Id: tif_zip.c,v 1.37 2017-05-10 15:21:16 erouault Exp $ */
/*
* Copyright (c) 1995-1997 Sam Leffler
* Copyright (c) 1995-1997 Silicon Graphics, Inc.

@ -0,0 +1,440 @@
/*
* Copyright (c) 2017, Planet Labs
* Author: <even.rouault at spatialys.com>
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS 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.
*/
#include "tiffiop.h"
#ifdef ZSTD_SUPPORT
/*
* TIFF Library.
*
* ZSTD Compression Support
*
*/
#include "tif_predict.h"
#include "zstd.h"
#include <stdio.h>
/*
* State block for each open TIFF file using ZSTD compression/decompression.
*/
typedef struct {
TIFFPredictorState predict;
ZSTD_DStream* dstream;
ZSTD_CStream* cstream;
int compression_level; /* compression level */
ZSTD_outBuffer out_buffer;
int state; /* state flags */
#define LSTATE_INIT_DECODE 0x01
#define LSTATE_INIT_ENCODE 0x02
TIFFVGetMethod vgetparent; /* super-class method */
TIFFVSetMethod vsetparent; /* super-class method */
} ZSTDState;
#define LState(tif) ((ZSTDState*) (tif)->tif_data)
#define DecoderState(tif) LState(tif)
#define EncoderState(tif) LState(tif)
static int ZSTDEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
static int ZSTDDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
static int
ZSTDFixupTags(TIFF* tif)
{
(void) tif;
return 1;
}
static int
ZSTDSetupDecode(TIFF* tif)
{
ZSTDState* sp = DecoderState(tif);
assert(sp != NULL);
/* if we were last encoding, terminate this mode */
if (sp->state & LSTATE_INIT_ENCODE) {
ZSTD_freeCStream(sp->cstream);
sp->cstream = NULL;
sp->state = 0;
}
sp->state |= LSTATE_INIT_DECODE;
return 1;
}
/*
* Setup state for decoding a strip.
*/
static int
ZSTDPreDecode(TIFF* tif, uint16 s)
{
static const char module[] = "ZSTDPreDecode";
ZSTDState* sp = DecoderState(tif);
size_t zstd_ret;
(void) s;
assert(sp != NULL);
if( (sp->state & LSTATE_INIT_DECODE) == 0 )
tif->tif_setupdecode(tif);
if( sp->dstream )
{
ZSTD_freeDStream(sp->dstream);
sp->dstream = NULL;
}
sp->dstream = ZSTD_createDStream();
if( sp->dstream == NULL ) {
TIFFErrorExt(tif->tif_clientdata, module,
"Cannot allocate decompression stream");
return 0;
}
zstd_ret = ZSTD_initDStream(sp->dstream);
if( ZSTD_isError(zstd_ret) ) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error in ZSTD_initDStream(): %s",
ZSTD_getErrorName(zstd_ret));
return 0;
}
return 1;
}
static int
ZSTDDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
{
static const char module[] = "ZSTDDecode";
ZSTDState* sp = DecoderState(tif);
ZSTD_inBuffer in_buffer;
ZSTD_outBuffer out_buffer;
size_t zstd_ret;
(void) s;
assert(sp != NULL);
assert(sp->state == LSTATE_INIT_DECODE);
in_buffer.src = tif->tif_rawcp;
in_buffer.size = (size_t) tif->tif_rawcc;
in_buffer.pos = 0;
out_buffer.dst = op;
out_buffer.size = (size_t) occ;
out_buffer.pos = 0;
do {
zstd_ret = ZSTD_decompressStream(sp->dstream, &out_buffer,
&in_buffer);
if( ZSTD_isError(zstd_ret) ) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error in ZSTD_decompressStream(): %s",
ZSTD_getErrorName(zstd_ret));
return 0;
}
} while( zstd_ret != 0 &&
in_buffer.pos < in_buffer.size &&
out_buffer.pos < out_buffer.size );
if (out_buffer.pos < (size_t)occ) {
TIFFErrorExt(tif->tif_clientdata, module,
"Not enough data at scanline %lu (short %lu bytes)",
(unsigned long) tif->tif_row,
(unsigned long) (size_t)occ - out_buffer.pos);
return 0;
}
tif->tif_rawcp += in_buffer.pos;
tif->tif_rawcc -= in_buffer.pos;
return 1;
}
static int
ZSTDSetupEncode(TIFF* tif)
{
ZSTDState* sp = EncoderState(tif);
assert(sp != NULL);
if (sp->state & LSTATE_INIT_DECODE) {
ZSTD_freeDStream(sp->dstream);
sp->dstream = NULL;
sp->state = 0;
}
sp->state |= LSTATE_INIT_ENCODE;
return 1;
}
/*
* Reset encoding state at the start of a strip.
*/
static int
ZSTDPreEncode(TIFF* tif, uint16 s)
{
static const char module[] = "ZSTDPreEncode";
ZSTDState *sp = EncoderState(tif);
size_t zstd_ret;
(void) s;
assert(sp != NULL);
if( sp->state != LSTATE_INIT_ENCODE )
tif->tif_setupencode(tif);
if (sp->cstream) {
ZSTD_freeCStream(sp->cstream);
sp->cstream = NULL;
}
sp->cstream = ZSTD_createCStream();
if( sp->cstream == NULL ) {
TIFFErrorExt(tif->tif_clientdata, module,
"Cannot allocate compression stream");
return 0;
}
zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level);
if( ZSTD_isError(zstd_ret) ) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error in ZSTD_initCStream(): %s",
ZSTD_getErrorName(zstd_ret));
return 0;
}
sp->out_buffer.dst = tif->tif_rawdata;
sp->out_buffer.size = (size_t)tif->tif_rawdatasize;
sp->out_buffer.pos = 0;
return 1;
}
/*
* Encode a chunk of pixels.
*/
static int
ZSTDEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
{
static const char module[] = "ZSTDEncode";
ZSTDState *sp = EncoderState(tif);
ZSTD_inBuffer in_buffer;
size_t zstd_ret;
assert(sp != NULL);
assert(sp->state == LSTATE_INIT_ENCODE);
(void) s;
in_buffer.src = bp;
in_buffer.size = (size_t)cc;
in_buffer.pos = 0;
do {
zstd_ret = ZSTD_compressStream(sp->cstream, &sp->out_buffer,
&in_buffer);
if( ZSTD_isError(zstd_ret) ) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error in ZSTD_compressStream(): %s",
ZSTD_getErrorName(zstd_ret));
return 0;
}
if( sp->out_buffer.pos == sp->out_buffer.size ) {
tif->tif_rawcc = tif->tif_rawdatasize;
TIFFFlushData1(tif);
sp->out_buffer.dst = tif->tif_rawcp;
sp->out_buffer.pos = 0;
}
} while( in_buffer.pos < in_buffer.size );
return 1;
}
/*
* Finish off an encoded strip by flushing it.
*/
static int
ZSTDPostEncode(TIFF* tif)
{
static const char module[] = "ZSTDPostEncode";
ZSTDState *sp = EncoderState(tif);
size_t zstd_ret;
do {
zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer);
if( ZSTD_isError(zstd_ret) ) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error in ZSTD_endStream(): %s",
ZSTD_getErrorName(zstd_ret));
return 0;
}
if( sp->out_buffer.pos > 0 ) {
tif->tif_rawcc = sp->out_buffer.pos;
TIFFFlushData1(tif);
sp->out_buffer.dst = tif->tif_rawcp;
sp->out_buffer.pos = 0;
}
} while (zstd_ret != 0);
return 1;
}
static void
ZSTDCleanup(TIFF* tif)
{
ZSTDState* sp = LState(tif);
assert(sp != 0);
(void)TIFFPredictorCleanup(tif);
tif->tif_tagmethods.vgetfield = sp->vgetparent;
tif->tif_tagmethods.vsetfield = sp->vsetparent;
if (sp->dstream) {
ZSTD_freeDStream(sp->dstream);
sp->dstream = NULL;
}
if (sp->cstream) {
ZSTD_freeCStream(sp->cstream);
sp->cstream = NULL;
}
_TIFFfree(sp);
tif->tif_data = NULL;
_TIFFSetDefaultCompressionState(tif);
}
static int
ZSTDVSetField(TIFF* tif, uint32 tag, va_list ap)
{
static const char module[] = "ZSTDVSetField";
ZSTDState* sp = LState(tif);
switch (tag) {
case TIFFTAG_ZSTD_LEVEL:
sp->compression_level = (int) va_arg(ap, int);
if( sp->compression_level <= 0 ||
sp->compression_level > ZSTD_maxCLevel() )
{
TIFFWarningExt(tif->tif_clientdata, module,
"ZSTD_LEVEL should be between 1 and %d",
ZSTD_maxCLevel());
}
return 1;
default:
return (*sp->vsetparent)(tif, tag, ap);
}
/*NOTREACHED*/
}
static int
ZSTDVGetField(TIFF* tif, uint32 tag, va_list ap)
{
ZSTDState* sp = LState(tif);
switch (tag) {
case TIFFTAG_ZSTD_LEVEL:
*va_arg(ap, int*) = sp->compression_level;
break;
default:
return (*sp->vgetparent)(tif, tag, ap);
}
return 1;
}
static const TIFFField ZSTDFields[] = {
{ TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
TIFF_SETGET_UNDEFINED,
FIELD_PSEUDO, TRUE, FALSE, "ZSTD compression_level", NULL },
};
int
TIFFInitZSTD(TIFF* tif, int scheme)
{
static const char module[] = "TIFFInitZSTD";
ZSTDState* sp;
assert( scheme == COMPRESSION_ZSTD );
/*
* Merge codec-specific tag information.
*/
if (!_TIFFMergeFields(tif, ZSTDFields, TIFFArrayCount(ZSTDFields))) {
TIFFErrorExt(tif->tif_clientdata, module,
"Merging ZSTD codec-specific tags failed");
return 0;
}
/*
* Allocate state block so tag methods have storage to record values.
*/
tif->tif_data = (uint8*) _TIFFmalloc(sizeof(ZSTDState));
if (tif->tif_data == NULL)
goto bad;
sp = LState(tif);
/*
* Override parent get/set field methods.
*/
sp->vgetparent = tif->tif_tagmethods.vgetfield;
tif->tif_tagmethods.vgetfield = ZSTDVGetField; /* hook for codec tags */
sp->vsetparent = tif->tif_tagmethods.vsetfield;
tif->tif_tagmethods.vsetfield = ZSTDVSetField; /* hook for codec tags */
/* Default values for codec-specific fields */
sp->compression_level = 9; /* default comp. level */
sp->state = 0;
sp->dstream = 0;
sp->cstream = 0;
sp->out_buffer.dst = NULL;
sp->out_buffer.size = 0;
sp->out_buffer.pos = 0;
/*
* Install codec methods.
*/
tif->tif_fixuptags = ZSTDFixupTags;
tif->tif_setupdecode = ZSTDSetupDecode;
tif->tif_predecode = ZSTDPreDecode;
tif->tif_decoderow = ZSTDDecode;
tif->tif_decodestrip = ZSTDDecode;
tif->tif_decodetile = ZSTDDecode;
tif->tif_setupencode = ZSTDSetupEncode;
tif->tif_preencode = ZSTDPreEncode;
tif->tif_postencode = ZSTDPostEncode;
tif->tif_encoderow = ZSTDEncode;
tif->tif_encodestrip = ZSTDEncode;
tif->tif_encodetile = ZSTDEncode;
tif->tif_cleanup = ZSTDCleanup;
/*
* Setup predictor setup.
*/
(void) TIFFPredictorInit(tif);
return 1;
bad:
TIFFErrorExt(tif->tif_clientdata, module,
"No space for ZSTD state block");
return 0;
}
#endif /* ZSTD_SUPPORT */
/* vim: set ts=8 sts=8 sw=8 noet: */

@ -1,5 +1,3 @@
/* $Id: tiff.h,v 1.70 2016-01-23 21:20:34 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -189,7 +187,11 @@ typedef enum {
#define COMPRESSION_SGILOG 34676 /* SGI Log Luminance RLE */
#define COMPRESSION_SGILOG24 34677 /* SGI Log 24-bit packed */
#define COMPRESSION_JP2000 34712 /* Leadtools JPEG2000 */
#define COMPRESSION_LERC 34887 /* ESRI Lerc codec: https://github.com/Esri/lerc */
/* compression codes 34887-34889 are reserved for ESRI */
#define COMPRESSION_LZMA 34925 /* LZMA2 */
#define COMPRESSION_ZSTD 50000 /* ZSTD: WARNING not registered in Adobe-maintained registry */
#define COMPRESSION_WEBP 50001 /* WEBP: WARNING not registered in Adobe-maintained registry */
#define TIFFTAG_PHOTOMETRIC 262 /* photometric interpretation */
#define PHOTOMETRIC_MINISWHITE 0 /* min value is white */
#define PHOTOMETRIC_MINISBLACK 1 /* min value is black */
@ -450,6 +452,8 @@ typedef enum {
/* tag 34929 is a private tag registered to FedEx */
#define TIFFTAG_FEDEX_EDR 34929 /* unknown use */
#define TIFFTAG_INTEROPERABILITYIFD 40965 /* Pointer to Interoperability private directory */
/* tags 50674 to 50677 are reserved for ESRI */
#define TIFFTAG_LERC_PARAMETERS 50674 /* Stores LERC version and additional compression method */
/* Adobe Digital Negative (DNG) format tags */
#define TIFFTAG_DNGVERSION 50706 /* &DNG version number */
#define TIFFTAG_DNGBACKWARDVERSION 50707 /* &DNG compatibility version */
@ -603,6 +607,16 @@ typedef enum {
#define TIFFTAG_PERSAMPLE 65563 /* interface for per sample tags */
#define PERSAMPLE_MERGED 0 /* present as a single value */
#define PERSAMPLE_MULTI 1 /* present as multiple values */
#define TIFFTAG_ZSTD_LEVEL 65564 /* ZSTD compression level */
#define TIFFTAG_LERC_VERSION 65565 /* LERC version */
#define LERC_VERSION_2_4 4
#define TIFFTAG_LERC_ADD_COMPRESSION 65566 /* LERC additional compression */
#define LERC_ADD_COMPRESSION_NONE 0
#define LERC_ADD_COMPRESSION_DEFLATE 1
#define LERC_ADD_COMPRESSION_ZSTD 2
#define TIFFTAG_LERC_MAXZERROR 65567 /* LERC maximum error */
#define TIFFTAG_WEBP_LEVEL 65568 /* WebP compression level: WARNING not registered in Adobe-maintained registry */
#define TIFFTAG_WEBP_LOSSLESS 65569 /* WebP lossless/lossy : WARNING not registered in Adobe-maintained registry */
/*
* EXIF tags

@ -40,15 +40,6 @@
/* Pointer difference type */
#define TIFF_PTRDIFF_T @TIFF_PTRDIFF_T@
/* Define to 1 if the system has the type `int16'. */
#cmakedefine HAVE_INT16 1
/* Define to 1 if the system has the type `int32'. */
#cmakedefine HAVE_INT32 1
/* Define to 1 if the system has the type `int8'. */
#cmakedefine HAVE_INT8 1
/* Compatibility stuff. */
/* Define as 0 or 1 according to the floating point format suported by the

@ -1,5 +1,3 @@
/* $Id: tiffio.h,v 1.94 2017-01-11 19:02:49 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -52,7 +50,7 @@ typedef struct tiff TIFF;
* promoted type (i.e. one of int, unsigned int, pointer,
* or double) and because we defined pseudo-tags that are
* outside the range of legal Aldus-assigned tags.
* NB: tsize_t is int32 and not uint32 because some functions
* NB: tsize_t is signed and not unsigned because some functions
* return -1.
* NB: toff_t is not off_t for many reasons; TIFFs max out at
* 32-bit file offsets, and BigTIFF maxes out at 64-bit

@ -1,5 +1,3 @@
/* $Id: tiffio.hxx,v 1.3 2010-06-08 18:55:15 bfriesen Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -33,6 +31,7 @@
#include <iostream>
#include "tiff.h"
#include "tiffio.h"
extern TIFF* TIFFStreamOpen(const char*, std::ostream *);
extern TIFF* TIFFStreamOpen(const char*, std::istream *);

@ -1,5 +1,3 @@
/* $Id: tiffiop.h,v 1.95 2017-09-07 14:02:52 erouault Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
@ -72,6 +70,7 @@ extern int snprintf(char* str, size_t size, const char* format, ...);
#endif
#define streq(a,b) (strcmp(a,b) == 0)
#define strneq(a,b,n) (strncmp(a,b,n) == 0)
#ifndef TRUE
#define TRUE 1
@ -314,11 +313,15 @@ typedef size_t TIFFIOSize_t;
#define _TIFF_off_t off_t
#endif
#if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
#if defined(__has_attribute) && defined(__clang__)
#if __has_attribute(no_sanitize)
#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
#else
#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
#endif
#else
#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
#endif
#if defined(__cplusplus)
@ -424,6 +427,12 @@ extern int TIFFInitSGILog(TIFF*, int);
#ifdef LZMA_SUPPORT
extern int TIFFInitLZMA(TIFF*, int);
#endif
#ifdef ZSTD_SUPPORT
extern int TIFFInitZSTD(TIFF*, int);
#endif
#ifdef WEBP_SUPPORT
extern int TIFFInitWebP(TIFF*, int);
#endif
#ifdef VMS
extern const TIFFCodec _TIFFBuiltinCODECS[];
#else

@ -1,4 +1,4 @@
#define TIFFLIB_VERSION_STR "LIBTIFF, Version 4.0.9\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
#define TIFFLIB_VERSION_STR "LIBTIFF, Version 4.0.10\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 20171118
#define TIFFLIB_VERSION 20181110

Loading…
Cancel
Save