public void compressImage(java.awt.image.BufferedImage image, DXTCompressionAttributes attributes,
                                  java.nio.ByteBuffer buffer)
        {
            if (image == null)
            {
                String message = Logging.getMessage("nullValue.ImageIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }
            if (attributes == null)
            {
                String message = Logging.getMessage("nullValue.AttributesIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }
            if (buffer == null)
            {
                String message = Logging.getMessage("nullValue.BufferNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }

            // If it is determined that the image and block have no alpha component, then we compress with DXT1 using a
            // four color palette. Otherwise, we use the three color palette (with the fourth color as transparent black).

            ColorBlock4x4       colorBlock          = new ColorBlock4x4();
            ColorBlockExtractor colorBlockExtractor = this.getColorBlockExtractor(image);

            BlockDXT1           dxt1Block      = new BlockDXT1();
            BlockDXT1Compressor dxt1Compressor = new BlockDXT1Compressor();

            int width  = image.getWidth();
            int height = image.getHeight();

            bool imageHasAlpha  = image.getColorModel().hasAlpha();
            bool enableAlpha    = attributes.isEnableDXT1Alpha();
            int  alphaThreshold = attributes.getDXT1AlphaThreshold();

            for (int j = 0; j < height; j += 4)
            {
                for (int i = 0; i < width; i += 4)
                {
                    colorBlockExtractor.extractColorBlock4x4(attributes, i, j, colorBlock);

                    if (enableAlpha && imageHasAlpha && blockHasDXT1Alpha(colorBlock, alphaThreshold))
                    {
                        dxt1Compressor.compressBlockDXT1a(colorBlock, attributes, dxt1Block);
                    }
                    else
                    {
                        dxt1Compressor.compressBlockDXT1(colorBlock, attributes, dxt1Block);
                    }

                    buffer.putShort((short)dxt1Block.color0);
                    buffer.putShort((short)dxt1Block.color1);
                    buffer.putInt((int)dxt1Block.colorIndexMask);
                }
            }
        }
Exemple #2
0
        public void compressImage(java.awt.image.BufferedImage image, DXTCompressionAttributes attributes,
                                  java.nio.ByteBuffer buffer)
        {
            if (image == null)
            {
                String message = Logging.getMessage("nullValue.ImageIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }
            if (attributes == null)
            {
                String message = Logging.getMessage("nullValue.AttributesIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }
            if (buffer == null)
            {
                String message = Logging.getMessage("nullValue.BufferNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }

            ColorBlock4x4       colorBlock          = new ColorBlock4x4();
            ColorBlockExtractor colorBlockExtractor = this.getColorBlockExtractor(image);

            BlockDXT3           dxt3Block      = new BlockDXT3();
            BlockDXT3Compressor dxt3Compressor = new BlockDXT3Compressor();

            int width  = image.getWidth();
            int height = image.getHeight();

            for (int j = 0; j < height; j += 4)
            {
                for (int i = 0; i < width; i += 4)
                {
                    colorBlockExtractor.extractColorBlock4x4(attributes, i, j, colorBlock);
                    dxt3Compressor.compressBlockDXT3(colorBlock, attributes, dxt3Block);

                    AlphaBlockDXT3 dxtAlphaBlock = dxt3Block.getAlphaBlock();
                    buffer.putLong(dxtAlphaBlock.alphaValueMask);

                    BlockDXT1 dxtColorBlock = dxt3Block.getColorBlock();
                    buffer.putShort((short)dxtColorBlock.color0);
                    buffer.putShort((short)dxtColorBlock.color1);
                    buffer.putInt((int)dxtColorBlock.colorIndexMask);
                }
            }
        }
        /**
         * Compress the 4x4 color block into a DXT1 block using four colors. This method ignores transparency and
         * guarantees that the DXT1 block will use four colors.
         * <p>
         * Access to this method must be synchronized by the caller. This method is frequently invoked by the DXT
         * compressor, so in order to reduce garbage each instance of this class has unsynchronized properties that are
         * reused during each call.
         *
         * @param colorBlock the 4x4 color block to compress.
         * @param attributes attributes that will control the compression.
         * @param dxtBlock the DXT1 block that will receive the compressed data.
         *
         * @throws ArgumentException if either <code>colorBlock</code> or <code>dxtBlock</code> are null.
         */
        public void compressBlockDXT1(ColorBlock4x4 colorBlock, DXTCompressionAttributes attributes, BlockDXT1 dxtBlock)
        {
            if (colorBlock == null)
            {
                String message = Logging.getMessage("nullValue.ColorBlockIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }
            if (attributes == null)
            {
                String message = Logging.getMessage("nullValue.AttributesIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }
            if (dxtBlock == null)
            {
                String message = Logging.getMessage("nullValue.DXTBlockIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }

            this.chooseMinMaxColors(colorBlock, attributes, this.minColor, this.maxColor);
            int color0 = short565FromColor32(this.maxColor);
            int color1 = short565FromColor32(this.minColor);

            if (color0 < color1)
            {
                int tmp = color0;
                color0 = color1;
                color1 = tmp;
            }

            // To get a four color palette with no alpha, the first color must be greater than the second color.
            computeColorPalette4(color0, color1, this.palette);

            dxtBlock.color0         = color0;
            dxtBlock.color1         = color1;
            dxtBlock.colorIndexMask = computePaletteIndices4(colorBlock, this.palette);
        }