Problems with ImageIO.write()

Well I am finding it very strange with ImageIO.write();

Consider the following code snippet:

//....

BufferedImage img=ImageIO.read(new File("fw2.jpg"));

BufferedImage img1=new BufferedImage(img.getWidth(),img.getHeight(),img.TYPE_INT_ARGB);

for(int i=0;i<img.getHeight();i++)

{

for(int j=0;j<img.getWidth();j++)

{

img1.setRGB(j,i,img.getRGB(j,i));

}

}

ImageIO.write(img1,"jpg",new File("fwinv.jpg"));

So simply I am copying fw2.jpg to fwinv.jpg. fw2.jpg was also created

using ImageIO.write().

But the two files are having different pixel values.

i.e If I read fwinv.jpg again I will get different pixel value that it was written.

Why is this...Can any one please help me..>

[796 byte] By [chetan_kra] at [2007-11-26 13:08:45]
# 1

Reading the written images back and displaying them in java works okay. The third image looks okay in RGB but wrong in ARGB when displayed by a native app. For more about this see reply 1 in [url=http://forum.java.sun.com/thread.jspa?threadID=770344]Color problems[/url] and the linked bug report: [url=http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4836466]Some Images written using JPEG Image Writer are not recognized by native applns[/url].

import java.awt.GridLayout;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class WriteTest {

private JPanel getContent(BufferedImage src) throws IOException {

File fileOne = new File("fileOne.jpg");

ImageIO.write(src, "jpg", fileOne);

BufferedImage image1 = ImageIO.read(fileOne);

BufferedImage image2 = copy(image1);

JPanel panel = new JPanel(new GridLayout(1,0));

panel.add(new JLabel(new ImageIcon(src)));

panel.add(new JLabel(new ImageIcon(image1)));

panel.add(new JLabel(new ImageIcon(image2)));

return panel;

}

private BufferedImage copy(BufferedImage in) throws IOException {

int w = in.getWidth();

int h = in.getHeight();

int type = BufferedImage.TYPE_INT_ARGB;// problem in native apps

//BufferedImage.TYPE_INT_RGB; // this one works okay

BufferedImage out = new BufferedImage(w, h, type);

for(int y = 0; y < h; y++) {

for(int x = 0; x < w; x++) {

out.setRGB(x, y, in.getRGB(x, y));

}

}

File fileTwo = new File("fileTwo.jpg");

ImageIO.write(out, "jpg", fileTwo);

return ImageIO.read(fileTwo);

}

public static void main(String[] args) throws IOException {

BufferedImage image = ImageIO.read(new File("images/cougar.jpg"));

WriteTest test = new WriteTest();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getContent(image));

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

}

crwooda at 2007-7-7 17:20:30 > top of Java-index,Security,Cryptography...