> I'm hoping you guys can help me a bit with the following. I have a 16-bit
> grayscale image that I want to store in a BufferedImage. For reasons of
> performance, I want to put it into an array of ints instead of USHORTS.
> This is because I'm informed that AffineTransforms are hopelessly broken
> when dealing with USHORT_GRAY. Anyway, once I have my array of ints
> (represneting 16bit grayscale) I try the following:
> .
> .
> .
> However it comes out all white!
>
> Can anyone shed some light on this? How would you go about it?
I don't see anything terribly wrong with this code snippet. You probably
want the scanlineStride argument to the PixelInterleavedSampleModel
constructor to be width rather than height. Also, the isRasterPremultiplied
argument to the BufferedImage constructor makes more sense being false,
although that wouldn't really have any effect in this case.
Below is a small program using your code which works fine. It also
works fine (and faster) if you use USHORT instead of INT.
I don't know of problems with AffineTransforms and USHORT_GRAY. If
you have an example, please send it to me.
In general using one of the predefined BufferedImage types or, failing
that, using a WritableRaster constructed with one of the factory methods
in the Raster class will give better performance.
Another alternative for grayscale images is to use an IndexColorModel with
indices which are greater than 8 bits. Since 12-bit data is common,
there are rendering optimizations for indexed images with color maps
consisting of 4096 grayscale entries.
Jerry
=====================================================================
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.image.*;
import java.awt.color.*;
public class UINT16 extends Canvas {
BufferedImage img;
public UINT16() {
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (img == null) {
int[] arr = new int[10000];
DataBuffer dbuff = new DataBufferInt(arr,arr.length);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel cm = new ComponentColorModel(cs, new int[]{16}, false,
false,
Transparency.OPAQUE,
DataBuffer.TYPE_INT);
int[] band = {0};
SampleModel sm =
new PixelInterleavedSampleModel(dbuff.getDataType(),
100,
100,
1,
100,
band);
WritableRaster raster =
Raster.createWritableRaster(sm, dbuff, new Point(0,0));
img = new BufferedImage(cm, raster, false, null);
for (int i = 0; i < 10000; i++) {
arr[i] = 0x8000;
}
}
g2.drawImage(img, 100, 100, this);
}
public static void main(String s[]) {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
Frame f = new Frame("Java 2D Demo - UINT16");
f.addWindowListener(l);
f.add("Center", new UINT16());
f.pack();
f.setSize(new Dimension(400,300));
f.show();
}
}
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".