Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / synfig / palette.cpp
index 4ff3a66..16742a4 100644 (file)
@@ -1,20 +1,21 @@
-/* === S I N F G =========================================================== */
-/*!    \file template.cpp
+/* === S Y N F I G ========================================================= */
+/*!    \file palette.cpp
 **     \brief Template File
 **
-**     $Id: palette.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
+**     $Id$
 **
 **     \legal
-**     Copyright (c) 2002 Robert B. Quattlebaum Jr.
+**     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
 **
-**     This software and associated documentation
-**     are CONFIDENTIAL and PROPRIETARY property of
-**     the above-mentioned copyright holder.
+**     This package 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.
 **
-**     You may not copy, print, publish, or in any
-**     other way distribute this software without
-**     a prior written agreement with
-**     the copyright holder.
+**     This package 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.
 **     \endlegal
 */
 /* ========================================================================= */
 
 using namespace std;
 using namespace etl;
-using namespace sinfg;
+using namespace synfig;
 
 /* === M A C R O S ========================================================= */
 
-#define PALETTE_FILE_COOKIE    "SINFGPAL1.0"
+#define PALETTE_FILE_COOKIE    "SYNFIGPAL1.0"
 
 /* === G L O B A L S ======================================================= */
 
@@ -86,14 +87,15 @@ PaletteItem::add(const Color& x,int xweight)
 
 Palette::Palette(const Surface& surface, int max_colors):
        name_(_("Surface Palette"))
-{      
+{
        max_colors-=2;
-       for(int y=0;y<surface.get_h();y++)
-               for(int x=0;x<surface.get_w();x++)
-               {
+       for(int i=0;(signed)size()<(max_colors-1) && i<max_colors*16;++i) {
+        int x=rand()%surface.get_w();
+        int y=rand()%surface.get_h();
+
                        float dist;
                        Color color(surface[y][x]);
-                                               
+
                        if(empty())
                        {
                                push_back(color);
@@ -114,7 +116,7 @@ Palette::Palette(const Surface& surface, int max_colors):
                                iter->add(color);
                                continue;
                        }
-                       
+
                        /*if(size()>=max_colors)
                        {
                                iterator iterlight(find_light());
@@ -123,9 +125,44 @@ Palette::Palette(const Surface& surface, int max_colors):
                                find_closest(light.color)->add(light.color,light.weight);
                        }
                        */
-                       
+
                        push_back(color);
-                       continue;                       
+                       continue;
+    }
+
+/*
+
+       max_colors-=2;
+       for(int y=0;y<surface.get_h();y++)
+               for(int x=0;x<surface.get_w();x++)
+               {
+                       float dist;
+                       Color color(surface[y][x]);
+
+                       if(empty())
+                       {
+                               push_back(color);
+                               continue;
+                       }
+
+                       if(color.get_a()==0)
+                       {
+                               if(front().color.get_a()!=0)
+                                       insert(begin(),Color(1,0,1,0));
+                               front().weight+=400;
+                               continue;
+                       }
+
+                       iterator iter(find_closest(color,&dist));
+                       if(sqrt(dist)<0.005)
+                       {
+                               iter->add(color);
+                               continue;
+                       }
+
+
+                       push_back(color);
+                       continue;
                }
        sort(rbegin(),rend());
 
@@ -138,6 +175,7 @@ Palette::Palette(const Surface& surface, int max_colors):
                pop_back();
                find_closest(item.color)->add(item.color,item.weight);
        }
+*/
        push_back(Color::black());
        push_back(Color::white());
 
@@ -165,7 +203,7 @@ Palette::find_closest(const Color& color, float* dist)
        const float prep_y(powf(color.get_y(),2.2f)*color.get_a());
        const float prep_u(color.get_u());
        const float prep_v(color.get_v());
-       
+
        for(iter=begin();iter!=end();++iter)
        {
                const float diff_y(prep_y-powf(iter->color.get_y(),2.2f)*iter->color.get_a());
@@ -178,12 +216,12 @@ Palette::find_closest(const Color& color, float* dist)
                        diff_y*diff_y*1.5f+
                        diff_a*diff_a+
 
-                       diff_u*diff_u+  
+                       diff_u*diff_u+
                        diff_v*diff_v
-                       
+
                        // cross product
                        /*abs(
-                               prep_u*iter->color.get_u()-     
+                               prep_u*iter->color.get_u()-
                                prep_v*iter->color.get_v()
                        )*/
                );
@@ -195,7 +233,7 @@ Palette::find_closest(const Color& color, float* dist)
        }
        if(dist)
                *dist=best_dist;
-       
+
        return best_match;
 }
 
@@ -206,13 +244,13 @@ Palette::find_heavy()
        iterator iter;
 
        iterator best_match(begin());
-       
+
        for(iter=begin();iter!=end();++iter)
        {
                if(iter->weight>best_match->weight)
                        best_match=iter;
        }
-       
+
        return best_match;
 }
 
@@ -222,13 +260,13 @@ Palette::find_light()
        iterator iter;
 
        iterator best_match(begin());
-       
+
        for(iter=begin();iter!=end();++iter)
        {
                if(iter->weight<best_match->weight)
                        best_match=iter;
        }
-       
+
        return best_match;
 }
 
@@ -251,17 +289,17 @@ Palette::grayscale(int steps)
 }
 
 void
-Palette::save_to_file(const sinfg::String& filename)const
+Palette::save_to_file(const synfig::String& filename)const
 {
        const_iterator iter;
 
        std::ofstream file(filename.c_str());
-       
+
        if(!file)
                throw strprintf(_("Unable to open %s for write"),filename.c_str());
-       
+
        file<<PALETTE_FILE_COOKIE<<endl;
-       file<<name_<<endl;      
+       file<<name_<<endl;
        for(iter=begin();iter!=end();++iter)
        {
                file<<iter->name<<endl;
@@ -270,28 +308,28 @@ Palette::save_to_file(const sinfg::String& filename)const
                        <<iter->color.get_g()<<endl
                        <<iter->color.get_b()<<endl
                        <<iter->color.get_a()<<endl;
-               
+
        }
 }
 
 Palette
-Palette::load_from_file(const sinfg::String& filename)
+Palette::load_from_file(const synfig::String& filename)
 {
        std::ifstream file(filename.c_str());
-       
+
        if(!file)
                throw strprintf(_("Unable to open %s for read"),filename.c_str());
-       
+
        Palette ret;
        String line;
-       
+
        getline(file,line);
 
        if(line!=PALETTE_FILE_COOKIE)
                throw strprintf(_("%s does not appear to be a palette file"),filename.c_str());
 
        getline(file,ret.name_);
-       
+
        while(!file.eof())
        {
                PaletteItem item;
@@ -313,9 +351,9 @@ Palette::load_from_file(const sinfg::String& filename)
                getline(file,line);
                if(!file.eof())break;
                item.color.set_a(atof(line.c_str()));
-               
+
                ret.push_back(item);
        }
-       
+
        return ret;
 }