Author: Diego Barrios Romero <[email protected]>
Date: Wed Mar 28 11:46:49 2012 +0200
Improved GPL format support, bugs corrected
---
synfig-core/src/synfig/palette.cpp | 80 +++++++++++++++++++++---------------
1 files changed, 47 insertions(+), 33 deletions(-)
diff --git a/synfig-core/src/synfig/palette.cpp
b/synfig-core/src/synfig/palette.cpp
index 533cf3f..50dae43 100644
--- a/synfig-core/src/synfig/palette.cpp
+++ b/synfig-core/src/synfig/palette.cpp
@@ -36,6 +36,7 @@
#include "general.h"
#include <fstream>
#include <iostream>
+#include <sstream>
#endif
@@ -366,56 +367,69 @@ Palette::load_from_file(const synfig::String& filename)
file format: GPL (GIMP Palette) file should have the following
layout:
GIMP Palette
Name: <palette name>
- <more header text> ...
- #
- # comments
- #
+ [Columns: <number>]
+ [#]
+ [# Optional comments]
+ [#]
<value R> <value G> <value B> <swatch name>
<value R> <value G> <value B> <swatch name>
... ...
- <new line>
+ [<new line>]
*/
- // drops empty first header lines in malformed files
- while (!file.eof() && line=="")
+ do {
getline(file,line);
+ } while (!file.eof() && line != PALETTE_GIMP_FILE_COOKIE);
- if (line!=PALETTE_GIMP_FILE_COOKIE)
+ if (line != PALETTE_GIMP_FILE_COOKIE)
throw strprintf(_("%s does not appear to be a valid %s
palette file"),filename.c_str(),"GIMP");
- getline(file,line);
- // drops 6 first chars from line (ie 'Name: ')
- ret.name_ = String(line.substr(6));
-
- getline(file,line);
- // drops remaining header lines
- while (!file.eof() && line.substr(0,1)!="#")
- getline(file,line);
-
- // drops comments
- while (!file.eof() && line.substr(0,1)=="#")
- getline(file,line);
-
- // line contains the first color so we put it back in
- for (int i = line.length(); i; i--) file.unget();
+
+ bool has_color = false;
+
+ do
+ {
+ getline(file, line);
+
+ if (!line.empty() && line.substr(0,5) == "Name:")
+ ret.name_ = String(line.substr(6));
+ else if (!line.empty() && line.substr(0,8) ==
"Columns:")
+ ; // Ignore columns
+ else if (!line.empty() && line.substr(0,1) == "#")
+ ; // Ignore comments
+ else if (!line.empty())
+ {
+ // not empty line not part of the header =>
color
+ has_color = true;
+ // line contains the first color so we put it
back in (including \n)
+ for (int i = line.length()+1; i; i--)
+ file.unget();
+ }
+ } while (!file.eof() && !has_color);
- while(!file.eof())
+ while(!file.eof() && has_color)
{
PaletteItem item;
float r, g, b;
- file >> r >> g >> b;
- getline(file, item.name);
+ stringstream ss;
+ getline (file, line);
- item.color.set_r(pow(r/255,
GAMMA_CORRECTION_FACTOR_GPL2SPAL));
- item.color.set_g(pow(g/255,
GAMMA_CORRECTION_FACTOR_GPL2SPAL));
- item.color.set_b(pow(b/255,
GAMMA_CORRECTION_FACTOR_GPL2SPAL));
- // Alpha is 1 by default
- item.color.set_a(1);
+ if (!line.empty())
+ {
+ ss << line;
+
+ ss >> r >> g >> b;
+ getline(ss, item.name);
+
+ item.color.set_r(pow(r/255,
GAMMA_CORRECTION_FACTOR_GPL2SPAL));
+ item.color.set_g(pow(g/255,
GAMMA_CORRECTION_FACTOR_GPL2SPAL));
+ item.color.set_b(pow(b/255,
GAMMA_CORRECTION_FACTOR_GPL2SPAL));
+ // Alpha is 1 by default
+ item.color.set_a(1);
- // file ends in new line
- if(!file.eof())
ret.push_back(item);
+ }
}
}
else
------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Synfig-devl mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/synfig-devl