From 0c30c86aa2b6dc0d12a90f8de899f6f057675d53 Mon Sep 17 00:00:00 2001 From: Kenneth Russel Date: Fri, 5 May 2006 19:31:38 +0000 Subject: Added StreamUtil for reading all data from an InputStream. Added support to DDSImage and TextureIO classes for reading DDS files from InputStreams and URLs based on request from Tom Gaskins. git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/trunk@754 232f8b59-042b-4e1e-8c03-345bb8c30851 --- .../com/sun/opengl/util/texture/TextureIO.java | 192 ++++++++++++--------- 1 file changed, 115 insertions(+), 77 deletions(-) (limited to 'src/classes/com/sun/opengl/util/texture/TextureIO.java') diff --git a/src/classes/com/sun/opengl/util/texture/TextureIO.java b/src/classes/com/sun/opengl/util/texture/TextureIO.java index c1e5bcf8b..ca57e9886 100755 --- a/src/classes/com/sun/opengl/util/texture/TextureIO.java +++ b/src/classes/com/sun/opengl/util/texture/TextureIO.java @@ -168,6 +168,9 @@ public class TextureIO { public static TextureData newTextureData(File file, boolean mipmap, String fileSuffix) throws IOException { + if (fileSuffix == null) { + fileSuffix = FileUtil.getFileSuffix(file); + } return newTextureDataImpl(file, 0, 0, mipmap, fileSuffix); } @@ -218,6 +221,9 @@ public class TextureIO { public static TextureData newTextureData(URL url, boolean mipmap, String fileSuffix) throws IOException { + if (fileSuffix == null) { + fileSuffix = FileUtil.getFileSuffix(url.getPath()); + } return newTextureDataImpl(url, 0, 0, mipmap, fileSuffix); } @@ -281,6 +287,10 @@ public class TextureIO { throw new IllegalArgumentException("internalFormat and pixelFormat must be non-zero"); } + if (fileSuffix == null) { + fileSuffix = FileUtil.getFileSuffix(file); + } + return newTextureDataImpl(file, internalFormat, pixelFormat, mipmap, fileSuffix); } @@ -365,6 +375,10 @@ public class TextureIO { throw new IllegalArgumentException("internalFormat and pixelFormat must be non-zero"); } + if (fileSuffix == null) { + fileSuffix = FileUtil.getFileSuffix(url.getPath()); + } + return newTextureDataImpl(url, internalFormat, pixelFormat, mipmap, fileSuffix); } @@ -496,6 +510,9 @@ public class TextureIO { * OpenGL error occurred */ public static Texture newTexture(URL url, boolean mipmap, String fileSuffix) throws IOException, GLException { + if (fileSuffix == null) { + fileSuffix = FileUtil.getFileSuffix(url.getPath()); + } TextureData data = newTextureData(url, mipmap, fileSuffix); Texture texture = newTexture(data); data.flush(); @@ -835,82 +852,8 @@ public class TextureIO { String fileSuffix) throws IOException { if (DDS.equals(fileSuffix) || DDS.equals(FileUtil.getFileSuffix(file))) { - final DDSImage image = DDSImage.read(file); - DDSImage.ImageInfo info = image.getMipMap(0); - if (pixelFormat == 0) { - switch (image.getPixelFormat()) { - case DDSImage.D3DFMT_R8G8B8: - pixelFormat = GL.GL_RGB; - break; - default: - pixelFormat = GL.GL_RGBA; - break; - } - } - if (info.isCompressed()) { - switch (info.getCompressionFormat()) { - case DDSImage.D3DFMT_DXT1: - internalFormat = GL.GL_COMPRESSED_RGB_S3TC_DXT1_EXT; - break; - case DDSImage.D3DFMT_DXT3: - internalFormat = GL.GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; - break; - case DDSImage.D3DFMT_DXT5: - internalFormat = GL.GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; - break; - default: - throw new RuntimeException("Unsupported DDS compression format \"" + - DDSImage.getCompressionFormatName(info.getCompressionFormat()) + "\""); - } - } - if (internalFormat == 0) { - switch (image.getPixelFormat()) { - case DDSImage.D3DFMT_R8G8B8: - pixelFormat = GL.GL_RGB; - break; - default: - pixelFormat = GL.GL_RGBA; - break; - } - } - TextureData.Flusher flusher = new TextureData.Flusher() { - public void flush() { - image.close(); - } - }; - TextureData data; - if (mipmap && image.getNumMipMaps() > 0) { - Buffer[] mipmapData = new Buffer[image.getNumMipMaps()]; - for (int i = 0; i < image.getNumMipMaps(); i++) { - mipmapData[i] = image.getMipMap(i).getData(); - } - data = new TextureData(internalFormat, - info.getWidth(), - info.getHeight(), - 0, - pixelFormat, - GL.GL_UNSIGNED_BYTE, - info.isCompressed(), - true, - mipmapData, - flusher); - } else { - // Fix this up for the end user because we can't generate - // mipmaps for compressed textures - mipmap = false; - data = new TextureData(internalFormat, - info.getWidth(), - info.getHeight(), - 0, - pixelFormat, - GL.GL_UNSIGNED_BYTE, - mipmap, - info.isCompressed(), - true, - info.getData(), - flusher); - } - return data; + DDSImage image = DDSImage.read(file); + return newTextureData(image, internalFormat, pixelFormat, mipmap); } return null; @@ -921,6 +864,14 @@ public class TextureIO { int pixelFormat, boolean mipmap, String fileSuffix) throws IOException { + if (DDS.equals(fileSuffix) || + DDSImage.isDDSImage(stream)) { + byte[] data = StreamUtil.readAll(stream); + ByteBuffer buf = ByteBuffer.wrap(data); + DDSImage image = DDSImage.read(buf); + return newTextureData(image, internalFormat, pixelFormat, mipmap); + } + return null; } @@ -929,7 +880,94 @@ public class TextureIO { int pixelFormat, boolean mipmap, String fileSuffix) throws IOException { - return null; + InputStream stream = new BufferedInputStream(url.openStream()); + try { + return newTextureData(stream, internalFormat, pixelFormat, mipmap, fileSuffix); + } finally { + stream.close(); + } + } + + private TextureData newTextureData(final DDSImage image, + int internalFormat, + int pixelFormat, + boolean mipmap) { + DDSImage.ImageInfo info = image.getMipMap(0); + if (pixelFormat == 0) { + switch (image.getPixelFormat()) { + case DDSImage.D3DFMT_R8G8B8: + pixelFormat = GL.GL_RGB; + break; + default: + pixelFormat = GL.GL_RGBA; + break; + } + } + if (info.isCompressed()) { + switch (info.getCompressionFormat()) { + case DDSImage.D3DFMT_DXT1: + internalFormat = GL.GL_COMPRESSED_RGB_S3TC_DXT1_EXT; + break; + case DDSImage.D3DFMT_DXT3: + internalFormat = GL.GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; + break; + case DDSImage.D3DFMT_DXT5: + internalFormat = GL.GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; + break; + default: + throw new RuntimeException("Unsupported DDS compression format \"" + + DDSImage.getCompressionFormatName(info.getCompressionFormat()) + "\""); + } + } + if (internalFormat == 0) { + switch (image.getPixelFormat()) { + case DDSImage.D3DFMT_R8G8B8: + pixelFormat = GL.GL_RGB; + break; + default: + pixelFormat = GL.GL_RGBA; + break; + } + } + TextureData.Flusher flusher = new TextureData.Flusher() { + public void flush() { + image.close(); + } + }; + TextureData data; + if (mipmap && image.getNumMipMaps() > 0) { + Buffer[] mipmapData = new Buffer[image.getNumMipMaps()]; + for (int i = 0; i < image.getNumMipMaps(); i++) { + mipmapData[i] = image.getMipMap(i).getData(); + } + System.err.println("Creating from mipmapped data"); + data = new TextureData(internalFormat, + info.getWidth(), + info.getHeight(), + 0, + pixelFormat, + GL.GL_UNSIGNED_BYTE, + info.isCompressed(), + true, + mipmapData, + flusher); + } else { + // Fix this up for the end user because we can't generate + // mipmaps for compressed textures + mipmap = false; + data = new TextureData(internalFormat, + info.getWidth(), + info.getHeight(), + 0, + pixelFormat, + GL.GL_UNSIGNED_BYTE, + mipmap, + info.isCompressed(), + true, + info.getData(), + flusher); + } + return data; } } -- cgit v1.2.3