Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / synfig / layer_composite.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_composite.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "layer_composite.h"
34 #include "context.h"
35 #include "time.h"
36 #include "color.h"
37 #include "surface.h"
38 #include "renddesc.h"
39 #include "target.h"
40
41 #include "layer_bitmap.h"
42
43 #include "general.h"
44 #include "render.h"
45 #include "paramdesc.h"
46
47 #endif
48
49 /* === U S I N G =========================================================== */
50
51 using namespace std;
52 using namespace etl;
53 using namespace synfig;
54
55 /* === M A C R O S ========================================================= */
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 bool
64 Layer_Composite::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc_, ProgressCallback *cb)  const
65 {
66         RendDesc renddesc(renddesc_);
67
68         if(!amount_)
69                 return context.accelerated_render(surface,quality,renddesc,cb);
70
71         CanvasBase image;
72
73         SuperCallback stageone(cb,0,50000,100000);
74         SuperCallback stagetwo(cb,50000,100000,100000);
75
76         Layer_Bitmap::Handle surfacelayer(new class Layer_Bitmap());
77
78         Context iter;
79
80         for(iter=context;*iter;iter++)
81                 image.push_back(*iter);
82
83         image.push_front(surfacelayer.get());
84
85         // We want to go ahead and schedule any other
86         // layers...
87 //      while(dynamic_cast<Layer_Composite*>(context->get()))
88 //      while(context->get() &&
89 //              &context->get()->AcceleratedRender==
90 //              &Layer_Composite::AcceleratedRender)
91 //              image.push_back(*(context++));
92
93         image.push_back(0);     // Alpha black
94
95         // Render the backdrop
96         if(!context.accelerated_render(&surfacelayer->surface,quality,renddesc,&stageone))
97                 return false;
98
99         if(quality<=4)surfacelayer->c=3;else
100         if(quality<=5)surfacelayer->c=2;
101         else if(quality<=6)surfacelayer->c=1;
102         else surfacelayer->c=0;
103         surfacelayer->tl=renddesc.get_tl();
104         surfacelayer->br=renddesc.get_br();
105         surfacelayer->set_blend_method(Color::BLEND_STRAIGHT);
106
107         image.push_front(const_cast<synfig::Layer_Composite*>(this));
108
109         // Set up a surface target
110         Target::Handle target(surface_target(surface));
111
112         if(!target)
113         {
114                 if(cb)cb->error(_("Unable to create surface target"));
115                 return false;
116         }
117
118         RendDesc desc(renddesc);
119
120         target->set_rend_desc(&desc);
121
122         // Render the scene
123         return render(Context(image.begin()),target,desc,&stagetwo);
124         //return render_threaded(Context(image.begin()),target,desc,&stagetwo,2);
125 }
126
127 Rect
128 Layer_Composite::get_full_bounding_rect(Context context)const
129 {
130         if(is_disabled() || Color::is_onto(get_blend_method()))
131                 return context.get_full_bounding_rect();
132
133         return context.get_full_bounding_rect()|get_bounding_rect();
134 }
135
136 Layer::Vocab
137 Layer_Composite::get_param_vocab()const
138 {
139         Layer::Vocab ret(Layer::get_param_vocab());
140
141         ret.push_back(ParamDesc(amount_,"amount")
142                 .set_local_name(_("Amount"))
143         );
144         ret.push_back(ParamDesc(blend_method_,"blend_method")
145                 .set_local_name(_("Blend Method"))
146         );
147
148         return ret;
149 }
150
151 bool
152 Layer_Composite::set_param(const String & param, const ValueBase &value)
153 {
154         if(param=="amount" && value.same_type_as(amount_))
155                 amount_=value.get(amount_);
156         else
157         if(param=="blend_method" && value.same_type_as(int()))
158         {
159                 blend_method_ = static_cast<Color::BlendMethod>(value.get(int()));
160
161                 if (blend_method_ < 0 || blend_method_ >= Color::BLEND_END)
162                 {
163                         warning("illegal value (%d) for blend_method - using Composite instead", blend_method_);
164                         blend_method_ = Color::BLEND_COMPOSITE;
165                         return false;
166                 }
167
168                 if (blend_method_ == Color::BLEND_STRAIGHT && !reads_context())
169                 {
170                         Canvas::Handle canvas(get_canvas());
171                         if (canvas)
172                         {
173                                 String version(canvas->get_version());
174
175                                 if (version == "0.1" || version == "0.2")
176                                 {
177                                         if (get_name() == "PasteCanvas")
178                                                 warning("loaded a version %s canvas with a 'Straight' blended PasteCanvas (%s) - check it renders OK",
179                                                                 version.c_str(), get_non_empty_description().c_str());
180                                         else
181                                         {
182                                                 blend_method_ = Color::BLEND_COMPOSITE;
183                                                 converted_blend_ = true;
184
185                                                 // if this layer has a transparent color, go back and set the color again
186                                                 // now that we know we are converting the blend method as well.  that will
187                                                 // make the color non-transparent, and change the blend method to alpha over
188                                                 if (transparent_color_)
189                                                         set_param("color", get_param("color"));
190                                         }
191                                 }
192                         }
193                 }
194         }
195         else
196                 return Layer::set_param(param,value);
197         return true;
198 }
199
200 ValueBase
201 Layer_Composite::get_param(const String & param)const
202 {
203         if(param=="amount")
204                 return get_amount();
205         if(param=="blend_method")
206                 return static_cast<int>(get_blend_method());
207         return Layer::get_param(param);
208 }