From 8fa54c85a4c3e1c6cc131d5a8863b787bcfdb6f7 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 2 Mar 2013 00:28:39 +0100 Subject: [PATCH] Optimized conversion Former-commit-id: f4c463c1dd42a5f8e90bc7796c88a175ceb2bbef --- src/Nazara/Utility/PixelFormat.cpp | 308 ++++++++++++++--------------- 1 file changed, 154 insertions(+), 154 deletions(-) diff --git a/src/Nazara/Utility/PixelFormat.cpp b/src/Nazara/Utility/PixelFormat.cpp index 840b2de17..66ef7f9b9 100644 --- a/src/Nazara/Utility/PixelFormat.cpp +++ b/src/Nazara/Utility/PixelFormat.cpp @@ -72,7 +72,7 @@ namespace { while (start < end) { - *dst++ = static_cast(start[2] * 0.3 + start[1] * 0.59 + start[0] * 0.11); + *dst++ = static_cast(start[2] * 0.3f + start[1] * 0.59f + start[0] * 0.11f); start += 3; } @@ -85,7 +85,7 @@ namespace { while (start < end) { - *dst++ = static_cast(start[2] * 0.3 + start[1] * 0.59 + start[0] * 0.11); + *dst++ = static_cast(start[2] * 0.3f + start[1] * 0.59f + start[0] * 0.11f); *dst++ = 0xFF; start += 3; @@ -94,28 +94,6 @@ namespace return dst; } - template<> - nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) - { - nzUInt16* ptr = reinterpret_cast(dst); - while (start < end) - { - *ptr = (static_cast(c8to4(start[2])) << 12) | - (static_cast(c8to4(start[1])) << 8) | - (static_cast(c8to4(start[0])) << 4) | - 0x0F; - - #ifdef NAZARA_BIG_ENDIAN - NzByteSwap(ptr, sizeof(nzUInt16)); - #endif - - ptr++; - start += 3; - } - - return dst; - } - template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -153,6 +131,28 @@ namespace return dst; } + template<> + nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) + { + nzUInt16* ptr = reinterpret_cast(dst); + while (start < end) + { + *ptr = (static_cast(c8to4(start[2])) << 12) | + (static_cast(c8to4(start[1])) << 8) | + (static_cast(c8to4(start[0])) << 4) | + 0x0F; + + #ifdef NAZARA_BIG_ENDIAN + NzByteSwap(ptr, sizeof(nzUInt16)); + #endif + + ptr++; + start += 3; + } + + return dst; + } + template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -190,7 +190,7 @@ namespace { while (start < end) { - *dst++ = static_cast(start[2] * 0.3 + start[1] * 0.59 + start[0] * 0.11); + *dst++ = static_cast(start[2] * 0.3f + start[1] * 0.59f + start[0] * 0.11f); start += 4; } @@ -203,7 +203,7 @@ namespace { while (start < end) { - *dst++ = static_cast(start[2] * 0.3 + start[1] * 0.59 + start[0] * 0.11); + *dst++ = static_cast(start[2] * 0.3f + start[1] * 0.59f + start[0] * 0.11f); *dst++ = start[3]; start += 4; @@ -333,30 +333,6 @@ namespace return dst; } - template<> - nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) - { - nzUInt16* ptr = reinterpret_cast(dst); - while (start < end) - { - nzUInt16 l = static_cast(c8to4(start[0])); - - *ptr = (l << 12) | - (l << 8) | - (l << 4) | - 0x0F; - - #ifdef NAZARA_BIG_ENDIAN - NzByteSwap(ptr, sizeof(nzUInt16)); - #endif - - ptr++; - start += 1; - } - - return dst; - } - template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -396,6 +372,30 @@ namespace return dst; } + template<> + nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) + { + nzUInt16* ptr = reinterpret_cast(dst); + while (start < end) + { + nzUInt16 l = static_cast(c8to4(start[0])); + + *ptr = (l << 12) | + (l << 8) | + (l << 4) | + 0x0F; + + #ifdef NAZARA_BIG_ENDIAN + NzByteSwap(ptr, sizeof(nzUInt16)); + #endif + + ptr++; + start += 1; + } + + return dst; + } + template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -457,27 +457,6 @@ namespace return dst; } - template<> - nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) - { - nzUInt16* ptr = reinterpret_cast(dst); - while (start < end) - { - nzUInt16 l = static_cast(c8to4(start[0])); - - *ptr = (l << 12) | (l << 8) | (l << 4) | c8to4(start[1]); - - #ifdef NAZARA_BIG_ENDIAN - NzByteSwap(ptr, sizeof(nzUInt16)); - #endif - - ptr++; - start += 2; - } - - return dst; - } - template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -514,6 +493,27 @@ namespace return dst; } + template<> + nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) + { + nzUInt16* ptr = reinterpret_cast(dst); + while (start < end) + { + nzUInt16 l = static_cast(c8to4(start[0])); + + *ptr = (l << 12) | (l << 8) | (l << 4) | c8to4(start[1]); + + #ifdef NAZARA_BIG_ENDIAN + NzByteSwap(ptr, sizeof(nzUInt16)); + #endif + + ptr++; + start += 2; + } + + return dst; + } + template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -589,7 +589,7 @@ namespace nzUInt16 g = c4to8((pixel & 0x0F00) >> 8); nzUInt16 b = c4to8((pixel & 0x00F0) >> 4); - *dst++ = static_cast(r * 0.3 + g * 0.59 + b * 0.11); + *dst++ = static_cast(r * 0.3f + g * 0.59f + b * 0.11f); start += 2; } @@ -612,7 +612,7 @@ namespace nzUInt16 g = c4to8((pixel & 0x0F00) >> 8); nzUInt16 b = c4to8((pixel & 0x00F0) >> 4); - *dst++ = static_cast(r * 0.3 + g * 0.59 + b * 0.11); + *dst++ = static_cast(r * 0.3f + g * 0.59f + b * 0.11f); *dst++ = c4to8(pixel & 0x000F); start += 2; @@ -753,7 +753,7 @@ namespace nzUInt8 g = c5to8((pixel & 0x07C0) >> 6); nzUInt8 b = c5to8((pixel & 0x003E) >> 1); - *dst++ = static_cast(r * 0.3 + g * 0.59 + b * 0.11); + *dst++ = static_cast(r * 0.3f + g * 0.59f + b * 0.11f); start += 2; } @@ -776,7 +776,7 @@ namespace nzUInt8 g = c5to8((pixel & 0x07C0) >> 6); nzUInt8 b = c5to8((pixel & 0x003E) >> 1); - *dst++ = static_cast(r * 0.3 + g * 0.59 + b * 0.11); + *dst++ = static_cast(r * 0.3f + g * 0.59f + b * 0.11f); *dst++ = static_cast((pixel & 0x1)*0xFF); start += 2; @@ -785,6 +785,27 @@ namespace return dst; } + template<> + nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) + { + while (start < end) + { + nzUInt16 pixel = *reinterpret_cast(start); + + #ifdef NAZARA_BIG_ENDIAN + NzByteSwap(&pixel, sizeof(nzUInt16)); + #endif + + *dst++ = c5to8((pixel & 0xF800) >> 11); + *dst++ = c5to8((pixel & 0x07C0) >> 6); + *dst++ = c5to8((pixel & 0x003E) >> 1); + + start += 2; + } + + return dst; + } + template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -814,27 +835,6 @@ namespace return dst; } - template<> - nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) - { - while (start < end) - { - nzUInt16 pixel = *reinterpret_cast(start); - - #ifdef NAZARA_BIG_ENDIAN - NzByteSwap(&pixel, sizeof(nzUInt16)); - #endif - - *dst++ = c5to8((pixel & 0xF800) >> 11); - *dst++ = c5to8((pixel & 0x07C0) >> 6); - *dst++ = c5to8((pixel & 0x003E) >> 1); - - start += 2; - } - - return dst; - } - template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -894,7 +894,7 @@ namespace { while (start < end) { - *dst++ = static_cast(start[0] * 0.3 + start[1] * 0.59 + start[2] * 0.11); + *dst++ = static_cast(start[0] * 0.3f + start[1] * 0.59f + start[2] * 0.11f); start += 3; } @@ -907,7 +907,7 @@ namespace { while (start < end) { - *dst++ = static_cast(start[0] * 0.3 + start[1] * 0.59 + start[2] * 0.11); + *dst++ = static_cast(start[0] * 0.3f + start[1] * 0.59f + start[2] * 0.11f); *dst++ = 0xFF; start += 3; @@ -916,28 +916,6 @@ namespace return dst; } - template<> - nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) - { - nzUInt16* ptr = reinterpret_cast(dst); - while (start < end) - { - *ptr = (static_cast(c8to4(start[0])) << 12) | - (static_cast(c8to4(start[1])) << 8) | - (static_cast(c8to4(start[2])) << 4) | - 0x0F; - - #ifdef NAZARA_BIG_ENDIAN - NzByteSwap(ptr, sizeof(nzUInt16)); - #endif - - ptr++; - start += 3; - } - - return dst; - } - template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -960,6 +938,28 @@ namespace return reinterpret_cast(ptr); } + template<> + nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) + { + nzUInt16* ptr = reinterpret_cast(dst); + while (start < end) + { + *ptr = (static_cast(c8to4(start[0])) << 12) | + (static_cast(c8to4(start[1])) << 8) | + (static_cast(c8to4(start[2])) << 4) | + 0x0F; + + #ifdef NAZARA_BIG_ENDIAN + NzByteSwap(ptr, sizeof(nzUInt16)); + #endif + + ptr++; + start += 3; + } + + return dst; + } + template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -1013,7 +1013,7 @@ namespace { while (start < end) { - *dst++ = static_cast(start[0] * 0.3 + start[1] * 0.59 + start[2] * 0.11); + *dst++ = static_cast(start[0] * 0.3f + start[1] * 0.59f + start[2] * 0.11f); start += 4; } @@ -1026,7 +1026,7 @@ namespace { while (start < end) { - *dst++ = static_cast(start[0] * 0.3 + start[1] * 0.59 + start[2] * 0.11); + *dst++ = static_cast(start[0] * 0.3f + start[1] * 0.59f + start[2] * 0.11f); *dst++ = start[3]; start += 4; @@ -1035,28 +1035,6 @@ namespace return dst; } - template<> - nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) - { - nzUInt16* ptr = reinterpret_cast(dst); - while (start < end) - { - *ptr = (static_cast(c8to4(start[0])) << 12) | - (static_cast(c8to4(start[1])) << 8) | - (static_cast(c8to4(start[2])) << 4) | - (static_cast(c8to4(start[3])) << 0); - - #ifdef NAZARA_BIG_ENDIAN - NzByteSwap(ptr, sizeof(nzUInt16)); - #endif - - ptr++; - start += 4; - } - - return dst; - } - template<> nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) { @@ -1094,6 +1072,28 @@ namespace return dst; } + template<> + nzUInt8* ConvertPixels(const nzUInt8* start, const nzUInt8* end, nzUInt8* dst) + { + nzUInt16* ptr = reinterpret_cast(dst); + while (start < end) + { + *ptr = (static_cast(c8to4(start[0])) << 12) | + (static_cast(c8to4(start[1])) << 8) | + (static_cast(c8to4(start[2])) << 4) | + (static_cast(c8to4(start[3])) << 0); + + #ifdef NAZARA_BIG_ENDIAN + NzByteSwap(ptr, sizeof(nzUInt16)); + #endif + + ptr++; + start += 4; + } + + return dst; + } + template void RegisterConverter() { @@ -1118,9 +1118,9 @@ bool NzPixelFormat::Initialize() RegisterConverter(); RegisterConverter(); RegisterConverter();*/ - RegisterConverter(); RegisterConverter(); RegisterConverter(); + RegisterConverter(); RegisterConverter(); /**********************************BGRA8**********************************/ @@ -1135,9 +1135,9 @@ bool NzPixelFormat::Initialize() RegisterConverter(); RegisterConverter(); RegisterConverter();*/ - RegisterConverter(); RegisterConverter(); RegisterConverter(); + RegisterConverter(); RegisterConverter(); /**********************************DXT1***********************************/ @@ -1157,9 +1157,9 @@ bool NzPixelFormat::Initialize() RegisterConverter(); RegisterConverter(); RegisterConverter(); - RegisterConverter(); RegisterConverter(); RegisterConverter(); + RegisterConverter(); RegisterConverter(); */ @@ -1180,9 +1180,9 @@ bool NzPixelFormat::Initialize() RegisterConverter(); RegisterConverter(); RegisterConverter(); - RegisterConverter(); RegisterConverter(); RegisterConverter(); + RegisterConverter(); RegisterConverter(); */ @@ -1203,9 +1203,9 @@ bool NzPixelFormat::Initialize() RegisterConverter(); RegisterConverter(); RegisterConverter(); - RegisterConverter(); RegisterConverter(); RegisterConverter(); + RegisterConverter(); RegisterConverter(); */ @@ -1221,9 +1221,9 @@ bool NzPixelFormat::Initialize() RegisterConverter(); RegisterConverter(); RegisterConverter();*/ - RegisterConverter(); RegisterConverter(); RegisterConverter(); + RegisterConverter(); RegisterConverter(); /***********************************LA8***********************************/ @@ -1238,9 +1238,9 @@ bool NzPixelFormat::Initialize() RegisterConverter(); RegisterConverter(); RegisterConverter();*/ - RegisterConverter(); RegisterConverter(); RegisterConverter(); + RegisterConverter(); RegisterConverter(); /**********************************RGBA4**********************************/ @@ -1273,8 +1273,8 @@ bool NzPixelFormat::Initialize() RegisterConverter(); RegisterConverter(); RegisterConverter();*/ - RegisterConverter(); RegisterConverter(); + RegisterConverter(); RegisterConverter(); /**********************************RGB8***********************************/ @@ -1290,8 +1290,8 @@ bool NzPixelFormat::Initialize() RegisterConverter(); RegisterConverter(); RegisterConverter();*/ - RegisterConverter(); RegisterConverter(); + RegisterConverter(); RegisterConverter(); /**********************************RGBA8**********************************/ @@ -1307,9 +1307,9 @@ bool NzPixelFormat::Initialize() RegisterConverter(); RegisterConverter(); RegisterConverter();*/ - RegisterConverter(); RegisterConverter(); RegisterConverter(); + RegisterConverter(); return true; }