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