1 /* === S Y N F I G ========================================================= */
3 ** \brief Color Gradient Class Member Definitions
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
35 #include "exception.h"
41 /* === U S I N G =========================================================== */
45 using namespace synfig;
47 /* === M A C R O S ========================================================= */
49 /* === G L O B A L S ======================================================= */
51 /* === P R O C E D U R E S ================================================= */
53 /* === M E T H O D S ======================================================= */
55 synfig::Gradient::Gradient(const Color &c1, const Color &c2)
57 push_back(CPoint(0.0,c1));
58 push_back(CPoint(1.0,c2));
61 synfig::Gradient::Gradient(const Color &c1, const Color &c2, const Color &c3)
63 push_back(CPoint(0.0,c1));
64 push_back(CPoint(0.5,c2));
65 push_back(CPoint(1.0,c3));
68 // This sort algorithm MUST be stable
69 // ie: it must not change the order of items with the same value.
70 // I am using a bubble sort.
71 // This algorithm will sort a nearly-sorted list at ~O(N), and
72 // it will sort an inverse sorted list at ~O(N*N).
74 synfig::Gradient::sort()
76 stable_sort(begin(),end());
81 for(iter=begin();iter!=end();iter++)
83 for(next=iter, iter2=next--;iter2!=begin();iter2=next--)
100 static synfig::ColorAccumulator
101 supersample_helper(const synfig::Gradient::CPoint &color1, const synfig::Gradient::CPoint &color2, float begin, float end, float &weight)
103 if(color1.pos==color2.pos || color1.pos>=end || color2.pos<=begin)
106 return Color::alpha();
108 if(color1.pos>=begin && color2.pos<end)
110 weight=color2.pos-color1.pos;
111 ColorAccumulator ret=Color::blend(color2.color,color1.color, 0.5, Color::BLEND_STRAIGHT).premult_alpha()*weight;
114 if(color1.pos>=begin && color2.pos>=end)
116 weight=end-color1.pos;
117 float pos((end+color1.pos)*0.5);
118 float amount((pos-color1.pos)/(color2.pos-color1.pos));
119 //if(abs(amount)>1)amount=(amount>0)?1:-1;
120 ColorAccumulator ret(Color::blend(color2.color,color1.color, amount, Color::BLEND_STRAIGHT).premult_alpha()*weight);
123 if(color1.pos<begin && color2.pos<end)
125 weight=color2.pos-begin;
126 float pos((begin+color2.pos)*0.5);
127 float amount((pos-color1.pos)/(color2.pos-color1.pos));
128 //if(abs(amount)>1)amount=(amount>0)?1:-1;
129 ColorAccumulator ret(Color::blend(color2.color,color1.color, amount, Color::BLEND_STRAIGHT).premult_alpha()*weight);
132 synfig::error("color1.pos=%f",color1.pos);
133 synfig::error("color2.pos=%f",color2.pos);
134 synfig::error("begin=%f",begin);
135 synfig::error("end=%f",end);
138 return Color::alpha();
143 static void show_gradient(const Gradient::CPointList x)
146 for (Gradient::const_iterator iter = x.begin(); iter != x.end(); iter++)
147 printf("%3d : %.3f %s\n", i++, (*iter).pos, (*iter).color.get_string().c_str());
151 synfig::Gradient::operator+=(const Gradient &rhs)
153 bool print=false; // for debugging
154 if (print) { printf("\nadding lhs:\n"); show_gradient(this->cpoints); printf("\n"); }
155 if (print) { printf("adding rhs:\n"); show_gradient(rhs.cpoints); printf("\n"); }
157 const_iterator iter1 = begin(), iter2 = rhs.begin(), left_same, right_same;
159 if (iter1 != end()) left = *iter1;
160 if (iter2 != rhs.end()) right = *iter2;
161 int pos1 = 0, pos2 = 0;
164 // if there are cpoints in both gradients run through both until one runs out
165 if (iter1 != end() && iter2 != rhs.end())
167 // if the left one has the first cpoint
168 if (left.pos < right.pos)
170 // add on the right gradient's value at this point
171 if (print) printf("using pos %.2f from left %d in loop\n", left.pos, pos1++);
172 ret.push_back(CPoint(left.pos, left.color + rhs(left.pos)));
173 if(++iter1 == end()) break;
176 // if the right one has the first cpoint
177 else if (left.pos > right.pos)
179 // add on the left gradient's value at this point
180 if (print) printf("using pos %.2f from right %d in loop\n", right.pos, pos2++);
181 ret.push_back(CPoint(right.pos, right.color + (*this)(right.pos)));
182 if(++iter2 == rhs.end()) break;
185 // they both have a cpoint at the same time
188 int tpos1 = pos1, tpos2 = pos2;
189 // skip past all cpoints at the same position
190 for(left_same = ++iter1; iter1 != end() && (*iter1).pos == left.pos; iter1++, pos1++)
191 if (print) printf("skipping past pos %d in left\n", pos1);
192 for(right_same = ++iter2; iter2 != rhs.end() && (*iter2).pos == right.pos; iter2++, pos2++)
193 if (print) printf("skipping past pos %d in right\n", pos2);
195 // if there is only one cpoint at this position in each gradient,
196 // there's only one corresponding cpoint in the sum
197 if (iter1 == left_same && iter2 == right_same)
199 if (print) printf("two singles at left %d and right %d\n", pos1++, pos2++);
200 ret.push_back(CPoint(left.pos, left.color + right.color));
202 // otherwise we sum the first in each, and the last in each
205 if (print) printf("[copying %d from left %d and %d from right %d at %.2f]\n", iter1-left_same+1, tpos1, iter2-right_same+1, tpos2, left.pos);
206 // merge the front two cpoints
207 if (print) printf(" copy front from left %d right %d\n", tpos1++, tpos2++);
208 ret.push_back(CPoint(left.pos, left.color + right.color));
210 // merge the middle pairs of points - each middle point merges with its counterpart
211 while(left_same < iter1-1 && right_same < iter2-1)
213 old1 = *(left_same++);
214 old2 = *(right_same++);
215 if (print) printf(" copy middle from left %d and right %d\n", tpos1++, tpos2++);
216 ret.push_back(CPoint(old1.pos, old1.color+old2.color));
218 // if one gradient has more middle points than the other, merge the rest with the last point in the other gradient
219 for(old2 = (*(iter2-1)); left_same < iter1-1; left_same++)
222 if (print) printf(" copy middle from left %d plus end of right\n", tpos1++);
223 ret.push_back(CPoint(old1.pos, old1.color + old2.color));
225 for(old1 = (*(iter1-1)); right_same < iter2-1; right_same++)
228 if (print) printf(" copy middle from right %d plus end of left\n", tpos2++);
229 ret.push_back(CPoint(old2.pos, old1.color + old2.color));
231 // merge the back two cpoints
232 if (print) printf(" copy end from left %d right %d\n", pos1++, pos2++);
233 ret.push_back(CPoint(left.pos, (*(iter1-1)).color + (*(iter2-1)).color));
235 // make sure we update 'left' and 'right'
236 if (iter1 != end()) left=*iter1;
237 if (iter2 == rhs.end()) break;
239 if (iter1 == end()) break;
242 // one of the gradients has run out of points
243 // does the left one have points left?
247 if (print) printf("finish end from left %d\n", pos1++);
248 ret.push_back(CPoint(left.pos, left.color + rhs(left.pos)));
249 if(++iter1 == end()) break;
252 // the left one was empty, so maybe the right one has points left
253 else if (iter2 != rhs.end())
256 if (print) printf("finish end from right %d\n", pos2++);
257 ret.push_back(CPoint(right.pos, right.color + (*this)(right.pos)));
258 if(++iter2 == rhs.end()) break;
262 if (print) { printf("\nsummed ret:\n"); show_gradient(ret); printf("\n"); }
268 synfig::Gradient::operator-=(const Gradient &rhs)
270 return (*this)+=(rhs*-1);
274 synfig::Gradient::operator*=(const float &rhs)
279 for (iterator iter = cpoints.begin(); iter!=cpoints.end(); iter++)
280 (*iter).color *= rhs;
285 synfig::Gradient::operator/=(const float &rhs)
287 for (iterator iter = cpoints.begin(); iter!=cpoints.end(); iter++)
288 (*iter).color /= rhs;
293 synfig::Gradient::operator()(const Real &x,float supersample)const
296 return Color(0,0,0,0);
298 supersample=-supersample;
302 float begin_sample(x-supersample*0.5);
303 float end_sample(x+supersample*0.5);
305 if(cpoints.size()==1 || end_sample<=cpoints.front().pos || isnan(x))
306 return cpoints.front().color;
308 if(begin_sample>=cpoints.back().pos)
309 return cpoints.back().color;
312 if(end_sample>=back().pos)
313 end_sample=back().pos;
315 if(begin_sample<=front().pos)
316 begin_sample=front().pos;
319 const_iterator iter,next;
323 Real left = x-supersample/2, right = x+supersample/2;
325 if(left < front().pos) left = front().pos;
326 if(right > back().pos) right = back().pos;
328 //find using binary search...
329 const_iterator iterl,iterr;
331 //the binary search should give us the values BEFORE the point we're looking for...
332 iterl = binary_find(begin(),end(),left);
333 iterr = binary_find(iterl,end(),right);
335 //now integrate over the range of left to right...
339 iterr++; //let's look at the next one shall we :)
341 //interpolate neighboring colors
342 const Real one = iterr->pos - iterl->pos;
343 const Real lambda = (x - iterl->pos)/one;
345 //(1-l)iterl + (l)iterr
346 return iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
348 //return Color::blend(iterr->color,iterl->color,lambda,Color::BLEND_STRAIGHT);
352 const_iterator i = iterl, ie = iterr+1;
355 ColorAccumulator clast,cwork;
357 const Real lambda = (x - iterl->pos)/(iterr->pos - iterl->pos);
359 //premultiply because that's the form in which we can combine things...
360 clast = iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
361 //Color::blend((i+1)->color,i->color,(left - i->pos)/((i+1)->pos - i->pos),Color::BLEND_STRAIGHT);
364 ColorAccumulator accum = 0;
366 //loop through all the trapezoids and integrate them as we go...
367 // area of trap = (yi + yi1)*(xi1 - xi)
368 // yi = clast, xi = wlast, yi1 = i->color, xi1 = i->pos
370 for(;i<=iterr; wlast=i->pos,clast=i->color.premult_alpha(),++i)
372 const Real diff = i->pos - wlast;
373 if(diff > 0) //only accumulate if there will be area to add
375 cwork = i->color.premult_alpha();
376 accum += (cwork + clast)*diff;
381 const_iterator ibef = i-1;
382 const Real diff = right - ibef->pos;
386 const Real lambda = diff/(i->pos - ibef->pos);
387 cwork = ibef->color.premult_alpha()*(1-lambda) + i->color.premult_alpha()*lambda;
389 accum += (cwork + clast)*diff; //can probably optimize this more... but it's not too bad
393 accum /= supersample; //should be the total area it was sampled over...
394 return accum.demult_alpha();
397 next=begin(),iter=next++;
399 //add for optimization
400 next = binary_find(begin(),end(),(Real)begin_sample);
403 //! As a future optimization, this could be performed faster
404 //! using a binary search.
405 for(;iter<end();iter=next++)
407 if(next==end() || x>=iter->pos && x<next->pos && iter->pos!=next->pos)
409 // If the supersample region falls square in between
410 // two CPoints, then we don't have to do anything special.
411 if(next!=end() && (!supersample || (iter->pos<=begin_sample && next->pos>=end_sample)))
413 const Real dist(next->pos-iter->pos);
414 const Real pos(x-iter->pos);
415 const Real amount(pos/dist);
416 return Color::blend(next->color,iter->color, amount, Color::BLEND_STRAIGHT);
418 // In this case our supersample region extends over one or more
419 // CPoints. So, we need to calculate our coverage amount.
420 ColorAccumulator pool(Color::alpha());
421 float divisor(0.0),weight(0);
423 const_iterator iter2,next2;
425 if(iter==begin() && iter->pos>x)
428 //weight*=iter->color.get_a();
429 pool+=ColorAccumulator(iter->color).premult_alpha()*weight;
434 while(iter2->pos>=begin_sample)
438 weight=iter2->pos-(begin_sample);
439 //weight*=iter2->color.get_a();
440 pool+=ColorAccumulator(iter2->color).premult_alpha()*weight;
445 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
452 while(iter2->pos<=end_sample)
456 weight=(end_sample)-iter2->pos;
457 pool+=ColorAccumulator(iter2->color).premult_alpha()*weight;
461 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
466 if(divisor && pool.get_a() && pool.is_valid())
469 pool.set_r(pool.get_r()/pool.get_a());
470 pool.set_g(pool.get_g()/pool.get_a());
471 pool.set_b(pool.get_b()/pool.get_a());
472 pool.set_a(pool.get_a()/divisor);
475 pool.set_r(pool.get_r()/pool.get_a());
476 pool.set_g(pool.get_g()/pool.get_a());
477 pool.set_b(pool.get_b()/pool.get_a());
481 return Color::alpha();
484 return Color::alpha();
488 // We should never get to this point.
490 synfig::error("synfig::Gradient::operator()(): Logic Error (x=%f)",x);
492 throw std::logic_error(strprintf("synfig::Gradient::operator()(): Logic Error (x=%f)",x));
495 synfig::Gradient::iterator
496 synfig::Gradient::proximity(const Real &x)
499 float dist(100000000);
500 float prev_pos(-0230);
501 // This algorithm requires a sorted list.
502 for(iter=begin();iter<end();iter++)
506 if(prev_pos==iter->pos)
507 new_dist=(abs(x-iter->pos-0.00001));
509 new_dist=(abs(x-iter->pos));
523 synfig::Gradient::const_iterator
524 synfig::Gradient::proximity(const Real &x)const
526 return const_cast<Gradient*>(this)->proximity(x);
529 float dist(100000000);
531 // This algorithm requires a sorted list.
532 for(iter=begin();iter<end();iter++)
534 const float new_dist(abs(x-iter->pos));
547 synfig::Gradient::iterator
548 synfig::Gradient::find(const UniqueID &id)
552 for(iter=begin();iter<end();iter++)
558 throw Exception::NotFound("synfig::Gradient::find(): Unable to find UniqueID in gradient");
561 synfig::Gradient::const_iterator
562 synfig::Gradient::find(const UniqueID &id)const
566 for(iter=begin();iter<end();iter++)
572 throw Exception::NotFound("synfig::Gradient::find()const: Unable to find UniqueID in gradient");