libtiff: avoid errors writing to last partial strip (#1981)

https://github.com/libsdl-org/libtiff/commit/9cb86fbad51fd0f06d052c1e522d91a263d9ab9b

From 9cb86fbad51fd0f06d052c1e522d91a263d9ab9b Mon Sep 17 00:00:00 2001
From: Frank Warmerdam <[EMAIL REDACTED]>
Date: Sun, 21 Dec 2008 20:24:51 +0000
Subject: [PATCH] avoid errors writing to last partial strip (#1981)

---
 ChangeLog          | 6 ++++++
 libtiff/tif_jpeg.c | 6 +++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 83e18802..5d009554 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2008-12-21  Frank Warmerdam  <warmerdam@pobox.com>
+
+	* libtiff/tif_jpeg.c: Avoid errors if the application writes a full
+	strip for the last partial strip in a jpeg compressed file.
+	http://bugzilla.maptools.org/show_bug.cgi?id=1981
+
 2008-12-21  Frank Warmerdam  <warmerdam@pobox.com>
 
 	* libtiff/tif_getimage.c, tiffio.h: More ABI corrections. 
diff --git a/libtiff/tif_jpeg.c b/libtiff/tif_jpeg.c
index c069b72c..0e95be66 100644
--- a/libtiff/tif_jpeg.c
+++ b/libtiff/tif_jpeg.c
@@ -1,4 +1,4 @@
-/* $Id: tif_jpeg.c,v 1.50.2.2 2007-04-07 14:58:30 dron Exp $ */
+/* $Id: tif_jpeg.c,v 1.50.2.3 2008-12-21 20:24:51 fwarmerdam Exp $ */
 
 /*
  * Copyright (c) 1994-1997 Sam Leffler
@@ -1425,6 +1425,10 @@ JPEGEncode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
 	if (cc % sp->bytesperline)
 		TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline discarded");
 
+        /* The last strip will be limited to image size */
+        if( !isTiled(tif) && tif->tif_row+nrows > tif->tif_dir.td_imagelength )
+            nrows = tif->tif_dir.td_imagelength - tif->tif_row;
+
 	while (nrows-- > 0) {
 		bufptr[0] = (JSAMPROW) buf;
 		if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)