libtiff: add -x switch to renumber pages to tiffcp (#2203

https://github.com/libsdl-org/libtiff/commit/ff6141d4921e6243e9e5f543a7c9224818d4f7ab

From ff6141d4921e6243e9e5f543a7c9224818d4f7ab Mon Sep 17 00:00:00 2001
From: Frank Warmerdam <[EMAIL REDACTED]>
Date: Fri, 4 Jun 2010 16:51:14 +0000
Subject: [PATCH] add -x switch to renumber pages to tiffcp (#2203

---
 ChangeLog              |  7 +++++++
 html/man/tiffcp.1.html | 14 ++++++++++++++
 man/tiffcp.1           |  5 ++++-
 tools/tiffcp.c         | 23 +++++++++++++++++------
 4 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index be9c63c7..432738cf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2010-06-03  Oliver Chen Feng <scip8183@gmail.com>
+
+        * libtiff/tools/tiffcp.c: add a new option -x to force merged tiff
+	file PAGENUMBER value in sequence for users who care the page
+	sequence, this will also prevent tiff2pdf from creating pdf file from
+	the merged tiff file with wrong page sequence.
+
 2010-05-07  Frank Warmerdam  <warmerdam@pobox.com>
 
 	* libtiff/tif_jpeg.c: Ensure that quality is always set in 
diff --git a/html/man/tiffcp.1.html b/html/man/tiffcp.1.html
index 2b30d5f1..c7e2b09e 100644
--- a/html/man/tiffcp.1.html
+++ b/html/man/tiffcp.1.html
@@ -341,6 +341,20 @@ <h2>OPTIONS</h2>
 </td>
 <td width="0%">
 </td>
+<tr valign="top" align="left">
+<td width="10%"></td>
+<td width="3%">
+
+<p><b>&minus;x</b></p>
+</td>
+<td width="5%"></td>
+<td width="80%">
+
+<p>Force the output file to be written with PAGENUMBER value
+in sequence.</p>
+</td>
+<td width="0%">
+</td>
 </table>
 <!-- INDENTATION -->
 <table width="100%" border=0 rules="none" frame="void"
diff --git a/man/tiffcp.1 b/man/tiffcp.1
index b1b40015..56f4ebe9 100644
--- a/man/tiffcp.1
+++ b/man/tiffcp.1
@@ -1,4 +1,4 @@
-.\" $Id: tiffcp.1,v 1.9 2007-02-24 11:19:33 dron Exp $
+.\" $Id: tiffcp.1,v 1.9.2.1 2010-06-04 16:51:14 fwarmerdam Exp $
 .\"
 .\" Copyright (c) 1988-1997 Sam Leffler
 .\" Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -210,6 +210,9 @@ appear in a tile.
 attempts to set the tile dimensions so that no more than 8 kilobytes of data
 appear in a tile.
 .TP
+.B \-x
+Force the output file to be written with PAGENUMBER value in sequence.
+.TP
 .BI \-,= character
 substitute
 .I character
diff --git a/tools/tiffcp.c b/tools/tiffcp.c
index 6e26a135..b0255df6 100644
--- a/tools/tiffcp.c
+++ b/tools/tiffcp.c
@@ -1,4 +1,4 @@
-/* $Id: tiffcp.c,v 1.37.2.1 2009-01-01 00:10:43 bfriesen Exp $ */
+/* $Id: tiffcp.c,v 1.37.2.2 2010-06-04 16:51:14 fwarmerdam Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -92,6 +92,7 @@ static	void usage(void);
 static char comma = ',';  /* (default) comma separator character */
 static TIFF* bias = NULL;
 static int pageNum = 0;
+static int pageInSeq = 0;
 
 static int nextSrcImage (TIFF *tif, char **imageSpec)
 /*
@@ -171,7 +172,7 @@ main(int argc, char* argv[])
 
 	*mp++ = 'w';
 	*mp = '\0';
-	while ((c = getopt(argc, argv, ",:b:c:f:l:o:z:p:r:w:aistBLMC")) != -1)
+	while ((c = getopt(argc, argv, ",:b:c:f:l:o:z:p:r:w:aistBLMCx")) != -1)
 		switch (c) {
                 case ',':
                         if (optarg[0] != '=') usage();
@@ -256,6 +257,9 @@ main(int argc, char* argv[])
 		case 'C':
 			*mp++ = 'c'; *mp = '\0';
 			break;
+		case 'x':
+			pageInSeq = 1;
+			break;
 		case '?':
 			usage();
 			/*NOTREACHED*/
@@ -725,12 +729,19 @@ tiffcp(TIFF* in, TIFF* out)
 	}
 	{
 	  unsigned short pg0, pg1;
-	  if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1)) {
-		if (pageNum < 0) /* only one input file */
+	  if(pageInSeq == 1) {
+	  	if (pageNum < 0) /* only one input file */ {
+		  if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1)) 
 			TIFFSetField(out, TIFFTAG_PAGENUMBER, pg0, pg1);
-		else 
+		} else
 			TIFFSetField(out, TIFFTAG_PAGENUMBER, pageNum++, 0);
-	  }
+	  } else
+		  if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1)) {
+			if (pageNum < 0) /* only one input file */
+				TIFFSetField(out, TIFFTAG_PAGENUMBER, pg0, pg1);
+			else 
+				TIFFSetField(out, TIFFTAG_PAGENUMBER, pageNum++, 0);
+		  }
 	}
 
 	for (p = tags; p < &tags[NTAGS]; p++)