ddscompressor.java
来自「world wind java sdk 源码」· Java 代码 · 共 436 行 · 第 1/2 页
JAVA
436 行
/* Copyright (C) 2001, 2008 United States Government as represented bythe Administrator of the National Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.formats.dds;import gov.nasa.worldwind.util.*;import java.awt.image.*;/** * <code>DDSCompressor</code> will convert an in-memory image into a DDS file encoded with one of the DXT block * compression schemes. If the caller wants to encode using a certain type of DXT compression, * <code>DDSCompressor</code> provides the appropriate methods to do that. Otherwise, <code>DDSCompressor</code> will * choose the DXT compression scheme that best suits the source image. * <p> * Each compression method accepts a reference to a <code>DXTCompressionAttributes</code>. This compressor will perform * the appropriate actions according to the attributes, such as building mip maps and converting the source image to a * premultiplied alpha format. * * @author dcollins * @version $Id: DDSCompressor.java 9600 2009-03-22 20:04:40Z tgaskins $ */public class DDSCompressor{ /** * Creates a new <code>DDSCompressor</code>, but otherwise does nothing. */ public DDSCompressor() { } /** * Convenience method to convert the specified <code>imageBuffer</code> to DDS according to the default attributes. * The bytes in <code>imageBuffer</code> must be readable by {@link javax.imageio.ImageIO}. Once the image data is * read, this is equivalent to calling {#compressImage(java.awt.image.BufferedImage)} with the BufferedImage * created by ImageIO. If the image data is not in a format understood by ImageIO, this method will return null. * * @param imageBuffer image file data to convert to the DDS file format. * * @return little endian ordered ByteBuffer containing the dds file bytes, or null if the bytes in * <code>imageBuffer</code> are not in a format understood by ImageIO. * @throws java.io.IOException if the bytes in <code>imageBuffer</code> are in a format understood by ImageIO, but * the image data cannot be read by ImageIO. * @throws IllegalArgumentException if <code>imageBuffer</code> is null. */ public static java.nio.ByteBuffer compressImageBuffer(java.nio.ByteBuffer imageBuffer) throws java.io.IOException { if (imageBuffer == null) { String message = Logging.getMessage("nullValue.Image"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } java.io.InputStream inputStream = WWIO.getInputStreamFromByteBuffer(imageBuffer); java.awt.image.BufferedImage image = javax.imageio.ImageIO.read(inputStream); if (image == null) { return null; } return compressImage(image); } /** * Convenience method to convert the specified image <code>file</code> to DDS according to the default attributes. * The <code>file</code> must be readable by {@link javax.imageio.ImageIO}. Once the file is read, this is * equivalent to calling {#compressImage(java.awt.image.BufferedImage)} with the BufferedImage created by ImageIO. * If the file is not in a format understood by ImageIO, this method will return null. * * @param file image file to convert to the DDS file format. * * @return little endian ordered ByteBuffer containing the dds file bytes, or null if the <code>file</code> is not * in a format understood by ImageIO. * @throws java.io.IOException if <code>file</code> is in a format understood by ImageIO, but the image data * cannot be read by ImageIO. * @throws IllegalArgumentException if <code>file</code> is null, does not exist, or read permission is not * allowed on the <code>file</code>. */ public static java.nio.ByteBuffer compressImageFile(java.io.File file) throws java.io.IOException { if (file == null) { String message = Logging.getMessage("nullValue.FileIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (!file.exists() || !file.canRead()) { String message = Logging.getMessage("DDSConverter.NoFileOrNoPermission"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } java.awt.image.BufferedImage image = javax.imageio.ImageIO.read(file); if (image == null) { return null; } return compressImage(image); } /** * Convenience method to convert the specified <code>image</code> to DDS according to the default attributes. This * will choose the DXT compression format best suited for the image type. * * @param image image to convert to the DDS file format. * * @return little endian ordered ByteBuffer containing the dds file bytes. * @throws IllegalArgumentException if <code>image</code> is null, or if <code>image</code> has non power of two * dimensions. */ public static java.nio.ByteBuffer compressImage(java.awt.image.BufferedImage image) { if (image == null) { String message = Logging.getMessage("nullValue.ImageIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (!WWMath.isPowerOfTwo(image.getWidth()) || !WWMath.isPowerOfTwo(image.getHeight())) { String message = Logging.getMessage("generic.InvalidImageSize", image.getWidth(), image.getHeight()); Logging.logger().severe(message); throw new IllegalArgumentException(message); } DDSCompressor compressor = new DDSCompressor(); DXTCompressionAttributes attributes = new DXTCompressionAttributes(); attributes.setBuildMipmaps(true); // Always build mipmaps. attributes.setPremultiplyAlpha(true); // Always create premultiplied alpha format files.. attributes.setDXTFormat(0); // Allow the DDSCompressor to choose the appropriate DXT format. return compressor.compressImage(image, attributes); } /** * Convert the specified <code>image</code> to DDS according to the <code>attributes</code>. If the caller * specified a DXT format in the attributes, then we return a compressor matching that format. Otherwise, we * choose one automatically from the image type. If no choice can be made from the image type, we default to using * a DXT3 compressor. * * @param image image to convert to the DDS file format. * @param attributes attributes that will control the compression. * * @return buffer little endian ordered ByteBuffer containing the dds file bytes. * @throws IllegalArgumentException if either <code>image</code> or <code>attributes</code> are null, or if * <code>image</code> has non power of two dimensions. */ public java.nio.ByteBuffer compressImage(java.awt.image.BufferedImage image, DXTCompressionAttributes attributes) { if (image == null) { String message = Logging.getMessage("nullValue.ImageIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (!WWMath.isPowerOfTwo(image.getWidth()) || !WWMath.isPowerOfTwo(image.getHeight())) { String message = Logging.getMessage("generic.InvalidImageSize", image.getWidth(), image.getHeight()); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (attributes == null) { String message = Logging.getMessage("nullValue.AttributesIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } DXTCompressor compressor = this.getDXTCompressor(image, attributes); return this.doCompressImage(compressor, image, attributes); } /** * Convert the specified <code>image</code> to DDS using the DXT1 codec, and otherwise according to the * <code>attributes</code>. * * @param image image to convert to the DDS file format using the DXT1 codec. * @param attributes attributes that will control the compression. * * @return buffer little endian ordered ByteBuffer containing the dds file bytes. * @throws IllegalArgumentException if either <code>image</code> or <code>attributes</code> are null, or if * <code>image</code> has non power of two dimensions. */ public java.nio.ByteBuffer compressImageDXT1(java.awt.image.BufferedImage image, DXTCompressionAttributes attributes) { if (image == null) { String message = Logging.getMessage("nullValue.ImageIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (!WWMath.isPowerOfTwo(image.getWidth()) || !WWMath.isPowerOfTwo(image.getHeight())) { String message = Logging.getMessage("generic.InvalidImageSize", image.getWidth(), image.getHeight()); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (attributes == null) { String message = Logging.getMessage("nullValue.AttributesIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } DXT1Compressor compressor = new DXT1Compressor(); return this.doCompressImage(compressor, image, attributes); } /** * Convert the specified <code>image</code> to DDS using the DXT3 codec, and otherwise according to the * <code>attributes</code>. * * @param image image to convert to the DDS file format using the DXT3 codec.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?