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