3

I have a program where I have to serialize a BufferedImage to disk and then read it back in. Currently I'm using a raw image format

ImageIO.getImageReadersByFormatName("raw");

After a lot of testing it seems it's performance varies wildly on the same data (from under a second to 15 seconds).
1. Any idea what is going on to cause this?
2. What is the fastest Image reader/writer in Java to accomplish this task?

Thanks

2 Answers 2

2
  1. It's hard to say what's causing such a huge difference in performance. There are all sorts of factors that could be contributing to it. See: How do I write a correct micro-benchmark in Java?
  2. This is also difficult to answer since it can depend on the data in the images, how fast your processor is and how fast your hard drive is. The only way to really know for your data set is to benchmark your different options.

That said, I'd imagine that png is a generally good choice to try out.

2
  • Would you posture that png is faster than raw?
    – Jon
    Apr 22, 2011 at 0:18
  • 3
    It might be. If you have large and highly compressible images with a fast enough processor, then the reduced disk activity might be worth that trade off. You could find that it's slower for writes and faster for reads, since it's generally more computationally intensive to do compression than decompression. It really depends on what you're dealing with. Apr 22, 2011 at 0:30
1

Unless you need interoperability outside your particular program, serializing via ImageIO can be a slow solution. It may be faster to read and write the BufferedImage's pixels directly. Here's a complete solution:

public class SerializedImage
    implements Serializable {

    private BufferedImage   mImage;

    public SerializedImage( BufferedImage image ) {

        mImage = image;
    }

    public BufferedImage getImage() {

        return mImage;
    }

    private void writeObject( ObjectOutputStream streamOut )
        throws IOException {

        streamOut.writeInt( mImage.getWidth() );
        streamOut.writeInt( mImage.getHeight() );
        streamOut.writeInt( mImage.getType() );
        streamOut.writeObject( ( (DataBufferInt) mImage.getRaster().getDataBuffer() ).getData() );
    }

    private void readObject( ObjectInputStream streamIn )
        throws IOException, ClassNotFoundException {

        mImage = new BufferedImage( streamIn.readInt(), streamIn.readInt(), streamIn.readInt() );

        int[] savedBuffer = (int[]) streamIn.readObject();
        int[] buffer = ( (DataBufferInt) mImage.getRaster().getDataBuffer() ).getData();

        System.arraycopy( savedBuffer, 0, buffer, 0, savedBuffer.length );
    }
}

UPDATE: @haraldK asked for some benchmarks. Like all micro-benchmarks, you should take these with a grain of salt. As @haraldK pointed out, if you're writing the serialized form out to disk, I/O probably becomes the limiting factor. Always test for your own use-case.

But a simple benchmark that does in-memory serializing/deserializing (to a byte array) is over 10x faster. This doesn't seem unreasonable, given that a) PNG compression is a non-trivial task; b) presumably the PNG compression has to at least read the pixel data, and then do a lot of processing on it, whereas SerializedImage just needs to read the pixel data and it's done.

Here's the code:

    // Create

    BufferedImage image = new BufferedImage( 100, 200, BufferedImage.TYPE_INT_ARGB );
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setColor( Color.red );
    graphics.fillRect( 1, 1, 50, 50 );
    SerializedImage serializedImage = new SerializedImage( image );

    long start = System.currentTimeMillis();

    for( int loop = 0; loop < 10_000; loop++ ) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        // Write

        try ( ObjectOutputStream objectOutputStream = new ObjectOutputStream( outputStream ) ) {
            objectOutputStream.writeObject( serializedImage );
        }

        // Read

        try ( ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream( outputStream.toByteArray() ) ) ) {
            ( (SerializedImage) objectInputStream.readObject() ).getImage();
        }
    }

    long end = System.currentTimeMillis() - start;
    System.out.println( "SerializedImage took " + end + "ms" );

    start = System.currentTimeMillis();

    for( int loop = 0; loop < 10_000; loop++ ) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        // Write

        ImageIO.write( image, "png", outputStream );

        // Read

        ImageIO.read( new ByteArrayInputStream( outputStream.toByteArray() ));
    }

    end = System.currentTimeMillis() - start;
    System.out.println( "ImageIO took " + end + "ms" );

Out of curiosity I tried this with FileOutputStream/FileInputStream instead. It's still faster than ImageIO, but only around 4x faster. Some of @haraldK's experiments (below) were only around 2x faster. So your mileage may vary.

3
  • Do you have any benchmarks to back up such a claim? ImageIO serializing and deserializing will be I/O-bound, just as your solution so any performance gain will likely be a lot smaller than the 10x range generally...
    – Harald K
    Sep 14, 2014 at 12:55
  • Just to clarify: I'm not saying the answer is useless. Just that the claim that this is faster, is probably false. Compare apples to apples. Use ImageIO with an uncompressed format like BMP, TIFF or PPM (these formats stores pixels very similar to just dumping the pixels to disk), and set ImageIO.setUseCache(false), and see if you still see a significant performance gain.
    – Harald K
    Sep 15, 2014 at 12:45
  • To prove I'm just making things up, I ran your benchmark and here's my results: Your original code: SerializedImage took 3765ms ImageIO took 57621ms With ImageIO.setUseCahce(false), still PNG format: SerializedImage took 4317ms ImageIO took 17705ms With ImageIO.setUseCahce(false), BMP format (default Sun/Oracle plugin) SerializedImage took 3824ms ImageIO took 5685ms Faster? Perhaps, but nowhere near the 10x range.
    – Harald K
    Sep 15, 2014 at 18:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.