| 1 | // Copyright 2009 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/ColorUtil.h" |
| 5 | |
| 6 | #include "Common/Swap.h" |
| 7 | |
| 8 | namespace Common |
| 9 | { |
| 10 | static const int s_lut5to8[] = {0x00, 0x08, 0x10, 0x18, 0x20, 0x29, 0x31, 0x39, 0x41, 0x4A, 0x52, |
| 11 | 0x5A, 0x62, 0x6A, 0x73, 0x7B, 0x83, 0x8B, 0x94, 0x9C, 0xA4, 0xAC, |
| 12 | 0xB4, 0xBD, 0xC5, 0xCD, 0xD5, 0xDE, 0xE6, 0xEE, 0xF6, 0xFF}; |
| 13 | |
| 14 | static const int s_lut4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 15 | 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; |
| 16 | |
| 17 | static const int s_lut3to8[] = {0x00, 0x24, 0x48, 0x6D, 0x91, 0xB6, 0xDA, 0xFF}; |
| 18 | |
| 19 | static u32 Decode5A3(u16 val) |
| 20 | { |
| 21 | const u32 bg_color = 0x00000000; |
| 22 | |
| 23 | int r, g, b, a; |
| 24 | |
| 25 | if (val & 0x8000) |
| 26 | { |
| 27 | r = s_lut5to8[(val >> 10) & 0x1f]; |
| 28 | g = s_lut5to8[(val >> 5) & 0x1f]; |
| 29 | b = s_lut5to8[(val) & 0x1f]; |
| 30 | a = 0xFF; |
| 31 | } |
| 32 | else |
| 33 | { |
| 34 | a = s_lut3to8[(val >> 12) & 0x7]; |
| 35 | r = (s_lut4to8[(val >> 8) & 0xf] * a + (bg_color & 0xFF) * (255 - a)) / 255; |
| 36 | g = (s_lut4to8[(val >> 4) & 0xf] * a + ((bg_color >> 8) & 0xFF) * (255 - a)) / 255; |
| 37 | b = (s_lut4to8[(val) & 0xf] * a + ((bg_color >> 16) & 0xFF) * (255 - a)) / 255; |
| 38 | a = 0xFF; |
| 39 | } |
| 40 | return (a << 24) | (r << 16) | (g << 8) | b; |
| 41 | } |
| 42 | |
| 43 | void Decode5A3Image(u32* dst, const u16* src, int width, int height) |
| 44 | { |
| 45 | for (int y = 0; y < height; y += 4) |
| 46 | { |
| 47 | for (int x = 0; x < width; x += 4) |
| 48 | { |
| 49 | for (int iy = 0; iy < 4; iy++, src += 4) |
| 50 | { |
| 51 | for (int ix = 0; ix < 4; ix++) |
| 52 | { |
| 53 | u32 RGBA = Decode5A3(Common::swap16(src[ix])); |
| 54 | dst[(y + iy) * width + (x + ix)] = RGBA; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void DecodeCI8Image(u32* dst, const u8* src, const u16* pal, int width, int height) |
| 62 | { |
| 63 | for (int y = 0; y < height; y += 4) |
| 64 | { |
| 65 | for (int x = 0; x < width; x += 8) |
| 66 | { |
| 67 | for (int iy = 0; iy < 4; iy++, src += 8) |
| 68 | { |
| 69 | u32* tdst = dst + (y + iy) * width + x; |
| 70 | for (int ix = 0; ix < 8; ix++) |
| 71 | { |
| 72 | // huh, this seems wrong. CI8, not 5A3, no? |
| 73 | tdst[ix] = Decode5A3(Common::swap16(pal[src[ix]])); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } // namespace Common |