using double[][] pixeldata to recreate the image

Discussion in 'Java' started by harryos, Jul 23, 2008.

  1. harryos

    harryos Guest

    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
    harryos, Jul 23, 2008
    #1
    1. Advertising

  2. harryos wrote:
    > 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.

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Jul 23, 2008
    #2
    1. Advertising

  3. harryos

    Tom Anderson Guest

    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 "double[]%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
    Tom Anderson, Jul 23, 2008
    #3
  4. In article
    <>,
    harryos <> wrote:

    > [...]
    > 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.]

    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
    John B. Matthews, Jul 24, 2008
    #4
  5. John B. Matthews wrote:
    > In article
    > <>,
    > harryos <> wrote:
    >
    >> [...]
    >> 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!

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Jul 24, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl Prothman [MVP]

    Re: how to recreate the default ASPNET account ?

    Carl Prothman [MVP], Aug 9, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    3,343
    Carl Prothman [MVP]
    Aug 9, 2003
  2. Mark
    Replies:
    2
    Views:
    363
    Chris Jackson
    Feb 23, 2004
  3. kevinsaucier
    Replies:
    0
    Views:
    1,485
    kevinsaucier
    May 12, 2005
  4. Hardeep Rakhra
    Replies:
    6
    Views:
    400
    Hardeep Rakhra
    Jan 12, 2004
  5. Sydex
    Replies:
    12
    Views:
    6,454
    Victor Bazarov
    Feb 17, 2005
Loading...

Share This Page