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