The from1bit() path passes a raster to colorSpace.toRGBImage(raster) where an RGB BufferedImage is created, which means a big memory footprint for scanned images.
I tried optimizing by using the raster to create smaller BufferedImages. Instead of calling colorSpace.toRGBImage(raster) where the raster would be copied into an RGB image, I did this:
Sadly, this resulted in a bigger memory footprint.
Lowest possible -Xmx setting to convert a file with 300dpi A4 scans: 76m
With the optimization: 123m
The stack trace suggests that java copies the image to an RGB image:
After I mentioned this on the dev mailing list, Petr Slaby replied:
your message caught my attention, so I could not resist to try and investigate it a little. I did not get too far and do not have the time to do any tests, but maybe at least a small hint. To at least have a chance that the sun java2d machinery draws the image without converting it first, BufferedImage.getType() must return something else than TYPE_CUSTOM. (At least I think so) For IndexColorModel, the raster has to be either BytePackedRaster or ByteComponentRaster. ByteComponentRaster resulting in BufferedImage type TYPE_BYTE_INDEXED is a safer bet.
So I looked at the source of BufferedImage and everything created by a user is TYPE_CUSTOM. Thus I tried using a TYPE_BYTE_BINARY image, but I got the same OOM stack trace suggesting a copying is taking place. I tried getting drawImage in the debugger but couldn't. But a look at the source code
shows at line 381 that java wants a "helper" and if there isn't, then it will convert to RGB / ARGB. And that is what's done according to the stack trace.
What I didn't search in the source code is what "helpers" would be available.
Then, in an act of desperation, I tried TYPE_BYTE_GRAY. This worked! It uses 1 byte per pixel, thus saves 2/3 of the RGB footprint, and the intermediate raster.
Minimal -Xmx setting got down to -Xmx26m.