I tried optimize SampledImageReader.from1Bit() not just for stencils but for all 1 bit images 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:

byte[] indexedValues = new byte[] { 0, (byte)0xFF };
ColorModel colorModel = new IndexColorModel(1, 2, indexedValues, indexedValues, indexedValues);
return new BufferedImage(colorModel, raster, false, null);

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:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.awt.image.DataBufferInt.<init>(Unknown Source)
    at java.awt.image.Raster.createPackedRaster(Unknown Source)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
    at java.awt.image.BufferedImage.<init>(Unknown Source)
    at sun.java2d.loops.GraphicsPrimitive.convertFrom(Unknown Source)
    at sun.java2d.loops.GraphicsPrimitive.convertFrom(Unknown Source)
    at sun.java2d.loops.MaskBlit$General.MaskBlit(Unknown Source)
    at sun.java2d.loops.Blit$GeneralMaskBlit.Blit(Unknown Source)
    at sun.java2d.pipe.DrawImage.blitSurfaceData(Unknown Source)
    at sun.java2d.pipe.DrawImage.renderImageCopy(Unknown Source)
    at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
    at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
    at sun.java2d.pipe.ValidatePipe.copyImage(Unknown Source)
    at sun.java2d.SunGraphics2D.copyImage(Unknown Source)
    at sun.java2d.pipe.DrawImage.makeBufferedImage(Unknown Source)
    at sun.java2d.pipe.DrawImage.renderImageXform(Unknown Source)
    at sun.java2d.pipe.DrawImage.transformImage(Unknown Source)
    at sun.java2d.pipe.DrawImage.transformImage(Unknown Source)
    at sun.java2d.pipe.DrawImage.transformImage(Unknown Source)
    at sun.java2d.pipe.ValidatePipe.transformImage(Unknown Source)
    at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at org.apache.pdfbox.rendering.PageDrawer.drawBufferedImage(PageDrawer.java:1007)


Tilman



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to