CreateImage problem

Discussion in 'Java' started by Amar, Jun 8, 2004.

  1. Amar

    Amar Guest

    Hi I am trying to extract image data from a binary file. I am new to
    java and I am using eclipse in windows 2000.

    there are a total of 9 images in the file out of which i am extracting
    8.the images are littleendian.

    The program always displays the last image. if i change
    NUMOFIMAGES to 2 then it displays img[2] and if it is 7 displays
    img[7] for all the images; Any ideas on this?


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

    public class scanner3d extends JPanel {
    static Image img[] = new Image[10];

    public static void main(String[] args) throws IOException {
    scanner3d scner = new scanner3d();
    int WIDTH = 150;
    int HEIGHT = 200;
    int NUMOFIMAGES = 9;


    try {
    File file = new File("c:\\java\\15");
    FileInputStream fileInput = new FileInputStream(file);
    // similar to DataInputStream but reads little endian data.
    // Available at http://mindprod.com/jgloss/ledatinputstream.html
    LEDataInputStream ledatainputstream =
    new LEDataInputStream(fileInput);
    //DataInputStream dataIn = new DataInputStream( fileInput );
    float data[][][] = new float[NUMOFIMAGES][HEIGHT][WIDTH];
    float max = 0;
    // Reading the input file
    for (int m = 0; m < NUMOFIMAGES; m++) {
    for (int k = 0; k < HEIGHT; k++) {
    for (int j = 0; j < WIDTH; j++) {
    float in = ledatainputstream.readFloat();
    // convert negative values to zero
    if (in < 0)
    in = 0;
    // find the maximum value for scaling
    if (in > max)
    max = in;
    data[m][k][j] = in;
    }
    }
    }
    // scale the Image
    int OneDimImage[] = new int[WIDTH * HEIGHT];

    for (int m = 0; m < NUMOFIMAGES; m++) {
    for (int k = 0; k < HEIGHT; k++) {
    for (int j = 0; j < WIDTH; j++) {
    int temp = (int) (data[m][k][j] / max * 255);
    int col = (255 << 24) | (temp << 16) | 0;
    //if(j <= 5) col = (255 << 24) | (temp << 16) | 255;
    OneDimImage[k * WIDTH + j] = col;
    }
    }
    //crerating image
    img[m] =
    java.awt.Toolkit.getDefaultToolkit().createImage(
    new MemoryImageSource(
    WIDTH,
    HEIGHT,
    OneDimImage,
    0,
    WIDTH));

    // When this statements are "included" nothing is displayed.
    // if they are commented out only the last image
    // "img[7] is diaplayed" for all the images.
    // It always displays the last image. if i change
    // NUMOFIMAGES to 2 then it displays img[2];
    // if ((m + 1) == NUMOFIMAGES)
    // for (int k = 0; k < HEIGHT; k++) {
    // for (int j = 0; j < WIDTH; j++) {
    // OneDimImage[k * WIDTH + j] = -1;
    // }
    // }

    }

    JFrame f = new JFrame();
    f.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    f.getContentPane().add(scner);
    //f.setBounds(50,50,WIDTH,HEIGHT);
    f.show();
    } catch (Exception r) {
    System.out.println(r);
    System.exit(0);
    }
    }
    public void paint(Graphics g) {
    super.paint(g);
    Dimension d = getSize();
    Insets i = getInsets();
    g.drawImage(img[0], 13, 15, 245, 190, this);
    g.drawImage(img[1], 263, 15, 245, 190, this);
    g.drawImage(img[2], 513, 15, 245, 190, this);
    g.drawImage(img[3], 763, 15, 245, 190, this);
    g.drawImage(img[4], 13, 211, 245, 190, this);
    g.drawImage(img[5], 263, 211, 245, 190, this);
    g.drawImage(img[6], 513, 211, 245, 190, this);
    g.drawImage(img[7], 763, 211, 245, 190, this);
    }
    } // @jve:visual-info decl-index=0 visual-constraint="10,10"
     
    Amar, Jun 8, 2004
    #1
    1. Advertising

  2. Amar

    ak Guest

    > Hi I am trying to extract image data from a binary file. I am new to
    > java and I am using eclipse in windows 2000.
    >
    > there are a total of 9 images in the file out of which i am extracting
    > 8.the images are littleendian.
    >
    > The program always displays the last image. if i change
    > NUMOFIMAGES to 2 then it displays img[2] and if it is 7 displays
    > img[7] for all the images; Any ideas on this?

    yes, MemoryImageSource don't copy your buffer content, but uses it direct.
    so, for every image you have to create its own buffer (> int OneDimImage[] =
    new int[WIDTH * HEIGHT];)

    > // similar to DataInputStream but reads little endian data.
    > // Available at http://mindprod.com/jgloss/ledatinputstream.html
    > LEDataInputStream ledatainputstream =
    > new LEDataInputStream(fileInput);


    try out Unified I/O http://uio.omagero.com or http://uio.dev.java.net

    --
    http://uio.dev.java.net Unified I/O for Java
    http://reader.imagero.com Java image reader
     
    ak, Jun 8, 2004
    #2
    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. Rob Love
    Replies:
    1
    Views:
    3,301
    dhek bhun kho
    Jul 2, 2003
  2. patrick

    createImage problem

    patrick, Dec 4, 2003, in forum: Java
    Replies:
    8
    Views:
    557
    patrick
    Dec 4, 2003
  3. Mike
    Replies:
    0
    Views:
    391
  4. Steven J Sobol

    J2ME MIDP1 CreateImage problem

    Steven J Sobol, Jul 15, 2004, in forum: Java
    Replies:
    19
    Views:
    12,915
    Darryl L. Pierce
    Jul 17, 2004
  5. vizlab
    Replies:
    3
    Views:
    4,212
    Michael Bar-Sinai
    Oct 17, 2007
Loading...

Share This Page