From 10dda30a15a58d09e561a59594f348b4db3a4405 Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 22 Feb 2024 19:43:37 -0300 Subject: [PATCH] Don't write color2 chunk for files with more than 256 colors (fix #4322) We were incorrectly saving a wrong number of entries for palettes with more than 256 colors in color2 chunk, anyway it doesn't make sense to use this chunks as it doesn't support more than 256 colors. So we removed it for this case. We've also removed the palette chunk for cases where it's not required at all, e.g. when we have less than 256 colors and doesn't have alpha channel, it makes sense to use the color2 chunk as it's smaller in the output file. --- docs/ase-file-specs.md | 9 ++++++--- src/app/file/ase_format.cpp | 14 +++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/docs/ase-file-specs.md b/docs/ase-file-specs.md index 8442ca9bb..18ff5b247 100644 --- a/docs/ase-file-specs.md +++ b/docs/ase-file-specs.md @@ -126,9 +126,12 @@ at least. ### Old palette chunk (0x0004) -Ignore this chunk if you find the new palette chunk (0x2019) Aseprite -v1.1 saves both chunks 0x0004 and 0x2019 just for backward -compatibility. +Ignore this chunk if you find the new palette chunk (0x2019). Aseprite +v1.1 saves both chunks (0x0004 and 0x2019) just for backward +compatibility. Aseprite v1.3.5 writes this chunk if the palette +doesn't have alpha channel and contains 256 colors or less (because +this chunk is smaller), in other case the new palette chunk (0x2019) +will be used (and the old one is not saved anymore). WORD Number of packets + For each packet diff --git a/src/app/file/ase_format.cpp b/src/app/file/ase_format.cpp index 8ca37ff26..20fd02588 100644 --- a/src/app/file/ase_format.cpp +++ b/src/app/file/ase_format.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2023 Igara Studio S.A. +// Copyright (C) 2018-2024 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -355,7 +355,7 @@ bool AseFormat::onSave(FileOp* fop) bool require_new_palette_chunk = false; for (Palette* pal : sprite->getPalettes()) { - if (pal->size() != 256 || pal->hasAlpha()) { + if (pal->size() > 256 || pal->hasAlpha()) { require_new_palette_chunk = true; break; } @@ -393,9 +393,12 @@ bool AseFormat::onSave(FileOp* fop) ase_file_write_palette_chunk(f, &frame_header, pal, palFrom, palTo); } - - // Write color chunk for backward compatibility only - ase_file_write_color2_chunk(f, &frame_header, pal); + else { + // Use old color chunk only when the palette has 256 or less + // colors, and we don't need the alpha channel (as this chunk + // is smaller than the new palette chunk). + ase_file_write_color2_chunk(f, &frame_header, pal); + } } // Write extra chunks in the first frame @@ -676,6 +679,7 @@ static void ase_file_write_color2_chunk(FILE* f, dio::AsepriteFrameHeader* frame // First packet fputc(0, f); // skip 0 colors + ASSERT(pal->size() <= 256); // For >256 we use the palette chunk fputc(pal->size() == 256 ? 0: pal->size(), f); // number of colors for (c=0; csize(); c++) { color = pal->getEntry(c);