📄 textureloader.java
字号:
/* * $RCSfile: TextureLoader.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.12 $ * $Date: 2007/04/03 23:48:44 $ * $State: Exp $ */package com.sun.j3d.utils.image;import javax.media.j3d.*;import java.awt.Image;import java.awt.Component;import java.awt.Transparency;import java.awt.color.ColorSpace;import java.awt.geom.AffineTransform;import java.awt.image.*;import java.io.File;import java.io.IOException;import java.net.URL;import java.lang.reflect.Method;import javax.imageio.ImageIO;/** * This class is used for loading a texture from an Image or BufferedImage. * The Image I/O API is used to load the images. (If the JAI IIO Tools * package is available, a larger set of formats can be loaded, including * TIFF, JPEG2000, and so on.) * * Methods are provided to retrieve the Texture object and the associated * ImageComponent object or a scaled version of the ImageComponent object. * * Default format is RGBA. Other legal formats are: RGBA, RGBA4, RGB5_A1, * RGB, RGB4, RGB5, R3_G3_B2, LUM8_ALPHA8, LUM4_ALPHA4, LUMINANCE and ALPHA */public class TextureLoader extends Object { /** * Optional flag - specifies that mipmaps are generated for all levels */ public static final int GENERATE_MIPMAP = 0x01; /** * Optional flag - specifies that the ImageComponent2D will * access the image data by reference * * @since Java 3D 1.2 */ public static final int BY_REFERENCE = 0x02; /** * Optional flag - specifies that the ImageComponent2D will * have a y-orientation of y up, meaning the origin of the image is the * lower left * * @since Java 3D 1.2 */ public static final int Y_UP = 0x04; /** * Optional flag - specifies that the ImageComponent2D is allowed * to have dimensions that are not a power of two. If this flag is set, * TextureLoader will not perform any scaling of images. If this flag * is not set, images will be scaled to the nearest power of two. This is * the default mode. * <p> * Note that non-power-of-two textures may not be supported by all graphics * cards. Applications should check whether a particular Canvas3D supports * non-power-of-two textures by calling the {@link Canvas3D#queryProperties} * method, and checking whether the * <code>textureNonPowerOfTwoAvailable</code> property is set to true. * * @since Java 3D 1.5 */ public static final int ALLOW_NON_POWER_OF_TWO = 0x08; /* * Private declaration for BufferedImage allocation */ private static ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); private static int[] nBits = {8, 8, 8, 8}; private static int[] bandOffset = { 0, 1, 2, 3}; private static ComponentColorModel colorModel = new ComponentColorModel(cs, nBits, true, false, Transparency.TRANSLUCENT, 0); private Texture2D tex = null; private BufferedImage bufferedImage = null; private ImageComponent2D imageComponent = null; private int textureFormat = Texture.RGBA; private int imageComponentFormat = ImageComponent.FORMAT_RGBA; private int flags; private boolean byRef = false; private boolean yUp = false; private boolean forcePowerOfTwo = true; /** * Contructs a TextureLoader object using the specified BufferedImage * and default format RGBA * @param bImage The BufferedImage used for loading the texture * * @exception NullPointerException if bImage is null */ public TextureLoader(BufferedImage bImage) { this(bImage, null, 0); } /** * Contructs a TextureLoader object using the specified BufferedImage * and format * @param bImage The BufferedImage used for loading the texture * @param format The format specifies which channels to use * * @exception NullPointerException if bImage is null */ public TextureLoader(BufferedImage bImage, String format) { this(bImage, format, 0); } /** * Contructs a TextureLoader object using the specified BufferedImage, * option flags and default format RGBA * @param bImage The BufferedImage used for loading the texture * @param flags The flags specify what options to use in texture loading (generate mipmap etc) * * @exception NullPointerException if bImage is null */ public TextureLoader(BufferedImage bImage, int flags) { this(bImage, null, flags); } /** * Contructs a TextureLoader object using the specified BufferedImage, * format and option flags * @param bImage The BufferedImage used for loading the texture * @param format The format specifies which channels to use * @param flags The flags specify what options to use in texture loading (generate mipmap etc) * * @exception NullPointerException if bImage is null */ public TextureLoader(BufferedImage bImage, String format, int flags) { if (bImage == null) { throw new NullPointerException(); } parseFormat(format); this.flags = flags; bufferedImage = bImage; 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 Image * and default format RGBA * @param image The Image used for loading the texture * @param observer The associated image observer * * @exception NullPointerException if image is null * @exception ImageException if there is a problem loading the image */ public TextureLoader(Image image, Component observer) { this(image, null, 0, observer); } /** * Contructs a TextureLoader object using the specified Image * and format * @param image The Image used for loading the texture * @param format The format specifies which channels to use * @param observer The associated image observer * * @exception NullPointerException if image is null * @exception ImageException if there is a problem loading the image */ public TextureLoader(Image image, String format, Component observer) { this(image, format, 0, observer); } /** * Contructs a TextureLoader object using the specified Image * flags and default format RGBA * @param image The Image used for loading the texture * @param flags The flags specify what options to use in texture loading (generate mipmap etc) * @param observer The associated image observer * * @exception NullPointerException if image is null * @exception ImageException if there is a problem loading the image */ public TextureLoader(Image image, int flags, Component observer) { this(image, null, flags, observer); } /** * Contructs a TextureLoader object using the specified Image * format and option flags * @param image The Image used for loading the texture * @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 NullPointerException if image is null * @exception ImageException if there is a problem loading the image */ public TextureLoader(Image image, String format, int flags, Component observer) { if (image == null) { throw new NullPointerException(); } if (observer == null) { observer = new java.awt.Container(); } parseFormat(format); this.flags = flags; bufferedImage = createBufferedImage(image, observer); if (bufferedImage==null) { throw new ImageException("Error loading image: " + image.toString()); } 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 file * and default format RGBA * @param fname The file 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(String fname, Component observer) { this(fname, null, 0, observer); } /** * Contructs a TextureLoader object using the specified file, * and format * @param fname The file 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(String fname, String format, Component observer) { this(fname, format, 0, observer); } /** * Contructs a TextureLoader object using the specified file, * option flags and default format RGBA
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -