Skip to content

Commit

Permalink
PALETTE: added cvar to control the 6bit palette color behavior of the…
Browse files Browse the repository at this point in the history
… RGBPalette format

this improves upon issue #519 to also allow to write such a 6bit palette as it is used in c&c
  • Loading branch information
mgerhardy committed Sep 15, 2024
1 parent 29da205 commit 6a42b48
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/modules/core/GameConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ constexpr const char *MetricFlavor = "metric_flavor";
constexpr const char *MetricUUID = "metric_uuid";

constexpr const char *VoxelPalette = "palette";
constexpr const char *PalformatRGB6Bit = "palformat_rgb6bit";
constexpr const char *VoxelCreatePalette = "voxformat_createpalette";
constexpr const char *VoxformatMergequads = "voxformat_mergequads";
constexpr const char *VoxformatReusevertices = "voxformat_reusevertices";
Expand Down
6 changes: 5 additions & 1 deletion src/modules/io/FormatDescription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ FormatDescription gimpPalette() {
return {"Gimp Palette", {"gpl"}, {}, FORMAT_FLAG_SAVE};
}

FormatDescription palPalette() {
return {"RGB Palette", {"pal"}, {}, FORMAT_FLAG_SAVE};
}

const FormatDescription *palettes() {
// clang-format: off
static thread_local FormatDescription desc[] = {
Expand All @@ -64,7 +68,7 @@ const FormatDescription *palettes() {
jascPalette(),
{"Adobe Swatch Exchange", {"ase"}, {"ASEF"}, FORMAT_FLAG_SAVE},
{"Photoshop Palette", {"aco", "8bco"}, {}, FORMAT_FLAG_SAVE},
{"RGB Palette", {"pal"}, {}, FORMAT_FLAG_SAVE},
palPalette(),
{"CSV Palette", {"csv"}, {}, FORMAT_FLAG_SAVE},
{"Paint.NET Palette", {"txt"}, {}, FORMAT_FLAG_SAVE},
{"Portable Network Graphics", {"png"}, {"\x89PNG"}, FORMAT_FLAG_SAVE},
Expand Down
1 change: 1 addition & 0 deletions src/modules/io/FormatDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const FormatDescription* palettes();
FormatDescription png();
FormatDescription jascPalette();
FormatDescription gimpPalette();
FormatDescription palPalette();

} // namespace format

Expand Down
19 changes: 14 additions & 5 deletions src/modules/palette/private/RGBPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "RGBPalette.h"
#include "core/Log.h"
#include "core/StringUtil.h"
#include "core/Var.h"

namespace palette {

Expand Down Expand Up @@ -36,7 +37,7 @@ bool RGBPalette::load(const core::String &filename, io::SeekableReadStream &stre
maxColor = core_max(rgba.b, maxColor);
}
const bool is6Bit = maxColor <= 63;
if (is6Bit) {
if (is6Bit && core::Var::getSafe(cfg::PalformatRGB6Bit)->boolVal()) {
const float scale = (255.0f / 63.0f);
for (int i = 0; i < PaletteMaxColors; ++i) {
core::RGBA rgba = palette.color(i);
Expand All @@ -51,11 +52,19 @@ bool RGBPalette::load(const core::String &filename, io::SeekableReadStream &stre
}

bool RGBPalette::save(const palette::Palette &palette, const core::String &filename, io::SeekableWriteStream &stream) {
const bool to6Bit = core::Var::getSafe(cfg::PalformatRGB6Bit)->boolVal();
const float scale = (255.0f / 63.0f);
for (size_t i = 0; i < palette.size(); ++i) {
const core::RGBA color = palette.color(i);
stream.writeUInt8(color.r);
stream.writeUInt8(color.g);
stream.writeUInt8(color.b);
core::RGBA color = palette.color(i);
if (to6Bit) {
stream.writeUInt8((uint8_t)((float)color.r / scale));
stream.writeUInt8((uint8_t)((float)color.g / scale));
stream.writeUInt8((uint8_t)((float)color.b / scale));
} else {
stream.writeUInt8(color.r);
stream.writeUInt8(color.g);
stream.writeUInt8(color.b);
}
}
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/modules/voxelformat/FormatConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ bool FormatConfig::init() {
"Import the image as volume for both sides", core::Var::boolValidator);
core::Var::get(cfg::VoxformatImageImportType, "0", core::CV_NOPERSIST, "0 = plane, 1 = heightmap, 2 = volume",
core::Var::minMaxValidator<0, 2>);

core::Var::get(cfg::PalformatRGB6Bit, "false", core::CV_NOPERSIST, "Use 6 bit color values for the palette (0-63) - used e.g. in C&C pal files",
core::Var::boolValidator);

return true;
}

Expand Down
4 changes: 4 additions & 0 deletions src/tools/voxedit/modules/voxedit-ui/FileDialogOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ void fileDialogOptions(video::OpenFileMode mode, const io::FormatDescription *de
return;
}

if (*desc == io::format::palPalette()) {
ImGui::CheckboxVar(_("6 bit color values"), cfg::PalformatRGB6Bit);
}

const bool forceApplyOptions = (desc->flags & FORMAT_FLAG_ALL) == FORMAT_FLAG_ALL;
const bool meshFormat = voxelformat::isMeshFormat(*desc);
if (forceApplyOptions || meshFormat) {
Expand Down

0 comments on commit 6a42b48

Please sign in to comment.