Make bitmap layer parameters to be able set static
[synfig.git] / synfig-core / src / synfig / layer_bitmap.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_bitmap.cpp
3 **      \brief Template Header
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_bitmap.h"
33 #include "layer.h"
34 #include "time.h"
35 #include "string.h"
36 #include "vector.h"
37
38 #include "context.h"
39 #include "time.h"
40 #include "color.h"
41 #include "surface.h"
42 #include "renddesc.h"
43 #include "target.h"
44
45 #include "general.h"
46 #include "paramdesc.h"
47 #include <ETL/misc>
48
49 #endif
50
51 /* === U S I N G =========================================================== */
52
53 using namespace synfig;
54 using namespace std;
55 using namespace etl;
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 synfig::Layer_Bitmap::Layer_Bitmap():
64     Layer_Composite     (1.0,Color::BLEND_COMPOSITE),
65         tl                              (-0.5,0.5),
66         br                              (0.5,-0.5),
67         c                               (1),
68         surface                 (128,128),
69         trimmed                 (false),
70         gamma_adjust    (1.0),
71         tl_static               (false),
72         br_static               (false),
73         c_static                (true),
74         gamma_adjust_static(false)
75 {
76 }
77
78 bool
79 synfig::Layer_Bitmap::set_param(const String & param, ValueBase value)
80 {
81         IMPORT(tl);
82         IMPORT(br);
83         IMPORT(c);
84         if(param=="gamma_adjust"&& value.get_type()==ValueBase::TYPE_REAL)
85         {
86                 gamma_adjust=1.0/value.get(Real());
87                 //gamma_adjust.set_gamma(1.0/value.get(Real()));
88                 return true;
89         }
90
91         return Layer_Composite::set_param(param,value);
92 }
93
94 ValueBase
95 synfig::Layer_Bitmap::get_param(const String & param)const
96 {
97         EXPORT(tl);
98         EXPORT(br);
99         EXPORT(c);
100         if(param=="gamma_adjust")
101                 return 1.0/gamma_adjust;
102
103         if(param=="_width")
104         {
105                 if (trimmed) return int(width);
106                 return surface.get_w();
107         }
108         if(param=="_height")
109         {
110                 if (trimmed) return int(height);
111                 return surface.get_h();
112         }
113
114         return Layer_Composite::get_param(param);
115 }
116
117 Layer::Vocab
118 Layer_Bitmap::get_param_vocab()const
119 {
120         Layer::Vocab ret(Layer_Composite::get_param_vocab());
121
122         ret.push_back(ParamDesc("tl")
123                 .set_local_name(_("Top-Left"))
124                 .set_description(_("Upper left-hand Corner of image"))
125         );
126
127         ret.push_back(ParamDesc("br")
128                 .set_local_name(_("Bottom-Right"))
129                 .set_description(_("Lower right-hand Corner of image"))
130         );
131
132         ret.push_back(ParamDesc("c")
133                 .set_local_name(_("Interpolation"))
134                 .set_description(_("What type of interpolation to use"))
135                 .set_hint("enum")
136                 .add_enum_value(0,"nearest",_("Nearest Neighbor"))
137                 .add_enum_value(1,"linear",_("Linear"))
138                 .add_enum_value(2,"cosine",_("Cosine"))
139                 .add_enum_value(3,"cubic",_("Cubic"))
140         );
141
142         ret.push_back(ParamDesc("gamma_adjust")
143                 .set_local_name(_("Gamma Adjustment"))
144         );
145
146         return ret;
147 }
148
149 synfig::Layer::Handle
150 Layer_Bitmap::hit_check(synfig::Context context, const synfig::Point &pos)const
151 {
152         Point surface_pos;
153         surface_pos=pos-tl;
154
155         surface_pos[0]/=br[0]-tl[0];
156         if(surface_pos[0]<=1.0 && surface_pos[0]>=0.0)
157         {
158                 surface_pos[1]/=br[1]-tl[1];
159                 if(surface_pos[1]<=1.0 && surface_pos[1]>=0.0)
160                 {
161                         return const_cast<Layer_Bitmap*>(this);
162                 }
163         }
164
165         return context.hit_check(pos);
166 }
167
168 inline
169 const Color&
170 synfig::Layer_Bitmap::filter(Color& x)const
171 {
172         if(gamma_adjust!=1.0)
173         {
174                 x.set_r(powf((float)x.get_r(),gamma_adjust));
175                 x.set_g(powf((float)x.get_g(),gamma_adjust));
176                 x.set_b(powf((float)x.get_b(),gamma_adjust));
177         }
178         return x;
179 }
180
181 Color
182 synfig::Layer_Bitmap::get_color(Context context, const Point &pos)const
183 {
184         Point surface_pos;
185
186         if(!get_amount())
187                 return context.get_color(pos);
188
189         surface_pos=pos-tl;
190
191         surface_pos[0]/=br[0]-tl[0];
192         if(surface_pos[0]<=1.0 && surface_pos[0]>=0.0)
193         {
194                 surface_pos[1]/=br[1]-tl[1];
195                 if(surface_pos[1]<=1.0 && surface_pos[1]>=0.0)
196                 {
197                         if (trimmed)
198                         {
199                                 surface_pos[0]*=width;
200                                 surface_pos[1]*=height;
201
202                                 if (surface_pos[0] > left+surface.get_w() || surface_pos[0] < left || surface_pos[1] > top+surface.get_h() || surface_pos[1] < top)
203                                         return context.get_color(pos);
204
205                                 surface_pos[0] -= left;
206                                 surface_pos[1] -= top;
207                         }
208                         else
209                         {
210                                 surface_pos[0]*=surface.get_w();
211                                 surface_pos[1]*=surface.get_h();
212                         }
213
214                         Color ret(Color::alpha());
215
216                         switch(c)
217                         {
218                         case 6: // Undefined
219                         case 5: // Undefined
220                         case 4: // Undefined
221                         case 3: // Cubic
222                                 ret=surface.cubic_sample(surface_pos[0],surface_pos[1]);
223                                 break;
224
225                         case 2: // Cosine
226                                 ret=surface.cosine_sample(surface_pos[0],surface_pos[1]);
227                                 break;
228                         case 1: // Linear
229                                 ret=surface.linear_sample(surface_pos[0],surface_pos[1]);
230                                 break;
231                         case 0: // Nearest Neighbor
232                         default:
233                                 {
234                                         int x(min(surface.get_w()-1,max(0,round_to_int(surface_pos[0]))));
235                                         int y(min(surface.get_h()-1,max(0,round_to_int(surface_pos[1]))));
236                                         ret= surface[y][x];
237                                 }
238                         break;
239                         }
240
241                         ret=filter(ret);
242
243                         if(get_amount()==1 && get_blend_method()==Color::BLEND_STRAIGHT)
244                                 return ret;
245                         else
246                                 return Color::blend(ret,context.get_color(pos),get_amount(),get_blend_method());
247                 }
248         }
249
250         return context.get_color(pos);
251 }
252
253 bool
254 Layer_Bitmap::accelerated_render(Context context,Surface *out_surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)  const
255 {
256         int interp=c;
257         if(quality>=10)
258                 interp=0;
259         else if(quality>=5 && interp>1)
260                 interp=1;
261
262         // We can only handle NN and Linear at the moment
263         //if(interp>1)
264         //      return Layer_Composite::accelerated_render(context,out_surface,quality,renddesc,cb);
265
266         //if we don't actually have a valid surface just skip us
267         if(!surface.is_valid())
268         {
269                 // Render what is behind us
270                 return context.accelerated_render(out_surface,quality,renddesc,cb);
271         }
272
273         SuperCallback subcb(cb,1,10000,10001+renddesc.get_h());
274
275         if(     get_amount()==1 &&
276                 get_blend_method()==Color::BLEND_STRAIGHT &&
277                 !trimmed &&
278                 renddesc.get_tl()==tl &&
279                 renddesc.get_br()==br)
280         {
281                 // Check for the trivial case
282                 if(surface.get_w()==renddesc.get_w() && surface.get_h()==renddesc.get_h() && gamma_adjust==1.0f)
283                 {
284                         if(cb && !cb->amount_complete(0,100)) return false;
285                         *out_surface=surface;
286                         if(cb && !cb->amount_complete(100,100)) return false;
287                         return true;
288                 }
289                 out_surface->set_wh(renddesc.get_w(),renddesc.get_h());
290         }
291         else
292         {
293                 // Render what is behind us
294                 if(!context.accelerated_render(out_surface,quality,renddesc,&subcb))
295                         return false;
296         }
297
298         if(cb && !cb->amount_complete(10000,10001+renddesc.get_h())) return false;
299
300         Point   obr     = renddesc.get_br(),
301                         otl = renddesc.get_tl();
302
303         //Vector::value_type pw=renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
304         //Vector::value_type ph=renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
305
306         //A = representation of input (just tl,br)      //just a scaling right now
307         //B = representation of output (just tl,br)     //just a scaling right now
308         //sa = scaling for input (0,1) -> (0,w/h)
309         //sb = scaling for output (0,1) -> (0,w/h)
310
311         float   outwf = obr[0] - otl[0];
312         float   outhf = obr[1] - otl[1];
313
314         int             inw = surface.get_w();
315         int             inh = surface.get_h();
316
317         int             outw = renddesc.get_w();
318         int             outh = renddesc.get_h();
319
320         float   inwf, inhf;
321         Point   itl, ibr;
322
323         if (trimmed)
324         {
325                 inwf = (br[0] - tl[0])*surface.get_w()/width;
326                 inhf = (br[1] - tl[1])*surface.get_h()/height;
327                 itl = Point(tl[0] + (br[0]-tl[0])*left/width,
328                                         tl[1] + (br[1]-tl[1])*top/height);
329                 ibr = Point(tl[0] + (br[0]-tl[0])*(left+inw)/width,
330                                         tl[1] + (br[1]-tl[1])*(top+inh)/height);
331         }
332         else
333         {
334                 inwf = br[0] - tl[0];
335                 inhf = br[1] - tl[1];
336                 itl = tl;
337                 ibr = br;
338         }
339
340         //need to get the input coords in output space, so we can clip
341
342         //get the desired corners of the bitmap (in increasing order) in integers
343         //floating point corners
344         float x1f = (itl[0] - otl[0])*outw/outwf;
345         float x2f = (ibr[0] - otl[0])*outw/outwf;
346         float y1f = (itl[1] - otl[1])*outh/outhf;
347         float y2f = (ibr[1] - otl[1])*outh/outhf;
348
349         if(x1f > x2f) swap(x1f,x2f);
350         if(y1f > y2f) swap(y1f,y2f);
351
352         int x_start = max(0,(int)floor(x1f));   //probably floor
353         int x_end       = min(outw,(int)ceil(x2f));     //probably ceil
354         int y_start = max(0,(int)floor(y1f));   //probably floor
355         int y_end       = min(outh,(int)ceil(y2f));     //probably ceil
356
357         //need to get the x,y,dx,dy values from output space to input, so we can do fast interpolation
358
359         //get the starting position in input space... for interpolating
360
361         // in int -> out float:
362         // Sb(B^-1)A(Sa^-1) x
363         float inx_start = (((x_start/*+0.5f*/)*outwf/outw + otl[0]) - itl[0])*inw/inwf; //may want to bias this (center of pixel)???
364         float iny_start = (((y_start/*+0.5f*/)*outhf/outh + otl[1]) - itl[1])*inh/inhf; //may want to bias this (center of pixel)???
365
366         //calculate the delta values in input space for one pixel movement in output space
367         //same matrix but with a vector instead of a point...
368         float indx = outwf*(inw)/((outw)*inwf);         //translations died
369         float indy = outhf*(inh)/((outh)*inhf);         //translations died
370
371         //perhaps use a DDA algorithm... if faster...
372         //   will still want pixel fractions to be floating point since colors are
373
374         //synfig::info("xstart:%d ystart:%d xend:%d yend:%d",x_start,y_start,x_end,y_end);
375
376         //start drawing at the start of the bitmap (either origin or corner of input...)
377         //and get other info
378         Surface::alpha_pen pen(out_surface->get_pen(x_start,y_start));
379         pen.set_alpha(get_amount());
380         pen.set_blend_method(get_blend_method());
381
382         //check if we should use the downscale filtering
383         if(quality <= 7)
384         {
385                 //the stride of the value should be inverted because we want to downsample
386                 //when the stride is small, not big
387                 //int multw = (int)ceil(indx);
388                 //int multh = (int)ceil(indy);
389
390                 if(indx > 1.7 || indy > 1.7)
391                 {
392                         /*synfig::info("Decided to downsample? ratios - (%f,%f) -> (%d,%d)",
393                                                 indx, indy, multw, multh);      */
394
395                         //use sample rect here...
396
397                         float iny, inx;
398                         int x,y;
399
400                         //Point sample - truncate
401                         iny = iny_start;//+0.5f;
402                         for(y = y_start; y < y_end; ++y, pen.inc_y(), iny += indy)
403                         {
404                                 inx = inx_start;//+0.5f;
405                                 for(x = x_start; x < x_end; x++, pen.inc_x(), inx += indx)
406                                 {
407                                         Color rc = surface.sample_rect_clip(inx,iny,inx+indx,iny+indy);
408                                         pen.put_value(filter(rc));
409                                 }
410                                 pen.dec_x(x_end-x_start);
411                         }
412
413                         //Color c = (*out_surface)[0][0];
414                         //synfig::info("ValueBase of first pixel = (%f,%f,%f,%f)",c.get_r(),c.get_g(),c.get_b(),c.get_a());
415
416                         return true;
417                 }
418         }
419
420         //perform normal interpolation
421         if(interp==0)
422         {
423                 //synfig::info("Decided to do nearest neighbor");
424                 float iny, inx;
425                 int x,y;
426
427                 //Point sample - truncate
428                 iny = iny_start;//+0.5f;
429                 for(y = y_start; y < y_end; y++, pen.inc_y(), iny += indy)
430                 {
431                         inx = inx_start;//+0.5f;
432                         int yclamp = min(inh-1, max(0, round_to_int(iny)));
433                         for(x = x_start; x < x_end; x++, pen.inc_x(), inx += indx)
434                         {
435                                 int xclamp = min(inw-1, max(0, round_to_int(inx)));
436                                 Color c = filter(surface[yclamp][xclamp]);
437                                 pen.put_value(c); //must get rid of the clip
438                         }
439                         pen.dec_x(x_end-x_start);
440                 }
441         }
442         else
443         if(interp==1)
444         {
445                 //bilinear filtering
446
447                 //float         xmf,xpf,ymf,ypf;
448                 //int           xm,xp,ym,yp;
449                 float   inx,iny;
450                 int             x,y;
451
452                 //can probably buffer for x values...
453
454                 //loop and based on inx,iny sample input image
455                 iny = iny_start;
456                 for(y = y_start; y < y_end; y++, pen.inc_y(), iny += indy)
457                 {
458                         inx = inx_start;
459                         for(x = x_start; x < x_end; x++, pen.inc_x(), inx += indx)
460                         {
461                                 Color col(surface.linear_sample(inx,iny));
462                                 pen.put_value(filter(col));
463                         }
464                         pen.dec_x(x_end-x_start);
465
466                 }
467         }
468         else
469         if(interp==2)
470         {
471                 //cosine filtering
472
473                 //float         xmf,xpf,ymf,ypf;
474                 //int           xm,xp,ym,yp;
475                 float   inx,iny;
476                 int             x,y;
477
478                 //can probably buffer for x values...
479
480                 //loop and based on inx,iny sample input image
481                 iny = iny_start;
482                 for(y = y_start; y < y_end; y++, pen.inc_y(), iny += indy)
483                 {
484                         inx = inx_start;
485                         for(x = x_start; x < x_end; x++, pen.inc_x(), inx += indx)
486                         {
487                                 Color col(surface.cosine_sample(inx,iny));
488                                 pen.put_value(filter(col));
489                         }
490                         pen.dec_x(x_end-x_start);
491
492                 }
493         }
494         else
495         {
496                 //cubic filtering
497
498                 //float         xmf,xpf,ymf,ypf;
499                 //int           xm,xp,ym,yp;
500                 float   inx,iny;
501                 int             x,y;
502
503                 //can probably buffer for x values...
504
505                 //loop and based on inx,iny sample input image
506                 iny = iny_start;
507                 for(y = y_start; y < y_end; y++, pen.inc_y(), iny += indy)
508                 {
509                         inx = inx_start;
510                         for(x = x_start; x < x_end; x++, pen.inc_x(), inx += indx)
511                         {
512                                 Color col(surface.cubic_sample(inx,iny));
513                                 pen.put_value(filter(col));
514                         }
515                         pen.dec_x(x_end-x_start);
516
517                 }
518         }
519
520         return true;
521 }
522
523 Rect
524 Layer_Bitmap::get_bounding_rect()const
525 {
526         return Rect(tl,br);
527 }
528
529
530 bool
531 Layer_Bitmap::set_param_static(const String &param, const bool x)
532 {
533
534         SET_STATIC(tl, x)
535         SET_STATIC(br, x)
536         SET_STATIC(c, x)
537         SET_STATIC(gamma_adjust, x)
538
539         return Layer_Composite::set_param_static(param, x);
540 }
541
542
543 bool
544 Layer_Bitmap::get_param_static(const String &param) const
545 {
546
547         GET_STATIC(tl)
548         GET_STATIC(br)
549         GET_STATIC(c)
550         GET_STATIC(gamma_adjust)
551
552         return Layer_Composite::get_param_static(param);
553 }