CreateImage problem

A

Amar

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"
 
A

ak

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
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top