Package: djview4
Version: 4.1.0+git191117-2

Attempting to do a PDF export of a color image from djview4 results in
a blank page, as displayed by the evince PDF viewer, and as reported
to me by someone whom I sent one of these bad PDF's.  I propose two
potential temporary fixes at the end (revert tiff2pdf.c or omit JPEG
SOI tokens).

I have observed the problem with Ubuntu for the past couple of years,
through the latest release (20.04).  Today I also observed the problem
on Debian Sid and Bullseye (all x86-64), and from the Debian git
sources at https://salsa.debian.org/debian/djview4.git.

The problem appears to be due to a longstanding libtiff bug discussed
at https://gitlab.com/libtiff/libtiff/-/issues/1 .  I am not that
knowledgeable about jpeg, tiff and pdf formats, but my impression is
that the problem is tiff2pdf.c incorrectly emits a separate "start of
image" (SOI) token (code 0xFF 0xD8) for each strip in a multi-strip
JPEG image, when it should emitting only one.  The plan of record
appears to be fix that bug after JPEG tile mode support is
implemented.

Because the problem is in tiff2pdf.c, it is also possible to produce
the problem by exporting a file to TIFF format instead of PDF, which
will produce a TIFF file that displays properly but which will result
in the same type of bad PDF when converted to PDF using the separate
tiff2pdf program.

I tried changing the strip size from 64 to the full image height in
djview4/src/qdjviewexporters.cpp in the function
QDjViewTiffExporter::doPage, at the line beginning with
"TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, ", but that still gave me a
blank page.

I am reporting the problem at the Debian level first rather than to
central djview4 development because the upstream djview4 uses an older
version of tiff2pdf.c that does not have this problem.  The Debian
packaging upgrades the tiff2pdf.c file to a version that has this bug
(libtiff 2.93 through the latest, 4.1.0).

I am aware of two possible temporary solutions that have worked so
far, but which I have only tested slightly.  Perhaps I should refer to
these options as workarounds, because I am not sure that either
generates files that fully conform to JPEG, but, if that is so, then
it is also probably the case that the current code makes similar
violations.  Anyhow, here are the possibilities I would like to
suggest::

1. Revert tiff2pdf.c.  The issue is that if someone went to the
trouble of upgrading it, I wonder what bug or bugs motivated that and
whether those bugs would return.

2. Commenting out the lines that emit the start-of-image tokens,
basically reverting the change in tiff2pdf.c that happened when
libtiff went from 2.9.2 to 2.9.3.  I include patch inline and as an
attachment, because I am not sure what the preferred way is for
submissions to bugs.debian.org.  The problem is that although I have
not gotten djview4 to have a problem with that change, doing the same
change to libtiff to generates a new tiff2pdf executable that still
has problems with tiff files that I generate with ImageMagick or
GraphicsMagick ("gm convert -compress JPEG foo.pnm foo.tiff") without
involving anything related to djvu, where the PDF is sometimes just
rendered as a top strip with the rest of the image blank or, as
before, a blank page.

Anyhow, here is the change to omit the SOI tokens.  Thanks in advance
for any attention to this bug report.

Adam

diff --git a/src/tiff2pdf.c b/src/tiff2pdf.c
index 9838c91..ab06145 100644
--- a/src/tiff2pdf.c
+++ b/src/tiff2pdf.c
@@ -3638,8 +3638,10 @@ int t2p_process_jpeg_strip(
                        case 0xd8:      /* SOI - start of image */
                 if( *bufferoffset + 2 > buffersize )
                     return(0);
+#if 0
                                _TIFFmemcpy(&(buffer[*bufferoffset]),
&(strip[i-1]), 2);
                                *bufferoffset+=2;
+#endif
                                break;
                        case 0xc0:      /* SOF0 */
                        case 0xc1:      /* SOF1 */
diff --git a/src/tiff2pdf.c b/src/tiff2pdf.c
index 9838c91..ab06145 100644
--- a/src/tiff2pdf.c
+++ b/src/tiff2pdf.c
@@ -3638,8 +3638,10 @@ int t2p_process_jpeg_strip(
 			case 0xd8:	/* SOI - start of image */
                 if( *bufferoffset + 2 > buffersize )
                     return(0);
+#if 0
 				_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), 2);
 				*bufferoffset+=2;
+#endif
 				break;
 			case 0xc0:	/* SOF0 */
 			case 0xc1:	/* SOF1 */

Reply via email to