Merge branch 'blender2.7'
[blender.git] / source / blender / blenkernel / intern / colorband.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup bke
21  */
22
23 #include "MEM_guardedalloc.h"
24
25 #include "BLI_math.h"
26 #include "BLI_utildefines.h"
27 #include "BLI_math_color.h"
28 #include "BLI_heap.h"
29
30 #include "DNA_key_types.h"
31 #include "DNA_texture_types.h"
32
33 #include "BKE_colorband.h"
34 #include "BKE_material.h"
35 #include "BKE_key.h"
36
37 void BKE_colorband_init(ColorBand *coba, bool rangetype)
38 {
39         int a;
40
41         coba->data[0].pos = 0.0;
42         coba->data[1].pos = 1.0;
43
44         if (rangetype == 0) {
45                 coba->data[0].r = 0.0;
46                 coba->data[0].g = 0.0;
47                 coba->data[0].b = 0.0;
48                 coba->data[0].a = 0.0;
49
50                 coba->data[1].r = 1.0;
51                 coba->data[1].g = 1.0;
52                 coba->data[1].b = 1.0;
53                 coba->data[1].a = 1.0;
54         }
55         else {
56                 coba->data[0].r = 0.0;
57                 coba->data[0].g = 0.0;
58                 coba->data[0].b = 0.0;
59                 coba->data[0].a = 1.0;
60
61                 coba->data[1].r = 1.0;
62                 coba->data[1].g = 1.0;
63                 coba->data[1].b = 1.0;
64                 coba->data[1].a = 1.0;
65         }
66
67         for (a = 2; a < MAXCOLORBAND; a++) {
68                 coba->data[a].r = 0.5;
69                 coba->data[a].g = 0.5;
70                 coba->data[a].b = 0.5;
71                 coba->data[a].a = 1.0;
72                 coba->data[a].pos = 0.5;
73         }
74
75         coba->tot = 2;
76         coba->cur = 0;
77         coba->color_mode = COLBAND_BLEND_RGB;
78         coba->ipotype = COLBAND_INTERP_LINEAR;
79 }
80
81 static void colorband_init_from_table_rgba_simple(
82         ColorBand *coba,
83         const float (*array)[4], const int array_len)
84 {
85         /* No Re-sample, just de-duplicate. */
86         const float eps = (1.0f / 255.0f) + 1e-6f;
87         BLI_assert(array_len < MAXCOLORBAND);
88         int stops = min_ii(MAXCOLORBAND, array_len);
89         if (stops) {
90                 const float step_size = 1.0f / (float)max_ii(stops - 1, 1);
91                 int i_curr = -1;
92                 for (int i_step = 0; i_step < stops; i_step++) {
93                         if ((i_curr != -1) && compare_v4v4(&coba->data[i_curr].r, array[i_step], eps)) {
94                                 continue;
95                         }
96                         i_curr += 1;
97                         copy_v4_v4(&coba->data[i_curr].r, array[i_step]);
98                         coba->data[i_curr].pos = i_step * step_size;
99                         coba->data[i_curr].cur = i_curr;
100                 }
101                 coba->tot = i_curr + 1;
102                 coba->cur = 0;
103         }
104         else {
105                 /* coba is empty, set 1 black stop */
106                 zero_v3(&coba->data[0].r);
107                 coba->data[0].a = 1.0f;
108                 coba->cur = 0;
109                 coba->tot = 1;
110         }
111 }
112
113
114 /* -------------------------------------------------------------------- */
115 /** \name Color Ramp Re-Sample
116  *
117  * Local functions for #BKE_colorband_init_from_table_rgba
118  * \{ */
119
120 /**
121  * Used for calculating which samples of a color-band to remove (when simplifying).
122  */
123 struct ColorResampleElem {
124         struct ColorResampleElem *next, *prev;
125         HeapNode *node;
126         float rgba[4];
127         float pos;
128 };
129
130 /**
131  * Measure the 'area' of each channel and combine to use as a cost for this samples removal.
132  */
133 static float color_sample_remove_cost(const struct ColorResampleElem *c)
134 {
135         if (c->next == NULL || c->prev == NULL) {
136                 return -1.0f;
137         }
138         float area = 0.0f;
139 #if 0
140         float xy_prev[2], xy_curr[2], xy_next[2];
141         xy_prev[0] = c->prev->pos;
142         xy_curr[0] = c->pos;
143         xy_next[0] = c->next->pos;
144         for (int i = 0; i < 4; i++) {
145                 xy_prev[1] = c->prev->rgba[i];
146                 xy_curr[1] = c->rgba[i];
147                 xy_next[1] = c->next->rgba[i];
148                 area += fabsf(cross_tri_v2(xy_prev, xy_curr, xy_next));
149         }
150 #else
151         /* Above logic, optimized (p: previous, c: current, n: next). */
152         const float xpc = c->prev->pos - c->pos;
153         const float xnc = c->next->pos - c->pos;
154         for (int i = 0; i < 4; i++) {
155                 const float ycn = c->rgba[i] - c->next->rgba[i];
156                 const float ypc = c->prev->rgba[i] - c->rgba[i];
157                 area += fabsf((xpc * ycn) + (ypc * xnc));
158         }
159 #endif
160         return area;
161 }
162
163 /* TODO(campbell): create BLI_math_filter? */
164 static float filter_gauss(float x)
165 {
166         const float gaussfac = 1.6f;
167         const float two_gaussfac2 = 2.0f * gaussfac * gaussfac;
168         x *= 3.0f * gaussfac;
169         return 1.0f / sqrtf((float)M_PI * two_gaussfac2) * expf(-x * x / two_gaussfac2);
170 }
171
172 static void colorband_init_from_table_rgba_resample(
173         ColorBand *coba,
174         const float (*array)[4], const int array_len,
175         bool filter_samples)
176 {
177         BLI_assert(array_len >= 2);
178         const float eps_2x = ((1.0f / 255.0f) + 1e-6f);
179         struct ColorResampleElem *c, *carr = MEM_mallocN(sizeof(*carr) * array_len, __func__);
180         int carr_len = array_len;
181         c = carr;
182         {
183                 const float step_size = 1.0f / (float)(array_len - 1);
184                 for (int i = 0; i < array_len; i++, c++) {
185                         copy_v4_v4(carr[i].rgba, array[i]);
186                         c->next = c + 1;
187                         c->prev = c - 1;
188                         c->pos = i * step_size;
189                 }
190         }
191         carr[0].prev = NULL;
192         carr[array_len - 1].next = NULL;
193
194         /* -2 to remove endpoints. */
195         Heap *heap = BLI_heap_new_ex(array_len - 2);
196         c = carr;
197         for (int i = 0; i < array_len; i++, c++) {
198                 float cost = color_sample_remove_cost(c);
199                 if (cost != -1.0f) {
200                         c->node = BLI_heap_insert(heap, cost, c);
201                 }
202                 else {
203                         c->node = NULL;
204                 }
205         }
206
207         while ((carr_len > 1 && !BLI_heap_is_empty(heap)) &&
208                ((carr_len >= MAXCOLORBAND) || (BLI_heap_top_value(heap) <= eps_2x)))
209         {
210                 c = BLI_heap_pop_min(heap);
211                 struct ColorResampleElem *c_next = c->next, *c_prev = c->prev;
212                 c_prev->next = c_next;
213                 c_next->prev = c_prev;
214                 /* Clear data (not essential, avoid confusion). */
215                 c->prev = c->next = NULL;
216                 c->node = NULL;
217
218                 /* Update adjacent */
219                 for (int i = 0; i < 2; i++) {
220                         struct ColorResampleElem *c_other = i ? c_next : c_prev;
221                         if (c_other->node != NULL) {
222                                 const float cost = color_sample_remove_cost(c_other);
223                                 if (cost != -1.0) {
224                                         BLI_heap_node_value_update(heap, c_other->node, cost);
225                                 }
226                                 else {
227                                         BLI_heap_remove(heap, c_other->node);
228                                         c_other->node = NULL;
229                                 }
230                         }
231                 }
232                 carr_len -= 1;
233         }
234         BLI_heap_free(heap, NULL);
235
236         /* First member is never removed. */
237         int i = 0;
238         BLI_assert(carr_len < MAXCOLORBAND);
239         if (filter_samples == false) {
240                 for (c = carr; c != NULL; c = c->next, i++) {
241                         copy_v4_v4(&coba->data[i].r, c->rgba);
242                         coba->data[i].pos = c->pos;
243                         coba->data[i].cur = i;
244                 }
245         }
246         else {
247                 for (c = carr; c != NULL; c = c->next, i++) {
248                         const int steps_prev = c->prev ? (c - c->prev) - 1 : 0;
249                         const int steps_next = c->next ? (c->next - c) - 1 : 0;
250                         if (steps_prev == 0 && steps_next == 0) {
251                                 copy_v4_v4(&coba->data[i].r, c->rgba);
252                         }
253                         else {
254                                 float rgba[4];
255                                 float rgba_accum = 1;
256                                 copy_v4_v4(rgba, c->rgba);
257
258                                 if (steps_prev) {
259                                         const float step_size = 1.0 / (float)(steps_prev + 1);
260                                         int j = steps_prev;
261                                         for (struct ColorResampleElem *c_other = c - 1; c_other != c->prev; c_other--, j--) {
262                                                 const float step_pos = (float)j * step_size;
263                                                 BLI_assert(step_pos > 0.0f && step_pos < 1.0f);
264                                                 const float f = filter_gauss(step_pos);
265                                                 madd_v4_v4fl(rgba, c_other->rgba, f);
266                                                 rgba_accum += f;
267                                         }
268                                 }
269                                 if (steps_next) {
270                                         const float step_size = 1.0 / (float)(steps_next + 1);
271                                         int j = steps_next;
272                                         for (struct ColorResampleElem *c_other = c + 1; c_other != c->next; c_other++, j--) {
273                                                 const float step_pos = (float)j * step_size;
274                                                 BLI_assert(step_pos > 0.0f && step_pos < 1.0f);
275                                                 const float f = filter_gauss(step_pos);
276                                                 madd_v4_v4fl(rgba, c_other->rgba, f);
277                                                 rgba_accum += f;
278                                         }
279                                 }
280
281                                 mul_v4_v4fl(&coba->data[i].r, rgba, 1.0f / rgba_accum);
282                         }
283                         coba->data[i].pos = c->pos;
284                         coba->data[i].cur = i;
285                 }
286         }
287         BLI_assert(i == carr_len);
288         coba->tot = i;
289         coba->cur = 0;
290
291         MEM_freeN(carr);
292 }
293
294 void BKE_colorband_init_from_table_rgba(
295         ColorBand *coba,
296         const float (*array)[4], const int array_len,
297         bool filter_samples)
298 {
299         /* Note, we could use MAXCOLORBAND here, but results of re-sampling are nicer,
300          * avoid different behavior when limit is hit. */
301         if (array_len < 2) {
302                 /* No Re-sample, just de-duplicate. */
303                 colorband_init_from_table_rgba_simple(coba, array, array_len);
304         }
305         else {
306                 /* Re-sample */
307                 colorband_init_from_table_rgba_resample(coba, array, array_len, filter_samples);
308         }
309 }
310
311 /** \} */
312
313 ColorBand *BKE_colorband_add(bool rangetype)
314 {
315         ColorBand *coba;
316
317         coba = MEM_callocN(sizeof(ColorBand), "colorband");
318         BKE_colorband_init(coba, rangetype);
319
320         return coba;
321 }
322
323 /* ------------------------------------------------------------------------- */
324
325 static float colorband_hue_interp(
326         const int ipotype_hue,
327         const float mfac, const float fac,
328         float h1, float h2)
329 {
330         float h_interp;
331         int mode = 0;
332
333 #define HUE_INTERP(h_a, h_b) ((mfac * (h_a)) + (fac * (h_b)))
334 #define HUE_MOD(h) (((h) < 1.0f) ? (h) : (h) - 1.0f)
335
336         h1 = HUE_MOD(h1);
337         h2 = HUE_MOD(h2);
338
339         BLI_assert(h1 >= 0.0f && h1 < 1.0f);
340         BLI_assert(h2 >= 0.0f && h2 < 1.0f);
341
342         switch (ipotype_hue) {
343                 case COLBAND_HUE_NEAR:
344                 {
345                         if      ((h1 < h2) && (h2 - h1) > +0.5f) mode = 1;
346                         else if ((h1 > h2) && (h2 - h1) < -0.5f) mode = 2;
347                         else                                     mode = 0;
348                         break;
349                 }
350                 case COLBAND_HUE_FAR:
351                 {
352                         if      ((h1 < h2) && (h2 - h1) < +0.5f) mode = 1;
353                         else if ((h1 > h2) && (h2 - h1) > -0.5f) mode = 2;
354                         else                                     mode = 0;
355                         break;
356                 }
357                 case COLBAND_HUE_CCW:
358                 {
359                         if (h1 > h2) mode = 2;
360                         else         mode = 0;
361                         break;
362                 }
363                 case COLBAND_HUE_CW:
364                 {
365                         if (h1 < h2) mode = 1;
366                         else         mode = 0;
367                         break;
368                 }
369         }
370
371         switch (mode) {
372                 case 0:
373                         h_interp = HUE_INTERP(h1, h2);
374                         break;
375                 case 1:
376                         h_interp = HUE_INTERP(h1 + 1.0f, h2);
377                         h_interp = HUE_MOD(h_interp);
378                         break;
379                 case 2:
380                         h_interp = HUE_INTERP(h1, h2 + 1.0f);
381                         h_interp = HUE_MOD(h_interp);
382                         break;
383         }
384
385         BLI_assert(h_interp >= 0.0f && h_interp < 1.0f);
386
387 #undef HUE_INTERP
388 #undef HUE_MOD
389
390         return h_interp;
391 }
392
393 bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4])
394 {
395         const CBData *cbd1, *cbd2, *cbd0, *cbd3;
396         float fac;
397         int ipotype;
398         int a;
399
400         if (coba == NULL || coba->tot == 0) return false;
401
402         cbd1 = coba->data;
403
404         ipotype = (coba->color_mode == COLBAND_BLEND_RGB) ? coba->ipotype : COLBAND_INTERP_LINEAR;
405
406         if (coba->tot == 1) {
407                 out[0] = cbd1->r;
408                 out[1] = cbd1->g;
409                 out[2] = cbd1->b;
410                 out[3] = cbd1->a;
411         }
412         else if ((in <= cbd1->pos) && ELEM(ipotype, COLBAND_INTERP_LINEAR, COLBAND_INTERP_EASE)) {
413                 out[0] = cbd1->r;
414                 out[1] = cbd1->g;
415                 out[2] = cbd1->b;
416                 out[3] = cbd1->a;
417         }
418         else {
419                 CBData left, right;
420
421                 /* we're looking for first pos > in */
422                 for (a = 0; a < coba->tot; a++, cbd1++) {
423                         if (cbd1->pos > in) {
424                                 break;
425                         }
426                 }
427
428                 if (a == coba->tot) {
429                         cbd2 = cbd1 - 1;
430                         right = *cbd2;
431                         right.pos = 1.0f;
432                         cbd1 = &right;
433                 }
434                 else if (a == 0) {
435                         left = *cbd1;
436                         left.pos = 0.0f;
437                         cbd2 = &left;
438                 }
439                 else {
440                         cbd2 = cbd1 - 1;
441                 }
442
443                 if ((in >= cbd1->pos) && ELEM(ipotype, COLBAND_INTERP_LINEAR, COLBAND_INTERP_EASE)) {
444                         out[0] = cbd1->r;
445                         out[1] = cbd1->g;
446                         out[2] = cbd1->b;
447                         out[3] = cbd1->a;
448                 }
449                 else {
450
451                         if (cbd2->pos != cbd1->pos) {
452                                 fac = (in - cbd1->pos) / (cbd2->pos - cbd1->pos);
453                         }
454                         else {
455                                 /* was setting to 0.0 in 2.56 & previous, but this
456                                  * is incorrect for the last element, see [#26732] */
457                                 fac = (a != coba->tot) ? 0.0f : 1.0f;
458                         }
459
460                         if (ipotype == COLBAND_INTERP_CONSTANT) {
461                                 /* constant */
462                                 out[0] = cbd2->r;
463                                 out[1] = cbd2->g;
464                                 out[2] = cbd2->b;
465                                 out[3] = cbd2->a;
466                         }
467                         else if (ipotype >= COLBAND_INTERP_B_SPLINE) {
468                                 /* ipo from right to left: 3 2 1 0 */
469                                 float t[4];
470
471                                 if (a >= coba->tot - 1) cbd0 = cbd1;
472                                 else cbd0 = cbd1 + 1;
473                                 if (a < 2) cbd3 = cbd2;
474                                 else cbd3 = cbd2 - 1;
475
476                                 CLAMP(fac, 0.0f, 1.0f);
477
478                                 if (ipotype == COLBAND_INTERP_CARDINAL) {
479                                         key_curve_position_weights(fac, t, KEY_CARDINAL);
480                                 }
481                                 else {
482                                         key_curve_position_weights(fac, t, KEY_BSPLINE);
483                                 }
484
485                                 out[0] = t[3] * cbd3->r + t[2] * cbd2->r + t[1] * cbd1->r + t[0] * cbd0->r;
486                                 out[1] = t[3] * cbd3->g + t[2] * cbd2->g + t[1] * cbd1->g + t[0] * cbd0->g;
487                                 out[2] = t[3] * cbd3->b + t[2] * cbd2->b + t[1] * cbd1->b + t[0] * cbd0->b;
488                                 out[3] = t[3] * cbd3->a + t[2] * cbd2->a + t[1] * cbd1->a + t[0] * cbd0->a;
489                                 CLAMP(out[0], 0.0f, 1.0f);
490                                 CLAMP(out[1], 0.0f, 1.0f);
491                                 CLAMP(out[2], 0.0f, 1.0f);
492                                 CLAMP(out[3], 0.0f, 1.0f);
493                         }
494                         else {
495                                 float mfac;
496
497                                 if (ipotype == COLBAND_INTERP_EASE) {
498                                         mfac = fac * fac;
499                                         fac = 3.0f * mfac - 2.0f * mfac * fac;
500                                 }
501
502                                 mfac = 1.0f - fac;
503
504                                 if (UNLIKELY(coba->color_mode == COLBAND_BLEND_HSV)) {
505                                         float col1[3], col2[3];
506
507                                         rgb_to_hsv_v(&cbd1->r, col1);
508                                         rgb_to_hsv_v(&cbd2->r, col2);
509
510                                         out[0] = colorband_hue_interp(coba->ipotype_hue, mfac, fac, col1[0], col2[0]);
511                                         out[1] = mfac * col1[1] + fac * col2[1];
512                                         out[2] = mfac * col1[2] + fac * col2[2];
513                                         out[3] = mfac * cbd1->a + fac * cbd2->a;
514
515                                         hsv_to_rgb_v(out, out);
516                                 }
517                                 else if (UNLIKELY(coba->color_mode == COLBAND_BLEND_HSL)) {
518                                         float col1[3], col2[3];
519
520                                         rgb_to_hsl_v(&cbd1->r, col1);
521                                         rgb_to_hsl_v(&cbd2->r, col2);
522
523                                         out[0] = colorband_hue_interp(coba->ipotype_hue, mfac, fac, col1[0], col2[0]);
524                                         out[1] = mfac * col1[1] + fac * col2[1];
525                                         out[2] = mfac * col1[2] + fac * col2[2];
526                                         out[3] = mfac * cbd1->a + fac * cbd2->a;
527
528                                         hsl_to_rgb_v(out, out);
529                                 }
530                                 else {
531                                         /* COLBAND_BLEND_RGB */
532                                         out[0] = mfac * cbd1->r + fac * cbd2->r;
533                                         out[1] = mfac * cbd1->g + fac * cbd2->g;
534                                         out[2] = mfac * cbd1->b + fac * cbd2->b;
535                                         out[3] = mfac * cbd1->a + fac * cbd2->a;
536                                 }
537                         }
538                 }
539         }
540         return true;   /* OK */
541 }
542
543 void BKE_colorband_evaluate_table_rgba(const ColorBand *coba, float **array, int *size)
544 {
545         int a;
546
547         *size = CM_TABLE + 1;
548         *array = MEM_callocN(sizeof(float) * (*size) * 4, "ColorBand");
549
550         for (a = 0; a < *size; a++)
551                 BKE_colorband_evaluate(coba, (float)a / (float)CM_TABLE, &(*array)[a * 4]);
552 }
553
554 static int vergcband(const void *a1, const void *a2)
555 {
556         const CBData *x1 = a1, *x2 = a2;
557
558         if (x1->pos > x2->pos) return 1;
559         else if (x1->pos < x2->pos) return -1;
560         return 0;
561 }
562
563 void BKE_colorband_update_sort(ColorBand *coba)
564 {
565         int a;
566
567         if (coba->tot < 2)
568                 return;
569
570         for (a = 0; a < coba->tot; a++)
571                 coba->data[a].cur = a;
572
573         qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
574
575         for (a = 0; a < coba->tot; a++) {
576                 if (coba->data[a].cur == coba->cur) {
577                         coba->cur = a;
578                         break;
579                 }
580         }
581 }
582
583 CBData *BKE_colorband_element_add(struct ColorBand *coba, float position)
584 {
585         if (coba->tot == MAXCOLORBAND) {
586                 return NULL;
587         }
588         else {
589                 CBData *xnew;
590
591                 xnew = &coba->data[coba->tot];
592                 xnew->pos = position;
593
594                 if (coba->tot != 0) {
595                         BKE_colorband_evaluate(coba, position, &xnew->r);
596                 }
597                 else {
598                         zero_v4(&xnew->r);
599                 }
600         }
601
602         coba->tot++;
603         coba->cur = coba->tot - 1;
604
605         BKE_colorband_update_sort(coba);
606
607         return coba->data + coba->cur;
608 }
609
610 int BKE_colorband_element_remove(struct ColorBand *coba, int index)
611 {
612         int a;
613
614         if (coba->tot < 2)
615                 return 0;
616
617         if (index < 0 || index >= coba->tot)
618                 return 0;
619
620         coba->tot--;
621         for (a = index; a < coba->tot; a++) {
622                 coba->data[a] = coba->data[a + 1];
623         }
624         if (coba->cur) coba->cur--;
625         return 1;
626 }