Merged changes in the trunk up to revision 51126.
[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 = minf(minx, maxx);
69         clipminy = minf(miny, maxy);
70         clipmaxx = maxf(minx, maxx);
71         clipmaxy = maxf(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 = maxf(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 = minf(cuma->mintable, cmp[a].x);
502                 cuma->maxtable = maxf(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 = minf(dx, cmp[a].x - clipr->xmin);
694                                 else if (cmp[a].x > clipr->xmax)
695                                         dx = maxf(dx, cmp[a].x - clipr->xmax);
696                                 if (cmp[a].y < clipr->ymin)
697                                         dy = minf(dy, cmp[a].y - clipr->ymin);
698                                 else if (cmp[a].y > clipr->ymax)
699                                         dy = maxf(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
839 /* only used for image editor curves */
840 void curvemapping_do_ibuf(CurveMapping *cumap, ImBuf *ibuf)
841 {
842         ImBuf *tmpbuf;
843         int pixel;
844         float *pix_in;
845         float col[3];
846         int stride = 4;
847         float *pix_out;
848         
849         if (ibuf == NULL)
850                 return;
851         if (ibuf->rect_float == NULL)
852                 IMB_float_from_rect(ibuf);
853         else if (ibuf->rect == NULL)
854                 imb_addrectImBuf(ibuf);
855         
856         if (!ibuf->rect || !ibuf->rect_float)
857                 return;
858         
859         /* work on a temp buffer, so can color manage afterwards.
860          * No worse off memory wise than comp nodes */
861         tmpbuf = IMB_dupImBuf(ibuf);
862         
863         curvemapping_premultiply(cumap, 0);
864         
865         pix_in = ibuf->rect_float;
866         pix_out = tmpbuf->rect_float;
867
868         if (ibuf->channels)
869                 stride = ibuf->channels;
870         
871         for (pixel = ibuf->x * ibuf->y; pixel > 0; pixel--, pix_in += stride, pix_out += stride) {
872                 if (stride < 3) {
873                         col[0] = curvemap_evaluateF(cumap->cm, *pix_in);
874                         
875                         pix_out[1] = pix_out[2] = pix_out[3] = pix_out[0] = col[0];
876                 }
877                 else {
878                         curvemapping_evaluate_premulRGBF(cumap, col, pix_in);
879                         pix_out[0] = col[0];
880                         pix_out[1] = col[1];
881                         pix_out[2] = col[2];
882                         if (stride > 3)
883                                 pix_out[3] = pix_in[3];
884                         else
885                                 pix_out[3] = 1.f;
886                 }
887         }
888         
889         IMB_rect_from_float(tmpbuf);
890         SWAP(unsigned int *, tmpbuf->rect, ibuf->rect);
891         IMB_freeImBuf(tmpbuf);
892         
893         curvemapping_premultiply(cumap, 1);
894 }
895
896 int curvemapping_RGBA_does_something(const CurveMapping *cumap)
897 {
898         int a;
899         
900         if (cumap->black[0] != 0.0f) return 1;
901         if (cumap->black[1] != 0.0f) return 1;
902         if (cumap->black[2] != 0.0f) return 1;
903         if (cumap->white[0] != 1.0f) return 1;
904         if (cumap->white[1] != 1.0f) return 1;
905         if (cumap->white[2] != 1.0f) return 1;
906         
907         for (a = 0; a < CM_TOT; a++) {
908                 if (cumap->cm[a].curve) {
909                         if (cumap->cm[a].totpoint != 2) return 1;
910                         
911                         if (cumap->cm[a].curve[0].x != 0.0f) return 1;
912                         if (cumap->cm[a].curve[0].y != 0.0f) return 1;
913                         if (cumap->cm[a].curve[1].x != 1.0f) return 1;
914                         if (cumap->cm[a].curve[1].y != 1.0f) return 1;
915                 }
916         }
917         return 0;
918 }
919
920 void curvemapping_initialize(CurveMapping *cumap)
921 {
922         int a;
923         
924         if (cumap == NULL) return;
925         
926         for (a = 0; a < CM_TOT; a++) {
927                 if (cumap->cm[a].table == NULL)
928                         curvemap_make_table(cumap->cm + a, &cumap->clipr);
929         }
930 }
931
932 void curvemapping_table_RGBA(const CurveMapping *cumap, float **array, int *size)
933 {
934         int a;
935         
936         *size = CM_TABLE + 1;
937         *array = MEM_callocN(sizeof(float) * (*size) * 4, "CurveMapping");
938
939         for (a = 0; a < *size; a++) {
940                 if (cumap->cm[0].table)
941                         (*array)[a * 4 + 0] = cumap->cm[0].table[a].y;
942                 if (cumap->cm[1].table)
943                         (*array)[a * 4 + 1] = cumap->cm[1].table[a].y;
944                 if (cumap->cm[2].table)
945                         (*array)[a * 4 + 2] = cumap->cm[2].table[a].y;
946                 if (cumap->cm[3].table)
947                         (*array)[a * 4 + 3] = cumap->cm[3].table[a].y;
948         }
949 }
950
951 /* ***************** Histogram **************** */
952
953 #define INV_255     (1.f / 255.f)
954
955 DO_INLINE int get_bin_float(float f)
956 {
957         int bin = (int)((f * 255.0f) + 0.5f);  /* 0.5 to prevent quantisation differences */
958
959         /* note: clamp integer instead of float to avoid problems with NaN */
960         CLAMP(bin, 0, 255);
961
962         return bin;
963 }
964
965 static void save_sample_line(Scopes *scopes, const int idx, const float fx, const float rgb[3], const float ycc[3])
966 {
967         float yuv[3];
968
969         /* vectorscope*/
970         rgb_to_yuv(rgb[0], rgb[1], rgb[2], &yuv[0], &yuv[1], &yuv[2]);
971         scopes->vecscope[idx + 0] = yuv[1];
972         scopes->vecscope[idx + 1] = yuv[2];
973
974         /* waveform */
975         switch (scopes->wavefrm_mode) {
976                 case SCOPES_WAVEFRM_RGB:
977                         scopes->waveform_1[idx + 0] = fx;
978                         scopes->waveform_1[idx + 1] = rgb[0];
979                         scopes->waveform_2[idx + 0] = fx;
980                         scopes->waveform_2[idx + 1] = rgb[1];
981                         scopes->waveform_3[idx + 0] = fx;
982                         scopes->waveform_3[idx + 1] = rgb[2];
983                         break;
984                 case SCOPES_WAVEFRM_LUMA:
985                         scopes->waveform_1[idx + 0] = fx;
986                         scopes->waveform_1[idx + 1] = ycc[0];
987                         break;
988                 case SCOPES_WAVEFRM_YCC_JPEG:
989                 case SCOPES_WAVEFRM_YCC_709:
990                 case SCOPES_WAVEFRM_YCC_601:
991                         scopes->waveform_1[idx + 0] = fx;
992                         scopes->waveform_1[idx + 1] = ycc[0];
993                         scopes->waveform_2[idx + 0] = fx;
994                         scopes->waveform_2[idx + 1] = ycc[1];
995                         scopes->waveform_3[idx + 0] = fx;
996                         scopes->waveform_3[idx + 1] = ycc[2];
997                         break;
998         }
999 }
1000
1001 void BKE_histogram_update_sample_line(Histogram *hist, ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
1002                                       const ColorManagedDisplaySettings *display_settings)
1003 {
1004         int i, x, y;
1005         float *fp;
1006         float rgb[3];
1007         unsigned char *cp;
1008
1009         int x1 = 0.5f + hist->co[0][0] * ibuf->x;
1010         int x2 = 0.5f + hist->co[1][0] * ibuf->x;
1011         int y1 = 0.5f + hist->co[0][1] * ibuf->y;
1012         int y2 = 0.5f + hist->co[1][1] * ibuf->y;
1013
1014         struct ColormanageProcessor *cm_processor = NULL;
1015
1016         hist->channels = 3;
1017         hist->x_resolution = 256;
1018         hist->xmax = 1.0f;
1019         /* hist->ymax = 1.0f; */ /* now do this on the operator _only_ */
1020
1021         if (ibuf->rect == NULL && ibuf->rect_float == NULL) return;
1022
1023         if (ibuf->rect_float)
1024                 cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
1025
1026         /* persistent draw */
1027         hist->flag |= HISTO_FLAG_SAMPLELINE; /* keep drawing the flag after */
1028
1029         for (i = 0; i < 256; i++) {
1030                 x = (int)(0.5f + x1 + (float)i * (x2 - x1) / 255.0f);
1031                 y = (int)(0.5f + y1 + (float)i * (y2 - y1) / 255.0f);
1032
1033                 if (x < 0 || y < 0 || x >= ibuf->x || y >= ibuf->y) {
1034                         hist->data_luma[i] = hist->data_r[i] = hist->data_g[i] = hist->data_b[i] = hist->data_a[i] = 0.0f;
1035                 }
1036                 else {
1037                         if (ibuf->rect_float) {
1038                                 fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
1039
1040                                 copy_v3_v3(rgb, fp);
1041                                 IMB_colormanagement_processor_apply_v3(cm_processor, rgb);
1042
1043                                 hist->data_luma[i]  = rgb_to_luma(rgb);
1044                                 hist->data_r[i]     = rgb[0];
1045                                 hist->data_g[i]     = rgb[1];
1046                                 hist->data_b[i]     = rgb[2];
1047                                 hist->data_a[i]     = fp[3];
1048                         }
1049                         else if (ibuf->rect) {
1050                                 cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
1051                                 hist->data_luma[i]  = (float)rgb_to_luma_byte(cp) / 255.0f;
1052                                 hist->data_r[i]     = (float)cp[0] / 255.0f;
1053                                 hist->data_g[i]     = (float)cp[1] / 255.0f;
1054                                 hist->data_b[i]     = (float)cp[2] / 255.0f;
1055                                 hist->data_a[i]     = (float)cp[3] / 255.0f;
1056                         }
1057                 }
1058         }
1059
1060         if (cm_processor)
1061                 IMB_colormanagement_processor_free(cm_processor);
1062 }
1063
1064 void scopes_update(Scopes *scopes, ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
1065                    const ColorManagedDisplaySettings *display_settings)
1066 {
1067         int x, y, c;
1068         unsigned int n, nl;
1069         double div, divl;
1070         float *rf = NULL;
1071         unsigned char *rc = NULL;
1072         unsigned int *bin_lum, *bin_r, *bin_g, *bin_b, *bin_a;
1073         int savedlines, saveline;
1074         float rgba[4], ycc[3], luma;
1075         int ycc_mode = -1;
1076         const short is_float = (ibuf->rect_float != NULL);
1077
1078         struct ColormanageProcessor *cm_processor = NULL;
1079
1080         if (ibuf->rect == NULL && ibuf->rect_float == NULL) return;
1081
1082         if (scopes->ok == 1) return;
1083
1084         if (scopes->hist.ymax == 0.f) scopes->hist.ymax = 1.f;
1085
1086         /* hmmmm */
1087         if (!(ELEM(ibuf->channels, 3, 4))) return;
1088
1089         scopes->hist.channels = 3;
1090         scopes->hist.x_resolution = 256;
1091
1092         switch (scopes->wavefrm_mode) {
1093                 case SCOPES_WAVEFRM_RGB:
1094                         ycc_mode = -1;
1095                         break;
1096                 case SCOPES_WAVEFRM_LUMA:
1097                 case SCOPES_WAVEFRM_YCC_JPEG:
1098                         ycc_mode = BLI_YCC_JFIF_0_255;
1099                         break;
1100                 case SCOPES_WAVEFRM_YCC_601:
1101                         ycc_mode = BLI_YCC_ITU_BT601;
1102                         break;
1103                 case SCOPES_WAVEFRM_YCC_709:
1104                         ycc_mode = BLI_YCC_ITU_BT709;
1105                         break;
1106         }
1107
1108         /* temp table to count pix value for histogram */
1109         bin_r     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1110         bin_g     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1111         bin_b     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1112         bin_a = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1113         bin_lum   = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1114
1115         /* convert to number of lines with logarithmic scale */
1116         scopes->sample_lines = (scopes->accuracy * 0.01f) * (scopes->accuracy * 0.01f) * ibuf->y;
1117         
1118         if (scopes->sample_full)
1119                 scopes->sample_lines = ibuf->y;
1120
1121         /* scan the image */
1122         savedlines = 0;
1123         for (c = 0; c < 3; c++) {
1124                 scopes->minmax[c][0] = 25500.0f;
1125                 scopes->minmax[c][1] = -25500.0f;
1126         }
1127         
1128         scopes->waveform_tot = ibuf->x * scopes->sample_lines;
1129         
1130         if (scopes->waveform_1)
1131                 MEM_freeN(scopes->waveform_1);
1132         if (scopes->waveform_2)
1133                 MEM_freeN(scopes->waveform_2);
1134         if (scopes->waveform_3)
1135                 MEM_freeN(scopes->waveform_3);
1136         if (scopes->vecscope)
1137                 MEM_freeN(scopes->vecscope);
1138         
1139         scopes->waveform_1 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 1");
1140         scopes->waveform_2 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 2");
1141         scopes->waveform_3 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 3");
1142         scopes->vecscope = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "vectorscope point channel");
1143         
1144         if (is_float)
1145                 rf = ibuf->rect_float;
1146         else
1147                 rc = (unsigned char *)ibuf->rect;
1148
1149         if (ibuf->rect_float)
1150                 cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
1151
1152         for (y = 0; y < ibuf->y; y++) {
1153                 if (savedlines < scopes->sample_lines && y >= ((savedlines) * ibuf->y) / (scopes->sample_lines + 1)) {
1154                         saveline = 1;
1155                 }
1156                 else {
1157                         saveline = 0;
1158                 }
1159                 for (x = 0; x < ibuf->x; x++) {
1160
1161                         if (is_float) {
1162                                 copy_v4_v4(rgba, rf);
1163                                 IMB_colormanagement_processor_apply_v4(cm_processor, rgba);
1164                         }
1165                         else {
1166                                 for (c = 0; c < 4; c++)
1167                                         rgba[c] = rc[c] * INV_255;
1168                         }
1169
1170                         /* we still need luma for histogram */
1171                         luma = rgb_to_luma(rgba);
1172
1173                         /* check for min max */
1174                         if (ycc_mode == -1) {
1175                                 for (c = 0; c < 3; c++) {
1176                                         if (rgba[c] < scopes->minmax[c][0]) scopes->minmax[c][0] = rgba[c];
1177                                         if (rgba[c] > scopes->minmax[c][1]) scopes->minmax[c][1] = rgba[c];
1178                                 }
1179                         }
1180                         else {
1181                                 rgb_to_ycc(rgba[0], rgba[1], rgba[2], &ycc[0], &ycc[1], &ycc[2], ycc_mode);
1182                                 for (c = 0; c < 3; c++) {
1183                                         ycc[c] *= INV_255;
1184                                         if (ycc[c] < scopes->minmax[c][0]) scopes->minmax[c][0] = ycc[c];
1185                                         if (ycc[c] > scopes->minmax[c][1]) scopes->minmax[c][1] = ycc[c];
1186                                 }
1187                         }
1188                         /* increment count for histo*/
1189                         bin_lum[get_bin_float(luma)] += 1;
1190                         bin_r[get_bin_float(rgba[0])] += 1;
1191                         bin_g[get_bin_float(rgba[1])] += 1;
1192                         bin_b[get_bin_float(rgba[2])] += 1;
1193                         bin_a[get_bin_float(rgba[3])] += 1;
1194
1195                         /* save sample if needed */
1196                         if (saveline) {
1197                                 const float fx = (float)x / (float)ibuf->x;
1198                                 const int idx = 2 * (ibuf->x * savedlines + x);
1199                                 save_sample_line(scopes, idx, fx, rgba, ycc);
1200                         }
1201
1202                         rf += ibuf->channels;
1203                         rc += ibuf->channels;
1204                 }
1205                 if (saveline)
1206                         savedlines += 1;
1207         }
1208
1209         /* convert hist data to float (proportional to max count) */
1210         n = 0;
1211         nl = 0;
1212         for (x = 0; x < 256; x++) {
1213                 if (bin_lum[x] > nl) nl = bin_lum[x];
1214                 if (bin_r[x]   > n) n = bin_r[x];
1215                 if (bin_g[x]   > n) n = bin_g[x];
1216                 if (bin_b[x]   > n) n = bin_b[x];
1217                 if (bin_a[x]   > n) n = bin_a[x];
1218         }
1219         div = 1.0 / (double)n;
1220         divl = 1.0 / (double)nl;
1221         for (x = 0; x < 256; x++) {
1222                 scopes->hist.data_luma[x] = bin_lum[x] * divl;
1223                 scopes->hist.data_r[x] = bin_r[x] * div;
1224                 scopes->hist.data_g[x] = bin_g[x] * div;
1225                 scopes->hist.data_b[x] = bin_b[x] * div;
1226                 scopes->hist.data_a[x] = bin_a[x] * div;
1227         }
1228         MEM_freeN(bin_lum);
1229         MEM_freeN(bin_r);
1230         MEM_freeN(bin_g);
1231         MEM_freeN(bin_b);
1232         MEM_freeN(bin_a);
1233
1234         if (cm_processor)
1235                 IMB_colormanagement_processor_free(cm_processor);
1236
1237         scopes->ok = 1;
1238 }
1239
1240 void scopes_free(Scopes *scopes)
1241 {
1242         if (scopes->waveform_1) {
1243                 MEM_freeN(scopes->waveform_1);
1244                 scopes->waveform_1 = NULL;
1245         }
1246         if (scopes->waveform_2) {
1247                 MEM_freeN(scopes->waveform_2);
1248                 scopes->waveform_2 = NULL;
1249         }
1250         if (scopes->waveform_3) {
1251                 MEM_freeN(scopes->waveform_3);
1252                 scopes->waveform_3 = NULL;
1253         }
1254         if (scopes->vecscope) {
1255                 MEM_freeN(scopes->vecscope);
1256                 scopes->vecscope = NULL;
1257         }
1258 }
1259
1260 void scopes_new(Scopes *scopes)
1261 {
1262         scopes->accuracy = 30.0;
1263         scopes->hist.mode = HISTO_MODE_RGB;
1264         scopes->wavefrm_alpha = 0.3;
1265         scopes->vecscope_alpha = 0.3;
1266         scopes->wavefrm_height = 100;
1267         scopes->vecscope_height = 100;
1268         scopes->hist.height = 100;
1269         scopes->ok = 0;
1270         scopes->waveform_1 = NULL;
1271         scopes->waveform_2 = NULL;
1272         scopes->waveform_3 = NULL;
1273         scopes->vecscope = NULL;
1274 }
1275
1276 void BKE_color_managed_display_settings_init(ColorManagedDisplaySettings *settings)
1277 {
1278         const char *display_name = IMB_colormanagement_display_get_default_name();
1279
1280         BLI_strncpy(settings->display_device, display_name, sizeof(settings->display_device));
1281 }
1282
1283 void BKE_color_managed_display_settings_copy(ColorManagedDisplaySettings *new_settings,
1284                                              const ColorManagedDisplaySettings *settings)
1285 {
1286         BLI_strncpy(new_settings->display_device, settings->display_device, sizeof(new_settings->display_device));
1287 }
1288
1289 void BKE_color_managed_view_settings_init(ColorManagedViewSettings *settings)
1290 {
1291         /* OCIO_TODO: use default view transform here when OCIO is completely integrated
1292          *            and proper versioning stuff is added.
1293          *            for now use NONE to be compatible with all current files
1294          */
1295         BLI_strncpy(settings->view_transform, "Default", sizeof(settings->view_transform));
1296
1297         settings->gamma = 1.0f;
1298         settings->exposure = 0.0f;
1299 }
1300
1301 void BKE_color_managed_view_settings_copy(ColorManagedViewSettings *new_settings,
1302                                           const ColorManagedViewSettings *settings)
1303 {
1304         BLI_strncpy(new_settings->view_transform, settings->view_transform, sizeof(new_settings->view_transform));
1305
1306         new_settings->flag = settings->flag;
1307         new_settings->exposure = settings->exposure;
1308         new_settings->gamma = settings->gamma;
1309
1310         if (settings->curve_mapping)
1311                 new_settings->curve_mapping = curvemapping_copy(settings->curve_mapping);
1312 }
1313
1314 void BKE_color_managed_view_settings_free(ColorManagedViewSettings *settings)
1315 {
1316         if (settings->curve_mapping)
1317                 curvemapping_free(settings->curve_mapping);
1318 }
1319
1320 void BKE_color_managed_colorspace_settings_init(ColorManagedColorspaceSettings *colorspace_settings)
1321 {
1322         BLI_strncpy(colorspace_settings->name, "", sizeof(colorspace_settings->name));
1323 }
1324
1325 void BKE_color_managed_colorspace_settings_copy(ColorManagedColorspaceSettings *colorspace_settings,
1326                                                 const ColorManagedColorspaceSettings *settings)
1327 {
1328         BLI_strncpy(colorspace_settings->name, settings->name, sizeof(colorspace_settings->name));
1329 }