Re: Using Magick++ to convert large PDFs into individual PNGs
Lukas Studer <[email protected]> Mon, 8 Mar 2021 15:46:21 +0100
| Newsgroups | gmane.comp.video.graphicsmagick.apis |
|---|---|
| Message-ID | <[email protected]> |
--===============3741594913980453840==
Content-Type: multipart/alternative;
boundary="------------8D29DEB012D5F747B73E8C02"
Content-Language: en-US
--------------8D29DEB012D5F747B73E8C02
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
SGkgQm9iLAoKCkkgbWFuYWdlZCB0byBnZXQgYSBjaHVua2VkLCBwYXJhbGxlbCBpbXBvcnQg
b2YgbGFyZ2UgUERGIGZpbGVzIHdvcmtpbmcKdXNpbmcgT3Blbk1QIGFuZCB0aGUgaW5kZXgt
b2YgbGlrZSBvcGVyYXRvciBpbiB0aGUgZmlsZSBuYW1lIHNwZWNpZmllcgphcyB5b3Ugc3Vn
Z2VzdGVkLgoKSSBhbHNvIG92ZXJsb2FkZWQgdGhlIHJlYWRJbWFnZXMgZnVuY3Rpb24gYWRk
aW5nIGEgcmVhZCBhcmd1bWVudHMgc3RydWN0CnRvIHNldCBzb21lIGF0dHJpYnV0ZXMgaW4g
dGhlIGltYWdlIGluZm8gc3RydWN0IChzZWUgY29kZSBiZWxvdykuIFVzaW5nCnRoaXMgSSBj
YW4gbm93IGFsc28gb3ZlcnJpZGUgdGhlIGRlbnNpdHkgZm9yIHRoZSBpbXBvcnQuIEkgbm90
aWNlZCB0aGlzCndhcyBhbHNvIGZsYWdnZWQgd2l0aCAiRklYTUUiLgoKdHlwZWRlZiBzdHJ1
Y3QgUmVhZEFyZ3MKe8KgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDC
oMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDC
oMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoArCoMKgwqDCoMKg
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2x1dGlvbiBzdWl0cyBteQo+PiBuZWVkcyBiZXN0Lgo+Cj4gSSBmb3Jnb3Qgb2Ygc29t
ZXRoaW5nIHdoaWNoIG1heSBoZWxwIHNvbHZlIHRoZSBwcm9ibGVtIGZvciB5b3UuwqAgVGhl
Cj4gc3RhcnRpbmcgcGFnZSBvZmZzZXQgYW5kIHJhbmdlIG1heSBiZSBzcGVjaWZpZWQgYXMg
cGFydCBvZiB0aGUgaW5wdXQKPiBmaWxlIG5hbWUuwqAgVGhpcyBtZWFucyB0aGF0IHlvdSBj
YW4gYWxyZWFkeSB1c2UgaXQgdG8gcmVxdWVzdCBhIHJhbmdlCj4gb2YgcGFnZXMgdXNpbmcg
dGhlIGV4aXN0aW5nIGltcGxlbWVudGF0aW9uLgo+Cj4gU28sIHlvdSBjYW4gcmVxdWVzdCAn
ZmlsZS5wZGZbMC05XScsIGFuZCB0aGVuICdmaWxlLnBkZlsxMC0xOV0nLCBldGMuCj4gaW4g
b3JkZXIgdG8ga2VlcCB0aGUgbnVtYmVyIG9mIHBhZ2VzIHJlcXVlc3RlZCBhdCBvbmUgdGlt
ZSBkb3duIHRvIGEKPiBtYW5hZ2VhYmxlIGxldmVsLgo+Cj4gQm9iCg==
--------------8D29DEB012D5F747B73E8C02
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 8bit
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi Bob,</p>
<p><br>
</p>
<p>I managed to get a chunked, parallel import of large PDF files
working using OpenMP and the index-of like operator in the file
name specifier as you suggested.</p>
<p>I also overloaded the <font size="+1"><tt>readImages</tt></font>
function adding a read arguments struct to set some attributes in
the image info struct (see code below). Using this I can now also
override the density for the import. I noticed this was also
flagged with "FIXME".<br>
</p>
<p><tt>typedef struct ReadArgs
{
</tt><tt><br>
</tt><tt> int subimage =
-1;
</tt><tt><br>
</tt><tt> int subrange = -1;</tt><tt><br>
</tt><tt> const std::string density = "";</tt><tt><br>
</tt><tt>} ReadArgs;</tt><tt><br>
</tt><tt><br>
</tt><tt>template <class Container></tt><tt><br>
</tt><tt>void readImages( Container *sequence_,</tt><tt><br>
</tt><tt> const std::string
&imageSpec_,
</tt><tt><br>
</tt><tt> ReadArgs args = {}) {</tt><tt><br>
</tt><tt> MagickLib::ImageInfo *imageInfo =
MagickLib::CloneImageInfo(0); </tt><tt><br>
</tt><tt><br>
</tt><tt> imageSpec_.copy( imageInfo->filename,
MaxTextExtent-1 ); </tt><tt><br>
</tt><tt> imageInfo->filename[ imageSpec_.length() ] =
0; </tt><tt><br>
</tt><tt><br>
</tt><tt> if (args.subimage >= 0)</tt><tt><br>
</tt><tt> imageInfo->subimage = args.subimage;</tt><tt><br>
</tt><tt><br>
</tt><tt> if (args.subrange >=
0)
</tt><tt><br>
</tt><tt> imageInfo->subrange = args.subrange;</tt><tt><br>
</tt><tt><br>
</tt><tt> if (args.density != "")
{ </tt><tt><br>
</tt><tt> imageInfo->density = (char *)
malloc(args.density.length()); </tt><tt><br>
</tt><tt> std::strcpy(imageInfo->density,
args.density.c_str());</tt><tt><br>
</tt><tt> imageInfo->density[
args.density.length() ] = 0;</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt><br>
</tt><tt> MagickLib::ExceptionInfo exceptionInfo;</tt><tt><br>
</tt><tt> MagickLib::GetExceptionInfo( &exceptionInfo
);</tt><tt><br>
</tt><tt> MagickLib::Image* images = MagickLib::ReadImage(
imageInfo, &exceptionInfo );</tt><tt><br>
</tt><tt> MagickLib::DestroyImageInfo(imageInfo);</tt><tt><br>
</tt><tt> insertImages( sequence_, images);</tt><tt><br>
</tt><tt> throwException( exceptionInfo );</tt><tt><br>
</tt><tt>}</tt><br>
<br>
</p>
<p>I did notice though that setting the "subimage" and "subrange"
attributes in ImageInfo was significantly slower than using the
file name specifier. Do you have any idea as to why this might be?<br>
</p>
<p>On a slightly different topic: It appears that the writing of PDF
files suffers the same limitations as the reading thereof. Writing
large documents causes the RAM to overflow crashing the
application. Do you have any insights on how to keep the RAM usage
in check while writing the PDF files? Is there a way to write them
in chunks? Further, I don't quite understand why this happens in
general. How is it that a PDF file of about 100MB can overflow my
RAM of about 64GB (when importing/exporting at 300 DPI). Is there
something I'm doing horribly wrong? Why is it bloated this much?<br>
</p>
<p><br>
</p>
<p>Thanks for your help!</p>
<p>Cheers,<br>
Lukas<br>
</p>
<div class="moz-cite-prefix">On 10.02.21 15:08, Bob Friesenhahn
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:[email protected]">On
Tue, 9 Feb 2021, Lukas Studer wrote:
<br>
<blockquote type="cite">
<br>
I will have a look at your suggestions and see which solution
suits my
<br>
needs best.
<br>
</blockquote>
<br>
I forgot of something which may help solve the problem for you.
The starting page offset and range may be specified as part of the
input file name. This means that you can already use it to
request a range of pages using the existing implementation.
<br>
<br>
So, you can request 'file.pdf[0-9]', and then 'file.pdf[10-19]',
etc. in order to keep the number of pages requested at one time
down to a manageable level.
<br>
<br>
Bob
<br>
</blockquote>
</body>
</html>
--------------8D29DEB012D5F747B73E8C02--
--===============3741594913980453840==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
--===============3741594913980453840==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Graphicsmagick-apis mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/graphicsmagick-apis
--===============3741594913980453840==--