diff --git a/src/app/file/palette_file.cpp b/src/app/file/palette_file.cpp index e1aafe7d6..0cb4ed6cf 100644 --- a/src/app/file/palette_file.cpp +++ b/src/app/file/palette_file.cpp @@ -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)) { diff --git a/src/doc/CMakeLists.txt b/src/doc/CMakeLists.txt index 9b942c868..6288c3ba0 100644 --- a/src/doc/CMakeLists.txt +++ b/src/doc/CMakeLists.txt @@ -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 diff --git a/src/doc/file/pal_file.cpp b/src/doc/file/pal_file.cpp new file mode 100644 index 000000000..021dc7ed7 --- /dev/null +++ b/src/doc/file/pal_file.cpp @@ -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 +#include +#include +#include +#include + +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 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; isize(); ++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 diff --git a/src/doc/file/pal_file.h b/src/doc/file/pal_file.h new file mode 100644 index 000000000..b861d57ab --- /dev/null +++ b/src/doc/file/pal_file.h @@ -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