libtiff: tiffcrop: fix issue #395: generation of strange section images.

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

From d1e3b382f1ea32a6192e4a67100f5702d87a001d Mon Sep 17 00:00:00 2001
From: Su Laus <[EMAIL REDACTED]>
Date: Tue, 8 Mar 2022 20:49:47 +0000
Subject: [PATCH] tiffcrop: fix issue #395: generation of strange section
 images.

---
 tools/tiffcrop.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
index f2e5474a..689436a8 100644
--- a/tools/tiffcrop.c
+++ b/tools/tiffcrop.c
@@ -5841,15 +5841,25 @@ computeOutputPixelOffsets (struct crop_mask *crop, struct image_data *image,
   if (orows < 1)
     orows = 1;
 
-  /* If user did not specify rows and cols, set them from calcuation */
-  if (page->rows < 1)
-    page->rows = orows;
-  if (page->cols < 1)
-    page->cols = ocols;
+  /* Always return rows and cols from calcuation above.
+   * (correct values needed external to this function)
+   * Warn, if user input settings has been changed.
+   */
+
+  if ((page->rows > 0) && (page->rows != orows)) {
+    TIFFError("computeOutputPixelOffsets",
+          "Number of user input section rows down (%"PRIu32") was changed to (%"PRIu32")", page->rows, orows);
+  }
+  page->rows = orows;
+  if ((page->cols > 0) && (page->cols != ocols)) {
+    TIFFError("computeOutputPixelOffsets",
+        "Number of user input section cols across (%"PRIu32") was changed to (%"PRIu32")", page->cols, ocols);
+  }
+  page->cols = ocols;
 
-  line_bytes = TIFFhowmany8(owidth * image->bps) * image->spp;
+  line_bytes = TIFFhowmany8(owidth * image->spp * image->bps);
 
-  if ((page->rows * page->cols) > MAX_SECTIONS)
+  if ((orows * ocols) > MAX_SECTIONS)
    {
    TIFFError("computeOutputPixelOffsets",
 	     "Rows and Columns exceed maximum sections\nIncrease resolution or reduce sections");
@@ -5857,13 +5867,13 @@ computeOutputPixelOffsets (struct crop_mask *crop, struct image_data *image,
    }
 
   /* build the list of offsets for each output section */
-  for (k = 0, i = 0 && k <= MAX_SECTIONS; i < orows; i++)
+  for (k = 0, i = 0; i < orows; i++)
     {
     y1 = (uint32_t)(olength * i);
     y2 = (uint32_t)(olength * (i + 1) - 1);
     if (y2 >= ilength)
       y2 = ilength - 1;
-    for (j = 0; j < ocols; j++, k++)
+    for (j = 0; (j < ocols) && (k < MAX_SECTIONS); j++, k++)
       {
       x1 = (uint32_t)(owidth * j);
       x2 = (uint32_t)(owidth * (j + 1) - 1);