Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / modules / lyr_std / shade.cpp
index 06f8261..651432d 100644 (file)
@@ -1,20 +1,22 @@
 /* === S Y N F I G ========================================================= */
 /*!    \file shade.cpp
-**     \brief Template Header
+**     \brief Implementation of the "Shade" layer
 **
-**     $Id: shade.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $
+**     $Id$
 **
 **     \legal
-**     Copyright (c) 2002 Robert B. Quattlebaum Jr.
+**     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2008 Chris Moore
 **
-**     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
 */
 /* ========================================================================= */
@@ -52,9 +54,7 @@ using namespace std;
 
 /*#define TYPE_BOX                     0
 #define TYPE_FASTGUASSIAN      1
-#define TYPE_FASTGAUSSIAN      1
 #define TYPE_CROSS                     2
-#define TYPE_GUASSIAN          3
 #define TYPE_GAUSSIAN          3
 #define TYPE_DISC                      4
 */
@@ -63,10 +63,10 @@ using namespace std;
 
 SYNFIG_LAYER_INIT(Layer_Shade);
 SYNFIG_LAYER_SET_NAME(Layer_Shade,"shade");
-SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Shade,_("Shade"));
-SYNFIG_LAYER_SET_CATEGORY(Layer_Shade,_("Stylize"));
+SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Shade,N_("Shade"));
+SYNFIG_LAYER_SET_CATEGORY(Layer_Shade,N_("Stylize"));
 SYNFIG_LAYER_SET_VERSION(Layer_Shade,"0.2");
-SYNFIG_LAYER_SET_CVS_ID(Layer_Shade,"$Id: shade.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $");
+SYNFIG_LAYER_SET_CVS_ID(Layer_Shade,"$Id$");
 
 /* -- F U N C T I O N S ----------------------------------------------------- */
 
@@ -91,10 +91,12 @@ Layer_Shade::set_param(const String &param, const ValueBase &value)
 {
        IMPORT_PLUS(size,clamp(size));
        IMPORT(type);
-       IMPORT(color);
+       IMPORT_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
+                                       set_blend_method(Color::BLEND_ALPHA_OVER);
+                                       color.set_a(1); } else transparent_color_ = true; } });
        IMPORT(offset);
        IMPORT(invert);
-       
+
        return Layer_Composite::set_param(param,value);
 }
 
@@ -106,11 +108,11 @@ Layer_Shade::get_param(const String &param)const
        EXPORT(color);
        EXPORT(offset);
        EXPORT(invert);
-       
+
        EXPORT_NAME();
        EXPORT_VERSION();
-               
-       return Layer_Composite::get_param(param);       
+
+       return Layer_Composite::get_param(param);
 }
 
 Color
@@ -120,7 +122,7 @@ Layer_Shade::get_color(Context context, const Point &pos)const
 
        if(get_amount()==0.0)
                return context.get_color(pos);
-       
+
        Color shade(color);
 
        if(!invert)
@@ -135,18 +137,18 @@ bool
 Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
 {
        int x,y;
-       
+
        const int       w = renddesc.get_w(),
                                h = renddesc.get_h();
        const Real      pw = renddesc.get_pw(),
                                ph = renddesc.get_ph();
-       
+
        RendDesc        workdesc(renddesc);
        Surface         worksurface;
        etl::surface<float> blurred;
-                       
+
        //expand the working surface to accommodate the blur
-       
+
        //the expanded size = 1/2 the size in each direction rounded up
        int     halfsizex = (int) (abs(size[0]*.5/pw) + 3),
                halfsizey = (int) (abs(size[1]*.5/ph) + 3);
@@ -162,19 +164,17 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
                (offset_v>0?offset_v:0)+h
        );
 
-       /*
-       if(quality >=10)
+       if(quality >= 10)
        {
                halfsizex=1;
                halfsizey=1;
-       }else
-       */
-       if(quality == 9)
+       }
+       else if (quality == 9)
        {
                halfsizex/=4;
                halfsizey/=4;
        }
-       
+
        //expand by 1/2 size in each direction on either side
        switch(type)
        {
@@ -200,7 +200,7 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
                #define GAUSSIAN_ADJUSTMENT             (0.05)
                        Real    pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
                        Real    ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
-                       
+
                        pw=pw*pw;
                        ph=ph*ph;
 
@@ -210,7 +210,7 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
                        halfsizex = (halfsizex + 1)/2;
                        halfsizey = (halfsizey + 1)/2;
                        workdesc.set_subwindow( -halfsizex, -halfsizey, offset_w+2*halfsizex, offset_h+2*halfsizey );
-                                               
+
                        break;
                }
        }
