From 70267fda150a0ba05d42ad4e6d9890583bf059c9 Mon Sep 17 00:00:00 2001 From: castano Date: Thu, 15 May 2008 06:18:24 +0000 Subject: [PATCH] Add support for input floating point images. Patch provided by Jim Tilander. See issue 27. --- src/nvimage/FloatImage.cpp | 17 +++++++++++ src/nvimage/FloatImage.h | 3 ++ src/nvtt/Compressor.cpp | 15 +++++++--- src/nvtt/InputOptions.cpp | 60 ++++++++++++++++++++++++++++++++++---- src/nvtt/InputOptions.h | 6 +++- src/nvtt/nvtt.h | 3 +- 6 files changed, 91 insertions(+), 13 deletions(-) diff --git a/src/nvimage/FloatImage.cpp b/src/nvimage/FloatImage.cpp index 9f42a56..93de5aa 100644 --- a/src/nvimage/FloatImage.cpp +++ b/src/nvimage/FloatImage.cpp @@ -862,3 +862,20 @@ void FloatImage::applyKernelHorizontal(const PolyphaseKernel & k, int y, int c, } } +FloatImage* FloatImage::clone() const +{ + FloatImage* copy = new FloatImage(); + copy->m_width = m_width; + copy->m_height = m_height; + copy->m_componentNum = m_componentNum; + copy->m_count = m_count; + + if(m_mem) + { + copy->allocate(m_componentNum, m_width, m_height); + memcpy(copy->m_mem, m_mem, m_count * sizeof(float)); + } + + return copy; +} + diff --git a/src/nvimage/FloatImage.h b/src/nvimage/FloatImage.h index 494c22f..9ef6f55 100644 --- a/src/nvimage/FloatImage.h +++ b/src/nvimage/FloatImage.h @@ -114,6 +114,9 @@ public: float sampleLinearMirror(float x, float y, int c) const; //@} + + FloatImage* clone() const; + public: uint index(uint x, uint y) const; diff --git a/src/nvtt/Compressor.cpp b/src/nvtt/Compressor.cpp index 6d4be87..9e1db44 100644 --- a/src/nvtt/Compressor.cpp +++ b/src/nvtt/Compressor.cpp @@ -115,6 +115,11 @@ namespace nvtt m_inputImage = inputOptions.image(idx); m_fixedImage = NULL; m_floatImage = NULL; + + if (const FloatImage * floatImage = inputOptions.floatImage(idx)) + { + m_floatImage = floatImage->clone(); + } } // Assign and take ownership of given image. @@ -203,7 +208,7 @@ namespace nvtt AutoPtr m_floatImage; }; -} +} // nvtt namespace Compressor::Compressor() : m(*new Compressor::Private()) @@ -502,7 +507,7 @@ int Compressor::Private::findExactMipmap(const InputOptions::Private & inputOpti if (inputImage.width == int(w) && inputImage.height == int(h) && inputImage.depth == int(d)) { - if (inputImage.data != NULL) + if (inputImage.hasValidData()) { return idx; } @@ -526,7 +531,7 @@ int Compressor::Private::findClosestMipmap(const InputOptions::Private & inputOp int idx = f * inputOptions.mipmapCount + m; const InputOptions::Private::InputImage & inputImage = inputOptions.images[idx]; - if (inputImage.data != NULL) + if (inputImage.hasValidData()) { int difference = (inputImage.width - w) + (inputImage.height - h) + (inputImage.depth - d); @@ -647,7 +652,9 @@ void Compressor::Private::processInputImage(Mipmap & mipmap, const InputOptions: } else { - if (inputOptions.inputGamma != inputOptions.outputGamma) + if (inputOptions.inputGamma != inputOptions.outputGamma || + inputOptions.colorTransform == ColorTransform_Linear || + inputOptions.colorTransform == ColorTransform_Swizzle) { mipmap.toFloatImage(inputOptions); } diff --git a/src/nvtt/InputOptions.cpp b/src/nvtt/InputOptions.cpp index b537e42..1dbd0e8 100644 --- a/src/nvtt/InputOptions.cpp +++ b/src/nvtt/InputOptions.cpp @@ -165,7 +165,8 @@ void InputOptions::setTextureLayout(TextureType type, int width, int height, int img.mipLevel = mipLevel; img.face = f; - img.data = NULL; + img.uint8data = NULL; + img.floatdata = NULL; w = max(1U, w / 2); h = max(1U, h / 2); @@ -203,9 +204,47 @@ bool InputOptions::setMipmapData(const void * data, int width, int height, int d return false; } - m.images[idx].data = new nv::Image(); - m.images[idx].data->allocate(width, height); - memcpy(m.images[idx].data->pixels(), data, width * height * 4); + switch(m.inputFormat) + { + case InputFormat_BGRA_8UB: + if (Image * image = new nv::Image()) + { + image->allocate(width, height); + memcpy(image->pixels(), data, width * height * 4); + m.images[idx].data = image; + } + else + { + // @@ Out of memory error. + return false; + } + break; + case InputFormat_RGBA_32F: + if (FloatImage * image = new nv::FloatImage()) + { + const float * floatData = (const float *)data; + image->allocate(4, width, height); + + for (int c = 0; c < 4; c++) + { + float * channel = image->channel(c); + for (int i = 0; i < width * height; i++) + { + channel[i] = floatData[i*4 + c]; + } + } + + m.images[idx].data = image; + } + else + { + // @@ Out of memory error. + return false; + } + break; + default: + return false; + } return true; } @@ -416,7 +455,7 @@ const Image * InputOptions::Private::image(uint face, uint mipmap) const nvDebugCheck(image.face == face); nvDebugCheck(image.mipLevel == mipmap); - return image.data.ptr(); + return image.uint8data.ptr(); } const Image * InputOptions::Private::image(uint idx) const @@ -425,5 +464,14 @@ const Image * InputOptions::Private::image(uint idx) const const InputImage & image = this->images[idx]; - return image.data.ptr(); + return image.uint8data.ptr(); +} + +const FloatImage * InputOptions::Private::image(uint idx) const +{ + nvDebugCheck(idx < faceCount * mipmapCount); + + const InputImage & image = this->images[idx]; + + return image.floatdata.ptr(); } diff --git a/src/nvtt/InputOptions.h b/src/nvtt/InputOptions.h index a4e487c..475472e 100644 --- a/src/nvtt/InputOptions.h +++ b/src/nvtt/InputOptions.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "nvtt.h" namespace nvtt @@ -94,6 +95,8 @@ namespace nvtt const nv::Image * image(uint face, uint mipmap) const; const nv::Image * image(uint idx) const; + const nv::FloatImage * floatImage(uint idx) const; + }; // Internal image structure. @@ -108,7 +111,8 @@ namespace nvtt int height; int depth; - nv::AutoPtr data; + nv::AutoPtr uint8data; + nv::AutoPtr floatdata; }; } // nvtt namespace diff --git a/src/nvtt/nvtt.h b/src/nvtt/nvtt.h index be4e72e..bf3633c 100644 --- a/src/nvtt/nvtt.h +++ b/src/nvtt/nvtt.h @@ -140,8 +140,7 @@ namespace nvtt enum InputFormat { InputFormat_BGRA_8UB, - // InputFormat_RGBE_8UB, - // InputFormat_BGRA_32F, + InputFormat_RGBA_32F, }; /// Mipmap downsampling filters.