use of java.awt.image.DataBufferByte in project nutz by nutzam.
the class Images method createJPEG4.
/**
* Java's ImageIO can't process 4-component images and Java2D can't apply
* AffineTransformOp either, so convert raster data to RGB. Technique due to
* MArk Stephens. Free for any use.
*/
private static BufferedImage createJPEG4(Raster raster) {
int w = raster.getWidth();
int h = raster.getHeight();
byte[] rgb = new byte[w * h * 3];
float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];
double val = y + 1.402 * (cr - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
val = y + 1.772 * (cb - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
}
raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3, new int[] { 0, 1, 2 }, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, (WritableRaster) raster, true, null);
}
use of java.awt.image.DataBufferByte in project scriptographer by scriptographer.
the class Raster method drawImage.
public void drawImage(Image image, int x, int y) {
BufferedImage buf;
// a new one:
if (image instanceof BufferedImage && getColorModel().isCompatibleSampleModel(((BufferedImage) image).getSampleModel())) {
buf = (BufferedImage) image;
} else {
buf = createCompatibleImage(image.getWidth(null), image.getHeight(null));
buf.createGraphics().drawImage(image, x, y, null);
}
WritableRaster raster = buf.getRaster();
byte[] data = ((DataBufferByte) raster.getDataBuffer()).getData();
nativeSetPixels(data, raster.getNumBands(), x, y, buf.getWidth(), buf.getHeight());
}
use of java.awt.image.DataBufferByte in project scriptographer by scriptographer.
the class Raster method getSubImage.
public BufferedImage getSubImage(int x, int y, int width, int height) {
if (width == -1 || height == -1) {
Size size = getSize();
if (width == -1)
width = (int) size.width;
if (height == -1)
height = (int) size.height;
}
BufferedImage img = createCompatibleImage(width, height);
Graphics2D g2d = img.createGraphics();
g2d.setColor(java.awt.Color.WHITE);
// g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0));
g2d.fillRect(0, 0, width, height);
g2d.dispose();
WritableRaster raster = img.getRaster();
byte[] data = ((DataBufferByte) raster.getDataBuffer()).getData();
nativeGetPixels(data, raster.getNumBands(), x, y, width, height);
return img;
}
use of java.awt.image.DataBufferByte in project playn by threerings.
the class JavaGLContext method updateTexture.
void updateTexture(int tex, BufferedImage image) {
// Convert the image into a format for quick uploading
image = convertImage(image);
DataBuffer dbuf = image.getRaster().getDataBuffer();
ByteBuffer bbuf;
int format, type;
if (image.getType() == BufferedImage.TYPE_INT_ARGB_PRE) {
DataBufferInt ibuf = (DataBufferInt) dbuf;
int iSize = ibuf.getSize() * 4;
bbuf = checkGetImageBuffer(iSize);
bbuf.asIntBuffer().put(ibuf.getData());
bbuf.position(bbuf.position() + iSize);
bbuf.flip();
format = GL12.GL_BGRA;
type = GL12.GL_UNSIGNED_INT_8_8_8_8_REV;
} else if (image.getType() == BufferedImage.TYPE_4BYTE_ABGR) {
DataBufferByte dbbuf = (DataBufferByte) dbuf;
bbuf = checkGetImageBuffer(dbbuf.getSize());
bbuf.put(dbbuf.getData());
bbuf.flip();
format = GL11.GL_RGBA;
type = GL12.GL_UNSIGNED_INT_8_8_8_8;
} else {
// except we don't know how to deal with it
throw new RuntimeException("Image type wasn't converted to usable: " + image.getType());
}
bindTexture(tex);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, format, type, bbuf);
checkGLError("updateTexture");
}
use of java.awt.image.DataBufferByte in project lwjgl by LWJGL.
the class TextureLoader method convertImageData.
/**
* Convert the buffered image to a texture
*
* @param bufferedImage The image to convert to a texture
* @param texture The texture to store the data into
* @return A buffer containing the data
*/
private ByteBuffer convertImageData(BufferedImage bufferedImage, Texture texture) {
ByteBuffer imageBuffer;
WritableRaster raster;
BufferedImage texImage;
int texWidth = 2;
int texHeight = 2;
// of the produced texture
while (texWidth < bufferedImage.getWidth()) {
texWidth *= 2;
}
while (texHeight < bufferedImage.getHeight()) {
texHeight *= 2;
}
texture.setTextureHeight(texHeight);
texture.setTextureWidth(texWidth);
// for a texture
if (bufferedImage.getColorModel().hasAlpha()) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable());
} else {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null);
texImage = new BufferedImage(glColorModel, raster, false, new Hashtable());
}
// copy the source image into the produced image
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f, 0f, 0f, 0f));
g.fillRect(0, 0, texWidth, texHeight);
g.drawImage(bufferedImage, 0, 0, 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();
imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();
return imageBuffer;
}
Aggregations