SnesColor housekeeping

This commit is contained in:
scawful
2024-01-31 23:56:58 -05:00
parent 693dca1329
commit edbeac8517
4 changed files with 119 additions and 153 deletions

View File

@@ -4,6 +4,7 @@
#include <imgui/imgui.h>
#include <cstdint>
#include <vector>
namespace yaze {
namespace app {
@@ -55,6 +56,49 @@ SnesColor ReadColorFromRom(int offset, const uint8_t* rom) {
return snes_color;
}
std::vector<snes_color> Extract(const char* data, unsigned int offset,
unsigned int palette_size) {
std::vector<snes_color> palette(palette_size);
for (unsigned int i = 0; i < palette_size * 2; i += 2) {
uint16_t snes_color = (static_cast<uint8_t>(data[offset + i + 1]) << 8) |
static_cast<uint8_t>(data[offset + i]);
palette[i / 2] = ConvertSNEStoRGB(snes_color);
}
return palette;
}
std::vector<char> Convert(const std::vector<snes_color>& palette) {
std::vector<char> data(palette.size() * 2);
for (unsigned int i = 0; i < palette.size(); i++) {
uint16_t snes_data = ConvertRGBtoSNES(palette[i]);
data[i * 2] = snes_data & 0xFF;
data[i * 2 + 1] = snes_data >> 8;
}
return data;
}
SnesColor GetCgxColor(uint16_t color) {
ImVec4 rgb;
rgb.x = (color & 0x1F) * 8;
rgb.y = ((color & 0x3E0) >> 5) * 8;
rgb.z = ((color & 0x7C00) >> 10) * 8;
SnesColor toret;
toret.set_rgb(rgb);
return toret;
}
std::vector<SnesColor> GetColFileData(uint8_t* data) {
std::vector<SnesColor> colors;
colors.reserve(256);
colors.resize(256);
for (int i = 0; i < 512; i += 2) {
colors[i / 2] = GetCgxColor((uint16_t)((data[i + 1] << 8) + data[i]));
}
return colors;
}
} // namespace gfx
} // namespace app
} // namespace yaze

View File

