levigo / jbig2-imageio

A Java ImageIO plugin for the JBIG2 bi-level image format
Apache License 2.0
31 stars 19 forks source link

Slow performance of the decoder #2

Closed vishwas1384 closed 9 years ago

vishwas1384 commented 9 years ago

Hi,

I am trying to decode a jbig2 file to a png file. But the code is slower especially while reading the image. Could you suggest me performance improvements so that execution is faster.Currently it takes 485ms.I want it to be reduced to under 150ms. I am posting the code that I use to decode.

Iterator<?> readers = ImageIO.getImageReadersByFormatName("JBIG2"); //takes about 80ms

ImageReader reader = (ImageReader) readers.next(); Object source = fis; //reading the jbig2 file from the disk using FileInputStream fis ImageInputStream iis = ImageIO.createImageInputStream(source); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam();

Image image = reader.read(0, param); // takes about 150ms

BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);

Graphics2D g2 = bufferedImage.createGraphics(); g2.drawImage(image, null, null);

Eagerly waiting for your reply. Thank You

vishwas1384 commented 9 years ago

Hi, I was able to reduce execution time by half. Below is the modified code that gives the same output but at half the time.

JBIG2ImageReaderSpi image_spi = new JBIG2ImageReaderSpi(); JBIG2ImageReader reader = new JBIG2ImageReader(image_spi); Object source = fis; //fis is the file image input stream reader.setInput(source, true); ImageReadParam param = reader.getDefaultReadParam();//
BufferedImage image = reader.read(0, param); Graphics2D g2 = image.createGraphics(); g2.drawImage(image, null, null);

Still need to reduce the execution time, but require your help. Thanks.

ghost commented 9 years ago

The time required for decoding an image cannot be reduced to a constant threshold as it depends fully on the compressed image data size and complexity. The time reader.read(...) takes is the minimum your code will take. On top of that your code adds execution time. Feel free to optimize the decoder's internal machinery for a performance gain.

If you don't need the Graphics2D object for further operations, image already contains the decoded samples and need not be rendered into g2 a second time.

As your code shows the simple and usual way working with an ImageReader directly, there is no more you can do.