use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class Picture method setImage.
/**
* Set the image to put on the picture.
*
* @param assetManager The {@link AssetManager} to use to load the image.
* @param imgName The image name.
* @param useAlpha If true, the picture will appear transparent and allow
* objects behind it to appear through. If false, the transparent
* portions will be the image's color at that pixel.
*/
public void setImage(AssetManager assetManager, String imgName, boolean useAlpha) {
TextureKey key = new TextureKey(imgName, true);
Texture2D tex = (Texture2D) assetManager.loadTexture(key);
setTexture(assetManager, tex, useAlpha);
}
use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class CursorLoader method loadCursor.
private JmeCursor loadCursor(InputStream inStream) throws IOException {
// new byte [0] facilitates read()
byte[] icoimages = new byte[0];
if (isAni) {
CursorLoader.CursorImageData ciDat = new CursorLoader.CursorImageData();
int numIcons = 0;
int jiffy = 0;
// not using those but keeping references for now.
int steps = 0;
int width = 0;
int height = 0;
// we don't use that.
int flag = 0;
int[] rate = null;
int[] animSeq = null;
ArrayList<byte[]> icons;
DataInput leIn = new LittleEndien(inStream);
int riff = leIn.readInt();
if (riff == 0x46464952) {
// RIFF
// read next int (file length), discarding it, we don't need that.
leIn.readInt();
int nextInt = 0;
nextInt = getNext(leIn);
if (nextInt == 0x4e4f4341) {
// We have ACON, we do nothing
// System.out.println("We have ACON. Next!");
nextInt = getNext(leIn);
while (nextInt >= 0) {
if (nextInt == 0x68696e61) {
// System.out.println("we have 'anih' header");
// internal struct length (always 36)
leIn.skipBytes(8);
numIcons = leIn.readInt();
// number of blits for ani cycles
steps = leIn.readInt();
width = leIn.readInt();
height = leIn.readInt();
leIn.skipBytes(8);
jiffy = leIn.readInt();
flag = leIn.readInt();
nextInt = leIn.readInt();
} else if (nextInt == 0x65746172) {
// found a 'rate' of animation
// System.out.println("we have 'rate'.");
// Fill rate here.
// Rate is synchronous with frames.
int length = leIn.readInt();
rate = new int[length / 4];
for (int i = 0; i < length / 4; i++) {
rate[i] = leIn.readInt();
}
nextInt = leIn.readInt();
} else if (nextInt == 0x20716573) {
// found a 'seq ' of animation
// System.out.println("we have 'seq '.");
// Fill animation sequence here
int length = leIn.readInt();
animSeq = new int[length / 4];
for (int i = 0; i < length / 4; i++) {
animSeq[i] = leIn.readInt();
}
nextInt = leIn.readInt();
} else if (nextInt == 0x5453494c) {
// Found a LIST
// System.out.println("we have 'LIST'.");
int length = leIn.readInt();
nextInt = leIn.readInt();
if (nextInt == 0x4f464e49) {
// Got an INFO, skip its length
// this part consist of Author, title, etc
leIn.skipBytes(length - 4);
// System.out.println(" Discarding INFO (skipped = " + skipped + ")");
nextInt = leIn.readInt();
} else if (nextInt == 0x6d617266) {
// System.out.println("we have 'fram'.");
if (leIn.readInt() == 0x6e6f6369) {
// we have 'icon'
// We have an icon and from this point on
// the rest is only icons.
int icoLength = leIn.readInt();
ciDat.numImages = numIcons;
icons = new ArrayList<byte[]>(numIcons);
for (int i = 0; i < numIcons; i++) {
if (i > 0) {
// skip 'icon' header and length as they are
// known already and won't change.
leIn.skipBytes(8);
}
byte[] data = new byte[icoLength];
((InputStream) leIn).read(data, 0, icoLength);
// get it from first image.
if (width == 0 || height == 0 && i == 1) {
width = data[6];
height = data[7];
}
icons.add(data);
}
// at this point we have the icons, rates (either
// through jiffy or rate array, the sequence (if
// applicable) and the ani header info.
// Put things together.
ciDat.assembleCursor(icons, rate, animSeq, jiffy, steps, width, height);
ciDat.completeCursor();
nextInt = leIn.readInt();
// if for some reason there's JUNK (nextInt > -1)
// bail out.
nextInt = nextInt > -1 ? -1 : nextInt;
}
}
}
}
}
return setJmeCursor(ciDat);
} else if (riff == 0x58464952) {
throw new IllegalArgumentException("Big-Endian RIFX is not supported. Sorry.");
} else {
throw new IllegalArgumentException("Unknown format.");
}
} else if (isCur || isIco) {
DataInputStream in = new DataInputStream(inStream);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[16384];
int bytesRead;
while ((bytesRead = in.read(buffer)) >= 0) {
out.write(buffer, 0, bytesRead);
}
icoimages = out.toByteArray();
}
BufferedImage[] bi = parseICOImage(icoimages);
int hotSpotX = 0;
int hotSpotY = 0;
CursorLoader.CursorImageData cid = new CursorLoader.CursorImageData(bi, 0, hotSpotX, hotSpotY, 0);
if (isCur) {
/*
* Per http://msdn.microsoft.com/en-us/library/ms997538.aspx
* every .cur file should provide hotspot coordinates.
*/
hotSpotX = icoimages[FDE_OFFSET + 4] + icoimages[FDE_OFFSET + 5] * 255;
hotSpotY = icoimages[FDE_OFFSET + 6] + icoimages[FDE_OFFSET + 7] * 255;
cid.xHotSpot = hotSpotX;
/*
* Flip the Y-coordinate.
*/
cid.yHotSpot = cid.height - 1 - hotSpotY;
}
cid.completeCursor();
return setJmeCursor(cid);
}
use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class AWTLoader method load.
public Image load(BufferedImage img, boolean flipY) {
int width = img.getWidth();
int height = img.getHeight();
switch(img.getType()) {
case // most common in PNG images w/ alpha
BufferedImage.TYPE_4BYTE_ABGR:
byte[] dataBuf1 = (byte[]) extractImageData(img);
if (flipY)
flipImage(dataBuf1, width, height, 32);
ByteBuffer data1 = BufferUtils.createByteBuffer(img.getWidth() * img.getHeight() * 4);
data1.put(dataBuf1);
return new Image(Format.ABGR8, width, height, data1, null, com.jme3.texture.image.ColorSpace.sRGB);
case // most common in JPEG images
BufferedImage.TYPE_3BYTE_BGR:
byte[] dataBuf2 = (byte[]) extractImageData(img);
if (flipY)
flipImage(dataBuf2, width, height, 24);
ByteBuffer data2 = BufferUtils.createByteBuffer(img.getWidth() * img.getHeight() * 3);
data2.put(dataBuf2);
return new Image(Format.BGR8, width, height, data2, null, com.jme3.texture.image.ColorSpace.sRGB);
case // grayscale fonts
BufferedImage.TYPE_BYTE_GRAY:
byte[] dataBuf3 = (byte[]) extractImageData(img);
if (flipY)
flipImage(dataBuf3, width, height, 8);
ByteBuffer data3 = BufferUtils.createByteBuffer(img.getWidth() * img.getHeight());
data3.put(dataBuf3);
return new Image(Format.Luminance8, width, height, data3, null, com.jme3.texture.image.ColorSpace.sRGB);
default:
break;
}
if (img.getTransparency() == Transparency.OPAQUE) {
ByteBuffer data = BufferUtils.createByteBuffer(img.getWidth() * img.getHeight() * 3);
// no alpha
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int ny = y;
if (flipY) {
ny = height - y - 1;
}
int rgb = img.getRGB(x, ny);
byte r = (byte) ((rgb & 0x00FF0000) >> 16);
byte g = (byte) ((rgb & 0x0000FF00) >> 8);
byte b = (byte) ((rgb & 0x000000FF));
data.put(r).put(g).put(b);
}
}
data.flip();
return new Image(Format.RGB8, width, height, data, null, com.jme3.texture.image.ColorSpace.sRGB);
} else {
ByteBuffer data = BufferUtils.createByteBuffer(img.getWidth() * img.getHeight() * 4);
// alpha
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int ny = y;
if (flipY) {
ny = height - y - 1;
}
int rgb = img.getRGB(x, ny);
byte a = (byte) ((rgb & 0xFF000000) >> 24);
byte r = (byte) ((rgb & 0x00FF0000) >> 16);
byte g = (byte) ((rgb & 0x0000FF00) >> 8);
byte b = (byte) ((rgb & 0x000000FF));
data.put(r).put(g).put(b).put(a);
}
}
data.flip();
return new Image(Format.RGBA8, width, height, data, null, com.jme3.texture.image.ColorSpace.sRGB);
}
}
use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class ImageToAwt method convert.
public static BufferedImage convert(Image image, boolean do16bit, boolean fullalpha, int mipLevel) {
Format format = image.getFormat();
DecodeParams p = params.get(image.getFormat());
if (p == null)
throw new UnsupportedOperationException();
int width = image.getWidth();
int height = image.getHeight();
int level = mipLevel;
while (--level >= 0) {
width /= 2;
height /= 2;
}
ByteBuffer buf = image.getData(0);
buf.order(ByteOrder.LITTLE_ENDIAN);
BufferedImage out;
boolean alpha = false;
boolean luminance = false;
boolean rgb = false;
if (p.am != 0)
alpha = true;
if (p.rm != 0 && p.gm == 0 && p.bm == 0)
luminance = true;
else if (p.rm != 0 && p.gm != 0 && p.bm != 0)
rgb = true;
// alpha OR luminance but not both
if ((alpha && !rgb && !luminance) || (luminance && !alpha && !rgb)) {
out = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
} else if ((rgb && alpha) || (luminance && alpha)) {
if (do16bit) {
if (fullalpha) {
ColorModel model = AWTLoader.AWT_RGBA4444;
WritableRaster raster = model.createCompatibleWritableRaster(width, width);
out = new BufferedImage(model, raster, false, null);
} else {
// RGB5_A1
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = { 5, 5, 5, 1 };
int[] bOffs = { 0, 1, 2, 3 };
ColorModel colorModel = new ComponentColorModel(cs, nBits, true, false, Transparency.BITMASK, DataBuffer.TYPE_BYTE);
WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 2, 2, bOffs, null);
out = new BufferedImage(colorModel, raster, false, null);
}
} else {
out = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
} else {
if (do16bit) {
out = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_565_RGB);
} else {
out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}
}
int expansionA = 8 - Integer.bitCount(p.am);
int expansionR = 8 - Integer.bitCount(p.rm);
int expansionG = 8 - Integer.bitCount(p.gm);
int expansionB = 8 - Integer.bitCount(p.bm);
if (expansionR < 0) {
expansionR = 0;
}
int mipPos = 0;
for (int i = 0; i < mipLevel; i++) {
mipPos += image.getMipMapSizes()[i];
}
int inputPixel;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int i = mipPos + (Ix(x, y, width) * p.bpp);
inputPixel = (readPixel(buf, i, p.bpp) & p.im) >> p.is;
int a = (inputPixel & p.am) >> p.as;
int r = (inputPixel & p.rm) >> p.rs;
int g = (inputPixel & p.gm) >> p.gs;
int b = (inputPixel & p.bm) >> p.bs;
r = r & 0xff;
g = g & 0xff;
b = b & 0xff;
a = a & 0xff;
a = a << expansionA;
r = r << expansionR;
g = g << expansionG;
b = b << expansionB;
if (luminance)
b = g = r;
if (!alpha)
a = 0xff;
int argb = (a << 24) | (r << 16) | (g << 8) | b;
out.setRGB(x, y, argb);
}
}
return out;
}
use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class MipMapGenerator method generateMipMaps.
public static void generateMipMaps(Image image) {
BufferedImage original = ImageToAwt.convert(image, false, true, 0);
int width = original.getWidth();
int height = original.getHeight();
int level = 0;
BufferedImage current = original;
AWTLoader loader = new AWTLoader();
ArrayList<ByteBuffer> output = new ArrayList<ByteBuffer>();
int totalSize = 0;
Format format = null;
while (height >= 1 || width >= 1) {
Image converted = loader.load(current, false);
format = converted.getFormat();
output.add(converted.getData(0));
totalSize += converted.getData(0).capacity();
if (height == 1 || width == 1) {
break;
}
level++;
height /= 2;
width /= 2;
current = scaleDown(current, width, height);
}
ByteBuffer combinedData = BufferUtils.createByteBuffer(totalSize);
int[] mipSizes = new int[output.size()];
for (int i = 0; i < output.size(); i++) {
ByteBuffer data = output.get(i);
data.clear();
combinedData.put(data);
mipSizes[i] = data.capacity();
}
combinedData.flip();
// insert mip data into image
image.setData(0, combinedData);
image.setMipMapSizes(mipSizes);
image.setFormat(format);
}
Aggregations