diff options
| author | 2015-05-24 12:20:31 -0700 | |
|---|---|---|
| committer | 2015-05-30 11:17:37 -0700 | |
| commit | 76690392bf8923d7172936836d15e3caebb26cf0 (patch) | |
| tree | e5896ecb4f042e88a650f4ad2287393e2346c094 /src/common/color.h | |
| parent | Move video_core/math.h to common/vector_math.h (diff) | |
| download | yuzu-76690392bf8923d7172936836d15e3caebb26cf0.tar.gz yuzu-76690392bf8923d7172936836d15e3caebb26cf0.tar.xz yuzu-76690392bf8923d7172936836d15e3caebb26cf0.zip | |
Move video_core/color.h to common/color.h
Diffstat (limited to 'src/common/color.h')
| -rw-r--r-- | src/common/color.h | 214 |
1 files changed, 214 insertions, 0 deletions
diff --git a/src/common/color.h b/src/common/color.h new file mode 100644 index 000000000..422fdc8af --- /dev/null +++ b/src/common/color.h | |||
| @@ -0,0 +1,214 @@ | |||
| 1 | // Copyright 2014 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "common/common_types.h" | ||
| 8 | #include "common/swap.h" | ||
| 9 | #include "common/vector_math.h" | ||
| 10 | |||
| 11 | namespace Color { | ||
| 12 | |||
| 13 | /// Convert a 1-bit color component to 8 bit | ||
| 14 | inline u8 Convert1To8(u8 value) { | ||
| 15 | return value * 255; | ||
| 16 | } | ||
| 17 | |||
| 18 | /// Convert a 4-bit color component to 8 bit | ||
| 19 | inline u8 Convert4To8(u8 value) { | ||
| 20 | return (value << 4) | value; | ||
| 21 | } | ||
| 22 | |||
| 23 | /// Convert a 5-bit color component to 8 bit | ||
| 24 | inline u8 Convert5To8(u8 value) { | ||
| 25 | return (value << 3) | (value >> 2); | ||
| 26 | } | ||
| 27 | |||
| 28 | /// Convert a 6-bit color component to 8 bit | ||
| 29 | inline u8 Convert6To8(u8 value) { | ||
| 30 | return (value << 2) | (value >> 4); | ||
| 31 | } | ||
| 32 | |||
| 33 | /// Convert a 8-bit color component to 1 bit | ||
| 34 | inline u8 Convert8To1(u8 value) { | ||
| 35 | return value >> 7; | ||
| 36 | } | ||
| 37 | |||
| 38 | /// Convert a 8-bit color component to 4 bit | ||
| 39 | inline u8 Convert8To4(u8 value) { | ||
| 40 | return value >> 4; | ||
| 41 | } | ||
| 42 | |||
| 43 | /// Convert a 8-bit color component to 5 bit | ||
| 44 | inline u8 Convert8To5(u8 value) { | ||
| 45 | return value >> 3; | ||
| 46 | } | ||
| 47 | |||
| 48 | /// Convert a 8-bit color component to 6 bit | ||
| 49 | inline u8 Convert8To6(u8 value) { | ||
| 50 | return value >> 2; | ||
| 51 | } | ||
| 52 | |||
| 53 | /** | ||
| 54 | * Decode a color stored in RGBA8 format | ||
| 55 | * @param bytes Pointer to encoded source color | ||
| 56 | * @return Result color decoded as Math::Vec4<u8> | ||
| 57 | */ | ||
| 58 | inline const Math::Vec4<u8> DecodeRGBA8(const u8* bytes) { | ||
| 59 | return { bytes[3], bytes[2], bytes[1], bytes[0] }; | ||
| 60 | } | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Decode a color stored in RGB8 format | ||
| 64 | * @param bytes Pointer to encoded source color | ||
| 65 | * @return Result color decoded as Math::Vec4<u8> | ||
| 66 | */ | ||
| 67 | inline const Math::Vec4<u8> DecodeRGB8(const u8* bytes) { | ||
| 68 | return { bytes[2], bytes[1], bytes[0], 255 }; | ||
| 69 | } | ||
| 70 | |||
| 71 | /** | ||
| 72 | * Decode a color stored in RGB565 format | ||
| 73 | * @param bytes Pointer to encoded source color | ||
| 74 | * @return Result color decoded as Math::Vec4<u8> | ||
| 75 | */ | ||
| 76 | inline const Math::Vec4<u8> DecodeRGB565(const u8* bytes) { | ||
| 77 | const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes); | ||
| 78 | return { Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F), | ||
| 79 | Convert5To8(pixel & 0x1F), 255 }; | ||
| 80 | } | ||
| 81 | |||
| 82 | /** | ||
| 83 | * Decode a color stored in RGB5A1 format | ||
| 84 | * @param bytes Pointer to encoded source color | ||
| 85 | * @return Result color decoded as Math::Vec4<u8> | ||
| 86 | */ | ||
| 87 | inline const Math::Vec4<u8> DecodeRGB5A1(const u8* bytes) { | ||
| 88 | const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes); | ||
| 89 | return { Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F), | ||
| 90 | Convert5To8((pixel >> 1) & 0x1F), Convert1To8(pixel & 0x1) }; | ||
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Decode a color stored in RGBA4 format | ||
| 95 | * @param bytes Pointer to encoded source color | ||
| 96 | * @return Result color decoded as Math::Vec4<u8> | ||
| 97 | */ | ||
| 98 | inline const Math::Vec4<u8> DecodeRGBA4(const u8* bytes) { | ||
| 99 | const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes); | ||
| 100 | return { Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF), | ||
| 101 | Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF) }; | ||
| 102 | } | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Decode a depth value stored in D16 format | ||
| 106 | * @param bytes Pointer to encoded source value | ||
| 107 | * @return Depth value as an u32 | ||
| 108 | */ | ||
| 109 | inline u32 DecodeD16(const u8* bytes) { | ||
| 110 | return *reinterpret_cast<const u16_le*>(bytes); | ||
| 111 | } | ||
| 112 | |||
| 113 | /** | ||
| 114 | * Decode a depth value stored in D24 format | ||
| 115 | * @param bytes Pointer to encoded source value | ||
| 116 | * @return Depth value as an u32 | ||
| 117 | */ | ||
| 118 | inline u32 DecodeD24(const u8* bytes) { | ||
| 119 | return (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; | ||
| 120 | } | ||
| 121 | |||
| 122 | /** | ||
| 123 | * Decode a depth value and a stencil value stored in D24S8 format | ||
| 124 | * @param bytes Pointer to encoded source values | ||
| 125 | * @return Resulting values stored as a Math::Vec2 | ||
| 126 | */ | ||
| 127 | inline const Math::Vec2<u32> DecodeD24S8(const u8* bytes) { | ||
| 128 | return { static_cast<u32>((bytes[2] << 16) | (bytes[1] << 8) | bytes[0]), bytes[3] }; | ||
| 129 | } | ||
| 130 | |||
| 131 | /** | ||
| 132 | * Encode a color as RGBA8 format | ||
| 133 | * @param color Source color to encode | ||
| 134 | * @param bytes Destination pointer to store encoded color | ||
| 135 | */ | ||
| 136 | inline void EncodeRGBA8(const Math::Vec4<u8>& color, u8* bytes) { | ||
| 137 | bytes[3] = color.r(); | ||
| 138 | bytes[2] = color.g(); | ||
| 139 | bytes[1] = color.b(); | ||
| 140 | bytes[0] = color.a(); | ||
| 141 | } | ||
| 142 | |||
| 143 | /** | ||
| 144 | * Encode a color as RGB8 format | ||
| 145 | * @param color Source color to encode | ||
| 146 | * @param bytes Destination pointer to store encoded color | ||
| 147 | */ | ||
| 148 | inline void EncodeRGB8(const Math::Vec4<u8>& color, u8* bytes) { | ||
| 149 | bytes[2] = color.r(); | ||
| 150 | bytes[1] = color.g(); | ||
| 151 | bytes[0] = color.b(); | ||
| 152 | } | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Encode a color as RGB565 format | ||
| 156 | * @param color Source color to encode | ||
| 157 | * @param bytes Destination pointer to store encoded color | ||
| 158 | */ | ||
| 159 | inline void EncodeRGB565(const Math::Vec4<u8>& color, u8* bytes) { | ||
| 160 | *reinterpret_cast<u16_le*>(bytes) = (Convert8To5(color.r()) << 11) | | ||
| 161 | (Convert8To6(color.g()) << 5) | Convert8To5(color.b()); | ||
| 162 | } | ||
| 163 | |||
| 164 | /** | ||
| 165 | * Encode a color as RGB5A1 format | ||
| 166 | * @param color Source color to encode | ||
| 167 | * @param bytes Destination pointer to store encoded color | ||
| 168 | */ | ||
| 169 | inline void EncodeRGB5A1(const Math::Vec4<u8>& color, u8* bytes) { | ||
| 170 | *reinterpret_cast<u16_le*>(bytes) = (Convert8To5(color.r()) << 11) | | ||
| 171 | (Convert8To5(color.g()) << 6) | (Convert8To5(color.b()) << 1) | Convert8To1(color.a()); | ||
| 172 | } | ||
| 173 | |||
| 174 | /** | ||
| 175 | * Encode a color as RGBA4 format | ||
| 176 | * @param color Source color to encode | ||
| 177 | * @param bytes Destination pointer to store encoded color | ||
| 178 | */ | ||
| 179 | inline void EncodeRGBA4(const Math::Vec4<u8>& color, u8* bytes) { | ||
| 180 | *reinterpret_cast<u16_le*>(bytes) = (Convert8To4(color.r()) << 12) | | ||
| 181 | (Convert8To4(color.g()) << 8) | (Convert8To4(color.b()) << 4) | Convert8To4(color.a()); | ||
| 182 | } | ||
| 183 | |||
| 184 | /** | ||
| 185 | * Encode a 16 bit depth value as D16 format | ||
| 186 | * @param value 16 bit source depth value to encode | ||
| 187 | * @param bytes Pointer where to store the encoded value | ||
| 188 | */ | ||
| 189 | inline void EncodeD16(u32 value, u8* bytes) { | ||
| 190 | *reinterpret_cast<u16_le*>(bytes) = value & 0xFFFF; | ||
| 191 | } | ||
| 192 | |||
| 193 | /** | ||
| 194 | * Encode a 24 bit depth value as D24 format | ||
| 195 | * @param value 24 bit source depth value to encode | ||
| 196 | * @param bytes Pointer where to store the encoded value | ||
| 197 | */ | ||
| 198 | inline void EncodeD24(u32 value, u8* bytes) { | ||
| 199 | bytes[0] = value & 0xFF; | ||
| 200 | bytes[1] = (value >> 8) & 0xFF; | ||
| 201 | bytes[2] = (value >> 16) & 0xFF; | ||
| 202 | } | ||
| 203 | |||
| 204 | /** | ||
| 205 | * Encode a 24 bit depth and 8 bit stencil values as D24S8 format | ||
| 206 | * @param depth 24 bit source depth value to encode | ||
| 207 | * @param stencil 8 bit source stencil value to encode | ||
| 208 | * @param bytes Pointer where to store the encoded value | ||
| 209 | */ | ||
| 210 | inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) { | ||
| 211 | *reinterpret_cast<u32_le*>(bytes) = (stencil << 24) | depth; | ||
| 212 | } | ||
| 213 | |||
| 214 | } // namespace | ||