diff --git a/src/nvimage/FloatImage.h b/src/nvimage/FloatImage.h index 06a1e61..efd85bd 100644 --- a/src/nvimage/FloatImage.h +++ b/src/nvimage/FloatImage.h @@ -88,10 +88,10 @@ namespace nv NVIMAGE_API void applyKernelVertical(const PolyphaseKernel & k, int x, uint c, uint a, WrapMode wm, float * output) const; NVIMAGE_API void applyKernelHorizontal(const PolyphaseKernel & k, int y, uint c, uint a, WrapMode wm, float * output) const; - NVIMAGE_API void flip(); - - NVIMAGE_API float alphaTestCoverage(float alphaRef, int alphaChannel) const; - NVIMAGE_API void scaleAlphaToCoverage(float coverage, float alphaRef, int alphaChannel); + NVIMAGE_API void flip(); + + NVIMAGE_API float alphaTestCoverage(float alphaRef, int alphaChannel) const; + NVIMAGE_API void scaleAlphaToCoverage(float coverage, float alphaRef, int alphaChannel); uint width() const { return m_width; } diff --git a/src/nvtt/CompressorRGB.cpp b/src/nvtt/CompressorRGB.cpp index f6f708c..e1d75d6 100644 --- a/src/nvtt/CompressorRGB.cpp +++ b/src/nvtt/CompressorRGB.cpp @@ -145,80 +145,87 @@ void PixelFormatConverter::compress(nvtt::InputFormat inputFormat, nvtt::AlphaMo const uint * src = (const uint *)data + y * srcPitch; const float * fsrc = (const float *)data + y * srcPitch; - uint8 * ptr = dst; - - for (uint x = 0; x < w; x++) - { - float r, g, b, a; - - 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; - a = float(c.a) / 255.0f; - } - else { - nvDebugCheck (inputFormat == nvtt::InputFormat_RGBA_32F); - - //r = ((float *)src)[4 * x + 0]; // Color components not interleaved. - //g = ((float *)src)[4 * x + 1]; - //b = ((float *)src)[4 * x + 2]; - //a = ((float *)src)[4 * x + 3]; - r = fsrc[x + 0 * srcPlane]; - g = fsrc[x + 1 * srcPlane]; - b = fsrc[x + 2 * srcPlane]; - a = fsrc[x + 3 * srcPlane]; - } - - if (compressionOptions.pixelType == nvtt::PixelType_Float) - { - if (rsize == 32) *((float *)ptr) = r; - else if (rsize == 16) *((uint16 *)ptr) = to_half(r); - ptr += rsize / 8; - - if (gsize == 32) *((float *)ptr) = g; - else if (gsize == 16) *((uint16 *)ptr) = to_half(g); - ptr += gsize / 8; - - if (bsize == 32) *((float *)ptr) = b; - else if (bsize == 16) *((uint16 *)ptr) = to_half(b); - ptr += bsize / 8; - - if (asize == 32) *((float *)ptr) = a; - else if (asize == 16) *((uint16 *)ptr) = to_half(a); - ptr += asize / 8; - } - else - { - Color32 c; - if (compressionOptions.pixelType == nvtt::PixelType_UnsignedNorm) { - c.r = uint8(clamp(r * 255, 0.0f, 255.0f)); - c.g = uint8(clamp(g * 255, 0.0f, 255.0f)); - c.b = uint8(clamp(b * 255, 0.0f, 255.0f)); - c.a = uint8(clamp(a * 255, 0.0f, 255.0f)); - } - // @@ Add support for nvtt::PixelType_SignedInt, nvtt::PixelType_SignedNorm, nvtt::PixelType_UnsignedInt - - uint p = 0; - p |= PixelFormat::convert(c.r, 8, rsize) << rshift; - p |= PixelFormat::convert(c.g, 8, gsize) << gshift; - p |= PixelFormat::convert(c.b, 8, bsize) << bshift; - p |= PixelFormat::convert(c.a, 8, asize) << ashift; - - // Output one byte at a time. - for (uint i = 0; i < byteCount; i++) - { - *(dst + x * byteCount + i) = (p >> (i * 8)) & 0xFF; - } - } + if (inputFormat == nvtt::InputFormat_BGRA_8UB && compressionOptions.pixelType == nvtt::PixelType_UnsignedNorm && bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0xFF000000) + { + convert_to_a8r8g8b8(src, dst, w); } + else + { + uint8 * ptr = dst; - // Zero padding. - for (uint x = w * byteCount; x < pitch; x++) - { - *(dst + x) = 0; - } + for (uint x = 0; x < w; x++) + { + float r, g, b, a; + + 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; + a = float(c.a) / 255.0f; + } + else { + nvDebugCheck (inputFormat == nvtt::InputFormat_RGBA_32F); + + //r = ((float *)src)[4 * x + 0]; // Color components not interleaved. + //g = ((float *)src)[4 * x + 1]; + //b = ((float *)src)[4 * x + 2]; + //a = ((float *)src)[4 * x + 3]; + r = fsrc[x + 0 * srcPlane]; + g = fsrc[x + 1 * srcPlane]; + b = fsrc[x + 2 * srcPlane]; + a = fsrc[x + 3 * srcPlane]; + } + + if (compressionOptions.pixelType == nvtt::PixelType_Float) + { + if (rsize == 32) *((float *)ptr) = r; + else if (rsize == 16) *((uint16 *)ptr) = to_half(r); + ptr += rsize / 8; + + if (gsize == 32) *((float *)ptr) = g; + else if (gsize == 16) *((uint16 *)ptr) = to_half(g); + ptr += gsize / 8; + + if (bsize == 32) *((float *)ptr) = b; + else if (bsize == 16) *((uint16 *)ptr) = to_half(b); + ptr += bsize / 8; + + if (asize == 32) *((float *)ptr) = a; + else if (asize == 16) *((uint16 *)ptr) = to_half(a); + ptr += asize / 8; + } + else + { + Color32 c; + if (compressionOptions.pixelType == nvtt::PixelType_UnsignedNorm) { + c.r = uint8(clamp(r * 255, 0.0f, 255.0f)); + c.g = uint8(clamp(g * 255, 0.0f, 255.0f)); + c.b = uint8(clamp(b * 255, 0.0f, 255.0f)); + c.a = uint8(clamp(a * 255, 0.0f, 255.0f)); + } + // @@ Add support for nvtt::PixelType_SignedInt, nvtt::PixelType_SignedNorm, nvtt::PixelType_UnsignedInt + + uint p = 0; + p |= PixelFormat::convert(c.r, 8, rsize) << rshift; + p |= PixelFormat::convert(c.g, 8, gsize) << gshift; + p |= PixelFormat::convert(c.b, 8, bsize) << bshift; + p |= PixelFormat::convert(c.a, 8, asize) << ashift; + + // Output one byte at a time. + for (uint i = 0; i < byteCount; i++) + { + *(dst + x * byteCount + i) = (p >> (i * 8)) & 0xFF; + } + } + } + + // Zero padding. + for (uint x = w * byteCount; x < pitch; x++) + { + *(dst + x) = 0; + } + } if (outputOptions.outputHandler != NULL) { diff --git a/src/nvtt/Context.cpp b/src/nvtt/Context.cpp index f0a1773..6d2e230 100644 --- a/src/nvtt/Context.cpp +++ b/src/nvtt/Context.cpp @@ -42,6 +42,7 @@ #include "InputOptions.h" #include "CompressionOptions.h" #include "OutputOptions.h" +#include "TexImage.h" #include "CompressorDXT.h" #include "CompressorRGB.h" @@ -334,10 +335,16 @@ bool Compressor::outputHeader(const TexImage & tex, int mipmapCount, const Compr bool Compressor::compress(const TexImage & tex, const CompressionOptions & compressionOptions, const OutputOptions & outputOptions) const { -#pragma message(NV_FILE_LINE "TODO: Implement TexImage compress api") + // @@ Decide whether to change the swizzling of FloatImage. - // @@ Convert to fixed point and call compress2D for each face. - return false; + foreach(i, tex.m->imageArray) { + FloatImage * image = tex.m->imageArray[i]; + if (!m.compress2D(InputFormat_RGBA_32F, tex.m->alphaMode, image->width(), image->height(), image->channel(0), compressionOptions.m, outputOptions.m)) { + return false; + } + } + + return true; } /// Estimate the size of compressing the given texture. diff --git a/src/nvtt/TexImage.cpp b/src/nvtt/TexImage.cpp index 8d51071..2d88c5b 100644 --- a/src/nvtt/TexImage.cpp +++ b/src/nvtt/TexImage.cpp @@ -246,7 +246,7 @@ int TexImage::countMipmaps() const float TexImage::alphaTestCoverage(float alphaRef/*= 0.5*/) const { - int imageCount = 0.0f; + int imageCount = 0; float coverage = 0.0f; foreach (i, m->imageArray) diff --git a/src/nvtt/TexImage.h b/src/nvtt/TexImage.h index d35aba1..b78c9a3 100644 --- a/src/nvtt/TexImage.h +++ b/src/nvtt/TexImage.h @@ -26,53 +26,55 @@ #include "nvtt.h" -#include -#include -#include +#include "nvcore/Array.h" +#include "nvcore/RefCounted.h" +#include "nvcore/Ptr.h" -#include -#include +#include "nvimage/Image.h" +#include "nvimage/FloatImage.h" namespace nvtt { - struct TexImage::Private : public nv::RefCounted - { - Private() - { - type = TextureType_2D; - wrapMode = WrapMode_Mirror; - alphaMode = AlphaMode_None; - isNormalMap = false; + struct TexImage::Private : public nv::RefCounted + { + void operator=(const Private &); + public: + Private() + { + type = TextureType_2D; + wrapMode = WrapMode_Mirror; + alphaMode = AlphaMode_None; + isNormalMap = false; - imageArray.resize(1, NULL); - } - Private(const Private & p) // Copy ctor. inits refcount to 0. - { - type = p.type; - wrapMode = p.wrapMode; - alphaMode = p.alphaMode; - isNormalMap = p.isNormalMap; + imageArray.resize(1, NULL); + } + Private(const Private & p) // Copy ctor. inits refcount to 0. + { + type = p.type; + wrapMode = p.wrapMode; + alphaMode = p.alphaMode; + isNormalMap = p.isNormalMap; - imageArray = p.imageArray; - } - ~Private() - { - const uint count = imageArray.count(); - for (uint i = 0; i < count; i++) { - delete imageArray[i]; - } - } + imageArray = p.imageArray; + } + ~Private() + { + const uint count = imageArray.count(); + for (uint i = 0; i < count; i++) { + delete imageArray[i]; + } + } - TextureType type; - WrapMode wrapMode; - AlphaMode alphaMode; - bool isNormalMap; + TextureType type; + WrapMode wrapMode; + AlphaMode alphaMode; + bool isNormalMap; + + nv::Array imageArray; + }; - nv::Array imageArray; - }; - } // nvtt namespace diff --git a/src/nvtt/cuda/CudaCompressorDXT.cpp b/src/nvtt/cuda/CudaCompressorDXT.cpp index b330244..8488d56 100644 --- a/src/nvtt/cuda/CudaCompressorDXT.cpp +++ b/src/nvtt/cuda/CudaCompressorDXT.cpp @@ -25,7 +25,6 @@ #include "CudaUtils.h" #include -#include #include #include #include @@ -35,6 +34,7 @@ #include #include + #if defined HAVE_CUDA #include #endif diff --git a/src/nvtt/nvtt.h b/src/nvtt/nvtt.h index 911858f..880004c 100644 --- a/src/nvtt/nvtt.h +++ b/src/nvtt/nvtt.h @@ -456,6 +456,7 @@ namespace nvtt private: void detach(); + friend struct Compressor; struct Private; Private * m; }; diff --git a/src/nvtt/tests/imperativeapi.cpp b/src/nvtt/tests/imperativeapi.cpp index 4a50c74..35b39b1 100644 --- a/src/nvtt/tests/imperativeapi.cpp +++ b/src/nvtt/tests/imperativeapi.cpp @@ -23,11 +23,13 @@ #include -#include +#include // EXIT_SUCCESS, EXIT_FAILURE int main(int argc, char *argv[]) { + if (argc != 2) return EXIT_FAILURE; + nvtt::CompressionOptions compressionOptions; compressionOptions.setFormat(nvtt::Format_BC1); @@ -37,20 +39,23 @@ int main(int argc, char *argv[]) nvtt::Context context; nvtt::TexImage image = context.createTexImage(); - image.load("kodim01.png"); + image.load(argv[1]); context.outputHeader(image, image.countMipmaps(), compressionOptions, outputOptions); - float gamma = 2.2; + float gamma = 2.2f; image.toLinear(gamma); + float coverage = image.alphaTestCoverage(); + while (image.buildNextMipmap(nvtt::MipmapFilter_Box)) { nvtt::TexImage tmpImage = image; tmpImage.toGamma(gamma); + tmpImage.scaleAlphaToCoverage(coverage); + context.compress(tmpImage, compressionOptions, outputOptions); - // tmpImage.compress(compressionOptions, outputOptions); } return EXIT_SUCCESS; diff --git a/src/nvtt/tools/compress.cpp b/src/nvtt/tools/compress.cpp index 9b5b8b3..e77cd99 100644 --- a/src/nvtt/tools/compress.cpp +++ b/src/nvtt/tools/compress.cpp @@ -406,7 +406,7 @@ int main(int argc, char *argv[]) for (uint i = 0; i < image->componentNum(); i++) { - inputOptions.setMipmapChannelData(image->channel(i), i, image->width(), image->height()); + inputOptions.setMipmapChannelData(image->channel(i), i, image->width(), image->height()); } } else diff --git a/src/nvtt/tools/resize.cpp b/src/nvtt/tools/resize.cpp index 40b5781..d72fbdb 100644 --- a/src/nvtt/tools/resize.cpp +++ b/src/nvtt/tools/resize.cpp @@ -168,7 +168,8 @@ int main(int argc, char *argv[]) nv::FloatImage fimage(&image); fimage.toLinear(0, 3, gamma); - + +#if 0 nv::AutoPtr fresult(fimage.resize(*filter, uint(image.width() * scale), uint(image.height() * scale), wrapMode)); nv::AutoPtr result(fresult->createImageGammaCorrect(gamma)); @@ -176,7 +177,7 @@ int main(int argc, char *argv[]) nv::StdOutputStream stream(output); nv::ImageIO::save(output, stream, result.ptr()); - +#endif return 0; }