📄 texturemanager.java
字号:
}
if (imageType.getColorModel().getTransparency() == ColorModel.OPAQUE) {
image = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
} else {
image = new BufferedImage(width, height,
BufferedImage.TYPE_4BYTE_ABGR);
}
ImageReadParam param = reader.getDefaultReadParam();
param.setDestination(image);
image = reader.read(0, param);
reader.dispose();
return image;
}
/**
* <code>loadImage</code> sets the image data. It will set the jme Image's
* format to RGB8 or RGBA8 or Alpha8 depending on the incoming java Image.
* (greyscale will be sent back as Alpha8)
*
* @param image
* The image data.
* @param flipImage
* if true will flip the image's y values.
* @return the loaded image.
*/
public static com.jme.image.Image loadImage(java.awt.Image image,
boolean flipImage) {
if (image == null)
return null;
boolean hasAlpha = hasAlpha(image), grayscale = isGreyscale(image);
BufferedImage tex;
if (flipImage
|| !(image instanceof BufferedImage)
|| (((BufferedImage) image).getType() != BufferedImage.TYPE_BYTE_GRAY && (hasAlpha ? ((BufferedImage) image)
.getType() != BufferedImage.TYPE_4BYTE_ABGR
: ((BufferedImage) image).getType() != BufferedImage.TYPE_3BYTE_BGR))) {
// Obtain the image data.
try {
tex = new BufferedImage(image.getWidth(null), image
.getHeight(null), grayscale ? BufferedImage.TYPE_BYTE_GRAY
: hasAlpha ? BufferedImage.TYPE_4BYTE_ABGR
: BufferedImage.TYPE_3BYTE_BGR);
} catch (IllegalArgumentException e) {
logger.warning("Problem creating buffered Image: "
+ e.getMessage());
return TextureState.getDefaultTextureImage();
}
image.getWidth(null);
image.getHeight(null);
if (image instanceof BufferedImage) {
int imageWidth = image.getWidth(null);
int[] tmpData = new int[imageWidth];
int row = 0;
BufferedImage bufferedImage = ((BufferedImage) image);
for (int y = image.getHeight(null) - 1; y >= 0; y--) {
bufferedImage.getRGB(0, (flipImage ? row++ : y),
imageWidth, 1, tmpData, 0, imageWidth);
tex.setRGB(0, y, imageWidth, 1, tmpData, 0, imageWidth);
}
} else {
AffineTransform tx = null;
if (flipImage) {
tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
}
Graphics2D g = (Graphics2D) tex.getGraphics();
g.drawImage(image, tx, null);
g.dispose();
}
} else {
tex = (BufferedImage) image;
}
// Get a pointer to the image memory
byte data[] = (byte[]) tex.getRaster().getDataElements(0, 0,
tex.getWidth(), tex.getHeight(), null);
ByteBuffer scratch = createOnHeap ? BufferUtils.createByteBufferOnHeap(data.length) : BufferUtils.createByteBuffer(data.length);
scratch.clear();
scratch.put(data);
scratch.flip();
com.jme.image.Image textureImage = new com.jme.image.Image();
textureImage.setFormat(grayscale ? Image.Format.Alpha8 : hasAlpha ? Image.Format.RGBA8
: Image.Format.RGB8);
textureImage.setWidth(tex.getWidth());
textureImage.setHeight(tex.getHeight());
textureImage.setData(scratch);
return textureImage;
}
/**
* <code>loadBMPImage</code> because bitmap is not directly supported by
* Java, we must load it manually. The requires opening a stream to the file
* and reading in each byte. After the image data is read, it is used to
* create a new <code>Image</code> object. This object is returned to be
* used for normal use.
*
* @param fs
* The bitmap file stream.
* @return <code>Image</code> object that contains the bitmap information.
*/
private static java.awt.Image loadBMPImage(InputStream fs) {
try {
DataInputStream dis = new DataInputStream(fs);
BitmapHeader bh = new BitmapHeader();
byte[] data = new byte[dis.available()];
dis.readFully(data);
dis.close();
bh.read(data);
if (bh.bitcount == 24) {
return (bh.readMap24(data));
}
if (bh.bitcount == 32) {
return (bh.readMap32(data));
}
if (bh.bitcount == 8) {
return (bh.readMap8(data));
}
} catch (IOException e) {
logger.warning("Error while loading bitmap texture.");
}
return null;
}
/**
* <code>hasAlpha</code> returns true if the specified image has
* transparent pixels
*
* @param image
* Image to check
* @return true if the specified image has transparent pixels
*/
public static boolean hasAlpha(java.awt.Image image) {
if (null == image) {
return false;
}
if (image instanceof BufferedImage) {
BufferedImage bufferedImage = (BufferedImage) image;
return bufferedImage.getColorModel().hasAlpha();
}
PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pixelGrabber.grabPixels();
ColorModel colorModel = pixelGrabber.getColorModel();
if (colorModel != null) {
return colorModel.hasAlpha();
}
return false;
} catch (InterruptedException e) {
logger.warning("Unable to determine alpha of image: " + image);
}
return false;
}
/**
* <code>isGreyscale</code> returns true if the specified image is greyscale.
*
* @param image
* Image to check
* @return true if the specified image is greyscale.
*/
public static boolean isGreyscale(java.awt.Image image) {
if (null == image) {
return false;
}
if (image instanceof BufferedImage) {
BufferedImage bufferedImage = (BufferedImage) image;
return bufferedImage.getColorModel().getNumComponents() == 1;
}
PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pixelGrabber.grabPixels();
ColorModel colorModel = pixelGrabber.getColorModel();
if (colorModel != null) {
return colorModel.getNumComponents() == 1;
}
return false;
} catch (InterruptedException e) {
logger.warning("Unable to determine if image is greyscale: " + image);
}
return false;
}
public static boolean releaseTexture(Texture texture) {
if (texture == null)
return false;
Collection<TextureKey> c = m_tCache.keySet();
Iterator<TextureKey> it = c.iterator();
TextureKey key;
Texture next;
while (it.hasNext()) {
key = it.next();
next = m_tCache.get(key);
if (texture.equals(next)) {
return releaseTexture(key);
}
}
return false;
}
public static boolean releaseTexture(TextureKey tKey) {
return m_tCache.remove(tKey) != null;
}
public static void clearCache() {
m_tCache.clear();
}
/**
* Register an ImageLoader to handle all files with a specific extention. An
* ImageLoader can be registered to handle several formats without problems.
*
* @param format
* The file extention for the format this ImageLoader will
* handle. Make sure to include the dot (eg. ".BMP"). This value
* is case insensitive (".Bmp" will register for ".BMP", ".bmp",
* etc.)
* @param handler
*/
public static void registerHandler(String format, ImageLoader handler) {
loaders.put(format.toLowerCase(), handler);
}
public static void unregisterHandler(String format) {
loaders.remove(format.toLowerCase());
}
public static void registerForCleanup(TextureKey textureKey, int textureId) {
Texture t = m_tCache.get(textureKey);
if (t != null) {
t.setTextureId(textureId);
}
cleanupStore.add(textureId);
}
public static void doTextureCleanup() {
if (DisplaySystem.getDisplaySystem() == null
|| DisplaySystem.getDisplaySystem().getRenderer() == null)
return;
TextureState ts = (TextureState)Renderer.defaultStateList[RenderState.StateType.Texture.ordinal()];
for (Integer i : cleanupStore) {
if (i != null) {
try {
ts.deleteTextureId(i.intValue());
} catch (Exception e) {
} // ignore.
}
}
}
public static void deleteTextureFromCard(Texture tex) {
if (tex == null || DisplaySystem.getDisplaySystem() == null
|| DisplaySystem.getDisplaySystem().getRenderer() == null)
return;
TextureState ts = (TextureState)Renderer.defaultStateList[RenderState.StateType.Texture.ordinal()];
try {
ts.deleteTextureId(tex.getTextureId());
} catch (Exception e) {
} // ignore.
}
public static Texture findCachedTexture(TextureKey textureKey) {
return m_tCache.get(textureKey);
}
public static void preloadCache(Renderer r) {
TextureState ts = r.createTextureState();
for (Texture t : m_tCache.values()) {
if (t.getTextureKey().location != null) {
ts.setTexture(t);
ts.load(0);
}
}
}
public static void setCreateOnHeap(boolean createOnHeap) {
TextureManager.createOnHeap = createOnHeap;
}
public static boolean isCreateOnHeap() {
return createOnHeap;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -