Bug 703245 - GS fails to convert all the PDF pages to color TIFF/JPEG
Summary: GS fails to convert all the PDF pages to color TIFF/JPEG
Status: NOTIFIED FIXED
Alias: None
Product: Ghostscript
Classification: Unclassified
Component: PDF Interpreter (show other bugs)
Version: master
Hardware: PC Windows 10
: P2 normal
Assignee: Ray Johnston
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-09 18:59 UTC by Mike
Modified: 2021-02-18 06:29 UTC (History)
1 user (show)

See Also:
Customer: 631
Word Size: ---


Attachments
PDF file (8.08 MB, application/pdf)
2020-12-09 18:59 UTC, Mike
Details
Simplifies sample file (9.32 MB, application/pdf)
2020-12-09 20:14 UTC, Peter Cherepanov
Details
Bug703245.tif (3.74 MB, application/octet-stream)
2021-01-31 19:23 UTC, Ray Johnston
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mike 2020-12-09 18:59:45 UTC
Created attachment 20336 [details]
PDF file

When converting the attached PDF file to color TIFF or JPEG (tried it with tiff24nc and jpeg devices) only the first 12 pages are converted and gs exits with no error message. When converting to monochrome tiff (tiffg4) everything works just fine. It also works if I convert the PDF to PDF with pdfwrite device. 

I tried it with gs9.53 and gs master and got the same results. Everything works fine in gs9.08.
Comment 1 Peter Cherepanov 2020-12-09 20:14:23 UTC
Created attachment 20337 [details]
Simplifies sample file

This is a simplified and decompressed version of the sample file.

Debug build of Ghostscript shows the following error:
GPL Ghostscript GIT PRERELEASE 9.54.0: ./base/gxclrast.c(1065): bitmap size exceeds buffer!  width=1920 raster=5760 height=1
    file pos 202745 buf pos 45/4096

The file runs to completion with -dNOINTERPOLATE option.
Comment 2 Ray Johnston 2020-12-30 23:36:58 UTC
Since this seems to be clist related, I'll take it. :-(
Comment 3 Ray Johnston 2021-01-31 19:16:13 UTC
I cannot reproduce this error with:

 -sDEVICE=tiff24nc -o Bug703245.tif Bug703245.pdf

(or with -sDEVICE=jpeg)

I've attached the output I get from the tiff24nc file. Note the debugbin build
does not produce any errors or warnings as Peter mentioned.

Tested with 9.53.3 and with current HEAD.
Comment 4 Ray Johnston 2021-01-31 19:23:39 UTC
Created attachment 20532 [details]
Bug703245.tif

Output from HEAD (commit 5bbb4c7) with:

 -sDEVICE=tiff24nc -o Bug703245.tif -Z: -sCompression=lzw Bug703245.pdf
Comment 5 Peter Cherepanov 2021-02-02 09:14:47 UTC
Ray, I can reproduce the problem on Windows and on Linux, in debug and release builds, with the original and with the simplified files. Please retest.
Comment 6 Ken Sharp 2021-02-02 12:08:35 UTC
I tried this as well for another data point. For me, on Linux using the current HEAD (SHA c23d68c1e8da7a4e4134b143f147427092b3d2f2) and this command line:

gs -sDEVICE=tiff24nc -o out.tif 12.pdf

the debug build gives a debug message before failing with an error:

GPL Ghostscript GIT PRERELEASE 9.54.0: ./base/gxclrast.c(1069): bitmap size exceeds buffer!  width=1920 raster=5760 height=1
    file pos 202745 buf pos 45/4096
Error: /ioerror in --showpage--

A release build crashes with a floating point exception and dumps core.

On Windows, 64-bit build using VS 2008, the behaviour of the debug build is identical with that on Linux. A release build does produce a TIFF file without any errors or warnings, but the content is garbage.

On Windows, using this command line:

gswin64c -sDEVICE=jpeg -o out.jpg 12.pdf

the debug build issues the same warning as the tiff24nc device, but both the release and debug builds continue to completion and produce apparently correct JPEG output.

On Linux the command line 

gs -sDEVICE=jpeg -o out.jpg 12.pdf

and a debug build produces the same warning but emits an apparently correct JPEG file. The release build still crashes with a floating point exception.

Note that I have *only* used Peter's simplified single page file, not the original file supplied by Mike. As Peter noted, setting -dNOINTERPOLATE resolves the problem as well.
Comment 7 Ray Johnston 2021-02-05 23:13:35 UTC
Still having inconsistent results attempting to reproduce on Win 64 or 32
builds.

Since it is inconsistent, trying a valgrind build.
Comment 8 Ray Johnston 2021-02-13 00:07:25 UTC
Traced thus far (after interruptions) to colorspace interactions (yet more??)
with the pdf14 compositor. The data written for the copy_color is small enough
for the buffer size because it is calculated with a color depth of 8 bits.

When reading (for this band) the depth is 24 bits, which would not fit if
the width > 1360 (in this case the width is 1920).
Comment 9 Ray Johnston 2021-02-18 06:20:33 UTC
Fixed by commit 331fcb49b62a615976711cccdd3ff9796152a323

The pdf14 compositor changes the color depth to 8 for a SMask, but the
compositor actions were not being put in all the bands needed. In this
case the copy_color_alpha was output from the interpolation logic because
the image gridfitting expanded the image. Fix it by expanding the top and
bottom limits for the "temp_cropping" which controls the RECT loop writing
the compositor info to the bands.
Comment 10 Ray Johnston 2021-02-18 06:29:02 UTC
Assuming the customer is notified, since the customer submitted the bug.

The patch is:

diff --git a/base/gxclimag.c b/base/gxclimag.c
index 8d39831..4e21b55 100644
--- a/base/gxclimag.c
+++ b/base/gxclimag.c
@@ -1376,6 +1376,11 @@ clist_create_compositor(gx_device * dev,
         temp_cropping_min = cdev->cropping_min;
         temp_cropping_max = cdev->cropping_max;
     }
+    /* Adjust the lower and upper bound to allow for image gridfitting changing boundaries */
+    if (temp_cropping_min > 0)
+        temp_cropping_min--;
+    if (temp_cropping_max < dev->height - 1)
+        temp_cropping_max++;
     if (temp_cropping_min < temp_cropping_max) {
         /* The pdf14 compositor could be applied
            only to bands covered by the pcte->params.bbox. */