It

It's better to call Java drawImage() many times than once

Hacker News

It's better to call Java drawImage() many times than once

When copying or resizing between buffered images, doing multiple calls to drawImage() can be preferable to a single call on the destination image's graphics. The most obvious reason is speed-up with parallelization, using multiple destination graphics with exclusive clips, and one drawImage() call per graphics. This can cause slight inaccuracies due to how clipping is handled, and might actually slow things down, possibly a lot, depending on image types, for example due to locks in images internals or due to slower clipped images processing paths. With the most efficiently supported image types though, such as INT_XXX and BYTE_GRAY, it's often well worth it. Another reason is speed-up and/or accuracy improvement by using intermediary image types, because for some combinations of source and destination image types drawImage() can be very slow and/or inaccurate. For example, when copying from INT_ARGB_PRE to BYTE_BINARY, the resulting intensity is not appropriate due to a same coefficient being used for each R/G/B component. This can be mostly fixed by first copying to BYTE_GRAY, which also speeds things up. Adding intermediary INT_ARGB images on each side of the BYTE_GRAY image further increases the accuracy and the speed of the overall process. Another example is upscaling an image by just a factor 2 while converting from BYTE_ABGR_PRE to a CUSTOM type (like INT_ABGR_PRE): using INT_ARGB_PRE intermediary images as source and destination for the resize operation can speed up the overall process by a factor of 7 sequentially, or 20 in parallel on a 8-core when using NEAREST, or by a factor of 2 to 7 when using BICUBIC. A third reason is cache misses: when copying between multiple intermediary image types (which, as we saw, can be useful when converting to BYTE_BINARY), rather than copying the whole image to each new type, it's better to go through all the types slice per slice, for the pixels written at one step to still be in cache when being written into the next image. Maintaining slices through resizing should also be possible, but it would be more tricky and require enlarged source slices, as resizing algorithms typically use surrounding pixels. At this point it would probably be better instead to optimize drawImage() internals, although that would only apply to newer JDK versions. I made this library to take care of these kinds of optimizations: https://github.com/jeffhain/jimsizr It also implements BOXSAMPLED algorithm. Its API is similar to that of Thumbnailator's Resizer interface, in that it takes source and destination buffered images as arguments, so it is straightforward to implement Resizer on top of it. I also added support for parallelism in a Thumbnailator fork, by adding ThumbnailMaker.parallel(Executor), ParallelResizer interface and implementations of it for usual algorithms: https://github.com/jeffhain/thumbnailator That makes it on par with Jimsizr for speed and accuracy if you stick to standard INT_XXX images.

Share card

Actual performance

2points
Did not reach leaderboard

Launch Intel predictions

Analyze your own launch →
Indie HackersFits the IH revenue-focused audience · Strong signals: para, efficiently · Missing: supports, reddit linkedin, podcasting
74%74% predicted probability of success on Indie Hackers, based on ML models trained on real launch data.
best fitHighest predicted score across all platforms for this description.
AppSumoStrong fit for a featured deal · Strong signals: exclusive, interface, efficient · Missing: plus, platform, intuitive
57%57% predicted probability of success on AppSumo, based on ML models trained on real launch data.
TrustMRRFits verified-revenue profile · Strong signals: para · Missing: mobile apps, ios, personal
56%56% predicted probability of success on TrustMRR, based on ML models trained on real launch data.
Hacker NewsStrong engagement from HN community · Strong signals: ide, io · Missing: https docs, excited, just released
54%54% predicted probability of success on Hacker News, based on ML models trained on real launch data.
nativeThis product was originally launched on this platform.
Product HuntUnlikely to reach the leaderboard · Strong signals: new, single, using · Missing: mac, agents, macos
49%49% predicted probability of success on Product Hunt, based on ML models trained on real launch data.
Acquire.comPre-revenue stage for this audience · Missing: arr, mrr, revenue
14%14% predicted probability of success on Acquire.com, based on ML models trained on real launch data.
BetaListMay not resonate with beta-testers · Missing: web3, chat, crypto
0%0% predicted probability of success on BetaList, based on ML models trained on real launch data.

Incorrect prediction on native model

Similar products

Th
The Probability Times39%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

The Probability Times

Hacker News20
Ho
How many more times will you see your mother?40%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

How many more times will you see your mother?

Hacker News1
TimeCheck
TimeCheck22%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Find times when everyone is free

Indie Hackers1b2b
BroadBoard Times Square 🗽🌟
BroadBoard Times Square 🗽🌟46%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Get your product launch featured in Times Square 🗽🌟

Indie Hackers1advertising
Fi
Find the best times to commute42%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Find the best times to commute

Hacker News3
Ge
Get featured on a famous billboard in Times Square with 1 click55%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Get featured on a famous billboard in Times Square with 1 click

Hacker News3
The Hamburg Times
The Hamburg Times39%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Manage Your Own Newspaper

Product Hunt+20
SF
SFTransit - easy San Francisco transit times55%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

SFTransit - easy San Francisco transit times

Hacker News7
NotionExtensions
NotionExtensions52%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Your Notion workspace, 10 times better.

Indie Hackerscommitment-full-time
At
Atom – All Times, You Know46%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Atom – All Times, You Know

Hacker News3