Cleanup: use float versions of functions when in/output are floats
[blender.git] / source / blender / blenkernel / intern / colortools.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL/BL DUAL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/colortools.c
29  *  \ingroup bke
30  */
31
32
33 #include <string.h>
34 #include <math.h>
35 #include <stdlib.h>
36 #include <float.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_color_types.h"
41 #include "DNA_curve_types.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_math.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_curve.h"
49 #include "BKE_fcurve.h"
50
51
52 #include "IMB_colormanagement.h"
53 #include "IMB_imbuf.h"
54 #include "IMB_imbuf_types.h"
55
56 /* ********************************* color curve ********************* */
57
58 /* ***************** operations on full struct ************* */
59
60 void curvemapping_set_defaults(CurveMapping *cumap, int tot, float minx, float miny, float maxx, float maxy)
61 {
62         int a;
63         float clipminx, clipminy, clipmaxx, clipmaxy;
64         
65         cumap->flag = CUMA_DO_CLIP;
66         if (tot == 4) cumap->cur = 3;   /* rhms, hack for 'col' curve? */
67         
68         clipminx = min_ff(minx, maxx);
69         clipminy = min_ff(miny, maxy);
70         clipmaxx = max_ff(minx, maxx);
71         clipmaxy = max_ff(miny, maxy);
72         
73         BLI_rctf_init(&cumap->curr, clipminx, clipmaxx, clipminy, clipmaxy);
74         cumap->clipr = cumap->curr;
75         
76         cumap->white[0] = cumap->white[1] = cumap->white[2] = 1.0f;
77         cumap->bwmul[0] = cumap->bwmul[1] = cumap->bwmul[2] = 1.0f;
78         
79         for (a = 0; a < tot; a++) {
80                 cumap->cm[a].flag = CUMA_EXTEND_EXTRAPOLATE;
81                 cumap->cm[a].totpoint = 2;
82                 cumap->cm[a].curve = MEM_callocN(2 * sizeof(CurveMapPoint), "curve points");
83
84                 cumap->cm[a].curve[0].x = minx;
85                 cumap->cm[a].curve[0].y = miny;
86                 cumap->cm[a].curve[1].x = maxx;
87                 cumap->cm[a].curve[1].y = maxy;
88         }
89
90         cumap->changed_timestamp = 0;
91 }
92
93 CurveMapping *curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
94 {
95         CurveMapping *cumap;
96
97         cumap = MEM_callocN(sizeof(CurveMapping), "new curvemap");
98
99         curvemapping_set_defaults(cumap, tot, minx, miny, maxx, maxy);
100
101         return cumap;
102 }
103
104 void curvemapping_free_data(CurveMapping *cumap)
105 {
106         int a;
107
108         for (a = 0; a < CM_TOT; a++) {
109                 if (cumap->cm[a].curve) {
110                         MEM_freeN(cumap->cm[a].curve);
111                         cumap->cm[a].curve = NULL;
112                 }
113                 if (cumap->cm[a].table) {
114                         MEM_freeN(cumap->cm[a].table);
115                         cumap->cm[a].table = NULL;
116                 }
117                 if (cumap->cm[a].premultable) {
118                         MEM_freeN(cumap->cm[a].premultable);
119                         cumap->cm[a].premultable = NULL;
120                 }
121         }
122 }
123
124 void curvemapping_free(CurveMapping *cumap)
125 {
126         if (cumap) {
127                 curvemapping_free_data(cumap);
128                 MEM_freeN(cumap);
129         }
130 }
131
132 void curvemapping_copy_data(CurveMapping *target, CurveMapping *cumap)
133 {
134         int a;
135
136         *target = *cumap;
137
138         for (a = 0; a < CM_TOT; a++) {
139                 if (cumap->cm[a].curve)
140                         target->cm[a].curve = MEM_dupallocN(cumap->cm[a].curve);
141                 if (cumap->cm[a].table)
142                         target->cm[a].table = MEM_dupallocN(cumap->cm[a].table);
143                 if (cumap->cm[a].premultable)
144                         target->cm[a].premultable = MEM_dupallocN(cumap->cm[a].premultable);
145         }
146 }
147
148 CurveMapping *curvemapping_copy(CurveMapping *cumap)
149 {
150         if (cumap) {
151                 CurveMapping *cumapn = MEM_dupallocN(cumap);
152                 curvemapping_copy_data(cumapn, cumap);
153                 return cumapn;
154         }
155         return NULL;
156 }
157
158 void curvemapping_set_black_white_ex(const float black[3], const float white[3], float r_bwmul[3])
159 {
160         int a;
161
162         for (a = 0; a < 3; a++) {
163                 const float delta = max_ff(white[a] - black[a], 1e-5f);
164                 r_bwmul[a] = 1.0f / delta;
165         }
166 }
167
168 void curvemapping_set_black_white(CurveMapping *cumap, const float black[3], const float white[3])
169 {
170         if (white) {
171                 copy_v3_v3(cumap->white, white);
172         }
173         if (black) {
174                 copy_v3_v3(cumap->black, black);
175         }
176
177         curvemapping_set_black_white_ex(cumap->black, cumap->white, cumap->bwmul);
178         cumap->changed_timestamp++;
179 }
180
181 /* ***************** operations on single curve ************* */
182 /* ********** NOTE: requires curvemapping_changed() call after ******** */
183
184 /* remove specified point */
185 bool curvemap_remove_point(CurveMap *cuma, CurveMapPoint *point)
186 {
187         CurveMapPoint *cmp;
188         int a, b, removed = 0;
189         
190         /* must have 2 points minimum */
191         if (cuma->totpoint <= 2)
192                 return false;
193
194         cmp = MEM_mallocN((cuma->totpoint) * sizeof(CurveMapPoint), "curve points");
195
196         /* well, lets keep the two outer points! */
197         for (a = 0, b = 0; a < cuma->totpoint; a++) {
198                 if (&cuma->curve[a] != point) {
199                         cmp[b] = cuma->curve[a];
200                         b++;
201                 }
202                 else {
203                         removed++;
204                 }
205         }
206         
207         MEM_freeN(cuma->curve);
208         cuma->curve = cmp;
209         cuma->totpoint -= removed;
210         return (removed != 0);
211 }
212
213 /* removes with flag set */
214 void curvemap_remove(CurveMap *cuma, const short flag)
215 {
216         CurveMapPoint *cmp = MEM_mallocN((cuma->totpoint) * sizeof(CurveMapPoint), "curve points");
217         int a, b, removed = 0;
218         
219         /* well, lets keep the two outer points! */
220         cmp[0] = cuma->curve[0];
221         for (a = 1, b = 1; a < cuma->totpoint - 1; a++) {
222                 if (!(cuma->curve[a].flag & flag)) {
223                         cmp[b] = cuma->curve[a];
224                         b++;
225                 }
226                 else {
227                         removed++;
228                 }
229         }
230         cmp[b] = cuma->curve[a];
231         
232         MEM_freeN(cuma->curve);
233         cuma->curve = cmp;
234         cuma->totpoint -= removed;
235 }
236
237 CurveMapPoint *curvemap_insert(CurveMap *cuma, float x, float y)
238 {
239         CurveMapPoint *cmp = MEM_callocN((cuma->totpoint + 1) * sizeof(CurveMapPoint), "curve points");
240         CurveMapPoint *newcmp = NULL;
241         int a, b;
242         bool foundloc = false;
243
244         /* insert fragments of the old one and the new point to the new curve */
245         cuma->totpoint++;
246         for (a = 0, b = 0; a < cuma->totpoint; a++) {
247                 if ((foundloc == false) && ((a + 1 == cuma->totpoint) || (x < cuma->curve[a].x))) {
248                         cmp[a].x = x;
249                         cmp[a].y = y;
250                         cmp[a].flag = CUMA_SELECT;
251                         foundloc = true;
252                         newcmp = &cmp[a];
253                 }
254                 else {
255                         cmp[a].x = cuma->curve[b].x;
256                         cmp[a].y = cuma->curve[b].y;
257                         /* make sure old points don't remain selected */
258                         cmp[a].flag = cuma->curve[b].flag & ~CUMA_SELECT;
259                         cmp[a].shorty = cuma->curve[b].shorty;
260                         b++;
261                 }
262         }
263
264         /* free old curve and replace it with new one */
265         MEM_freeN(cuma->curve);
266         cuma->curve = cmp;
267
268         return newcmp;
269 }
270
271 void curvemap_reset(CurveMap *cuma, const rctf *clipr, int preset, int slope)
272 {
273         if (cuma->curve)
274                 MEM_freeN(cuma->curve);
275
276         switch (preset) {
277                 case CURVE_PRESET_LINE: cuma->totpoint = 2; break;
278                 case CURVE_PRESET_SHARP: cuma->totpoint = 4; break;
279                 case CURVE_PRESET_SMOOTH: cuma->totpoint = 4; break;
280                 case CURVE_PRESET_MAX: cuma->totpoint = 2; break;
281                 case CURVE_PRESET_MID9: cuma->totpoint = 9; break;
282                 case CURVE_PRESET_ROUND: cuma->totpoint = 4; break;
283                 case CURVE_PRESET_ROOT: cuma->totpoint = 4; break;
284         }
285
286         cuma->curve = MEM_callocN(cuma->totpoint * sizeof(CurveMapPoint), "curve points");
287
288         switch (preset) {
289                 case CURVE_PRESET_LINE:
290                         cuma->curve[0].x = clipr->xmin;
291                         cuma->curve[0].y = clipr->ymax;
292                         cuma->curve[0].flag = 0;
293                         cuma->curve[1].x = clipr->xmax;
294                         cuma->curve[1].y = clipr->ymin;
295                         cuma->curve[1].flag = 0;
296                         break;
297                 case CURVE_PRESET_SHARP:
298                         cuma->curve[0].x = 0;
299                         cuma->curve[0].y = 1;
300                         cuma->curve[1].x = 0.25;
301                         cuma->curve[1].y = 0.50;
302                         cuma->curve[2].x = 0.75;
303                         cuma->curve[2].y = 0.04;
304                         cuma->curve[3].x = 1;
305                         cuma->curve[3].y = 0;
306                         break;
307                 case CURVE_PRESET_SMOOTH:
308                         cuma->curve[0].x = 0;
309                         cuma->curve[0].y = 1;
310                         cuma->curve[1].x = 0.25;
311                         cuma->curve[1].y = 0.94;
312                         cuma->curve[2].x = 0.75;
313                         cuma->curve[2].y = 0.06;
314                         cuma->curve[3].x = 1;
315                         cuma->curve[3].y = 0;
316                         break;
317                 case CURVE_PRESET_MAX:
318                         cuma->curve[0].x = 0;
319                         cuma->curve[0].y = 1;
320                         cuma->curve[1].x = 1;
321                         cuma->curve[1].y = 1;
322                         break;
323                 case CURVE_PRESET_MID9:
324                 {
325                         int i;
326                         for (i = 0; i < cuma->totpoint; i++) {
327                                 cuma->curve[i].x = i / ((float)cuma->totpoint - 1);
328                                 cuma->curve[i].y = 0.5;
329                         }
330                         break;
331                 }
332                 case CURVE_PRESET_ROUND:
333                         cuma->curve[0].x = 0;
334                         cuma->curve[0].y = 1;
335                         cuma->curve[1].x = 0.5;
336                         cuma->curve[1].y = 0.90;
337                         cuma->curve[2].x = 0.86;
338                         cuma->curve[2].y = 0.5;
339                         cuma->curve[3].x = 1;
340                         cuma->curve[3].y = 0;
341                         break;
342                 case CURVE_PRESET_ROOT:
343                         cuma->curve[0].x = 0;
344                         cuma->curve[0].y = 1;
345                         cuma->curve[1].x = 0.25;
346                         cuma->curve[1].y = 0.95;
347                         cuma->curve[2].x = 0.75;
348                         cuma->curve[2].y = 0.44;
349                         cuma->curve[3].x = 1;
350                         cuma->curve[3].y = 0;
351                         break;
352         }
353
354         /* mirror curve in x direction to have positive slope
355          * rather than default negative slope */
356         if (slope == CURVEMAP_SLOPE_POSITIVE) {
357                 int i, last = cuma->totpoint - 1;
358                 CurveMapPoint *newpoints = MEM_dupallocN(cuma->curve);
359                 
360                 for (i = 0; i < cuma->totpoint; i++) {
361                         newpoints[i].y = cuma->curve[last - i].y;
362                 }
363                 
364                 MEM_freeN(cuma->curve);
365                 cuma->curve = newpoints;
366         }
367         
368         if (cuma->table) {
369                 MEM_freeN(cuma->table);
370                 cuma->table = NULL;
371         }
372 }
373
374 /* if type==1: vector, else auto */
375 void curvemap_sethandle(CurveMap *cuma, int type)
376 {
377         int a;
378         
379         for (a = 0; a < cuma->totpoint; a++) {
380                 if (cuma->curve[a].flag & CUMA_SELECT) {
381                         if (type) cuma->curve[a].flag |= CUMA_VECTOR;
382                         else cuma->curve[a].flag &= ~CUMA_VECTOR;
383                 }
384         }
385 }
386
387 /* *********************** Making the tables and display ************** */
388
389 /* reduced copy of garbled calchandleNurb() code in curve.c */
390 static void calchandle_curvemap(BezTriple *bezt, BezTriple *prev, BezTriple *next, int UNUSED(mode))
391 {
392         float *p1, *p2, *p3, pt[3];
393         float len, len_a, len_b;
394         float dvec_a[2], dvec_b[2];
395
396         if (bezt->h1 == 0 && bezt->h2 == 0) {
397                 return;
398         }
399         
400         p2 = bezt->vec[1];
401         
402         if (prev == NULL) {
403                 p3 = next->vec[1];
404                 pt[0] = 2.0f * p2[0] - p3[0];
405                 pt[1] = 2.0f * p2[1] - p3[1];
406                 p1 = pt;
407         }
408         else {
409                 p1 = prev->vec[1];
410         }
411         
412         if (next == NULL) {
413                 p1 = prev->vec[1];
414                 pt[0] = 2.0f * p2[0] - p1[0];
415                 pt[1] = 2.0f * p2[1] - p1[1];
416                 p3 = pt;
417         }
418         else {
419                 p3 = next->vec[1];
420         }
421
422         sub_v2_v2v2(dvec_a, p2, p1);
423         sub_v2_v2v2(dvec_b, p3, p2);
424
425         len_a = len_v2(dvec_a);
426         len_b = len_v2(dvec_b);
427
428         if (len_a == 0.0f) len_a = 1.0f;
429         if (len_b == 0.0f) len_b = 1.0f;
430
431         if (bezt->h1 == HD_AUTO || bezt->h2 == HD_AUTO) { /* auto */
432                 float tvec[2];
433                 tvec[0] = dvec_b[0] / len_b + dvec_a[0] / len_a;
434                 tvec[1] = dvec_b[1] / len_b + dvec_a[1] / len_a;
435
436                 len = len_v2(tvec) * 2.5614f;
437                 if (len != 0.0f) {
438                         
439                         if (bezt->h1 == HD_AUTO) {
440                                 len_a /= len;
441                                 madd_v2_v2v2fl(p2 - 3, p2, tvec, -len_a);
442                         }
443                         if (bezt->h2 == HD_AUTO) {
444                                 len_b /= len;
445                                 madd_v2_v2v2fl(p2 + 3, p2, tvec,  len_b);
446                         }
447                 }
448         }
449
450         if (bezt->h1 == HD_VECT) {    /* vector */
451                 madd_v2_v2v2fl(p2 - 3, p2, dvec_a, -1.0f / 3.0f);
452         }
453         if (bezt->h2 == HD_VECT) {
454                 madd_v2_v2v2fl(p2 + 3, p2, dvec_b,  1.0f / 3.0f);
455         }
456 }
457
458 /* in X, out Y. 
459  * X is presumed to be outside first or last */
460 static float curvemap_calc_extend(const CurveMap *cuma, float x, const float first[2], const float last[2])
461 {
462         if (x <= first[0]) {
463                 if ((cuma->flag & CUMA_EXTEND_EXTRAPOLATE) == 0) {
464                         /* no extrapolate */
465                         return first[1];
466                 }
467                 else {
468                         if (cuma->ext_in[0] == 0.0f)
469                                 return first[1] + cuma->ext_in[1] * 10000.0f;
470                         else
471                                 return first[1] + cuma->ext_in[1] * (x - first[0]) / cuma->ext_in[0];
472                 }
473         }
474         else if (x >= last[0]) {
475                 if ((cuma->flag & CUMA_EXTEND_EXTRAPOLATE) == 0) {
476                         /* no extrapolate */
477                         return last[1];
478                 }
479                 else {
480                         if (cuma->ext_out[0] == 0.0f)
481                                 return last[1] - cuma->ext_out[1] * 10000.0f;
482                         else
483                                 return last[1] + cuma->ext_out[1] * (x - last[0]) / cuma->ext_out[0];
484                 }
485         }
486         return 0.0f;
487 }
488
489 /* only creates a table for a single channel in CurveMapping */
490 static void curvemap_make_table(CurveMap *cuma, const rctf *clipr)
491 {
492         CurveMapPoint *cmp = cuma->curve;
493         BezTriple *bezt;
494         float *fp, *allpoints, *lastpoint, curf, range;
495         int a, totpoint;
496         
497         if (cuma->curve == NULL) return;
498         
499         /* default rect also is table range */
500         cuma->mintable = clipr->xmin;
501         cuma->maxtable = clipr->xmax;
502         
503         /* hrmf... we now rely on blender ipo beziers, these are more advanced */
504         bezt = MEM_callocN(cuma->totpoint * sizeof(BezTriple), "beztarr");
505         
506         for (a = 0; a < cuma->totpoint; a++) {
507                 cuma->mintable = min_ff(cuma->mintable, cmp[a].x);
508                 cuma->maxtable = max_ff(cuma->maxtable, cmp[a].x);
509                 bezt[a].vec[1][0] = cmp[a].x;
510                 bezt[a].vec[1][1] = cmp[a].y;
511                 if (cmp[a].flag & CUMA_VECTOR)
512                         bezt[a].h1 = bezt[a].h2 = HD_VECT;
513                 else
514                         bezt[a].h1 = bezt[a].h2 = HD_AUTO;
515         }
516         
517         for (a = 0; a < cuma->totpoint; a++) {
518                 if (a == 0)
519                         calchandle_curvemap(bezt, NULL, bezt + 1, 0);
520                 else if (a == cuma->totpoint - 1)
521                         calchandle_curvemap(bezt + a, bezt + a - 1, NULL, 0);
522                 else
523                         calchandle_curvemap(bezt + a, bezt + a - 1, bezt + a + 1, 0);
524         }
525         
526         /* first and last handle need correction, instead of pointing to center of next/prev, 
527          * we let it point to the closest handle */
528         if (cuma->totpoint > 2) {
529                 float hlen, nlen, vec[3];
530                 
531                 if (bezt[0].h2 == HD_AUTO) {
532                         
533                         hlen = len_v3v3(bezt[0].vec[1], bezt[0].vec[2]); /* original handle length */
534                         /* clip handle point */
535                         copy_v3_v3(vec, bezt[1].vec[0]);
536                         if (vec[0] < bezt[0].vec[1][0])
537                                 vec[0] = bezt[0].vec[1][0];
538                         
539                         sub_v3_v3(vec, bezt[0].vec[1]);
540                         nlen = len_v3(vec);
541                         if (nlen > FLT_EPSILON) {
542                                 mul_v3_fl(vec, hlen / nlen);
543                                 add_v3_v3v3(bezt[0].vec[2], vec, bezt[0].vec[1]);
544                                 sub_v3_v3v3(bezt[0].vec[0], bezt[0].vec[1], vec);
545                         }
546                 }
547                 a = cuma->totpoint - 1;
548                 if (bezt[a].h2 == HD_AUTO) {
549                         
550                         hlen = len_v3v3(bezt[a].vec[1], bezt[a].vec[0]); /* original handle length */
551                         /* clip handle point */
552                         copy_v3_v3(vec, bezt[a - 1].vec[2]);
553                         if (vec[0] > bezt[a].vec[1][0])
554                                 vec[0] = bezt[a].vec[1][0];
555                         
556                         sub_v3_v3(vec, bezt[a].vec[1]);
557                         nlen = len_v3(vec);
558                         if (nlen > FLT_EPSILON) {
559                                 mul_v3_fl(vec, hlen / nlen);
560                                 add_v3_v3v3(bezt[a].vec[0], vec, bezt[a].vec[1]);
561                                 sub_v3_v3v3(bezt[a].vec[2], bezt[a].vec[1], vec);
562                         }
563                 }
564         }
565         /* make the bezier curve */
566         if (cuma->table)
567                 MEM_freeN(cuma->table);
568         totpoint = (cuma->totpoint - 1) * CM_RESOL;
569         fp = allpoints = MEM_callocN(totpoint * 2 * sizeof(float), "table");
570         
571         for (a = 0; a < cuma->totpoint - 1; a++, fp += 2 * CM_RESOL) {
572                 correct_bezpart(bezt[a].vec[1], bezt[a].vec[2], bezt[a + 1].vec[0], bezt[a + 1].vec[1]);
573                 BKE_curve_forward_diff_bezier(bezt[a].vec[1][0], bezt[a].vec[2][0], bezt[a + 1].vec[0][0], bezt[a + 1].vec[1][0], fp, CM_RESOL - 1, 2 * sizeof(float));
574                 BKE_curve_forward_diff_bezier(bezt[a].vec[1][1], bezt[a].vec[2][1], bezt[a + 1].vec[0][1], bezt[a + 1].vec[1][1], fp + 1, CM_RESOL - 1, 2 * sizeof(float));
575         }
576         
577         /* store first and last handle for extrapolation, unit length */
578         cuma->ext_in[0] = bezt[0].vec[0][0] - bezt[0].vec[1][0];
579         cuma->ext_in[1] = bezt[0].vec[0][1] - bezt[0].vec[1][1];
580         range = sqrtf(cuma->ext_in[0] * cuma->ext_in[0] + cuma->ext_in[1] * cuma->ext_in[1]);
581         cuma->ext_in[0] /= range;
582         cuma->ext_in[1] /= range;
583
584         a = cuma->totpoint - 1;
585         cuma->ext_out[0] = bezt[a].vec[1][0] - bezt[a].vec[2][0];
586         cuma->ext_out[1] = bezt[a].vec[1][1] - bezt[a].vec[2][1];
587         range = sqrtf(cuma->ext_out[0] * cuma->ext_out[0] + cuma->ext_out[1] * cuma->ext_out[1]);
588         cuma->ext_out[0] /= range;
589         cuma->ext_out[1] /= range;
590         
591         /* cleanup */
592         MEM_freeN(bezt);
593
594         range = CM_TABLEDIV * (cuma->maxtable - cuma->mintable);
595         cuma->range = 1.0f / range;
596         
597         /* now make a table with CM_TABLE equal x distances */
598         fp = allpoints;
599         lastpoint = allpoints + 2 * (totpoint - 1);
600         cmp = MEM_callocN((CM_TABLE + 1) * sizeof(CurveMapPoint), "dist table");
601
602         for (a = 0; a <= CM_TABLE; a++) {
603                 curf = cuma->mintable + range * (float)a;
604                 cmp[a].x = curf;
605                 
606                 /* get the first x coordinate larger than curf */
607                 while (curf >= fp[0] && fp != lastpoint) {
608                         fp += 2;
609                 }
610                 if (fp == allpoints || (curf >= fp[0] && fp == lastpoint))
611                         cmp[a].y = curvemap_calc_extend(cuma, curf, allpoints, lastpoint);
612                 else {
613                         float fac1 = fp[0] - fp[-2];
614                         float fac2 = fp[0] - curf;
615                         if (fac1 > FLT_EPSILON)
616                                 fac1 = fac2 / fac1;
617                         else
618                                 fac1 = 0.0f;
619                         cmp[a].y = fac1 * fp[-1] + (1.0f - fac1) * fp[1];
620                 }
621         }
622         
623         MEM_freeN(allpoints);
624         cuma->table = cmp;
625 }
626
627 /* call when you do images etc, needs restore too. also verifies tables */
628 /* it uses a flag to prevent premul or free to happen twice */
629 void curvemapping_premultiply(CurveMapping *cumap, int restore)
630 {
631         int a;
632         
633         if (restore) {
634                 if (cumap->flag & CUMA_PREMULLED) {
635                         for (a = 0; a < 3; a++) {
636                                 MEM_freeN(cumap->cm[a].table);
637                                 cumap->cm[a].table = cumap->cm[a].premultable;
638                                 cumap->cm[a].premultable = NULL;
639
640                                 copy_v2_v2(cumap->cm[a].ext_in, cumap->cm[a].premul_ext_in);
641                                 copy_v2_v2(cumap->cm[a].ext_out, cumap->cm[a].premul_ext_out);
642                                 zero_v2(cumap->cm[a].premul_ext_in);
643                                 zero_v2(cumap->cm[a].premul_ext_out);
644                         }
645                         
646                         cumap->flag &= ~CUMA_PREMULLED;
647                 }
648         }
649         else {
650                 if ((cumap->flag & CUMA_PREMULLED) == 0) {
651                         /* verify and copy */
652                         for (a = 0; a < 3; a++) {
653                                 if (cumap->cm[a].table == NULL)
654                                         curvemap_make_table(cumap->cm + a, &cumap->clipr);
655                                 cumap->cm[a].premultable = cumap->cm[a].table;
656                                 cumap->cm[a].table = MEM_mallocN((CM_TABLE + 1) * sizeof(CurveMapPoint), "premul table");
657                                 memcpy(cumap->cm[a].table, cumap->cm[a].premultable, (CM_TABLE + 1) * sizeof(CurveMapPoint));
658                         }
659                         
660                         if (cumap->cm[3].table == NULL)
661                                 curvemap_make_table(cumap->cm + 3, &cumap->clipr);
662                 
663                         /* premul */
664                         for (a = 0; a < 3; a++) {
665                                 int b;
666                                 for (b = 0; b <= CM_TABLE; b++) {
667                                         cumap->cm[a].table[b].y = curvemap_evaluateF(cumap->cm + 3, cumap->cm[a].table[b].y);
668                                 }
669
670                                 copy_v2_v2(cumap->cm[a].premul_ext_in, cumap->cm[a].ext_in);
671                                 copy_v2_v2(cumap->cm[a].premul_ext_out, cumap->cm[a].ext_out);
672                                 mul_v2_v2(cumap->cm[a].ext_in, cumap->cm[3].ext_in);
673                                 mul_v2_v2(cumap->cm[a].ext_out, cumap->cm[3].ext_out);
674                         }
675                         
676                         cumap->flag |= CUMA_PREMULLED;
677                 }
678         }
679 }
680
681 static int sort_curvepoints(const void *a1, const void *a2)
682 {
683         const struct CurveMapPoint *x1 = a1, *x2 = a2;
684         
685         if (x1->x > x2->x) return 1;
686         else if (x1->x < x2->x) return -1;
687         return 0;
688 }
689
690 /* ************************ more CurveMapping calls *************** */
691
692 /* note; only does current curvemap! */
693 void curvemapping_changed(CurveMapping *cumap, const bool rem_doubles)
694 {
695         CurveMap *cuma = cumap->cm + cumap->cur;
696         CurveMapPoint *cmp = cuma->curve;
697         rctf *clipr = &cumap->clipr;
698         float thresh = 0.01f * BLI_rctf_size_x(clipr);
699         float dx = 0.0f, dy = 0.0f;
700         int a;
701
702         cumap->changed_timestamp++;
703
704         /* clamp with clip */
705         if (cumap->flag & CUMA_DO_CLIP) {
706                 for (a = 0; a < cuma->totpoint; a++) {
707                         if (cmp[a].flag & CUMA_SELECT) {
708                                 if (cmp[a].x < clipr->xmin)
709                                         dx = min_ff(dx, cmp[a].x - clipr->xmin);
710                                 else if (cmp[a].x > clipr->xmax)
711                                         dx = max_ff(dx, cmp[a].x - clipr->xmax);
712                                 if (cmp[a].y < clipr->ymin)
713                                         dy = min_ff(dy, cmp[a].y - clipr->ymin);
714                                 else if (cmp[a].y > clipr->ymax)
715                                         dy = max_ff(dy, cmp[a].y - clipr->ymax);
716                         }
717                 }
718                 for (a = 0; a < cuma->totpoint; a++) {
719                         if (cmp[a].flag & CUMA_SELECT) {
720                                 cmp[a].x -= dx;
721                                 cmp[a].y -= dy;
722                         }
723                 }
724         }
725         
726         
727         qsort(cmp, cuma->totpoint, sizeof(CurveMapPoint), sort_curvepoints);
728         
729         /* remove doubles, threshold set on 1% of default range */
730         if (rem_doubles && cuma->totpoint > 2) {
731                 for (a = 0; a < cuma->totpoint - 1; a++) {
732                         dx = cmp[a].x - cmp[a + 1].x;
733                         dy = cmp[a].y - cmp[a + 1].y;
734                         if (sqrtf(dx * dx + dy * dy) < thresh) {
735                                 if (a == 0) {
736                                         cmp[a + 1].flag |= CUMA_VECTOR;
737                                         if (cmp[a + 1].flag & CUMA_SELECT)
738                                                 cmp[a].flag |= CUMA_SELECT;
739                                 }
740                                 else {
741                                         cmp[a].flag |= CUMA_VECTOR;
742                                         if (cmp[a].flag & CUMA_SELECT)
743                                                 cmp[a + 1].flag |= CUMA_SELECT;
744                                 }
745                                 break;  /* we assume 1 deletion per edit is ok */
746                         }
747                 }
748                 if (a != cuma->totpoint - 1)
749                         curvemap_remove(cuma, 2);
750         }
751         curvemap_make_table(cuma, clipr);
752 }
753
754 void curvemapping_changed_all(CurveMapping *cumap)
755 {
756         int a, cur = cumap->cur;
757
758         for (a = 0; a < CM_TOT; a++) {
759                 if (cumap->cm[a].curve) {
760                         cumap->cur = a;
761                         curvemapping_changed(cumap, false);
762                 }
763         }
764
765         cumap->cur = cur;
766 }
767
768 /* table should be verified */
769 float curvemap_evaluateF(const CurveMap *cuma, float value)
770 {
771         float fi;
772         int i;
773
774         /* index in table */
775         fi = (value - cuma->mintable) * cuma->range;
776         i = (int)fi;
777         
778         /* fi is table float index and should check against table range i.e. [0.0 CM_TABLE] */
779         if (fi < 0.0f || fi > CM_TABLE)
780                 return curvemap_calc_extend(cuma, value, &cuma->table[0].x, &cuma->table[CM_TABLE].x);
781         else {
782                 if (i < 0) return cuma->table[0].y;
783                 if (i >= CM_TABLE) return cuma->table[CM_TABLE].y;
784                 
785                 fi = fi - (float)i;
786                 return (1.0f - fi) * cuma->table[i].y + (fi) * cuma->table[i + 1].y;
787         }
788 }
789
790 /* works with curve 'cur' */
791 float curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
792 {
793         const CurveMap *cuma = cumap->cm + cur;
794         return curvemap_evaluateF(cuma, value);
795 }
796
797 /* vector case */
798 void curvemapping_evaluate3F(const CurveMapping *cumap, float vecout[3], const float vecin[3])
799 {
800         vecout[0] = curvemap_evaluateF(&cumap->cm[0], vecin[0]);
801         vecout[1] = curvemap_evaluateF(&cumap->cm[1], vecin[1]);
802         vecout[2] = curvemap_evaluateF(&cumap->cm[2], vecin[2]);
803 }
804
805 /* RGB case, no black/white points, no premult */
806 void curvemapping_evaluateRGBF(const CurveMapping *cumap, float vecout[3], const float vecin[3])
807 {
808         vecout[0] = curvemap_evaluateF(&cumap->cm[0], curvemap_evaluateF(&cumap->cm[3], vecin[0]));
809         vecout[1] = curvemap_evaluateF(&cumap->cm[1], curvemap_evaluateF(&cumap->cm[3], vecin[1]));
810         vecout[2] = curvemap_evaluateF(&cumap->cm[2], curvemap_evaluateF(&cumap->cm[3], vecin[2]));
811 }
812
813 /** same as #curvemapping_evaluate_premulRGBF
814  * but black/bwmul are passed as args for the compositor
815  * where they can change per pixel.
816  *
817  * Use in conjunction with #curvemapping_set_black_white_ex
818  *
819  * \param black Use instead of cumap->black
820  * \param bwmul Use instead of cumap->bwmul
821  */
822 void curvemapping_evaluate_premulRGBF_ex(const CurveMapping *cumap, float vecout[3], const float vecin[3],
823                                          const float black[3], const float bwmul[3])
824 {
825         vecout[0] = curvemap_evaluateF(&cumap->cm[0], (vecin[0] - black[0]) * bwmul[0]);
826         vecout[1] = curvemap_evaluateF(&cumap->cm[1], (vecin[1] - black[1]) * bwmul[1]);
827         vecout[2] = curvemap_evaluateF(&cumap->cm[2], (vecin[2] - black[2]) * bwmul[2]);
828 }
829
830 /* RGB with black/white points and premult. tables are checked */
831 void curvemapping_evaluate_premulRGBF(const CurveMapping *cumap, float vecout[3], const float vecin[3])
832 {
833         vecout[0] = curvemap_evaluateF(&cumap->cm[0], (vecin[0] - cumap->black[0]) * cumap->bwmul[0]);
834         vecout[1] = curvemap_evaluateF(&cumap->cm[1], (vecin[1] - cumap->black[1]) * cumap->bwmul[1]);
835         vecout[2] = curvemap_evaluateF(&cumap->cm[2], (vecin[2] - cumap->black[2]) * cumap->bwmul[2]);
836 }
837
838 /* same as above, byte version */
839 void curvemapping_evaluate_premulRGB(const CurveMapping *cumap, unsigned char vecout_byte[3], const unsigned char vecin_byte[3])
840 {
841         float vecin[3], vecout[3];
842
843         vecin[0] = (float) vecin_byte[0] / 255.0f;
844         vecin[1] = (float) vecin_byte[1] / 255.0f;
845         vecin[2] = (float) vecin_byte[2] / 255.0f;
846
847         curvemapping_evaluate_premulRGBF(cumap, vecout, vecin);
848
849         vecout_byte[0] = FTOCHAR(vecout[0]);
850         vecout_byte[1] = FTOCHAR(vecout[1]);
851         vecout_byte[2] = FTOCHAR(vecout[2]);
852 }
853
854 int curvemapping_RGBA_does_something(const CurveMapping *cumap)
855 {
856         int a;
857         
858         if (cumap->black[0] != 0.0f) return 1;
859         if (cumap->black[1] != 0.0f) return 1;
860         if (cumap->black[2] != 0.0f) return 1;
861         if (cumap->white[0] != 1.0f) return 1;
862         if (cumap->white[1] != 1.0f) return 1;
863         if (cumap->white[2] != 1.0f) return 1;
864         
865         for (a = 0; a < CM_TOT; a++) {
866                 if (cumap->cm[a].curve) {
867                         if (cumap->cm[a].totpoint != 2) return 1;
868                         
869                         if (cumap->cm[a].curve[0].x != 0.0f) return 1;
870                         if (cumap->cm[a].curve[0].y != 0.0f) return 1;
871                         if (cumap->cm[a].curve[1].x != 1.0f) return 1;
872                         if (cumap->cm[a].curve[1].y != 1.0f) return 1;
873                 }
874         }
875         return 0;
876 }
877
878 void curvemapping_initialize(CurveMapping *cumap)
879 {
880         int a;
881         
882         if (cumap == NULL) return;
883         
884         for (a = 0; a < CM_TOT; a++) {
885                 if (cumap->cm[a].table == NULL)
886                         curvemap_make_table(cumap->cm + a, &cumap->clipr);
887         }
888 }
889
890 void curvemapping_table_RGBA(const CurveMapping *cumap, float **array, int *size)
891 {
892         int a;
893         
894         *size = CM_TABLE + 1;
895         *array = MEM_callocN(sizeof(float) * (*size) * 4, "CurveMapping");
896
897         for (a = 0; a < *size; a++) {
898                 if (cumap->cm[0].table)
899                         (*array)[a * 4 + 0] = cumap->cm[0].table[a].y;
900                 if (cumap->cm[1].table)
901                         (*array)[a * 4 + 1] = cumap->cm[1].table[a].y;
902                 if (cumap->cm[2].table)
903                         (*array)[a * 4 + 2] = cumap->cm[2].table[a].y;
904                 if (cumap->cm[3].table)
905                         (*array)[a * 4 + 3] = cumap->cm[3].table[a].y;
906         }
907 }
908
909 /* ***************** Histogram **************** */
910
911 #define INV_255     (1.f / 255.f)
912
913 BLI_INLINE int get_bin_float(float f)
914 {
915         int bin = (int)((f * 255.0f) + 0.5f);  /* 0.5 to prevent quantisation differences */
916
917         /* note: clamp integer instead of float to avoid problems with NaN */
918         CLAMP(bin, 0, 255);
919
920         return bin;
921 }
922
923 static void save_sample_line(Scopes *scopes, const int idx, const float fx, const float rgb[3], const float ycc[3])
924 {
925         float yuv[3];
926
927         /* vectorscope*/
928         rgb_to_yuv(rgb[0], rgb[1], rgb[2], &yuv[0], &yuv[1], &yuv[2]);
929         scopes->vecscope[idx + 0] = yuv[1];
930         scopes->vecscope[idx + 1] = yuv[2];
931
932         /* waveform */
933         switch (scopes->wavefrm_mode) {
934                 case SCOPES_WAVEFRM_RGB:
935                         scopes->waveform_1[idx + 0] = fx;
936                         scopes->waveform_1[idx + 1] = rgb[0];
937                         scopes->waveform_2[idx + 0] = fx;
938                         scopes->waveform_2[idx + 1] = rgb[1];
939                         scopes->waveform_3[idx + 0] = fx;
940                         scopes->waveform_3[idx + 1] = rgb[2];
941                         break;
942                 case SCOPES_WAVEFRM_LUMA:
943                         scopes->waveform_1[idx + 0] = fx;
944                         scopes->waveform_1[idx + 1] = ycc[0];
945                         break;
946                 case SCOPES_WAVEFRM_YCC_JPEG:
947                 case SCOPES_WAVEFRM_YCC_709:
948                 case SCOPES_WAVEFRM_YCC_601:
949                         scopes->waveform_1[idx + 0] = fx;
950                         scopes->waveform_1[idx + 1] = ycc[0];
951                         scopes->waveform_2[idx + 0] = fx;
952                         scopes->waveform_2[idx + 1] = ycc[1];
953                         scopes->waveform_3[idx + 0] = fx;
954                         scopes->waveform_3[idx + 1] = ycc[2];
955                         break;
956         }
957 }
958
959 void BKE_histogram_update_sample_line(Histogram *hist, ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
960                                       const ColorManagedDisplaySettings *display_settings)
961 {
962         int i, x, y;
963         const float *fp;
964         float rgb[3];
965         unsigned char *cp;
966
967         int x1 = 0.5f + hist->co[0][0] * ibuf->x;
968         int x2 = 0.5f + hist->co[1][0] * ibuf->x;
969         int y1 = 0.5f + hist->co[0][1] * ibuf->y;
970         int y2 = 0.5f + hist->co[1][1] * ibuf->y;
971
972         struct ColormanageProcessor *cm_processor = NULL;
973
974         hist->channels = 3;
975         hist->x_resolution = 256;
976         hist->xmax = 1.0f;
977         /* hist->ymax = 1.0f; */ /* now do this on the operator _only_ */
978
979         if (ibuf->rect == NULL && ibuf->rect_float == NULL) return;
980
981         if (ibuf->rect_float)
982                 cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
983
984         for (i = 0; i < 256; i++) {
985                 x = (int)(0.5f + x1 + (float)i * (x2 - x1) / 255.0f);
986                 y = (int)(0.5f + y1 + (float)i * (y2 - y1) / 255.0f);
987
988                 if (x < 0 || y < 0 || x >= ibuf->x || y >= ibuf->y) {
989                         hist->data_luma[i] = hist->data_r[i] = hist->data_g[i] = hist->data_b[i] = hist->data_a[i] = 0.0f;
990                 }
991                 else {
992                         if (ibuf->rect_float) {
993                                 fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
994
995                                 copy_v3_v3(rgb, fp);
996                                 IMB_colormanagement_processor_apply_v3(cm_processor, rgb);
997
998                                 hist->data_luma[i]  = rgb_to_luma(rgb);
999                                 hist->data_r[i]     = rgb[0];
1000                                 hist->data_g[i]     = rgb[1];
1001                                 hist->data_b[i]     = rgb[2];
1002                                 hist->data_a[i]     = fp[3];
1003                         }
1004                         else if (ibuf->rect) {
1005                                 cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
1006                                 hist->data_luma[i]  = (float)rgb_to_luma_byte(cp) / 255.0f;
1007                                 hist->data_r[i]     = (float)cp[0] / 255.0f;
1008                                 hist->data_g[i]     = (float)cp[1] / 255.0f;
1009                                 hist->data_b[i]     = (float)cp[2] / 255.0f;
1010                                 hist->data_a[i]     = (float)cp[3] / 255.0f;
1011                         }
1012                 }
1013         }
1014
1015         if (cm_processor)
1016                 IMB_colormanagement_processor_free(cm_processor);
1017 }
1018
1019 /* if view_settings, it also applies this to byte buffers */
1020 void scopes_update(Scopes *scopes, ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
1021                    const ColorManagedDisplaySettings *display_settings)
1022 {
1023         int x, y, c;
1024         unsigned int nl, na, nr, ng, nb;
1025         double divl, diva, divr, divg, divb;
1026         const float *rf = NULL;
1027         unsigned char *rc = NULL;
1028         unsigned int *bin_lum, *bin_r, *bin_g, *bin_b, *bin_a;
1029         int savedlines, saveline;
1030         float rgba[4], ycc[3], luma;
1031         int ycc_mode = -1;
1032         const bool is_float = (ibuf->rect_float != NULL);
1033         void *cache_handle = NULL;
1034         struct ColormanageProcessor *cm_processor = NULL;
1035
1036         if (ibuf->rect == NULL && ibuf->rect_float == NULL) return;
1037
1038         if (scopes->ok == 1) return;
1039
1040         if (scopes->hist.ymax == 0.f) scopes->hist.ymax = 1.f;
1041
1042         /* hmmmm */
1043         if (!(ELEM(ibuf->channels, 3, 4))) return;
1044
1045         scopes->hist.channels = 3;
1046         scopes->hist.x_resolution = 256;
1047
1048         switch (scopes->wavefrm_mode) {
1049                 case SCOPES_WAVEFRM_RGB:
1050                         ycc_mode = -1;
1051                         break;
1052                 case SCOPES_WAVEFRM_LUMA:
1053                 case SCOPES_WAVEFRM_YCC_JPEG:
1054                         ycc_mode = BLI_YCC_JFIF_0_255;
1055                         break;
1056                 case SCOPES_WAVEFRM_YCC_601:
1057                         ycc_mode = BLI_YCC_ITU_BT601;
1058                         break;
1059                 case SCOPES_WAVEFRM_YCC_709:
1060                         ycc_mode = BLI_YCC_ITU_BT709;
1061                         break;
1062         }
1063
1064         /* temp table to count pix value for histogram */
1065         bin_r     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1066         bin_g     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1067         bin_b     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1068         bin_a = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1069         bin_lum   = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1070
1071         /* convert to number of lines with logarithmic scale */
1072         scopes->sample_lines = (scopes->accuracy * 0.01f) * (scopes->accuracy * 0.01f) * ibuf->y;
1073         
1074         if (scopes->sample_full)
1075                 scopes->sample_lines = ibuf->y;
1076
1077         /* scan the image */
1078         savedlines = 0;
1079         for (c = 0; c < 3; c++) {
1080                 scopes->minmax[c][0] = 25500.0f;
1081                 scopes->minmax[c][1] = -25500.0f;
1082         }
1083         
1084         scopes->waveform_tot = ibuf->x * scopes->sample_lines;
1085         
1086         if (scopes->waveform_1)
1087                 MEM_freeN(scopes->waveform_1);
1088         if (scopes->waveform_2)
1089                 MEM_freeN(scopes->waveform_2);
1090         if (scopes->waveform_3)
1091                 MEM_freeN(scopes->waveform_3);
1092         if (scopes->vecscope)
1093                 MEM_freeN(scopes->vecscope);
1094         
1095         scopes->waveform_1 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 1");
1096         scopes->waveform_2 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 2");
1097         scopes->waveform_3 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 3");
1098         scopes->vecscope = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "vectorscope point channel");
1099         
1100         if (is_float)
1101                 rf = ibuf->rect_float;
1102         else {
1103                 rc = (unsigned char *)IMB_display_buffer_acquire(ibuf, view_settings, display_settings, &cache_handle);
1104         }
1105         
1106         if (ibuf->rect_float)
1107                 cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
1108
1109         for (y = 0; y < ibuf->y; y++) {
1110                 if (savedlines < scopes->sample_lines && y >= ((savedlines) * ibuf->y) / (scopes->sample_lines + 1)) {
1111                         saveline = 1;
1112                 }
1113                 else {
1114                         saveline = 0;
1115                 }
1116                 for (x = 0; x < ibuf->x; x++) {
1117
1118                         if (is_float) {
1119                                 copy_v4_v4(rgba, rf);
1120                                 IMB_colormanagement_processor_apply_v4(cm_processor, rgba);
1121                         }
1122                         else {
1123                                 for (c = 0; c < 4; c++)
1124                                         rgba[c] = rc[c] * INV_255;
1125                         }
1126
1127                         /* we still need luma for histogram */
1128                         luma = rgb_to_luma(rgba);
1129
1130                         /* check for min max */
1131                         if (ycc_mode == -1) {
1132                                 for (c = 0; c < 3; c++) {
1133                                         if (rgba[c] < scopes->minmax[c][0]) scopes->minmax[c][0] = rgba[c];
1134                                         if (rgba[c] > scopes->minmax[c][1]) scopes->minmax[c][1] = rgba[c];
1135                                 }
1136                         }
1137                         else {
1138                                 rgb_to_ycc(rgba[0], rgba[1], rgba[2], &ycc[0], &ycc[1], &ycc[2], ycc_mode);
1139                                 for (c = 0; c < 3; c++) {
1140                                         ycc[c] *= INV_255;
1141                                         if (ycc[c] < scopes->minmax[c][0]) scopes->minmax[c][0] = ycc[c];
1142                                         if (ycc[c] > scopes->minmax[c][1]) scopes->minmax[c][1] = ycc[c];
1143                                 }
1144                         }
1145                         /* increment count for histo*/
1146                         bin_lum[get_bin_float(luma)] += 1;
1147                         bin_r[get_bin_float(rgba[0])] += 1;
1148                         bin_g[get_bin_float(rgba[1])] += 1;
1149                         bin_b[get_bin_float(rgba[2])] += 1;
1150                         bin_a[get_bin_float(rgba[3])] += 1;
1151
1152                         /* save sample if needed */
1153                         if (saveline) {
1154                                 const float fx = (float)x / (float)ibuf->x;
1155                                 const int idx = 2 * (ibuf->x * savedlines + x);
1156                                 save_sample_line(scopes, idx, fx, rgba, ycc);
1157                         }
1158
1159                         rf += ibuf->channels;
1160                         rc += ibuf->channels;
1161                 }
1162                 if (saveline)
1163                         savedlines += 1;
1164         }
1165
1166         /* test for nicer distribution even - non standard, leave it out for a while */
1167 #if 0
1168         for (x = 0; x < 256; x++) {
1169                 bin_lum[x] = sqrt (bin_lum[x]);
1170                 bin_r[x] = sqrt(bin_r[x]);
1171                 bin_g[x] = sqrt(bin_g[x]);
1172                 bin_b[x] = sqrt(bin_b[x]);
1173                 bin_a[x] = sqrt(bin_a[x]);
1174         }
1175 #endif
1176         
1177         /* convert hist data to float (proportional to max count) */
1178         nl = na = nr = nb = ng = 0;
1179         for (x = 0; x < 256; x++) {
1180                 if (bin_lum[x] > nl) nl = bin_lum[x];
1181                 if (bin_r[x]   > nr) nr = bin_r[x];
1182                 if (bin_g[x]   > ng) ng = bin_g[x];
1183                 if (bin_b[x]   > nb) nb = bin_b[x];
1184                 if (bin_a[x]   > na) na = bin_a[x];
1185         }
1186         divl = nl ? 1.0 / (double)nl : 1.0;
1187         diva = na ? 1.0 / (double)na : 1.0;
1188         divr = nr ? 1.0 / (double)nr : 1.0;
1189         divg = ng ? 1.0 / (double)ng : 1.0;
1190         divb = nb ? 1.0 / (double)nb : 1.0;
1191         
1192         for (x = 0; x < 256; x++) {
1193                 scopes->hist.data_luma[x] = bin_lum[x] * divl;
1194                 scopes->hist.data_r[x] = bin_r[x] * divr;
1195                 scopes->hist.data_g[x] = bin_g[x] * divg;
1196                 scopes->hist.data_b[x] = bin_b[x] * divb;
1197                 scopes->hist.data_a[x] = bin_a[x] * diva;
1198         }
1199         MEM_freeN(bin_lum);
1200         MEM_freeN(bin_r);
1201         MEM_freeN(bin_g);
1202         MEM_freeN(bin_b);
1203         MEM_freeN(bin_a);
1204
1205         if (cm_processor)
1206                 IMB_colormanagement_processor_free(cm_processor);
1207         if (cache_handle)
1208                 IMB_display_buffer_release(cache_handle);
1209         
1210         scopes->ok = 1;
1211 }
1212
1213 void scopes_free(Scopes *scopes)
1214 {
1215         if (scopes->waveform_1) {
1216                 MEM_freeN(scopes->waveform_1);
1217                 scopes->waveform_1 = NULL;
1218         }
1219         if (scopes->waveform_2) {
1220                 MEM_freeN(scopes->waveform_2);
1221                 scopes->waveform_2 = NULL;
1222         }
1223         if (scopes->waveform_3) {
1224                 MEM_freeN(scopes->waveform_3);
1225                 scopes->waveform_3 = NULL;
1226         }
1227         if (scopes->vecscope) {
1228                 MEM_freeN(scopes->vecscope);
1229                 scopes->vecscope = NULL;
1230         }
1231 }
1232
1233 void scopes_new(Scopes *scopes)
1234 {
1235         scopes->accuracy = 30.0;
1236         scopes->hist.mode = HISTO_MODE_RGB;
1237         scopes->wavefrm_alpha = 0.3;
1238         scopes->vecscope_alpha = 0.3;
1239         scopes->wavefrm_height = 100;
1240         scopes->vecscope_height = 100;
1241         scopes->hist.height = 100;
1242         scopes->ok = 0;
1243         scopes->waveform_1 = NULL;
1244         scopes->waveform_2 = NULL;
1245         scopes->waveform_3 = NULL;
1246         scopes->vecscope = NULL;
1247 }
1248
1249 void BKE_color_managed_display_settings_init(ColorManagedDisplaySettings *settings)
1250 {
1251         const char *display_name = IMB_colormanagement_display_get_default_name();
1252
1253         BLI_strncpy(settings->display_device, display_name, sizeof(settings->display_device));
1254 }
1255
1256 void BKE_color_managed_display_settings_copy(ColorManagedDisplaySettings *new_settings,
1257                                              const ColorManagedDisplaySettings *settings)
1258 {
1259         BLI_strncpy(new_settings->display_device, settings->display_device, sizeof(new_settings->display_device));
1260 }
1261
1262 void BKE_color_managed_view_settings_init(ColorManagedViewSettings *settings)
1263 {
1264         /* OCIO_TODO: use default view transform here when OCIO is completely integrated
1265          *            and proper versioning stuff is added.
1266          *            for now use NONE to be compatible with all current files
1267          */
1268         BLI_strncpy(settings->view_transform, "Default", sizeof(settings->view_transform));
1269         BLI_strncpy(settings->look, "None", sizeof(settings->look));
1270
1271         settings->gamma = 1.0f;
1272         settings->exposure = 0.0f;
1273 }
1274
1275 void BKE_color_managed_view_settings_copy(ColorManagedViewSettings *new_settings,
1276                                           const ColorManagedViewSettings *settings)
1277 {
1278         BLI_strncpy(new_settings->look, settings->look, sizeof(new_settings->look));
1279         BLI_strncpy(new_settings->view_transform, settings->view_transform, sizeof(new_settings->view_transform));
1280
1281         new_settings->flag = settings->flag;
1282         new_settings->exposure = settings->exposure;
1283         new_settings->gamma = settings->gamma;
1284
1285         if (settings->curve_mapping)
1286                 new_settings->curve_mapping = curvemapping_copy(settings->curve_mapping);
1287         else
1288                 new_settings->curve_mapping = NULL;
1289 }
1290
1291 void BKE_color_managed_view_settings_free(ColorManagedViewSettings *settings)
1292 {
1293         if (settings->curve_mapping)
1294                 curvemapping_free(settings->curve_mapping);
1295 }
1296
1297 void BKE_color_managed_colorspace_settings_init(ColorManagedColorspaceSettings *colorspace_settings)
1298 {
1299         BLI_strncpy(colorspace_settings->name, "", sizeof(colorspace_settings->name));
1300 }
1301
1302 void BKE_color_managed_colorspace_settings_copy(ColorManagedColorspaceSettings *colorspace_settings,
1303                                                 const ColorManagedColorspaceSettings *settings)
1304 {
1305         BLI_strncpy(colorspace_settings->name, settings->name, sizeof(colorspace_settings->name));
1306 }