Problem with colours when writing BufferedImage to file

I have made a little Java snippet to load some image files, combine them together into a single image, and write them to file. Now, when I display the BufferedImage that is internal to my application, it looks fine, very nice primary colours. But when I write to file and inspect the resulting jpg, I get a fairly weird set of colours. Does anyone know what the problem might be? Perhaps I need to specify what colour model I am using, to the ImageWriter object? I’m using the ImageIO.write(BufferedImage, String, OutputStream) method.

(Incidentally, has anyone else noticed how easy it is to miss the ‘f’ key and write BuggeredImage instead?)

maybe something is wrong with ur outputstream?

Well, this might not be very helpful, but I have this too. Writing in PNG format is ok, but in JPG the colors get an orange/pink shade over them. I just write RGB or ARGB BufferedImages, so I that is a pretty standard case.

Well some image quiality loss in jpg is to be excepected as its lossy compression so u dont get pixel perfect reproduction.

Eh… I didn’t think that a pink/orange pic was considered quality-loss.

Have you actually read the thread?

More information : I tried making a png (which indeed works) for comparison. Then I uploaded the png and jpg versions to Photobucket so people could see what the problem was. However, my browser refuses to display the jpeg version; I just get the little red cross. I find this exceedingly weird, since my windows Picture Viewer does display it, even if it is in weird colours.

The problem is with the image type of your bufferedimage, the ImageIO JPEG compressor needs a BufferedImage.TYPE_INT_RGB image. When you provide a BufferedImage.TYPE_INT_ABGR or BufferedImage.TYPE_INT_ARGB you will get the weird results you are seeing now. If you need to use ABGR or ARGB in your program, just create a new RGB bufferedimage and Graphics.drawImage() the ARGB/ABGR image on that and write the RGB image away.

Doh ;D :-[

There is no point in having A in a JPG anyway… Thanks.

Still a bit weird that ImageIO doesn’t take care of this automagicly.

Ah, that was it. Thanks. Oddly enough, though, I manage to run out of heap space when drawing to the Graphics object. Never seen that in a program without bugs. I had to scale my image by 0.5 before it would fit in memory. :-\

In case you don’t know yet, enlarge the maximum heap-space with -mx128M as vm-argument.