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