doubts about Raster and BufferedImage

Discussion in 'Java' started by mark jason, Nov 18, 2010.

  1. mark jason

    mark jason Guest

    hi,
    what is the purpose of the optionally preallocated double array in the
    method getPixels of class Raster ?

    public double[] getPixels(int x,int y,int w, int h,double[] dArray)

    I was playing around with the api to create a BufferedImage using a
    double array,andwas trying to normalise the pixel values as follows.
    I expected to find the normalised values in the final array ,but it
    still contains the original image data. Obviously the
    this.bi.getRaster().setPixels(0,0,this.getWidth(),this.getHeight(),ndata)
    doesn't do what I thought it would do.
    if somebody can tell me where I messed it up ,it will be great.

    thanks and regards,

    mark

    <code>
    import java.awt.image.BufferedImage;
    import java.awt.image.ColorConvertOp;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;

    public class MyImage {

    private BufferedImage bi;//of type BufferedImage.TYPE_BYTE_GRAY
    private String fileName;
    public MyImage(String imageFileName) throws IOException{
    this.fileName = imageFileName;
    File imageFile = new File(imageFileName);
    this.bi=makeGrayScaleImage(ImageIO.read(imageFile));
    }
    public MyImage(String imageName,int width,int height,double[] data){
    if (data.length != width*height){
    throw new IllegalArgumentException("data size must be equal to
    "+width*height);
    }
    this.bi = new BufferedImage(width, height,
    BufferedImage.TYPE_BYTE_GRAY);
    this.bi.getRaster().setPixels(0, 0, width, height, data);
    this.fileName = imageName;
    }
    private BufferedImage makeGrayScaleImage(BufferedImage img) {
    BufferedImage gray = null;
    try{
    gray = new BufferedImage(img.getWidth(),img.getHeight(),
    BufferedImage.TYPE_BYTE_GRAY);
    ColorConvertOp ccop = new ColorConvertOp(
    img.getColorModel().getColorSpace(),
    gray.getColorModel().getColorSpace(),null);
    ccop.filter(img,gray);

    }catch(Exception e){
    System.err.println("grayscale conversion failed");
    }
    return gray;
    }
    public int getWidth(){
    return this.bi.getWidth();
    }
    public int getHeight(){
    return this.bi.getHeight();
    }
    public double[] getData(){
    int h = getHeight();
    int w = getWidth();
    double[] data = new double[h*w];
    double[] pdata=this.bi.getData().getPixels(0,0,w,h,data);
    return pdata;
    }
    public void normaliseImageData(){
    double[] ndata = getNormalisedData();

    this.bi.getRaster().setPixels(0,0,this.getWidth(),this.getHeight(),ndata);
    }
    private double[] getNormalisedData(){
    double[] d=getData();
    double maxval=max(d);
    for (int i=0;i<d.length;i++){
    d/=maxval;
    }
    return d;
    }
    private static double max(double[] arr){
    double m=Double.MIN_VALUE;
    for(int i=0;i<arr.length;i++){
    m=Math.max(m,arr);
    }
    return m;
    }
    private static void printArray(double[] a){
    for (double x:a){
    System.out.print(x+" ");
    }
    System.out.println();
    }
    private static void debug(String msg) {
    System.out.println(msg);
    }
    public static void main(String[] args) {
    MyImage my = new MyImage("dummy.png", 4, 5, new double[]
    {23,32,13,55,65,36,46,64,27,43,71,58,38,25,62,47,19,72,37,55});
    double[] data = my.getData();
    debug("original image data:");
    printArray(data);
    double[] ndata = my.getNormalisedData();
    debug("normalised data:");
    printArray(ndata);
    debug("image data after normalisation:");
    double[] newdata = my.getData();
    printArray(newdata);

    }
    }

    </code>
     
    mark jason, Nov 18, 2010
    #1
    1. Advertising

  2. mark jason

    mark jason Guest

    for some reason the format gets screwed up when I copy the code from
    eclipse to the html page..
    I tried to correct it by deleting spaces on each line.. still it looks
    ugly
    sorry for this
     
    mark jason, Nov 18, 2010
    #2
    1. Advertising

  3. In article
    <>,
    mark jason <> wrote:

    [...]
    > I was playing around with the api to create a BufferedImage using a
    > double array,andwas trying to normalise the pixel values as follows.
    > I expected to find the normalised values in the final array, but it
    > still contains the original image data. Obviously the
    > [normaliseImageData() method] doesn't do what I thought it would do.
    > if somebody can tell me where I messed it up, it will be great.


    I think you forgot to call my.normaliseImageData();

    > for some reason the format gets screwed up when I copy the code from
    > eclipse to the html page.. I tried to correct it by deleting spaces
    > on each line.. still it looks ugly


    Looks like a tab problem; <http://sscce.org/> has a lot of good
    information on this.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Nov 18, 2010
    #3
  4. mark jason

    Lew Guest

    On 11/18/2010 01:30 AM, mark jason wrote:
    > for some reason the format gets screwed up when I copy the code from
    > eclipse to the html [sic] page..
    > I tried to correct it by deleting spaces on each line.. still it looks
    > ugly


    I thought your issue was with Swing and Java2D. You might have mentioned that
    it involved HTML before.

    You are aware that HTML collapses all whitespace to a single space upon
    presentation, right?

    On a side note, please do not embed TABs in Usenet code listings. Use spaces
    to indent listings, up to four spaces per indent level. It makes the listings
    much easier to read.

    --
    Lew
     
    Lew, Nov 18, 2010
    #4
    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. Tomasz
    Replies:
    1
    Views:
    4,778
    matador
    Oct 9, 2007
  2. John Palmer

    SVG to Raster converter

    John Palmer, Aug 20, 2003, in forum: XML
    Replies:
    2
    Views:
    1,037
    Oleg Tkachenko
    Aug 21, 2003
  3. R.Marquez
    Replies:
    3
    Views:
    6,402
    Andrew MacIntyre
    Apr 17, 2004
  4. Tom Peel
    Replies:
    3
    Views:
    3,391
    Thomas Fritsch
    Oct 8, 2006
  5. paul
    Replies:
    1
    Views:
    338
    Victor Bazarov
    Oct 1, 2005
Loading...

Share This Page