Optimized image flipping

Added new extensible flipping system
Optimized NzTexture::Update(Face)
This commit is contained in:
Lynix
2012-06-22 23:18:37 +02:00
parent acc9b99ad2
commit 71cf7876c5
5 changed files with 140 additions and 35 deletions

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/String.hpp>
#include <map>
enum nzPixelFormat
{
@@ -49,6 +50,14 @@ enum nzPixelFormat
nzPixelFormat_Max = nzPixelFormat_RGBA8
};
enum nzPixelFlipping
{
nzPixelFlipping_Horizontally,
nzPixelFlipping_Vertically,
nzPixelFlipping_Max = nzPixelFlipping_Vertically
};
class NzUtility;
class NzPixelFormat
@@ -57,10 +66,13 @@ class NzPixelFormat
public:
typedef nzUInt8* (*ConvertFunction)(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst);
typedef void (*FlipFunction)(unsigned int width, unsigned int height, unsigned int depth, const nzUInt8* src, nzUInt8* dst);
static bool Convert(nzPixelFormat srcFormat, nzPixelFormat dstFormat, const void* src, void* dst);
static bool Convert(nzPixelFormat srcFormat, nzPixelFormat dstFormat, const void* start, const void* end, void* dst);
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 bool HasAlpha(nzPixelFormat format);
@@ -70,6 +82,7 @@ class NzPixelFormat
static bool IsValid(nzPixelFormat format);
static void SetConvertFunction(nzPixelFormat srcFormat, nzPixelFormat dstFormat, ConvertFunction func);
static void SetFlipFunction(nzPixelFlipping flipping, nzPixelFormat format, FlipFunction func);
static NzString ToString(nzPixelFormat format);
@@ -78,6 +91,7 @@ class NzPixelFormat
static void Uninitialize();
static NAZARA_API ConvertFunction s_convertFunctions[nzPixelFormat_Max+1][nzPixelFormat_Max+1];
static NAZARA_API std::map<nzPixelFormat, FlipFunction> s_flipFunctions[nzPixelFlipping_Max+1];
};
#include <Nazara/Utility/PixelFormat.inl>

View File

@@ -68,6 +68,102 @@ inline bool NzPixelFormat::Convert(nzPixelFormat srcFormat, nzPixelFormat dstFor
return true;
}
inline bool NzPixelFormat::Flip(nzPixelFlipping flipping, nzPixelFormat format, unsigned int width, unsigned int height, unsigned int depth, const void* src, void* dst)
{
#if NAZARA_UTILITY_SAFE
if (!IsValid(format))
{
NazaraError("Invalid pixel format");
return false;
}
#endif
auto it = s_flipFunctions[flipping].find(format);
if (it != s_flipFunctions[flipping].end())
it->second(width, height, depth, reinterpret_cast<const nzUInt8*>(src), reinterpret_cast<nzUInt8*>(dst));
else
{
// Flipping générique
#if NAZARA_UTILITY_SAFE
if (IsCompressed(format))
{
NazaraError("No function to flip compressed format");
return false;
}
#endif
nzUInt8 bpp = GetBPP(format);
unsigned int lineStride = width*bpp;
switch (flipping)
{
case nzPixelFlipping_Horizontally:
{
if (src == dst)
{
for (unsigned int z = 0; z < depth; ++z)
{
nzUInt8* ptr = reinterpret_cast<nzUInt8*>(dst) + width*height*z;
for (unsigned int y = 0; y < height; ++y)
{
for (unsigned int x = 0; x < width/2; ++x)
std::swap_ranges(&ptr[x*bpp], &ptr[(x+1)*bpp], &ptr[(width-x)*bpp]);
ptr += lineStride;
}
}
}
else
{
for (unsigned int z = 0; z < depth; ++z)
{
nzUInt8* ptr = reinterpret_cast<nzUInt8*>(dst) + width*height*z;
for (unsigned int y = 0; y < height; ++y)
{
for (unsigned int x = 0; x < width; ++x)
std::memcpy(&ptr[x*bpp], &ptr[(width-x)*bpp], bpp);
ptr += lineStride;
}
}
}
break;
}
case nzPixelFlipping_Vertically:
{
if (src == dst)
{
for (unsigned int z = 0; z < depth; ++z)
{
nzUInt8* ptr = reinterpret_cast<nzUInt8*>(dst) + width*height*z;
for (unsigned int y = 0; y < height/2; ++y)
std::swap_ranges(&ptr[y*lineStride], &ptr[(y+1)*lineStride-1], &ptr[(height-y-1)*lineStride]);
}
}
else
{
for (unsigned int z = 0; z < depth; ++z)
{
const nzUInt8* srcPtr = reinterpret_cast<const nzUInt8*>(src);
nzUInt8* dstPtr = reinterpret_cast<nzUInt8*>(dst) + (width-1)*height*depth*bpp;
for (unsigned int y = 0; y < height; ++y)
{
std::memcpy(dstPtr, srcPtr, lineStride);
srcPtr += lineStride;
dstPtr -= lineStride;
}
}
}
break;
}
}
}
return true;
}
inline nzUInt8 NzPixelFormat::GetBPP(nzPixelFormat format)
{
switch (format)
@@ -198,6 +294,11 @@ inline void NzPixelFormat::SetConvertFunction(nzPixelFormat srcFormat, nzPixelFo
s_convertFunctions[srcFormat][dstFormat] = func;
}
inline void NzPixelFormat::SetFlipFunction(nzPixelFlipping flipping, nzPixelFormat format, FlipFunction func)
{
s_flipFunctions[flipping][format] = func;
}
inline NzString NzPixelFormat::ToString(nzPixelFormat format)
{
switch (format)