Skip to content

Commit

Permalink
Fix export sprite sheet to RGB is not exporting the palette (fix asep…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gasparoken committed Nov 8, 2023
1 parent 53f0cd6 commit c9bbdcf
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/app/doc_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1180,10 +1180,13 @@ Doc* DocExporter::createEmptyTexture(const Samples& samples,
base::task_token& token) const
{
ColorMode colorMode = ColorMode::INDEXED;
Palette* palette = nullptr;
Palette palette(0, 0);
int maxColors = 256;
gfx::ColorSpaceRef colorSpace;
color_t transparentColor = 0;
const bool textureSupportsPalette =
(m_textureFilename.empty() || // This is the preview and we create the palette anyway
format_supports_palette(m_textureFilename));

for (const auto& sample : samples) {
if (token.canceled())
Expand All @@ -1210,15 +1213,22 @@ Doc* DocExporter::createEmptyTexture(const Samples& samples,
else if (sample.sprite()->getPalettes().size() > 1) {
colorMode = ColorMode::RGB;
}
else if (palette &&
palette->countDiff(sample.sprite()->palette(frame_t(0)),
nullptr, nullptr) > 0) {
else if (palette.size() > 0 &&
palette.countDiff(sample.sprite()->palette(frame_t(0)),
nullptr, nullptr) > 0) {
colorMode = ColorMode::RGB;
}
else if (!palette) {
palette = sample.sprite()->palette(frame_t(0));
else if (palette.size() == 0) {
palette = *sample.sprite()->palette(frame_t(0));
transparentColor = sample.sprite()->transparentColor();
}
if (colorMode == ColorMode::RGB &&
textureSupportsPalette &&
sample.sprite() &&
sample.sprite()->getPalettes()[0]) {
Palette* samplePalette = sample.sprite()->getPalettes()[0];
palette.addNonRepeatedColors(samplePalette);
}
}
}

Expand All @@ -1236,8 +1246,8 @@ Doc* DocExporter::createEmptyTexture(const Samples& samples,
maxColors,
m_docBuf));

if (palette)
sprite->setPalette(palette, false);
if (palette.size() > 0)
sprite->setPalette(&palette, false);

std::unique_ptr<Doc> document(new Doc(sprite.get()));
sprite.release();
Expand Down
10 changes: 10 additions & 0 deletions src/app/file/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ bool is_static_image_format(const std::string& filename)
return (format && format->support(FILE_SUPPORT_SEQUENCES));
}

bool format_supports_palette(const std::string& filename)
{
// Get the format through the extension of the filename
FileFormat* format =
FileFormatsManager::instance()
->getFileFormat(dio::detect_format_by_file_extension(filename));

return (format && format->support(FILE_SUPPORT_PALETTES));
}

FileOpROI::FileOpROI()
: m_document(nullptr)
, m_slice(nullptr)
Expand Down
3 changes: 3 additions & 0 deletions src/app/file/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ namespace app {
// as sequence of files).
bool is_static_image_format(const std::string& filename);

// Returns true if the given file format supports palette/s
bool format_supports_palette(const std::string& filename);

} // namespace app

#endif
16 changes: 16 additions & 0 deletions src/doc/palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@ int Palette::countDiff(const Palette* other, int* from, int* to) const
return diff;
}

void Palette::addNonRepeatedColors(const Palette* palette,
const int max)
{
ASSERT(palette);
if (!palette || size() >= max)
return;
for (int i=0; i < palette->size(); i++) {
color_t newColor = palette->getEntry(i);
if (!findExactMatch(newColor)) {
addEntry(newColor);
if (size() >= max)
return;
}
}
}

bool Palette::isBlack() const
{
for (std::size_t c=0; c<m_colors.size(); ++c)
Expand Down
3 changes: 3 additions & 0 deletions src/doc/palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ namespace doc {

int countDiff(const Palette* other, int* from, int* to) const;

void addNonRepeatedColors(const Palette* palette,
const int max = 256);

bool operator==(const Palette& other) const {
return (countDiff(&other, nullptr, nullptr) == 0);
}
Expand Down

0 comments on commit c9bbdcf

Please sign in to comment.