Re: using double[][] pixeldata to recreate the image
On Wed, 23 Jul 2008, harryos wrote:
i have some code that reads a greyscale image and stores the pixeldata
as a double[][].This double[][] is later on passed to another program
that recreates the image.
when i checked the documentation i found that there is a method to
create BufferedImage from given int[] like
setRGB(int startX, int startY, int w, int h, int[] rgbArray, int
offset, int scansize)
suppose i rearrange the pixel values of the greyscale image as a
double[] ,is there a similar method to construct a greyscale image out
of it?
GcIYF:
http://www.google.com/codesearch?q=BufferedImage+%22double[]%22&hl=en&btnG=Search+Code
Which finds you:
public static BufferedImage makeImage(double[][] data, int w, int h, int[] bits)
{
int dataType = DataBuffer.TYPE_DOUBLE;
ColorModel colorModel = makeColorModel(data.length, dataType, bits);
if (colorModel == null) return null;
SampleModel model = new BandedSampleModel(dataType, w, h, data.length);
DataBuffer buffer = new DataBufferDouble(data, data[0].length);
WritableRaster raster = Raster.createWritableRaster(model, buffer, null);
return new BufferedImage(colorModel, raster, false, null);
}
IMPORTANT NOTE - that double[][] data isn't a square matrix of pixels,
it's an array of bands, so there's one array of samples for each channel.
In your case, that means it will be an array of one element, which will be
your double[] containing all your pixel values.
I won't copy and paste makeColorModel, but all you need to know is that
for you, it does this:
return new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false, ColorModel.TRANSLUCENT, dataType);
Based on this, you can probably simplify things quite a bit.
I do not keep any reference to the original image other than the array
of doubles.So i can't figure out how to create a WritableRaster on which
i can set the pixels.
Raster.createWritableRaster(SampleModel, DataBuffer, Point) is what you
want.
I have to say, though, your question indicates that you didn't make much
effort to read the documentation. The javadoc for WritableRaster says:
"To instantiate a WritableRaster, use one of the createWritableRaster
factory methods in the Raster class."
And if you look at the javadoc for Raster, hey presto, there are two
createWritableRaster methods and a whole bunch of related createXXXRaster
methods, several of which you could have used to do what you want to do.
Having looked at it a bit more, i think this is the simplest possible way
to do what you want to do:
public BufferedImage createGrayscaleDoubleImage(int w, int h) {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY) ;
ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_DOUBLE) ;
WritableRaster r = cm.createCompatibleWritableRaster(w, h) ;
BufferedImage img = new BufferedImage(cm, r, false, null) ;
return img ;
}
tom
--
Is that dark pixel a prox mine or a bullet hole? HERE COME THE PROX MINE
SWEATS! -- D