views:

259

answers:

5

I have approx. 6000 PNG files (256*256 pixels) and want to combine them into a big PNG holding all of them programmatically.

What's the best/fastest way to do that?

(The purpose is printing on paper, so using some web-technology is not an option and having one, single picture file will eliminate many usage errors.)

I tried fahd's suggestion but I get a NullPointerException when I try to create a BufferedImage with 24576 pixels wide and 15360 pixels high. Any ideas?

+6  A: 

I do not see how it would be possible "without processing and re-encoding". If you insist on using Java then I just suggest you to use JAI (project page here). With that you would create one big BufferedImage, load smaller images and draw them on the bigger one.

Or just use ImageMagick.

Neeme Praks
+1 for ref to ImageMagik
Jayan
+3  A: 

The PNG format has no support for tiling, so there is no way you can escape at least decompressing and recompressing the data stream. If the palettes of all images are identical (or all absent), this is the only thing you really need to do. (I'm also assuming the images aren't interlaced.)

You could do this in a streaming way, only having open one "row" of PNGs at a time, reading appropriately-sized chunks from their data stream and writing them to the output stream. This way you would not need to keep entire images in memory. The most efficient way would be to program this on top of libpng yourself. You may need to keep slightly more than one scanline of pixels in memory because of the pixel prediction.

But just using the command-line utilities of ImageMagick, netpbm or similar will save you a large amount of development time for what may be little gain.

gpvos
+3  A: 

Create a large image which you will write to. Work out its dimensions based on how many rows and columns you want.

    BufferedImage result = new BufferedImage(
                               width, height, //work these out
                               BufferedImage.TYPE_INT_RGB);
    Graphics g = result.getGraphics();

Now loop through your images and draw them:

    for(String image : images){
        BufferedImage bi = ImageIO.read(new File(image));
        g.drawImage(bi, x, y, null);
        x += 256;
        if(x > result.getWidth()){
            x = 0;
            y += bi.getHeight();
        }
    }

Finally write it out to file:

    ImageIO.write(result,"png",new File("result.png"));
dogbane
A: 

You might be best off bouncing things off another (lossless) image format. PPM is dead easy to use (and to put tiles in programmatically; it's just a big array on disk, so you'll only have to store one row of tiles at most), but it's very wasteful of space (12 bytes per pixel!).

Then use a standard converter (e.g. ppm2png) that takes the intermediary format and turns it into the giant PNG.

Alex Feinman
+3  A: 

As others have pointed out, using Java is not necessarily the best bet here.

If you're going to use Java, your best bet--assuming you're sufficiently short on memory so that you can't read the entire dataset into memory multiple times and then write it out again--is to implement RenderedImage with a class that will read your PNGs off the disk upon demand. If you just create your own new BufferedImage and then try to write it out, the PNG writer will create an extra copy of the data. If you create your own RenderedImage, you can pass it to ImageIO.write(myImageSet,"png",myFileName). You can copy SampleModel and ColorModel information from your first PNG--hopefully they're all the same.

If you pretend that the entire image is multiple tiles (one tile per source image), then ImageIO.write will create a WritableRaster that is the size of the entire image data set, and will call your implementation of RenderedImage.copyData to fill it with data. If you have enough memory, this is an easy way to go (because you get a huge target set of data and can just dump all your image data into it--using the setRect(dx,dy,Raster) method--and then don't have to worry about it again). I haven't tested to see whether this saves memory, but it seems to me that it should.

Alternatively, if you pretend that the whole image is a single tile, ImageIO.write will then ask, using getTile(0,0), for the raster that corresponds to that entire image. So you have to create your own Raster, which in turn makes you create your own DataBuffer. When I tried this approach, the minimum memory usage that successfully wrote a 15360x25600 RGB PNG was -Xmx1700M (in Scala, incidentally), which is just barely over 4 bytes per pixel of written image, so there's very little overhead above one full image in memory.

The PNG data format itself is not one that requires the entire image in memory--it would work okay in chunks--but, sadly, the default implementation of the PNG writer assumes it will have the entire pixel array in memory.

Rex Kerr