Add support to load/save GIMP palettes (.gpl files). Issue #112.

This commit is contained in:
David Capello 2012-06-16 17:50:52 -03:00
parent 3835af7ff0
commit 6f91238264
8 changed files with 169 additions and 3 deletions

View File

@ -339,6 +339,7 @@ add_library(aseprite-library
util/col_file.cpp
util/expand_cel_canvas.cpp
util/filetoks.cpp
util/gpl_file.cpp
util/misc.cpp
util/msk_file.cpp
util/pic_file.cpp

View File

@ -31,4 +31,5 @@ add_library(base-lib
split_string.cpp
string.cpp
thread.cpp
trim_string.cpp
version.cpp)

28
src/base/trim_string.cpp Normal file
View File

@ -0,0 +1,28 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include "base/trim_string.h"
#include <cctype>
void base::trim_string(const base::string& input, base::string& output)
{
size_t i, j;
for (i=0; i<input.size(); ++i)
if (!std::isspace(input.at(i)))
break;
for (j=input.size()-1; j>i; --j)
if (!std::isspace(input.at(j)))
break;
if (i < j)
output = input.substr(i, j - i + 1);
else
output = "";
}

18
src/base/trim_string.h Normal file
View File

@ -0,0 +1,18 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef BASE_TRIM_STRING_H_INCLUDED
#define BASE_TRIM_STRING_H_INCLUDED
#include "base/string.h"
namespace base {
void trim_string(const base::string& input, base::string& output);
}
#endif

View File

@ -578,7 +578,7 @@ void PaletteEntryEditor::onPasteColorsClick(Event& ev)
void PaletteEntryEditor::onLoadPaletteClick(Event& ev)
{
Palette *palette;
base::string filename = app::show_file_selector("Load Palette", "", "png,pcx,bmp,tga,lbm,col");
base::string filename = app::show_file_selector("Load Palette", "", "png,pcx,bmp,tga,lbm,col,gpl");
if (!filename.empty()) {
palette = Palette::load(filename.c_str());
if (!palette) {
@ -597,7 +597,7 @@ void PaletteEntryEditor::onSavePaletteClick(Event& ev)
int ret;
again:
filename = app::show_file_selector("Save Palette", "", "png,pcx,bmp,tga,col");
filename = app::show_file_selector("Save Palette", "", "png,pcx,bmp,tga,col,gpl");
if (!filename.empty()) {
if (exists(filename.c_str())) {
ret = Alert::show("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",

View File

@ -25,7 +25,8 @@
#include "gfx/rgb.h"
#include "raster/image.h"
#include "raster/palette.h"
#include "util/col_file.h"
#include "util/col_file.h" // TODO Remove circular dependency between "raster" <-> "util"
#include "util/gpl_file.h"
using namespace gfx;
@ -451,6 +452,9 @@ Palette* Palette::load(const char *filename)
else if (ustricmp(ext, "col") == 0) {
pal = load_col_file(filename);
}
else if (ustricmp(ext, "gpl") == 0) {
pal = load_gpl_file(filename);
}
return pal;
}
@ -483,6 +487,9 @@ bool Palette::save(const char *filename) const
else if (ustricmp(ext, "col") == 0) {
success = save_col_file(this, filename);
}
else if (ustricmp(ext, "gpl") == 0) {
success = save_gpl_file(this, filename);
}
return success;
}

84
src/util/gpl_file.cpp Normal file
View File

@ -0,0 +1,84 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "base/split_string.h"
#include "base/trim_string.h"
#include "base/unique_ptr.h"
#include "raster/image.h"
#include "raster/palette.h"
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
Palette* load_gpl_file(const char *filename)
{
std::ifstream f(filename);
if (f.bad()) return NULL;
// Read first line, it must be "GIMP Palette"
std::string line;
if (!std::getline(f, line)) return NULL;
base::trim_string(line, line);
if (line != "GIMP Palette") return NULL;
UniquePtr<Palette> pal(new Palette(0, 256));
int entryCounter = 0;
while (std::getline(f, line)) {
// Trim line.
base::trim_string(line, line);
// Remove comments
if (line.empty() || line[0] == '#')
continue;
int r, g, b;
std::istringstream lineIn(line);
lineIn >> r >> g >> b;
if (lineIn.good()) {
pal->setEntry(entryCounter, _rgba(r, g, b, 255));
++entryCounter;
if (entryCounter >= Palette::MaxColors)
break;
}
}
return pal.release();
}
bool save_gpl_file(const Palette *pal, const char *filename)
{
std::ofstream f(filename);
if (f.bad()) return false;
f << "GIMP Palette\n"
<< "#\n";
for (int i=0; i<pal->size(); ++i) {
uint32_t col = pal->getEntry(i);
f << std::setfill(' ') << std::setw(3) << ((int)_rgba_getr(col)) << " "
<< std::setfill(' ') << std::setw(3) << ((int)_rgba_getg(col)) << " "
<< std::setfill(' ') << std::setw(3) << ((int)_rgba_getb(col)) << "\tUntitled\n";
}
return true;
}

27
src/util/gpl_file.h Normal file
View File

@ -0,0 +1,27 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef UTIL_GPL_FILE_H_INCLUDED
#define UTIL_GPL_FILE_H_INCLUDED
class Palette;
Palette* load_gpl_file(const char* filename);
bool save_gpl_file(const Palette* pal, const char* filename);
#endif