From f436a714798ad043033ca47b30dc8db10dd46d2f Mon Sep 17 00:00:00 2001 From: castano Date: Wed, 17 Mar 2010 07:55:03 +0000 Subject: [PATCH] Add support for RGBE. --- src/nvtt/CompressorRGBE.cpp | 102 ++++++++++++++++++++++++++++++++++++ src/nvtt/CompressorRGBE.h | 40 ++++++++++++++ src/nvtt/Context.cpp | 23 ++++++-- src/nvtt/nvtt.h | 18 ++++--- 4 files changed, 171 insertions(+), 12 deletions(-) create mode 100644 src/nvtt/CompressorRGBE.cpp create mode 100644 src/nvtt/CompressorRGBE.h diff --git a/src/nvtt/CompressorRGBE.cpp b/src/nvtt/CompressorRGBE.cpp new file mode 100644 index 0000000..87c2b42 --- /dev/null +++ b/src/nvtt/CompressorRGBE.cpp @@ -0,0 +1,102 @@ +// Copyright NVIDIA Corporation 2007 -- Ignacio Castano +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#include "CompressorRGBE.h" +#include "CompressionOptions.h" +#include "OutputOptions.h" + +#include +#include + +#include + +#include + +using namespace nv; +using namespace nvtt; + +static Color32 toRgbe8(float r, float g, float b) +{ + Color32 c; + float v = max(max(r, g), b); + if (v < 1e-32) { + c.r = c.g = c.b = c.a = 0; + } + else { + int e; + v = frexp(v, &e) * 256.0f / v; + c.r = uint8(clamp(r * v, 0.0f, 255.0f)); + c.g = uint8(clamp(g * v, 0.0f, 255.0f)); + c.b = uint8(clamp(b * v, 0.0f, 255.0f)); + c.a = e + 128; + } + + return c; +} + + +void CompressorRGBE::compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, const void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions) +{ + nvDebugCheck (compressionOptions.format == nvtt::Format_RGBE); + + uint srcPitch = w; + uint srcPlane = w * h; + + // Allocate output scanline. + Color32 * dst = (Color32 *)mem::malloc(w); + + for (uint y = 0; y < h; y++) + { + const uint * src = (const uint *)data + y * srcPitch; + const float * fsrc = (const float *)data + y * srcPitch; + + for (uint x = 0; x < w; x++) + { + float r, g, b; + + if (inputFormat == nvtt::InputFormat_BGRA_8UB) { + Color32 c = Color32(src[x]); + r = float(c.r) / 255.0f; + g = float(c.g) / 255.0f; + b = float(c.b) / 255.0f; + } + else { + nvDebugCheck (inputFormat == nvtt::InputFormat_RGBA_32F); + + // Color components not interleaved. + r = fsrc[x + 0 * srcPlane]; + g = fsrc[x + 1 * srcPlane]; + b = fsrc[x + 2 * srcPlane]; + } + + dst[x] = toRgbe8(r, g, b); + } + + if (outputOptions.outputHandler != NULL) + { + outputOptions.outputHandler->writeData(dst, w * 4); + } + } + + mem::free(dst); +} diff --git a/src/nvtt/CompressorRGBE.h b/src/nvtt/CompressorRGBE.h new file mode 100644 index 0000000..97fd3eb --- /dev/null +++ b/src/nvtt/CompressorRGBE.h @@ -0,0 +1,40 @@ +// Copyright NVIDIA Corporation 2007 -- Ignacio Castano +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#ifndef NV_TT_COMPRESSORRGBE_H +#define NV_TT_COMPRESSORRGBE_H + +#include "nvtt.h" +#include "Compressor.h" + +namespace nv +{ + struct CompressorRGBE : public CompressorInterface + { + virtual void compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, const void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions); + }; + +} // nv namespace + + +#endif // NV_TT_COMPRESSORRGBE_H diff --git a/src/nvtt/Context.cpp b/src/nvtt/Context.cpp index 3be3b41..deaff3a 100644 --- a/src/nvtt/Context.cpp +++ b/src/nvtt/Context.cpp @@ -45,6 +45,7 @@ #include "CompressorDXT.h" #include "CompressorRGB.h" +#include "CompressorRGBE.h" #include "cuda/CudaUtils.h" #include "cuda/CudaCompressorDXT.h" @@ -905,6 +906,13 @@ bool Compressor::Private::compressMipmaps(uint f, const InputOptions::Private & mipmap.toFloatImage(inputOptions); // @@ Convert to linear space. + + // @@ HACK + const FloatImage * image = mipmap.asFloatImage(); + nvDebugCheck(image != NULL); + + // @@ Ignore return value? + compress2D(InputFormat_RGBA_32F, inputOptions.alphaMode, image->width(), image->height(), image->channel(0), compressionOptions, outputOptions); } else { @@ -952,13 +960,16 @@ bool Compressor::Private::compressMipmaps(uint f, const InputOptions::Private & }*/ quantizeMipmap(mipmap, compressionOptions); + + const Image * image = mipmap.asFixedImage(); + nvDebugCheck(image != NULL); + + // @@ Ignore return value? + compress2D(InputFormat_BGRA_8UB, inputOptions.alphaMode, image->width(), image->height(), image->pixels(), compressionOptions, outputOptions); } - const Image * image = mipmap.asFixedImage(); - nvDebugCheck(image != NULL); - // @@ Ignore return value? - compress2D(InputFormat_BGRA_8UB, inputOptions.alphaMode, image->width(), image->height(), image->pixels(), compressionOptions, outputOptions); + // Compute extents of next mipmap: w = max(1U, w / 2); @@ -1406,6 +1417,10 @@ CompressorInterface * Compressor::Private::chooseCpuCompressor(const Compression { // Not supported. } + else if (compressionOptions.format == Format_RGBE) + { + return new CompressorRGBE; + } return NULL; } diff --git a/src/nvtt/nvtt.h b/src/nvtt/nvtt.h index ed3f98b..2c450aa 100644 --- a/src/nvtt/nvtt.h +++ b/src/nvtt/nvtt.h @@ -91,21 +91,23 @@ namespace nvtt Format_BC4, // ATI1 Format_BC5, // 3DC, ATI2 - Format_DXT1n, - Format_CTX1, - Format_YCoCg_DXT5, + Format_DXT1n,// Not supported on CPU yet. + Format_CTX1, // Not supported on CPU yet. + Format_YCoCg_DXT5, // Not supported yet. - Format_BC6, - Format_BC7, + Format_BC6, // Not supported yet. + Format_BC7, // Not supported yet. + + Format_RGBE, }; /// Pixel types. enum PixelType { PixelType_UnsignedNorm, - PixelType_SignedNorm, - PixelType_UnsignedInt, - PixelType_SignedInt, + PixelType_SignedNorm, // Not supported yet. + PixelType_UnsignedInt, // Not supported yet. + PixelType_SignedInt, // Not supported yet. PixelType_Float, };