from FloatProcessor to BufferedImage

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

from FloatProcessor to BufferedImage

Thomas Leduc
Hi,
In the following class, I'm trying to convert a FloatProcessor with 4
differents values (one of them is Float.NaN) into a BufferedImage. The
resulting BufferedImage contains only 3 different colors : I mean, NaN pixel
value and smallest pixel value produce same pixel color. What's the way to
solve this problem (obtain 4 different pixel colors) ?
Thanks a lot,

The execution trace :
    pixel value : 33.0
    pixel value : 70.3
    pixel value : NaN
    pixel value : 244.4
Number of different colors = 3
    color value : -16777216
    color value : -1
    color value : -13882324

public class ToBeDeleted {
    private static BufferedImage toBufferedImage(Image image) {
        if (image instanceof BufferedImage) {
            return ((BufferedImage) image);
        } else {
            image = new ImageIcon(image).getImage();
            final BufferedImage bufferedImage = new BufferedImage(image
                    .getWidth(null), image.getHeight(null),
                    BufferedImage.TYPE_INT_RGB);
            final Graphics g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();
            return bufferedImage;
        }
    }

    public static void main(String[] args) {
        final float xxx = Float.NaN;
        final ImageProcessor fp = new FloatProcessor(4, 1, new float[] { 33,

                70.3f, xxx, 244.4f }, null);
        // fp.resetMinAndMax();
        // fp.autoThreshold();

        final ImagePlus imagePlus = new ImagePlus("test", fp);
        final BufferedImage bi = toBufferedImage(imagePlus.getImage());

         for (float item : (float[]) fp.getPixels()) {
            System.out.println("\tpixel value : " + item);
        }

        final Set<Integer> colorSet = new HashSet<Integer>();
        for (int r = 0; r < bi.getWidth(); r++) {
            for (int c = 0; c < bi.getHeight(); c++) {
                colorSet.add(bi.getRGB(r, c));
            }
        }
        System.out.println("Number of different colors = " + colorSet.size());

        for (int item : colorSet) {
            System.out.println("+++" + item);
        }
        // new ImagePlus("test", fp).show();
    }
}

--
Thomas LEDUC