summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/color.h50
-rw-r--r--src/common/vector_math.h6
2 files changed, 34 insertions, 22 deletions
diff --git a/src/common/color.h b/src/common/color.h
index 24a445dac..0379040be 100644
--- a/src/common/color.h
+++ b/src/common/color.h
@@ -4,6 +4,8 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <cstring>
8
7#include "common/common_types.h" 9#include "common/common_types.h"
8#include "common/swap.h" 10#include "common/swap.h"
9#include "common/vector_math.h" 11#include "common/vector_math.h"
@@ -55,7 +57,7 @@ constexpr u8 Convert8To6(u8 value) {
55 * @param bytes Pointer to encoded source color 57 * @param bytes Pointer to encoded source color
56 * @return Result color decoded as Math::Vec4<u8> 58 * @return Result color decoded as Math::Vec4<u8>
57 */ 59 */
58inline const Math::Vec4<u8> DecodeRGBA8(const u8* bytes) { 60inline Math::Vec4<u8> DecodeRGBA8(const u8* bytes) {
59 return {bytes[3], bytes[2], bytes[1], bytes[0]}; 61 return {bytes[3], bytes[2], bytes[1], bytes[0]};
60} 62}
61 63
@@ -64,7 +66,7 @@ inline const Math::Vec4<u8> DecodeRGBA8(const u8* bytes) {
64 * @param bytes Pointer to encoded source color 66 * @param bytes Pointer to encoded source color
65 * @return Result color decoded as Math::Vec4<u8> 67 * @return Result color decoded as Math::Vec4<u8>
66 */ 68 */
67inline const Math::Vec4<u8> DecodeRGB8(const u8* bytes) { 69inline Math::Vec4<u8> DecodeRGB8(const u8* bytes) {
68 return {bytes[2], bytes[1], bytes[0], 255}; 70 return {bytes[2], bytes[1], bytes[0], 255};
69} 71}
70 72
@@ -73,7 +75,7 @@ inline const Math::Vec4<u8> DecodeRGB8(const u8* bytes) {
73 * @param bytes Pointer to encoded source color 75 * @param bytes Pointer to encoded source color
74 * @return Result color decoded as Math::Vec4<u8> 76 * @return Result color decoded as Math::Vec4<u8>
75 */ 77 */
76inline const Math::Vec4<u8> DecodeRG8(const u8* bytes) { 78inline Math::Vec4<u8> DecodeRG8(const u8* bytes) {
77 return {bytes[1], bytes[0], 0, 255}; 79 return {bytes[1], bytes[0], 0, 255};
78} 80}
79 81
@@ -82,8 +84,9 @@ inline const Math::Vec4<u8> DecodeRG8(const u8* bytes) {
82 * @param bytes Pointer to encoded source color 84 * @param bytes Pointer to encoded source color
83 * @return Result color decoded as Math::Vec4<u8> 85 * @return Result color decoded as Math::Vec4<u8>
84 */ 86 */
85inline const Math::Vec4<u8> DecodeRGB565(const u8* bytes) { 87inline Math::Vec4<u8> DecodeRGB565(const u8* bytes) {
86 const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes); 88 u16_le pixel;
89 std::memcpy(&pixel, bytes, sizeof(pixel));
87 return {Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F), 90 return {Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F),
88 Convert5To8(pixel & 0x1F), 255}; 91 Convert5To8(pixel & 0x1F), 255};
89} 92}
@@ -93,8 +96,9 @@ inline const Math::Vec4<u8> DecodeRGB565(const u8* bytes) {
93 * @param bytes Pointer to encoded source color 96 * @param bytes Pointer to encoded source color
94 * @return Result color decoded as Math::Vec4<u8> 97 * @return Result color decoded as Math::Vec4<u8>
95 */ 98 */
96inline const Math::Vec4<u8> DecodeRGB5A1(const u8* bytes) { 99inline Math::Vec4<u8> DecodeRGB5A1(const u8* bytes) {
97 const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes); 100 u16_le pixel;
101 std::memcpy(&pixel, bytes, sizeof(pixel));
98 return {Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F), 102 return {Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F),
99 Convert5To8((pixel >> 1) & 0x1F), Convert1To8(pixel & 0x1)}; 103 Convert5To8((pixel >> 1) & 0x1F), Convert1To8(pixel & 0x1)};
100} 104}
@@ -104,8 +108,9 @@ inline const Math::Vec4<u8> DecodeRGB5A1(const u8* bytes) {
104 * @param bytes Pointer to encoded source color 108 * @param bytes Pointer to encoded source color
105 * @return Result color decoded as Math::Vec4<u8> 109 * @return Result color decoded as Math::Vec4<u8>
106 */ 110 */
107inline const Math::Vec4<u8> DecodeRGBA4(const u8* bytes) { 111inline Math::Vec4<u8> DecodeRGBA4(const u8* bytes) {
108 const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes); 112 u16_le pixel;
113 std::memcpy(&pixel, bytes, sizeof(pixel));
109 return {Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF), 114 return {Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF),
110 Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF)}; 115 Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF)};
111} 116}
@@ -116,7 +121,9 @@ inline const Math::Vec4<u8> DecodeRGBA4(const u8* bytes) {
116 * @return Depth value as an u32 121 * @return Depth value as an u32
117 */ 122 */
118inline u32 DecodeD16(const u8* bytes) { 123inline u32 DecodeD16(const u8* bytes) {
119 return *reinterpret_cast<const u16_le*>(bytes); 124 u16_le data;
125 std::memcpy(&data, bytes, sizeof(data));
126 return data;
120} 127}
121 128
122/** 129/**
@@ -133,7 +140,7 @@ inline u32 DecodeD24(const u8* bytes) {
133 * @param bytes Pointer to encoded source values 140 * @param bytes Pointer to encoded source values
134 * @return Resulting values stored as a Math::Vec2 141 * @return Resulting values stored as a Math::Vec2
135 */ 142 */
136inline const Math::Vec2<u32> DecodeD24S8(const u8* bytes) { 143inline Math::Vec2<u32> DecodeD24S8(const u8* bytes) {
137 return {static_cast<u32>((bytes[2] << 16) | (bytes[1] << 8) | bytes[0]), bytes[3]}; 144 return {static_cast<u32>((bytes[2] << 16) | (bytes[1] << 8) | bytes[0]), bytes[3]};
138} 145}
139 146
@@ -175,8 +182,10 @@ inline void EncodeRG8(const Math::Vec4<u8>& color, u8* bytes) {
175 * @param bytes Destination pointer to store encoded color 182 * @param bytes Destination pointer to store encoded color
176 */ 183 */
177inline void EncodeRGB565(const Math::Vec4<u8>& color, u8* bytes) { 184inline void EncodeRGB565(const Math::Vec4<u8>& color, u8* bytes) {
178 *reinterpret_cast<u16_le*>(bytes) = 185 const u16_le data =
179 (Convert8To5(color.r()) << 11) | (Convert8To6(color.g()) << 5) | Convert8To5(color.b()); 186 (Convert8To5(color.r()) << 11) | (Convert8To6(color.g()) << 5) | Convert8To5(color.b());
187
188 std::memcpy(bytes, &data, sizeof(data));
180} 189}
181 190
182/** 191/**
@@ -185,9 +194,10 @@ inline void EncodeRGB565(const Math::Vec4<u8>& color, u8* bytes) {
185 * @param bytes Destination pointer to store encoded color 194 * @param bytes Destination pointer to store encoded color
186 */ 195 */
187inline void EncodeRGB5A1(const Math::Vec4<u8>& color, u8* bytes) { 196inline void EncodeRGB5A1(const Math::Vec4<u8>& color, u8* bytes) {
188 *reinterpret_cast<u16_le*>(bytes) = (Convert8To5(color.r()) << 11) | 197 const u16_le data = (Convert8To5(color.r()) << 11) | (Convert8To5(color.g()) << 6) |
189 (Convert8To5(color.g()) << 6) | 198 (Convert8To5(color.b()) << 1) | Convert8To1(color.a());
190 (Convert8To5(color.b()) << 1) | Convert8To1(color.a()); 199
200 std::memcpy(bytes, &data, sizeof(data));
191} 201}
192 202
193/** 203/**
@@ -196,9 +206,10 @@ inline void EncodeRGB5A1(const Math::Vec4<u8>& color, u8* bytes) {
196 * @param bytes Destination pointer to store encoded color 206 * @param bytes Destination pointer to store encoded color
197 */ 207 */
198inline void EncodeRGBA4(const Math::Vec4<u8>& color, u8* bytes) { 208inline void EncodeRGBA4(const Math::Vec4<u8>& color, u8* bytes) {
199 *reinterpret_cast<u16_le*>(bytes) = (Convert8To4(color.r()) << 12) | 209 const u16 data = (Convert8To4(color.r()) << 12) | (Convert8To4(color.g()) << 8) |
200 (Convert8To4(color.g()) << 8) | 210 (Convert8To4(color.b()) << 4) | Convert8To4(color.a());
201 (Convert8To4(color.b()) << 4) | Convert8To4(color.a()); 211
212 std::memcpy(bytes, &data, sizeof(data));
202} 213}
203 214
204/** 215/**
@@ -207,7 +218,8 @@ inline void EncodeRGBA4(const Math::Vec4<u8>& color, u8* bytes) {
207 * @param bytes Pointer where to store the encoded value 218 * @param bytes Pointer where to store the encoded value
208 */ 219 */
209inline void EncodeD16(u32 value, u8* bytes) { 220inline void EncodeD16(u32 value, u8* bytes) {
210 *reinterpret_cast<u16_le*>(bytes) = value & 0xFFFF; 221 const u16_le data = static_cast<u16>(value);
222 std::memcpy(bytes, &data, sizeof(data));
211} 223}
212 224
213/** 225/**
diff --git a/src/common/vector_math.h b/src/common/vector_math.h
index 5c94fcda3..8feb49941 100644
--- a/src/common/vector_math.h
+++ b/src/common/vector_math.h
@@ -78,7 +78,7 @@ public:
78 } 78 }
79 79
80 template <typename U = T> 80 template <typename U = T>
81 constexpr Vec2<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { 81 constexpr Vec2<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const {
82 return {-x, -y}; 82 return {-x, -y};
83 } 83 }
84 constexpr Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const { 84 constexpr Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const {
@@ -227,7 +227,7 @@ public:
227 } 227 }
228 228
229 template <typename U = T> 229 template <typename U = T>
230 constexpr Vec3<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { 230 constexpr Vec3<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const {
231 return {-x, -y, -z}; 231 return {-x, -y, -z};
232 } 232 }
233 233
@@ -436,7 +436,7 @@ public:
436 } 436 }
437 437
438 template <typename U = T> 438 template <typename U = T>
439 constexpr Vec4<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { 439 constexpr Vec4<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const {
440 return {-x, -y, -z, -w}; 440 return {-x, -y, -z, -w};
441 } 441 }
442 442