📄 textureloader.java
字号:
* @param fname The file that specifies an Image to load the texture with * @param flags The flags specify what options to use in texture loading (generate mipmap etc) * @param observer The associated image observer * * @exception ImageException if there is a problem reading the image */ public TextureLoader(String fname, int flags, Component observer) { this(fname, null, flags, observer); } /** * Contructs a TextureLoader object using the specified file, * format and option flags * @param fname The file that specifies an Image to load the texture with * @param format The format specifies which channels to use * @param flags The flags specify what options to use in texture loading (generate mipmap etc) * @param observer The associated image observer * * @exception ImageException if there is a problem reading the image */ public TextureLoader(final String fname, String format, int flags, Component observer) { if (observer == null) { observer = new java.awt.Container(); } bufferedImage = (BufferedImage) java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Object run() { try { return ImageIO.read(new File(fname)); } catch (IOException e) { throw new ImageException(e); } } } ); if (bufferedImage==null) { throw new ImageException("Error loading image: " + fname); } parseFormat(format); this.flags = flags; if (format==null) chooseFormat(bufferedImage); if ((flags & BY_REFERENCE) != 0) { byRef = true; } if ((flags & Y_UP) != 0) { yUp = true; } if ((flags & ALLOW_NON_POWER_OF_TWO) != 0) { forcePowerOfTwo = false; } } /** * Contructs a TextureLoader object using the specified URL * and default format RGBA * @param url The URL that specifies an Image to load the texture with * @param observer The associated image observer * * @exception ImageException if there is a problem reading the image */ public TextureLoader(URL url, Component observer) { this(url, null, 0, observer); } /** * Contructs a TextureLoader object using the specified URL, * and format * @param url The URL that specifies an Image to load the texture with * @param format The format specifies which channels to use * @param observer The associated image observer * * @exception ImageException if there is a problem reading the image */ public TextureLoader(URL url, String format, Component observer) { this(url, format, 0, observer); } /** * Contructs a TextureLoader object using the specified URL, * option flags and default format RGBA * @param url The URL that specifies an Image to load the texture with * @param flags The flags specify what options to use in texture loading (generate mipmap etc) * @param observer The associated image observer * * @exception ImageException if there is a problem reading the image */ public TextureLoader(URL url, int flags, Component observer) { this(url, null, flags, observer); } /** * Contructs a TextureLoader object using the specified URL, * format and option flags * @param url The url that specifies an Image to load the texture with * @param format The format specifies which channels to use * @param flags The flags specify what options to use in texture loading (generate mipmap etc) * @param observer The associated image observer * * @exception ImageException if there is a problem reading the image */ public TextureLoader(final URL url, String format, int flags, Component observer) { if (observer == null) { observer = new java.awt.Container(); } bufferedImage = (BufferedImage) java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Object run() { try { return ImageIO.read(url); } catch (IOException e) { throw new ImageException(e); } } } ); if (bufferedImage==null) { throw new ImageException("Error loading image: " + url.toString()); } parseFormat(format); this.flags = flags; if (format==null) chooseFormat(bufferedImage); if ((flags & BY_REFERENCE) != 0) { byRef = true; } if ((flags & Y_UP) != 0) { yUp = true; } if ((flags & ALLOW_NON_POWER_OF_TWO) != 0) { forcePowerOfTwo = false; } } /** * Returns the associated ImageComponent2D object * * @return The associated ImageComponent2D object */ public ImageComponent2D getImage() { if (imageComponent == null) imageComponent = new ImageComponent2D(imageComponentFormat, bufferedImage, byRef, yUp); return imageComponent; } /** * Returns the scaled ImageComponent2D object * * @param xScale The X scaling factor * @param yScale The Y scaling factor * * @return The scaled ImageComponent2D object */ public ImageComponent2D getScaledImage(float xScale, float yScale) { if (xScale == 1.0f && yScale == 1.0f) return getImage(); else return(new ImageComponent2D(imageComponentFormat, getScaledImage(bufferedImage, xScale, yScale), byRef, yUp)); } /** * Returns the scaled ImageComponent2D object * * @param width The desired width * @param height The desired height * * @return The scaled ImageComponent2D object */ public ImageComponent2D getScaledImage(int width, int height) { if (bufferedImage.getWidth() == width && bufferedImage.getHeight() == height) return getImage(); else return(new ImageComponent2D(imageComponentFormat, getScaledImage(bufferedImage, width, height), byRef, yUp)); } /** * Returns the associated Texture object. * * @return The associated Texture object */ public Texture getTexture() { ImageComponent2D[] scaledImageComponents = null; BufferedImage[] scaledBufferedImages = null; if (tex == null) { int width; int height; if (forcePowerOfTwo) { width = getClosestPowerOf2(bufferedImage.getWidth()); height = getClosestPowerOf2(bufferedImage.getHeight()); } else { width = bufferedImage.getWidth(); height = bufferedImage.getHeight(); } if ((flags & GENERATE_MIPMAP) != 0) { BufferedImage origImage = bufferedImage; int newW = width; int newH = height; int level = Math.max(computeLog(width), computeLog(height)) + 1; scaledImageComponents = new ImageComponent2D[level]; scaledBufferedImages = new BufferedImage[level]; tex = new Texture2D(tex.MULTI_LEVEL_MIPMAP, textureFormat, width, height); for (int i = 0; i < level; i++) { scaledBufferedImages[i] = getScaledImage(origImage, newW, newH); scaledImageComponents[i] = new ImageComponent2D( imageComponentFormat, scaledBufferedImages[i], byRef, yUp); tex.setImage(i, scaledImageComponents[i]); if (forcePowerOfTwo) { if (newW > 1) newW >>= 1; if (newH > 1) newH >>= 1; } else { if (newW > 1) { newW = (int) Math.floor(newW / 2.0); } if (newH > 1) { newH = (int) Math.floor(newH / 2.0); } } origImage = scaledBufferedImages[i]; } } else { scaledImageComponents = new ImageComponent2D[1]; scaledBufferedImages = new BufferedImage[1]; // Create texture from image scaledBufferedImages[0] = getScaledImage(bufferedImage, width, height); scaledImageComponents[0] = new ImageComponent2D( imageComponentFormat, scaledBufferedImages[0], byRef, yUp); tex = new Texture2D(tex.BASE_LEVEL, textureFormat, width, height); tex.setImage(0, scaledImageComponents[0]); } tex.setMinFilter(tex.BASE_LEVEL_LINEAR); tex.setMagFilter(tex.BASE_LEVEL_LINEAR); } return tex; } // create a BufferedImage from an Image object private BufferedImage createBufferedImage(Image image, Component observer) { int status; observer.prepareImage(image, null); while(true) { status = observer.checkImage(image, null); if ((status & ImageObserver.ERROR) != 0) { return null; } else if ((status & ImageObserver.ALLBITS) != 0) { break; } try { Thread.sleep(100); } catch (InterruptedException e) {} } int width = image.getWidth(observer); int height = image.getHeight(observer); WritableRaster wr = java.awt.image.Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 4, 4, bandOffset, null); BufferedImage bImage = new BufferedImage(colorModel, wr, false, null); java.awt.Graphics g = bImage.getGraphics(); g.drawImage(image, 0, 0, observer); return bImage; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -