As to this post, when converting image to PDF the resultsing PDF is invalid.
Please include A.jpg and A.pdf inside a zipfile or tarball (so
SourceForge does not re-write the files) and attach it to this problem
report so we can see what the problem is and attempt to recreate it.
It displays OK, however iText PDF library fails to parse it. The author of this library supposes that resulting PDF is not ISO-32000-1 compliant, in particular as stated in this post, the section
110obj<<endstreamendobj
is invalid.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The resulting pdf file is indeed malformed. The reason this does not cause a problem for most (all?) pdf previewers, is that the offending object (#11 in this example) is never referenced by any other object, so pdf viewers are unlikely to try to parse it. However, any program that parses entire pdf files, such as qpdf, will at least issue a warning and potentially fail. So I think the issue should be fixed. Fortunately, I think it is quite easy. My analysis is as follows. The problem is the function WritePDFImage in file coders/pdf.c.
This block will unconditionally start writing a new object, open a new dictionary by writing the initial <<, then conditionally write some stuff into the dictionary, end it with >>, start a stream, and write the stream contents. Then, once more unconditionally, it ends the stream and the object.
If the condition is true, this is no problem. But if the condition is false, it leads to unbalanced << and endstream tokens in the file.
What I think should happen, is that the entire object construction should be moved within the body of the if statement.
Moreover, if you look earlier in the code, you will find that the Colormap object will only be referenced if the innermost else of this nested statements is executed (irrelevant parts replaced by […]:
:::cif(image->colorspace==CMYKColorspace){[…]}else{if((compression==FaxCompression)||(compression==Group4Compression)||((image_info->type!=TrueColorType)&&(characteristics.grayscale))){[…]}else{if((image->storage_class==DirectClass)||(image->colors>256)||(compression==JPEGCompression))[…];elseFormatString(buffer,"[ /Indexed /DeviceRGB %u %lu 0 R ]\n",(unsignedint)image->colors-1,object+1);}}
It is not immediately clear that this is equivalent to the condition in the if statement in the “Write Colormap object” block. Perhaps it would be better to set a flag in the innermost else, and use it to test whether to create the Colormap block?
Finally, the object after the Colormap block just holds a length, and is referenced from within the Colormap object. Its creation could also be moved inside the conditional, in the interest of not creating unreferenced objects.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The resulting pdf file is indeed malformed. The reason this does not
cause a problem for most (all?) pdf previewers, is that the
offending object (#11 in this example) is never referenced by any
other object, so pdf viewers are unlikely to try to parse it.
However, any program that parses entire pdf files, such as qpdf,
will at least issue a warning and potentially fail. So I think the
issue should be fixed. Fortunately, I think it is quite easy. My
analysis is as follows. The problem is the function WritePDFImage
in file coders/pdf.c.
Would you be able to submit source code changes to solve this in the
form of a source code patch? The analysis is very helpful, but a
tested patch that I can easily apply is even more helpful.
I normally get my copy of gm via macports. Not sure how hard it is to build it outside the macports world, but I suppose I can give it a shot. (Or possibly, I can subvert the macports build system. There is an approved way to do that, but I have never tried it. Hmm.) I cannot promise to do it soon, however. Too many other pressing concerns.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
On Fri, 13 Oct 2017, Dmitry Katsubo wrote:
Please include A.jpg and A.pdf inside a zipfile or tarball (so
SourceForge does not re-write the files) and attach it to this problem
report so we can see what the problem is and attempt to recreate it.
Thanks,
Bob
Bob Friesenhahn
bfriesen@simple.dallas.tx.us, http://www.simplesystems.org/users/bfriesen/
GraphicsMagick Maintainer, http://www.GraphicsMagick.org/
Attaching files.
What causes you to believe that the PDF is invalid? It produces a
nice purple gradient when I open it with a couple of PDF viewers.
It displays OK, however iText PDF library fails to parse it. The author of this library supposes that resulting PDF is not ISO-32000-1 compliant, in particular as stated in this post, the section
is invalid.
I hit this old bug today. Here is a simple way to reproduce it:
The resulting pdf file is indeed malformed. The reason this does not cause a problem for most (all?) pdf previewers, is that the offending object (#11 in this example) is never referenced by any other object, so pdf viewers are unlikely to try to parse it. However, any program that parses entire pdf files, such as
qpdf, will at least issue a warning and potentially fail. So I think the issue should be fixed. Fortunately, I think it is quite easy. My analysis is as follows. The problem is the functionWritePDFImagein filecoders/pdf.c.This block will unconditionally start writing a new object, open a new dictionary by writing the initial
<<, then conditionally write some stuff into the dictionary, end it with>>, start a stream, and write the stream contents. Then, once more unconditionally, it ends the stream and the object.If the condition is true, this is no problem. But if the condition is false, it leads to unbalanced
<<andendstreamtokens in the file.What I think should happen, is that the entire object construction should be moved within the body of the
ifstatement.Moreover, if you look earlier in the code, you will find that the Colormap object will only be referenced if the innermost
elseof this nested statements is executed (irrelevant parts replaced by[…]:It is not immediately clear that this is equivalent to the condition in the
ifstatement in the “Write Colormap object” block. Perhaps it would be better to set a flag in the innermostelse, and use it to test whether to create the Colormap block?Finally, the object after the Colormap block just holds a length, and is referenced from within the Colormap object. Its creation could also be moved inside the conditional, in the interest of not creating unreferenced objects.
On Fri, 31 May 2019, Harald Hanche-Olsen wrote:
Would you be able to submit source code changes to solve this in the
form of a source code patch? The analysis is very helpful, but a
tested patch that I can easily apply is even more helpful.
Bob
Bob Friesenhahn
bfriesen@simple.dallas.tx.us, http://www.simplesystems.org/users/bfriesen/
GraphicsMagick Maintainer, http://www.GraphicsMagick.org/
Public Key, http://www.simplesystems.org/users/bfriesen/public-key.txt
I normally get my copy of gm via macports. Not sure how hard it is to build it outside the macports world, but I suppose I can give it a shot. (Or possibly, I can subvert the macports build system. There is an approved way to do that, but I have never tried it. Hmm.) I cannot promise to do it soon, however. Too many other pressing concerns.