use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.
the class AndroidBufferImageLoader method load.
public Object load(AssetInfo assetInfo) throws IOException {
Bitmap bitmap = null;
Image.Format format;
InputStream in = null;
int bpp;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferQualityOverSpeed = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inTempStorage = tempData;
options.inScaled = false;
options.inDither = false;
options.inInputShareable = true;
options.inPurgeable = true;
options.inSampleSize = 1;
try {
in = assetInfo.openStream();
bitmap = BitmapFactory.decodeStream(in, null, options);
if (bitmap == null) {
throw new IOException("Failed to load image: " + assetInfo.getKey().getName());
}
} finally {
if (in != null) {
in.close();
}
}
switch(bitmap.getConfig()) {
case ALPHA_8:
format = Image.Format.Alpha8;
bpp = 1;
break;
case ARGB_8888:
format = Image.Format.RGBA8;
bpp = 4;
break;
case RGB_565:
format = Image.Format.RGB565;
bpp = 2;
break;
default:
throw new UnsupportedOperationException("Unrecognized Android bitmap format: " + bitmap.getConfig());
}
TextureKey texKey = (TextureKey) assetInfo.getKey();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
ByteBuffer data = BufferUtils.createByteBuffer(bitmap.getWidth() * bitmap.getHeight() * bpp);
if (format == Image.Format.RGBA8) {
int[] pixelData = new int[width * height];
bitmap.getPixels(pixelData, 0, width, 0, 0, width, height);
if (texKey.isFlipY()) {
int[] sln = new int[width];
int y2;
for (int y1 = 0; y1 < height / 2; y1++) {
y2 = height - y1 - 1;
convertARGBtoABGR(pixelData, y1 * width, sln, 0, width);
convertARGBtoABGR(pixelData, y2 * width, pixelData, y1 * width, width);
System.arraycopy(sln, 0, pixelData, y2 * width, width);
}
} else {
convertARGBtoABGR(pixelData, 0, pixelData, 0, pixelData.length);
}
data.asIntBuffer().put(pixelData);
} else {
if (texKey.isFlipY()) {
// Flip the image, then delete the old one.
Matrix flipMat = new Matrix();
flipMat.preScale(1.0f, -1.0f);
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flipMat, false);
bitmap.recycle();
bitmap = newBitmap;
if (bitmap == null) {
throw new IOException("Failed to flip image: " + texKey);
}
}
bitmap.copyPixelsToBuffer(data);
}
data.flip();
bitmap.recycle();
Image image = new Image(format, width, height, data, ColorSpace.sRGB);
return image;
}
use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.
the class VertexBuffer method clone.
/**
* Creates a deep clone of this VertexBuffer but overrides the
* {@link Type}.
*
* @param overrideType The type of the cloned VertexBuffer
* @return A deep clone of the buffer
*/
public VertexBuffer clone(Type overrideType) {
VertexBuffer vb = new VertexBuffer(overrideType);
vb.components = components;
vb.componentsLength = componentsLength;
// Make sure to pass a read-only buffer to clone so that
// the position information doesn't get clobbered by another
// reading thread during cloning (and vice versa) since this is
// a purely read-only operation.
vb.data = BufferUtils.clone(getDataReadOnly());
vb.format = format;
vb.handleRef = new Object();
vb.id = -1;
vb.normalized = normalized;
vb.instanceSpan = instanceSpan;
vb.offset = offset;
vb.stride = stride;
vb.updateNeeded = true;
vb.usage = usage;
return vb;
}
use of com.jme3.scene.VertexBuffer.Format 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.scene.VertexBuffer.Format 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.scene.VertexBuffer.Format 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