Add support for RGBE.

This commit is contained in:
castano 2010-03-17 07:55:03 +00:00
parent e848645e88
commit f436a71479
4 changed files with 171 additions and 12 deletions

102
src/nvtt/CompressorRGBE.cpp Normal file
View File

@ -0,0 +1,102 @@
// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
//
// 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 <nvimage/Image.h>
#include <nvimage/FloatImage.h>
#include <nvmath/Color.h>
#include <nvcore/Debug.h>
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);
}

40
src/nvtt/CompressorRGBE.h Normal file
View File

@ -0,0 +1,40 @@
// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
//
// 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

View File

@ -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;
}

View File

@ -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,
};