use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class BMPSubsamplingTest method getTestImage.
private BufferedImage getTestImage(int type) {
BufferedImage dst = null;
ColorModel colorModel = null;
WritableRaster raster = null;
switch(type) {
case TYPE_INT_GRB:
colorModel = new DirectColorModel(24, 0x0000ff00, 0x00ff0000, 0x000000ff);
break;
case TYPE_INT_GBR:
colorModel = new DirectColorModel(24, 0x000000ff, 0x00ff0000, 0x0000ff00);
break;
case TYPE_INT_RBG:
colorModel = new DirectColorModel(24, 0x00ff0000, 0x000000ff, 0x0000ff00);
break;
case TYPE_INT_BRG:
colorModel = new DirectColorModel(24, 0x0000ff00, 0x000000ff, 0x00ff0000);
break;
case TYPE_3BYTE_RGB:
dst = create3ByteImage(new int[] { 8, 8, 8 }, new int[] { 0, 1, 2 });
break;
case TYPE_3BYTE_GRB:
dst = create3ByteImage(new int[] { 8, 8, 8 }, new int[] { 1, 0, 2 });
break;
case TYPE_USHORT_555_GRB:
colorModel = new DirectColorModel(16, 0x03E0, 0x7C00, 0x001F);
break;
case TYPE_USHORT_555_BGR:
colorModel = new DirectColorModel(16, 0x001F, 0x03E0, 0x7C00);
break;
case TYPE_USHORT_565_BGR:
colorModel = new DirectColorModel(16, 0x001F, 0x07E0, 0xf800);
break;
case TYPE_4BPP_INDEXED:
dst = createIndexImage(4);
break;
default:
dst = new BufferedImage(w, h, type);
}
if (dst == null) {
raster = colorModel.createCompatibleWritableRaster(w, h);
dst = new BufferedImage(colorModel, raster, false, null);
}
Graphics2D g = dst.createGraphics();
for (int i = 0; i < colors.length; i++) {
g.setColor(colors[i]);
g.fillRect(i * dx, 0, dx, h);
}
g.dispose();
return dst;
}
use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class BMPSubsamplingTest method create3ByteImage.
private BufferedImage create3ByteImage(int[] nBits, int[] bOffs) {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, w * 3, 3, bOffs, null);
return new BufferedImage(colorModel, raster, false, null);
}
use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class Bug7049339 method main.
public static void main(String[] argv) {
int x = 100, y = 100;
BufferedImage src = new BufferedImage(x, y, BufferedImage.TYPE_INT_ARGB);
BufferedImage dst = new BufferedImage(x, y, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D dstg2d = dst.createGraphics();
dstg2d.setComposite(new Composite() {
@Override
public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints) {
return new CompositeContext() {
@Override
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
// do nothing
}
@Override
public void dispose() {
}
};
}
});
Shape clip = new Ellipse2D.Double(x / 4, y / 4, x / 2, y / 2);
dstg2d.setClip(clip);
// This will throw a RasterFormatException if the bug is present.
dstg2d.drawImage(src, 0, 0, null);
}
use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class RenderToCustomBufferTest method createCustomBuffer.
private static BufferedImage createCustomBuffer() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
WritableRaster wr = cm.createCompatibleWritableRaster(width, height);
return new BufferedImage(cm, wr, false, null);
}
use of java.awt.image.WritableRaster in project tdme by andreasdr.
the class ImageIO method imageToByteBuffer.
/**
* Loads an image to byte buffer
* @param image
* @param flipped
* @param forceAlpha
* @param transparent
* @param powerOfTwoSupport
* @param modeARGB
* @return byte buffer
*/
private ByteBuffer imageToByteBuffer(BufferedImage image, boolean flipped, boolean forceAlpha, int[] transparent, boolean powerOfTwoSupport, boolean modeARGB) {
//
ByteBuffer imageBuffer = null;
WritableRaster raster;
BufferedImage texImage;
int texWidth = image.getWidth();
int texHeight = image.getHeight();
if (powerOfTwoSupport) {
// find the closest power of 2 for the width and height
// of the produced texture
texWidth = 2;
texHeight = 2;
while (texWidth < image.getWidth()) {
texWidth *= 2;
}
while (texHeight < image.getHeight()) {
texHeight *= 2;
}
}
this.width = image.getWidth();
this.height = image.getHeight();
this.texHeight = texHeight;
this.texWidth = texWidth;
// create a raster that can be used by OpenGL as a source
// for a texture
boolean useAlpha = image.getColorModel().hasAlpha() || forceAlpha;
if (useAlpha) {
depth = 32;
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
texImage = new BufferedImage(glAlphaColorModel, raster, false, null);
} else {
depth = 24;
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null);
texImage = new BufferedImage(glColorModel, raster, false, null);
}
// copy the source image into the produced image
Graphics2D g = (Graphics2D) texImage.getGraphics();
// alpha
if (useAlpha) {
g.setColor(new Color(0f, 0f, 0f, 0f));
g.fillRect(0, 0, texWidth, texHeight);
}
if (flipped) {
g.scale(1, -1);
g.drawImage(image, 0, -height, texWidth, texHeight, null);
} else {
g.drawImage(image, 0, 0, texWidth, texHeight, null);
}
// build a byte buffer from the temporary image
// that be used by OpenGL to produce a texture.
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
if (transparent != null) {
for (int i = 0; i < data.length; i += 4) {
boolean match = true;
for (int c = 0; c < 3; c++) {
int value = data[i + c] < 0 ? 256 + data[i + c] : data[i + c];
if (value != transparent[c]) {
match = false;
}
}
if (match) {
data[i + 3] = 0;
}
}
}
if (modeARGB) {
for (int i = 0; i < data.length; i += 4) {
byte rr = data[i + 0];
byte gg = data[i + 1];
byte bb = data[i + 2];
byte aa = data[i + 3];
data[i + 0] = bb;
data[i + 1] = gg;
data[i + 2] = rr;
data[i + 3] = aa;
}
}
imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();
g.dispose();
return imageBuffer;
}
Aggregations