recreating an image using int[] from getRGB()

R

royG

hi
i was learning some image manipulation using JDK's imageio classes.I
am extracting the imagedata as int[] using getRGB() and then using
that int[] ,i want to create a new image.


import java.awt.image.*;
import java.io.File;
public class ImageDemo {
public static void main(String[] args)throws Exception {
String inputFile="F:\\myimgs\\grayscale.png";
BufferedImage img = javax.imageio.ImageIO.read(new File(inputFile));
int ht=img.getHeight();
int wd=img.getWidth();
int numpixels=ht*wd;
int[] imgdataints=new int[numpixels];
img.getRGB( 0, 0, wd, ht,imgdataints, 0, wd);
String newimage="F:\\myimgs\\newimage.jpg";
writeImage(newimage,imgdataints,wd);
}
public static void writeImage(String imgname,int[] data,int wd)throws
Exception{
BufferedImage bufimg=new BufferedImage(wd,data.length/wd,10);
Raster rast=bufimg.getData();
WritableRaster wr= rast.createCompatibleWritableRaster();
wr.setPixels(0, 0, wd,data.length/wd, data);
bufimg.setData(wr);
File newfile=new File(imgname);
javax.imageio.ImageIO.write(bufimg, "jpg", newfile);

}

}

I am getting a newimage,jpg created.But it looks a too bright unlike
the original image.Why does this happen?I am using the int[] extracted
from getRGB() so should i not get the exact image?
Is there anything i need to recreate the original image,with the same
brightness ?
thanks
roy
 
R

royG

is this specific to jpeg encoding?when i tried png images also i got
the same result..the image looks 'bleached'.

but the problem doesn't occur if i replace the getRGB() call in the
above code with
double[] imgdata=new double[numpixels];
img.getData().getPixels(0, 0, wd, ht, imgdata);

and pass a double[] imgdata to writeImage() instead of int[].
Even jpeg images are reproduced exactly.
thanks
roy
 
R

Roedy Green

is this specific to jpeg encoding?when i tried png images also i got
the same result..the image looks 'bleached'.

jpg is lossy. png is lossless.

To explore this, look up the format for png so you can tell what the
pixel at 0,0, should actually be by hex editor exam. Then see what
getRGB says. The look at your result image. The idea is to figure
out where the distortion is creeping in.

see http://mindprod.com/jgloss/png.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
Your old road is
Rapidly agin'.
Please get out of the new one
If you can't lend your hand
For the times they are a-changin'.
 
M

Mayeul

royG a écrit :
is this specific to jpeg encoding?

No, it was just worth to point out that jpeg is a lossy format, and that
you shouldn't expect a jpeg image to be exactly the same as its source
image.
> when i tried png images also i got
the same result..the image looks 'bleached'.
>
but the problem doesn't occur if i replace the getRGB() call in the
above code with
double[] imgdata=new double[numpixels];
img.getData().getPixels(0, 0, wd, ht, imgdata);

and pass a double[] imgdata to writeImage() instead of int[].
Even jpeg images are reproduced exactly.
thanks
roy

I think it pretty much demonstrates the problem.

In your previous test case, you were using BufferedImage.getRGB()

According to javadoc, it fills an int[] array with pixels values
expressed in default color model, non-premultiplied ARGB.

Then you pass these pixel values to WritableRaster.setPixels(), that
according to javadoc takes as argument an an int[] array of pixels
values... Of whatever color model the raster is using.

Considering the raster is created after a BufferedImage built with image
type 10 (which, after looking up the library's source code, is
BufferedImage.TYPE_BYTE_GRAY, and should preferably be referenced that
way for clarity's sake), I think it would be very unlikely for its color
model to be non-premultiplied ARGB.

Hence resulting rendering differences. It take it only the mere fact the
image is grayscale enabled the result to even look like its source.


Now, the problem doesn't reproduce when using Raster.getPixels() and the
corresponding setPixels(), probably because the two rasters share the
same color model, and are probably supposed to contain the very same
thing. I'm guessing using getPixels() and setPixels() with an int[]
array would work too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top