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