Internal node links are now cached in a per-node list, instead of being generated...
[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 void 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;
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 removed++;
203         }
204         
205         MEM_freeN(cuma->curve);
206         cuma->curve = cmp;
207         cuma->totpoint -= removed;
208 }
209
210 /* removes with flag set */
211 void curvemap_remove(CurveMap *cuma, const short flag)
212 {
213         CurveMapPoint *cmp = MEM_mallocN((cuma->totpoint) * sizeof(CurveMapPoint), "curve points");
214         int a, b, removed = 0;
215         
216         /* well, lets keep the two outer points! */
217         cmp[0] = cuma->curve[0];
218         for (a = 1, b = 1; a < cuma->totpoint - 1; a++) {
219                 if (!(cuma->curve[a].flag & flag)) {
220                         cmp[b] = cuma->curve[a];
221                         b++;
222                 }
223                 else removed++;
224         }
225         cmp[b] = cuma->curve[a];
226         
227         MEM_freeN(cuma->curve);
228         cuma->curve = cmp;
229         cuma->totpoint -= removed;
230 }
231
232 CurveMapPoint *curvemap_insert(CurveMap *cuma, float x, float y)
233 {
234         CurveMapPoint *cmp = MEM_callocN((cuma->totpoint + 1) * sizeof(CurveMapPoint), "curve points");
235         CurveMapPoint *newcmp = NULL;
236         int a, b, foundloc = 0;
237                 
238         /* insert fragments of the old one and the new point to the new curve */
239         cuma->totpoint++;
240         for (a = 0, b = 0; a < cuma->totpoint; a++) {
241                 if ((x < cuma->curve[a].x) && !foundloc) {
242                         cmp[a].x = x;
243                         cmp[a].y = y;
244                         cmp[a].flag = CUMA_SELECT;
245                         foundloc = 1;
246                         newcmp = &cmp[a];
247                 }
248                 else {
249                         cmp[a].x = cuma->curve[b].x;
250                         cmp[a].y = cuma->curve[b].y;
251                         cmp[a].flag = cuma->curve[b].flag;
252                         cmp[a].flag &= ~CUMA_SELECT; /* make sure old points don't remain selected */
253                         cmp[a].shorty = cuma->curve[b].shorty;
254                         b++;
255                 }
256         }
257
258         /* free old curve and replace it with new one */
259         MEM_freeN(cuma->curve);
260         cuma->curve = cmp;
261
262         return newcmp;
263 }
264
265 void curvemap_reset(CurveMap *cuma, const rctf *clipr, int preset, int slope)
266 {
267         if (cuma->curve)
268                 MEM_freeN(cuma->curve);
269
270         switch (preset) {
271                 case CURVE_PRESET_LINE: cuma->totpoint = 2; break;
272                 case CURVE_PRESET_SHARP: cuma->totpoint = 4; break;
273                 case CURVE_PRESET_SMOOTH: cuma->totpoint = 4; break;
274                 case CURVE_PRESET_MAX: cuma->totpoint = 2; break;
275                 case CURVE_PRESET_MID9: cuma->totpoint = 9; break;
276                 case CURVE_PRESET_ROUND: cuma->totpoint = 4; break;
277                 case CURVE_PRESET_ROOT: cuma->totpoint = 4; break;
278         }
279
280         cuma->curve = MEM_callocN(cuma->totpoint * sizeof(CurveMapPoint), "curve points");
281
282         switch (preset) {
283                 case CURVE_PRESET_LINE:
284                         cuma->curve[0].x = clipr->xmin;
285                         cuma->curve[0].y = clipr->ymax;
286                         cuma->curve[0].flag = 0;
287                         cuma->curve[1].x = clipr->xmax;
288                         cuma->curve[1].y = clipr->ymin;
289                         cuma->curve[1].flag = 0;
290                         break;
291                 case CURVE_PRESET_SHARP:
292                         cuma->curve[0].x = 0;
293                         cuma->curve[0].y = 1;
294                         cuma->curve[1].x = 0.25;
295                         cuma->curve[1].y = 0.50;
296                         cuma->curve[2].x = 0.75;
297                         cuma->curve[2].y = 0.04;
298                         cuma->curve[3].x = 1;
299                         cuma->curve[3].y = 0;
300                         break;
301                 case CURVE_PRESET_SMOOTH:
302                         cuma->curve[0].x = 0;
303                         cuma->curve[0].y = 1;
304                         cuma->curve[1].x = 0.25;
305                         cuma->curve[1].y = 0.94;
306                         cuma->curve[2].x = 0.75;
307                         cuma->curve[2].y = 0.06;
308                         cuma->curve[3].x = 1;
309                         cuma->curve[3].y = 0;
310                         break;
311                 case CURVE_PRESET_MAX:
312                         cuma->curve[0].x = 0;
313                         cuma->curve[0].y = 1;
314                         cuma->curve[1].x = 1;
315                         cuma->curve[1].y = 1;
316                         break;
317                 case CURVE_PRESET_MID9:
318                 {
319                         int i;
320                         for (i = 0; i < cuma->totpoint; i++) {
321                                 cuma->curve[i].x = i / ((float)cuma->totpoint - 1);
322                                 cuma->curve[i].y = 0.5;
323                         }
324                 }
325                 break;
326                 case CURVE_PRESET_ROUND:
327                         cuma->curve[0].x = 0;
328                         cuma->curve[0].y = 1;
329                         cuma->curve[1].x = 0.5;
330                         cuma->curve[1].y = 0.90;
331                         cuma->curve[2].x = 0.86;
332                         cuma->curve[2].y = 0.5;
333                         cuma->curve[3].x = 1;
334                         cuma->curve[3].y = 0;
335                         break;
336                 case CURVE_PRESET_ROOT:
337                         cuma->curve[0].x = 0;
338                         cuma->curve[0].y = 1;
339                         cuma->curve[1].x = 0.25;
340                         cuma->curve[1].y = 0.95;
341                         cuma->curve[2].x = 0.75;
342                         cuma->curve[2].y = 0.44;
343                         cuma->curve[3].x = 1;
344                         cuma->curve[3].y = 0;
345                         break;
346         }
347
348         /* mirror curve in x direction to have positive slope
349          * rather than default negative slope */
350         if (slope == CURVEMAP_SLOPE_POSITIVE) {
351                 int i, last = cuma->totpoint - 1;
352                 CurveMapPoint *newpoints = MEM_dupallocN(cuma->curve);
353                 
354                 for (i = 0; i < cuma->totpoint; i++) {
355                         newpoints[i].y = cuma->curve[last - i].y;
356                 }
357                 
358                 MEM_freeN(cuma->curve);
359                 cuma->curve = newpoints;
360         }
361         
362         if (cuma->table) {
363                 MEM_freeN(cuma->table);
364                 cuma->table = NULL;
365         }
366 }
367
368 /* if type==1: vector, else auto */
369 void curvemap_sethandle(CurveMap *cuma, int type)
370 {
371         int a;
372         
373         for (a = 0; a < cuma->totpoint; a++) {
374                 if (cuma->curve[a].flag & CUMA_SELECT) {
375                         if (type) cuma->curve[a].flag |= CUMA_VECTOR;
376                         else cuma->curve[a].flag &= ~CUMA_VECTOR;
377                 }
378         }
379 }
380
381 /* *********************** Making the tables and display ************** */
382
383 /* reduced copy of garbled calchandleNurb() code in curve.c */
384 static void calchandle_curvemap(BezTriple *bezt, BezTriple *prev, BezTriple *next, int UNUSED(mode))
385 {
386         float *p1, *p2, *p3, pt[3];
387         float len, len_a, len_b;
388         float dvec_a[2], dvec_b[2];
389
390         if (bezt->h1 == 0 && bezt->h2 == 0) {
391                 return;
392         }
393         
394         p2 = bezt->vec[1];
395         
396         if (prev == NULL) {
397                 p3 = next->vec[1];
398                 pt[0] = 2.0f * p2[0] - p3[0];
399                 pt[1] = 2.0f * p2[1] - p3[1];
400                 p1 = pt;
401         }
402         else {
403                 p1 = prev->vec[1];
404         }
405         
406         if (next == NULL) {
407                 p1 = prev->vec[1];
408                 pt[0] = 2.0f * p2[0] - p1[0];
409                 pt[1] = 2.0f * p2[1] - p1[1];
410                 p3 = pt;
411         }
412         else {
413                 p3 = next->vec[1];
414         }
415
416         sub_v2_v2v2(dvec_a, p2, p1);
417         sub_v2_v2v2(dvec_b, p3, p2);
418
419         len_a = len_v2(dvec_a);
420         len_b = len_v2(dvec_b);
421
422         if (len_a == 0.0f) len_a = 1.0f;
423         if (len_b == 0.0f) len_b = 1.0f;
424
425         if (bezt->h1 == HD_AUTO || bezt->h2 == HD_AUTO) { /* auto */
426                 float tvec[2];
427                 tvec[0] = dvec_b[0] / len_b + dvec_a[0] / len_a;
428                 tvec[1] = dvec_b[1] / len_b + dvec_a[1] / len_a;
429
430                 len = len_v2(tvec) * 2.5614f;
431                 if (len != 0.0f) {
432                         
433                         if (bezt->h1 == HD_AUTO) {
434                                 len_a /= len;
435                                 madd_v2_v2v2fl(p2 - 3, p2, tvec, -len_a);
436                         }
437                         if (bezt->h2 == HD_AUTO) {
438                                 len_b /= len;
439                                 madd_v2_v2v2fl(p2 + 3, p2, tvec,  len_b);
440                         }
441                 }
442         }
443
444         if (bezt->h1 == HD_VECT) {    /* vector */
445                 madd_v2_v2v2fl(p2 - 3, p2, dvec_a, -1.0f / 3.0f);
446         }
447         if (bezt->h2 == HD_VECT) {
448                 madd_v2_v2v2fl(p2 + 3, p2, dvec_b,  1.0f / 3.0f);
449         }
450 }
451
452 /* in X, out Y. 
453  * X is presumed to be outside first or last */
454 static float curvemap_calc_extend(const CurveMap *cuma, float x, const float first[2], const float last[2])
455 {
456         if (x <= first[0]) {
457                 if ((cuma->flag & CUMA_EXTEND_EXTRAPOLATE) == 0) {
458                         /* no extrapolate */
459                         return first[1];
460                 }
461                 else {
462                         if (cuma->ext_in[0] == 0.0f)
463                                 return first[1] + cuma->ext_in[1] * 10000.0f;
464                         else
465                                 return first[1] + cuma->ext_in[1] * (x - first[0]) / cuma->ext_in[0];
466                 }
467         }
468         else if (x >= last[0]) {
469                 if ((cuma->flag & CUMA_EXTEND_EXTRAPOLATE) == 0) {
470                         /* no extrapolate */
471                         return last[1];
472                 }
473                 else {
474                         if (cuma->ext_out[0] == 0.0f)
475                                 return last[1] - cuma->ext_out[1] * 10000.0f;
476                         else
477                                 return last[1] + cuma->ext_out[1] * (x - last[0]) / cuma->ext_out[0];
478                 }
479         }
480         return 0.0f;
481 }
482
483 /* only creates a table for a single channel in CurveMapping */
484 static void curvemap_make_table(CurveMap *cuma, const rctf *clipr)
485 {
486         CurveMapPoint *cmp = cuma->curve;
487         BezTriple *bezt;
488         float *fp, *allpoints, *lastpoint, curf, range;
489         int a, totpoint;
490         
491         if (cuma->curve == NULL) return;
492         
493         /* default rect also is table range */
494         cuma->mintable = clipr->xmin;
495         cuma->maxtable = clipr->xmax;
496         
497         /* hrmf... we now rely on blender ipo beziers, these are more advanced */
498         bezt = MEM_callocN(cuma->totpoint * sizeof(BezTriple), "beztarr");
499         
500         for (a = 0; a < cuma->totpoint; a++) {
501                 cuma->mintable = min_ff(cuma->mintable, cmp[a].x);
502                 cuma->maxtable = max_ff(cuma->maxtable, cmp[a].x);
503                 bezt[a].vec[1][0] = cmp[a].x;
504                 bezt[a].vec[1][1] = cmp[a].y;
505                 if (cmp[a].flag & CUMA_VECTOR)
506                         bezt[a].h1 = bezt[a].h2 = HD_VECT;
507                 else
508                         bezt[a].h1 = bezt[a].h2 = HD_AUTO;
509         }
510         
511         for (a = 0; a < cuma->totpoint; a++) {
512                 if (a == 0)
513                         calchandle_curvemap(bezt, NULL, bezt + 1, 0);
514                 else if (a == cuma->totpoint - 1)
515                         calchandle_curvemap(bezt + a, bezt + a - 1, NULL, 0);
516                 else
517                         calchandle_curvemap(bezt + a, bezt + a - 1, bezt + a + 1, 0);
518         }
519         
520         /* first and last handle need correction, instead of pointing to center of next/prev, 
521          * we let it point to the closest handle */
522         if (cuma->totpoint > 2) {
523                 float hlen, nlen, vec[3];
524                 
525                 if (bezt[0].h2 == HD_AUTO) {
526                         
527                         hlen = len_v3v3(bezt[0].vec[1], bezt[0].vec[2]); /* original handle length */
528                         /* clip handle point */
529                         copy_v3_v3(vec, bezt[1].vec[0]);
530                         if (vec[0] < bezt[0].vec[1][0])
531                                 vec[0] = bezt[0].vec[1][0];
532                         
533                         sub_v3_v3(vec, bezt[0].vec[1]);
534                         nlen = len_v3(vec);
535                         if (nlen > FLT_EPSILON) {
536                                 mul_v3_fl(vec, hlen / nlen);
537                                 add_v3_v3v3(bezt[0].vec[2], vec, bezt[0].vec[1]);
538                                 sub_v3_v3v3(bezt[0].vec[0], bezt[0].vec[1], vec);
539                         }
540                 }
541                 a = cuma->totpoint - 1;
542                 if (bezt[a].h2 == HD_AUTO) {
543                         
544                         hlen = len_v3v3(bezt[a].vec[1], bezt[a].vec[0]); /* original handle length */
545                         /* clip handle point */
546                         copy_v3_v3(vec, bezt[a - 1].vec[2]);
547                         if (vec[0] > bezt[a].vec[1][0])
548                                 vec[0] = bezt[a].vec[1][0];
549                         
550                         sub_v3_v3(vec, bezt[a].vec[1]);
551                         nlen = len_v3(vec);
552                         if (nlen > FLT_EPSILON) {
553                                 mul_v3_fl(vec, hlen / nlen);
554                                 add_v3_v3v3(bezt[a].vec[0], vec, bezt[a].vec[1]);
555                                 sub_v3_v3v3(bezt[a].vec[2], bezt[a].vec[1], vec);
556                         }
557                 }
558         }
559         /* make the bezier curve */
560         if (cuma->table)
561                 MEM_freeN(cuma->table);
562         totpoint = (cuma->totpoint - 1) * CM_RESOL;
563         fp = allpoints = MEM_callocN(totpoint * 2 * sizeof(float), "table");
564         
565         for (a = 0; a < cuma->totpoint - 1; a++, fp += 2 * CM_RESOL) {
566                 correct_bezpart(bezt[a].vec[1], bezt[a].vec[2], bezt[a + 1].vec[0], bezt[a + 1].vec[1]);
567                 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));
568                 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));
569         }
570         
571         /* store first and last handle for extrapolation, unit length */
572         cuma->ext_in[0] = bezt[0].vec[0][0] - bezt[0].vec[1][0];
573         cuma->ext_in[1] = bezt[0].vec[0][1] - bezt[0].vec[1][1];
574         range = sqrt(cuma->ext_in[0] * cuma->ext_in[0] + cuma->ext_in[1] * cuma->ext_in[1]);
575         cuma->ext_in[0] /= range;
576         cuma->ext_in[1] /= range;
577
578         a = cuma->totpoint - 1;
579         cuma->ext_out[0] = bezt[a].vec[1][0] - bezt[a].vec[2][0];
580         cuma->ext_out[1] = bezt[a].vec[1][1] - bezt[a].vec[2][1];
581         range = sqrt(cuma->ext_out[0] * cuma->ext_out[0] + cuma->ext_out[1] * cuma->ext_out[1]);
582         cuma->ext_out[0] /= range;
583         cuma->ext_out[1] /= range;
584         
585         /* cleanup */
586         MEM_freeN(bezt);
587
588         range = CM_TABLEDIV * (cuma->maxtable - cuma->mintable);
589         cuma->range = 1.0f / range;
590         
591         /* now make a table with CM_TABLE equal x distances */
592         fp = allpoints;
593         lastpoint = allpoints + 2 * (totpoint - 1);
594         cmp = MEM_callocN((CM_TABLE + 1) * sizeof(CurveMapPoint), "dist table");
595
596         for (a = 0; a <= CM_TABLE; a++) {
597                 curf = cuma->mintable + range * (float)a;
598                 cmp[a].x = curf;
599                 
600                 /* get the first x coordinate larger than curf */
601                 while (curf >= fp[0] && fp != lastpoint) {
602                         fp += 2;
603                 }
604                 if (fp == allpoints || (curf >= fp[0] && fp == lastpoint))
605                         cmp[a].y = curvemap_calc_extend(cuma, curf, allpoints, lastpoint);
606                 else {
607                         float fac1 = fp[0] - fp[-2];
608                         float fac2 = fp[0] - curf;
609                         if (fac1 > FLT_EPSILON)
610                                 fac1 = fac2 / fac1;
611                         else
612                                 fac1 = 0.0f;
613                         cmp[a].y = fac1 * fp[-1] + (1.0f - fac1) * fp[1];
614                 }
615         }
616         
617         MEM_freeN(allpoints);
618         cuma->table = cmp;
619 }
620
621 /* call when you do images etc, needs restore too. also verifies tables */
622 /* it uses a flag to prevent premul or free to happen twice */
623 void curvemapping_premultiply(CurveMapping *cumap, int restore)
624 {
625         int a;
626         
627         if (restore) {
628                 if (cumap->flag & CUMA_PREMULLED) {
629                         for (a = 0; a < 3; a++) {
630                                 MEM_freeN(cumap->cm[a].table);
631                                 cumap->cm[a].table = cumap->cm[a].premultable;
632                                 cumap->cm[a].premultable = NULL;
633                         }
634                         
635                         cumap->flag &= ~CUMA_PREMULLED;
636                 }
637         }
638         else {
639                 if ((cumap->flag & CUMA_PREMULLED) == 0) {
640                         /* verify and copy */
641                         for (a = 0; a < 3; a++) {
642                                 if (cumap->cm[a].table == NULL)
643                                         curvemap_make_table(cumap->cm + a, &cumap->clipr);
644                                 cumap->cm[a].premultable = cumap->cm[a].table;
645                                 cumap->cm[a].table = MEM_mallocN((CM_TABLE + 1) * sizeof(CurveMapPoint), "premul table");
646                                 memcpy(cumap->cm[a].table, cumap->cm[a].premultable, (CM_TABLE + 1) * sizeof(CurveMapPoint));
647                         }
648                         
649                         if (cumap->cm[3].table == NULL)
650                                 curvemap_make_table(cumap->cm + 3, &cumap->clipr);
651                 
652                         /* premul */
653                         for (a = 0; a < 3; a++) {
654                                 int b;
655                                 for (b = 0; b <= CM_TABLE; b++) {
656                                         cumap->cm[a].table[b].y = curvemap_evaluateF(cumap->cm + 3, cumap->cm[a].table[b].y);
657                                 }
658                         }
659                         
660                         cumap->flag |= CUMA_PREMULLED;
661                 }
662         }
663 }
664
665 static int sort_curvepoints(const void *a1, const void *a2)
666 {
667         const struct CurveMapPoint *x1 = a1, *x2 = a2;
668         
669         if (x1->x > x2->x) return 1;
670         else if (x1->x < x2->x) return -1;
671         return 0;
672 }
673
674 /* ************************ more CurveMapping calls *************** */
675
676 /* note; only does current curvemap! */
677 void curvemapping_changed(CurveMapping *cumap, int rem_doubles)
678 {
679         CurveMap *cuma = cumap->cm + cumap->cur;
680         CurveMapPoint *cmp = cuma->curve;
681         rctf *clipr = &cumap->clipr;
682         float thresh = 0.01f * BLI_rctf_size_x(clipr);
683         float dx = 0.0f, dy = 0.0f;
684         int a;
685
686         cumap->changed_timestamp++;
687
688         /* clamp with clip */
689         if (cumap->flag & CUMA_DO_CLIP) {
690                 for (a = 0; a < cuma->totpoint; a++) {
691                         if (cmp[a].flag & CUMA_SELECT) {
692                                 if (cmp[a].x < clipr->xmin)
693                                         dx = min_ff(dx, cmp[a].x - clipr->xmin);
694                                 else if (cmp[a].x > clipr->xmax)
695                                         dx = max_ff(dx, cmp[a].x - clipr->xmax);
696                                 if (cmp[a].y < clipr->ymin)
697                                         dy = min_ff(dy, cmp[a].y - clipr->ymin);
698                                 else if (cmp[a].y > clipr->ymax)
699                                         dy = max_ff(dy, cmp[a].y - clipr->ymax);
700                         }
701                 }
702                 for (a = 0; a < cuma->totpoint; a++) {
703                         if (cmp[a].flag & CUMA_SELECT) {
704                                 cmp[a].x -= dx;
705                                 cmp[a].y -= dy;
706                         }
707                 }
708         }
709         
710         
711         qsort(cmp, cuma->totpoint, sizeof(CurveMapPoint), sort_curvepoints);
712         
713         /* remove doubles, threshold set on 1% of default range */
714         if (rem_doubles && cuma->totpoint > 2) {
715                 for (a = 0; a < cuma->totpoint - 1; a++) {
716                         dx = cmp[a].x - cmp[a + 1].x;
717                         dy = cmp[a].y - cmp[a + 1].y;
718                         if (sqrtf(dx * dx + dy * dy) < thresh) {
719                                 if (a == 0) {
720                                         cmp[a + 1].flag |= CUMA_VECTOR;
721                                         if (cmp[a + 1].flag & CUMA_SELECT)
722                                                 cmp[a].flag |= CUMA_SELECT;
723                                 }
724                                 else {
725                                         cmp[a].flag |= CUMA_VECTOR;
726                                         if (cmp[a].flag & CUMA_SELECT)
727                                                 cmp[a + 1].flag |= CUMA_SELECT;
728                                 }
729                                 break;  /* we assume 1 deletion per edit is ok */
730                         }
731                 }
732                 if (a != cuma->totpoint - 1)
733                         curvemap_remove(cuma, 2);
734         }
735         curvemap_make_table(cuma, clipr);
736 }
737
738 void curvemapping_changed_all(CurveMapping *cumap)
739 {
740         int a, cur = cumap->cur;
741
742         for (a = 0; a < CM_TOT; a++) {
743                 if (cumap->cm[a].curve) {
744                         cumap->cur = a;
745                         curvemapping_changed(cumap, FALSE);
746                 }
747         }
748
749         cumap->cur = cur;
750 }
751
752 /* table should be verified */
753 float curvemap_evaluateF(const CurveMap *cuma, float value)
754 {
755         float fi;
756         int i;
757
758         /* index in table */
759         fi = (value - cuma->mintable) * cuma->range;
760         i = (int)fi;
761         
762         /* fi is table float index and should check against table range i.e. [0.0 CM_TABLE] */
763         if (fi < 0.0f || fi > CM_TABLE)
764                 return curvemap_calc_extend(cuma, value, &cuma->table[0].x, &cuma->table[CM_TABLE].x);
765         else {
766                 if (i < 0) return cuma->table[0].y;
767                 if (i >= CM_TABLE) return cuma->table[CM_TABLE].y;
768                 
769                 fi = fi - (float)i;
770                 return (1.0f - fi) * cuma->table[i].y + (fi) * cuma->table[i + 1].y;
771         }
772 }
773
774 /* works with curve 'cur' */
775 float curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
776 {
777         const CurveMap *cuma = cumap->cm + cur;
778         return curvemap_evaluateF(cuma, value);
779 }
780
781 /* vector case */
782 void curvemapping_evaluate3F(const CurveMapping *cumap, float vecout[3], const float vecin[3])
783 {
784         vecout[0] = curvemap_evaluateF(&cumap->cm[0], vecin[0]);
785         vecout[1] = curvemap_evaluateF(&cumap->cm[1], vecin[1]);
786         vecout[2] = curvemap_evaluateF(&cumap->cm[2], vecin[2]);
787 }
788
789 /* RGB case, no black/white points, no premult */
790 void curvemapping_evaluateRGBF(const CurveMapping *cumap, float vecout[3], const float vecin[3])
791 {
792         vecout[0] = curvemap_evaluateF(&cumap->cm[0], curvemap_evaluateF(&cumap->cm[3], vecin[0]));
793         vecout[1] = curvemap_evaluateF(&cumap->cm[1], curvemap_evaluateF(&cumap->cm[3], vecin[1]));
794         vecout[2] = curvemap_evaluateF(&cumap->cm[2], curvemap_evaluateF(&cumap->cm[3], vecin[2]));
795 }
796
797 /** same as #curvemapping_evaluate_premulRGBF
798  * but black/bwmul are passed as args for the compositor
799  * where they can change per pixel.
800  *
801  * Use in conjunction with #curvemapping_set_black_white_ex
802  *
803  * \param black Use instead of cumap->black
804  * \param bwmul Use instead of cumap->bwmul
805  */
806 void curvemapping_evaluate_premulRGBF_ex(const CurveMapping *cumap, float vecout[3], const float vecin[3],
807                                          const float black[3], const float bwmul[3])
808 {
809         vecout[0] = curvemap_evaluateF(&cumap->cm[0], (vecin[0] - black[0]) * bwmul[0]);
810         vecout[1] = curvemap_evaluateF(&cumap->cm[1], (vecin[1] - black[1]) * bwmul[1]);
811         vecout[2] = curvemap_evaluateF(&cumap->cm[2], (vecin[2] - black[2]) * bwmul[2]);
812 }
813
814 /* RGB with black/white points and premult. tables are checked */
815 void curvemapping_evaluate_premulRGBF(const CurveMapping *cumap, float vecout[3], const float vecin[3])
816 {
817         vecout[0] = curvemap_evaluateF(&cumap->cm[0], (vecin[0] - cumap->black[0]) * cumap->bwmul[0]);
818         vecout[1] = curvemap_evaluateF(&cumap->cm[1], (vecin[1] - cumap->black[1]) * cumap->bwmul[1]);
819         vecout[2] = curvemap_evaluateF(&cumap->cm[2], (vecin[2] - cumap->black[2]) * cumap->bwmul[2]);
820 }
821
822 /* same as above, byte version */
823 void curvemapping_evaluate_premulRGB(const CurveMapping *cumap, unsigned char vecout_byte[3], const unsigned char vecin_byte[3])
824 {
825         float vecin[3], vecout[3];
826
827         vecin[0] = (float) vecin_byte[0] / 255.0f;
828         vecin[1] = (float) vecin_byte[1] / 255.0f;
829         vecin[2] = (float) vecin_byte[2] / 255.0f;
830
831         curvemapping_evaluate_premulRGBF(cumap, vecout, vecin);
832
833         vecout_byte[0] = FTOCHAR(vecout[0]);
834         vecout_byte[1] = FTOCHAR(vecout[1]);
835         vecout_byte[2] = FTOCHAR(vecout[2]);
836 }
837
838 int curvemapping_RGBA_does_something(const CurveMapping *cumap)
839 {
840         int a;
841         
842         if (cumap->black[0] != 0.0f) return 1;
843         if (cumap->black[1] != 0.0f) return 1;
844         if (cumap->black[2] != 0.0f) return 1;
845         if (cumap->white[0] != 1.0f) return 1;
846         if (cumap->white[1] != 1.0f) return 1;
847         if (cumap->white[2] != 1.0f) return 1;
848         
849         for (a = 0; a < CM_TOT; a++) {
850                 if (cumap->cm[a].curve) {
851                         if (cumap->cm[a].totpoint != 2) return 1;
852                         
853                         if (cumap->cm[a].curve[0].x != 0.0f) return 1;
854                         if (cumap->cm[a].curve[0].y != 0.0f) return 1;
855                         if (cumap->cm[a].curve[1].x != 1.0f) return 1;
856                         if (cumap->cm[a].curve[1].y != 1.0f) return 1;
857                 }
858         }
859         return 0;
860 }
861
862 void curvemapping_initialize(CurveMapping *cumap)
863 {
864         int a;
865         
866         if (cumap == NULL) return;
867         
868         for (a = 0; a < CM_TOT; a++) {
869                 if (cumap->cm[a].table == NULL)
870                         curvemap_make_table(cumap->cm + a, &cumap->clipr);
871         }
872 }
873
874 void curvemapping_table_RGBA(const CurveMapping *cumap, float **array, int *size)
875 {
876         int a;
877         
878         *size = CM_TABLE + 1;
879         *array = MEM_callocN(sizeof(float) * (*size) * 4, "CurveMapping");
880
881         for (a = 0; a < *size; a++) {
882                 if (cumap->cm[0].table)
883                         (*array)[a * 4 + 0] = cumap->cm[0].table[a].y;
884                 if (cumap->cm[1].table)
885                         (*array)[a * 4 + 1] = cumap->cm[1].table[a].y;
886                 if (cumap->cm[2].table)
887                         (*array)[a * 4 + 2] = cumap->cm[2].table[a].y;
888                 if (cumap->cm[3].table)
889                         (*array)[a * 4 + 3] = cumap->cm[3].table[a].y;
890         }
891 }
892
893 /* ***************** Histogram **************** */
894
895 #define INV_255     (1.f / 255.f)
896
897 DO_INLINE int get_bin_float(float f)
898 {
899         int bin = (int)((f * 255.0f) + 0.5f);  /* 0.5 to prevent quantisation differences */
900
901         /* note: clamp integer instead of float to avoid problems with NaN */
902         CLAMP(bin, 0, 255);
903
904         return bin;
905 }
906
907 static void save_sample_line(Scopes *scopes, const int idx, const float fx, const float rgb[3], const float ycc[3])
908 {
909         float yuv[3];
910
911         /* vectorscope*/
912         rgb_to_yuv(rgb[0], rgb[1], rgb[2], &yuv[0], &yuv[1], &yuv[2]);
913         scopes->vecscope[idx + 0] = yuv[1];
914         scopes->vecscope[idx + 1] = yuv[2];
915
916         /* waveform */
917         switch (scopes->wavefrm_mode) {
918                 case SCOPES_WAVEFRM_RGB:
919                         scopes->waveform_1[idx + 0] = fx;
920                         scopes->waveform_1[idx + 1] = rgb[0];
921                         scopes->waveform_2[idx + 0] = fx;
922                         scopes->waveform_2[idx + 1] = rgb[1];
923                         scopes->waveform_3[idx + 0] = fx;
924                         scopes->waveform_3[idx + 1] = rgb[2];
925                         break;
926                 case SCOPES_WAVEFRM_LUMA:
927                         scopes->waveform_1[idx + 0] = fx;
928                         scopes->waveform_1[idx + 1] = ycc[0];
929                         break;
930                 case SCOPES_WAVEFRM_YCC_JPEG:
931                 case SCOPES_WAVEFRM_YCC_709:
932                 case SCOPES_WAVEFRM_YCC_601:
933                         scopes->waveform_1[idx + 0] = fx;
934                         scopes->waveform_1[idx + 1] = ycc[0];
935                         scopes->waveform_2[idx + 0] = fx;
936                         scopes->waveform_2[idx + 1] = ycc[1];
937                         scopes->waveform_3[idx + 0] = fx;
938                         scopes->waveform_3[idx + 1] = ycc[2];
939                         break;
940         }
941 }
942
943 void BKE_histogram_update_sample_line(Histogram *hist, ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
944                                       const ColorManagedDisplaySettings *display_settings)
945 {
946         int i, x, y;
947         float *fp;
948         float rgb[3];
949         unsigned char *cp;
950
951         int x1 = 0.5f + hist->co[0][0] * ibuf->x;
952         int x2 = 0.5f + hist->co[1][0] * ibuf->x;
953         int y1 = 0.5f + hist->co[0][1] * ibuf->y;
954         int y2 = 0.5f + hist->co[1][1] * ibuf->y;
955
956         struct ColormanageProcessor *cm_processor = NULL;
957
958         hist->channels = 3;
959         hist->x_resolution = 256;
960         hist->xmax = 1.0f;
961         /* hist->ymax = 1.0f; */ /* now do this on the operator _only_ */
962
963         if (ibuf->rect == NULL && ibuf->rect_float == NULL) return;
964
965         if (ibuf->rect_float)
966                 cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
967
968         /* persistent draw */
969         hist->flag |= HISTO_FLAG_SAMPLELINE; /* keep drawing the flag after */
970
971         for (i = 0; i < 256; i++) {
972                 x = (int)(0.5f + x1 + (float)i * (x2 - x1) / 255.0f);
973                 y = (int)(0.5f + y1 + (float)i * (y2 - y1) / 255.0f);
974
975                 if (x < 0 || y < 0 || x >= ibuf->x || y >= ibuf->y) {
976                         hist->data_luma[i] = hist->data_r[i] = hist->data_g[i] = hist->data_b[i] = hist->data_a[i] = 0.0f;
977                 }
978                 else {
979                         if (ibuf->rect_float) {
980                                 fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
981
982                                 copy_v3_v3(rgb, fp);
983                                 IMB_colormanagement_processor_apply_v3(cm_processor, rgb);
984
985                                 hist->data_luma[i]  = rgb_to_luma(rgb);
986                                 hist->data_r[i]     = rgb[0];
987                                 hist->data_g[i]     = rgb[1];
988                                 hist->data_b[i]     = rgb[2];
989                                 hist->data_a[i]     = fp[3];
990                         }
991                         else if (ibuf->rect) {
992                                 cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
993                                 hist->data_luma[i]  = (float)rgb_to_luma_byte(cp) / 255.0f;
994                                 hist->data_r[i]     = (float)cp[0] / 255.0f;
995                                 hist->data_g[i]     = (float)cp[1] / 255.0f;
996                                 hist->data_b[i]     = (float)cp[2] / 255.0f;
997                                 hist->data_a[i]     = (float)cp[3] / 255.0f;
998                         }
999                 }
1000         }
1001
1002         if (cm_processor)
1003                 IMB_colormanagement_processor_free(cm_processor);
1004 }
1005
1006 void scopes_update(Scopes *scopes, ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
1007                    const ColorManagedDisplaySettings *display_settings)
1008 {
1009         int x, y, c;
1010         unsigned int n, nl;
1011         double div, divl;
1012         float *rf = NULL;
1013         unsigned char *rc = NULL;
1014         unsigned int *bin_lum, *bin_r, *bin_g, *bin_b, *bin_a;
1015         int savedlines, saveline;
1016         float rgba[4], ycc[3], luma;
1017         int ycc_mode = -1;
1018         const short is_float = (ibuf->rect_float != NULL);
1019
1020         struct ColormanageProcessor *cm_processor = NULL;
1021
1022         if (ibuf->rect == NULL && ibuf->rect_float == NULL) return;
1023
1024         if (scopes->ok == 1) return;
1025
1026         if (scopes->hist.ymax == 0.f) scopes->hist.ymax = 1.f;
1027
1028         /* hmmmm */
1029         if (!(ELEM(ibuf->channels, 3, 4))) return;
1030
1031         scopes->hist.channels = 3;
1032         scopes->hist.x_resolution = 256;
1033
1034         switch (scopes->wavefrm_mode) {
1035                 case SCOPES_WAVEFRM_RGB:
1036                         ycc_mode = -1;
1037                         break;
1038                 case SCOPES_WAVEFRM_LUMA:
1039                 case SCOPES_WAVEFRM_YCC_JPEG:
1040                         ycc_mode = BLI_YCC_JFIF_0_255;
1041                         break;
1042                 case SCOPES_WAVEFRM_YCC_601:
1043                         ycc_mode = BLI_YCC_ITU_BT601;
1044                         break;
1045                 case SCOPES_WAVEFRM_YCC_709:
1046                         ycc_mode = BLI_YCC_ITU_BT709;
1047                         break;
1048         }
1049
1050         /* temp table to count pix value for histogram */
1051         bin_r     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1052         bin_g     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1053         bin_b     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1054         bin_a = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1055         bin_lum   = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1056
1057         /* convert to number of lines with logarithmic scale */
1058         scopes->sample_lines = (scopes->accuracy * 0.01f) * (scopes->accuracy * 0.01f) * ibuf->y;
1059         
1060         if (scopes->sample_full)
1061                 scopes->sample_lines = ibuf->y;
1062
1063         /* scan the image */
1064         savedlines = 0;
1065         for (c = 0; c < 3; c++) {
1066                 scopes->minmax[c][0] = 25500.0f;
1067                 scopes->minmax[c][1] = -25500.0f;
1068         }
1069         
1070         scopes->waveform_tot = ibuf->x * scopes->sample_lines;
1071         
1072         if (scopes->waveform_1)
1073                 MEM_freeN(scopes->waveform_1);
1074         if (scopes->waveform_2)
1075                 MEM_freeN(scopes->waveform_2);
1076         if (scopes->waveform_3)
1077                 MEM_freeN(scopes->waveform_3);
1078         if (scopes->vecscope)
1079                 MEM_freeN(scopes->vecscope);
1080         
1081         scopes->waveform_1 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 1");
1082         scopes->waveform_2 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 2");
1083         scopes->waveform_3 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 3");
1084         scopes->vecscope = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "vectorscope point channel");
1085         
1086         if (is_float)
1087                 rf = ibuf->rect_float;
1088         else
1089                 rc = (unsigned char *)ibuf->rect;
1090
1091         if (ibuf->rect_float)
1092                 cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
1093
1094         for (y = 0; y < ibuf->y; y++) {
1095                 if (savedlines < scopes->sample_lines && y >= ((savedlines) * ibuf->y) / (scopes->sample_lines + 1)) {
1096                         saveline = 1;
1097                 }
1098                 else {
1099                         saveline = 0;
1100                 }
1101                 for (x = 0; x < ibuf->x; x++) {
1102
1103                         if (is_float) {
1104                                 copy_v4_v4(rgba, rf);
1105                                 IMB_colormanagement_processor_apply_v4(cm_processor, rgba);
1106                         }
1107                         else {
1108                                 for (c = 0; c < 4; c++)
1109                                         rgba[c] = rc[c] * INV_255;
1110                         }
1111
1112                         /* we still need luma for histogram */
1113                         luma = rgb_to_luma(rgba);
1114
1115                         /* check for min max */
1116                         if (ycc_mode == -1) {
1117                                 for (c = 0; c < 3; c++) {
1118                                         if (rgba[c] < scopes->minmax[c][0]) scopes->minmax[c][0] = rgba[c];
1119                                         if (rgba[c] > scopes->minmax[c][1]) scopes->minmax[c][1] = rgba[c];
1120                                 }
1121                         }
1122                         else {
1123                                 rgb_to_ycc(rgba[0], rgba[1], rgba[2], &ycc[0], &ycc[1], &ycc[2], ycc_mode);
1124                                 for (c = 0; c < 3; c++) {
1125                                         ycc[c] *= INV_255;
1126                                         if (ycc[c] < scopes->minmax[c][0]) scopes->minmax[c][0] = ycc[c];
1127                                         if (ycc[c] > scopes->minmax[c][1]) scopes->minmax[c][1] = ycc[c];
1128                                 }
1129                         }
1130                         /* increment count for histo*/
1131                         bin_lum[get_bin_float(luma)] += 1;
1132                         bin_r[get_bin_float(rgba[0])] += 1;
1133                         bin_g[get_bin_float(rgba[1])] += 1;
1134                         bin_b[get_bin_float(rgba[2])] += 1;
1135                         bin_a[get_bin_float(rgba[3])] += 1;
1136
1137                         /* save sample if needed */
1138                         if (saveline) {
1139                                 const float fx = (float)x / (float)ibuf->x;
1140                                 const int idx = 2 * (ibuf->x * savedlines + x);
1141                                 save_sample_line(scopes, idx, fx, rgba, ycc);
1142                         }
1143
1144                         rf += ibuf->channels;
1145                         rc += ibuf->channels;
1146                 }
1147                 if (saveline)
1148                         savedlines += 1;
1149         }
1150
1151         /* convert hist data to float (proportional to max count) */
1152         n = 0;
1153         nl = 0;
1154         for (x = 0; x < 256; x++) {
1155                 if (bin_lum[x] > nl) nl = bin_lum[x];
1156                 if (bin_r[x]   > n) n = bin_r[x];
1157                 if (bin_g[x]   > n) n = bin_g[x];
1158                 if (bin_b[x]   > n) n = bin_b[x];
1159                 if (bin_a[x]   > n) n = bin_a[x];
1160         }
1161         div = 1.0 / (double)n;
1162         divl = 1.0 / (double)nl;
1163         for (x = 0; x < 256; x++) {
1164                 scopes->hist.data_luma[x] = bin_lum[x] * divl;
1165                 scopes->hist.data_r[x] = bin_r[x] * div;
1166                 scopes->hist.data_g[x] = bin_g[x] * div;
1167                 scopes->hist.data_b[x] = bin_b[x] * div;
1168                 scopes->hist.data_a[x] = bin_a[x] * div;
1169         }
1170         MEM_freeN(bin_lum);
1171         MEM_freeN(bin_r);
1172         MEM_freeN(bin_g);
1173         MEM_freeN(bin_b);
1174         MEM_freeN(bin_a);
1175
1176         if (cm_processor)
1177                 IMB_colormanagement_processor_free(cm_processor);
1178
1179         scopes->ok = 1;
1180 }
1181
1182 void scopes_free(Scopes *scopes)
1183 {
1184         if (scopes->waveform_1) {
1185                 MEM_freeN(scopes->waveform_1);
1186                 scopes->waveform_1 = NULL;
1187         }
1188         if (scopes->waveform_2) {
1189                 MEM_freeN(scopes->waveform_2);
1190                 scopes->waveform_2 = NULL;
1191         }
1192         if (scopes->waveform_3) {
1193                 MEM_freeN(scopes->waveform_3);
1194                 scopes->waveform_3 = NULL;
1195         }
1196         if (scopes->vecscope) {
1197                 MEM_freeN(scopes->vecscope);
1198                 scopes->vecscope = NULL;
1199         }
1200 }
1201
1202 void scopes_new(Scopes *scopes)
1203 {
1204         scopes->accuracy = 30.0;
1205         scopes->hist.mode = HISTO_MODE_RGB;
1206         scopes->wavefrm_alpha = 0.3;
1207         scopes->vecscope_alpha = 0.3;
1208         scopes->wavefrm_height = 100;
1209         scopes->vecscope_height = 100;
1210         scopes->hist.height = 100;
1211         scopes->ok = 0;
1212         scopes->waveform_1 = NULL;
1213         scopes->waveform_2 = NULL;
1214         scopes->waveform_3 = NULL;
1215         scopes->vecscope = NULL;
1216 }
1217
1218 void BKE_color_managed_display_settings_init(ColorManagedDisplaySettings *settings)
1219 {
1220         const char *display_name = IMB_colormanagement_display_get_default_name();
1221
1222         BLI_strncpy(settings->display_device, display_name, sizeof(settings->display_device));
1223 }
1224
1225 void BKE_color_managed_display_settings_copy(ColorManagedDisplaySettings *new_settings,
1226                                              const ColorManagedDisplaySettings *settings)
1227 {
1228         BLI_strncpy(new_settings->display_device, settings->display_device, sizeof(new_settings->display_device));
1229 }
1230
1231 void BKE_color_managed_view_settings_init(ColorManagedViewSettings *settings)
1232 {
1233         /* OCIO_TODO: use default view transform here when OCIO is completely integrated
1234          *            and proper versioning stuff is added.
1235          *            for now use NONE to be compatible with all current files
1236          */
1237         BLI_strncpy(settings->view_transform, "Default", sizeof(settings->view_transform));
1238
1239         settings->gamma = 1.0f;
1240         settings->exposure = 0.0f;
1241 }
1242
1243 void BKE_color_managed_view_settings_copy(ColorManagedViewSettings *new_settings,
1244                                           const ColorManagedViewSettings *settings)
1245 {
1246         BLI_strncpy(new_settings->view_transform, settings->view_transform, sizeof(new_settings->view_transform));
1247
1248         new_settings->flag = settings->flag;
1249         new_settings->exposure = settings->exposure;
1250         new_settings->gamma = settings->gamma;
1251
1252         if (settings->curve_mapping)
1253                 new_settings->curve_mapping = curvemapping_copy(settings->curve_mapping);
1254 }
1255
1256 void BKE_color_managed_view_settings_free(ColorManagedViewSettings *settings)
1257 {
1258         if (settings->curve_mapping)
1259                 curvemapping_free(settings->curve_mapping);
1260 }
1261
1262 void BKE_color_managed_colorspace_settings_init(ColorManagedColorspaceSettings *colorspace_settings)
1263 {
1264         BLI_strncpy(colorspace_settings->name, "", sizeof(colorspace_settings->name));
1265 }
1266
1267 void BKE_color_managed_colorspace_settings_copy(ColorManagedColorspaceSettings *colorspace_settings,
1268                                                 const ColorManagedColorspaceSettings *settings)
1269 {
1270         BLI_strncpy(colorspace_settings->name, settings->name, sizeof(colorspace_settings->name));
1271 }