Make ctrff usable again

This commit is contained in:
2025-12-06 21:34:19 +01:00
parent b021609c4c
commit bfa6c9e92a
15 changed files with 97 additions and 91 deletions

View File

@@ -2,31 +2,33 @@
#include <cwchar>
#include <iostream>
void MakePixelRGBA(PD::u8 &r, PD::u8 &g, PD::u8 &b, PD::u8 &a, PD::u16 px) {
void MakePixelRGBA(ctrff::u8 &r, ctrff::u8 &g, ctrff::u8 &b, ctrff::u8 &a,
ctrff::u16 px) {
b = (px & 0x1f) << 3;
g = ((px >> 0x5) & 0x3f) << 2;
r = ((px >> 0xb) & 0x1f) << 3;
a = 0xff;
}
CTRFF_API PD::u16 MakePixel565(const PD::u8 &r, const PD::u8 &g,
const PD::u8 &b) {
PD::u16 res = 0;
CTRFF_API ctrff::u16 MakePixel565(const ctrff::u8 &r, const ctrff::u8 &g,
const ctrff::u8 &b) {
ctrff::u16 res = 0;
res |= (r & ~0x7) << 8;
res |= (g & ~0x3) << 3;
res |= (b) >> 3;
return res;
}
CTRFF_API PD::u32 TileIndex(const int &x, const int &y, const int &w) {
CTRFF_API ctrff::u32 TileIndex(const int &x, const int &y, const int &w) {
return (((y >> 3) * (w >> 3) + (x >> 3)) << 6) +
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) |
((x & 4) << 2) | ((y & 4) << 3));
}
// TODO: Fix colors
CTRFF_API void ctrff::RGB565toRGBA(std::vector<PD::u8> &img, PD::u16 *icon,
const int &w, const int &h) {
CTRFF_API void ctrff::RGB565toRGBA(std::vector<ctrff::u8> &img,
ctrff::u16 *icon, const int &w,
const int &h) {
if (img.size() != (48 * 48 * 4)) {
img.resize(48 * 48 * 4);
img.clear();
@@ -34,36 +36,37 @@ CTRFF_API void ctrff::RGB565toRGBA(std::vector<PD::u8> &img, PD::u16 *icon,
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
auto idx = TileIndex(x, y, w);
PD::u32 pos = (y * w + x) * 4;
ctrff::u32 pos = (y * w + x) * 4;
MakePixelRGBA(img[pos + 0], img[pos + 1], img[pos + 2], img[pos + 3],
icon[idx]);
}
}
}
CTRFF_API void ctrff::RGBA2RGB565(PD::u16 *out, const std::vector<PD::u8> &img,
CTRFF_API void ctrff::RGBA2RGB565(ctrff::u16 *out,
const std::vector<ctrff::u8> &img,
const int &w, const int &h) {
if (img.size() != size_t(w * h * 4)) return;
std::vector<PD::u8> px8 = img;
std::vector<ctrff::u8> px8 = img;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
auto idx = TileIndex(x, y, w);
PD::u32 pos = (y * w + x) * 4;
ctrff::u32 pos = (y * w + x) * 4;
out[idx] = MakePixel565(img[pos], img[pos + 1], img[pos + 2]);
}
}
}
CTRFF_API std::vector<PD::u8> ctrff::DownscaleImage(
const std::vector<PD::u8> &img, int w, int h, int scale) {
std::vector<PD::u8> res(((w / scale) * (h / scale)) * 4);
CTRFF_API std::vector<ctrff::u8> ctrff::DownscaleImage(
const std::vector<ctrff::u8> &img, int w, int h, int scale) {
std::vector<ctrff::u8> res(((w / scale) * (h / scale)) * 4);
int samples = scale * scale;
for (int y = 0; y < h; y += scale) {
for (int x = 0; x < w; x += scale) {
PD::u32 r = 0;
PD::u32 g = 0;
PD::u32 b = 0;
PD::u32 a = 0;
ctrff::u32 r = 0;
ctrff::u32 g = 0;
ctrff::u32 b = 0;
ctrff::u32 a = 0;
for (int oy = 0; oy < scale; oy++) {
for (int ox = 0; ox < scale; ox++) {
int pos = ((y + oy) * w + (x + ox)) * 4;
@@ -74,16 +77,16 @@ CTRFF_API std::vector<PD::u8> ctrff::DownscaleImage(
}
}
int pos = ((y / scale) * (w / scale) + (x / scale)) * 4;
res[pos++] = (PD::u8)(r / samples);
res[pos++] = (PD::u8)(g / samples);
res[pos++] = (PD::u8)(b / samples);
res[pos++] = (PD::u8)(a / samples);
res[pos++] = (ctrff::u8)(r / samples);
res[pos++] = (ctrff::u8)(g / samples);
res[pos++] = (ctrff::u8)(b / samples);
res[pos++] = (ctrff::u8)(a / samples);
}
}
return res;
}
CTRFF_API void ctrff::String2U16(PD::u16 *res, const std::string &src,
CTRFF_API void ctrff::String2U16(ctrff::u16 *res, const std::string &src,
size_t max) {
/// GOT FORCED TO REPLACE std::wstring_convert by some
/// manual work as it got removed in cxx20
@@ -93,7 +96,7 @@ CTRFF_API void ctrff::String2U16(PD::u16 *res, const std::string &src,
size_t len = 0;
size_t i = 0;
while (i < src.size() && len < max) {
PD::u8 c = src[i];
ctrff::u8 c = src[i];
if (c < 0x80) {
// 1byte
@@ -116,8 +119,8 @@ CTRFF_API void ctrff::String2U16(PD::u16 *res, const std::string &src,
// 4byte
if (i + 3 >= src.size())
throw std::invalid_argument("Invalid UTF-8 sequence");
PD::u32 codepoint = ((c & 0x07) << 18) | ((src[i + 1] & 0x3F) << 12) |
((src[i + 2] & 0x3F) << 6) | (src[i + 3] & 0x3F);
ctrff::u32 codepoint = ((c & 0x07) << 18) | ((src[i + 1] & 0x3F) << 12) |
((src[i + 2] & 0x3F) << 6) | (src[i + 3] & 0x3F);
codepoint -= 0x10000;
res[len++] = 0xD800 | ((codepoint >> 10) & 0x3FF);
res[len++] = 0xDC00 | (codepoint & 0x3FF);
@@ -128,7 +131,7 @@ CTRFF_API void ctrff::String2U16(PD::u16 *res, const std::string &src,
}
}
CTRFF_API std::string ctrff::U16toU8(PD::u16 *in, size_t max) {
CTRFF_API std::string ctrff::U16toU8(ctrff::u16 *in, size_t max) {
/// GOT FORCED TO REPLACE std::wstring_convert by some
/// manual work as it got removed in cxx20
if (!in || max == 0) {