diff options
author | Kenneth Russel <[email protected]> | 2006-01-06 08:17:17 +0000 |
---|---|---|
committer | Kenneth Russel <[email protected]> | 2006-01-06 08:17:17 +0000 |
commit | 605053cfa6696ebf5850f62480e37f960a91d127 (patch) | |
tree | 9bf432ee06fa824b606fee0d83868a17f073bf5e /src/classes | |
parent | 39f0a9938af2d6219d4a29c89dbde60f515e2695 (diff) |
Incorporated TextureIO texture loader (and, eventually, writer) and
associated classes for being able to easily read in and display
textures with OpenGL. Collaboration with Chris Campbell from the
Java2D team. Supports multiple file formats both through ImageIO as
well as custom texture loaders for DDS, SGI and TGA files. Writing of
textures back to disk is not yet implemented. Reading of mipmaps from
files which support it as well as autogeneration of mipmaps are not
yet well supported and usage of mipmap arguments in the API still need
to be rethought. Added TestTexture demo to jogl-demos workspace to
show simple usage.
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/trunk@519 232f8b59-042b-4e1e-8c03-345bb8c30851
Diffstat (limited to 'src/classes')
-rwxr-xr-x | src/classes/com/sun/opengl/utils/DDSReader.java | 508 | ||||
-rwxr-xr-x | src/classes/com/sun/opengl/utils/LEDataInputStream.java | 223 | ||||
-rwxr-xr-x | src/classes/com/sun/opengl/utils/SGIImage.java | 390 | ||||
-rwxr-xr-x | src/classes/com/sun/opengl/utils/TGAImage.java | 320 | ||||
-rwxr-xr-x | src/classes/com/sun/opengl/utils/Texture.java | 440 | ||||
-rwxr-xr-x | src/classes/com/sun/opengl/utils/TextureData.java | 447 | ||||
-rwxr-xr-x | src/classes/com/sun/opengl/utils/TextureIO.java | 794 | ||||
-rwxr-xr-x | src/classes/com/sun/opengl/utils/TextureProvider.java | 151 |
8 files changed, 3273 insertions, 0 deletions
diff --git a/src/classes/com/sun/opengl/utils/DDSReader.java b/src/classes/com/sun/opengl/utils/DDSReader.java new file mode 100755 index 000000000..dbbf8e65b --- /dev/null +++ b/src/classes/com/sun/opengl/utils/DDSReader.java @@ -0,0 +1,508 @@ +/* + * Copyright (c) 2005 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 or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.sun.opengl.utils; + +import java.io.*; +import java.nio.*; +import java.nio.channels.*; + +/** A reader for DirectDraw Surface (.dds) files, which are used to + describe textures. These files can contain multiple mipmap levels + in one file. This reader is currently minimal and does not support + all of the possible file formats. */ + +public class DDSReader { + + /** Simple class describing images and data; does not encapsulate + image format information. User is responsible for transmitting + that information in another way. */ + + public static class ImageInfo { + private ByteBuffer data; + private int width; + private int height; + private boolean isCompressed; + private int compressionFormat; + + public ImageInfo(ByteBuffer data, int width, int height, boolean compressed, int compressionFormat) { + this.data = data; this.width = width; this.height = height; + this.isCompressed = compressed; this.compressionFormat = compressionFormat; + } + public int getWidth() { return width; } + public int getHeight() { return height; } + public ByteBuffer getData() { return data; } + public boolean isCompressed() { return isCompressed; } + public int getCompressionFormat() { + if (!isCompressed()) + throw new RuntimeException("Should not call unless compressed"); + return compressionFormat; + } + } + + private FileInputStream fis; + private FileChannel chan; + private ByteBuffer buf; + private Header header; + + // + // Selected bits in header flags + // + + public static final int DDSD_CAPS = 0x00000001; // Capacities are valid + public static final int DDSD_HEIGHT = 0x00000002; // Height is valid + public static final int DDSD_WIDTH = 0x00000004; // Width is valid + public static final int DDSD_PITCH = 0x00000008; // Pitch is valid + public static final int DDSD_BACKBUFFERCOUNT = 0x00000020; // Back buffer count is valid + public static final int DDSD_ZBUFFERBITDEPTH = 0x00000040; // Z-buffer bit depth is valid (shouldn't be used in DDSURFACEDESC2) + public static final int DDSD_ALPHABITDEPTH = 0x00000080; // Alpha bit depth is valid + public static final int DDSD_LPSURFACE = 0x00000800; // lpSurface is valid + public static final int DDSD_PIXELFORMAT = 0x00001000; // ddpfPixelFormat is valid + public static final int DDSD_MIPMAPCOUNT = 0x00020000; // Mip map count is valid + public static final int DDSD_LINEARSIZE = 0x00080000; // dwLinearSize is valid + public static final int DDSD_DEPTH = 0x00800000; // dwDepth is valid + + public static final int DDPF_ALPHAPIXELS = 0x00000001; // Alpha channel is present + public static final int DDPF_ALPHA = 0x00000002; // Only contains alpha information + public static final int DDPF_FOURCC = 0x00000004; // FourCC code is valid + public static final int DDPF_PALETTEINDEXED4 = 0x00000008; // Surface is 4-bit color indexed + public static final int DDPF_PALETTEINDEXEDTO8 = 0x00000010; // Surface is indexed into a palette which stores indices + // into the destination surface's 8-bit palette + public static final int DDPF_PALETTEINDEXED8 = 0x00000020; // Surface is 8-bit color indexed + public static final int DDPF_RGB = 0x00000040; // RGB data is present + public static final int DDPF_COMPRESSED = 0x00000080; // Surface will accept pixel data in the format specified + // and compress it during the write + public static final int DDPF_RGBTOYUV = 0x00000100; // Surface will accept RGB data and translate it during + // the write to YUV data. The format of the data to be written + // will be contained in the pixel format structure. The DDPF_RGB + // flag will be set. + public static final int DDPF_YUV = 0x00000200; // Pixel format is YUV - YUV data in pixel format struct is valid + public static final int DDPF_ZBUFFER = 0x00000400; // Pixel format is a z buffer only surface + public static final int DDPF_PALETTEINDEXED1 = 0x00000800; // Surface is 1-bit color indexed + public static final int DDPF_PALETTEINDEXED2 = 0x00001000; // Surface is 2-bit color indexed + public static final int DDPF_ZPIXELS = 0x00002000; // Surface contains Z information in the pixels + + // Selected bits in DDS capabilities flags + public static final int DDSCAPS_TEXTURE = 0x00001000; // Can be used as a texture + public static final int DDSCAPS_MIPMAP = 0x00400000; // Is one level of a mip-map + + // Known pixel formats + public static final int D3DFMT_UNKNOWN = 0; + public static final int D3DFMT_R8G8B8 = 20; + public static final int D3DFMT_A8R8G8B8 = 21; + public static final int D3DFMT_X8R8G8B8 = 22; + // The following are also valid FourCC codes + public static final int D3DFMT_DXT1 = 0x31545844; + public static final int D3DFMT_DXT2 = 0x32545844; + public static final int D3DFMT_DXT3 = 0x33545844; + public static final int D3DFMT_DXT4 = 0x34545844; + public static final int D3DFMT_DXT5 = 0x35545844; + + public void loadFile(String filename) throws IOException { + loadFile(new File(filename)); + } + + public void loadFile(File file) throws IOException { + fis = new FileInputStream(file); + chan = fis.getChannel(); + buf = chan.map(FileChannel.MapMode.READ_ONLY, + 0, (int) file.length()); + buf.order(ByteOrder.LITTLE_ENDIAN); + header = new Header(); + header.read(buf); + fixupHeader(); + } + + public void close() { + try { + chan.close(); + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** Test for presence/absence of surface description flags (DDSD_*) */ + public boolean isSurfaceDescFlagSet(int flag) { + return ((header.flags & flag) != 0); + } + + /** Test for presence/absence of pixel format flags (DDPF_*) */ + public boolean isPixelFormatFlagSet(int flag) { + return ((header.pfFlags & flag) != 0); + } + + /** Gets the pixel format of this texture (D3DFMT_*) based on some + heuristics. Returns D3DFMT_UNKNOWN if could not recognize the + pixel format. */ + public int getPixelFormat() { + if (isCompressed()) { + return getCompressionFormat(); + } else if (isPixelFormatFlagSet(DDPF_RGB)) { + if (isPixelFormatFlagSet(DDPF_ALPHAPIXELS)) { + if (getDepth() == 32 && + header.pfRBitMask == 0x00FF0000 && + header.pfGBitMask == 0x0000FF00 && + header.pfBBitMask == 0x000000FF && + header.pfABitMask == 0xFF000000) { + return D3DFMT_A8R8G8B8; + } + } else { + if (getDepth() == 24 && + header.pfRBitMask == 0x00FF0000 && + header.pfGBitMask == 0x0000FF00 && + header.pfBBitMask == 0x000000FF) { + return D3DFMT_R8G8B8; + } else if (getDepth() == 32 && + header.pfRBitMask == 0x00FF0000 && + header.pfGBitMask == 0x0000FF00 && + header.pfBBitMask == 0x000000FF) { + return D3DFMT_X8R8G8B8; + } + } + } + + return D3DFMT_UNKNOWN; + } + + /** Indicates whether this texture is compressed. */ + public boolean isCompressed() { + return (isPixelFormatFlagSet(DDPF_FOURCC)); + } + + /** If this surface is compressed, returns the kind of compression + used (DXT1..DXT5). */ + public int getCompressionFormat() { + return header.pfFourCC; + } + + /** Width of the texture (or the top-most mipmap if mipmaps are + present) */ + public int getWidth() { + return header.width; + } + + /** Height of the texture (or the top-most mipmap if mipmaps are + present) */ + public int getHeight() { + return header.height; + } + + /** Total number of bits per pixel. Only valid if DDPF_RGB is + present. For A8R8G8B8, would be 32. */ + public int getDepth() { + return header.pfRGBBitCount; + } + + /** Number of mip maps in the texture */ + public int getNumMipMaps() { + if (!isSurfaceDescFlagSet(DDSD_MIPMAPCOUNT)) { + return 0; + } + return header.mipMapCountOrAux; + } + + /** Gets the <i>i</i>th mipmap data (0..getNumMipMaps() - 1) */ + public ImageInfo getMipMap(int map) { + if (getNumMipMaps() > 0 && + ((map < 0) || (map >= getNumMipMaps()))) { + throw new RuntimeException("Illegal mipmap number " + map + " (0.." + (getNumMipMaps() - 1) + ")"); + } + + // Figure out how far to seek + int seek = 4 + header.size; + for (int i = 0; i < map; i++) { + seek += mipMapSizeInBytes(i); + } + buf.limit(seek + mipMapSizeInBytes(map)); + buf.position(seek); + ByteBuffer next = buf.slice(); + buf.position(0); + buf.limit(buf.capacity()); + return new ImageInfo(next, mipMapWidth(map), mipMapHeight(map), isCompressed(), getCompressionFormat()); + } + + /** Returns an array of ImageInfos corresponding to all mipmap + levels of this DDS file. */ + public ImageInfo[] getAllMipMaps() { + int numLevels = getNumMipMaps(); + if (numLevels == 0) { + numLevels = 1; + } + ImageInfo[] result = new ImageInfo[numLevels]; + for (int i = 0; i < numLevels; i++) { + result[i] = getMipMap(i); + } + return result; + } + + /** Converts e.g. DXT1 compression format constant (see {@link + #getCompressionFormat}) into "DXT1". */ + public static String getCompressionFormatName(int compressionFormat) { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < 4; i++) { + char c = (char) (compressionFormat & 0xFF); + buf.append(c); + compressionFormat = compressionFormat >> 8; + } + return buf.toString(); + } + + public void debugPrint() { + PrintStream tty = System.err; + tty.println("Compressed texture: " + isCompressed()); + if (isCompressed()) { + int fmt = getCompressionFormat(); + String name = getCompressionFormatName(fmt); + tty.println("Compression format: 0x" + Integer.toHexString(fmt) + " (" + name + ")"); + } + tty.println("Width: " + header.width + " Height: " + header.height); + tty.println("header.pitchOrLinearSize: " + header.pitchOrLinearSize); + tty.println("header.pfRBitMask: 0x" + Integer.toHexString(header.pfRBitMask)); + tty.println("header.pfGBitMask: 0x" + Integer.toHexString(header.pfGBitMask)); + tty.println("header.pfBBitMask: 0x" + Integer.toHexString(header.pfBBitMask)); + tty.println("SurfaceDesc flags:"); + boolean recognizedAny = false; + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_CAPS, "DDSD_CAPS"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_HEIGHT, "DDSD_HEIGHT"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_WIDTH, "DDSD_WIDTH"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_PITCH, "DDSD_PITCH"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_BACKBUFFERCOUNT, "DDSD_BACKBUFFERCOUNT"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_ZBUFFERBITDEPTH, "DDSD_ZBUFFERBITDEPTH"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_ALPHABITDEPTH, "DDSD_ALPHABITDEPTH"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_LPSURFACE, "DDSD_LPSURFACE"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_PIXELFORMAT, "DDSD_PIXELFORMAT"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_MIPMAPCOUNT, "DDSD_MIPMAPCOUNT"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_LINEARSIZE, "DDSD_LINEARSIZE"); + recognizedAny |= printIfRecognized(tty, header.flags, DDSD_DEPTH, "DDSD_DEPTH"); + if (!recognizedAny) { + tty.println("(none)"); + } + tty.println("Raw SurfaceDesc flags: 0x" + Integer.toHexString(header.flags)); + tty.println("Pixel format flags:"); + recognizedAny = false; + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_ALPHAPIXELS, "DDPF_ALPHAPIXELS"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_ALPHA, "DDPF_ALPHA"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_FOURCC, "DDPF_FOURCC"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_PALETTEINDEXED4, "DDPF_PALETTEINDEXED4"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_PALETTEINDEXEDTO8, "DDPF_PALETTEINDEXEDTO8"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_PALETTEINDEXED8, "DDPF_PALETTEINDEXED8"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_RGB, "DDPF_RGB"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_COMPRESSED, "DDPF_COMPRESSED"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_RGBTOYUV, "DDPF_RGBTOYUV"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_YUV, "DDPF_YUV"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_ZBUFFER, "DDPF_ZBUFFER"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_PALETTEINDEXED1, "DDPF_PALETTEINDEXED1"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_PALETTEINDEXED2, "DDPF_PALETTEINDEXED2"); + recognizedAny |= printIfRecognized(tty, header.pfFlags, DDPF_ZPIXELS, "DDPF_ZPIXELS"); + if (!recognizedAny) { + tty.println("(none)"); + } + tty.println("Raw pixel format flags: 0x" + Integer.toHexString(header.pfFlags)); + tty.println("Depth: " + getDepth()); + tty.println("Number of mip maps: " + getNumMipMaps()); + int fmt = getPixelFormat(); + tty.print("Pixel format: "); + switch (fmt) { + case D3DFMT_R8G8B8: tty.println("D3DFMT_R8G8B8"); break; + case D3DFMT_A8R8G8B8: tty.println("D3DFMT_A8R8G8B8"); break; + case D3DFMT_X8R8G8B8: tty.println("D3DFMT_X8R8G8B8"); break; + case D3DFMT_DXT1: tty.println("D3DFMT_DXT1"); break; + case D3DFMT_DXT2: tty.println("D3DFMT_DXT2"); break; + case D3DFMT_DXT3: tty.println("D3DFMT_DXT3"); break; + case D3DFMT_DXT4: tty.println("D3DFMT_DXT4"); break; + case D3DFMT_DXT5: tty.println("D3DFMT_DXT5"); break; + case D3DFMT_UNKNOWN: tty.println("D3DFMT_UNKNOWN"); break; + default: tty.println("(unknown pixel format " + fmt + ")"); break; + } + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private static final int MAGIC = 0x20534444; + + static class Header { + int size; // size of the DDSURFACEDESC structure + int flags; // determines what fields are valid + int height; // height of surface to be created + int width; // width of input surface + int pitchOrLinearSize; + int backBufferCountOrDepth; + int mipMapCountOrAux; // number of mip-map levels requested (in this context) + int alphaBitDepth; // depth of alpha buffer requested + int reserved1; // reserved + int surface; // pointer to the associated surface memory + // NOTE: following two entries are from DDCOLORKEY data structure + // Are overlaid with color for empty cubemap faces (unused in this reader) + int colorSpaceLowValue; + int colorSpaceHighValue; + int destBltColorSpaceLowValue; + int destBltColorSpaceHighValue; + int srcOverlayColorSpaceLowValue; + int srcOverlayColorSpaceHighValue; + int srcBltColorSpaceLowValue; + int srcBltColorSpaceHighValue; + // NOTE: following entries are from DDPIXELFORMAT data structure + // Are overlaid with flexible vertex format description of vertex + // buffers (unused in this reader) + int pfSize; // size of DDPIXELFORMAT structure + int pfFlags; // pixel format flags + int pfFourCC; // (FOURCC code) + // Following five entries have multiple interpretations, not just + // RGBA (but that's all we support right now) + int pfRGBBitCount; // how many bits per pixel + int pfRBitMask; // mask for red bits + int pfGBitMask; // mask for green bits + int pfBBitMask; // mask for blue bits + int pfABitMask; // mask for alpha channel + int ddsCaps1; // Texture and mip-map flags + int ddsCaps2; // Advanced capabilities, not yet used + int ddsCapsReserved1; + int ddsCapsReserved2; + int textureStage; // stage in multitexture cascade + + void read(ByteBuffer buf) throws IOException { + int magic = buf.getInt(); + if (magic != MAGIC) { + throw new IOException("Incorrect magic number 0x" + + Integer.toHexString(magic) + + " (expected " + MAGIC + ")"); + } + + size = buf.getInt(); + flags = buf.getInt(); + height = buf.getInt(); + width = buf.getInt(); + pitchOrLinearSize = buf.getInt(); + backBufferCountOrDepth = buf.getInt(); + mipMapCountOrAux = buf.getInt(); + alphaBitDepth = buf.getInt(); + reserved1 = buf.getInt(); + surface = buf.getInt(); + colorSpaceLowValue = buf.getInt(); + colorSpaceHighValue = buf.getInt(); + destBltColorSpaceLowValue = buf.getInt(); + destBltColorSpaceHighValue = buf.getInt(); + srcOverlayColorSpaceLowValue = buf.getInt(); + srcOverlayColorSpaceHighValue = buf.getInt(); + srcBltColorSpaceLowValue = buf.getInt(); + srcBltColorSpaceHighValue = buf.getInt(); + pfSize = buf.getInt(); + pfFlags = buf.getInt(); + pfFourCC = buf.getInt(); + pfRGBBitCount = buf.getInt(); + pfRBitMask = buf.getInt(); + pfGBitMask = buf.getInt(); + pfBBitMask = buf.getInt(); + pfABitMask = buf.getInt(); + ddsCaps1 = buf.getInt(); + ddsCaps2 = buf.getInt(); + ddsCapsReserved1 = buf.getInt(); + ddsCapsReserved2 = buf.getInt(); + textureStage = buf.getInt(); + } + } + + // Microsoft doesn't follow their own specifications and the + // simplest conversion using the DxTex tool to e.g. a DXT3 texture + // results in an illegal .dds file without either DDSD_PITCH or + // DDSD_LINEARSIZE set in the header's flags. This code, adapted + // from the DevIL library, fixes up the header in these situations. + private void fixupHeader() { + if (isCompressed() && !isSurfaceDescFlagSet(DDSD_LINEARSIZE)) { + // Figure out how big the linear size should be + int depth = header.backBufferCountOrDepth; + if (depth == 0) { + depth = 1; + } + + int blockSize = ((getWidth() + 3)/4) * ((getHeight() + 3)/4) * ((depth + 3)/4); + switch (getCompressionFormat()) { + case D3DFMT_DXT1: blockSize *= 8; break; + default: blockSize *= 16; break; + } + + header.pitchOrLinearSize = blockSize; + header.flags |= DDSD_LINEARSIZE; + } + } + + private int mipMapWidth(int map) { + int width = getWidth(); + for (int i = 0; i < map; i++) { + width >>= 1; + } + return width; + } + + private int mipMapHeight(int map) { + int height = getHeight(); + for (int i = 0; i < map; i++) { + height >>= 1; + } + return height; + } + + private int mipMapSizeInBytes(int map) { + if (isCompressed()) { + if (!isSurfaceDescFlagSet(DDSD_LINEARSIZE)) { + throw new RuntimeException("Illegal compressed texture: DDSD_LINEARSIZE not specified in texture header"); + } + int bytes = header.pitchOrLinearSize; + for (int i = 0; i < map; i++) { + bytes >>= 2; + } + return bytes; + } else { + int width = mipMapWidth(map); + int height = mipMapHeight(map); + return width * height * (getDepth() / 8); + } + } + + private boolean printIfRecognized(PrintStream tty, int flags, int flag, String what) { + if ((flags & flag) != 0) { + tty.println(what); + return true; + } + return false; + } +} diff --git a/src/classes/com/sun/opengl/utils/LEDataInputStream.java b/src/classes/com/sun/opengl/utils/LEDataInputStream.java new file mode 100755 index 000000000..eb041c161 --- /dev/null +++ b/src/classes/com/sun/opengl/utils/LEDataInputStream.java @@ -0,0 +1,223 @@ +/* + * Copyright (c) 2003 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 or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.sun.opengl.utils; + +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.FilterInputStream; +import java.io.InputStream; +import java.io.FileInputStream; +import java.io.EOFException; +import java.io.IOException; + +/** + * Little Endian Data Input Stream. + * + * This class implements an input stream filter to allow reading + * of java native datatypes from an input stream which has those + * native datatypes stored in a little endian byte order.<p> + * + * This is the sister class of the DataInputStream which allows + * for reading of java native datatypes from an input stream with + * the datatypes stored in big endian byte order.<p> + * + * This class implements the minimum required and calls DataInputStream + * for some of the required methods for DataInput.<p> + * + * Not all methods are implemented due to lack of immediatte requirement + * for that functionality. It is not clear if it is ever going to be + * functionally required to be able to read UTF data in a LittleEndianManner<p> + * + * @author Robin Luiten + * @version 1.1 15/Dec/1997 + */ +class LEDataInputStream extends FilterInputStream implements DataInput +{ + /** + * To reuse some of the non endian dependent methods from + * DataInputStreams methods. + */ + DataInputStream dataIn; + + public LEDataInputStream(InputStream in) + { + super(in); + dataIn = new DataInputStream(in); + } + + public void close() throws IOException + { + dataIn.close(); // better close as we create it. + // this will close underlying as well. + } + + public synchronized final int read(byte b[]) throws IOException + { + return dataIn.read(b, 0, b.length); + } + + public synchronized final int read(byte b[], int off, int len) throws IOException + { + int rl = dataIn.read(b, off, len); + return rl; + } + + public final void readFully(byte b[]) throws IOException + { + dataIn.readFully(b, 0, b.length); + } + + public final void readFully(byte b[], int off, int len) throws IOException + { + dataIn.readFully(b, off, len); + } + + public final int skipBytes(int n) throws IOException + { + return dataIn.skipBytes(n); + } + + public final boolean readBoolean() throws IOException + { + int ch = dataIn.read(); + if (ch < 0) + throw new EOFException(); + return (ch != 0); + } + + public final byte readByte() throws IOException + { + int ch = dataIn.read(); + if (ch < 0) + throw new EOFException(); + return (byte)(ch); + } + + public final int readUnsignedByte() throws IOException + { + int ch = dataIn.read(); + if (ch < 0) + throw new EOFException(); + return ch; + } + + public final short readShort() throws IOException + { + int ch1 = dataIn.read(); + int ch2 = dataIn.read(); + if ((ch1 | ch2) < 0) + throw new EOFException(); + return (short)((ch1 << 0) + (ch2 << 8)); + } + + public final int readUnsignedShort() throws IOException + { + int ch1 = dataIn.read(); + int ch2 = dataIn.read(); + if ((ch1 | ch2) < 0) + throw new EOFException(); + return (ch1 << 0) + (ch2 << 8); + } + + public final char readChar() throws IOException + { + int ch1 = dataIn.read(); + int ch2 = dataIn.read(); + if ((ch1 | ch2) < 0) + throw new EOFException(); + return (char)((ch1 << 0) + (ch2 << 8)); + } + + public final int readInt() throws IOException + { + int ch1 = dataIn.read(); + int ch2 = dataIn.read(); + int ch3 = dataIn.read(); + int ch4 = dataIn.read(); + if ((ch1 | ch2 | ch3 | ch4) < 0) + throw new EOFException(); + return ((ch1 << 0) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24)); + } + + public final long readLong() throws IOException + { + int i1 = readInt(); + int i2 = readInt(); + return ((long)(i1) & 0xFFFFFFFFL) + (i2 << 32); + } + + public final float readFloat() throws IOException + { + return Float.intBitsToFloat(readInt()); + } + + public final double readDouble() throws IOException + { + return Double.longBitsToDouble(readLong()); + } + + /** + * dont call this it is not implemented. + * @return empty new string + **/ + public final String readLine() throws IOException + { + return new String(); + } + + /** + * dont call this it is not implemented + * @return empty new string + **/ + public final String readUTF() throws IOException + { + return new String(); + } + + /** + * dont call this it is not implemented + * @return empty new string + **/ + public final static String readUTF(DataInput in) throws IOException + { + return new String(); + } +} + diff --git a/src/classes/com/sun/opengl/utils/SGIImage.java b/src/classes/com/sun/opengl/utils/SGIImage.java new file mode 100755 index 000000000..d115604b1 --- /dev/null +++ b/src/classes/com/sun/opengl/utils/SGIImage.java @@ -0,0 +1,390 @@ +/* + * Portions Copyright (c) 2005 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 or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.sun.opengl.utils; + +import java.io.*; +import javax.media.opengl.*; +import com.sun.opengl.utils.*; + +// Test harness +import java.awt.image.*; +import javax.swing.*; + +/** <p> Reads SGI RGB/RGBA images. </p> + + <p> Written from <a href = + "http://astronomy.swin.edu.au/~pbourke/dataformats/sgirgb/">Paul + Bourke's adaptation</a> of the <a href = + "http://astronomy.swin.edu.au/~pbourke/dataformats/sgirgb/sgiversion.html">SGI + specification</a>. </p> +*/ + +public class SGIImage { + private Header header; + private int format; + private byte[] data; + // Used for decoding RLE-compressed images + private int[] rowStart; + private int[] rowSize; + private int rleEnd; + private byte[] tmpData; + private byte[] tmpRead; + + private static final int MAGIC = 474; + + static class Header { + short magic; // IRIS image file magic number + // This should be decimal 474 + byte storage; // Storage format + // 0 for uncompressed + // 1 for RLE compression + byte bpc; // Number of bytes per pixel channel + // Legally 1 or 2 + short dimension; // Number of dimensions + // Legally 1, 2, or 3 + // 1 means a single row, XSIZE long + // 2 means a single 2D image + // 3 means multiple 2D images + short xsize; // X size in pixels + short ysize; // Y size in pixels + short zsize; // Number of channels + // 1 indicates greyscale + // 3 indicates RGB + // 4 indicates RGB and Alpha + int pixmin; // Minimum pixel value + // This is the lowest pixel value in the image + int pixmax; // Maximum pixel value + // This is the highest pixel value in the image + int dummy; // Ignored + // Normally set to 0 + String imagename; // Image name; 80 bytes long + // Must be null terminated, therefore at most 79 bytes + int colormap; // Colormap ID + // 0 - normal mode + // 1 - dithered, 3 mits for red and green, 2 for blue, obsolete + // 2 - index colour, obsolete + // 3 - not an image but a colourmap + // 404 bytes char DUMMY Ignored + // Should be set to 0, makes the header 512 bytes. + + Header(DataInputStream in) throws IOException { + magic = in.readShort(); + storage = in.readByte(); + bpc = in.readByte(); + dimension = in.readShort(); + xsize = in.readShort(); + ysize = in.readShort(); + zsize = in.readShort(); + pixmin = in.readInt(); + pixmax = in.readInt(); + dummy = in.readInt(); + byte[] tmpname = new byte[80]; + in.read(tmpname); + int numChars = 0; + while (tmpname[numChars++] != 0); + imagename = new String(tmpname, 0, numChars); + colormap = in.readInt(); + byte[] tmp = new byte[404]; + in.read(tmp); + } + + public String toString() { + return ("magic: " + magic + + " storage: " + (int) storage + + " bpc: " + (int) bpc + + " dimension: " + dimension + + " xsize: " + xsize + + " ysize: " + ysize + + " zsize: " + zsize + + " pixmin: " + pixmin + + " pixmax: " + pixmax + + " imagename: " + imagename + + " colormap: " + colormap); + } + } + + private SGIImage(Header header) { + this.header = header; + } + + /** Reads an SGI image from the specified file. */ + public static SGIImage read(String filename) throws IOException { + return read(new FileInputStream(filename)); + } + + /** Reads an SGI image from the specified InputStream. */ + public static SGIImage read(InputStream in) throws IOException { + DataInputStream dIn = new DataInputStream(new BufferedInputStream(in)); + + Header header = new Header(dIn); + SGIImage res = new SGIImage(header); + res.decodeImage(dIn); + return res; + } + + /** Determines from the magic number whether the given InputStream + points to an SGI RGB image. The given InputStream must return + true from markSupported() and support a minimum of two bytes + of read-ahead. */ + public static boolean isSGIImage(InputStream in) throws IOException { + if (!(in instanceof BufferedInputStream)) { + in = new BufferedInputStream(in); + } + if (!in.markSupported()) { + throw new IOException("Can not test non-destructively whether given InputStream is an SGI RGB image"); + } + DataInputStream dIn = new DataInputStream(in); + dIn.mark(4); + short magic = dIn.readShort(); + dIn.reset(); + return (magic == MAGIC); + } + + /** Returns the width of the image. */ + public int getWidth() { + return header.xsize; + } + + /** Returns the height of the image. */ + public int getHeight() { + return header.ysize; + } + + /** Returns the OpenGL format for this texture; e.g. GL.GL_RGB or GL.GL_RGBA. */ + public int getFormat() { + return format; + } + + /** Returns the raw data for this texture in the correct + (bottom-to-top) order for calls to glTexImage2D. */ + public byte[] getData() { return data; } + + public String toString() { + return header.toString(); + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private void decodeImage(DataInputStream in) throws IOException { + if (header.storage == 1) { + // Read RLE compression data; row starts and sizes + int x = header.ysize * header.zsize; + rowStart = new int[x]; + rowSize = new int[x]; + rleEnd = 4 * 2 * x + 512; + for (int i = 0; i < x; i++) { + rowStart[i] = in.readInt(); + } + for (int i = 0; i < x; i++) { + rowSize[i] = in.readInt(); + } + tmpRead = new byte[header.xsize * 256]; + } + tmpData = readAll(in); + + int xsize = header.xsize; + int ysize = header.ysize; + int zsize = header.zsize; + int lptr = 0; + + data = new byte[xsize * ysize * 4]; + byte[] rbuf = new byte[xsize]; + byte[] gbuf = new byte[xsize]; + byte[] bbuf = new byte[xsize]; + byte[] abuf = new byte[xsize]; + for (int y = 0; y < ysize; y++) { + if (zsize >= 4) { + getRow(rbuf, y, 0); + getRow(gbuf, y, 1); + getRow(bbuf, y, 2); + getRow(abuf, y, 3); + rgbatorgba(rbuf, gbuf, bbuf, abuf, data, lptr); + } else if (zsize == 3) { + getRow(rbuf, y, 0); + getRow(gbuf, y, 1); + getRow(bbuf, y, 2); + rgbtorgba(rbuf, gbuf, bbuf, data, lptr); + } else if (zsize == 2) { + getRow(rbuf, y, 0); + getRow(abuf, y, 1); + latorgba(rbuf, abuf, data, lptr); + } else { + getRow(rbuf, y, 0); + bwtorgba(rbuf, data, lptr); + } + lptr += 4 * xsize; + } + rowStart = null; + rowSize = null; + tmpData = null; + tmpRead = null; + format = GL.GL_RGBA; + } + + private void getRow(byte[] buf, int y, int z) { + if (header.storage == 1) { + int offs = rowStart[y + z * header.ysize] - rleEnd; + System.arraycopy(tmpData, offs, tmpRead, 0, rowSize[y + z * header.ysize]); + int iPtr = 0; + int oPtr = 0; + for (;;) { + byte pixel = tmpRead[iPtr++]; + int count = (int) (pixel & 0x7F); + if (count == 0) { + return; + } + if ((pixel & 0x80) != 0) { + while ((count--) > 0) { + buf[oPtr++] = tmpRead[iPtr++]; + } + } else { + pixel = tmpRead[iPtr++]; + while ((count--) > 0) { + buf[oPtr++] = pixel; + } + } + } + } else { + int offs = (y * header.xsize) + (z * header.xsize * header.ysize); + System.arraycopy(tmpData, offs, buf, 0, header.xsize); + } + } + + private void bwtorgba(byte[] b, byte[] dest, int lptr) { + for (int i = 0; i < b.length; i++) { + dest[4 * i + lptr + 0] = b[i]; + dest[4 * i + lptr + 1] = b[i]; + dest[4 * i + lptr + 2] = b[i]; + dest[4 * i + lptr + 3] = (byte) 0xFF; + } + } + + private void latorgba(byte[] b, byte[] a, byte[] dest, int lptr) { + for (int i = 0; i < b.length; i++) { + dest[4 * i + lptr + 0] = b[i]; + dest[4 * i + lptr + 1] = b[i]; + dest[4 * i + lptr + 2] = b[i]; + dest[4 * i + lptr + 3] = a[i]; + } + } + + private void rgbtorgba(byte[] r, byte[] g, byte[] b, byte[] dest, int lptr) { + for (int i = 0; i < b.length; i++) { + dest[4 * i + lptr + 0] = r[i]; + dest[4 * i + lptr + 1] = g[i]; + dest[4 * i + lptr + 2] = b[i]; + dest[4 * i + lptr + 3] = (byte) 0xFF; + } + } + + private void rgbatorgba(byte[] r, byte[] g, byte[] b, byte[] a, byte[] dest, int lptr) { + for (int i = 0; i < b.length; i++) { + dest[4 * i + lptr + 0] = r[i]; + dest[4 * i + lptr + 1] = g[i]; + dest[4 * i + lptr + 2] = b[i]; + dest[4 * i + lptr + 3] = a[i]; + } + } + + private byte[] readAll(DataInputStream in) throws IOException { + byte[] dest = new byte[16384]; + int pos = 0; + int numRead = 0; + + boolean done = false; + + do { + numRead = in.read(dest, pos, dest.length - pos); + if (pos == dest.length) { + // Resize destination buffer + byte[] newDest = new byte[2 * dest.length]; + System.arraycopy(dest, 0, newDest, 0, pos); + dest = newDest; + } + if (numRead > 0) { + pos += numRead; + } + + done = ((numRead == -1) || (in.available() == 0)); + } while (!done); + + // Trim destination buffer + if (pos != dest.length) { + byte[] finalDest = new byte[pos]; + System.arraycopy(dest, 0, finalDest, 0, pos); + dest = finalDest; + } + + return dest; + } + + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + try { + System.out.println(args[i] + ":"); + SGIImage image = SGIImage.read(args[i]); + System.out.println(image); + BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); + WritableRaster raster = img.getRaster(); + DataBufferByte db = (DataBufferByte) raster.getDataBuffer(); + byte[] src = image.getData(); + byte[] dest = db.getData(); + for (int j = 0; j < src.length; j += 4) { + dest[j + 0] = src[j + 3]; + dest[j + 1] = src[j + 2]; + dest[j + 2] = src[j + 1]; + dest[j + 3] = src[j + 0]; + } + // System.arraycopy(src, 0, dest, 0, src.length); + ImageIcon icon = new ImageIcon(img); + JLabel label = new JLabel(); + label.setIcon(icon); + JFrame frame = new JFrame(args[i]); + frame.getContentPane().add(label); + frame.pack(); + frame.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/classes/com/sun/opengl/utils/TGAImage.java b/src/classes/com/sun/opengl/utils/TGAImage.java new file mode 100755 index 000000000..d5505afd5 --- /dev/null +++ b/src/classes/com/sun/opengl/utils/TGAImage.java @@ -0,0 +1,320 @@ +/* + * Copyright (c) 2003-2005 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 or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.sun.opengl.utils; + +import java.io.*; +import javax.media.opengl.*; +import com.sun.opengl.utils.*; + +/** + * Targa image reader adapted from sources of the <a href = + * "http://java.sun.com/products/jimi/">Jimi</a> image I/O class library. + * + * <P> + * + * Image decoder for image data stored in TGA file format. + * Currently only the original TGA file format is supported. This is + * because the new TGA format has data at the end of the file, getting + * to the end of a file in an InputStream orient environment presents + * several difficulties which are avoided at the moment. + * + * <P> + * + * This is a simple decoder and is only setup to load a single image + * from the input stream + * + * <P> + * + * @author Robin Luiten + * @author Kenneth Russell + * @version $Revision$ + */ + +public class TGAImage { + private Header header; + private int format; + private byte[] data; + + private TGAImage(Header header) { + this.header = header; + } + + /** + * This class reads in all of the TGA image header in addition it also + * reads in the imageID field as it is convenient to handle that here. + * + * @author Robin Luiten + * @version 1.1 + */ + public static class Header { + /** Set of possible file format TGA types */ + public final static int TYPE_NEW = 0; + public final static int TYPE_OLD = 1; + public final static int TYPE_UNK = 2; // cant rewind stream so unknown for now. + + /** Set of possible image types in TGA file */ + public final static int NO_IMAGE = 0; // no image data + public final static int UCOLORMAPPED = 1; // uncompressed color mapped image + public final static int UTRUECOLOR = 2; // uncompressed true color image + public final static int UBLACKWHITE = 3; // uncompressed black and white image + public final static int COLORMAPPED = 9; // compressed color mapped image + public final static int TRUECOLOR = 10; // compressed true color image + public final static int BLACKWHITE = 11; // compressed black and white image + + /** Field image descriptor bitfield values definitions */ + public final static int ID_ATTRIBPERPIXEL = 0xF; + public final static int ID_LEFTTORIGHT = 0x10; + public final static int ID_TOPTOBOTTOM = 0x20; + public final static int ID_INTERLEAVE = 0xC0; + + /** Field image descriptor / interleave values */ + public final static int I_NOTINTERLEAVED = 0; + public final static int I_TWOWAY = 1; + public final static int I_FOURWAY = 2; + + /** Type of this TGA file format */ + private int tgaType; + + /** initial TGA image data fields */ + private int idLength; // byte value + private int colorMapType; // byte value + private int imageType; // byte value + + /** TGA image colour map fields */ + private int firstEntryIndex; + private int colorMapLength; + private byte colorMapEntrySize; + + /** TGA image specification fields */ + private int xOrigin; + private int yOrigin; + private int width; + private int height; + private byte pixelDepth; + private byte imageDescriptor; + + /** bitfields in imageDescriptor */ + private byte attribPerPixel; // how many attribute bits per pixel + private boolean leftToRight; // order of data on scan line + private boolean topToBottom; // order scan lines stored + private byte interleave; // how rows are stored in image data + + private byte[] imageIDbuf; + private String imageID; + + Header(LEDataInputStream in) throws IOException { + int ret; + + tgaType = TYPE_OLD; // dont try and get footer. + + // initial header fields + idLength = in.readUnsignedByte(); + colorMapType = in.readUnsignedByte(); + imageType = in.readUnsignedByte(); + + // color map header fields + firstEntryIndex = in.readUnsignedShort(); + colorMapLength = in.readUnsignedShort(); + colorMapEntrySize = in.readByte(); + + // TGA image specification fields + xOrigin = in.readUnsignedShort(); + yOrigin = in.readUnsignedShort(); + width = in.readUnsignedShort(); + height = in.readUnsignedShort(); + pixelDepth = in.readByte(); + imageDescriptor = in.readByte(); + + attribPerPixel = (byte)(imageDescriptor & ID_ATTRIBPERPIXEL); + leftToRight = (imageDescriptor & ID_LEFTTORIGHT) != 0; // not used ? + topToBottom = (imageDescriptor & ID_TOPTOBOTTOM) != 0; + interleave = (byte)((imageDescriptor & ID_INTERLEAVE) >> 6); + + if (idLength > 0) { + imageIDbuf = new byte[idLength]; + in.read(imageIDbuf, 0, idLength); + imageID = new String(imageIDbuf, "US-ASCII"); + } + } + + public int tgaType() { return tgaType; } + + /** initial TGA image data fields */ + public int idLength() { return idLength; } + public int colorMapType() { return colorMapType; } + public int imageType() { return imageType; } + + /** TGA image colour map fields */ + public int firstEntryIndex() { return firstEntryIndex; } + public int colorMapLength() { return colorMapLength; } + public byte colorMapEntrySize() { return colorMapEntrySize; } + + /** TGA image specification fields */ + public int xOrigin() { return xOrigin; } + public int yOrigin() { return yOrigin; } + public int width() { return width; } + public int height() { return height; } + public byte pixelDepth() { return pixelDepth; } + public byte imageDescriptor() { return imageDescriptor; } + + /** bitfields in imageDescriptor */ + public byte attribPerPixel() { return attribPerPixel; } + public boolean leftToRight() { return leftToRight; } + public boolean topToBottom() { return topToBottom; } + public byte interleave() { return interleave; } + + public byte[] imageIDbuf() { return imageIDbuf; } + public String imageID() { return imageID; } + + public String toString() { + return "TGA Header " + + " id length: " + idLength + + " color map type: "+ colorMapType + + " image type: "+ imageType + + " first entry index: " + firstEntryIndex + + " color map length: " + colorMapLength + + " color map entry size: " + colorMapEntrySize + + " x Origin: " + xOrigin + + " y Origin: " + yOrigin + + " width: "+ width + + " height: "+ height + + " pixel depth: "+ pixelDepth + + " image descriptor: "+ imageDescriptor + + (imageIDbuf == null ? "" : (" ID String: " + imageID)); + } + } + + + /** + * Identifies the image type of the tga image data and loads + * it into the JimiImage structure. This was taken from the + * prototype and modified for the new Jimi structure + */ + private void decodeImage(LEDataInputStream dIn) throws IOException { + switch (header.imageType()) { + case Header.UCOLORMAPPED: + throw new IOException("TGADecoder Uncompressed Colormapped images not supported"); + + case Header.UTRUECOLOR: // pixelDepth 15, 16, 24 and 32 + switch (header.pixelDepth) { + case 16: + throw new IOException("TGADecoder Compressed 16-bit True Color images not supported"); + + case 24: + case 32: + decodeRGBImageU24_32(dIn); + break; + } + break; + + case Header.UBLACKWHITE: + throw new IOException("TGADecoder Uncompressed Grayscale images not supported"); + + case Header.COLORMAPPED: + throw new IOException("TGADecoder Compressed Colormapped images not supported"); + + case Header.TRUECOLOR: + throw new IOException("TGADecoder Compressed True Color images not supported"); + + case Header.BLACKWHITE: + throw new IOException("TGADecoder Compressed Grayscale images not supported"); + } + } + + /** + * This assumes that the body is for a 24 bit or 32 bit for a + * RGB or ARGB image respectively. + */ + private void decodeRGBImageU24_32(LEDataInputStream dIn) throws IOException { + int i; // row index + int j; // column index + int y; // output row index + int raw; // index through the raw input buffer + int rawWidth = header.width() * (header.pixelDepth() / 8); + byte[] rawBuf = new byte[rawWidth]; + data = new byte[rawWidth * header.height()]; + + if (header.pixelDepth() == 24) { + format = GL.GL_BGR; + } else { + assert header.pixelDepth() == 32; + format = GL.GL_BGRA; + } + + for (i = 0; i < header.height(); ++i) { + dIn.readFully(rawBuf, 0, rawWidth); + + if (header.topToBottom) + y = header.height - i - 1; // range 0 to (header.height - 1) + else + y = i; + + System.arraycopy(rawBuf, 0, data, y * rawWidth, rawBuf.length); + } + } + + /** Returns the width of the image. */ + public int getWidth() { return header.width(); } + + /** Returns the height of the image. */ + public int getHeight() { return header.height(); } + + /** Returns the OpenGL format for this texture; e.g. GL.GL_BGR or GL.GL_BGRA. */ + public int getGLFormat() { return format; } + + /** Returns the raw data for this texture in the correct + (bottom-to-top) order for calls to glTexImage2D. */ + public byte[] getData() { return data; } + + /** Reads a Targa image from the specified file. */ + public static TGAImage read(String filename) throws IOException { + return read(new FileInputStream(filename)); + } + + /** Reads a Targa image from the specified InputStream. */ + public static TGAImage read(InputStream in) throws IOException { + LEDataInputStream dIn = new LEDataInputStream(new BufferedInputStream(in)); + + Header header = new Header(dIn); + TGAImage res = new TGAImage(header); + res.decodeImage(dIn); + return res; + } +} diff --git a/src/classes/com/sun/opengl/utils/Texture.java b/src/classes/com/sun/opengl/utils/Texture.java new file mode 100755 index 000000000..6c3fbd1c3 --- /dev/null +++ b/src/classes/com/sun/opengl/utils/Texture.java @@ -0,0 +1,440 @@ +/* + * Copyright (c) 2005 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 or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + */ + +package com.sun.opengl.utils; + +import java.awt.geom.*; + +import javax.media.opengl.*; +import com.sun.opengl.impl.*; + +/** + * Represents an OpenGL texture object. Contains convenience routines + * for enabling/disabling OpenGL texture state, binding this texture, + * and computing texture coordinates for both the entire image as well + * as a sub-image. + * + * <br> REMIND: document GL_TEXTURE_2D/GL_TEXTURE_RECTANGLE_ARB issues... + * <br> REMIND: translucent images will have premultiplied comps by default... + * + * @author Chris Campbell + * @author Kenneth Russell + */ +public class Texture { + /** The GL target type. */ + private int target; + /** The GL texture ID. */ + private int texID; + /** The width of the texture. */ + private int texWidth; + /** The height of the texture. */ + private int texHeight; + /** The width of the image. */ + private int imgWidth; + /** The height of the image. */ + private int imgHeight; + /** Indicates whether the TextureData requires a vertical flip of + the texture coords. */ + private boolean mustFlipVertically; + + /** The texture coordinate corresponding to the lower left corner + of the texture when properly oriented. */ + private Point2D lowerLeftTexCoord = new Point2D.Float(); + + /** The texture coordinate corresponding to the upper right corner + of the texture when properly oriented. */ + private Point2D upperRightTexCoord = new Point2D.Float(); + + private static final boolean DEBUG = Debug.debug("Texture"); + + // For now make Texture constructor package-private to limit the + // number of public APIs we commit to + Texture(TextureData data) throws GLException { + GL gl = getCurrentGL(); + + imgWidth = data.getWidth(); + imgHeight = data.getHeight(); + mustFlipVertically = data.getMustFlipVertically(); + + if ((isPowerOfTwo(imgWidth) && isPowerOfTwo(imgHeight)) || + gl.isExtensionAvailable("GL_ARB_texture_non_power_of_two")) { + if (DEBUG) { + if (isPowerOfTwo(imgWidth) && isPowerOfTwo(imgHeight)) { + System.err.println("Power-of-two texture"); + } else { + System.err.println("Using GL_ARB_texture_non_power_of_two"); + } + } + + texWidth = imgWidth; + texHeight = imgHeight; + target = GL.GL_TEXTURE_2D; + } else if (gl.isExtensionAvailable("GL_ARB_texture_rectangle")) { + if (DEBUG) { + System.err.println("Using GL_ARB_texture_rectangle"); + } + + texWidth = imgWidth; + texHeight = imgHeight; + target = GL.GL_TEXTURE_RECTANGLE_ARB; + } else { + if (DEBUG) { + System.err.println("Expanding texture to power-of-two dimensions"); + } + + if (data.getBorder() != 0) { + throw new RuntimeException("Scaling up a non-power-of-two texture which has a border won't work"); + } + texWidth = nextPowerOfTwo(imgWidth); + texHeight = nextPowerOfTwo(imgHeight); + target = GL.GL_TEXTURE_2D; + } + + // REMIND: let the user specify these, optionally + int minFilter = GL.GL_LINEAR; + int magFilter = GL.GL_LINEAR; + int wrapMode = GL.GL_CLAMP_TO_EDGE; + + texID = createTextureID(gl); + setImageSize(imgWidth, imgHeight); + gl.glBindTexture(target, texID); + gl.glTexImage2D(target, 0, data.getInternalFormat(), + texWidth, texHeight, data.getBorder(), + data.getPixelFormat(), data.getPixelType(), null); + + // REMIND: figure out what to do for GL_TEXTURE_RECTANGLE_ARB + if (target == GL.GL_TEXTURE_2D) { + gl.glTexParameteri(target, GL.GL_TEXTURE_MIN_FILTER, minFilter); + gl.glTexParameteri(target, GL.GL_TEXTURE_MAG_FILTER, magFilter); + gl.glTexParameteri(target, GL.GL_TEXTURE_WRAP_S, wrapMode); + gl.glTexParameteri(target, GL.GL_TEXTURE_WRAP_T, wrapMode); + } + + updateSubImage(data, 0, 0); + } + + /** + * Enables this texture's target (e.g., GL_TEXTURE_2D) in the + * current GL context's state. + * + * @throws GLException if no OpenGL context was current or if any + * OpenGL-related errors occurred + */ + public void enable() throws GLException { + getCurrentGL().glEnable(target); + } + + /** + * Disables this texture's target (e.g., GL_TEXTURE_2D) in the + * current GL context's state. + * + * @throws GLException if no OpenGL context was current or if any + * OpenGL-related errors occurred + */ + public void disable() throws GLException { + getCurrentGL().glDisable(target); + } + + /** + * Binds this texture to the current GL context. + * + * @throws GLException if no OpenGL context was current or if any + * OpenGL-related errors occurred + */ + public void bind() throws GLException { + getCurrentGL().glBindTexture(target, texID); + } + + /** + * Disposes the native resources used by this texture object. + * + * @throws GLException if no OpenGL context was current or if any + * OpenGL-related errors occurred + */ + public void dispose() throws GLException { + getCurrentGL().glDeleteTextures(1, new int[] {texID}, 0); + } + + /** + * Returns the OpenGL "target" of this texture. + * + * @return the OpenGL target of this texture + * @see javax.media.opengl.GL#GL_TEXTURE_2D + * @see javax.media.opengl.GL#GL_TEXTURE_RECTANGLE_ARB + */ + public int getTarget() { + return target; + } + + /** + * Returns the width of the texture. Note that the texture width will + * be greater than or equal to the width of the image contained within. + * + * @return the width of the texture + */ + public int getWidth() { + return texWidth; + } + + /** + * Returns the height of the texture. Note that the texture height will + * be greater than or equal to the height of the image contained within. + * + * @return the height of the texture + */ + public int getHeight() { + return texHeight; + } + + /** + * Returns the width of the image contained within this texture. + * + * @return the width of the image + */ + public int getImageWidth() { + return imgWidth; + } + + /** + * Returns the height of the image contained within this texture. + * + * @return the height of the image + */ + public int getImageHeight() { + return imgHeight; + } + + /** + * Returns the texture coordinate corresponding to the lower-left + * point of this texture when oriented properly. If the TextureData + * indicated that the texture coordinates must be flipped + * vertically, the returned Point will take that into account. + * + * @return the texture coordinate of the lower-left point of the + * texture + */ + public Point2D getImageLowerLeftTexCoord() { + return (Point2D) lowerLeftTexCoord.clone(); + } + + /** + * Returns the texture coordinate corresponding to the upper-right + * point of this texture when oriented properly. If the TextureData + * indicated that the texture coordinates must be flipped + * vertically, the returned Point will take that into account. + * + * @return the texture coordinates of the upper-right point of the + * texture + */ + public Point2D getImageUpperRightTexCoord() { + return (Point2D) upperRightTexCoord.clone(); + } + + + /** + * Returns the texture coordinate corresponding to the lower-left + * point of the specified sub-image of this texture when oriented + * properly. If the TextureData indicated that the texture + * coordinates must be flipped vertically, the returned Point will + * take that into account. + * + * @return the texture coordinate of the lower-left point of the + * texture + */ + public Point2D getSubImageLowerLeftTexCoord(int x1, int y1, int x2, int y2) { + if (target == GL.GL_TEXTURE_RECTANGLE_ARB) { + if (mustFlipVertically) { + return new Point2D.Float(x1, texHeight - y1); + } else { + return new Point2D.Float(x1, y1); + } + } else { + float tx = (float)x1 / (float)texWidth; + float ty = (float)y1 / (float)texHeight; + + if (mustFlipVertically) { + return new Point2D.Float(tx, 1.0f - ty); + } else { + return new Point2D.Float(tx, ty); + } + } + } + + /** + * Returns the texture coordinate corresponding to the upper-right + * point of the specified sub-image of this texture when oriented + * properly. If the TextureData indicated that the texture + * coordinates must be flipped vertically, the returned Point will + * take that into account. + * + * @return the texture coordinate of the upper-right point of the + * texture + */ + public Point2D getSubImageUpperRightTexCoord(int x1, int y1, int x2, int y2) { + if (target == GL.GL_TEXTURE_RECTANGLE_ARB) { + if (mustFlipVertically) { + return new Point2D.Float(x2, texHeight - y2); + } else { + return new Point2D.Float(x2, y2); + } + } else { + float tx = (float)x2 / (float)texWidth; + float ty = (float)y2 / (float)texHeight; + + if (mustFlipVertically) { + return new Point2D.Float(tx, 1.0f - ty); + } else { + return new Point2D.Float(tx, ty); + } + } + } + + /** + * Updates a subregion of the content area of this texture using the + * data in the given image. + * + * @param data the image data to be uploaded to this texture + * @param x the x offset (in pixels) relative to the lower-left corner + * of this texture + * @param y the y offset (in pixels) relative to the lower-left corner + * of this texture + * + * @throws GLException if no OpenGL context was current or if any + * OpenGL-related errors occurred + */ + public void updateSubImage(TextureData data, int x, int y) throws GLException { + GL gl = getCurrentGL(); + bind(); + if (data.isDataCompressed()) { + // FIXME: should test availability of appropriate texture + // compression extension here + gl.glCompressedTexSubImage2D(target, data.getMipmapLevel(), + x, y, data.getWidth(), data.getHeight(), + data.getInternalFormat(), data.getBuffer().remaining(), + data.getBuffer()); + } else { + int[] align = new int[1]; + gl.glGetIntegerv(GL.GL_UNPACK_ALIGNMENT, align, 0); // save alignment + gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, data.getAlignment()); + + gl.glTexSubImage2D(target, data.getMipmapLevel(), + x, y, data.getWidth(), data.getHeight(), + data.getPixelFormat(), data.getPixelType(), + data.getBuffer()); + gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, align[0]); // restore align + } + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + /** + * Returns the current GL object. Throws GLException if no OpenGL + * context was current. + */ + private static GL getCurrentGL() throws GLException { + GLContext context = GLContext.getCurrent(); + if (context == null) { + throw new GLException("No OpenGL context current on current thread"); + } + return context.getGL(); + } + + /** + * Returns true if the given value is a power of two. + * + * @return true if the given value is a power of two, false otherwise + */ + private static boolean isPowerOfTwo(int val) { + return ((val & (val - 1)) == 0); + } + + /** + * Returns the nearest power of two that is larger than the given value. + * If the given value is already a power of two, this method will simply + * return that value. + * + * @param val the value + * @return the next power of two + */ + private static int nextPowerOfTwo(int val) { + int ret = 1; + while (ret < val) { + ret <<= 1; + } + return ret; + } + + /** + * Updates the actual image dimensions; usually only called from + * <code>updateImage</code>. + */ + private void setImageSize(int width, int height) { + imgWidth = width; + imgHeight = height; + if (target == GL.GL_TEXTURE_RECTANGLE_ARB) { + if (mustFlipVertically) { + lowerLeftTexCoord = new Point2D.Float(0, imgHeight); + upperRightTexCoord = new Point2D.Float(imgWidth, 0); + } else { + lowerLeftTexCoord = new Point2D.Float(0, 0); + upperRightTexCoord = new Point2D.Float(imgWidth, imgHeight); + } + } else { + if (mustFlipVertically) { + lowerLeftTexCoord = new Point2D.Float(0, (float) imgHeight / (float) texHeight); + upperRightTexCoord = new Point2D.Float((float) imgWidth / (float) texWidth, 0); + } else { + lowerLeftTexCoord = new Point2D.Float(0, 0); + upperRightTexCoord = new Point2D.Float((float) imgWidth / (float) texWidth, + (float) imgHeight / (float) texHeight); + } + } + } + + /** + * Creates a new texture ID. + * + * @param gl the GL object associated with the current OpenGL context + * @return a new texture ID + */ + private static int createTextureID(GL gl) { + int[] tmp = new int[1]; + gl.glGenTextures(1, tmp, 0); + return tmp[0]; + } +} diff --git a/src/classes/com/sun/opengl/utils/TextureData.java b/src/classes/com/sun/opengl/utils/TextureData.java new file mode 100755 index 000000000..d88a18233 --- /dev/null +++ b/src/classes/com/sun/opengl/utils/TextureData.java @@ -0,0 +1,447 @@ +/* + * Copyright (c) 2005 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 or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + */ + +package com.sun.opengl.utils; + +import java.awt.AlphaComposite; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Transparency; +import java.awt.color.*; +import java.awt.image.*; +import java.nio.*; + +import javax.media.opengl.*; + +/** + * Represents the data for an OpenGL texture. This is separated from + * the notion of a Texture to support things like streaming in of + * textures in a background thread without requiring an OpenGL context + * to be current on that thread. + * + * @author Chris Campbell + * @author Kenneth Russell + */ + +public class TextureData { + private int width; + private int height; + private int border; + private int pixelFormat; + private int pixelType; + private int internalFormat; // perhaps inferred from pixelFormat? + private int mipmapLevel; + private boolean dataIsCompressed; + private boolean mustFlipVertically; // Must flip texture coordinates + // vertically to get OpenGL output + // to look correct + private Buffer buffer; // the actual data + private Flusher flusher; + private int alignment; // 1, 2, or 4 bytes + + private static final ColorModel rgbaColorModel = + new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), + new int[] {8, 8, 8, 8}, true, true, + Transparency.TRANSLUCENT, + DataBuffer.TYPE_BYTE); + private static final ColorModel rgbColorModel = + new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), + new int[] {8, 8, 8, 0}, false, false, + Transparency.OPAQUE, + DataBuffer.TYPE_BYTE); + + /** + * Constructs a new TextureData object with the specified parameters + * and data contained in the given Buffer. The optional Flusher can + * be used to clean up native resources associated with this + * TextureData when processing is complete; for example, closing of + * memory-mapped files that might otherwise require a garbage + * collection to reclaim and close. + * + * @param mipmapLevel the mipmap level for the resulting texture + * this data represents (FIXME: needs + * rethinking, currently unused) + * @param internalFormat the OpenGL internal format for the + * resulting texture; must be specified, may + * not be 0 + * @param width the width in pixels of the texture + * @param height the height in pixels of the texture + * @param border the number of pixels of border this texture + * data has (0 or 1) + * @param pixelFormat the OpenGL pixel format for the + * resulting texture; must be specified, may + * not be 0 + * @param pixelType the OpenGL type of the pixels of the texture + * @param dataIsCompressed indicates whether the texture data is in + * compressed form + * (e.g. GL_COMPRESSED_RGB_S3TC_DXT1_EXT) + * @param mustFlipVertically indicates whether the texture + * coordinates must be flipped vertically + * in order to properly display the + * texture + * @param buffer the buffer containing the texture data + * @param flusher optional flusher to perform cleanup tasks + * upon call to flush() + */ + public TextureData(int mipmapLevel, + int internalFormat, + int width, + int height, + int border, + int pixelFormat, + int pixelType, + boolean dataIsCompressed, + boolean mustFlipVertically, + Buffer buffer, + Flusher flusher) { + this.width = width; + this.height = height; + this.border = border; + this.pixelFormat = pixelFormat; + this.pixelType = pixelType; + this.internalFormat = internalFormat; + this.mipmapLevel = mipmapLevel; + this.dataIsCompressed = dataIsCompressed; + this.mustFlipVertically = mustFlipVertically; + this.buffer = buffer; + this.flusher = flusher; + alignment = 1; // FIXME: is this correct enough in all situations? + } + + /** + * Constructs a new TextureData object with the specified parameters + * and data contained in the given BufferedImage. + * + * @param mipmapLevel the mipmap level for the resulting texture + * this data represents (FIXME: needs + * rethinking, currently unused) + * @param internalFormat the OpenGL internal format for the + * resulting texture; may be 0, in which case + * it is inferred from the image's type + * @param pixelFormat the OpenGL internal format for the + * resulting texture; may be 0, in which case + * it is inferred from the image's type (note: + * this argument is currently always ignored) + * @param image the image containing the texture data + */ + public TextureData(int mipmapLevel, + int internalFormat, + int pixelFormat, + BufferedImage image) { + if (internalFormat == 0) { + this.internalFormat = image.getColorModel().hasAlpha() ? GL.GL_RGBA : GL.GL_RGB; + } else { + this.internalFormat = internalFormat; + } + createFromImage(image); + this.mipmapLevel = mipmapLevel; + } + + /** Returns the width in pixels of the texture data. */ + public int getWidth() { return width; } + /** Returns the height in pixels of the texture data. */ + public int getHeight() { return height; } + /** Returns the border in pixels of the texture data. */ + public int getBorder() { return border; } + /** Returns the intended OpenGL pixel format of the texture data. */ + public int getPixelFormat() { return pixelFormat; } + /** Returns the intended OpenGL pixel type of the texture data. */ + public int getPixelType() { return pixelType; } + /** Returns the intended OpenGL internal format of the texture data. */ + public int getInternalFormat() { return internalFormat; } + /** Returns the intended mipmap level of the texture data. */ + public int getMipmapLevel() { return mipmapLevel; } + /** Indicates whether the texture data is in compressed form. */ + public boolean isDataCompressed() { return dataIsCompressed; } + /** Indicates whether the texture coordinates must be flipped + vertically for proper display. */ + public boolean getMustFlipVertically() { return mustFlipVertically; } + /** Returns the texture data. */ + public Buffer getBuffer() { return buffer; } + /** Returns the required byte alignment for the texture data. */ + public int getAlignment() { return alignment; } + + /** Sets the width in pixels of the texture data. */ + public void setWidth(int width) { this.width = width; } + /** Sets the height in pixels of the texture data. */ + public void setHeight(int height) { this.height = height; } + /** Sets the border in pixels of the texture data. */ + public void setBorder(int border) { this.border = border; } + /** Sets the intended OpenGL pixel format of the texture data. */ + public void setPixelFormat(int pixelFormat) { this.pixelFormat = pixelFormat; } + /** Sets the intended OpenGL pixel type of the texture data. */ + public void setPixelType(int pixelType) { this.pixelType = pixelType; } + /** Sets the intended OpenGL internal format of the texture data. */ + public void setInternalFormat(int internalFormat) { this.internalFormat = internalFormat; } + /** Sets the intended mipmap level of the texture data. */ + public void setMipmapLevel(int mipmapLevel) { this.mipmapLevel = mipmapLevel; } + /** Sets whether the texture data is in compressed form. */ + public void setIsDataCompressed(boolean compressed) { this.dataIsCompressed = compressed; } + /** Sets whether the texture coordinates must be flipped vertically + for proper display. */ + public void setMustFlipVertically(boolean mustFlipVertically) { this.mustFlipVertically = mustFlipVertically; } + /** Sets the texture data. */ + public void setBuffer(Buffer buffer) { this.buffer = buffer; } + /** Sets the required byte alignment for the texture data. */ + public void setAlignment(int alignment) { this.alignment = alignment; } + + /** Flushes resources associated with this TextureData by calling + Flusher.flush(). */ + public void flush() { + if (flusher != null) { + flusher.flush(); + flusher = null; + } + } + + /** Defines a callback mechanism to allow the user to explicitly + deallocate native resources (memory-mapped files, etc.) + associated with a particular TextureData. */ + public static interface Flusher { + /** Flushes any native resources associated with this + TextureData. */ + public void flush(); + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private void createNIOBufferFromImage(BufferedImage image, boolean flipVertically) { + if (flipVertically) { + WritableRaster raster = image.getRaster(); + Object scanline1 = null; + Object scanline2 = null; + + for (int i = 0; i < image.getHeight() / 2; i++) { + scanline1 = raster.getDataElements(0, i, image.getWidth(), 1, scanline1); + scanline2 = raster.getDataElements(0, image.getHeight() - i - 1, image.getWidth(), 1, scanline2); + raster.setDataElements(0, i, image.getWidth(), 1, scanline2); + raster.setDataElements(0, image.getHeight() - i - 1, image.getWidth(), 1, scanline1); + } + } + + // + // Note: Grabbing the DataBuffer will defeat Java2D's image + // management mechanism (as of JDK 5/6, at least). This shouldn't + // be a problem for most JOGL apps, but those that try to upload + // the image into an OpenGL texture and then use the same image in + // Java2D rendering might find the 2D rendering is not as fast as + // it could be. + // + + // Allow previously-selected pixelType (if any) to override that + // we can infer from the DataBuffer + DataBuffer data = image.getRaster().getDataBuffer(); + if (data instanceof DataBufferByte) { + if (pixelType == 0) pixelType = GL.GL_UNSIGNED_BYTE; + buffer = ByteBuffer.wrap(((DataBufferByte) data).getData()); + } else if (data instanceof DataBufferDouble) { + throw new RuntimeException("DataBufferDouble rasters not supported by OpenGL"); + } else if (data instanceof DataBufferFloat) { + if (pixelType == 0) pixelType = GL.GL_FLOAT; + buffer = FloatBuffer.wrap(((DataBufferFloat) data).getData()); + } else if (data instanceof DataBufferInt) { + // FIXME: should we support signed ints? + if (pixelType == 0) pixelType = GL.GL_UNSIGNED_INT; + buffer = IntBuffer.wrap(((DataBufferInt) data).getData()); + } else if (data instanceof DataBufferShort) { + if (pixelType == 0) pixelType = GL.GL_SHORT; + buffer = ShortBuffer.wrap(((DataBufferShort) data).getData()); + } else if (data instanceof DataBufferUShort) { + if (pixelType == 0) pixelType = GL.GL_UNSIGNED_SHORT; + buffer = ShortBuffer.wrap(((DataBufferShort) data).getData()); + } else { + throw new RuntimeException("Unexpected DataBuffer type?"); + } + } + + private void createFromImage(BufferedImage image) { + pixelType = 0; // Determine from image + + width = image.getWidth(); + height = image.getHeight(); + + switch (image.getType()) { + case BufferedImage.TYPE_INT_RGB: + pixelFormat = GL.GL_BGRA; + pixelType = GL.GL_UNSIGNED_INT_8_8_8_8_REV; + alignment = 4; + break; + case BufferedImage.TYPE_INT_ARGB_PRE: + pixelFormat = GL.GL_BGRA; + pixelType = GL.GL_UNSIGNED_INT_8_8_8_8_REV; + alignment = 4; + break; + case BufferedImage.TYPE_INT_BGR: + pixelFormat = GL.GL_RGBA; + pixelType = GL.GL_UNSIGNED_INT_8_8_8_8_REV; + alignment = 4; + break; + case BufferedImage.TYPE_3BYTE_BGR: + { + Raster raster = image.getRaster(); + ComponentSampleModel csm = + (ComponentSampleModel)raster.getSampleModel(); + // we can pass the image data directly to OpenGL only if + // the raster is tightly packed (i.e. there is no extra + // space at the end of each scanline) + if ((csm.getScanlineStride() / 3) == csm.getWidth()) { + pixelFormat = GL.GL_BGR; + pixelType = GL.GL_UNSIGNED_BYTE; + alignment = 1; + } else { + createFromCustom(image); + return; + } + } + break; + case BufferedImage.TYPE_4BYTE_ABGR_PRE: + { + Raster raster = image.getRaster(); + ComponentSampleModel csm = + (ComponentSampleModel)raster.getSampleModel(); + // we can pass the image data directly to OpenGL only if + // the raster is tightly packed (i.e. there is no extra + // space at the end of each scanline) and only if the + // GL_EXT_abgr extension is present + + // FIXME: with the way this is currently organized we can't + // probe for the existence of the GL_EXT_abgr extension + // here; disable this code path for now + if (((csm.getScanlineStride() / 4) == csm.getWidth()) && + /* gl.isExtensionAvailable("GL_EXT_abgr") */ false) + { + pixelFormat = GL.GL_ABGR_EXT; + pixelType = GL.GL_UNSIGNED_BYTE; + alignment = 4; + } else { + createFromCustom(image); + return; + } + } + break; + case BufferedImage.TYPE_USHORT_565_RGB: + pixelFormat = GL.GL_RGB; + pixelType = GL.GL_UNSIGNED_SHORT_5_6_5; + alignment = 2; + break; + case BufferedImage.TYPE_USHORT_555_RGB: + pixelFormat = GL.GL_BGRA; + pixelType = GL.GL_UNSIGNED_SHORT_1_5_5_5_REV; + alignment = 2; + break; + case BufferedImage.TYPE_BYTE_GRAY: + pixelFormat = GL.GL_LUMINANCE; + pixelType = GL.GL_UNSIGNED_BYTE; + alignment = 1; + break; + case BufferedImage.TYPE_USHORT_GRAY: + pixelFormat = GL.GL_LUMINANCE; + pixelType = GL.GL_UNSIGNED_SHORT; + alignment = 2; + break; + case BufferedImage.TYPE_INT_ARGB: + case BufferedImage.TYPE_4BYTE_ABGR: + case BufferedImage.TYPE_BYTE_BINARY: + case BufferedImage.TYPE_BYTE_INDEXED: + case BufferedImage.TYPE_CUSTOM: + default: + ColorModel cm = image.getColorModel(); + if (cm.equals(rgbColorModel)) { + pixelFormat = GL.GL_RGB; + pixelType = GL.GL_UNSIGNED_BYTE; + alignment = 1; + } else if (cm.equals(rgbaColorModel)) { + pixelFormat = GL.GL_RGBA; + pixelType = GL.GL_UNSIGNED_BYTE; + alignment = 4; + } else { + createFromCustom(image); + return; + } + break; + } + + createNIOBufferFromImage(image, true); + } + + private void createFromCustom(BufferedImage image) { + int width = image.getWidth(); + int height = image.getHeight(); + + // create a temporary image that is compatible with OpenGL + boolean hasAlpha = image.getColorModel().hasAlpha(); + ColorModel cm = null; + int dataBufferType = image.getRaster().getDataBuffer().getDataType(); + if (dataBufferType == DataBuffer.TYPE_BYTE) { + cm = hasAlpha ? rgbaColorModel : rgbColorModel; + } else { + if (hasAlpha) { + cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), + null, true, true, + Transparency.TRANSLUCENT, + dataBufferType); + } else { + cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), + null, false, false, + Transparency.OPAQUE, + dataBufferType); + } + } + + boolean premult = cm.isAlphaPremultiplied(); + WritableRaster raster = + cm.createCompatibleWritableRaster(width, height); + BufferedImage texImage = new BufferedImage(cm, raster, premult, null); + + // copy the source image into the temporary image + Graphics2D g = texImage.createGraphics(); + g.setComposite(AlphaComposite.Src); + // Flip image vertically as long as we're at it + g.drawImage(image, + 0, height, width, 0, + 0, 0, width, height, + Color.BLACK, + null); + g.dispose(); + + // Wrap the buffer from the temporary image + createNIOBufferFromImage(texImage, false); + pixelFormat = hasAlpha ? GL.GL_RGBA : GL.GL_RGB; + alignment = 1; // FIXME: do we need better? + } +} diff --git a/src/classes/com/sun/opengl/utils/TextureIO.java b/src/classes/com/sun/opengl/utils/TextureIO.java new file mode 100755 index 000000000..c39cf65e1 --- /dev/null +++ b/src/classes/com/sun/opengl/utils/TextureIO.java @@ -0,0 +1,794 @@ +/* + * Copyright (c) 2005 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 or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.sun.opengl.utils; + +import java.awt.image.*; +import java.io.*; +import java.net.*; +import java.nio.*; +import java.util.*; +import javax.imageio.*; + +import javax.media.opengl.*; + +/** <P> Provides input and output facilities for both loading OpenGL + textures from disk and streams as well as writing textures already + in memory back to disk. </P> + + <P> The TextureIO class supports an arbitrary number of plug-in + TextureProviders which know how to produce TextureData objects + from files, InputStreams and URLs. The TextureData class + represents the raw data of the texture before it has been + converted to an OpenGL texture object. The Texture class + represents the OpenGL texture object and provides easy facilities + for using the texture. </P> + + <P> There are several built-in TextureProviders supplied with the + TextureIO implementation. The most basic provider uses the + platform's Image I/O facilities to read in a BufferedImage and + convert it to a texture. This is the baseline provider and is + registered so that it is the last one consulted. All others are + asked first to open a given file. </P> + + <P> There are three other providers registered by default as of + the time of this writing. One handles SGI RGB (".sgi", ".rgb") + images from both files and streams. One handles DirectDraw Surface + (".dds") images read from files, though can not read these images + from streams. One handles Targa (".tga") images read from both + files and streams. These providers are executed in an arbitrary + order. Some of these providers require the file's suffix to either + be specified via the newTextureData methods or for the file to be + named with the appropriate suffix. In general a file suffix should + be provided to the newTexture and newTextureData methods if at all + possible. </P> + + <P> Note that additional TextureProviders, if reading images from + InputStreams, must use the mark()/reset() methods on InputStream + when probing for e.g. magic numbers at the head of the file to + make sure not to disturb the state of the InputStream for + downstream TextureProviders. </P> +*/ + +public class TextureIO { + /** Constant which can be used as a file suffix to indicate a + DirectDraw Surface file. */ + public static final String DDS = "dds"; + + /** Constant which can be used as a file suffix to indicate an SGI + RGB file. */ + public static final String SGI = "sgi"; + + /** Constant which can be used as a file suffix to indicate an SGI + RGB file. */ + public static final String SGI_RGB = "rgb"; + + /** Constant which can be used as a file suffix to indicate a GIF + file. */ + public static final String GIF = "gif"; + + /** Constant which can be used as a file suffix to indicate a JPEG + file. */ + public static final String JPG = "jpg"; + + /** Constant which can be used as a file suffix to indicate a PNG + file. */ + public static final String PNG = "png"; + + /** Constant which can be used as a file suffix to indicate a Targa + file. */ + public static final String TGA = "tga"; + + /** Constant which can be used as a file suffix to indicate a TIFF + file. */ + public static final String TIFF = "tiff"; + + //---------------------------------------------------------------------- + // methods that *do not* require a current context + // These methods assume RGB or RGBA textures. + // Some texture providers may not recognize the file format unless + // the fileSuffix is specified, so it is strongly recommended to + // specify it wherever it is known. + // Some texture providers may also only support one kind of input, + // i.e., reading from a file as opposed to a stream. + + /** + * Creates a TextureData representing the specified mipmap level of + * a texture from the given file. Does no OpenGL work. + * + * @param file the file from which to read the texture data + * @param mipmapLevel the mipmap level this data represents (FIXME: + * not currently used, needs to be rethought) + * @param fileSuffix the suffix of the file name to be used as a + * hint of the file format to the underlying + * texture provider, or null if none and should be + * auto-detected (some texture providers do not + * support this) + * @return the texture data from the file, or null if none of the + * registered texture providers could read the file + * @throws IOException if an error occurred while reading the file + */ + public static TextureData newTextureData(File file, + int mipmapLevel, + String fileSuffix) throws IOException { + return newTextureDataImpl(file, mipmapLevel, 0, 0, fileSuffix); + } + + /** + * Creates a TextureData representing the specified mipmap level of + * a texture from the given stream. Does no OpenGL work. + * + * @param stream the stream from which to read the texture data + * @param mipmapLevel the mipmap level this data represents (FIXME: + * not currently used, needs to be rethought) + * @param fileSuffix the suffix of the file name to be used as a + * hint of the file format to the underlying + * texture provider, or null if none and should be + * auto-detected (some texture providers do not + * support this) + * @return the texture data from the stream, or null if none of the + * registered texture providers could read the stream + * @throws IOException if an error occurred while reading the stream + */ + public static TextureData newTextureData(InputStream stream, + int mipmapLevel, + String fileSuffix) throws IOException { + return newTextureDataImpl(stream, mipmapLevel, 0, 0, fileSuffix); + } + + /** + * Creates a TextureData representing the specified mipmap level of + * a texture from the given URL. Does no OpenGL work. + * + * @param url the URL from which to read the texture data + * @param mipmapLevel the mipmap level this data represents (FIXME: + * not currently used, needs to be rethought) + * @param fileSuffix the suffix of the file name to be used as a + * hint of the file format to the underlying + * texture provider, or null if none and should be + * auto-detected (some texture providers do not + * support this) + * @return the texture data from the URL, or null if none of the + * registered texture providers could read the URL + * @throws IOException if an error occurred while reading the URL + */ + public static TextureData newTextureData(URL url, + int mipmapLevel, + String fileSuffix) throws IOException { + return newTextureDataImpl(url, mipmapLevel, 0, 0, fileSuffix); + } + + /** + * Creates a TextureData representing the specified mipmap level of + * a texture from the given BufferedImage. Does no OpenGL work. + * + * @param image the BufferedImage containing the texture data + * @param mipmapLevel the mipmap level this data represents (FIXME: + * not currently used, needs to be rethought) + * @return the texture data from the image + */ + public static TextureData newTextureData(BufferedImage image, + int mipmapLevel) { + return newTextureDataImpl(image, mipmapLevel, 0, 0); + } + + //---------------------------------------------------------------------- + // These methods make no assumption about the OpenGL internal format + // or pixel format of the texture; they must be specified by the + // user. It is not allowed to supply 0 (indicating no preference) + // for either the internalFormat or the pixelFormat; + // IllegalArgumentException will be thrown in this case. + + /** + * Creates a TextureData representing the specified mipmap level of + * a texture from the given file, using the specified OpenGL + * internal format and pixel format for the texture which will + * eventually result. The internalFormat and pixelFormat must be + * specified and may not be zero; to use default values, use the + * variant of this method which does not take these arguments. Does + * no OpenGL work. + * + * @param file the file from which to read the texture data + * @param mipmapLevel the mipmap level this data represents (FIXME: + * not currently used, needs to be rethought) + * @param internalFormat the OpenGL internal format of the texture + * which will eventually result from the TextureData + * @param pixelFormat the OpenGL pixel format of the texture + * which will eventually result from the TextureData + * @param fileSuffix the suffix of the file name to be used as a + * hint of the file format to the underlying + * texture provider, or null if none and should be + * auto-detected (some texture providers do not + * support this) + * @return the texture data from the file, or null if none of the + * registered texture providers could read the file + * @throws IllegalArgumentException if either internalFormat or + * pixelFormat was 0 + * @throws IOException if an error occurred while reading the file + */ + public static TextureData newTextureData(File file, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException, IllegalArgumentException { + if ((internalFormat == 0) || (pixelFormat == 0)) { + throw new IllegalArgumentException("internalFormat and pixelFormat must be non-zero"); + } + + return newTextureDataImpl(file, mipmapLevel, internalFormat, pixelFormat, fileSuffix); + } + + /** + * Creates a TextureData representing the specified mipmap level of + * a texture from the given stream, using the specified OpenGL + * internal format and pixel format for the texture which will + * eventually result. The internalFormat and pixelFormat must be + * specified and may not be zero; to use default values, use the + * variant of this method which does not take these arguments. Does + * no OpenGL work. + * + * @param stream the stream from which to read the texture data + * @param mipmapLevel the mipmap level this data represents (FIXME: + * not currently used, needs to be rethought) + * @param internalFormat the OpenGL internal format of the texture + * which will eventually result from the TextureData + * @param pixelFormat the OpenGL pixel format of the texture + * which will eventually result from the TextureData + * @param fileSuffix the suffix of the file name to be used as a + * hint of the file format to the underlying + * texture provider, or null if none and should be + * auto-detected (some texture providers do not + * support this) + * @return the texture data from the stream, or null if none of the + * registered texture providers could read the stream + * @throws IllegalArgumentException if either internalFormat or + * pixelFormat was 0 + * @throws IOException if an error occurred while reading the stream + */ + public static TextureData newTextureData(InputStream stream, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException, IllegalArgumentException { + if ((internalFormat == 0) || (pixelFormat == 0)) { + throw new IllegalArgumentException("internalFormat and pixelFormat must be non-zero"); + } + + return newTextureDataImpl(stream, mipmapLevel, internalFormat, pixelFormat, fileSuffix); + } + + /** + * Creates a TextureData representing the specified mipmap level of + * a texture from the given URL, using the specified OpenGL + * internal format and pixel format for the texture which will + * eventually result. The internalFormat and pixelFormat must be + * specified and may not be zero; to use default values, use the + * variant of this method which does not take these arguments. Does + * no OpenGL work. + * + * @param url the URL from which to read the texture data + * @param mipmapLevel the mipmap level this data represents (FIXME: + * not currently used, needs to be rethought) + * @param internalFormat the OpenGL internal format of the texture + * which will eventually result from the TextureData + * @param pixelFormat the OpenGL pixel format of the texture + * which will eventually result from the TextureData + * @param fileSuffix the suffix of the file name to be used as a + * hint of the file format to the underlying + * texture provider, or null if none and should be + * auto-detected (some texture providers do not + * support this) + * @return the texture data from the URL, or null if none of the + * registered texture providers could read the URL + * @throws IllegalArgumentException if either internalFormat or + * pixelFormat was 0 + * @throws IOException if an error occurred while reading the URL + */ + public static TextureData newTextureData(URL url, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException, IllegalArgumentException { + if ((internalFormat == 0) || (pixelFormat == 0)) { + throw new IllegalArgumentException("internalFormat and pixelFormat must be non-zero"); + } + + return newTextureDataImpl(url, mipmapLevel, internalFormat, pixelFormat, fileSuffix); + } + + /** + * Creates a TextureData representing the specified mipmap level of + * a texture from the given BufferedImage, using the specified + * OpenGL internal format and pixel format for the texture which + * will eventually result. The internalFormat and pixelFormat must + * be specified and may not be zero; to use default values, use the + * variant of this method which does not take these arguments. Does + * no OpenGL work. + * + * @param image the BufferedImage containing the texture data + * @param mipmapLevel the mipmap level this data represents (FIXME: + * not currently used, needs to be rethought) + * @param internalFormat the OpenGL internal format of the texture + * which will eventually result from the TextureData + * @param pixelFormat the OpenGL pixel format of the texture + * which will eventually result from the TextureData + * @return the texture data from the image + * @throws IllegalArgumentException if either internalFormat or + * pixelFormat was 0 + */ + public static TextureData newTextureData(BufferedImage image, + int mipmapLevel, + int internalFormat, + int pixelFormat) throws IllegalArgumentException { + if ((internalFormat == 0) || (pixelFormat == 0)) { + throw new IllegalArgumentException("internalFormat and pixelFormat must be non-zero"); + } + + return newTextureDataImpl(image, mipmapLevel, internalFormat, pixelFormat); + } + + //---------------------------------------------------------------------- + // methods that *do* require a current context + // + + /** + * Creates an OpenGL texture object from the specified TextureData + * using the current OpenGL context. + * + * @param data the texture data to turn into an OpenGL texture + * @throws GLException if no OpenGL context is current or if an + * OpenGL error occurred + */ + public static Texture newTexture(TextureData data) throws GLException { + if (data == null) { + return null; + } + return new Texture(data); + } + + /** + * Creates an OpenGL texture object from the specified file using + * the current OpenGL context. + * + * @param file the file from which to read the texture data + * @throws IOException if an error occurred while reading the file + * @throws GLException if no OpenGL context is current or if an + * OpenGL error occurred + */ + public static Texture newTexture(File file) throws IOException, GLException { + TextureData data = newTextureData(file, 0, getFileSuffix(file)); + Texture texture = newTexture(data); + data.flush(); + return texture; + } + + /** + * Creates an OpenGL texture object from the specified stream using + * the current OpenGL context. + * + * @param stream the stream from which to read the texture data + * @throws IOException if an error occurred while reading the stream + * @throws GLException if no OpenGL context is current or if an + * OpenGL error occurred + */ + public static Texture newTexture(InputStream stream) throws IOException, GLException { + TextureData data = newTextureData(stream, 0, null); + Texture texture = newTexture(data); + data.flush(); + return texture; + } + + /** + * Creates an OpenGL texture object from the specified URL using + * the current OpenGL context. + * + * @param url the URL from which to read the texture data + * @throws IOException if an error occurred while reading the URL + * @throws GLException if no OpenGL context is current or if an + * OpenGL error occurred + */ + public static Texture newTexture(URL url) throws IOException, GLException { + TextureData data = newTextureData(url, 0, null); + Texture texture = newTexture(data); + data.flush(); + return texture; + } + + /** + * Creates an OpenGL texture object from the specified BufferedImage + * using the current OpenGL context. + * + * @param image the BufferedImage from which to read the texture data + * @throws GLException if no OpenGL context is current or if an + * OpenGL error occurred + */ + public static Texture newTexture(BufferedImage image) throws GLException { + TextureData data = newTextureData(image, 0); + Texture texture = newTexture(data); + data.flush(); + return texture; + } + + // FIXME: add texture writing capabilities + // public void writeTextureToFile(Texture texture, File file, boolean saveUncompressed) throws IOException, GLException; + + //---------------------------------------------------------------------- + // SPI support + // + + /** Adds a TextureProvider to support reading of a new file + format. */ + public static void addTextureProvider(TextureProvider provider) { + // Must always add at the front so the ImageIO provider is last, + // so we don't accidentally use it instead of a user's possibly + // more optimal provider + textureProviders.add(0, provider); + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private static List/*<TextureProvider>*/ textureProviders = new ArrayList/*<TextureProvider>*/(); + + static { + // ImageIO provider, the fall-back, must be the first one added + addTextureProvider(new IIOTextureProvider()); + + // Other special-case providers + addTextureProvider(new DDSTextureProvider()); + addTextureProvider(new SGITextureProvider()); + addTextureProvider(new TGATextureProvider()); + } + + // Implementation methods + private static TextureData newTextureDataImpl(File file, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + for (Iterator iter = textureProviders.iterator(); iter.hasNext(); ) { + TextureProvider provider = (TextureProvider) iter.next(); + TextureData data = provider.newTextureData(file, + mipmapLevel, + internalFormat, + pixelFormat, + fileSuffix); + if (data != null) { + return data; + } + } + return null; + } + + private static TextureData newTextureDataImpl(InputStream stream, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + for (Iterator iter = textureProviders.iterator(); iter.hasNext(); ) { + TextureProvider provider = (TextureProvider) iter.next(); + TextureData data = provider.newTextureData(stream, + mipmapLevel, + internalFormat, + pixelFormat, + fileSuffix); + if (data != null) { + return data; + } + } + + return null; + } + + private static TextureData newTextureDataImpl(URL url, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + for (Iterator iter = textureProviders.iterator(); iter.hasNext(); ) { + TextureProvider provider = (TextureProvider) iter.next(); + TextureData data = provider.newTextureData(url, + mipmapLevel, + internalFormat, + pixelFormat, + fileSuffix); + if (data != null) { + return data; + } + } + + return null; + } + + private static TextureData newTextureDataImpl(BufferedImage image, + int mipmapLevel, + int internalFormat, + int pixelFormat) { + return new TextureData(mipmapLevel, internalFormat, pixelFormat, image); + } + + //---------------------------------------------------------------------- + // Base provider - used last + static class IIOTextureProvider implements TextureProvider { + public TextureData newTextureData(File file, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + BufferedImage img = ImageIO.read(file); + if (img == null) { + return null; + } + return new TextureData(mipmapLevel, internalFormat, pixelFormat, img); + } + + public TextureData newTextureData(InputStream stream, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + BufferedImage img = ImageIO.read(stream); + if (img == null) { + return null; + } + return new TextureData(mipmapLevel, internalFormat, pixelFormat, img); + } + + public TextureData newTextureData(URL url, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + InputStream stream = url.openStream(); + try { + return newTextureData(stream, mipmapLevel, internalFormat, pixelFormat, fileSuffix); + } finally { + stream.close(); + } + } + } + + //---------------------------------------------------------------------- + // DDS provider -- supports files only for now + static class DDSTextureProvider implements TextureProvider { + public TextureData newTextureData(File file, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + if (DDS.equals(fileSuffix) || + DDS.equals(getFileSuffix(file))) { + final DDSReader reader = new DDSReader(); + reader.loadFile(file); + // FIXME: handle case where all mipmaps are requested -- this + // will require API changes + DDSReader.ImageInfo info = reader.getMipMap(mipmapLevel); + if (pixelFormat == 0) { + switch (reader.getPixelFormat()) { + case DDSReader.D3DFMT_R8G8B8: + pixelFormat = GL.GL_RGB; + break; + default: + pixelFormat = GL.GL_RGBA; + break; + } + } + if (info.isCompressed()) { + switch (info.getCompressionFormat()) { + case DDSReader.D3DFMT_DXT1: + internalFormat = GL.GL_COMPRESSED_RGB_S3TC_DXT1_EXT; + break; + case DDSReader.D3DFMT_DXT3: + internalFormat = GL.GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; + break; + case DDSReader.D3DFMT_DXT5: + internalFormat = GL.GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; + break; + default: + throw new RuntimeException("Unsupported DDS compression format \"" + + DDSReader.getCompressionFormatName(info.getCompressionFormat()) + "\""); + } + } + if (internalFormat == 0) { + switch (reader.getPixelFormat()) { + case DDSReader.D3DFMT_R8G8B8: + pixelFormat = GL.GL_RGB; + break; + default: + pixelFormat = GL.GL_RGBA; + break; + } + } + TextureData.Flusher flusher = new TextureData.Flusher() { + public void flush() { + reader.close(); + } + }; + TextureData data = new TextureData(mipmapLevel, + internalFormat, + info.getWidth(), + info.getHeight(), + 0, + pixelFormat, + GL.GL_UNSIGNED_BYTE, + info.isCompressed(), + true, + info.getData(), + flusher); + return data; + } + + return null; + } + + public TextureData newTextureData(InputStream stream, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + return null; + } + + public TextureData newTextureData(URL url, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + return null; + } + } + + //---------------------------------------------------------------------- + // Base class for SGI RGB and TGA image providers + static abstract class StreamBasedTextureProvider implements TextureProvider { + public TextureData newTextureData(File file, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + InputStream inStream = new BufferedInputStream(new FileInputStream(file)); + try { + // The SGIImage and TGAImage implementations use InputStreams + // anyway so there isn't much point in having a separate code + // path for files + return newTextureData(inStream, + mipmapLevel, + internalFormat, + pixelFormat, + ((fileSuffix != null) ? fileSuffix : getFileSuffix(file))); + } finally { + inStream.close(); + } + } + + public TextureData newTextureData(URL url, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + InputStream stream = url.openStream(); + try { + return newTextureData(stream, mipmapLevel, internalFormat, pixelFormat, fileSuffix); + } finally { + stream.close(); + } + } + } + + //---------------------------------------------------------------------- + // SGI RGB image provider + static class SGITextureProvider extends StreamBasedTextureProvider { + public TextureData newTextureData(InputStream stream, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + if (SGI.equals(fileSuffix) || + SGI_RGB.equals(fileSuffix) || + SGIImage.isSGIImage(stream)) { + SGIImage image = SGIImage.read(stream); + if (pixelFormat == 0) { + pixelFormat = image.getFormat(); + } + if (internalFormat == 0) { + internalFormat = image.getFormat(); + } + return new TextureData(mipmapLevel, + internalFormat, + image.getWidth(), + image.getHeight(), + 0, + pixelFormat, + GL.GL_UNSIGNED_BYTE, + false, + false, + ByteBuffer.wrap(image.getData()), + null); + } + + return null; + } + } + + //---------------------------------------------------------------------- + // TGA (Targa) image provider + static class TGATextureProvider extends StreamBasedTextureProvider { + public TextureData newTextureData(InputStream stream, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException { + if (TGA.equals(fileSuffix)) { + TGAImage image = TGAImage.read(stream); + if (pixelFormat == 0) { + pixelFormat = image.getGLFormat(); + } + if (internalFormat == 0) { + internalFormat = GL.GL_RGBA8; + } + return new TextureData(mipmapLevel, + internalFormat, + image.getWidth(), + image.getHeight(), + 0, + pixelFormat, + GL.GL_UNSIGNED_BYTE, + false, + false, + ByteBuffer.wrap(image.getData()), + null); + } + + return null; + } + } + + //---------------------------------------------------------------------- + // Helper function for above TextureProviders + private static String getFileSuffix(File file) { + String name = file.getName().toLowerCase(); + + int lastDot = name.lastIndexOf('.'); + if (lastDot < 0) { + return null; + } + return name.substring(lastDot + 1); + } +} diff --git a/src/classes/com/sun/opengl/utils/TextureProvider.java b/src/classes/com/sun/opengl/utils/TextureProvider.java new file mode 100755 index 000000000..4b71ae982 --- /dev/null +++ b/src/classes/com/sun/opengl/utils/TextureProvider.java @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2005 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 or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.sun.opengl.utils; + +import java.io.*; +import java.net.*; + +/** Plug-in interface to TextureIO to support reading OpenGL textures + from new file formats. For all methods, either internalFormat or + pixelFormat may be 0 in which case they must be inferred as + e.g. RGB or RGBA depending on the file contents. +*/ + +public interface TextureProvider { + + /** + * Produces a TextureData object from a file, or returns null if the + * file format was not supported by this TextureProvider. Does not + * do any OpenGL-related work. The resulting TextureData can be + * converted into an OpenGL texture in a later step. + * + * @param file the file from which to read the texture data + * + * @param mipmapLevel the mipmap level of the resulting texture being + * read (FIXME: needs rethinking, not yet working) + * + * @param internalFormat the OpenGL internal format to be used for + * the texture, or 0 if it should be inferred + * from the file's contents + * + * @param pixelFormat the OpenGL pixel format to be used for + * the texture, or 0 if it should be inferred + * from the file's contents + * + * @param fileSuffix the file suffix to be used as a hint to the + * provider to more quickly decide whether it + * can handle the file, or null if the + * provider should infer the type from the + * file's contents + * + * @throws IOException if an error occurred while reading the file + */ + public TextureData newTextureData(File file, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException; + + /** + * Produces a TextureData object from a stream, or returns null if + * the file format was not supported by this TextureProvider. Does + * not do any OpenGL-related work. The resulting TextureData can be + * converted into an OpenGL texture in a later step. + * + * @param stream the stream from which to read the texture data + * + * @param mipmapLevel the mipmap level of the resulting texture being + * read (FIXME: needs rethinking, not yet working) + * + * @param internalFormat the OpenGL internal format to be used for + * the texture, or 0 if it should be inferred + * from the file's contents + * + * @param pixelFormat the OpenGL pixel format to be used for + * the texture, or 0 if it should be inferred + * from the file's contents + * + * @param fileSuffix the file suffix to be used as a hint to the + * provider to more quickly decide whether it + * can handle the file, or null if the + * provider should infer the type from the + * file's contents + * + * @throws IOException if an error occurred while reading the stream + */ + public TextureData newTextureData(InputStream stream, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException; + + /** + * Produces a TextureData object from a URL, or returns null if the + * file format was not supported by this TextureProvider. Does not + * do any OpenGL-related work. The resulting TextureData can be + * converted into an OpenGL texture in a later step. + * + * @param url the URL from which to read the texture data + * + * @param mipmapLevel the mipmap level of the resulting texture being + * read (FIXME: needs rethinking, not yet working) + * + * @param internalFormat the OpenGL internal format to be used for + * the texture, or 0 if it should be inferred + * from the file's contents + * + * @param pixelFormat the OpenGL pixel format to be used for + * the texture, or 0 if it should be inferred + * from the file's contents + * + * @param fileSuffix the file suffix to be used as a hint to the + * provider to more quickly decide whether it + * can handle the file, or null if the + * provider should infer the type from the + * file's contents + * + * @throws IOException if an error occurred while reading the URL + */ + public TextureData newTextureData(URL url, + int mipmapLevel, + int internalFormat, + int pixelFormat, + String fileSuffix) throws IOException; +} |