diff --git a/src/nvimage/DirectDrawSurface.cpp b/src/nvimage/DirectDrawSurface.cpp index fd76950..9fa7f43 100644 --- a/src/nvimage/DirectDrawSurface.cpp +++ b/src/nvimage/DirectDrawSurface.cpp @@ -1196,13 +1196,9 @@ uint DirectDrawSurface::mipmapSize(uint mipmap) const { nvDebugCheck((header.pf.flags & DDPF_RGB) || (header.pf.flags & DDPF_LUMINANCE)); - // Align pixels to bytes. - uint byteCount = (header.pf.bitcount + 7) / 8; - - // Align pitch to 4 bytes. - uint pitch = 4 * ((w * byteCount + 3) / 4); - - return pitch * h * d; + uint pitch = computePitch(w, header.pf.bitcount, 8); // Asuming 8 bit alignment, which is the same D3DX expects. + + return pitch * h * d; } } diff --git a/src/nvimage/nvimage.h b/src/nvimage/nvimage.h index beb3965..5aacb13 100644 --- a/src/nvimage/nvimage.h +++ b/src/nvimage/nvimage.h @@ -19,4 +19,14 @@ #define NVIMAGE_CLASS #endif + +// Some utility functions: + +inline uint computePitch(uint w, uint bitsize, uint alignment) +{ + return ((w * bitsize + 8 * alignment - 1) / (8 * alignment)) * alignment; +} + + + #endif // NV_IMAGE_H diff --git a/src/nvtt/CompressRGB.cpp b/src/nvtt/CompressRGB.cpp index bc2f594..a48ebd6 100644 --- a/src/nvtt/CompressRGB.cpp +++ b/src/nvtt/CompressRGB.cpp @@ -37,12 +37,6 @@ using namespace nvtt; namespace { - 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); @@ -86,7 +80,7 @@ void nv::compressRGB(const Image * image, const OutputOptions::Private & outputO PixelFormat::maskShiftAndSize(amask, &ashift, &asize); // Determine pitch. - uint pitch = computePitch(w, compressionOptions.bitcount); + uint pitch = computePitch(w, compressionOptions.bitcount, 8); uint8 * dst = (uint8 *)mem::malloc(pitch + 4); diff --git a/src/nvtt/Compressor.cpp b/src/nvtt/Compressor.cpp index d7f6403..4d8aae4 100644 --- a/src/nvtt/Compressor.cpp +++ b/src/nvtt/Compressor.cpp @@ -74,18 +74,10 @@ namespace return 0; } - 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 d, uint bitCount, Format format) { if (format == Format_RGBA) { - return d * h * computePitch(w, bitCount); + return d * h * computePitch(w, bitCount, 8); } else { // @@ Handle 3D textures. DXT and VTC have different behaviors. @@ -346,7 +338,7 @@ bool Compressor::Private::outputHeader(const InputOptions::Private & inputOption if (compressionOptions.format == Format_RGBA) { - header.setPitch(computePitch(inputOptions.targetWidth, compressionOptions.bitcount)); + header.setPitch(computePitch(inputOptions.targetWidth, compressionOptions.bitcount, 8)); header.setPixelFormat(compressionOptions.bitcount, compressionOptions.rmask, compressionOptions.gmask, compressionOptions.bmask, compressionOptions.amask); } else