Dimension imageSize = new Dimension(), textureSize = new Dimension();
DataBuffer dataBuffer = null;
int bands = 0;
int transferMask = transferMode & bitStack._getAllBits();
if ((transferMask & (TRANSFER_ABGR_TO_BGRA | TRANSFER_ABGR_TO_RGBA | TRANSFER_BGR_TO_RGB)) == 0 && transferMask != 0) {
throw new JXAException("invalid transfer mode (use only ABGR->any or BGR->any conversions at loading !)");
}
Image bi = null;
if (src instanceof Image) {
bi = (Image) src;
} else if (src instanceof String || src instanceof File) {
File f = src instanceof String ? new File((String) src) : (File) src;
bi = ImageIO.read(f);
} else if (src instanceof URL) {
bi = ImageIO.read((URL) src);
} else if (src instanceof InputStream) {
bi = ImageIO.read((InputStream) src);
} else if (src instanceof ImageReader) {
ImageReader r = ((ImageReader) src);
bi = r.read(0, r.getDefaultReadParam());
SpriteIO._mt.addImage(bi, 0);
SpriteIO._mt.waitForID(0);
SpriteIO._mt.removeImage(bi);
r.dispose();
} else {
throw new JXAException("src is not one of the supported class.");
}
Sprite sp = null;
boolean flushSp = false;
ImageTypeSpecifier type = SpriteIO._getITS(DEFAULT_TYPE);
if (!(src instanceof Sprite)) {
sp = new Sprite(bi, "image/x-png", imageSize = new Dimension(bi.getWidth(null), bi.getHeight(null)));
if (bi instanceof BufferedImage) {
type = SpriteIO._getITS((BufferedImage) bi);
}
flushSp = true;
} else {
sp = (Sprite) src;
type = sp.getFeatures();
imageSize = sp.getSize();
}
sp.setBufferedType(type.getNumComponents() == 3 ? BufferedImage.TYPE_3BYTE_BGR : DEFAULT_TYPE);
BufferedImage data = null;
if (sp != null) {
/**
* provide a shortcut to texture rectangle (no resizing) ^^
*/
if (_EXTtex == ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB) {
textureSize = imageSize.getSize();
} else {
textureSize.width = Sf3Texture._getNextPowerOfTwo(imageSize.width);
textureSize.height = Sf3Texture._getNextPowerOfTwo(imageSize.height);
}
sp.setSize(textureSize);
sp.runValidate();
data = sp.toBuffered();
textureSize.setSize(data.getWidth(), data.getHeight());
Raster dataRaster = data.getRaster();
dataBuffer = dataRaster.getDataBuffer();
bands = dataRaster.getNumBands();
if (JXAenvUtils._debug) {
System.out.println("bufferedimage for texture has " + data.getRaster().getDataBuffer().getNumBanks() + " banks");
}
} else {
return null;
}
Buffer pixelBuffer = null;
switch (dataBuffer.getDataType()) {
case DataBuffer.TYPE_BYTE:
pixelBuffer = BufferIO._wrap(((DataBufferByte) dataBuffer).getData()).order(ByteOrder.nativeOrder());
break;
case DataBuffer.TYPE_DOUBLE:
pixelBuffer = BufferIO._wrapd(((DataBufferDouble) dataBuffer).getData());
break;
case DataBuffer.TYPE_FLOAT:
pixelBuffer = BufferIO._wrapf(((DataBufferFloat) dataBuffer).getData());
break;
case DataBuffer.TYPE_INT:
pixelBuffer = BufferIO._wrapi(((DataBufferInt) dataBuffer).getData());
break;
case DataBuffer.TYPE_SHORT:
pixelBuffer = BufferIO._wraps(((DataBufferShort) dataBuffer).getData());
break;
case DataBuffer.TYPE_USHORT:
pixelBuffer = BufferIO._wraps(((DataBufferUShort) dataBuffer).getData());
break;
default:
throw new JXAException("incompatible or unknown BufferedImage type :" + texture.toString());
}
data.flush();
pixelBuffer = _convert2DTexBuffer(pixelBuffer, textureSize, bands, transferMode, dataBuffer.getDataType());
if (pixelBuffer != null) {
/**
* instance Sf3Texture with the filled up pixelBuffer
*/
texture = new Sf3Texture(hashCode, pixelBuffer, textureSize.width, textureSize.height, bands, imageSize, transferMode);
if (JXAenvUtils._debug) {
System.err.println("new Sf3Texture buffer " + BufferIO._getBufferDataTypeName(pixelBuffer) + " size : " + BufferIO._countByteSize(pixelBuffer) + " w/ " + bands + " bands is POT ? : " + (isPOT((float) pixelBuffer.limit() / (float) bands) ? "yes" : "no"));
}
} else {
throw new JXAException("pixel buffer is null, no data could be loaded");
}
if (flushSp) {
sp.clearResource();
}
return texture;