New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optimize applyMask in PdImageXObject #121
Conversation
optimized PDImageXObject: - applyMask(): faster alpha blending routines, avoid extra image allocation - scaleImage(): use a lower quality scaling algorithm if large image sizes are involved
fixed some formatting, also explicitly disabled interpolation on image format conversion "scaling".
Do you have a PDF where this large scaling happened? I found only one in my entire collection where the "largeScale switch is arbitrarily chosen" segment is hit, which is the second page of PDFBOX-2103, but this was a small image. |
Yes, you can download the PDF from here: https://archive.org/details/AlfaWaffenkatalog1911 |
Oops, you did mention that one in your mailing list post. OK, I'll need some time to look at all of this. |
I tested this one with the Alpha file (page 5) and all my files. Rendering of the Alfa file is definitively much faster. I intend to remove the "interpolate" block from scaleImage because the effect is almost invisible (but I would leave a comment). I did not have any difference in the rendering tests (done at 96 dpi), I tried at 300 dpi where I did a visual test and saw only meaningless differences, but it is a bit slower (with interpolation). Is there a reason that you'd like to keep that block? (besides that the block was there before). It comes from PDFBOX-4218, where I had corrected previous code (PDFBOX-2750) that always interpolated. I retested the file PDFBOX-2750 and it always looks ok. Even when trying the "worst" interpolation I can't get a bad rendering of the file from PDFBOX-2750. I was wondering if java itself was improved, but no, when running an old version PDFBox the rendering was still bad. So there were other changes that improved the quality of the scaling. |
forgot to remove debug message
I did some further testing, because I felt like the large scale test that the scaling must be bigger than factor 9 x 9 is pointless and only the target resolution is important, i.e. no matter how small the source image is, if the target is 10000x10000, it's going to be slow. And I was right. Here some tests, scaling up from 10, 50, 100, 500, 1000, 2000, 5000 and 10000 square to 10000:
What we can see is:
|
- Ensure that mask is 8 bit gray when doing alpha composition. - Scaling speed depends on destination size only, so largeScale switch will only depend on that, too.
#121 git-svn-id: https://svn.apache.org/repos/asf/pdfbox/branches/2.0@1891076 13f79535-47bb-0310-9956-ffa450edef68
Thanks, this has been committed in https://issues.apache.org/jira/browse/PDFBOX-5229 |
There was a severe performance issue with really big masks if the image needs to be scaled to it (i.e. 10000*10000 pixels). Scaling bicubic can take 6-10 seconds. This patch tries to switch to bilinear resizing for these cases, although the threshold might have to be fine tuned, still.
There was also a double allocation for the final masked image when we can simply use the image since applyMask() is always fed with a newly created one. Reference hogging and needless allocation have been removed.
Additionally the alpha blending routines were very slow, working on pixels. There is now a staggered approach by:
Additionally also using the interpolation flag of the mask to decide if the mask should be interpolated.