@@ -4,6 +4,7 @@
#include <imgui/imgui.h>
#include <cstdint>
#include <vector>
namespace yaze {
namespace app {
@@ -20,67 +21,49 @@ snes_color ConvertSNEStoRGB(uint16_t snes_color);
uint16_t ConvertRGBtoSNES(const snes_color& color);
uint16_t ConvertRGBtoSNES(const ImVec4& color);
// class SnesColor {
// public:
// SnesColor() : rgb(0.f, 0.f, 0.f, 0.f), snes(0) {}
// SnesColor(const ImVec4 val) : rgb(val) {
// snes_color color;
// color.red = val.x / 255;
// color.green = val.y / 255;
// color.blue = val.z / 255;
// snes = ConvertRGBtoSNES(color);
// }
// SnesColor(const snes_color internal_format) {
// rgb.x = internal_format.red;
// rgb.y = internal_format.green;
// rgb.z = internal_format.blue;
// snes = ConvertRGBtoSNES(internal_format);
// }
std::vector<snes_color> Extract(const char* data, unsigned int offset,
unsigned int palette_size);
// ImVec4 rgb; /**< The color in RGB format. */
// uint16_t snes; /**< The color in SNES format. */
// bool modified = false;
// bool transparent = false;
// };
std::vector<char> Convert(const std::vector<snes_color>& palette);
class SnesColor {
public:
SnesColor() : rgb(0.f, 0.f, 0.f, 0.f), snes(0) {}
SnesColor() : rgb_(0.f, 0.f, 0.f, 0.f), snes_(0) {}
explicit SnesColor(const ImVec4 val) : rgb(val) {
explicit SnesColor(const ImVec4 val) : rgb_(val) {
snes_color color;
color.red = val.x / 255;
color.green = val.y / 255;
color.blue = val.z / 255;
snes = ConvertRGBtoSNES(color);
snes_ = ConvertRGBtoSNES(color);
}
explicit SnesColor(const snes_color val)
: rgb(val.red, val.green, val.blue, 255.f),
snes(ConvertRGBtoSNES(val)),
rom_color(val) {}
: rgb_(val.red, val.green, val.blue, 255.f),
snes_(ConvertRGBtoSNES(val)),
rom_color_(val) {}
ImVec4 GetRGB() const { return rgb; }
void SetRGB(const ImVec4 val) {
rgb.x = val.x / 255;
rgb.y = val.y / 255;
rgb.z = val.z / 255;
ImVec4 rgb() const { return rgb_; }
void set_rgb(const ImVec4 val) {
rgb_.x = val.x / 255;
rgb_.y = val.y / 255;
rgb_.z = val.z / 255;
snes_color color;
color.red = val.x;
color.green = val.y;
color.blue = val.z;
rom_color = color;
snes = ConvertRGBtoSNES(color);
rom_color_ = color;
snes_ = ConvertRGBtoSNES(color);
modified = true;
}
snes_color GetRomRGB() const { return rom_color; }
snes_color rom_color() const { return rom_color_; }
uint16_t GetSNES() const { return snes; }
void SetSNES(uint16_t val) {
snes = val;
uint16_t snes() const { return snes_; }
void set_snes(uint16_t val) {
snes_ = val;
snes_color col = ConvertSNEStoRGB(val);
rgb = ImVec4(col.red, col.green, col.blue, 0.f);
rgb_ = ImVec4(col.red, col.green, col.blue, 0.f);
modified = true;
}
@@ -90,15 +73,18 @@ class SnesColor {
void SetModified(bool m) { modified = m; }
private:
ImVec4 rgb;
uint16_t snes;
snes_color rom_color;
ImVec4 rgb_;
uint16_t snes_;
snes_color rom_color_;
bool modified = false;
bool transparent = false;
};
SnesColor ReadColorFromRom(int offset, const uint8_t* rom);
SnesColor GetCgxColor(uint16_t color);
std::vector<SnesColor> GetColFileData(uint8_t* data);
} // namespace gfx
} // namespace app
} // namespace yaze

View File

@@ -12,6 +12,7 @@
#include "absl/container/flat_hash_map.h" // for flat_hash_map
#include "absl/status/status.h" // for Status
#include "absl/status/statusor.h"
#include "app/core/constants.h"
#include "app/gfx/snes_color.h"
@@ -19,7 +20,6 @@ namespace yaze {
namespace app {
namespace gfx {
// Define a hash map to hold the addresses of different palette groups
const absl::flat_hash_map<std::string, uint32_t> kPaletteGroupAddressMap = {
{"ow_main", core::overworldPaletteMain},
{"ow_aux", core::overworldPaletteAuxialiary},
@@ -38,7 +38,6 @@ const absl::flat_hash_map<std::string, uint32_t> kPaletteGroupAddressMap = {
{"ow_mini_map", core::overworldMiniMapPalettes},
};
// Define a hash map to hold the number of colors in each palette group
const absl::flat_hash_map<std::string, uint32_t> kPaletteGroupColorCounts = {
{"ow_main", 35}, {"ow_aux", 21}, {"ow_animated", 7},
{"hud", 32}, {"global_sprites", 60}, {"armors", 15},
@@ -47,49 +46,19 @@ const absl::flat_hash_map<std::string, uint32_t> kPaletteGroupColorCounts = {
{"grass", 1}, {"3d_object", 8}, {"ow_mini_map", 128},
};
uint32_t GetPaletteAddress(const std::string& group_name, size_t palette_index,
size_t color_index) {
// Retrieve the base address for the palette group
uint32_t base_address = kPaletteGroupAddressMap.at(group_name);
std::vector<snes_color> Extract(const char* data, unsigned int offset,
unsigned int palette_size) {
std::vector<snes_color> palette(palette_size);
for (unsigned int i = 0; i < palette_size * 2; i += 2) {
uint16_t snes_color = (static_cast<uint8_t>(data[offset + i + 1]) << 8) |
static_cast<uint8_t>(data[offset + i]);
palette[i / 2] = ConvertSNEStoRGB(snes_color);
}
return palette;
}
// Retrieve the number of colors for each palette in the group
uint32_t colors_per_palette = kPaletteGroupColorCounts.at(group_name);
std::vector<char> Convert(const std::vector<snes_color>& palette) {
std::vector<char> data(palette.size() * 2);
for (unsigned int i = 0; i < palette.size(); i++) {
uint16_t snes_data = ConvertRGBtoSNES(palette[i]);
data[i * 2] = snes_data & 0xFF;
data[i * 2 + 1] = snes_data >> 8;
}
return data;
}
// Calculate the address for thes specified color in the ROM
uint32_t address = base_address + (palette_index * colors_per_palette * 2) +
(color_index * 2);
SnesColor GetCgxColor(uint16_t color) {
ImVec4 rgb;
rgb.x = (color & 0x1F) * 8;
rgb.y = ((color & 0x3E0) >> 5) * 8;
rgb.z = ((color & 0x7C00) >> 10) * 8;
SnesColor toret;
toret.SetRGB(rgb);
return toret;
}
std::vector<SnesColor> GetColFileData(uchar* data) {
std::vector<SnesColor> colors;
colors.reserve(256);
colors.resize(256);
for (int i = 0; i < 512; i += 2) {
colors[i / 2] = GetCgxColor((uint16_t)((data[i + 1] << 8) + data[i]));
}
return colors;
return address;
}
// ============================================================================
@@ -106,10 +75,10 @@ SnesPalette::SnesPalette(char* data) : size_(sizeof(data) / 2) {
assert((sizeof(data) % 4 == 0) && (sizeof(data) <= 32));
for (unsigned i = 0; i < sizeof(data); i += 2) {
SnesColor col;
col.SetSNES(static_cast<uchar>(data[i + 1]) << 8);
col.SetSNES(col.GetSNES() | static_cast<uchar>(data[i]));
snes_color mColor = ConvertSNEStoRGB(col.GetSNES());
col.SetRGB(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f));
col.set_snes(static_cast<uchar>(data[i + 1]) << 8);
col.set_snes(col.snes() | static_cast<uchar>(data[i]));
snes_color mColor = ConvertSNEStoRGB(col.snes());
col.set_rgb(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f));
colors.push_back(col);
}
size_ = sizeof(data) / 2;
@@ -120,10 +89,10 @@ SnesPalette::SnesPalette(const unsigned char* snes_pal)
assert((sizeof(snes_pal) % 4 == 0) && (sizeof(snes_pal) <= 32));
for (unsigned i = 0; i < sizeof(snes_pal); i += 2) {
SnesColor col;
col.SetSNES(snes_pal[i + 1] << (uint16_t)8);
col.SetSNES(col.GetSNES() | snes_pal[i]);
snes_color mColor = ConvertSNEStoRGB(col.GetSNES());
col.SetRGB(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f));
col.set_snes(snes_pal[i + 1] << (uint16_t)8);
col.set_snes(col.snes() | snes_pal[i]);
snes_color mColor = ConvertSNEStoRGB(col.snes());
col.set_rgb(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f));
colors.push_back(col);
}
size_ = sizeof(snes_pal) / 2;
@@ -132,7 +101,7 @@ SnesPalette::SnesPalette(const unsigned char* snes_pal)
SnesPalette::SnesPalette(const std::vector<ImVec4>& cols) {
for (const auto& each : cols) {
SnesColor scol;
scol.SetRGB(each);
scol.set_rgb(each);
colors.push_back(scol);
}
size_ = cols.size();
@@ -141,7 +110,7 @@ SnesPalette::SnesPalette(const std::vector<ImVec4>& cols) {
SnesPalette::SnesPalette(const std::vector<snes_color>& cols) {
for (const auto& each : cols) {
SnesColor scol;
scol.SetSNES(ConvertRGBtoSNES(each));
scol.set_snes(ConvertRGBtoSNES(each));
colors.push_back(scol);
}
size_ = cols.size();
@@ -160,9 +129,9 @@ SDL_Palette* SnesPalette::GetSDL_Palette() {
auto color = std::vector<SDL_Color>(size_);
for (int i = 0; i < size_; i++) {
color[i].r = (uint8_t)colors[i].GetRGB().x * 100;
color[i].g = (uint8_t)colors[i].GetRGB().y * 100;
color[i].b = (uint8_t)colors[i].GetRGB().z * 100;
color[i].r = (uint8_t)colors[i].rgb().x * 100;
color[i].g = (uint8_t)colors[i].rgb().y * 100;
color[i].b = (uint8_t)colors[i].rgb().z * 100;
color[i].a = 0;
std::cout << "Color " << i << " added (R:" << color[i].r
<< " G:" << color[i].g << " B:" << color[i].b << ")" << std::endl;
@@ -171,7 +140,7 @@ SDL_Palette* SnesPalette::GetSDL_Palette() {
return sdl_palette.get();
}
SnesPalette ReadPaletteFromROM(int offset, int num_colors, const uchar* rom) {
SnesPalette ReadPaletteFromRom(int offset, int num_colors, const uchar* rom) {
int color_offset = 0;
std::vector<gfx::SnesColor> colors(num_colors);
@@ -181,7 +150,7 @@ SnesPalette ReadPaletteFromROM(int offset, int num_colors, const uchar* rom) {
new_color.red = (color & 0x1F) * 8;
new_color.green = ((color >> 5) & 0x1F) * 8;
new_color.blue = ((color >> 10) & 0x1F) * 8;
colors[color_offset].SetSNES(ConvertRGBtoSNES(new_color));
colors[color_offset].set_snes(ConvertRGBtoSNES(new_color));
if (color_offset == 0) {
colors[color_offset].SetTransparent(true);
}
@@ -189,53 +158,37 @@ SnesPalette ReadPaletteFromROM(int offset, int num_colors, const uchar* rom) {
offset += 2;
}
gfx::SnesPalette palette(colors);
return palette;
}
uint32_t GetPaletteAddress(const std::string& group_name, size_t palette_index,
size_t color_index) {
// Retrieve the base address for the palette group
uint32_t base_address = kPaletteGroupAddressMap.at(group_name);
// Retrieve the number of colors for each palette in the group
uint32_t colors_per_palette = kPaletteGroupColorCounts.at(group_name);
// Calculate the address for thes specified color in the ROM
uint32_t address = base_address + (palette_index * colors_per_palette * 2) +
(color_index * 2);
return address;
return gfx::SnesPalette(colors);
}
std::array<float, 4> ToFloatArray(const SnesColor& color) {
std::array<float, 4> colorArray;
colorArray[0] = color.GetRGB().x / 255.0f;
colorArray[1] = color.GetRGB().y / 255.0f;
colorArray[2] = color.GetRGB().z / 255.0f;
colorArray[3] = color.GetRGB().w;
colorArray[0] = color.rgb().x / 255.0f;
colorArray[1] = color.rgb().y / 255.0f;
colorArray[2] = color.rgb().z / 255.0f;
colorArray[3] = color.rgb().w;
return colorArray;
}
PaletteGroup::PaletteGroup(uint8_t mSize) : size_(mSize) {}
PaletteGroup CreatePaletteGroupFromColFile(
absl::StatusOr<PaletteGroup> CreatePaletteGroupFromColFile(
std::vector<SnesColor>& palette_rows) {
PaletteGroup toret;
for (int i = 0; i < palette_rows.size(); i += 8) {
SnesPalette palette;
for (int j = 0; j < 8; j++) {
palette.AddColor(palette_rows[i + j].GetRomRGB());
palette.AddColor(palette_rows[i + j].rom_color());
}
toret.AddPalette(palette);
RETURN_IF_ERROR(toret.AddPalette(palette));
}
return toret;
}
// Take a SNESPalette with N many colors and divide it into palettes of 8 colors
// each
PaletteGroup CreatePaletteGroupFromLargePalette(SnesPalette& palette) {
absl::StatusOr<PaletteGroup> CreatePaletteGroupFromLargePalette(
SnesPalette& palette) {
PaletteGroup toret;
for (int i = 0; i < palette.size(); i += 8) {
@@ -246,7 +199,7 @@ PaletteGroup CreatePaletteGroupFromLargePalette(SnesPalette& palette) {
}
}
toret.AddPalette(new_palette);
RETURN_IF_ERROR(toret.AddPalette(new_palette));
}
return toret;
}

View File

@@ -13,6 +13,7 @@
#include "absl/base/casts.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "app/core/constants.h"
#include "app/gfx/snes_color.h"
@@ -27,27 +28,8 @@ struct snes_palette {
};
using snes_palette = struct snes_palette;
/**
* @brief Extracts a vector of SNES colors from a data buffer.
*
* @param data The data buffer to extract from.
* @param offset The offset in the buffer to start extracting from.
* @param palette_size The size of the palette to extract.
* @return A vector of SNES colors extracted from the buffer.
*/
std::vector<snes_color> Extract(const char* data, unsigned int offset,
unsigned int palette_size);
/**
* @brief Converts a vector of SNES colors to a vector of characters.
*
* @param palette The vector of SNES colors to convert.
* @return A vector of characters representing the converted SNES colors.
*/
std::vector<char> Convert(const std::vector<snes_color>& palette);
SnesColor GetCgxColor(uint16_t color);
std::vector<SnesColor> GetColFileData(uint8_t* data);
uint32_t GetPaletteAddress(const std::string& group_name, size_t palette_index,
size_t color_index);
class SnesPalette {
public:
@@ -121,7 +103,7 @@ class SnesPalette {
if (i >= size_) {
throw std::out_of_range("SNESPalette: Index out of bounds");
}
colors[i].SetRGB(color);
colors[i].set_rgb(color);
colors[i].SetModified(true);
}
@@ -138,9 +120,8 @@ class SnesPalette {
std::vector<SnesColor> colors; /**< The colors in the palette. */
};
SnesPalette ReadPaletteFromROM(int offset, int num_colors, const uint8_t* rom);
uint32_t GetPaletteAddress(const std::string& group_name, size_t palette_index,
size_t color_index);
SnesPalette ReadPaletteFromRom(int offset, int num_colors, const uint8_t* rom);
std::array<float, 4> ToFloatArray(const SnesColor& color);
struct PaletteGroup {
@@ -208,9 +189,11 @@ struct PaletteGroup {
std::vector<SnesPalette> palettes;
};
PaletteGroup CreatePaletteGroupFromColFile(std::vector<SnesColor>& colors);
absl::StatusOr<PaletteGroup> CreatePaletteGroupFromColFile(
std::vector<SnesColor>& colors);
PaletteGroup CreatePaletteGroupFromLargePalette(SnesPalette& palette);
absl::StatusOr<PaletteGroup> CreatePaletteGroupFromLargePalette(
SnesPalette& palette);
struct Paletteset {
Paletteset() = default;