From 8d057fb3b131cc489669ef40b28e4e52187ed867 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 30 Sep 2012 23:11:00 +0200 Subject: [PATCH] Renamed GetBPP to GetBytesPerPixel Also added GetBitsPerPixel Former-commit-id: 9cc1df58133c538b1dc74215a9022d13dfd9d7f6 --- include/Nazara/Renderer/Texture.hpp | 2 +- include/Nazara/Utility/Image.hpp | 2 +- include/Nazara/Utility/PixelFormat.hpp | 3 +- include/Nazara/Utility/PixelFormat.inl | 82 +++++++++++++++----------- src/Nazara/Renderer/Texture.cpp | 10 ++-- src/Nazara/Utility/Image.cpp | 36 +++++------ 6 files changed, 73 insertions(+), 62 deletions(-) diff --git a/include/Nazara/Renderer/Texture.hpp b/include/Nazara/Renderer/Texture.hpp index 9a849146e..111063113 100644 --- a/include/Nazara/Renderer/Texture.hpp +++ b/include/Nazara/Renderer/Texture.hpp @@ -34,7 +34,7 @@ class NAZARA_API NzTexture : public NzResource, NzNonCopyable bool EnableMipmapping(bool enable); unsigned int GetAnisotropyLevel() const; - nzUInt8 GetBPP() const; + nzUInt8 GetBytesPerPixel() const; unsigned int GetDepth() const; nzTextureFilter GetFilterMode() const; nzPixelFormat GetFormat() const; diff --git a/include/Nazara/Utility/Image.hpp b/include/Nazara/Utility/Image.hpp index d58ff1736..5c7411496 100644 --- a/include/Nazara/Utility/Image.hpp +++ b/include/Nazara/Utility/Image.hpp @@ -66,7 +66,7 @@ class NAZARA_API NzImage : public NzResource bool FlipHorizontally(); bool FlipVertically(); - nzUInt8 GetBPP() const; + nzUInt8 GetBytesPerPixel() const; const nzUInt8* GetConstPixels(unsigned int x = 0, unsigned int y = 0, unsigned int z = 0, nzUInt8 level = 0) const; unsigned int GetDepth(nzUInt8 level = 0) const; nzPixelFormat GetFormat() const; diff --git a/include/Nazara/Utility/PixelFormat.hpp b/include/Nazara/Utility/PixelFormat.hpp index 7d46d82ef..e155bd9a1 100644 --- a/include/Nazara/Utility/PixelFormat.hpp +++ b/include/Nazara/Utility/PixelFormat.hpp @@ -27,7 +27,8 @@ class NzPixelFormat static bool Flip(nzPixelFlipping flipping, nzPixelFormat format, unsigned int width, unsigned int height, unsigned int depth, const void* src, void* dst); - static nzUInt8 GetBPP(nzPixelFormat format); + static nzUInt8 GetBitsPerPixel(nzPixelFormat format); + static nzUInt8 GetBytesPerPixel(nzPixelFormat format); static nzPixelFormatType GetType(nzPixelFormat format); static bool HasAlpha(nzPixelFormat format); diff --git a/include/Nazara/Utility/PixelFormat.inl b/include/Nazara/Utility/PixelFormat.inl index b48af518c..d3fdc5d30 100644 --- a/include/Nazara/Utility/PixelFormat.inl +++ b/include/Nazara/Utility/PixelFormat.inl @@ -10,7 +10,7 @@ inline bool NzPixelFormat::Convert(nzPixelFormat srcFormat, nzPixelFormat dstFor { if (srcFormat == dstFormat) { - std::memcpy(dst, src, GetBPP(srcFormat)); + std::memcpy(dst, src, GetBytesPerPixel(srcFormat)); return true; } @@ -35,7 +35,7 @@ inline bool NzPixelFormat::Convert(nzPixelFormat srcFormat, nzPixelFormat dstFor return false; } - if (!func(reinterpret_cast(src), reinterpret_cast(src) + GetBPP(srcFormat), reinterpret_cast(dst))) + if (!func(reinterpret_cast(src), reinterpret_cast(src) + GetBytesPerPixel(srcFormat), reinterpret_cast(dst))) { NazaraError("Pixel format conversion from " + ToString(srcFormat) + " to " + ToString(dstFormat) + " failed"); return false; @@ -93,7 +93,7 @@ inline bool NzPixelFormat::Flip(nzPixelFlipping flipping, nzPixelFormat format, } #endif - nzUInt8 bpp = GetBPP(format); + nzUInt8 bpp = GetBytesPerPixel(format); unsigned int lineStride = width*bpp; switch (flipping) { @@ -164,93 +164,91 @@ inline bool NzPixelFormat::Flip(nzPixelFlipping flipping, nzPixelFormat format, return true; } -inline nzUInt8 NzPixelFormat::GetBPP(nzPixelFormat format) +inline nzUInt8 NzPixelFormat::GetBitsPerPixel(nzPixelFormat format) { switch (format) { case nzPixelFormat_BGR8: - return 3; + return 24; case nzPixelFormat_BGRA8: - return 4; + return 32; case nzPixelFormat_DXT1: - return 1; + return 8; case nzPixelFormat_DXT3: - return 2; + return 16; case nzPixelFormat_DXT5: - return 2; + return 16; case nzPixelFormat_L8: - return 1; + return 8; case nzPixelFormat_LA8: - return 2; + return 16; /* case nzPixelFormat_RGB16F: - return 6; + return 48; case nzPixelFormat_RGB16I: - return 6; + return 48; case nzPixelFormat_RGB32F: - return 12; + return 96; case nzPixelFormat_RGB32I: - return 12; + return 96; case nzPixelFormat_RGBA16F: - return 8; + return 64; case nzPixelFormat_RGBA16I: - return 8; + return 64; case nzPixelFormat_RGBA32F: - return 16; + return 128; case nzPixelFormat_RGBA32I: - return 16; + return 128; */ case nzPixelFormat_RGBA4: - return 2; + return 16; case nzPixelFormat_RGB5A1: - return 2; + return 16; case nzPixelFormat_RGB8: - return 3; + return 24; case nzPixelFormat_RGBA8: - return 4; + return 32; case nzPixelFormat_Depth16: - return 2; + return 16; case nzPixelFormat_Depth24: - return 3; + return 24; case nzPixelFormat_Depth24Stencil8: - return 4; + return 32; case nzPixelFormat_Depth32: - return 4; + return 32; case nzPixelFormat_Stencil1: - NazaraWarning("This format uses less than one byte per pixel"); - return 0; - - case nzPixelFormat_Stencil4: - NazaraWarning("This format uses less than one byte per pixel"); - return 0; - - case nzPixelFormat_Stencil8: return 1; - case nzPixelFormat_Stencil16: + case nzPixelFormat_Stencil4: return 2; + case nzPixelFormat_Stencil8: + return 8; + + case nzPixelFormat_Stencil16: + return 16; + case nzPixelFormat_Undefined: break; } @@ -259,6 +257,18 @@ inline nzUInt8 NzPixelFormat::GetBPP(nzPixelFormat format) return 0; } +inline nzUInt8 NzPixelFormat::GetBytesPerPixel(nzPixelFormat format) +{ + nzUInt8 bytesPerPixel = GetBitsPerPixel(format)/8; + + #if NAZARA_UTILITY_SAFE + if (bytesPerPixel == 0) + NazaraWarning("This format is invalid or uses less than one byte per pixel"); + #endif + + return bytesPerPixel; +} + inline nzPixelFormatType NzPixelFormat::GetType(nzPixelFormat format) { switch (format) diff --git a/src/Nazara/Renderer/Texture.cpp b/src/Nazara/Renderer/Texture.cpp index 870fda854..786faa070 100644 --- a/src/Nazara/Renderer/Texture.cpp +++ b/src/Nazara/Renderer/Texture.cpp @@ -485,7 +485,7 @@ unsigned int NzTexture::GetAnisotropyLevel() const return anisotropyLevel; } -nzUInt8 NzTexture::GetBPP() const +nzUInt8 NzTexture::GetBytesPerPixel() const { #if NAZARA_RENDERER_SAFE if (!m_impl) @@ -495,7 +495,7 @@ nzUInt8 NzTexture::GetBPP() const } #endif - return NzPixelFormat::GetBPP(m_impl->format); + return NzPixelFormat::GetBytesPerPixel(m_impl->format); } unsigned int NzTexture::GetDepth() const @@ -1113,7 +1113,7 @@ bool NzTexture::Update(const nzUInt8* pixels, const NzRectui& rect, unsigned int return false; } - nzUInt8 bpp = NzPixelFormat::GetBPP(m_impl->format); + nzUInt8 bpp = NzPixelFormat::GetBytesPerPixel(m_impl->format); // Inversion de la texture pour le repère d'OpenGL NzImage mirrored; @@ -1210,7 +1210,7 @@ bool NzTexture::Update(const nzUInt8* pixels, const NzCubeui& cube, nzUInt8 leve return false; } - nzUInt8 bpp = NzPixelFormat::GetBPP(m_impl->format); + nzUInt8 bpp = NzPixelFormat::GetBytesPerPixel(m_impl->format); // Inversion de la texture pour le repère d'OpenGL unsigned int size = cube.width*cube.height*cube.depth*bpp; @@ -1365,7 +1365,7 @@ bool NzTexture::UpdateFace(nzCubemapFace face, const nzUInt8* pixels, const NzRe return false; } - nzUInt8 bpp = NzPixelFormat::GetBPP(m_impl->format); + nzUInt8 bpp = NzPixelFormat::GetBytesPerPixel(m_impl->format); // Inversion de la texture pour le repère d'OpenGL unsigned int size = rect.width*rect.height*bpp; diff --git a/src/Nazara/Utility/Image.cpp b/src/Nazara/Utility/Image.cpp index 68bee7d9b..021f4211d 100644 --- a/src/Nazara/Utility/Image.cpp +++ b/src/Nazara/Utility/Image.cpp @@ -105,10 +105,10 @@ bool NzImage::Convert(nzPixelFormat format) for (unsigned int i = 0; i < m_sharedImage->levelCount; ++i) { unsigned int pixelsPerFace = width*height; - nzUInt8* ptr = new nzUInt8[pixelsPerFace*depth*NzPixelFormat::GetBPP(format)]; + nzUInt8* ptr = new nzUInt8[pixelsPerFace*depth*NzPixelFormat::GetBytesPerPixel(format)]; nzUInt8* pixels = m_sharedImage->pixels[i]; - unsigned int srcStride = pixelsPerFace * NzPixelFormat::GetBPP(m_sharedImage->format); - unsigned int dstStride = pixelsPerFace * NzPixelFormat::GetBPP(format); + unsigned int srcStride = pixelsPerFace * NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); + unsigned int dstStride = pixelsPerFace * NzPixelFormat::GetBytesPerPixel(format); for (unsigned int d = 0; d < depth; ++d) { @@ -180,7 +180,7 @@ bool NzImage::Copy(const NzImage& source, const NzCubeui& srcCube, const NzVecto ///FIXME: Trouver une interface pour gérer ce genre de problème (Façon OpenGL?) (Appliquer l'interface à NzTexture également) */ - nzUInt8 bpp = NzPixelFormat::GetBPP(m_sharedImage->format); + nzUInt8 bpp = NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); unsigned int dstLineStride = srcCube.width*bpp; unsigned int dstFaceStride = dstLineStride*srcCube.height; unsigned int srcLineStride = m_sharedImage->width*bpp; @@ -302,7 +302,7 @@ bool NzImage::Create(nzImageType type, nzPixelFormat format, unsigned int width, // Cette allocation est protégée car sa taille dépend directement de paramètres utilisateurs try { - levels[i] = new nzUInt8[w * h * d * NzPixelFormat::GetBPP(format)]; + levels[i] = new nzUInt8[w * h * d * NzPixelFormat::GetBytesPerPixel(format)]; if (w > 1) w >>= 1; @@ -358,7 +358,7 @@ bool NzImage::Fill(const NzColor& color) EnsureOwnership(); - nzUInt8 bpp = NzPixelFormat::GetBPP(m_sharedImage->format); + nzUInt8 bpp = NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); nzUInt8* pixels = new nzUInt8[bpp]; if (!NzPixelFormat::Convert(nzPixelFormat_RGBA8, m_sharedImage->format, &color.r, pixels)) { @@ -429,7 +429,7 @@ bool NzImage::Fill(const NzColor& color, const NzRectui& rect, unsigned int z) EnsureOwnership(); - nzUInt8 bpp = NzPixelFormat::GetBPP(m_sharedImage->format); + nzUInt8 bpp = NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); nzUInt8* pixels = new nzUInt8[bpp]; if (!NzPixelFormat::Convert(nzPixelFormat_RGBA8, m_sharedImage->format, &color.r, pixels)) { @@ -485,7 +485,7 @@ bool NzImage::Fill(const NzColor& color, const NzCubeui& cube) EnsureOwnership(); - nzUInt8 bpp = NzPixelFormat::GetBPP(m_sharedImage->format); + nzUInt8 bpp = NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); nzUInt8* pixels = new nzUInt8[bpp]; if (!NzPixelFormat::Convert(nzPixelFormat_RGBA8, m_sharedImage->format, &color.r, pixels)) { @@ -601,9 +601,9 @@ bool NzImage::FlipVertically() return true; } -nzUInt8 NzImage::GetBPP() const +nzUInt8 NzImage::GetBytesPerPixel() const { - return NzPixelFormat::GetBPP(m_sharedImage->format); + return NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); } const nzUInt8* NzImage::GetConstPixels(unsigned int x, unsigned int y, unsigned int z, nzUInt8 level) const @@ -641,7 +641,7 @@ const nzUInt8* NzImage::GetConstPixels(unsigned int x, unsigned int y, unsigned } #endif - return GetPixelPtr(m_sharedImage->pixels[level], NzPixelFormat::GetBPP(m_sharedImage->format), x, y, z, m_sharedImage->width, m_sharedImage->height); + return GetPixelPtr(m_sharedImage->pixels[level], NzPixelFormat::GetBytesPerPixel(m_sharedImage->format), x, y, z, m_sharedImage->width, m_sharedImage->height); } unsigned int NzImage::GetDepth(nzUInt8 level) const @@ -720,7 +720,7 @@ NzColor NzImage::GetPixelColor(unsigned int x, unsigned int y, unsigned int z) c } #endif - const nzUInt8* pixel = GetPixelPtr(m_sharedImage->pixels[0], NzPixelFormat::GetBPP(m_sharedImage->format), x, y, z, m_sharedImage->width, m_sharedImage->height); + const nzUInt8* pixel = GetPixelPtr(m_sharedImage->pixels[0], NzPixelFormat::GetBytesPerPixel(m_sharedImage->format), x, y, z, m_sharedImage->width, m_sharedImage->height); NzColor color; if (!NzPixelFormat::Convert(m_sharedImage->format, nzPixelFormat_RGBA8, pixel, &color.r)) @@ -767,7 +767,7 @@ nzUInt8* NzImage::GetPixels(unsigned int x, unsigned int y, unsigned int z, nzUI EnsureOwnership(); - return GetPixelPtr(m_sharedImage->pixels[level], NzPixelFormat::GetBPP(m_sharedImage->format), x, y, z, m_sharedImage->width, m_sharedImage->height); + return GetPixelPtr(m_sharedImage->pixels[level], NzPixelFormat::GetBytesPerPixel(m_sharedImage->format), x, y, z, m_sharedImage->width, m_sharedImage->height); } unsigned int NzImage::GetSize() const @@ -794,7 +794,7 @@ unsigned int NzImage::GetSize() const if (m_sharedImage->type == nzImageType_Cubemap) size *= 6; - return size * NzPixelFormat::GetBPP(m_sharedImage->format); + return size * NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); } unsigned int NzImage::GetSize(nzUInt8 level) const @@ -810,7 +810,7 @@ unsigned int NzImage::GetSize(nzUInt8 level) const return (GetLevelSize(m_sharedImage->width, level)) * (GetLevelSize(m_sharedImage->height, level)) * ((m_sharedImage->type == nzImageType_Cubemap) ? 6 : GetLevelSize(m_sharedImage->depth, level)) * - NzPixelFormat::GetBPP(m_sharedImage->format); + NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); } nzImageType NzImage::GetType() const @@ -941,7 +941,7 @@ bool NzImage::SetPixelColor(const NzColor& color, unsigned int x, unsigned int y } #endif - nzUInt8* pixel = GetPixelPtr(m_sharedImage->pixels[0], NzPixelFormat::GetBPP(m_sharedImage->format), x, y, z, m_sharedImage->width, m_sharedImage->height); + nzUInt8* pixel = GetPixelPtr(m_sharedImage->pixels[0], NzPixelFormat::GetBytesPerPixel(m_sharedImage->format), x, y, z, m_sharedImage->width, m_sharedImage->height); if (!NzPixelFormat::Convert(nzPixelFormat_RGBA8, m_sharedImage->format, &color.r, pixel)) { @@ -1029,7 +1029,7 @@ bool NzImage::Update(const nzUInt8* pixels, const NzRectui& rect, unsigned int z EnsureOwnership(); - nzUInt8 bpp = NzPixelFormat::GetBPP(m_sharedImage->format); + nzUInt8 bpp = NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); nzUInt8* dstPixels = GetPixelPtr(m_sharedImage->pixels[level], bpp, rect.x, rect.y, z, width, height); unsigned int srcStride = rect.width * bpp; unsigned int dstStride = m_sharedImage->width * bpp; @@ -1086,7 +1086,7 @@ bool NzImage::Update(const nzUInt8* pixels, const NzCubeui& cube, nzUInt8 level) EnsureOwnership(); - nzUInt8 bpp = NzPixelFormat::GetBPP(m_sharedImage->format); + nzUInt8 bpp = NzPixelFormat::GetBytesPerPixel(m_sharedImage->format); nzUInt8* dstPixels = GetPixelPtr(m_sharedImage->pixels[level], bpp, cube.x, cube.y, cube.z, width, height); unsigned int srcStride = cube.width * bpp; unsigned int dstStride = width * bpp;