use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class KTXWriter method write.
/**
* Writes an image with the given params
*
* textureType, allows one to write textureArrays, Texture3D, and TextureCubeMaps.
* Texture2D will write a 2D image.
* Note that the fileName should contain the extension (.ktx sounds like a wise choice)
* @param image the image to write
* @param textureType the texture type
* @param fileName the name of the file to write
*/
public void write(Image image, Class<? extends Texture> textureType, String fileName) {
FileOutputStream outs = null;
try {
File file = new File(filePath + "/" + fileName);
outs = new FileOutputStream(file);
DataOutput out = new DataOutputStream(outs);
//fileID
out.write(fileIdentifier);
//endianness
out.writeInt(0x04030201);
GLImageFormat format = getGlFormat(image.getFormat());
//glType
out.writeInt(format.dataType);
//glTypeSize
out.writeInt(1);
//glFormat
out.writeInt(format.format);
//glInernalFormat
out.writeInt(format.internalFormat);
//glBaseInternalFormat
out.writeInt(format.format);
//pixelWidth
out.writeInt(image.getWidth());
//pixelHeight
out.writeInt(image.getHeight());
int pixelDepth = 1;
int numberOfArrayElements = 1;
int numberOfFaces = 1;
if (image.getDepth() > 1) {
//pixelDepth
if (textureType == Texture3D.class) {
pixelDepth = image.getDepth();
}
}
if (image.getData().size() > 1) {
//numberOfArrayElements
if (textureType == TextureArray.class) {
numberOfArrayElements = image.getData().size();
}
//numberOfFaces
if (textureType == TextureCubeMap.class) {
numberOfFaces = image.getData().size();
}
}
out.writeInt(pixelDepth);
out.writeInt(numberOfArrayElements);
out.writeInt(numberOfFaces);
int numberOfMipmapLevels = 1;
//numberOfMipmapLevels
if (image.hasMipmaps()) {
numberOfMipmapLevels = image.getMipMapSizes().length;
}
out.writeInt(numberOfMipmapLevels);
//bytesOfKeyValueData
String keyValues = "KTXorientation\0S=r,T=u\0";
int bytesOfKeyValueData = keyValues.length() + 4;
int padding = 3 - ((bytesOfKeyValueData + 3) % 4);
bytesOfKeyValueData += padding;
out.writeInt(bytesOfKeyValueData);
//keyAndValueByteSize
out.writeInt(bytesOfKeyValueData - 4 - padding);
//values
out.writeBytes(keyValues);
pad(padding, out);
int offset = 0;
//iterate over data
for (int mipLevel = 0; mipLevel < numberOfMipmapLevels; mipLevel++) {
int width = Math.max(1, image.getWidth() >> mipLevel);
int height = Math.max(1, image.getHeight() >> mipLevel);
int imageSize;
if (image.hasMipmaps()) {
imageSize = image.getMipMapSizes()[mipLevel];
} else {
imageSize = width * height * image.getFormat().getBitsPerPixel() / 8;
}
out.writeInt(imageSize);
for (int arrayElem = 0; arrayElem < numberOfArrayElements; arrayElem++) {
for (int face = 0; face < numberOfFaces; face++) {
int nbPixelWritten = 0;
for (int depth = 0; depth < pixelDepth; depth++) {
ByteBuffer byteBuffer = image.getData(getSlice(face, arrayElem));
// BufferUtils.ensureLargeEnough(byteBuffer, imageSize);
log.log(Level.FINE, "position {0}", byteBuffer.position());
byteBuffer.position(offset);
byte[] b = getByteBufferArray(byteBuffer, imageSize);
out.write(b);
nbPixelWritten = b.length;
}
//cube padding
if (numberOfFaces == 6 && numberOfArrayElements == 0) {
padding = 3 - ((nbPixelWritten + 3) % 4);
pad(padding, out);
}
}
}
//mip padding
log.log(Level.FINE, "skipping {0}", (3 - ((imageSize + 3) % 4)));
padding = 3 - ((imageSize + 3) % 4);
pad(padding, out);
offset += imageSize;
}
} catch (FileNotFoundException ex) {
Logger.getLogger(KTXWriter.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(KTXWriter.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (outs != null) {
outs.close();
}
} catch (IOException ex) {
Logger.getLogger(KTXWriter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class DDSLoader method load.
public Image load(InputStream stream) throws IOException {
in = new LittleEndien(stream);
loadHeader();
ArrayList<ByteBuffer> data = readData(false);
return new Image(pixelFormat, width, height, depth, data, sizes, ColorSpace.sRGB);
}
use of com.jme3.texture.Image 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.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class AndroidNativeImageLoader method load.
public Image load(AssetInfo info) throws IOException {
boolean flip = ((TextureKey) info.getKey()).isFlipY();
InputStream in = null;
try {
in = info.openStream();
return load(info.openStream(), flip, tmpArray);
} finally {
if (in != null) {
in.close();
}
}
}
use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class GLRenderer method updateRenderTexture.
public void updateRenderTexture(FrameBuffer fb, RenderBuffer rb) {
Texture tex = rb.getTexture();
Image image = tex.getImage();
if (image.isUpdateNeeded()) {
// Check NPOT requirements
checkNonPowerOfTwo(tex);
updateTexImageData(image, tex.getType(), 0, false);
// NOTE: For depth textures, sets nearest/no-mips mode
// Required to fix "framebuffer unsupported"
// for old NVIDIA drivers!
setupTextureParams(0, tex);
}
if (rb.getLayer() < 0) {
glfbo.glFramebufferTexture2DEXT(GLFbo.GL_FRAMEBUFFER_EXT, convertAttachmentSlot(rb.getSlot()), convertTextureType(tex.getType(), image.getMultiSamples(), rb.getFace()), image.getId(), 0);
} else {
gl3.glFramebufferTextureLayer(GLFbo.GL_FRAMEBUFFER_EXT, convertAttachmentSlot(rb.getSlot()), image.getId(), 0, rb.getLayer());
}
}
Aggregations