Change format to save default palette from .gpl to .ase

In this way the default palette can contain colors with alpha channel.
Also, we migrate the old .gpl to .ase format, removing extra black entries
in the default palette (the old format contains 256 colors, and now we
can handle less colors).
This commit is contained in:
David Capello 2015-07-06 10:59:44 -03:00
parent 32785562dd
commit 8923e04a33
5 changed files with 112 additions and 55 deletions

View File

@ -24,7 +24,6 @@
#include "app/document_undo.h"
#include "app/file/file.h"
#include "app/file/file_formats_manager.h"
#include "app/file/palette_file.h"
#include "app/file_system.h"
#include "app/filename_formatter.h"
#include "app/find_widget.h"
@ -178,41 +177,9 @@ void App::initialize(const AppOptions& options)
if (isPortable())
PRINTF("Running in portable mode\n");
// Default palette from command line.
std::string palFile = options.paletteFileName();
if (palFile.empty()) {
// If there is no palette in command line, we use the default one.
palFile = get_preset_palette_filename(get_default_palette_preset_name());
// If the default palette file doesn't exist, we copy db32.gpl as
// the default one.
if (!base::is_file(palFile)) {
ResourceFinder rf;
rf.includeDataDir("palettes/db32.gpl");
if (rf.findFirst()) {
// Copy db32.gpl as the default palette file.
base::UniquePtr<Palette> pal(load_palette(rf.filename().c_str()));
if (pal)
save_palette(palFile.c_str(), pal.get(), 0);
}
}
}
// Load default palette file.
if (!palFile.empty()) {
PRINTF("Loading default palette file: %s\n", palFile.c_str());
base::UniquePtr<Palette> pal(load_palette(palFile.c_str()));
if (pal) {
set_default_palette(pal.get());
}
else {
PRINTF("Error loading default palette file\n");
}
}
// Set system palette to the default one.
set_current_palette(NULL, true);
// Load or create the default palette, or migrate the default
// palette from an old format palette to the new one, etc.
load_default_palette(options.paletteFileName());
// Initialize GUI interface
UIContext* ctx = UIContext::instance();

View File

