Fix alignment assumption. Fixes issue 132.

2.0
castano 14 years ago
parent 2ea2b40c72
commit 66a8237215

@ -1196,13 +1196,9 @@ uint DirectDrawSurface::mipmapSize(uint mipmap) const
{ {
nvDebugCheck((header.pf.flags & DDPF_RGB) || (header.pf.flags & DDPF_LUMINANCE)); nvDebugCheck((header.pf.flags & DDPF_RGB) || (header.pf.flags & DDPF_LUMINANCE));
// Align pixels to bytes. uint pitch = computePitch(w, header.pf.bitcount, 8); // Asuming 8 bit alignment, which is the same D3DX expects.
uint byteCount = (header.pf.bitcount + 7) / 8;
return pitch * h * d;
// Align pitch to 4 bytes.
uint pitch = 4 * ((w * byteCount + 3) / 4);
return pitch * h * d;
} }
} }

@ -19,4 +19,14 @@
#define NVIMAGE_CLASS #define NVIMAGE_CLASS
#endif #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 #endif // NV_IMAGE_H

@ -37,12 +37,6 @@ using namespace nvtt;
namespace 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) inline void convert_to_a8r8g8b8(const void * src, void * dst, uint w)
{ {
memcpy(dst, src, 4 * 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); PixelFormat::maskShiftAndSize(amask, &ashift, &asize);
// Determine pitch. // Determine pitch.
uint pitch = computePitch(w, compressionOptions.bitcount); uint pitch = computePitch(w, compressionOptions.bitcount, 8);
uint8 * dst = (uint8 *)mem::malloc(pitch + 4); uint8 * dst = (uint8 *)mem::malloc(pitch + 4);

@ -74,18 +74,10 @@ namespace
return 0; 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) static int computeImageSize(uint w, uint h, uint d, uint bitCount, Format format)
{ {
if (format == Format_RGBA) { if (format == Format_RGBA) {
return d * h * computePitch(w, bitCount); return d * h * computePitch(w, bitCount, 8);
} }
else { else {
// @@ Handle 3D textures. DXT and VTC have different behaviors. // @@ 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) 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); header.setPixelFormat(compressionOptions.bitcount, compressionOptions.rmask, compressionOptions.gmask, compressionOptions.bmask, compressionOptions.amask);
} }
else else

Loading…
Cancel
Save