libtiff: * libtiff/tif_dirread.c: Allow reading directories where

https://github.com/libsdl-org/libtiff/commit/4592a786cecc9d6fe6c9b7e7f3ca6504b9751925

From 4592a786cecc9d6fe6c9b7e7f3ca6504b9751925 Mon Sep 17 00:00:00 2001
From: Olivier Paquet <[EMAIL REDACTED]>
Date: Fri, 31 Dec 2010 16:12:40 +0000
Subject: [PATCH] * libtiff/tif_dirread.c: Allow reading directories where
 TIFFTAG_SMINSAMPLEVALUE and TIFFTAG_SMAXSAMPLEVALUE values differ for each
 channel. The min/max of all channels is used as appropriate.

---
 ChangeLog             |  6 ++++++
 libtiff/tif_dirread.c | 38 ++++++++++++++++++++++----------------
 2 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3f443998..4ddb920c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2010-12-31  Olivier Paquet  <olivier.paquet@gmail.com>
+
+	* libtiff/tif_dirread.c: Allow reading directories where
+	TIFFTAG_SMINSAMPLEVALUE and TIFFTAG_SMAXSAMPLEVALUE values differ for each
+	channel. The min/max of all channels is used as appropriate.
+
 2010-12-14  Lee Howard <faxguy@howardsilvan.com>
 
 	* libtiff/tif_dirread.c: tolerate some cases where
diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
index 9e8b9c5c..667a5d96 100644
--- a/libtiff/tif_dirread.c
+++ b/libtiff/tif_dirread.c
@@ -1,4 +1,4 @@
-/* $Id: tif_dirread.c,v 1.92.2.14 2010-12-15 01:04:34 faxguy Exp $ */
+/* $Id: tif_dirread.c,v 1.92.2.15 2010-12-31 16:12:40 olivier Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -54,7 +54,7 @@ static	float TIFFFetchRational(TIFF*, TIFFDirEntry*);
 static	int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*);
 static	int TIFFFetchPerSampleShorts(TIFF*, TIFFDirEntry*, uint16*);
 static	int TIFFFetchPerSampleLongs(TIFF*, TIFFDirEntry*, uint32*);
-static	int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*);
+static	int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*, double*);
 static	int TIFFFetchShortArray(TIFF*, TIFFDirEntry*, uint16*);
 static	int TIFFFetchStripThing(TIFF*, TIFFDirEntry*, long, uint32**);
 static	int TIFFFetchRefBlackWhite(TIFF*, TIFFDirEntry*);
@@ -480,11 +480,18 @@ TIFFReadDirectory(TIFF* tif)
 			}
 			break;
 		case TIFFTAG_SMINSAMPLEVALUE:
+			{
+				double minv = 0.0, maxv = 0.0;
+				if (!TIFFFetchPerSampleAnys(tif, dp, &minv, &maxv) ||
+				    !TIFFSetField(tif, dp->tdir_tag, minv))
+					goto bad;
+			}
+			break;
 		case TIFFTAG_SMAXSAMPLEVALUE:
 			{
-				double dv = 0.0;
-				if (!TIFFFetchPerSampleAnys(tif, dp, &dv) ||
-				    !TIFFSetField(tif, dp->tdir_tag, dv))
+				double minv = 0.0, maxv = 0.0;
+				if (!TIFFFetchPerSampleAnys(tif, dp, &minv, &maxv) ||
+				    !TIFFSetField(tif, dp->tdir_tag, maxv))
 					goto bad;
 			}
 			break;
@@ -1852,11 +1859,11 @@ TIFFFetchPerSampleLongs(TIFF* tif, TIFFDirEntry* dir, uint32* pl)
 }
 
 /*
- * Fetch samples/pixel ANY values for the specified tag and verify that all
- * values are the same.
+ * Fetch samples/pixel ANY values for the specified tag and returns their min
+ * and max.
  */
 static int
-TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* pl)
+TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* minv, double* maxv)
 {
     uint16 samples = tif->tif_dir.td_samplesperpixel;
     int status = 0;
@@ -1874,17 +1881,16 @@ TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* pl)
             if( samples < check_count )
                 check_count = samples;
 
+            *minv = *maxv = v[0];
             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;
-                }
-            *pl = v[0];
+            {
+                if (v[i] < *minv)
+                    *minv = v[i];
+                if (v[i] > *maxv)
+                    *maxv = v[i];
+            }
             status = 1;
         }
-      bad:
         if (v && v != buf)
             _TIFFfree(v);
     }