@ -16,6 +16,7 @@
#include "app/file/palette_file.h"
#include "app/file_selector.h"
#include "app/modules/palettes.h"
#include "base/fs.h"
#include "base/unique_ptr.h"
#include "doc/palette.h"
#include "ui/alert.h"
@ -54,7 +55,9 @@ void LoadPaletteCommand::onExecute(Context* context)
std::string filename;
if (!m_preset.empty()) {
filename = get_preset_palette_filename(m_preset);
filename = get_preset_palette_filename(m_preset, ".ase");
if (!base::is_file(filename))
filename = get_preset_palette_filename(m_preset, ".gpl");
}
else {
std::string exts = get_readable_palette_extensions();

View File

@ -56,7 +56,7 @@ void SavePaletteCommand::onExecute(Context* context)
std::string filename;
if (!m_preset.empty()) {
filename = get_preset_palette_filename(m_preset);
filename = get_preset_palette_filename(m_preset, ".ase");
}
else {
std::string exts = get_writable_palette_extensions();

View File

@ -12,6 +12,7 @@
#include "app/modules/palettes.h"
#include "app/app.h"
#include "app/file/palette_file.h"
#include "app/resource_finder.h"
#include "base/fs.h"
#include "base/path.h"
@ -23,15 +24,10 @@
namespace app {
/**
* The default color palette.
*/
// The default color palette.
static Palette* ase_default_palette = NULL;
/**
* Current original palette (you can use _current_palette from Allegro
* to refer to the current system "mapped-palette").
*/
// Palette in current sprite frame.
static Palette* ase_current_palette = NULL;
int init_module_palette()
@ -47,6 +43,91 @@ void exit_module_palette()
delete ase_current_palette;
}
void load_default_palette(const std::string& userDefined)
{
base::UniquePtr<Palette> pal;
// Load specific palette file defined by the user in the command line.
std::string palFile = userDefined;
if (!palFile.empty())
pal.reset(load_palette(palFile.c_str()));
// Load default palette file
else {
std::string defaultPalName = get_preset_palette_filename(
get_default_palette_preset_name(), ".ase");
// If there is no palette in command line, we use the default one.
palFile = defaultPalName;
if (base::is_file(palFile)) {
pal.reset(load_palette(palFile.c_str()));
}
else {
// Migrate old default.gpl to default.ase format
palFile = get_preset_palette_filename(
get_default_palette_preset_name(), ".gpl");
if (base::is_file(palFile)) {
pal.reset(load_palette(palFile.c_str()));
// Remove duplicate black entries at the end (as old palettes
// contains 256 colors)
if (pal && pal->size() == 256) {
doc::color_t black = rgba(0, 0, 0, 255);
// Get the last non-black entry
int i = 0;
for (i=pal->size()-1; i>0; --i) {
if (pal->getEntry(i) != black)
break;
}
if (i < pal->size()-1) {
// Check if there is a black entry in the first entries.
bool hasBlack = false;
for (int j=0; j<i; ++j) {
if (pal->getEntry(j) == black) {
hasBlack = true;
break;
}
}
if (!hasBlack)
++i; // Leave one black entry
// Resize the palette
if (i < pal->size()-1)
pal->resize(i+1);
}
}
// We could remove the old .gpl file (palFile), but as the
// user could be using multiple versions of Aseprite, it's a
// good idea to keep both formats (.gpl for Aseprite <=
// v1.1-beta5, and .ase for future versions).
}
// If the default palette file doesn't exist, we copy db32.gpl
// as the default one (default.ase).
else {
ResourceFinder rf;
rf.includeDataDir("palettes/db32.gpl");
if (rf.findFirst()) {
pal.reset(load_palette(rf.filename().c_str()));
}
}
// Save default.ase file
if (pal) {
palFile = defaultPalName;
save_palette(palFile.c_str(), pal.get(), 0);
}
}
}
if (pal)
set_default_palette(pal.get());
set_current_palette(nullptr, true);
}
Palette* get_current_palette()
{
return ase_current_palette;
@ -62,15 +143,13 @@ void set_default_palette(const Palette* palette)
palette->copyColorsTo(ase_default_palette);
}
/**
* Changes the current system palette. Triggers the APP_PALETTE_CHANGE event
*
* @param palette If "palette" is NULL will be used the default one
* (ase_default_palette)
*/
// Changes the current system palette and triggers the
// App::PaletteChange signal.
//
// If "_palette" is nullptr the default palette is set.
bool set_current_palette(const Palette *_palette, bool forced)
{
const Palette* palette = _palette ? _palette: ase_default_palette;
const Palette* palette = (_palette ? _palette: ase_default_palette);
bool ret = false;
// Have changes
@ -88,7 +167,8 @@ bool set_current_palette(const Palette *_palette, bool forced)
return ret;
}
std::string get_preset_palette_filename(const std::string& preset)
std::string get_preset_palette_filename(const std::string& preset,
const std::string& dot_extension)
{
ResourceFinder rf;
rf.includeUserDir(base::join_path("palettes", ".").c_str());
@ -97,7 +177,7 @@ std::string get_preset_palette_filename(const std::string& preset)
if (!base::is_directory(palettesDir))
base::make_directory(palettesDir);
return base::join_path(palettesDir, preset + ".gpl");
return base::join_path(palettesDir, preset + dot_extension);
}
std::string get_default_palette_preset_name()

View File

@ -21,13 +21,20 @@ namespace app {
int init_module_palette();
void exit_module_palette();
// Loads the default palette or creates it. Also it migrates the
// palette if the palette format changes, etc. The "userDefined"
// parameter can be a default palette name specified in the command
// line.
void load_default_palette(const std::string& userDefined);
Palette* get_default_palette();
Palette* get_current_palette();
void set_default_palette(const Palette* palette);
bool set_current_palette(const Palette* palette, bool forced);
std::string get_preset_palette_filename(const std::string& preset);
std::string get_preset_palette_filename(const std::string& preset,
const std::string& dot_extension);
std::string get_default_palette_preset_name();
} // namespace app