Reputation: 453
I've been looking at the WICTextureLoader.cpp file that is part of the DirectX Toolkit. And I'm trying to understand this bit of code:
https://github.com/microsoft/DirectXTK/blob/master/Src/WICTextureLoader.cpp#L530
// Allocate temporary memory for image
uint64_t rowBytes = (uint64_t(twidth) * uint64_t(bpp) + 7u) / 8u;
uint64_t numBytes = rowBytes * uint64_t(theight);
I understand that you'd need to allocate the texture width * bits per pixel for a single row of the texture. But I do not understand the addition of 7 per pixel and then the division of the whole thing by 8. What is that about?
Upvotes: 0
Views: 182
Reputation: 41047
bpp
here is "bits per pixel". The expression rounds up to the next whole byte (8 bits).
This is a classic C pattern for "align up" for power-of-2 alignments, here expressed as a C++ template.
template<typename T>
inline T AlignUp(T size, size_t alignment) noexcept
{
if (alignment > 0)
{
assert(((alignment - 1) & alignment) == 0);
auto mask = static_cast<T>(alignment - 1);
return (size + mask) & ~mask;
}
return size;
}
Instead of / 8
, I could have used bit operators (again, since it's a power of two) and done & ~8
but the / 8
seems clearer.
Upvotes: 1