From 1308795378c2c24a4020fe51e21d245c54b5ba7f Mon Sep 17 00:00:00 2001 From: castano Date: Tue, 17 Jul 2007 10:00:48 +0000 Subject: [PATCH] RGB conversion fixes. --- src/nvimage/nvtt/CompressRGB.cpp | 11 ++++------- src/nvimage/nvtt/CompressionOptions.cpp | 9 +++++++++ src/nvimage/nvtt/dxtlib.cpp | 22 ++++++++++++++++------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/nvimage/nvtt/CompressRGB.cpp b/src/nvimage/nvtt/CompressRGB.cpp index b01a1d0..d9afd49 100644 --- a/src/nvimage/nvtt/CompressRGB.cpp +++ b/src/nvimage/nvtt/CompressRGB.cpp @@ -72,13 +72,13 @@ namespace { *shift = 0; while((mask & 1) == 0) { - *shift++; + ++(*shift); mask >>= 1; } *size = 0; while((mask & 1) == 1) { - *size++; + ++(*size); mask >>= 1; } } @@ -120,7 +120,7 @@ void nv::compressRGB(const Image * image, const OutputOptions & outputOptions, c // Determine pitch. uint pitch = computePitch(w, compressionOptions.bitcount); - void * dst = mem::malloc(pitch); + uint8 * dst = (uint8 *)mem::malloc(pitch + 4); for (uint y = 0; y < h; y++) { @@ -145,10 +145,7 @@ void nv::compressRGB(const Image * image, const OutputOptions & outputOptions, c c |= convert(src[x].b, 8, bsize) << bshift; c |= convert(src[x].a, 8, asize) << ashift; - *(uint *)dst = c; - - ++src; - dst = (uint8 *)dst + byteCount; + *(uint *)(dst + x * byteCount) = c; } } diff --git a/src/nvimage/nvtt/CompressionOptions.cpp b/src/nvimage/nvtt/CompressionOptions.cpp index 22c9047..0cf2113 100644 --- a/src/nvimage/nvtt/CompressionOptions.cpp +++ b/src/nvimage/nvtt/CompressionOptions.cpp @@ -108,6 +108,15 @@ void CompressionOptions::setPixelFormat(uint bitcount, uint rmask, uint gmask, u nvCheck((gmask & amask) == 0); nvCheck((bmask & amask) == 0); + if (bitcount != 32) + { + uint maxMask = (1 << bitcount); + nvCheck(maxMask > rmask); + nvCheck(maxMask > gmask); + nvCheck(maxMask > bmask); + nvCheck(maxMask > amask); + } + m.bitcount = bitcount; m.rmask = rmask; m.gmask = gmask; diff --git a/src/nvimage/nvtt/dxtlib.cpp b/src/nvimage/nvtt/dxtlib.cpp index 1b87f77..43dab2e 100644 --- a/src/nvimage/nvtt/dxtlib.cpp +++ b/src/nvimage/nvtt/dxtlib.cpp @@ -67,11 +67,19 @@ namespace } return 0; } - - static int computeImageSize(int w, int h, Format format) + + inline uint computePitch(uint w, uint bitsize) + { + uint p = w * ((bitsize + 7) / 8); + + // Align to 32 bits. + return ((p + 3) / 4) * 4; + } + + static int computeImageSize(uint w, uint h, uint bitCount, Format format) { if (format == Format_RGBA) { - return w * h * sizeof(Color32); + return h * computePitch(w, bitCount); } else { return ((w + 3) / 4) * ((h + 3) / 4) * blockSize(format); @@ -124,7 +132,7 @@ static void outputHeader(const InputOptions::Private & inputOptions, const Outpu } else { - header.setLinearSize(computeImageSize(img->width, img->height, compressionOptions.format)); + header.setLinearSize(computeImageSize(img->width, img->height, compressionOptions.bitcount, compressionOptions.format)); if (compressionOptions.format == Format_DXT1 /*|| compressionOptions.format == Format_DXT1a*/) { header.setFourCC('D', 'X', 'T', '1'); @@ -376,6 +384,7 @@ bool nvtt::compress(const InputOptions & inputOptions, const OutputOptions & out outputHeader(inputOptions.m, outputOptions, compressionOptions.m); Format format = compressionOptions.m.format; + const uint bitCount = compressionOptions.m.bitcount; for (int f = 0; f < inputOptions.m.faceCount; f++) { @@ -389,7 +398,7 @@ bool nvtt::compress(const InputOptions & inputOptions, const OutputOptions & out if (outputOptions.outputHandler) { - int size = computeImageSize(mipmap.width, mipmap.height, format); + int size = computeImageSize(mipmap.width, mipmap.height, bitCount, format); outputOptions.outputHandler->mipmap(size, mipmap.width, mipmap.height, mipmap.depth, mipmap.face, mipmap.mipLevel); } @@ -460,6 +469,7 @@ bool nvtt::compress(const InputOptions & inputOptions, const OutputOptions & out int nvtt::estimateSize(const InputOptions & inputOptions, const CompressionOptions & compressionOptions) { Format format = compressionOptions.m.format; + const uint bitCount = compressionOptions.m.bitcount; int size = 0; @@ -470,7 +480,7 @@ int nvtt::estimateSize(const InputOptions & inputOptions, const CompressionOptio int idx = f * inputOptions.m.mipmapCount + m; const InputOptions::Private::Image & img = inputOptions.m.images[idx]; - size += computeImageSize(img.width, img.height, format); + size += computeImageSize(img.width, img.height, bitCount, format); if (!inputOptions.m.generateMipmaps || (inputOptions.m.maxLevel >= 0 && m >= inputOptions.m.maxLevel)) { // continue with next face.