2014-12-31 17:17:07 +03:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-08-08 23:00:07 +03:00
|
|
|
#include <cstring>
|
|
|
|
|
2014-12-31 17:17:07 +03:00
|
|
|
#include "common/common_types.h"
|
2015-05-06 10:06:12 +03:00
|
|
|
#include "common/swap.h"
|
2015-05-24 22:16:22 +03:00
|
|
|
#include "common/vector_math.h"
|
2014-12-31 17:17:07 +03:00
|
|
|
|
|
|
|
namespace Color {
|
|
|
|
|
|
|
|
/// Convert a 1-bit color component to 8 bit
|
2016-01-28 06:14:17 +03:00
|
|
|
constexpr u8 Convert1To8(u8 value) {
|
2014-12-31 17:17:07 +03:00
|
|
|
return value * 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a 4-bit color component to 8 bit
|
2016-01-28 06:14:17 +03:00
|
|
|
constexpr u8 Convert4To8(u8 value) {
|
2014-12-31 17:17:07 +03:00
|
|
|
return (value << 4) | value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a 5-bit color component to 8 bit
|
2016-01-28 06:14:17 +03:00
|
|
|
constexpr u8 Convert5To8(u8 value) {
|
2014-12-31 17:17:07 +03:00
|
|
|
return (value << 3) | (value >> 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a 6-bit color component to 8 bit
|
2016-01-28 06:14:17 +03:00
|
|
|
constexpr u8 Convert6To8(u8 value) {
|
2014-12-31 17:17:07 +03:00
|
|
|
return (value << 2) | (value >> 4);
|
|
|
|
}
|
|
|
|
|
2015-02-27 06:11:39 +03:00
|
|
|
/// Convert a 8-bit color component to 1 bit
|
2016-01-28 06:14:17 +03:00
|
|
|
constexpr u8 Convert8To1(u8 value) {
|
2015-02-27 06:11:39 +03:00
|
|
|
return value >> 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a 8-bit color component to 4 bit
|
2016-01-28 06:14:17 +03:00
|
|
|
constexpr u8 Convert8To4(u8 value) {
|
2015-02-27 06:11:39 +03:00
|
|
|
return value >> 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a 8-bit color component to 5 bit
|
2016-01-28 06:14:17 +03:00
|
|
|
constexpr u8 Convert8To5(u8 value) {
|
2015-02-27 06:11:39 +03:00
|
|
|
return value >> 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a 8-bit color component to 6 bit
|
2016-01-28 06:14:17 +03:00
|
|
|
constexpr u8 Convert8To6(u8 value) {
|
2015-02-27 06:11:39 +03:00
|
|
|
return value >> 2;
|
|
|
|
}
|
2014-12-31 17:17:07 +03:00
|
|
|
|
2015-02-26 06:44:55 +03:00
|
|
|
/**
|
|
|
|
* Decode a color stored in RGBA8 format
|
|
|
|
* @param bytes Pointer to encoded source color
|
|
|
|
* @return Result color decoded as Math::Vec4<u8>
|
|
|
|
*/
|
2018-08-08 23:17:38 +03:00
|
|
|
inline Math::Vec4<u8> DecodeRGBA8(const u8* bytes) {
|
2016-09-18 03:38:01 +03:00
|
|
|
return {bytes[3], bytes[2], bytes[1], bytes[0]};
|
2015-02-26 06:44:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a color stored in RGB8 format
|
|
|
|
* @param bytes Pointer to encoded source color
|
|
|
|
* @return Result color decoded as Math::Vec4<u8>
|
|
|
|
*/
|
2018-08-08 23:17:38 +03:00
|
|
|
inline Math::Vec4<u8> DecodeRGB8(const u8* bytes) {
|
2016-09-18 03:38:01 +03:00
|
|
|
return {bytes[2], bytes[1], bytes[0], 255};
|
2015-02-26 06:44:55 +03:00
|
|
|
}
|
|
|
|
|
2015-05-14 23:39:46 +03:00
|
|
|
/**
|
|
|
|
* Decode a color stored in RG8 (aka HILO8) format
|
|
|
|
* @param bytes Pointer to encoded source color
|
|
|
|
* @return Result color decoded as Math::Vec4<u8>
|
|
|
|
*/
|
2018-08-08 23:17:38 +03:00
|
|
|
inline Math::Vec4<u8> DecodeRG8(const u8* bytes) {
|
2016-09-18 03:38:01 +03:00
|
|
|
return {bytes[1], bytes[0], 0, 255};
|
2015-05-14 23:39:46 +03:00
|
|
|
}
|
|
|
|
|
2015-02-26 06:44:55 +03:00
|
|
|
/**
|
|
|
|
* Decode a color stored in RGB565 format
|
|
|
|
* @param bytes Pointer to encoded source color
|
|
|
|
* @return Result color decoded as Math::Vec4<u8>
|
|
|
|
*/
|
2018-08-08 23:17:38 +03:00
|
|
|
inline Math::Vec4<u8> DecodeRGB565(const u8* bytes) {
|
2018-08-08 23:00:07 +03:00
|
|
|
u16_le pixel;
|
|
|
|
std::memcpy(&pixel, bytes, sizeof(pixel));
|
2016-09-18 03:38:01 +03:00
|
|
|
return {Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F),
|
|
|
|
Convert5To8(pixel & 0x1F), 255};
|
2015-02-26 06:44:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a color stored in RGB5A1 format
|
|
|
|
* @param bytes Pointer to encoded source color
|
|
|
|
* @return Result color decoded as Math::Vec4<u8>
|
|
|
|
*/
|
2018-08-08 23:17:38 +03:00
|
|
|
inline Math::Vec4<u8> DecodeRGB5A1(const u8* bytes) {
|
2018-08-08 23:00:07 +03:00
|
|
|
u16_le pixel;
|
|
|
|
std::memcpy(&pixel, bytes, sizeof(pixel));
|
2016-09-18 03:38:01 +03:00
|
|
|
return {Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F),
|
|
|
|
Convert5To8((pixel >> 1) & 0x1F), Convert1To8(pixel & 0x1)};
|
2015-02-26 06:44:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a color stored in RGBA4 format
|
|
|
|
* @param bytes Pointer to encoded source color
|
|
|
|
* @return Result color decoded as Math::Vec4<u8>
|
|
|
|
*/
|
2018-08-08 23:17:38 +03:00
|
|
|
inline Math::Vec4<u8> DecodeRGBA4(const u8* bytes) {
|
2018-08-08 23:00:07 +03:00
|
|
|
u16_le pixel;
|
|
|
|
std::memcpy(&pixel, bytes, sizeof(pixel));
|
2016-09-18 03:38:01 +03:00
|
|
|
return {Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF),
|
|
|
|
Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF)};
|
2015-02-26 06:44:55 +03:00
|
|
|
}
|
|
|
|
|
2015-03-08 20:05:17 +03:00
|
|
|
/**
|
|
|
|
* Decode a depth value stored in D16 format
|
|
|
|
* @param bytes Pointer to encoded source value
|
|
|
|
* @return Depth value as an u32
|
|
|
|
*/
|
2015-03-09 05:31:15 +03:00
|
|
|
inline u32 DecodeD16(const u8* bytes) {
|
2018-08-08 23:00:07 +03:00
|
|
|
u16_le data;
|
|
|
|
std::memcpy(&data, bytes, sizeof(data));
|
|
|
|
return data;
|
2015-03-08 20:05:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a depth value stored in D24 format
|
|
|
|
* @param bytes Pointer to encoded source value
|
|
|
|
* @return Depth value as an u32
|
|
|
|
*/
|
2015-03-09 05:31:15 +03:00
|
|
|
inline u32 DecodeD24(const u8* bytes) {
|
2015-03-08 20:05:17 +03:00
|
|
|
return (bytes[2] << 16) | (bytes[1] << 8) | bytes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a depth value and a stencil value stored in D24S8 format
|
|
|
|
* @param bytes Pointer to encoded source values
|
|
|
|
* @return Resulting values stored as a Math::Vec2
|
|
|
|
*/
|
2018-08-08 23:17:38 +03:00
|
|
|
inline Math::Vec2<u32> DecodeD24S8(const u8* bytes) {
|
2016-09-18 03:38:01 +03:00
|
|
|
return {static_cast<u32>((bytes[2] << 16) | (bytes[1] << 8) | bytes[0]), bytes[3]};
|
2015-03-08 20:05:17 +03:00
|
|
|
}
|
|
|
|
|
2015-02-26 06:44:55 +03:00
|
|
|
/**
|
|
|
|
* Encode a color as RGBA8 format
|
|
|
|
* @param color Source color to encode
|
|
|
|
* @param bytes Destination pointer to store encoded color
|
|
|
|
*/
|
|
|
|
inline void EncodeRGBA8(const Math::Vec4<u8>& color, u8* bytes) {
|
|
|
|
bytes[3] = color.r();
|
|
|
|
bytes[2] = color.g();
|
|
|
|
bytes[1] = color.b();
|
|
|
|
bytes[0] = color.a();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode a color as RGB8 format
|
|
|
|
* @param color Source color to encode
|
|
|
|
* @param bytes Destination pointer to store encoded color
|
|
|
|
*/
|
|
|
|
inline void EncodeRGB8(const Math::Vec4<u8>& color, u8* bytes) {
|
|
|
|
bytes[2] = color.r();
|
|
|
|
bytes[1] = color.g();
|
|
|
|
bytes[0] = color.b();
|
|
|
|
}
|
|
|
|
|
2015-05-14 23:39:46 +03:00
|
|
|
/**
|
|
|
|
* Encode a color as RG8 (aka HILO8) format
|
|
|
|
* @param color Source color to encode
|
|
|
|
* @param bytes Destination pointer to store encoded color
|
|
|
|
*/
|
|
|
|
inline void EncodeRG8(const Math::Vec4<u8>& color, u8* bytes) {
|
|
|
|
bytes[1] = color.r();
|
|
|
|
bytes[0] = color.g();
|
|
|
|
}
|
2015-02-26 06:44:55 +03:00
|
|
|
/**
|
|
|
|
* Encode a color as RGB565 format
|
|
|
|
* @param color Source color to encode
|
|
|
|
* @param bytes Destination pointer to store encoded color
|
|
|
|
*/
|
|
|
|
inline void EncodeRGB565(const Math::Vec4<u8>& color, u8* bytes) {
|
2018-08-08 23:00:07 +03:00
|
|
|
const u16_le data =
|
2016-09-18 03:38:01 +03:00
|
|
|
(Convert8To5(color.r()) << 11) | (Convert8To6(color.g()) << 5) | Convert8To5(color.b());
|
2018-08-08 23:00:07 +03:00
|
|
|
|
|
|
|
std::memcpy(bytes, &data, sizeof(data));
|
2015-02-26 06:44:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode a color as RGB5A1 format
|
|
|
|
* @param color Source color to encode
|
|
|
|
* @param bytes Destination pointer to store encoded color
|
|
|
|
*/
|
|
|
|
inline void EncodeRGB5A1(const Math::Vec4<u8>& color, u8* bytes) {
|
2018-08-08 23:00:07 +03:00
|
|
|
const u16_le data = (Convert8To5(color.r()) << 11) | (Convert8To5(color.g()) << 6) |
|
|
|
|
(Convert8To5(color.b()) << 1) | Convert8To1(color.a());
|
|
|
|
|
|
|
|
std::memcpy(bytes, &data, sizeof(data));
|
2015-02-26 06:44:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode a color as RGBA4 format
|
|
|
|
* @param color Source color to encode
|
|
|
|
* @param bytes Destination pointer to store encoded color
|
|
|
|
*/
|
|
|
|
inline void EncodeRGBA4(const Math::Vec4<u8>& color, u8* bytes) {
|
2018-08-08 23:00:07 +03:00
|
|
|
const u16 data = (Convert8To4(color.r()) << 12) | (Convert8To4(color.g()) << 8) |
|
|
|
|
(Convert8To4(color.b()) << 4) | Convert8To4(color.a());
|
|
|
|
|
|
|
|
std::memcpy(bytes, &data, sizeof(data));
|
2015-02-26 06:44:55 +03:00
|
|
|
}
|
|
|
|
|
2015-03-08 20:05:17 +03:00
|
|
|
/**
|
2015-03-09 05:31:15 +03:00
|
|
|
* Encode a 16 bit depth value as D16 format
|
|
|
|
* @param value 16 bit source depth value to encode
|
2015-03-08 20:05:17 +03:00
|
|
|
* @param bytes Pointer where to store the encoded value
|
|
|
|
*/
|
|
|
|
inline void EncodeD16(u32 value, u8* bytes) {
|
2018-08-08 23:00:07 +03:00
|
|
|
const u16_le data = static_cast<u16>(value);
|
|
|
|
std::memcpy(bytes, &data, sizeof(data));
|
2015-03-08 20:05:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-09 05:31:15 +03:00
|
|
|
* Encode a 24 bit depth value as D24 format
|
|
|
|
* @param value 24 bit source depth value to encode
|
2015-03-08 20:05:17 +03:00
|
|
|
* @param bytes Pointer where to store the encoded value
|
|
|
|
*/
|
|
|
|
inline void EncodeD24(u32 value, u8* bytes) {
|
|
|
|
bytes[0] = value & 0xFF;
|
|
|
|
bytes[1] = (value >> 8) & 0xFF;
|
|
|
|
bytes[2] = (value >> 16) & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-09 05:31:15 +03:00
|
|
|
* Encode a 24 bit depth and 8 bit stencil values as D24S8 format
|
|
|
|
* @param depth 24 bit source depth value to encode
|
|
|
|
* @param stencil 8 bit source stencil value to encode
|
2015-03-08 20:05:17 +03:00
|
|
|
* @param bytes Pointer where to store the encoded value
|
|
|
|
*/
|
|
|
|
inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) {
|
2015-05-21 17:51:31 +03:00
|
|
|
bytes[0] = depth & 0xFF;
|
|
|
|
bytes[1] = (depth >> 8) & 0xFF;
|
|
|
|
bytes[2] = (depth >> 16) & 0xFF;
|
|
|
|
bytes[3] = stencil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode a 24 bit depth value as D24X8 format (32 bits per pixel with 8 bits unused)
|
|
|
|
* @param depth 24 bit source depth value to encode
|
|
|
|
* @param bytes Pointer where to store the encoded value
|
|
|
|
* @note unused bits will not be modified
|
|
|
|
*/
|
|
|
|
inline void EncodeD24X8(u32 depth, u8* bytes) {
|
|
|
|
bytes[0] = depth & 0xFF;
|
|
|
|
bytes[1] = (depth >> 8) & 0xFF;
|
|
|
|
bytes[2] = (depth >> 16) & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode an 8 bit stencil value as X24S8 format (32 bits per pixel with 24 bits unused)
|
|
|
|
* @param stencil 8 bit source stencil value to encode
|
|
|
|
* @param bytes Pointer where to store the encoded value
|
|
|
|
* @note unused bits will not be modified
|
|
|
|
*/
|
|
|
|
inline void EncodeX24S8(u8 stencil, u8* bytes) {
|
|
|
|
bytes[3] = stencil;
|
2015-03-08 20:05:17 +03:00
|
|
|
}
|
|
|
|
|
2018-01-20 10:48:02 +03:00
|
|
|
} // namespace Color
|