Initial commit. Not in build system so shouldn't interfere with anything at this...
[blender-staging.git] / source / blender / nodes / intern / CMP_util.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2006 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 LICENSE BLOCK *****
28  */
29
30 #include "CMP_util.h"
31
32 CompBuf *alloc_compbuf(int sizex, int sizey, int type, int alloc)
33 {
34         CompBuf *cbuf= MEM_callocN(sizeof(CompBuf), "compbuf");
35         
36         cbuf->x= sizex;
37         cbuf->y= sizey;
38         cbuf->xrad= sizex/2;
39         cbuf->yrad= sizey/2;
40         
41         cbuf->type= type;
42         if(alloc) {
43                 if(cbuf->type==CB_RGBA)
44                         cbuf->rect= MEM_mapallocN(4*sizeof(float)*sizex*sizey, "compbuf RGBA rect");
45                 else if(cbuf->type==CB_VEC3)
46                         cbuf->rect= MEM_mapallocN(3*sizeof(float)*sizex*sizey, "compbuf Vector3 rect");
47                 else if(cbuf->type==CB_VEC2)
48                         cbuf->rect= MEM_mapallocN(2*sizeof(float)*sizex*sizey, "compbuf Vector2 rect");
49                 else
50                         cbuf->rect= MEM_mapallocN(sizeof(float)*sizex*sizey, "compbuf Fac rect");
51                 cbuf->malloc= 1;
52         }
53         cbuf->disprect.xmin= 0;
54         cbuf->disprect.ymin= 0;
55         cbuf->disprect.xmax= sizex;
56         cbuf->disprect.ymax= sizey;
57         
58         return cbuf;
59 }
60
61 CompBuf *dupalloc_compbuf(CompBuf *cbuf)
62 {
63         CompBuf *dupbuf= alloc_compbuf(cbuf->x, cbuf->y, cbuf->type, 1);
64         if(dupbuf) {
65                 memcpy(dupbuf->rect, cbuf->rect, cbuf->type*sizeof(float)*cbuf->x*cbuf->y);
66         
67                 dupbuf->xof= cbuf->xof;
68                 dupbuf->yof= cbuf->yof;
69         }       
70         return dupbuf;
71 }
72
73 /* instead of reference counting, we create a list */
74 CompBuf *pass_on_compbuf(CompBuf *cbuf)
75 {
76         CompBuf *dupbuf= alloc_compbuf(cbuf->x, cbuf->y, cbuf->type, 0);
77         CompBuf *lastbuf;
78         
79         if(dupbuf) {
80                 dupbuf->rect= cbuf->rect;
81                 dupbuf->xof= cbuf->xof;
82                 dupbuf->yof= cbuf->yof;
83                 dupbuf->malloc= 0;
84                 
85                 /* get last buffer in list, and append dupbuf */
86                 for(lastbuf= dupbuf; lastbuf; lastbuf= lastbuf->next)
87                         if(lastbuf->next==NULL)
88                                 break;
89                 lastbuf->next= dupbuf;
90                 dupbuf->prev= lastbuf;
91         }       
92         return dupbuf;
93 }
94
95
96 void free_compbuf(CompBuf *cbuf)
97 {
98         /* check referencing, then remove from list and set malloc tag */
99         if(cbuf->prev || cbuf->next) {
100                 if(cbuf->prev)
101                         cbuf->prev->next= cbuf->next;
102                 if(cbuf->next)
103                         cbuf->next->prev= cbuf->prev;
104                 if(cbuf->malloc) {
105                         if(cbuf->prev)
106                                 cbuf->prev->malloc= 1;
107                         else
108                                 cbuf->next->malloc= 1;
109                         cbuf->malloc= 0;
110                 }
111         }
112         
113         if(cbuf->malloc && cbuf->rect)
114                 MEM_freeN(cbuf->rect);
115
116         MEM_freeN(cbuf);
117 }
118
119 void print_compbuf(char *str, CompBuf *cbuf)
120 {
121         printf("Compbuf %s %d %d %p\n", str, cbuf->x, cbuf->y, cbuf->rect);
122         
123 }
124
125 CompBuf *get_cropped_compbuf(rcti *drect, float *rectf, int rectx, int recty, int type)
126 {
127         CompBuf *cbuf;
128         rcti disprect= *drect;
129         float *outfp;
130         int dx, y;
131         
132         if(disprect.xmax>rectx) disprect.xmax= rectx;
133         if(disprect.ymax>recty) disprect.ymax= recty;
134         if(disprect.xmin>= disprect.xmax) return NULL;
135         if(disprect.ymin>= disprect.ymax) return NULL;
136         
137         cbuf= alloc_compbuf(disprect.xmax-disprect.xmin, disprect.ymax-disprect.ymin, type, 1);
138         outfp= cbuf->rect;
139         rectf += type*(disprect.ymin*rectx + disprect.xmin);
140         dx= type*cbuf->x;
141         for(y=cbuf->y; y>0; y--, outfp+=dx, rectf+=type*rectx)
142                 memcpy(outfp, rectf, sizeof(float)*dx);
143         
144         return cbuf;
145 }
146
147 CompBuf *scalefast_compbuf(CompBuf *inbuf, int newx, int newy)
148 {
149         CompBuf *outbuf; 
150         float *rectf, *newrectf, *rf;
151         int x, y, c, pixsize= inbuf->type;
152         int ofsx, ofsy, stepx, stepy;
153         
154         if(inbuf->x==newx && inbuf->y==newy)
155                 return dupalloc_compbuf(inbuf);
156         
157         outbuf= alloc_compbuf(newx, newy, inbuf->type, 1);
158         newrectf= outbuf->rect;
159         
160         stepx = (65536.0 * (inbuf->x - 1.0) / (newx - 1.0)) + 0.5;
161         stepy = (65536.0 * (inbuf->y - 1.0) / (newy - 1.0)) + 0.5;
162         ofsy = 32768;
163         
164         for (y = newy; y > 0 ; y--){
165                 rectf = inbuf->rect;
166                 rectf += pixsize * (ofsy >> 16) * inbuf->x;
167
168                 ofsy += stepy;
169                 ofsx = 32768;
170                 
171                 for (x = newx ; x>0 ; x--) {
172                         
173                         rf= rectf + pixsize*(ofsx >> 16);
174                         for(c=0; c<pixsize; c++)
175                                 newrectf[c] = rf[c];
176                         
177                         newrectf+= pixsize;
178                         
179                         ofsx += stepx;
180                 }
181         }
182         
183         return outbuf;
184 }
185
186 CompBuf *typecheck_compbuf(CompBuf *inbuf, int type)
187 {
188         if(inbuf && inbuf->type!=type && inbuf->rect_procedural==NULL) {
189                 CompBuf *outbuf= alloc_compbuf(inbuf->x, inbuf->y, type, 1); 
190                 float *inrf= inbuf->rect;
191                 float *outrf= outbuf->rect;
192                 int x= inbuf->x*inbuf->y;
193                 
194                 /* warning note: xof and yof are applied in pixelprocessor, but should be copied otherwise? */
195                 outbuf->xof= inbuf->xof;
196                 outbuf->yof= inbuf->yof;
197                 
198                 if(type==CB_VAL) {
199                         if(inbuf->type==CB_VEC2) {
200                                 for(; x>0; x--, outrf+= 1, inrf+= 2)
201                                         *outrf= 0.5f*(inrf[0]+inrf[1]);
202                         }
203                         else if(inbuf->type==CB_VEC3) {
204                                 for(; x>0; x--, outrf+= 1, inrf+= 3)
205                                         *outrf= 0.333333f*(inrf[0]+inrf[1]+inrf[2]);
206                         }
207                         else if(inbuf->type==CB_RGBA) {
208                                 for(; x>0; x--, outrf+= 1, inrf+= 4)
209                                         *outrf= inrf[0]*0.35f + inrf[1]*0.45f + inrf[2]*0.2f;
210                         }
211                 }
212                 else if(type==CB_VEC2) {
213                         if(inbuf->type==CB_VAL) {
214                                 for(; x>0; x--, outrf+= 2, inrf+= 1) {
215                                         outrf[0]= inrf[0];
216                                         outrf[1]= inrf[0];
217                                 }
218                         }
219                         else if(inbuf->type==CB_VEC3) {
220                                 for(; x>0; x--, outrf+= 2, inrf+= 3) {
221                                         outrf[0]= inrf[0];
222                                         outrf[1]= inrf[1];
223                                 }
224                         }
225                         else if(inbuf->type==CB_RGBA) {
226                                 for(; x>0; x--, outrf+= 2, inrf+= 4) {
227                                         outrf[0]= inrf[0];
228                                         outrf[1]= inrf[1];
229                                 }
230                         }
231                 }
232                 else if(type==CB_VEC3) {
233                         if(inbuf->type==CB_VAL) {
234                                 for(; x>0; x--, outrf+= 3, inrf+= 1) {
235                                         outrf[0]= inrf[0];
236                                         outrf[1]= inrf[0];
237                                         outrf[2]= inrf[0];
238                                 }
239                         }
240                         else if(inbuf->type==CB_VEC2) {
241                                 for(; x>0; x--, outrf+= 3, inrf+= 2) {
242                                         outrf[0]= inrf[0];
243                                         outrf[1]= inrf[1];
244                                         outrf[2]= 0.0f;
245                                 }
246                         }
247                         else if(inbuf->type==CB_RGBA) {
248                                 for(; x>0; x--, outrf+= 3, inrf+= 4) {
249                                         outrf[0]= inrf[0];
250                                         outrf[1]= inrf[1];
251                                         outrf[2]= inrf[2];
252                                 }
253                         }
254                 }
255                 else if(type==CB_RGBA) {
256                         if(inbuf->type==CB_VAL) {
257                                 for(; x>0; x--, outrf+= 4, inrf+= 1) {
258                                         outrf[0]= inrf[0];
259                                         outrf[1]= inrf[0];
260                                         outrf[2]= inrf[0];
261                                         outrf[3]= 1.0f;
262                                 }
263                         }
264                         else if(inbuf->type==CB_VEC2) {
265                                 for(; x>0; x--, outrf+= 4, inrf+= 2) {
266                                         outrf[0]= inrf[0];
267                                         outrf[1]= inrf[1];
268                                         outrf[2]= 0.0f;
269                                         outrf[3]= 1.0f;
270                                 }
271                         }
272                         else if(inbuf->type==CB_VEC3) {
273                                 for(; x>0; x--, outrf+= 4, inrf+= 3) {
274                                         outrf[0]= inrf[0];
275                                         outrf[1]= inrf[1];
276                                         outrf[2]= inrf[2];
277                                         outrf[3]= 1.0f;
278                                 }
279                         }
280                 }
281                 
282                 return outbuf;
283         }
284         return inbuf;
285 }
286
287 float *compbuf_get_pixel(CompBuf *cbuf, float *rectf, int x, int y, int xrad, int yrad)
288 {
289         if(cbuf) {
290                 if(cbuf->rect_procedural) {
291                         cbuf->rect_procedural(cbuf, rectf, (float)x/(float)xrad, (float)y/(float)yrad);
292                         return rectf;
293                 }
294                 else {
295                         static float col[4]= {0.0f, 0.0f, 0.0f, 0.0f};
296                         
297                         /* map coords */
298                         x-= cbuf->xof;
299                         y-= cbuf->yof;
300                         
301                         if(y<-cbuf->yrad || y>= -cbuf->yrad+cbuf->y) return col;
302                         if(x<-cbuf->xrad || x>= -cbuf->xrad+cbuf->x) return col;
303                         
304                         return cbuf->rect + cbuf->type*( (cbuf->yrad+y)*cbuf->x + (cbuf->xrad+x) );
305                 }
306         }
307         else return rectf;
308 }
309
310 /* **************************************************** */
311
312 /* Pixel-to-Pixel operation, 1 Image in, 1 out */
313 void composit1_pixel_processor(bNode *node, CompBuf *out, CompBuf *src_buf, float *src_col,
314                                                                           void (*func)(bNode *, float *, float *), 
315                                                                           int src_type)
316 {
317         CompBuf *src_use;
318         float *outfp=out->rect, *srcfp;
319         int xrad, yrad, x, y;
320         
321         src_use= typecheck_compbuf(src_buf, src_type);
322         
323         xrad= out->xrad;
324         yrad= out->yrad;
325         
326         for(y= -yrad; y<-yrad+out->y; y++) {
327                 for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
328                         srcfp= compbuf_get_pixel(src_use, src_col, x, y, xrad, yrad);
329                         func(node, outfp, srcfp);
330                 }
331         }
332         
333         if(src_use!=src_buf)
334                 free_compbuf(src_use);
335 }
336
337 /* Pixel-to-Pixel operation, 2 Images in, 1 out */
338 void composit2_pixel_processor(bNode *node, CompBuf *out, CompBuf *src_buf, float *src_col,
339                                                                           CompBuf *fac_buf, float *fac, void (*func)(bNode *, float *, float *, float *), 
340                                                                           int src_type, int fac_type)
341 {
342         CompBuf *src_use, *fac_use;
343         float *outfp=out->rect, *srcfp, *facfp;
344         int xrad, yrad, x, y;
345         
346         src_use= typecheck_compbuf(src_buf, src_type);
347         fac_use= typecheck_compbuf(fac_buf, fac_type);
348
349         xrad= out->xrad;
350         yrad= out->yrad;
351         
352         for(y= -yrad; y<-yrad+out->y; y++) {
353                 for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
354                         srcfp= compbuf_get_pixel(src_use, src_col, x, y, xrad, yrad);
355                         facfp= compbuf_get_pixel(fac_use, fac, x, y, xrad, yrad);
356                         
357                         func(node, outfp, srcfp, facfp);
358                 }
359         }
360         if(src_use!=src_buf)
361                 free_compbuf(src_use);
362         if(fac_use!=fac_buf)
363                 free_compbuf(fac_use);
364 }
365
366 /* Pixel-to-Pixel operation, 3 Images in, 1 out */
367 void composit3_pixel_processor(bNode *node, CompBuf *out, CompBuf *src1_buf, float *src1_col, CompBuf *src2_buf, float *src2_col, 
368                                                                           CompBuf *fac_buf, float *fac, void (*func)(bNode *, float *, float *, float *, float *), 
369                                                                           int src1_type, int src2_type, int fac_type)
370 {
371         CompBuf *src1_use, *src2_use, *fac_use;
372         float *outfp=out->rect, *src1fp, *src2fp, *facfp;
373         int xrad, yrad, x, y;
374         
375         src1_use= typecheck_compbuf(src1_buf, src1_type);
376         src2_use= typecheck_compbuf(src2_buf, src2_type);
377         fac_use= typecheck_compbuf(fac_buf, fac_type);
378         
379         xrad= out->xrad;
380         yrad= out->yrad;
381         
382         for(y= -yrad; y<-yrad+out->y; y++) {
383                 for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
384                         src1fp= compbuf_get_pixel(src1_use, src1_col, x, y, xrad, yrad);
385                         src2fp= compbuf_get_pixel(src2_use, src2_col, x, y, xrad, yrad);
386                         facfp= compbuf_get_pixel(fac_use, fac, x, y, xrad, yrad);
387                         
388                         func(node, outfp, src1fp, src2fp, facfp);
389                 }
390         }
391         
392         if(src1_use!=src1_buf)
393                 free_compbuf(src1_use);
394         if(src2_use!=src2_buf)
395                 free_compbuf(src2_use);
396         if(fac_use!=fac_buf)
397                 free_compbuf(fac_use);
398 }
399
400 /* Pixel-to-Pixel operation, 4 Images in, 1 out */
401 void composit4_pixel_processor(bNode *node, CompBuf *out, CompBuf *src1_buf, float *src1_col, CompBuf *fac1_buf, float *fac1, 
402                                                                           CompBuf *src2_buf, float *src2_col, CompBuf *fac2_buf, float *fac2, 
403                                                                           void (*func)(bNode *, float *, float *, float *, float *, float *), 
404                                                                           int src1_type, int fac1_type, int src2_type, int fac2_type)
405 {
406         CompBuf *src1_use, *src2_use, *fac1_use, *fac2_use;
407         float *outfp=out->rect, *src1fp, *src2fp, *fac1fp, *fac2fp;
408         int xrad, yrad, x, y;
409         
410         src1_use= typecheck_compbuf(src1_buf, src1_type);
411         src2_use= typecheck_compbuf(src2_buf, src2_type);
412         fac1_use= typecheck_compbuf(fac1_buf, fac1_type);
413         fac2_use= typecheck_compbuf(fac2_buf, fac2_type);
414         
415         xrad= out->xrad;
416         yrad= out->yrad;
417         
418         for(y= -yrad; y<-yrad+out->y; y++) {
419                 for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
420                         src1fp= compbuf_get_pixel(src1_use, src1_col, x, y, xrad, yrad);
421                         src2fp= compbuf_get_pixel(src2_use, src2_col, x, y, xrad, yrad);
422                         fac1fp= compbuf_get_pixel(fac1_use, fac1, x, y, xrad, yrad);
423                         fac2fp= compbuf_get_pixel(fac2_use, fac2, x, y, xrad, yrad);
424                         
425                         func(node, outfp, src1fp, fac1fp, src2fp, fac2fp);
426                 }
427         }
428         
429         if(src1_use!=src1_buf)
430                 free_compbuf(src1_use);
431         if(src2_use!=src2_buf)
432                 free_compbuf(src2_use);
433         if(fac1_use!=fac1_buf)
434                 free_compbuf(fac1_use);
435         if(fac2_use!=fac2_buf)
436                 free_compbuf(fac2_use);
437 }
438
439
440 CompBuf *valbuf_from_rgbabuf(CompBuf *cbuf, int channel)
441 {
442         CompBuf *valbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_VAL, 1);
443         float *valf, *rectf;
444         int tot;
445         
446         /* warning note: xof and yof are applied in pixelprocessor, but should be copied otherwise? */
447         valbuf->xof= cbuf->xof;
448         valbuf->yof= cbuf->yof;
449         
450         valf= valbuf->rect;
451         
452         /* defaults to returning alpha channel */
453         if ((channel < CHAN_R) && (channel > CHAN_A)) channel = CHAN_A;
454
455         rectf= cbuf->rect + channel;
456         
457         for(tot= cbuf->x*cbuf->y; tot>0; tot--, valf++, rectf+=4)
458                 *valf= *rectf;
459         
460         return valbuf;
461 }
462
463 void generate_preview(bNode *node, CompBuf *stackbuf)
464 {
465         bNodePreview *preview= node->preview;
466         
467         if(preview && stackbuf) {
468                 CompBuf *cbuf, *stackbuf_use;
469                 
470                 if(stackbuf->rect==NULL) return;
471                 
472                 stackbuf_use= typecheck_compbuf(stackbuf, CB_RGBA);
473                 
474                 if(stackbuf->x > stackbuf->y) {
475                         preview->xsize= 140;
476                         preview->ysize= (140*stackbuf->y)/stackbuf->x;
477                 }
478                 else {
479                         preview->ysize= 140;
480                         preview->xsize= (140*stackbuf->x)/stackbuf->y;
481                 }
482                 
483                 cbuf= scalefast_compbuf(stackbuf_use, preview->xsize, preview->ysize);
484                 
485                 /* this ensures free-compbuf does the right stuff */
486                 SWAP(float *, cbuf->rect, node->preview->rect);
487                 
488                 free_compbuf(cbuf);
489                 if(stackbuf_use!=stackbuf)
490                         free_compbuf(stackbuf_use);
491
492         }
493 }
494
495 void do_rgba_to_yuva(bNode *node, float *out, float *in)
496 {
497    rgb_to_yuv(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
498    out[3]=in[3];
499 }
500
501 void do_rgba_to_hsva(bNode *node, float *out, float *in)
502 {
503    rgb_to_hsv(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
504    out[3]=in[3];
505 }
506
507 void do_rgba_to_ycca(bNode *node, float *out, float *in)
508 {
509    rgb_to_ycc(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
510    out[3]=in[3];
511 }
512
513 void do_yuva_to_rgba(bNode *node, float *out, float *in)
514 {
515    yuv_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
516    out[3]=in[3];
517 }
518
519 void do_hsva_to_rgba(bNode *node, float *out, float *in)
520 {
521    hsv_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
522    out[3]=in[3];
523 }
524
525 void do_ycca_to_rgba(bNode *node, float *out, float *in)
526 {
527    ycc_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
528    out[3]=in[3];
529 }
530
531 void do_copy_rgba(bNode *node, float *out, float *in)
532 {
533    QUATCOPY(out, in);
534 }
535
536 void do_copy_rgb(bNode *node, float *out, float *in)
537 {
538    VECCOPY(out, in);
539    out[3]= 1.0f;
540 }
541
542 void do_copy_value(bNode *node, float *out, float *in)
543 {
544    out[0]= in[0];
545 }
546
547 void do_copy_a_rgba(bNode *node, float *out, float *in, float *fac)
548 {
549    VECCOPY(out, in);
550    out[3]= *fac;
551 }
552
553 /* only accepts RGBA buffers */
554 void gamma_correct_compbuf(CompBuf *img, int inversed)
555 {
556    float *drect;
557    int x;
558
559    if(img->type!=CB_RGBA) return;
560
561    drect= img->rect;
562    if(inversed) {
563       for(x=img->x*img->y; x>0; x--, drect+=4) {
564          if(drect[0]>0.0f) drect[0]= sqrt(drect[0]); else drect[0]= 0.0f;
565          if(drect[1]>0.0f) drect[1]= sqrt(drect[1]); else drect[1]= 0.0f;
566          if(drect[2]>0.0f) drect[2]= sqrt(drect[2]); else drect[2]= 0.0f;
567       }
568    }
569    else {
570       for(x=img->x*img->y; x>0; x--, drect+=4) {
571          if(drect[0]>0.0f) drect[0]*= drect[0]; else drect[0]= 0.0f;
572          if(drect[1]>0.0f) drect[1]*= drect[1]; else drect[1]= 0.0f;
573          if(drect[2]>0.0f) drect[2]*= drect[2]; else drect[2]= 0.0f;
574       }
575    }
576 }