2d4e0ac8c0832a1b1dcabdf285e6230854014fbe
[blender.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 #include <string.h>
31 #include <math.h>
32 #include <stdlib.h>
33 #include <float.h>
34
35 #ifdef WITH_LCMS
36 #include <lcms.h>
37 #endif
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_color_types.h"
42 #include "DNA_curve_types.h"
43
44 #include "BKE_colortools.h"
45 #include "BKE_curve.h"
46 #include "BKE_ipo.h"
47 #include "BKE_utildefines.h"
48
49 #include "BLI_blenlib.h"
50 #include "BLI_math.h"
51
52 #include "IMB_imbuf.h"
53 #include "IMB_imbuf_types.h"
54
55
56 void floatbuf_to_srgb_byte(float *rectf, unsigned char *rectc, int x1, int x2, int y1, int y2, int UNUSED(w))
57 {
58         int x, y;
59         float *rf= rectf;
60         float srgb[3];
61         unsigned char *rc= rectc;
62         
63         for(y=y1; y<y2; y++) {
64                 for(x=x1; x<x2; x++, rf+=4, rc+=4) {
65                         srgb[0]= linearrgb_to_srgb(rf[0]);
66                         srgb[1]= linearrgb_to_srgb(rf[1]);
67                         srgb[2]= linearrgb_to_srgb(rf[2]);
68
69                         rc[0]= FTOCHAR(srgb[0]);
70                         rc[1]= FTOCHAR(srgb[1]);
71                         rc[2]= FTOCHAR(srgb[2]);
72                         rc[3]= FTOCHAR(rf[3]);
73                 }
74         }
75 }
76
77 void floatbuf_to_byte(float *rectf, unsigned char *rectc, int x1, int x2, int y1, int y2, int UNUSED(w))
78 {
79         int x, y;
80         float *rf= rectf;
81         unsigned char *rc= rectc;
82         
83         for(y=y1; y<y2; y++) {
84                 for(x=x1; x<x2; x++, rf+=4, rc+=4) {
85                         rc[0]= FTOCHAR(rf[0]);
86                         rc[1]= FTOCHAR(rf[1]);
87                         rc[2]= FTOCHAR(rf[2]);
88                         rc[3]= FTOCHAR(rf[3]);
89                 }
90         }
91 }
92
93
94 /* ********************************* color curve ********************* */
95
96 /* ***************** operations on full struct ************* */
97
98 CurveMapping *curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
99 {
100         CurveMapping *cumap;
101         int a;
102         float clipminx, clipminy, clipmaxx, clipmaxy;
103         
104         cumap= MEM_callocN(sizeof(CurveMapping), "new curvemap");
105         cumap->flag= CUMA_DO_CLIP;
106         if(tot==4) cumap->cur= 3;               /* rhms, hack for 'col' curve? */
107         
108         clipminx = MIN2(minx, maxx);
109         clipminy = MIN2(miny, maxy);
110         clipmaxx = MAX2(minx, maxx);
111         clipmaxy = MAX2(miny, maxy);
112         
113         BLI_init_rctf(&cumap->curr, clipminx, clipmaxx, clipminy, clipmaxy);
114         cumap->clipr= cumap->curr;
115         
116         cumap->white[0]= cumap->white[1]= cumap->white[2]= 1.0f;
117         cumap->bwmul[0]= cumap->bwmul[1]= cumap->bwmul[2]= 1.0f;
118         
119         for(a=0; a<tot; a++) {
120                 cumap->cm[a].flag= CUMA_EXTEND_EXTRAPOLATE;
121                 cumap->cm[a].totpoint= 2;
122                 cumap->cm[a].curve= MEM_callocN(2*sizeof(CurveMapPoint), "curve points");
123                 
124                 cumap->cm[a].curve[0].x= minx;
125                 cumap->cm[a].curve[0].y= miny;
126                 cumap->cm[a].curve[1].x= maxx;
127                 cumap->cm[a].curve[1].y= maxy;
128         }       
129
130         cumap->changed_timestamp = 0;
131
132         return cumap;
133 }
134
135 void curvemapping_free(CurveMapping *cumap)
136 {
137         int a;
138         
139         if(cumap) {
140                 for(a=0; a<CM_TOT; a++) {
141                         if(cumap->cm[a].curve) MEM_freeN(cumap->cm[a].curve);
142                         if(cumap->cm[a].table) MEM_freeN(cumap->cm[a].table);
143                         if(cumap->cm[a].premultable) MEM_freeN(cumap->cm[a].premultable);
144                 }
145                 MEM_freeN(cumap);
146         }
147 }
148
149 CurveMapping *curvemapping_copy(CurveMapping *cumap)
150 {
151         int a;
152         
153         if(cumap) {
154                 CurveMapping *cumapn= MEM_dupallocN(cumap);
155                 for(a=0; a<CM_TOT; a++) {
156                         if(cumap->cm[a].curve) 
157                                 cumapn->cm[a].curve= MEM_dupallocN(cumap->cm[a].curve);
158                         if(cumap->cm[a].table) 
159                                 cumapn->cm[a].table= MEM_dupallocN(cumap->cm[a].table);
160                         if(cumap->cm[a].premultable) 
161                                 cumapn->cm[a].premultable= MEM_dupallocN(cumap->cm[a].premultable);
162                 }
163                 return cumapn;
164         }
165         return NULL;
166 }
167
168 void curvemapping_set_black_white(CurveMapping *cumap, float *black, float *white)
169 {
170         int a;
171         
172         if(white)
173                 VECCOPY(cumap->white, white);
174         if(black)
175                 VECCOPY(cumap->black, black);
176         
177         for(a=0; a<3; a++) {
178                 if(cumap->white[a]==cumap->black[a])
179                         cumap->bwmul[a]= 0.0f;
180                 else
181                         cumap->bwmul[a]= 1.0f/(cumap->white[a] - cumap->black[a]);
182         }       
183 }
184
185 /* ***************** operations on single curve ************* */
186 /* ********** NOTE: requires curvemapping_changed() call after ******** */
187
188 /* removes with flag set */
189 void curvemap_remove(CurveMap *cuma, int flag)
190 {
191         CurveMapPoint *cmp= MEM_mallocN((cuma->totpoint)*sizeof(CurveMapPoint), "curve points");
192         int a, b, removed=0;
193         
194         /* well, lets keep the two outer points! */
195         cmp[0]= cuma->curve[0];
196         for(a=1, b=1; a<cuma->totpoint-1; a++) {
197                 if(!(cuma->curve[a].flag & flag)) {
198                         cmp[b]= cuma->curve[a];
199                         b++;
200                 }
201                 else removed++;
202         }
203         cmp[b]= cuma->curve[a];
204         
205         MEM_freeN(cuma->curve);
206         cuma->curve= cmp;
207         cuma->totpoint -= removed;
208 }
209
210 void curvemap_insert(CurveMap *cuma, float x, float y)
211 {
212         CurveMapPoint *cmp= MEM_callocN((cuma->totpoint+1)*sizeof(CurveMapPoint), "curve points");
213         int a, b, foundloc= 0;
214                 
215         /* insert fragments of the old one and the new point to the new curve */
216         cuma->totpoint++;
217         for(a=0, b=0; a<cuma->totpoint; a++) {
218                 if((x < cuma->curve[a].x) && !foundloc) {
219                         cmp[a].x= x;
220                         cmp[a].y= y;
221                         cmp[a].flag= CUMA_SELECT;
222                         foundloc= 1;
223                 }
224                 else {
225                         cmp[a].x= cuma->curve[b].x;
226                         cmp[a].y= cuma->curve[b].y;
227                         cmp[a].flag= cuma->curve[b].flag;
228                         cmp[a].flag &= ~CUMA_SELECT; /* make sure old points don't remain selected */
229                         cmp[a].shorty= cuma->curve[b].shorty;
230                         b++;
231                 }
232         }
233
234         /* free old curve and replace it with new one */
235         MEM_freeN(cuma->curve);
236         cuma->curve= cmp;
237 }
238
239 void curvemap_reset(CurveMap *cuma, rctf *clipr, int preset, int slope)
240 {
241         if(cuma->curve)
242                 MEM_freeN(cuma->curve);
243
244         switch(preset) {
245                 case CURVE_PRESET_LINE: cuma->totpoint= 2; break;
246                 case CURVE_PRESET_SHARP: cuma->totpoint= 4; break;
247                 case CURVE_PRESET_SMOOTH: cuma->totpoint= 4; break;
248                 case CURVE_PRESET_MAX: cuma->totpoint= 2; break;
249                 case CURVE_PRESET_MID9: cuma->totpoint= 9; break;
250                 case CURVE_PRESET_ROUND: cuma->totpoint= 4; break;
251                 case CURVE_PRESET_ROOT: cuma->totpoint= 4; break;
252         }
253
254         cuma->curve= MEM_callocN(cuma->totpoint*sizeof(CurveMapPoint), "curve points");
255
256         switch(preset) {
257                 case CURVE_PRESET_LINE:
258                         cuma->curve[0].x= clipr->xmin;
259                         cuma->curve[0].y= clipr->ymax;
260                         cuma->curve[0].flag= 0;
261                         cuma->curve[1].x= clipr->xmax;
262                         cuma->curve[1].y= clipr->ymin;
263                         cuma->curve[1].flag= 0;
264                         break;
265                 case CURVE_PRESET_SHARP:
266                         cuma->curve[0].x= 0;
267                         cuma->curve[0].y= 1;
268                         cuma->curve[1].x= 0.25;
269                         cuma->curve[1].y= 0.50;
270                         cuma->curve[2].x= 0.75;
271                         cuma->curve[2].y= 0.04;
272                         cuma->curve[3].x= 1;
273                         cuma->curve[3].y= 0;
274                         break;
275                 case CURVE_PRESET_SMOOTH:
276                         cuma->curve[0].x= 0;
277                         cuma->curve[0].y= 1;
278                         cuma->curve[1].x= 0.25;
279                         cuma->curve[1].y= 0.94;
280                         cuma->curve[2].x= 0.75;
281                         cuma->curve[2].y= 0.06;
282                         cuma->curve[3].x= 1;
283                         cuma->curve[3].y= 0;
284                         break;
285                 case CURVE_PRESET_MAX:
286                         cuma->curve[0].x= 0;
287                         cuma->curve[0].y= 1;
288                         cuma->curve[1].x= 1;
289                         cuma->curve[1].y= 1;
290                         break;
291                 case CURVE_PRESET_MID9:
292                         {
293                                 int i;
294                                 for (i=0; i < cuma->totpoint; i++)
295                                 {
296                                         cuma->curve[i].x= i / ((float)cuma->totpoint-1);
297                                         cuma->curve[i].y= 0.5;
298                                 }
299                         }
300                         break;
301                 case CURVE_PRESET_ROUND:
302                         cuma->curve[0].x= 0;
303                         cuma->curve[0].y= 1;
304                         cuma->curve[1].x= 0.5;
305                         cuma->curve[1].y= 0.90;
306                         cuma->curve[2].x= 0.86;
307                         cuma->curve[2].y= 0.5;
308                         cuma->curve[3].x= 1;
309                         cuma->curve[3].y= 0;
310                         break;
311                 case CURVE_PRESET_ROOT:
312                         cuma->curve[0].x= 0;
313                         cuma->curve[0].y= 1;
314                         cuma->curve[1].x= 0.25;
315                         cuma->curve[1].y= 0.95;
316                         cuma->curve[2].x= 0.75;
317                         cuma->curve[2].y= 0.44;
318                         cuma->curve[3].x= 1;
319                         cuma->curve[3].y= 0;
320                         break;
321         }
322
323         /* mirror curve in x direction to have positive slope
324          * rather than default negative slope */
325         if (slope == CURVEMAP_SLOPE_POSITIVE) {
326                 int i, last=cuma->totpoint-1;
327                 CurveMapPoint *newpoints= MEM_dupallocN(cuma->curve);
328                 
329                 for (i=0; i<cuma->totpoint; i++) {
330                         newpoints[i].y = cuma->curve[last-i].y;
331                 }
332                 
333                 MEM_freeN(cuma->curve);
334                 cuma->curve = newpoints;
335         }
336         
337         if(cuma->table) {
338                 MEM_freeN(cuma->table);
339                 cuma->table= NULL;
340         }
341 }
342
343 /* if type==1: vector, else auto */
344 void curvemap_sethandle(CurveMap *cuma, int type)
345 {
346         int a;
347         
348         for(a=0; a<cuma->totpoint; a++) {
349                 if(cuma->curve[a].flag & CUMA_SELECT) {
350                         if(type) cuma->curve[a].flag |= CUMA_VECTOR;
351                         else cuma->curve[a].flag &= ~CUMA_VECTOR;
352                 }
353         }
354 }
355
356 /* *********************** Making the tables and display ************** */
357
358 /* reduced copy of garbled calchandleNurb() code in curve.c */
359 static void calchandle_curvemap(BezTriple *bezt, BezTriple *prev, BezTriple *next, int UNUSED(mode))
360 {
361         float *p1,*p2,*p3,pt[3];
362         float dx1,dy1, dx,dy, vx,vy, len,len1,len2;
363         
364         if(bezt->h1==0 && bezt->h2==0) return;
365         
366         p2= bezt->vec[1];
367         
368         if(prev==NULL) {
369                 p3= next->vec[1];
370                 pt[0]= 2*p2[0]- p3[0];
371                 pt[1]= 2*p2[1]- p3[1];
372                 p1= pt;
373         }
374         else p1= prev->vec[1];
375         
376         if(next==NULL) {
377                 p1= prev->vec[1];
378                 pt[0]= 2*p2[0]- p1[0];
379                 pt[1]= 2*p2[1]- p1[1];
380                 p3= pt;
381         }
382         else p3= next->vec[1];
383         
384         dx= p2[0]- p1[0];
385         dy= p2[1]- p1[1];
386
387         len1= (float)sqrt(dx*dx+dy*dy);
388         
389         dx1= p3[0]- p2[0];
390         dy1= p3[1]- p2[1];
391
392         len2= (float)sqrt(dx1*dx1+dy1*dy1);
393         
394         if(len1==0.0f) len1=1.0f;
395         if(len2==0.0f) len2=1.0f;
396         
397         if(bezt->h1==HD_AUTO || bezt->h2==HD_AUTO) {    /* auto */
398                 vx= dx1/len2 + dx/len1;
399                 vy= dy1/len2 + dy/len1;
400                 
401                 len= 2.5614f*(float)sqrt(vx*vx + vy*vy);
402                 if(len!=0.0f) {
403                         
404                         if(bezt->h1==HD_AUTO) {
405                                 len1/=len;
406                                 *(p2-3)= *p2-vx*len1;
407                                 *(p2-2)= *(p2+1)-vy*len1;
408                         }
409                         if(bezt->h2==HD_AUTO) {
410                                 len2/=len;
411                                 *(p2+3)= *p2+vx*len2;
412                                 *(p2+4)= *(p2+1)+vy*len2;
413                         }
414                 }
415         }
416
417         if(bezt->h1==HD_VECT) { /* vector */
418                 dx/=3.0; 
419                 dy/=3.0; 
420                 *(p2-3)= *p2-dx;
421                 *(p2-2)= *(p2+1)-dy;
422         }
423         if(bezt->h2==HD_VECT) {
424                 dx1/=3.0; 
425                 dy1/=3.0; 
426                 *(p2+3)= *p2+dx1;
427                 *(p2+4)= *(p2+1)+dy1;
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, float *first, float *last)
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( sqrt(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, const float *vecin)
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, const float *vecin)
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, const float *vecin)
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 #ifdef WITH_LCMS
787 /* basic error handler, if we dont do this blender will exit */
788 static int ErrorReportingFunction(int ErrorCode, const char *ErrorText)
789 {
790     fprintf(stderr, "%s:%d\n", ErrorText, ErrorCode);
791         return 1;
792 }
793 #endif
794
795 void colorcorrection_do_ibuf(ImBuf *ibuf, const char *profile)
796 {
797 #ifdef WITH_LCMS
798         if (ibuf->crect == NULL)
799         {
800                 cmsHPROFILE proofingProfile;
801                 
802                 /* TODO, move to initialization area of code */
803                 //cmsSetLogErrorHandler(ErrorReportingFunction);
804                 cmsSetErrorHandler(ErrorReportingFunction);
805                 
806                 /* will return NULL if the file isn't fount */
807                 proofingProfile = cmsOpenProfileFromFile(profile, "r");
808
809                 cmsErrorAction(LCMS_ERROR_SHOW);
810
811                 if(proofingProfile) {
812                         cmsHPROFILE imageProfile;
813                         cmsHTRANSFORM hTransform;
814
815                         ibuf->crect = MEM_mallocN(ibuf->x*ibuf->y*sizeof(int), "imbuf crect");
816
817                         imageProfile  = cmsCreate_sRGBProfile();
818
819
820                         hTransform = cmsCreateProofingTransform(imageProfile, TYPE_RGBA_8, imageProfile, TYPE_RGBA_8, 
821                                                                                                   proofingProfile,
822                                                                                                   INTENT_ABSOLUTE_COLORIMETRIC,
823                                                                                                   INTENT_ABSOLUTE_COLORIMETRIC,
824                                                                                                   cmsFLAGS_SOFTPROOFING);
825                 
826                         cmsDoTransform(hTransform, ibuf->rect, ibuf->crect, ibuf->x * ibuf->y);
827
828                         cmsDeleteTransform(hTransform);
829                         cmsCloseProfile(imageProfile);
830                         cmsCloseProfile(proofingProfile);
831                 }
832         }
833 #else
834         /* unused */
835         (void)ibuf;
836         (void)profile;
837 #endif
838 }
839
840 /* only used for image editor curves */
841 void curvemapping_do_ibuf(CurveMapping *cumap, ImBuf *ibuf)
842 {
843         ImBuf *tmpbuf;
844         int pixel;
845         float *pix_in;
846         float col[3];
847         int stride= 4;
848         float *pix_out;
849         
850         if(ibuf==NULL)
851                 return;
852         if(ibuf->rect_float==NULL)
853                 IMB_float_from_rect(ibuf);
854         else if(ibuf->rect==NULL)
855                 imb_addrectImBuf(ibuf);
856         
857         if (!ibuf->rect || !ibuf->rect_float)
858                 return;
859         
860         /* work on a temp buffer, so can color manage afterwards.
861          * No worse off memory wise than comp nodes */
862         tmpbuf = IMB_dupImBuf(ibuf);
863         
864         curvemapping_premultiply(cumap, 0);
865         
866         pix_in= ibuf->rect_float;
867         pix_out= tmpbuf->rect_float;
868
869         if(ibuf->channels)
870                 stride= ibuf->channels;
871         
872         for(pixel= ibuf->x*ibuf->y; pixel>0; pixel--, pix_in+=stride, pix_out+=stride) {
873                 if(stride<3) {
874                         col[0]= curvemap_evaluateF(cumap->cm, *pix_in);
875                         
876                         pix_out[1]= pix_out[2]= pix_out[3]= pix_out[0]= col[0];
877                 }
878                 else {
879                         curvemapping_evaluate_premulRGBF(cumap, col, pix_in);
880                         pix_out[0]= col[0];
881                         pix_out[1]= col[1];
882                         pix_out[2]= col[2];
883                         if(stride>3)
884                                 pix_out[3]= pix_in[3];
885                         else
886                                 pix_out[3]= 1.f;
887                 }
888         }
889         
890         IMB_rect_from_float(tmpbuf);
891         SWAP(unsigned int *, tmpbuf->rect, ibuf->rect);
892         IMB_freeImBuf(tmpbuf);
893         
894         curvemapping_premultiply(cumap, 1);
895 }
896
897 int curvemapping_RGBA_does_something(CurveMapping *cumap)
898 {
899         int a;
900         
901         if(cumap->black[0]!=0.0f) return 1;
902         if(cumap->black[1]!=0.0f) return 1;
903         if(cumap->black[2]!=0.0f) return 1;
904         if(cumap->white[0]!=1.0f) return 1;
905         if(cumap->white[1]!=1.0f) return 1;
906         if(cumap->white[2]!=1.0f) return 1;
907         
908         for(a=0; a<CM_TOT; a++) {
909                 if(cumap->cm[a].curve) {
910                         if(cumap->cm[a].totpoint!=2)  return 1;
911                         
912                         if(cumap->cm[a].curve[0].x != 0.0f) return 1;
913                         if(cumap->cm[a].curve[0].y != 0.0f) return 1;
914                         if(cumap->cm[a].curve[1].x != 1.0f) return 1;
915                         if(cumap->cm[a].curve[1].y != 1.0f) return 1;
916                 }
917         }
918         return 0;
919 }
920
921 void curvemapping_initialize(CurveMapping *cumap)
922 {
923         int a;
924         
925         if(cumap==NULL) return;
926         
927         for(a=0; a<CM_TOT; a++) {
928                 if(cumap->cm[a].table==NULL)
929                         curvemap_make_table(cumap->cm+a, &cumap->clipr);
930         }
931 }
932
933 void curvemapping_table_RGBA(CurveMapping *cumap, float **array, int *size)
934 {
935         int a;
936         
937         *size = CM_TABLE+1;
938         *array = MEM_callocN(sizeof(float)*(*size)*4, "CurveMapping");
939         curvemapping_initialize(cumap);
940
941         for(a=0; a<*size; a++) {
942                 if(cumap->cm[0].table)
943                         (*array)[a*4+0]= cumap->cm[0].table[a].y;
944                 if(cumap->cm[1].table)
945                         (*array)[a*4+1]= cumap->cm[1].table[a].y;
946                 if(cumap->cm[2].table)
947                         (*array)[a*4+2]= cumap->cm[2].table[a].y;
948                 if(cumap->cm[3].table)
949                         (*array)[a*4+3]= cumap->cm[3].table[a].y;
950         }
951 }
952
953 /* ***************** Histogram **************** */
954
955 #define INV_255         (1.f/255.f)
956
957 DO_INLINE int get_bin_float(float f)
958 {
959         int bin= (int)(f*255);
960
961         /* note: clamp integer instead of float to avoid problems with NaN */
962         CLAMP(bin, 0, 255);
963         
964         //return (int) (((f + 0.25) / 1.5) * 255);
965         
966         return bin;
967 }
968
969 DO_INLINE void save_sample_line(Scopes *scopes, const int idx, const float fx, float *rgb, float *ycc)
970 {
971         float yuv[3];
972
973         /* vectorscope*/
974         rgb_to_yuv(rgb[0], rgb[1], rgb[2], &yuv[0], &yuv[1], &yuv[2]);
975         scopes->vecscope[idx + 0] = yuv[1];
976         scopes->vecscope[idx + 1] = yuv[2];
977
978         /* waveform */
979         switch (scopes->wavefrm_mode) {
980                 case SCOPES_WAVEFRM_RGB:
981                         scopes->waveform_1[idx + 0] = fx;
982                         scopes->waveform_1[idx + 1] = rgb[0];
983                         scopes->waveform_2[idx + 0] = fx;
984                         scopes->waveform_2[idx + 1] = rgb[1];
985                         scopes->waveform_3[idx + 0] = fx;
986                         scopes->waveform_3[idx + 1] = rgb[2];
987                         break;
988                 case SCOPES_WAVEFRM_LUMA:
989                         scopes->waveform_1[idx + 0] = fx;
990                         scopes->waveform_1[idx + 1] = ycc[0];
991                         break;
992                 case SCOPES_WAVEFRM_YCC_JPEG:
993                 case SCOPES_WAVEFRM_YCC_709:
994                 case SCOPES_WAVEFRM_YCC_601:
995                         scopes->waveform_1[idx + 0] = fx;
996                         scopes->waveform_1[idx + 1] = ycc[0];
997                         scopes->waveform_2[idx + 0] = fx;
998                         scopes->waveform_2[idx + 1] = ycc[1];
999                         scopes->waveform_3[idx + 0] = fx;
1000                         scopes->waveform_3[idx + 1] = ycc[2];
1001                         break;
1002         }
1003 }
1004
1005 void scopes_update(Scopes *scopes, ImBuf *ibuf, int use_color_management)
1006 {
1007         int x, y, c, n, nl;
1008         double div, divl;
1009         float *rf=NULL;
1010         unsigned char *rc=NULL;
1011         unsigned int *bin_r, *bin_g, *bin_b, *bin_lum;
1012         int savedlines, saveline;
1013         float rgb[3], ycc[3], luma;
1014         int ycc_mode=-1;
1015
1016         if (scopes->ok == 1 ) return;
1017
1018         if (scopes->hist.ymax == 0.f) scopes->hist.ymax = 1.f;
1019
1020         /* hmmmm */
1021         if (!(ELEM(ibuf->channels, 3, 4))) return;
1022         scopes->hist.channels = 3;
1023         scopes->hist.x_resolution = 256;
1024
1025         switch (scopes->wavefrm_mode) {
1026                 case SCOPES_WAVEFRM_RGB:
1027                         ycc_mode = -1;
1028                         break;
1029                 case SCOPES_WAVEFRM_LUMA:
1030                 case SCOPES_WAVEFRM_YCC_JPEG:
1031                         ycc_mode = BLI_YCC_JFIF_0_255;
1032                         break;
1033                 case SCOPES_WAVEFRM_YCC_601:
1034                         ycc_mode = BLI_YCC_ITU_BT601;
1035                         break;
1036                 case SCOPES_WAVEFRM_YCC_709:
1037                         ycc_mode = BLI_YCC_ITU_BT709;
1038                         break;
1039         }
1040
1041         /* temp table to count pix value for histo */
1042         bin_r = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1043         bin_g = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1044         bin_b = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1045         bin_lum = MEM_callocN(256 * sizeof(unsigned int), "temp historgram bins");
1046
1047         /* convert to number of lines with logarithmic scale */
1048         scopes->sample_lines = (scopes->accuracy*0.01) * (scopes->accuracy*0.01) * ibuf->y;
1049         
1050         if (scopes->sample_full)
1051                 scopes->sample_lines = ibuf->y;
1052
1053         /* scan the image */
1054         savedlines=0;
1055         for (c=0; c<3; c++) {
1056                 scopes->minmax[c][0]=25500.0f;
1057                 scopes->minmax[c][1]=-25500.0f;
1058         }
1059         
1060         scopes->waveform_tot = ibuf->x*scopes->sample_lines;
1061         
1062         if (scopes->waveform_1)
1063                 MEM_freeN(scopes->waveform_1);
1064         if (scopes->waveform_2)
1065                 MEM_freeN(scopes->waveform_2);
1066         if (scopes->waveform_3)
1067                 MEM_freeN(scopes->waveform_3);
1068         if (scopes->vecscope)
1069                 MEM_freeN(scopes->vecscope);
1070         
1071         scopes->waveform_1= MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 1");
1072         scopes->waveform_2= MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 2");
1073         scopes->waveform_3= MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "waveform point channel 3");
1074         scopes->vecscope= MEM_callocN(scopes->waveform_tot * 2 * sizeof(float), "vectorscope point channel");
1075         
1076         if (ibuf->rect_float)
1077                 rf = ibuf->rect_float;
1078         else if (ibuf->rect)
1079                 rc = (unsigned char *)ibuf->rect;
1080
1081         for (y = 0; y < ibuf->y; y++) {
1082                 if (savedlines<scopes->sample_lines && y>=((savedlines)*ibuf->y)/(scopes->sample_lines+1)) {
1083                         saveline=1;
1084                 } else saveline=0;
1085                 for (x = 0; x < ibuf->x; x++) {
1086
1087                         if (ibuf->rect_float) {
1088                                 if (use_color_management)
1089                                         linearrgb_to_srgb_v3_v3(rgb, rf);
1090                                 else
1091                                         copy_v3_v3(rgb, rf);
1092                         }
1093                         else if (ibuf->rect) {
1094                                 for (c=0; c<3; c++)
1095                                         rgb[c] = rc[c] * INV_255;
1096                         }
1097
1098                         /* we still need luma for histogram */
1099                         luma = 0.299*rgb[0] + 0.587*rgb[1] + 0.114 * rgb[2];
1100
1101                         /* check for min max */
1102                         if(ycc_mode == -1 ) {
1103                                 for (c=0; c<3; c++) {
1104                                         if (rgb[c] < scopes->minmax[c][0]) scopes->minmax[c][0] = rgb[c];
1105                                         if (rgb[c] > scopes->minmax[c][1]) scopes->minmax[c][1] = rgb[c];
1106                                 }
1107                         }
1108                         else {
1109                                 rgb_to_ycc(rgb[0],rgb[1],rgb[2],&ycc[0],&ycc[1],&ycc[2], ycc_mode);
1110                                 for (c=0; c<3; c++) {
1111                                         ycc[c] *=INV_255;
1112                                         if (ycc[c] < scopes->minmax[c][0]) scopes->minmax[c][0] = ycc[c];
1113                                         if (ycc[c] > scopes->minmax[c][1]) scopes->minmax[c][1] = ycc[c];
1114                                 }
1115                         }
1116                         /* increment count for histo*/
1117                         bin_r[ get_bin_float(rgb[0]) ] += 1;
1118                         bin_g[ get_bin_float(rgb[1]) ] += 1;
1119                         bin_b[ get_bin_float(rgb[2]) ] += 1;
1120                         bin_lum[ get_bin_float(luma) ] += 1;
1121
1122                         /* save sample if needed */
1123                         if(saveline) {
1124                                 const float fx = (float)x / (float)ibuf->x;
1125                                 const int idx = 2*(ibuf->x*savedlines+x);
1126                                 save_sample_line(scopes, idx, fx, rgb, ycc);
1127                         }
1128
1129                         rf+= ibuf->channels;
1130                         rc+= ibuf->channels;
1131                 }
1132                 if (saveline)
1133                         savedlines +=1;
1134         }
1135
1136         /* convert hist data to float (proportional to max count) */
1137         n=0;
1138         nl=0;
1139         for (x=0; x<256; x++) {
1140                 if (bin_r[x] > n)
1141                         n = bin_r[x];
1142                 if (bin_g[x] > n)
1143                         n = bin_g[x];
1144                 if (bin_b[x] > n)
1145                         n = bin_b[x];
1146                 if (bin_lum[x] > nl)
1147                         nl = bin_lum[x];
1148         }
1149         div = 1.f/(double)n;
1150         divl = 1.f/(double)nl;
1151         for (x=0; x<256; x++) {
1152                 scopes->hist.data_r[x] = bin_r[x] * div;
1153                 scopes->hist.data_g[x] = bin_g[x] * div;
1154                 scopes->hist.data_b[x] = bin_b[x] * div;
1155                 scopes->hist.data_luma[x] = bin_lum[x] * divl;
1156         }
1157         MEM_freeN(bin_r);
1158         MEM_freeN(bin_g);
1159         MEM_freeN(bin_b);
1160         MEM_freeN(bin_lum);
1161
1162         scopes->ok = 1;
1163 }
1164
1165 void scopes_free(Scopes *scopes)
1166 {
1167         if (scopes->waveform_1) {
1168                 MEM_freeN(scopes->waveform_1);
1169                 scopes->waveform_1 = NULL;
1170         }
1171         if (scopes->waveform_2) {
1172                 MEM_freeN(scopes->waveform_2);
1173                 scopes->waveform_2 = NULL;
1174         }
1175         if (scopes->waveform_3) {
1176                 MEM_freeN(scopes->waveform_3);
1177                 scopes->waveform_3 = NULL;
1178         }
1179         if (scopes->vecscope) {
1180                 MEM_freeN(scopes->vecscope);
1181                 scopes->vecscope = NULL;
1182         }
1183 }
1184
1185 void scopes_new(Scopes *scopes)
1186 {
1187         scopes->accuracy=30.0;
1188         scopes->hist.mode=HISTO_MODE_RGB;
1189         scopes->wavefrm_alpha=0.3;
1190         scopes->vecscope_alpha=0.3;
1191         scopes->wavefrm_height= 100;
1192         scopes->vecscope_height= 100;
1193         scopes->hist.height= 100;
1194         scopes->ok= 0;
1195         scopes->waveform_1 = NULL;
1196         scopes->waveform_2 = NULL;
1197         scopes->waveform_3 = NULL;
1198         scopes->vecscope = NULL;
1199 }