@@ -219,25 +219,25 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
        {
                SuperCallback stageone(cb,0,5000,10000);
                SuperCallback stagetwo(cb,5000,10000,10000);
-               
+
                //callbacks depend on how long the blur takes
                if(size[0] || size[1])
                {
                        if(type == Blur::DISC)
                        {
                                stageone = SuperCallback(cb,0,5000,10000);
-                               stagetwo = SuperCallback(cb,5000,10000,10000);  
+                               stagetwo = SuperCallback(cb,5000,10000,10000);
                        }
                        else
                        {
                                stageone = SuperCallback(cb,0,9000,10000);
-                               stagetwo = SuperCallback(cb,9000,10000,10000);  
+                               stagetwo = SuperCallback(cb,9000,10000,10000);
                        }
                }
                else
                {
                        stageone = SuperCallback(cb,0,9999,10000);
-                       stagetwo = SuperCallback(cb,9999,10000,10000);  
+                       stagetwo = SuperCallback(cb,9999,10000,10000);
                }
 
 
@@ -245,7 +245,7 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
                //render the background onto the expanded surface
                if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
                        return false;
-       
+
                // Copy over the alpha
                blurred.set_wh(worksurface.get_w(),worksurface.get_h());
                for(int j=0;j<worksurface.get_h();j++)
@@ -253,13 +253,13 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
                        {
                                blurred[j][i]=worksurface[j][i].get_a();
                        }
-               
+
                //blur the image
                Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
-               
+
                //be sure the surface is of the correct size
                surface->set_wh(renddesc.get_w(),renddesc.get_h());
-               
+
                int u = halfsizex-(offset_u<0?offset_u:0), v = halfsizey-(offset_v<0?offset_v:0);
                for(y=0;y<renddesc.get_h();y++,v++)
                {
@@ -267,12 +267,12 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
                        for(x=0;x<renddesc.get_w();x++,u++)
                        {
                                Color a(color);
-       
+
                                if(!invert)
                                        a.set_a(blurred.linear_sample(offset_u+(float)u,offset_v+(float)v));
                                else
                                        a.set_a(1.0f-blurred.linear_sample(offset_u+(float)u,offset_v+(float)v));
-                               
+
                                if(a.get_a() || get_blend_method()==Color::BLEND_STRAIGHT)
                                {
                                        (*surface)[y][x]=Color::blend(a,worksurface[v][u],get_amount(),get_blend_method());
@@ -283,34 +283,34 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
        }
        else
        {
-               
+
                SuperCallback stageone(cb,0,5000,10000);
                SuperCallback stagetwo(cb,5000,10000,10000);
-               
+
                //callbacks depend on how long the blur takes
                if(size[0] || size[1])
                {
                        if(type == Blur::DISC)
                        {
                                stageone = SuperCallback(cb,0,5000,10000);
-                               stagetwo = SuperCallback(cb,5000,10000,10000);  
+                               stagetwo = SuperCallback(cb,5000,10000,10000);
                        }
                        else
                        {
                                stageone = SuperCallback(cb,0,9000,10000);
-                               stagetwo = SuperCallback(cb,9000,10000,10000);  
+                               stagetwo = SuperCallback(cb,9000,10000,10000);
                        }
                }
                else
                {
                        stageone = SuperCallback(cb,0,9999,10000);
-                       stagetwo = SuperCallback(cb,9999,10000,10000);  
+                       stagetwo = SuperCallback(cb,9999,10000,10000);
                }
 
                int fw(floor_to_int(abs(size[0]/(pw*SCALE_FACTOR)))+1);
                int fh(floor_to_int(abs(size[1]/(ph*SCALE_FACTOR)))+1);
                int tmpw(round_to_int((float)workdesc.get_w()/fw)),tmph(round_to_int((float)workdesc.get_h()/fh));
-               
+
                workdesc.clear_flags();
                workdesc.set_wh(tmpw,tmph);
                //synfig::info("fw: %d, fh: %d",fw,fh);
@@ -328,7 +328,7 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
                for(int j=0;j<worksurface.get_h();j++)
                        for(int i=0;i<worksurface.get_w();i++)
                                blurred[j][i]=worksurface[j][i].get_a();
-               
+
                //blur the image
                Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
 
@@ -340,12 +340,12 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
                        for(x=0;x<renddesc.get_w();x++,u++)
                        {
                                Color a(color);
-       
+
                                if(!invert)
                                        a.set_a(blurred.linear_sample(((float)offset_u+(float)u)/(float)fw,((float)offset_v+(float)v)/(float)fh));
                                else
                                        a.set_a(1.0f-blurred.linear_sample(((float)offset_u+(float)u)/fw,((float)offset_v+(float)v)/(float)fh));
-                               
+
                                if(a.get_a() || get_blend_method()==Color::BLEND_STRAIGHT)
                                {
                                        (*surface)[y][x]=Color::blend(a,(*surface)[y][x],get_amount(),get_blend_method());
@@ -353,17 +353,17 @@ Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, co
                        }
                }
        }
-       
-       
+
+
        if(cb && !cb->amount_complete(10000,10000))
        {
                //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
                return false;
        }
-               
+
        return true;
 }
-       
+
 Layer::Vocab
 Layer_Shade::get_param_vocab(void)const
 {
@@ -391,11 +391,11 @@ Layer_Shade::get_param_vocab(void)const
                .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
                .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
        );
-       
+
        ret.push_back(ParamDesc("invert")
                .set_local_name(_("Invert"))
        );
-       
+
        return ret;
 }
 
@@ -417,6 +417,6 @@ Layer_Shade::get_full_bounding_rect(Context context)const
 
        if(is_solid_color())
                return bounds;
-               
+
        return bounds|under;
 }