using double[][] pixeldata to recreate the image

H

harryos

hi all,
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?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.Can someone help?

harry
 
K

Knute Johnson

harryos said:
hi all,
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?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.Can someone help?

harry

The answer to your question requires a couple of questions. What format
is the data in? The double[][], is that indexed for each row or what?
Are the values 0.0-1.0? If you will make the data available I will try
to create your image for you.
 
T

Tom Anderson

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+"double[]"&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
 
J

John B. Matthews

harryos said:
[...]
have some code that reads a greyscale image and stores the
pixeldata as a double[][].
[...]
So can't figure out how to create a WritableRaster
on which can set the pixels. Can someone help?


You've gotten excellent suggestions from Tom & Knute, but knowing what's
in the array is essential. If you just want to tinker, you can create a
BufferedImage from any visible Component, a JPanel in this example:

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class RasterTest extends JPanel {

private static final int WIDTH = 256;
private static final int HEIGHT = 256;

public RasterTest() {
setPreferredSize(new Dimension(WIDTH * 3, HEIGHT * 3));
}

public void paintComponent(Graphics g) {
final BufferedImage image;
int[] iArray = { 0, 0, 0, 255 };

image = (BufferedImage) createImage(WIDTH, HEIGHT);
WritableRaster raster = image.getRaster();
for (int row = 0; row < HEIGHT; row++) {
for (int col = 0 ; col < WIDTH; col++) {
int v = row * col;
iArray[0] = v << 1;
iArray[1] = v << 2;
iArray[2] = v << 3;
raster.setPixel(col, row, iArray);
}
}
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RasterTest rt = new RasterTest();
f.add(rt, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}

You'll have to run the code to see the nice picture. :)

[Thanks to Knute for the concise EDT startup code. The Component's
createImage() method returns null until it is made visible.]
 
K

Knute Johnson

John said:
harryos said:
[...]
have some code that reads a greyscale image and stores the
pixeldata as a double[][].
[...]
So can't figure out how to create a WritableRaster
on which can set the pixels. Can someone help?


You've gotten excellent suggestions from Tom & Knute, but knowing what's
in the array is essential. If you just want to tinker, you can create a
BufferedImage from any visible Component, a JPanel in this example:

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class RasterTest extends JPanel {

private static final int WIDTH = 256;
private static final int HEIGHT = 256;

public RasterTest() {
setPreferredSize(new Dimension(WIDTH * 3, HEIGHT * 3));
}

public void paintComponent(Graphics g) {
final BufferedImage image;
int[] iArray = { 0, 0, 0, 255 };

image = (BufferedImage) createImage(WIDTH, HEIGHT);
WritableRaster raster = image.getRaster();
for (int row = 0; row < HEIGHT; row++) {
for (int col = 0 ; col < WIDTH; col++) {
int v = row * col;
iArray[0] = v << 1;
iArray[1] = v << 2;
iArray[2] = v << 3;
raster.setPixel(col, row, iArray);
}
}
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RasterTest rt = new RasterTest();
f.add(rt, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}

You'll have to run the code to see the nice picture. :)

[Thanks to Knute for the concise EDT startup code. The Component's
createImage() method returns null until it is made visible.]


That's cool, I like it!
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top