From e2ac16f1004e76e793eb73b2a3d75fcaef359a7d Mon Sep 17 00:00:00 2001 From: castano Date: Tue, 7 Sep 2010 18:07:53 +0000 Subject: [PATCH] Align rows to 8 bits only. --- src/nvtt/CompressRGB.cpp | 182 +++++++++++++++++++-------------------- 1 file changed, 90 insertions(+), 92 deletions(-) diff --git a/src/nvtt/CompressRGB.cpp b/src/nvtt/CompressRGB.cpp index 35239c4..bc2f594 100644 --- a/src/nvtt/CompressRGB.cpp +++ b/src/nvtt/CompressRGB.cpp @@ -37,23 +37,21 @@ using namespace nvtt; namespace { - inline uint computePitch(uint w, uint bitsize) - { - uint p = w * ((bitsize + 7) / 8); - - // Align to 32 bits. - return ((p + 3) / 4) * 4; - } - - inline void convert_to_a8r8g8b8(const void * src, void * dst, uint w) - { - memcpy(dst, src, 4 * w); - } - - inline void convert_to_x8r8g8b8(const void * src, void * dst, uint w) - { - memcpy(dst, src, 4 * w); - } + inline uint computePitch(uint w, uint bitsize) + { + // Align to 8 bits. + return w * ((bitsize + 7) / 8); + } + + inline void convert_to_a8r8g8b8(const void * src, void * dst, uint w) + { + memcpy(dst, src, 4 * w); + } + + inline void convert_to_x8r8g8b8(const void * src, void * dst, uint w) + { + memcpy(dst, src, 4 * w); + } } // namespace @@ -61,80 +59,80 @@ namespace // Pixel format converter. void nv::compressRGB(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions) { - nvCheck(image != NULL); - - const uint w = image->width(); - const uint h = image->height(); - - const uint bitCount = compressionOptions.bitcount; - nvCheck(bitCount == 8 || bitCount == 16 || bitCount == 24 || bitCount == 32); - - const uint byteCount = bitCount / 8; - - const uint rmask = compressionOptions.rmask; - uint rshift, rsize; - PixelFormat::maskShiftAndSize(rmask, &rshift, &rsize); - - const uint gmask = compressionOptions.gmask; - uint gshift, gsize; - PixelFormat::maskShiftAndSize(gmask, &gshift, &gsize); - - const uint bmask = compressionOptions.bmask; - uint bshift, bsize; - PixelFormat::maskShiftAndSize(bmask, &bshift, &bsize); - - const uint amask = compressionOptions.amask; - uint ashift, asize; - PixelFormat::maskShiftAndSize(amask, &ashift, &asize); - - // Determine pitch. - uint pitch = computePitch(w, compressionOptions.bitcount); - - uint8 * dst = (uint8 *)mem::malloc(pitch + 4); - - for (uint y = 0; y < h; y++) - { - const Color32 * src = image->scanline(y); - - if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0xFF000000) - { - convert_to_a8r8g8b8(src, dst, w); - } - else if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0) - { - convert_to_x8r8g8b8(src, dst, w); - } - else - { - // Generic pixel format conversion. - for (uint x = 0; x < w; x++) - { - uint c = 0; - c |= PixelFormat::convert(src[x].r, 8, rsize) << rshift; - c |= PixelFormat::convert(src[x].g, 8, gsize) << gshift; - c |= PixelFormat::convert(src[x].b, 8, bsize) << bshift; - c |= PixelFormat::convert(src[x].a, 8, asize) << ashift; - - // Output one byte at a time. - for (uint i = 0; i < byteCount; i++) - { - *(dst + x * byteCount + i) = (c >> (i * 8)) & 0xFF; - } - } - - // Zero padding. - for (uint x = w * byteCount; x < pitch; x++) - { - *(dst + x) = 0; - } - } - - if (outputOptions.outputHandler != NULL) - { - outputOptions.outputHandler->writeData(dst, pitch); - } - } - - mem::free(dst); + nvCheck(image != NULL); + + const uint w = image->width(); + const uint h = image->height(); + + const uint bitCount = compressionOptions.bitcount; + nvCheck(bitCount == 8 || bitCount == 16 || bitCount == 24 || bitCount == 32); + + const uint byteCount = bitCount / 8; + + const uint rmask = compressionOptions.rmask; + uint rshift, rsize; + PixelFormat::maskShiftAndSize(rmask, &rshift, &rsize); + + const uint gmask = compressionOptions.gmask; + uint gshift, gsize; + PixelFormat::maskShiftAndSize(gmask, &gshift, &gsize); + + const uint bmask = compressionOptions.bmask; + uint bshift, bsize; + PixelFormat::maskShiftAndSize(bmask, &bshift, &bsize); + + const uint amask = compressionOptions.amask; + uint ashift, asize; + PixelFormat::maskShiftAndSize(amask, &ashift, &asize); + + // Determine pitch. + uint pitch = computePitch(w, compressionOptions.bitcount); + + uint8 * dst = (uint8 *)mem::malloc(pitch + 4); + + for (uint y = 0; y < h; y++) + { + const Color32 * src = image->scanline(y); + + if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0xFF000000) + { + convert_to_a8r8g8b8(src, dst, w); + } + else if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0) + { + convert_to_x8r8g8b8(src, dst, w); + } + else + { + // Generic pixel format conversion. + for (uint x = 0; x < w; x++) + { + uint c = 0; + c |= PixelFormat::convert(src[x].r, 8, rsize) << rshift; + c |= PixelFormat::convert(src[x].g, 8, gsize) << gshift; + c |= PixelFormat::convert(src[x].b, 8, bsize) << bshift; + c |= PixelFormat::convert(src[x].a, 8, asize) << ashift; + + // Output one byte at a time. + for (uint i = 0; i < byteCount; i++) + { + *(dst + x * byteCount + i) = (c >> (i * 8)) & 0xFF; + } + } + + // Zero padding. + for (uint x = w * byteCount; x < pitch; x++) + { + *(dst + x) = 0; + } + } + + if (outputOptions.outputHandler != NULL) + { + outputOptions.outputHandler->writeData(dst, pitch); + } + } + + mem::free(dst); }