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