Add support for .pal files (fix #349, related to #181)

This commit is contained in:
David Capello 2015-05-20 16:08:16 -03:00
parent 5dee5f3a57
commit 15a227d06f
4 changed files with 130 additions and 2 deletions

View File

@ -19,6 +19,7 @@
#include "doc/cel.h"
#include "doc/file/col_file.h"
#include "doc/file/gpl_file.h"
#include "doc/file/pal_file.h"
#include "doc/image.h"
#include "doc/layer.h"
#include "doc/palette.h"
@ -33,14 +34,14 @@ using namespace doc;
std::string get_readable_palette_extensions()
{
std::string buf = get_readable_extensions();
buf += ",col,gpl";
buf += ",col,gpl,pal";
return buf;
}
std::string get_writable_palette_extensions()
{
std::string buf = get_writable_extensions();
buf += ",col,gpl";
buf += ",col,gpl,pal";
return buf;
}
@ -55,6 +56,9 @@ Palette* load_palette(const char *filename)
else if (ext == "gpl") {
pal = doc::file::load_gpl_file(filename);
}
else if (ext == "pal") {
pal = doc::file::load_pal_file(filename);
}
else {
FileFormat* ff = FileFormatsManager::instance()->getFileFormatByExtension(ext.c_str());
if (ff->support(FILE_SUPPORT_LOAD)) {
@ -99,6 +103,9 @@ bool save_palette(const char *filename, const Palette* pal)
else if (ext == "gpl") {
success = doc::file::save_gpl_file(pal, filename);
}
else if (ext == "pal") {
success = doc::file::save_pal_file(pal, filename);
}
else {
FileFormat* ff = FileFormatsManager::instance()->getFileFormatByExtension(ext.c_str());
if (ff->support(FILE_SUPPORT_SAVE)) {

View File

@ -25,6 +25,7 @@ add_library(doc-lib
documents.cpp
file/col_file.cpp
file/gpl_file.cpp
file/pal_file.cpp
frame_tag.cpp
frame_tag_io.cpp
frame_tags.cpp

97
src/doc/file/pal_file.cpp Normal file
View File

@ -0,0 +1,97 @@
// Aseprite Document Library
// Copyright (c) 2001-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "base/fstream_path.h"
#include "base/split_string.h"
#include "base/trim_string.h"
#include "base/unique_ptr.h"
#include "doc/image.h"
#include "doc/palette.h"
#include <cctype>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
namespace doc {
namespace file {
Palette* load_pal_file(const char *filename)
{
std::ifstream f(FSTREAM_PATH(filename));
if (f.bad())
return nullptr;
// Read first line, it must be "JASC-PAL"
std::string line;
if (!std::getline(f, line))
return nullptr;
base::trim_string(line, line);
if (line != "JASC-PAL")
return nullptr;
// Second line is the version (0100)
if (!std::getline(f, line))
return nullptr;
base::trim_string(line, line);
if (line != "0100")
return nullptr;
// Ignore number of colors (we'll read line by line anyway)
if (!std::getline(f, line))
return nullptr;
base::UniquePtr<Palette> pal(new Palette(frame_t(0), 0));
while (std::getline(f, line)) {
// Trim line
base::trim_string(line, line);
// Remove comments
if (line.empty())
continue;
int r, g, b;
std::istringstream lineIn(line);
lineIn >> r >> g >> b;
pal->addEntry(rgba(r, g, b, 255));
if (pal->size() == Palette::MaxColors)
break;
}
// TODO remove this when Aseprite supports palettes with less than 256 colors
if (pal->size() != Palette::MaxColors)
pal->resize(Palette::MaxColors);
return pal.release();
}
bool save_pal_file(const Palette *pal, const char *filename)
{
std::ofstream f(FSTREAM_PATH(filename));
if (f.bad()) return false;
f << "JASC-PAL\n"
<< "0100\n"
<< pal->size() << "\n";
for (int i=0; i<pal->size(); ++i) {
uint32_t col = pal->getEntry(i);
f << ((int)rgba_getr(col)) << " "
<< ((int)rgba_getg(col)) << " "
<< ((int)rgba_getb(col)) << "\n";
}
return true;
}
} // namespace file
} // namespace doc

23
src/doc/file/pal_file.h Normal file
View File

@ -0,0 +1,23 @@
// Aseprite Document Library
// Copyright (c) 2001-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef DOC_FILE_PAL_FILE_H_INCLUDED
#define DOC_FILE_PAL_FILE_H_INCLUDED
#pragma once
namespace doc {
class Palette;
namespace file {
Palette* load_pal_file(const char* filename);
bool save_pal_file(const Palette* pal, const char* filename);
} // namespace file
} // namespace doc
#endif