libtiff: Merge branch 'default_tag_values_extended' into 'master'

From e70dee48f55884fd5405efd3b15418d44578f8a0 Mon Sep 17 00:00:00 2001
From: Su Laus <[EMAIL REDACTED]>
Date: Tue, 16 Aug 2022 07:04:19 +0000
Subject: [PATCH] Presetting of default tag values extended (e.g.
 PlanarConfig). (fixes #449)

---
 libtiff/tif_aux.c     | 17 ++++++++++++++++-
 libtiff/tif_dir.c     | 11 +++++++++++
 libtiff/tif_dirread.c |  2 ++
 libtiff/tif_write.c   | 16 ----------------
 4 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/libtiff/tif_aux.c b/libtiff/tif_aux.c
index 5b88c8d0..bfb68a7b 100644
--- a/libtiff/tif_aux.c
+++ b/libtiff/tif_aux.c
@@ -237,8 +237,23 @@ TIFFVGetFieldDefaulted(TIFF* tif, uint32_t tag, va_list ap)
 		*va_arg(ap, uint16_t *) = td->td_minsamplevalue;
 		return (1);
 	case TIFFTAG_MAXSAMPLEVALUE:
-		*va_arg(ap, uint16_t *) = td->td_maxsamplevalue;
+	{
+		uint16_t maxsamplevalue;
+		/* td_bitspersample=1 is always set in TIFFDefaultDirectory().
+		 * Therefore, td_maxsamplevalue has to be re-calculated in TIFFGetFieldDefaulted(). */
+		if (td->td_bitspersample > 0) {
+			/* This shift operation into a uint16_t limits the value to 65535 even if td_bitspersamle is > 16 */
+			if (td->td_bitspersample <= 16) {
+				maxsamplevalue = (1 << td->td_bitspersample) - 1;  /* 2**(BitsPerSample) - 1 */
+			} else {
+				maxsamplevalue = 65535;
+			}
+		} else {
+			maxsamplevalue = 0;
+		}
+		*va_arg(ap, uint16_t *) = maxsamplevalue;
 		return (1);
+	}
 	case TIFFTAG_PLANARCONFIG:
 		*va_arg(ap, uint16_t *) = td->td_planarconfig;
 		return (1);
diff --git a/libtiff/tif_dir.c b/libtiff/tif_dir.c
index 3c27d38b..793e8a79 100644
--- a/libtiff/tif_dir.c
+++ b/libtiff/tif_dir.c
@@ -1531,6 +1531,17 @@ TIFFDefaultDirectory(TIFF* tif)
 	tif->tif_tagmethods.vsetfield = _TIFFVSetField;  
 	tif->tif_tagmethods.vgetfield = _TIFFVGetField;
 	tif->tif_tagmethods.printdir = NULL;
+	/* additional default values */
+	td->td_planarconfig = PLANARCONFIG_CONTIG;
+	td->td_compression = COMPRESSION_NONE;
+	td->td_subfiletype = 0;
+	td->td_minsamplevalue = 0;
+	/* td_bitspersample=1 is always set in TIFFDefaultDirectory(). 
+	 * Therefore, td_maxsamplevalue has to be re-calculated in TIFFGetFieldDefaulted(). */
+	td->td_maxsamplevalue = 1;  /* Default for td_bitspersample=1 */
+	td->td_extrasamples = 0;
+	td->td_sampleinfo = NULL;
+
 	/*
 	 *  Give client code a chance to install their own
 	 *  tag extensions & methods, prior to compression overloads,
diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
index c6218829..8f3fc28d 100644
--- a/libtiff/tif_dirread.c
+++ b/libtiff/tif_dirread.c
@@ -3969,6 +3969,8 @@ TIFFReadDirectory(TIFF* tif)
 	 * without a PlanarConfiguration directory entry.
 	 * Thus we setup a default value here, even though
 	 * the TIFF spec says there is no default value.
+	 * After PlanarConfiguration is preset in TIFFDefaultDirectory()
+	 * the following setting is not needed, but does not harm either.
 	 */
 	TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
 	/*
diff --git a/libtiff/tif_write.c b/libtiff/tif_write.c
index 46e07763..3f72add4 100644
--- a/libtiff/tif_write.c
+++ b/libtiff/tif_write.c
@@ -602,22 +602,6 @@ TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
 		    "Must set \"ImageWidth\" before writing data");
 		return (0);
 	}
-	if (tif->tif_dir.td_samplesperpixel == 1) {
-		/*
-		 * Planarconfiguration is irrelevant in case of single band
-		 * images and need not be included. We will set it anyway,
-		 * because this field is used in other parts of library even
-		 * in the single band case.
-		 */
-		if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))
-                    tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
-	} else {
-		if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
-			TIFFErrorExt(tif->tif_clientdata, module,
-			    "Must set \"PlanarConfiguration\" before writing data");
-			return (0);
-		}
-	}
 	if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif)) {
 		tif->tif_dir.td_nstrips = 0;
 		TIFFErrorExt(tif->tif_clientdata, module, "No space for %s arrays",