Merging r50049 through r50076 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenkernel / intern / colortools.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL/BL DUAL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/colortools.c
29  *  \ingroup bke
30  */
31
32
33 #include <string.h>
34 #include <math.h>
35 #include <stdlib.h>
36 #include <float.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_color_types.h"
41 #include "DNA_curve_types.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_math.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_curve.h"
49 #include "BKE_fcurve.h"
50
51
52 #include "IMB_colormanagement.h"
53 #include "IMB_imbuf.h"
54 #include "IMB_imbuf_types.h"
55
56 /* ********************************* color curve ********************* */
57
58 /* ***************** operations on full struct ************* */
59
60 void curvemapping_set_defaults(CurveMapping *cumap, int tot, float minx, float miny, float maxx, float maxy)
61 {
62         int a;
63         float clipminx, clipminy, clipmaxx, clipmaxy;
64         
65         cumap->flag = CUMA_DO_CLIP;
66         if (tot == 4) cumap->cur = 3;   /* rhms, hack for 'col' curve? */
67         
68         clipminx = minf(minx, maxx);
69         clipminy = minf(miny, maxy);
70         clipmaxx = maxf(minx, maxx);
71         clipmaxy = maxf(miny, maxy);
72         
73         BLI_rctf_init(&cumap->curr, clipminx, clipmaxx, clipminy, clipmaxy);
74         cumap->clipr = cumap->curr;
75         
76         cumap->white[0] = cumap->white[1] = cumap->white[2] = 1.0f;
77         cumap->bwmul[0] = cumap->bwmul[1] = cumap->bwmul[2] = 1.0f;
78         
79         for (a = 0; a < tot; a++) {
80                 cumap->cm[a].flag = CUMA_EXTEND_EXTRAPOLATE;
81                 cumap->cm[a].totpoint = 2;
82                 cumap->cm[a].curve = MEM_callocN(2 * sizeof(CurveMapPoint), "curve points");
83
84                 cumap->cm[a].curve[0].x = minx;
85                 cumap->cm[a].curve[0].y = miny;
86                 cumap->cm[a].curve[1].x = maxx;
87                 cumap->cm[a].curve[1].y = maxy;
88         }       
89
90         cumap->changed_timestamp = 0;
91 }
92
93 CurveMapping *curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
94 {
95         CurveMapping *cumap;
96
97         cumap = MEM_callocN(sizeof(CurveMapping), "new curvemap");
98
99         curvemapping_set_defaults(cumap, tot, minx, miny, maxx, maxy);
100
101         return cumap;
102 }
103
104 void curvemapping_free_data(CurveMapping *cumap)
105 {
106         int a;
107
108         for (a = 0; a < CM_TOT; a++) {
109                 if (cumap->cm[a].curve) MEM_freeN(cumap->cm[a].curve);
110                 if (cumap->cm[a].table) MEM_freeN(cumap->cm[a].table);
111                 if (cumap->cm[a].premultable) MEM_freeN(cumap->cm[a].premultable);
112         }
113 }
114
115 void curvemapping_free(CurveMapping *cumap)
116 {
117         if (cumap) {
118                 curvemapping_free_data(cumap);
119                 MEM_freeN(cumap);
120         }
121 }
122
123 void curvemapping_copy_data(CurveMapping *target, CurveMapping *cumap)
124 {
125         int a;
126
127         *target = *cumap;
128
129         for (a = 0; a < CM_TOT; a++) {
130                 if (cumap->cm[a].curve)
131                         target->cm[a].curve = MEM_dupallocN(cumap->cm[a].curve);
132                 if (cumap->cm[a].table)
133                         target->cm[a].table = MEM_dupallocN(cumap->cm[a].table);
134                 if (cumap->cm[a].premultable)
135                         target->cm[a].premultable = MEM_dupallocN(cumap->cm[a].premultable);
136         }
137 }
138
139 CurveMapping *curvemapping_copy(CurveMapping *cumap)
140 {
141         if (cumap) {
142                 CurveMapping *cumapn = MEM_dupallocN(cumap);
143                 curvemapping_copy_data(cumapn, cumap);
144                 return cumapn;
145         }
146         return NULL;
147 }
148
149 void curvemapping_set_black_white_ex(const float black[3], const float white[3], float r_bwmul[3])
150 {
151         int a;
152
153         for (a = 0; a < 3; a++) {
154                 const float delta = white[a] - black[a];
155                 if (delta != 0.0f) {
156                         r_bwmul[a] = 1.0f / delta;
157                 }
158                 else {
159                         r_bwmul[a] = 0.0f;
160                 }
161         }
162 }
163
164 void curvemapping_set_black_white(CurveMapping *cumap, const float black[3], const float white[3])
165 {
166         if (white) {
167                 copy_v3_v3(cumap->white, white);
168         }
169         if (black) {
170                 copy_v3_v3(cumap->black, black);
171         }
172
173         curvemapping_set_black_white_ex(cumap->black, cumap->white, cumap->bwmul);
174 }
175
176 /* ***************** operations on single curve ************* */
177 /* ********** NOTE: requires curvemapping_changed() call after ******** */
178
179 /* remove specified point */
180 void curvemap_remove_point(CurveMap *cuma, CurveMapPoint *point)
181 {
182         CurveMapPoint *cmp;
183         int a, b, removed = 0;
184         
185         /* must have 2 points minimum */
186         if (cuma->totpoint <= 2)
187                 return;
188
189         cmp = MEM_mallocN((cuma->totpoint) * sizeof(CurveMapPoint), "curve points");
190
191         /* well, lets keep the two outer points! */
192         for (a = 0, b = 0; a < cuma->totpoint; a++) {
193                 if (&cuma->curve[a] != point) {
194                         cmp[b] = cuma->curve[a];
195                         b++;
196                 }
197                 else removed++;
198         }
199         
200         MEM_freeN(cuma->curve);
201         cuma->curve = cmp;
202         cuma->totpoint -= removed;
203 }
204
205 /* removes with flag set */
206 void curvemap_remove(CurveMap *cuma, const short flag)
207 {
208         CurveMapPoint *cmp = MEM_mallocN((cuma->totpoint) * sizeof(CurveMapPoint), "curve points");
209         int a, b, removed = 0;
210         
211         /* well, lets keep the two outer points! */
212         cmp[0] = cuma->curve[0];
213         for (a = 1, b = 1; a < cuma->totpoint - 1; a++) {
214                 if (!(cuma->curve[a].flag & flag)) {
215                         cmp[b] = cuma->curve[a];
216                         b++;
217                 }
218                 else removed++;
219         }
220         cmp[b] = cuma->curve[a];
221         
222         MEM_freeN(cuma->curve);
223         cuma->curve = cmp;
224         cuma->totpoint -= removed;
225 }
226
227 CurveMapPoint *curvemap_insert(CurveMap *cuma, float x, float y)
228 {
229         CurveMapPoint *cmp = MEM_callocN((cuma->totpoint + 1) * sizeof(CurveMapPoint), "curve points");
230         CurveMapPoint *newcmp = NULL;
231         int a, b, foundloc = 0;
232                 
233         /* insert fragments of the old one and the new point to the new curve */
234         cuma->totpoint++;
235         for (a = 0, b = 0; a < cuma->totpoint; a++) {
236                 if ((x < cuma->curve[a].x) && !foundloc) {
237                         cmp[a].x = x;
238                         cmp[a].y = y;
239                         cmp[a].flag = CUMA_SELECT;
240                         foundloc = 1;
241                         newcmp = &cmp[a];
242                 }
243                 else {
244                         cmp[a].x = cuma->curve[b].x;
245                         cmp[a].y = cuma->curve[b].y;
246                         cmp[a].flag = cuma->curve[b].flag;
247                         cmp[a].flag &= ~CUMA_SELECT; /* make sure old points don't remain selected */
248                         cmp[a].shorty = cuma->curve[b].shorty;
249                         b++;
250                 }
251         }
252
253         /* free old curve and replace it with new one */
254         MEM_freeN(cuma->curve);
255         cuma->curve = cmp;
256
257         return newcmp;
258 }
259
260 void curvemap_reset(CurveMap *cuma, rctf *clipr, int preset, int slope)
261 {
262         if (cuma->curve)
263                 MEM_freeN(cuma->curve);
264
265         switch (preset) {
266                 case CURVE_PRESET_LINE: cuma->totpoint = 2; break;
267                 case CURVE_PRESET_SHARP: cuma->totpoint = 4; break;
268                 case CURVE_PRESET_SMOOTH: cuma->totpoint = 4; break;
269                 case CURVE_PRESET_MAX: cuma->totpoint = 2; break;
270                 case CURVE_PRESET_MID9: cuma->totpoint = 9; break;
271                 case CURVE_PRESET_ROUND: cuma->totpoint = 4; break;
272                 case CURVE_PRESET_ROOT: cuma->totpoint = 4; break;
273         }
274
275         cuma->curve = MEM_callocN(cuma->totpoint * sizeof(CurveMapPoint), "curve points");
276
277         switch (preset) {
278                 case CURVE_PRESET_LINE:
279                         cuma->curve[0].x = clipr->xmin;
280                         cuma->curve[0].y = clipr->ymax;
281                         cuma->curve[0].flag = 0;
282                         cuma->curve[1].x = clipr->xmax;
283                         cuma->curve[1].y = clipr->ymin;
284                         cuma->curve[1].flag = 0;
285                         break;
286                 case CURVE_PRESET_SHARP:
287                         cuma->curve[0].x = 0;
288                         cuma->curve[0].y = 1;
289                         cuma->curve[1].x = 0.25;
290                         cuma->curve[1].y = 0.50;
291                         cuma->curve[2].x = 0.75;
292                         cuma->curve[2].y = 0.04;
293                         cuma->curve[3].x = 1;
294                         cuma->curve[3].y = 0;
295                         break;
296                 case CURVE_PRESET_SMOOTH:
297                         cuma->curve[0].x = 0;
298                         cuma->curve[0].y = 1;
299                         cuma->curve[1].x = 0.25;
300                         cuma->curve[1].y = 0.94;
301                         cuma->curve[2].x = 0.75;
302                         cuma->curve[2].y = 0.06;
303                         cuma->curve[3].x = 1;
304                         cuma->curve[3].y = 0;
305                         break;
306                 case CURVE_PRESET_MAX:
307                         cuma->curve[0].x = 0;
308                         cuma->curve[0].y = 1;
309                         cuma->curve[1].x = 1;
310                         cuma->curve[1].y = 1;
311                         break;
312                 case CURVE_PRESET_MID9:
313                 {
314                         int i;
315                         for (i = 0; i < cuma->totpoint; i++) {
316                                 cuma->curve[i].x = i / ((float)cuma->totpoint - 1);
317                                 cuma->curve[i].y = 0.5;
318                         }
319                 }
320                 break;
321                 case CURVE_PRESET_ROUND:
322                         cuma->curve[0].x = 0;
323                         cuma->curve[0].y = 1;
324                         cuma->curve[1].x = 0.5;
325                         cuma->curve[1].y = 0.90;
326                         cuma->curve[2].x = 0.86;
327                         cuma->curve[2].y = 0.5;
328                         cuma->curve[3].x = 1;
329                         cuma->curve[3].y = 0;
330                         break;
331                 case CURVE_PRESET_ROOT:
332                         cuma->curve[0].x = 0;
333                         cuma->curve[0].y = 1;
334                         cuma->curve[1].x = 0.25;
335                         cuma->curve[1].y = 0.95;
336                         cuma->curve[2].x = 0.75;
337                         cuma->curve[2].y = 0.44;
338                         cuma->curve[3].x = 1;
339                         cuma->curve[3].y = 0;
340                         break;
341         }
342
343         /* mirror curve in x direction to have positive slope
344          * rather than default negative slope */
345         if (slope == CURVEMAP_SLOPE_POSITIVE) {
346                 int i, last = cuma->totpoint - 1;
347                 CurveMapPoint *newpoints = MEM_dupallocN(cuma->curve);
348                 
349                 for (i = 0; i < cuma->totpoint; i++) {
350                         newpoints[i].y = cuma->curve[last - i].y;
351                 }
352                 
353                 MEM_freeN(cuma->curve);
354                 cuma->curve = newpoints;
355         }
356         
357         if (cuma->table) {
358                 MEM_freeN(cuma->table);
359                 cuma->table = NULL;
360         }
361 }
362
363 /* if type==1: vector, else auto */
364 void curvemap_sethandle(CurveMap *cuma, int type)
365 {
366         int a;
367         
368         for (a = 0; a < cuma->totpoint; a++) {
369                 if (cuma->curve[a].flag & CUMA_SELECT) {
370                         if (type) cuma->curve[a].flag |= CUMA_VECTOR;
371                         else cuma->curve[a].flag &= ~CUMA_VECTOR;
372                 }
373         }
374 }
375
376 /* *********************** Making the tables and display ************** */
377
378 /* reduced copy of garbled calchandleNurb() code in curve.c */
379 static void calchandle_curvemap(BezTriple *bezt, BezTriple *prev, BezTriple *next, int UNUSED(mode))
380 {
381         float *p1, *p2, *p3, pt[3];
382         float len, len_a, len_b;
383         float dvec_a[2], dvec_b[2];
384
385         if (bezt->h1 == 0 && bezt->h2 == 0) {
386                 return;
387         }
388         
389         p2 = bezt->vec[1];
390         
391         if (prev == NULL) {
392                 p3 = next->vec[1];
393                 pt[0] = 2.0f * p2[0] - p3[0];
394                 pt[1] = 2.0f * p2[1] - p3[1];
395                 p1 = pt;
396         }
397         else {
398                 p1 = prev->vec[1];
399         }
400         
401         if (next == NULL) {
402                 p1 = prev->vec[1];
403                 pt[0] = 2.0f * p2[0] - p1[0];
404                 pt[1] = 2.0f * p2[1] - p1[1];
405                 p3 = pt;
406         }
407         else {
408                 p3 = next->vec[1];
409         }
410
411         sub_v2_v2v2(dvec_a, p2, p1);
412         sub_v2_v2v2(dvec_b, p3, p2);
413
414         len_a = len_v2(dvec_a);
415         len_b = len_v2(dvec_b);
416
417         if (len_a == 0.0f) len_a = 1.0f;
418         if (len_b == 0.0f) len_b = 1.0f;
419
420         if (bezt->h1 == HD_AUTO || bezt->h2 == HD_AUTO) { /* auto */
421                 float tvec[2];
422                 tvec[0] = dvec_b[0] / len_b + dvec_a[0] / len_a;
423                 tvec[1] = dvec_b[1] / len_b + dvec_a[1] / len_a;
424
425                 len = len_v2(tvec) * 2.5614f;
426                 if (len != 0.0f) {
427                         
428                         if (bezt->h1 == HD_AUTO) {
429                                 len_a /= len;
430                                 madd_v2_v2v2fl(p2 - 3, p2, tvec, -len_a);
431                         }
432                         if (bezt->h2 == HD_AUTO) {
433                                 len_b /= len;
434                                 madd_v2_v2v2fl(p2 + 3, p2, tvec,  len_b);
435                         }
436                 }
437         }
438
439         if (bezt->h1 == HD_VECT) {    /* vector */
440                 madd_v2_v2v2fl(p2 - 3, p2, dvec_a, -1.0f / 3.0f);
441         }
442         if (bezt->h2 == HD_VECT) {
443                 madd_v2_v2v2fl(p2 + 3, p2, dvec_b,  1.0f / 3.0f);
444         }
445 }
446
447 /* in X, out Y. 
448  * X is presumed to be outside first or last */
449 static float curvemap_calc_extend(CurveMap *cuma, float x, const float first[2], const float last[2])
450 {
451         if (x <= first[0]) {
452                 if ((cuma->flag & CUMA_EXTEND_EXTRAPOLATE) == 0) {
453                         /* no extrapolate */
454                         return first[1];
455                 }
456                 else {
457                         if (cuma->ext_in[0] == 0.0f)
458                                 return first[1] + cuma->ext_in[1] * 10000.0f;
459                         else
460                                 return first[1] + cuma->ext_in[1] * (x - first[0]) / cuma->ext_in[0];
461                 }
462         }
463         else if (x >= last[0]) {
464                 if ((cuma->flag & CUMA_EXTEND_EXTRAPOLATE) == 0) {
465                         /* no extrapolate */
466                         return last[1];
467                 }
468                 else {
469                         if (cuma->ext_out[0] == 0.0f)
470                                 return last[1] - cuma->ext_out[1] * 10000.0f;
471                         else
472                                 return last[1] + cuma->ext_out[1] * (x - last[0]) / cuma->ext_out[0];
473                 }
474         }
475         return 0.0f;
476 }
477
478 /* only creates a table for a single channel in CurveMapping */
479 static void curvemap_make_table(CurveMap *cuma, rctf *clipr)
480 {
481         CurveMapPoint *cmp = cuma->curve;
482         BezTriple *bezt;
483         float *fp, *allpoints, *lastpoint, curf, range;
484         int a, totpoint;
485         
486         if (cuma->curve == NULL) return;
487         
488         /* default rect also is table range */
489         cuma->mintable = clipr->xmin;
490         cuma->maxtable = clipr->xmax;
491         
492         /* hrmf... we now rely on blender ipo beziers, these are more advanced */
493         bezt = MEM_callocN(cuma->totpoint * sizeof(BezTriple), "beztarr");
494         
495         for (a = 0; a < cuma->totpoint; a++) {
496                 cuma->mintable = minf(cuma->mintable, cmp[a].x);
497                 cuma->maxtable = maxf(cuma->maxtable, cmp[a].x);
498                 bezt[a].vec[1][0] = cmp[a].x;
499                 bezt[a].vec[1][1] = cmp[a].y;
500                 if (cmp[a].flag & CUMA_VECTOR)
501                         bezt[a].h1 = bezt[a].h2 = HD_VECT;
502                 else
503                         bezt[a].h1 = bezt[a].h2 = HD_AUTO;
504         }
505         
506         for (a = 0; a < cuma->totpoint; a++) {
507                 if (a == 0)
508                         calchandle_curvemap(bezt, NULL, bezt + 1, 0);
509                 else if (a == cuma->totpoint - 1)
510                         calchandle_curvemap(bezt + a, bezt + a - 1, NULL, 0);
511                 else
512                         calchandle_curvemap(bezt + a, bezt + a - 1, bezt + a + 1, 0);
513         }
514         
515         /* first and last handle need correction, instead of pointing to center of next/prev, 
516          * we let it point to the closest handle */
517         if (cuma->totpoint > 2) {
518                 float hlen, nlen, vec[3];
519                 
520                 if (bezt[0].h2 == HD_AUTO) {
521                         
522                         hlen = len_v3v3(bezt[0].vec[1], bezt[0].vec[2]); /* original handle length */
523                         /* clip handle point */
524                         copy_v3_v3(vec, bezt[1].vec[0]);
525                         if (vec[0] < bezt[0].vec[1][0])
526                                 vec[0] = bezt[0].vec[1][0];
527                         
528                         sub_v3_v3(vec, bezt[0].vec[1]);
529                         nlen = len_v3(vec);
530                         if (nlen > FLT_EPSILON) {
531                                 mul_v3_fl(vec, hlen / nlen);
532                                 add_v3_v3v3(bezt[0].vec[2], vec, bezt[0].vec[1]);
533                                 sub_v3_v3v3(bezt[0].vec[0], bezt[0].vec[1], vec);
534                         }
535                 }
536                 a = cuma->totpoint - 1;
537                 if (bezt[a].h2 == HD_AUTO) {
538                         
539                         hlen = len_v3v3(bezt[a].vec[1], bezt[a].vec[0]); /* original handle length */
540                         /* clip handle point */
541                         copy_v3_v3(vec, bezt[a - 1].vec[2]);
542                         if (vec[0] > bezt[a].vec[1][0])
543                                 vec[0] = bezt[a].vec[1][0];
544                         
545                         sub_v3_v3(vec, bezt[a].vec[1]);
546                         nlen = len_v3(vec);
547                         if (nlen > FLT_EPSILON) {
548                                 mul_v3_fl(vec, hlen / nlen);
549                                 add_v3_v3v3(bezt[a].vec[0], vec, bezt[a].vec[1]);
550                                 sub_v3_v3v3(bezt[a].vec[2], bezt[a].vec[1], vec);
551                         }
552                 }
553         }       
554         /* make the bezier curve */
555         if (cuma->table)
556                 MEM_freeN(cuma->table);
557         totpoint = (cuma->totpoint - 1) * CM_RESOL;
558         fp = allpoints = MEM_callocN(totpoint * 2 * sizeof(float), "table");
559         
560         for (a = 0; a < cuma->totpoint - 1; a++, fp += 2 * CM_RESOL) {
561                 correct_bezpart(bezt[a].vec[1], bezt[a].vec[2], bezt[a + 1].vec[0], bezt[a + 1].vec[1]);
562                 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));
563                 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));
564         }
565         
566         /* store first and last handle for extrapolation, unit length */
567         cuma->ext_in[0] = bezt[0].vec[0][0] - bezt[0].vec[1][0];
568         cuma->ext_in[1] = bezt[0].vec[0][1] - bezt[0].vec[1][1];
569         range = sqrt(cuma->ext_in[0] * cuma->ext_in[0] + cuma->ext_in[1] * cuma->ext_in[1]);
570         cuma->ext_in[0] /= range;
571         cuma->ext_in[1] /= range;
572
573         a = cuma->totpoint - 1;
574         cuma->ext_out[0] = bezt[a].vec[1][0] - bezt[a].vec[2][0];
575         cuma->ext_out[1] = bezt[a].vec[1][1] - bezt[a].vec[2][1];
576         range = sqrt(cuma->ext_out[0] * cuma->ext_out[0] + cuma->ext_out[1] * cuma->ext_out[1]);
577         cuma->ext_out[0] /= range;
578         cuma->ext_out[1] /= range;
579         
580         /* cleanup */
581         MEM_freeN(bezt);
582
583         range = CM_TABLEDIV * (cuma->maxtable - cuma->mintable);
584         cuma->range = 1.0f / range;
585         
586         /* now make a table with CM_TABLE equal x distances */
587         fp = allpoints;
588         lastpoint = allpoints + 2 * (totpoint - 1);
589         cmp = MEM_callocN((CM_TABLE + 1) * sizeof(CurveMapPoint), "dist table");
590
591         for (a = 0; a <= CM_TABLE; a++) {
592                 curf = cuma->mintable + range * (float)a;
593                 cmp[a].x = curf;
594                 
595                 /* get the first x coordinate larger than curf */
596                 while (curf >= fp[0] && fp != lastpoint) {
597                         fp += 2;
598                 }
599                 if (fp == allpoints || (curf >= fp[0] && fp == lastpoint))
600                         cmp[a].y = curvemap_calc_extend(cuma, curf, allpoints, lastpoint);
601                 else {
602                         float fac1 = fp[0] - fp[-2];
603                         float fac2 = fp[0] - curf;
604                         if (fac1 > FLT_EPSILON)
605                                 fac1 = fac2 / fac1;
606                         else
607                                 fac1 = 0.0f;
608                         cmp[a].y = fac1 * fp[-1] + (1.0f - fac1) * fp[1];
609                 }
610         }
611         
612         MEM_freeN(allpoints);
613         cuma->table = cmp;
614 }
615
616 /* call when you do images etc, needs restore too. also verifies tables */
617 /* it uses a flag to prevent premul or free to happen twice */
618 void curvemapping_premultiply(CurveMapping *cumap, int restore)
619 {
620         int a;
621         
622         if (restore) {
623                 if (cumap->flag & CUMA_PREMULLED) {
624                         for (a = 0; a < 3; a++) {
625                                 MEM_freeN(cumap->cm[a].table);
626                                 cumap->cm[a].table = cumap->cm[a].premultable;
627                                 cumap->cm[a].premultable = NULL;
628                         }
629                         
630                         cumap->flag &= ~CUMA_PREMULLED;
631                 }
632         }
633         else {
634                 if ((cumap->flag & CUMA_PREMULLED) == 0) {
635                         /* verify and copy */
636                         for (a = 0; a < 3; a++) {
637                                 if (cumap->cm[a].table == NULL)
638                                         curvemap_make_table(cumap->cm + a, &cumap->clipr);
639                                 cumap->cm[a].premultable = cumap->cm[a].table;
640                                 cumap->cm[a].table = MEM_mallocN((CM_TABLE + 1) * sizeof(CurveMapPoint), "premul table");
641                                 memcpy(cumap->cm[a].table, cumap->cm[a].premultable, (CM_TABLE + 1) * sizeof(CurveMapPoint));
642                         }
643                         
644                         if (cumap->cm[3].table == NULL)
645                                 curvemap_make_table(cumap->cm + 3, &cumap->clipr);
646                 
647                         /* premul */
648                         for (a = 0; a < 3; a++) {
649                                 int b;
650                                 for (b = 0; b <= CM_TABLE; b++) {
651                                         cumap->cm[a].table[b].y = curvemap_evaluateF(cumap->cm + 3, cumap->cm[a].table[b].y);
652                                 }
653                         }
654                         
655                         cumap->flag |= CUMA_PREMULLED;
656                 }
657         }
658 }
659
660 static int sort_curvepoints(const void *a1, const void *a2)
661 {
662         const struct CurveMapPoint *x1 = a1, *x2 = a2;
663         
664         if (x1->x > x2->x) return 1;
665         else if (x1->x < x2->x) return -1;
666         return 0;
667 }
668
669 /* ************************ more CurveMapping calls *************** */
670
671 /* note; only does current curvemap! */
672 void curvemapping_changed(CurveMapping *cumap, int rem_doubles)
673 {
674         CurveMap *cuma = cumap->cm + cumap->cur;
675         CurveMapPoint *cmp = cuma->curve;
676         rctf *clipr = &cumap->clipr;
677         float thresh = 0.01f * (clipr->xmax - clipr->xmin);
678         float dx = 0.0f, dy = 0.0f;
679         int a;
680
681         cumap->changed_timestamp++;
682
683         /* clamp with clip */
684         if (cumap->flag & CUMA_DO_CLIP) {
685                 for (a = 0; a < cuma->totpoint; a++) {
686                         if (cmp[a].flag & CUMA_SELECT) {
687                                 if (cmp[a].x < clipr->xmin)
688                                         dx = minf(dx, cmp[a].x - clipr->xmin);
689                                 else if (cmp[a].x > clipr->xmax)
690                                         dx = maxf(dx, cmp[a].x - clipr->xmax);
691                                 if (cmp[a].y < clipr->ymin)
692                                         dy = minf(dy, cmp[a].y - clipr->ymin);
693                                 else if (cmp[a].y > clipr->ymax)
694                                         dy = maxf(dy, cmp[a].y - clipr->ymax);
695                         }
696                 }
697                 for (a = 0; a < cuma->totpoint; a++) {
698                         if (cmp[a].flag & CUMA_SELECT) {
699                                 cmp[a].x -= dx;
700                                 cmp[a].y -= dy;
701                         }
702                 }
703         }
704         
705         
706         qsort(cmp, cuma->totpoint, sizeof(CurveMapPoint), sort_curvepoints);
707         
708         /* remove doubles, threshold set on 1% of default range */
709         if (rem_doubles && cuma->totpoint > 2) {
710                 for (a = 0; a < cuma->totpoint - 1; a++) {
711                         dx = cmp[a].x - cmp[a + 1].x;
712                         dy = cmp[a].y - cmp[a + 1].y;
713                         if (sqrtf(dx * dx + dy * dy) < thresh) {
714                                 if (a == 0) {
715                                         cmp[a + 1].flag |= CUMA_VECTOR;
716                                         if (cmp[a + 1].flag & CUMA_SELECT)
717                                                 cmp[a].flag |= CUMA_SELECT;
718                                 }
719                                 else {
720                                         cmp[a].flag |= CUMA_VECTOR;
721                                         if (cmp[a].flag & CUMA_SELECT)
722                                                 cmp[a + 1].flag |= CUMA_SELECT;
723                                 }
724                                 break;  /* we assume 1 deletion per edit is ok */
725                         }
726                 }
727                 if (a != cuma->totpoint - 1)
728                         curvemap_remove(cuma, 2);
729         }       
730         curvemap_make_table(cuma, clipr);
731 }
732
733 void curvemapping_changed_all(CurveMapping *cumap)
734 {
735         int a, cur = cumap->cur;
736
737         for (a = 0; a < CM_TOT; a++) {
738                 if (cumap->cm[a].curve) {
739                         cumap->cur = a;
740                         curvemapping_changed(cumap, FALSE);
741                 }
742         }
743
744         cumap->cur = cur;
745 }
746
747 /* table should be verified */
748 float curvemap_evaluateF(CurveMap *cuma, float value)
749 {
750         float fi;
751         int i;
752
753         /* index in table */
754         fi = (value - cuma->mintable) * cuma->range;
755         i = (int)fi;
756         
757         /* fi is table float index and should check against table range i.e. [0.0 CM_TABLE] */
758         if (fi < 0.0f || fi > CM_TABLE)
759                 return curvemap_calc_extend(cuma, value, &cuma->table[0].x, &cuma->table[CM_TABLE].x);
760         else {
761                 if (i < 0) return cuma->table[0].y;
762                 if (i >= CM_TABLE) return cuma->table[CM_TABLE].y;
763                 
764                 fi = fi - (float)i;
765                 return (1.0f - fi) * cuma->table[i].y + (fi) * cuma->table[i + 1].y;
766         }
767 }
768
769 /* works with curve 'cur' */
770 float curvemapping_evaluateF(CurveMapping *cumap, int cur, float value)
771 {
772         CurveMap *cuma = cumap->cm + cur;
773         
774         /* allocate or bail out */
775         if (cuma->table == NULL) {
776                 curvemap_make_table(cuma, &cumap->clipr);
777                 if (cuma->table == NULL)
778                         return 1.0f - value;
779         }
780         return curvemap_evaluateF(cuma, value);
781 }
782
783 /* vector case */
784 void curvemapping_evaluate3F(CurveMapping *cumap, float vecout[3], const float vecin[3])
785 {
786         vecout[0] = curvemapping_evaluateF(cumap, 0, vecin[0]);
787         vecout[1] = curvemapping_evaluateF(cumap, 1, vecin[1]);
788         vecout[2] = curvemapping_evaluateF(cumap, 2, vecin[2]);
789 }
790
791 /* RGB case, no black/white points, no premult */
792 void curvemapping_evaluateRGBF(CurveMapping *cumap, float vecout[3], const float vecin[3])
793 {
794         vecout[0] = curvemapping_evaluateF(cumap, 0, curvemapping_evaluateF(cumap, 3, vecin[0]));
795         vecout[1] = curvemapping_evaluateF(cumap, 1, curvemapping_evaluateF(cumap, 3, vecin[1]));
796         vecout[2] = curvemapping_evaluateF(cumap, 2, curvemapping_evaluateF(cumap, 3, vecin[2]));
797 }
798
799 /** same as #curvemapping_evaluate_premulRGBF
800  * but black/bwmul are passed as args for the compositor
801  * where they can change per pixel.
802  *
803  * Use in conjunction with #curvemapping_set_black_white_ex
804  *
805  * \param black Use instead of cumap->black
806  * \param bwmul Use instead of cumap->bwmul
807  */
808 void curvemapping_evaluate_premulRGBF_ex(CurveMapping *cumap, float vecout[3], const float vecin[3],
809                                          const float black[3], const float bwmul[3])
810 {
811         vecout[0] = curvemap_evaluateF(&cumap->cm[0], (vecin[0] - black[0]) * bwmul[0]);
812         vecout[1] = curvemap_evaluateF(&cumap->cm[1], (vecin[1] - black[1]) * bwmul[1]);
813         vecout[2] = curvemap_evaluateF(&cumap->cm[2], (vecin[2] - black[2]) * bwmul[2]);
814 }
815
816 /* RGB with black/white points and premult. tables are checked */
817 void curvemapping_evaluate_premulRGBF(CurveMapping *cumap, float vecout[3], const float vecin[3])
818 {
819         vecout[0] = curvemap_evaluateF(&cumap->cm[0], (vecin[0] - cumap->black[0]) * cumap->bwmul[0]);
820         vecout[1] = curvemap_evaluateF(&cumap->cm[1], (vecin[1] - cumap->black[1]) * cumap->bwmul[1]);
821         vecout[2] = curvemap_evaluateF(&cumap->cm[2], (vecin[2] - cumap->black[2]) * cumap->bwmul[2]);
822 }
823
824 /* same as above, byte version */
825 void curvemapping_evaluate_premulRGB(CurveMapping *cumap, unsigned char vecout_byte[3], const unsigned char vecin_byte[3])
826 {
827         float vecin[3], vecout[3];
828
829         vecin[0] = (float) vecin_byte[0] / 255.0f;
830         vecin[1] = (float) vecin_byte[1] / 255.0f;
831         vecin[2] = (float) vecin_byte[2] / 255.0f;
832
833         curvemapping_evaluate_premulRGBF(cumap, vecout, vecin);
834
835         vecout_byte[0] = FTOCHAR(vecout[0]);
836         vecout_byte[1] = FTOCHAR(vecout[1]);
837         vecout_byte[2] = FTOCHAR(vecout[2]);
838 }
839
840
841 /* only used for image editor curves */
842 void curvemapping_do_ibuf(CurveMapping *cumap, ImBuf *ibuf)
843 {
844         ImBuf *tmpbuf;
845         int pixel;
846         float *pix_in;
847         float col[3];
848         int stride = 4;
849         float *pix_out;
850         
851         if (ibuf == NULL)
852                 return;
853         if (ibuf->rect_float == NULL)
854                 IMB_float_from_rect(ibuf);
855         else if (ibuf->rect == NULL)
856                 imb_addrectImBuf(ibuf);
857         
858         if (!ibuf->rect || !ibuf->rect_float)
859                 return;
860         
861         /* work on a temp buffer, so can color manage afterwards.
862          * No worse off memory wise than comp nodes */
863         tmpbuf = IMB_dupImBuf(ibuf);
864         
865         curvemapping_premultiply(cumap, 0);
866         
867         pix_in = ibuf->rect_float;
868         pix_out = tmpbuf->rect_float;
869
870         if (ibuf->channels)
871                 stride = ibuf->channels;
872         
873         for (pixel = ibuf->x * ibuf->y; pixel > 0; pixel--, pix_in += stride, pix_out += stride) {
874                 if (stride < 3) {
875                         col[0] = curvemap_evaluateF(cumap->cm, *pix_in);
876                         
877                         pix_out[1] = pix_out[2] = pix_out[3] = pix_out[0] = col[0];
878                 }
879                 else {
880                         curvemapping_evaluate_premulRGBF(cumap, col, pix_in);
881                         pix_out[0] = col[0];
882                         pix_out[1] = col[1];
883                         pix_out[2] = col[2];
884                         if (stride > 3)
885                                 pix_out[3] = pix_in[3];
886                         else
887                                 pix_out[3] = 1.f;
888                 }
889         }
890         
891         IMB_rect_from_float(tmpbuf);
892         SWAP(unsigned int *, tmpbuf->rect, ibuf->rect);
893         IMB_freeImBuf(tmpbuf);
894         
895         curvemapping_premultiply(cumap, 1);
896 }
897
898 int curvemapping_RGBA_does_something(CurveMapping *cumap)
899 {
900         int a;
901         
902         if (cumap->black[0] != 0.0f) return 1;
903         if (cumap->black[1] != 0.0f) return 1;
904         if (cumap->black[2] != 0.0f) return 1;
905         if (cumap->white[0] != 1.0f) return 1;
906         if (cumap->white[1] != 1.0f) return 1;
907         if (cumap->white[2] != 1.0f) return 1;
908         
909         for (a = 0; a < CM_TOT; a++) {
910                 if (cumap->cm[a].curve) {
911                         if (cumap->cm[a].totpoint != 2) return 1;
912                         
913                         if (cumap->cm[a].curve[0].x != 0.0f) return 1;
914                         if (cumap->cm[a].curve[0].y != 0.0f) return 1;
915                         if (cumap->cm[a].curve[1].x != 1.0f) return 1;
916                         if (cumap->cm[a].curve[1].y != 1.0f) return 1;
917                 }
918         }
919         return 0;
920 }
921
922 void curvemapping_initialize(CurveMapping *cumap)
923 {
924         int a;
925         
926         if (cumap == NULL) return;
927         
928         for (a = 0; a < CM_TOT; a++) {
929                 if (cumap->cm[a].table == NULL)
930                         curvemap_make_table(cumap->cm + a, &cumap->clipr);
931         }
932 }
933
934 void curvemapping_table_RGBA(CurveMapping *cumap, float **array, int *size)
935 {
936         int a;
937         
938         *size = CM_TABLE + 1;
939         *array = MEM_callocN(sizeof(float) * (*size) * 4, "CurveMapping");
940         curvemapping_initialize(cumap);
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 DO_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 short use_color_management)
1005 {
1006         int i, x, y;
1007         float *fp;
1008         float rgb[3];
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         hist->channels = 3;
1017         hist->x_resolution = 256;
1018         hist->xmax = 1.0f;
1019         /* hist->ymax = 1.0f; */ /* now do this on the operator _only_ */
1020
1021         if (ibuf->rect == NULL && ibuf->rect_float == NULL) return;
1022
1023         /* persistent draw */
1024         hist->flag |= HISTO_FLAG_SAMPLELINE; /* keep drawing the flag after */
1025
1026         for (i = 0; i < 256; i++) {
1027                 x = (int)(0.5f + x1 + (float)i * (x2 - x1) / 255.0f);
1028                 y = (int)(0.5f + y1 + (float)i * (y2 - y1) / 255.0f);
1029
1030                 if (x < 0 || y < 0 || x >= ibuf->x || y >= ibuf->y) {
1031                         hist->data_luma[i] = hist->data_r[i] = hist->data_g[i] = hist->data_b[i] = hist->data_a[i] = 0.0f;
1032                 }
1033                 else {
1034                         if (ibuf->rect_float) {
1035                                 fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
1036
1037                                 if (use_color_management)
1038                                         linearrgb_to_srgb_v3_v3(rgb, fp);
1039                                 else
1040                                         copy_v3_v3(rgb, fp);
1041
1042                                 hist->data_luma[i]  = rgb_to_luma(rgb);
1043                                 hist->data_r[i]     = rgb[0];
1044                                 hist->data_g[i]     = rgb[1];
1045                                 hist->data_b[i]     = rgb[2];
1046                                 hist->data_a[i]     = fp[3];
1047                         }
1048                         else if (ibuf->rect) {
1049                                 cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
1050                                 hist->data_luma[i]  = (float)rgb_to_luma_byte(cp) / 255.0f;
1051                                 hist->data_r[i]     = (float)cp[0] / 255.0f;
1052                                 hist->data_g[i]     = (float)cp[1] / 255.0f;
1053                                 hist->data_b[i]     = (float)cp[2] / 255.0f;
1054                                 hist->data_a[i]     = (float)cp[3] / 255.0f;
1055                         }
1056                 }
1057         }
1058 }
1059
1060 void scopes_update(Scopes *scopes, ImBuf *ibuf, int use_color_management)
1061 {
1062         int x, y, c;
1063         unsigned int n, nl;
1064         double div, divl;
1065         float *rf = NULL;
1066         unsigned char *rc = NULL;
1067         unsigned int *bin_lum, *bin_r, *bin_g, *bin_b, *bin_a;
1068         int savedlines, saveline;
1069         float rgba[4], ycc[3], luma;
1070         int ycc_mode = -1;
1071         const short is_float = (ibuf->rect_float != NULL);
1072
1073         if (ibuf->rect == NULL && ibuf->rect_float == NULL) return;
1074
1075         if (scopes->ok == 1) return;
1076
1077         if (scopes->hist.ymax == 0.f) scopes->hist.ymax = 1.f;
1078
1079         /* hmmmm */
1080         if (!(ELEM(ibuf->channels, 3, 4))) return;
1081
1082         scopes->hist.channels = 3;
1083         scopes->hist.x_resolution = 256;
1084
1085         switch (scopes->wavefrm_mode) {
1086                 case SCOPES_WAVEFRM_RGB:
1087                         ycc_mode = -1;
1088                         break;
1089                 case SCOPES_WAVEFRM_LUMA:
1090                 case SCOPES_WAVEFRM_YCC_JPEG:
1091                         ycc_mode = BLI_YCC_JFIF_0_255;
1092                         break;
1093                 case SCOPES_WAVEFRM_YCC_601:
1094                         ycc_mode = BLI_YCC_ITU_BT601;
1095                         break;
1096                 case SCOPES_WAVEFRM_YCC_709:
1097                         ycc_mode = BLI_YCC_ITU_BT709;
1098                         break;
1099         }
1100
1101         /* temp table to count pix value for histogram */
1102         bin_r     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1103         bin_g     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1104         bin_b     = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1105         bin_a = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1106         bin_lum   = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1107
1108         /* convert to number of lines with logarithmic scale */
1109         scopes->sample_lines = (scopes->accuracy * 0.01f) * (scopes->accuracy * 0.01f) * ibuf->y;
1110         
1111         if (scopes->sample_full)
1112                 scopes->sample_lines = ibuf->y;
1113
1114         /* scan the image */
1115         savedlines = 0;
1116         for (c = 0; c < 3; c++) {
1117                 scopes->minmax[c][0] = 25500.0f;
1118                 scopes->minmax[c][1] = -25500.0f;
1119         }
1120         
1121         scopes->waveform_tot = ibuf->x * scopes->sample_lines;
1122         
1123         if (scopes->waveform_1)
1124                 MEM_freeN(scopes->waveform_1);
1125         if (scopes->waveform_2)
1126                 MEM_freeN(scopes->waveform_2);
1127         if (scopes->waveform_3)
1128                 MEM_freeN(scopes->waveform_3);
1129         if (scopes->vecscope)
1130                 MEM_freeN(scopes->vecscope);
1131         
1132         scopes->waveform_1 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 1");
1133         scopes->waveform_2 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 2");
1134         scopes->waveform_3 = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 3");
1135         scopes->vecscope = MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "vectorscope point channel");
1136         
1137         if (is_float)
1138                 rf = ibuf->rect_float;
1139         else
1140                 rc = (unsigned char *)ibuf->rect;
1141
1142         for (y = 0; y < ibuf->y; y++) {
1143                 if (savedlines < scopes->sample_lines && y >= ((savedlines) * ibuf->y) / (scopes->sample_lines + 1)) {
1144                         saveline = 1;
1145                 }
1146                 else {
1147                         saveline = 0;
1148                 }
1149                 for (x = 0; x < ibuf->x; x++) {
1150
1151                         if (is_float) {
1152                                 if (use_color_management)
1153                                         linearrgb_to_srgb_v3_v3(rgba, rf);
1154                                 else
1155                                         copy_v3_v3(rgba, rf);
1156                                 rgba[3] = rf[3];
1157                         }
1158                         else {
1159                                 for (c = 0; c < 4; c++)
1160                                         rgba[c] = rc[c] * INV_255;
1161                         }
1162
1163                         /* we still need luma for histogram */
1164                         luma = rgb_to_luma(rgba);
1165
1166                         /* check for min max */
1167                         if (ycc_mode == -1) {
1168                                 for (c = 0; c < 3; c++) {
1169                                         if (rgba[c] < scopes->minmax[c][0]) scopes->minmax[c][0] = rgba[c];
1170                                         if (rgba[c] > scopes->minmax[c][1]) scopes->minmax[c][1] = rgba[c];
1171                                 }
1172                         }
1173                         else {
1174                                 rgb_to_ycc(rgba[0], rgba[1], rgba[2], &ycc[0], &ycc[1], &ycc[2], ycc_mode);
1175                                 for (c = 0; c < 3; c++) {
1176                                         ycc[c] *= INV_255;
1177                                         if (ycc[c] < scopes->minmax[c][0]) scopes->minmax[c][0] = ycc[c];
1178                                         if (ycc[c] > scopes->minmax[c][1]) scopes->minmax[c][1] = ycc[c];
1179                                 }
1180                         }
1181                         /* increment count for histo*/
1182                         bin_lum[get_bin_float(luma)] += 1;
1183                         bin_r[get_bin_float(rgba[0])] += 1;
1184                         bin_g[get_bin_float(rgba[1])] += 1;
1185                         bin_b[get_bin_float(rgba[2])] += 1;
1186                         bin_a[get_bin_float(rgba[3])] += 1;
1187
1188                         /* save sample if needed */
1189                         if (saveline) {
1190                                 const float fx = (float)x / (float)ibuf->x;
1191                                 const int idx = 2 * (ibuf->x * savedlines + x);
1192                                 save_sample_line(scopes, idx, fx, rgba, ycc);
1193                         }
1194
1195                         rf += ibuf->channels;
1196                         rc += ibuf->channels;
1197                 }
1198                 if (saveline)
1199                         savedlines += 1;
1200         }
1201
1202         /* convert hist data to float (proportional to max count) */
1203         n = 0;
1204         nl = 0;
1205         for (x = 0; x < 256; x++) {
1206                 if (bin_lum[x] > nl) nl = bin_lum[x];
1207                 if (bin_r[x]   > n) n = bin_r[x];
1208                 if (bin_g[x]   > n) n = bin_g[x];
1209                 if (bin_b[x]   > n) n = bin_b[x];
1210                 if (bin_a[x]   > n) n = bin_a[x];
1211         }
1212         div = 1.0 / (double)n;
1213         divl = 1.0 / (double)nl;
1214         for (x = 0; x < 256; x++) {
1215                 scopes->hist.data_luma[x] = bin_lum[x] * divl;
1216                 scopes->hist.data_r[x] = bin_r[x] * div;
1217                 scopes->hist.data_g[x] = bin_g[x] * div;
1218                 scopes->hist.data_b[x] = bin_b[x] * div;
1219                 scopes->hist.data_a[x] = bin_a[x] * div;
1220         }
1221         MEM_freeN(bin_lum);
1222         MEM_freeN(bin_r);
1223         MEM_freeN(bin_g);
1224         MEM_freeN(bin_b);
1225         MEM_freeN(bin_a);
1226
1227         scopes->ok = 1;
1228 }
1229
1230 void scopes_free(Scopes *scopes)
1231 {
1232         if (scopes->waveform_1) {
1233                 MEM_freeN(scopes->waveform_1);
1234                 scopes->waveform_1 = NULL;
1235         }
1236         if (scopes->waveform_2) {
1237                 MEM_freeN(scopes->waveform_2);
1238                 scopes->waveform_2 = NULL;
1239         }
1240         if (scopes->waveform_3) {
1241                 MEM_freeN(scopes->waveform_3);
1242                 scopes->waveform_3 = NULL;
1243         }
1244         if (scopes->vecscope) {
1245                 MEM_freeN(scopes->vecscope);
1246                 scopes->vecscope = NULL;
1247         }
1248 }
1249
1250 void scopes_new(Scopes *scopes)
1251 {
1252         scopes->accuracy = 30.0;
1253         scopes->hist.mode = HISTO_MODE_RGB;
1254         scopes->wavefrm_alpha = 0.3;
1255         scopes->vecscope_alpha = 0.3;
1256         scopes->wavefrm_height = 100;
1257         scopes->vecscope_height = 100;
1258         scopes->hist.height = 100;
1259         scopes->ok = 0;
1260         scopes->waveform_1 = NULL;
1261         scopes->waveform_2 = NULL;
1262         scopes->waveform_3 = NULL;
1263         scopes->vecscope = NULL;
1264 }
1265
1266 void BKE_color_managed_display_settings_init(ColorManagedDisplaySettings *settings)
1267 {
1268         const char *display_name = IMB_colormanagement_display_get_default_name();
1269
1270         BLI_strncpy(settings->display_device, display_name, sizeof(settings->display_device));
1271 }
1272
1273 void BKE_color_managed_display_settings_copy(ColorManagedDisplaySettings *new_settings,
1274                                              const ColorManagedDisplaySettings *settings)
1275 {
1276         BLI_strncpy(new_settings->display_device, settings->display_device, sizeof(new_settings->display_device));
1277 }
1278
1279 void BKE_color_managed_view_settings_init(ColorManagedViewSettings *settings)
1280 {
1281         /* OCIO_TODO: use default view transform here when OCIO is completely integrated
1282         *             and proper versioning stuff is added.
1283         *             for now use NONE to be compatible with all current files
1284         */
1285         BLI_strncpy(settings->view_transform, "NONE", sizeof(settings->view_transform));
1286
1287         settings->gamma = 1.0f;
1288         settings->exposure = 0.0f;
1289
1290         settings->flag |= COLORMANAGE_VIEW_USE_GLOBAL;
1291 }
1292
1293 void BKE_color_managed_view_settings_copy(ColorManagedViewSettings *new_settings,
1294                                           const ColorManagedViewSettings *settings)
1295 {
1296         BLI_strncpy(new_settings->view_transform, settings->view_transform, sizeof(new_settings->view_transform));
1297
1298         new_settings->exposure = settings->exposure;
1299         new_settings->gamma = settings->gamma;
1300 }
1301
1302 void BKE_color_managed_colorspace_settings_init(ColorManagedColorspaceSettings *colorspace_settings)
1303 {
1304         BLI_strncpy(colorspace_settings->name, "NONE", sizeof(colorspace_settings->name));
1305 }
1306
1307 void BKE_color_managed_colorspace_settings_copy(ColorManagedColorspaceSettings *colorspace_settings,
1308                                                 const ColorManagedColorspaceSettings *settings)
1309 {
1310         BLI_strncpy(colorspace_settings->name, settings->name, sizeof(colorspace_settings->name));
1311 }