fix [#26732] Colorband bug with compositing.
[blender.git] / source / blender / blenkernel / intern / texture.c
1 /* texture.c
2  *
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 /** \file blender/blenkernel/intern/texture.c
33  *  \ingroup bke
34  */
35
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <math.h>
41
42 #include "MEM_guardedalloc.h"
43
44 #include "PIL_dynlib.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_math.h"
48 #include "BLI_kdopbvh.h"
49 #include "BLI_utildefines.h"
50
51 #include "DNA_key_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_world_types.h"
56 #include "DNA_brush_types.h"
57 #include "DNA_node_types.h"
58 #include "DNA_color_types.h"
59 #include "DNA_particle_types.h"
60
61 #include "IMB_imbuf.h"
62
63 #include "BKE_plugin_types.h"
64 #include "BKE_utildefines.h"
65 #include "BKE_global.h"
66 #include "BKE_main.h"
67
68 #include "BKE_library.h"
69 #include "BKE_image.h"
70 #include "BKE_material.h"
71 #include "BKE_texture.h"
72 #include "BKE_key.h"
73 #include "BKE_icons.h"
74 #include "BKE_node.h"
75 #include "BKE_animsys.h"
76
77
78 /* ------------------------------------------------------------------------- */
79
80 /* All support for plugin textures: */
81 int test_dlerr(const char *name, const char *symbol)
82 {
83         char *err;
84         
85         err= PIL_dynlib_get_error_as_string(NULL);
86         if(err) {
87                 printf("var1: %s, var2: %s, var3: %s\n", name, symbol, err);
88                 return 1;
89         }
90         
91         return 0;
92 }
93
94 /* ------------------------------------------------------------------------- */
95
96 void open_plugin_tex(PluginTex *pit)
97 {
98         int (*version)(void);
99         
100         /* init all the happy variables */
101         pit->doit= NULL;
102         pit->pname= NULL;
103         pit->stnames= NULL;
104         pit->varstr= NULL;
105         pit->result= NULL;
106         pit->cfra= NULL;
107         pit->version= 0;
108         pit->instance_init= NULL;
109         
110         /* clear the error list */
111         PIL_dynlib_get_error_as_string(NULL);
112
113         /* no PIL_dynlib_close! multiple opened plugins... */
114         /* if(pit->handle) PIL_dynlib_close(pit->handle); */
115         /* pit->handle= 0; */
116
117         /* open the needed object */
118         pit->handle= PIL_dynlib_open(pit->name);
119         if(test_dlerr(pit->name, pit->name)) return;
120
121         if (pit->handle != NULL) {
122                 /* find the address of the version function */
123                 version= (int (*)(void)) PIL_dynlib_find_symbol(pit->handle, "plugin_tex_getversion");
124                 if (test_dlerr(pit->name, "plugin_tex_getversion")) return;
125                 
126                 if (version != NULL) {
127                         pit->version= version();
128                         if( pit->version >= 2 && pit->version <=6) {
129                                 int (*info_func)(PluginInfo *);
130                                 PluginInfo *info= (PluginInfo*) MEM_mallocN(sizeof(PluginInfo), "plugin_info"); 
131
132                                 info_func= (int (*)(PluginInfo *))PIL_dynlib_find_symbol(pit->handle, "plugin_getinfo");
133                                 if (!test_dlerr(pit->name, "plugin_getinfo")) {
134                                         info->instance_init = NULL;
135
136                                         info_func(info);
137
138                                         pit->doit= (int(*)(void)) info->tex_doit;
139                                         pit->callback= (void(*)(unsigned short)) info->callback;
140                                         pit->stypes= info->stypes;
141                                         pit->vars= info->nvars;
142                                         pit->pname= info->name;
143                                         pit->stnames= info->snames;
144                                         pit->varstr= info->varstr;
145                                         pit->result= info->result;
146                                         pit->cfra= info->cfra;
147                                         pit->instance_init = info->instance_init;
148                                         if (info->init) info->init();
149                                 }
150                                 MEM_freeN(info);
151                         } else {
152                                 printf ("Plugin returned unrecognized version number\n");
153                                 return;
154                         }
155                 }
156         }
157 }
158
159 /* ------------------------------------------------------------------------- */
160
161 /* very badlevel define to bypass linking with BIF_interface.h */
162 #define INT     96
163 #define FLO     128
164
165 PluginTex *add_plugin_tex(char *str)
166 {
167         PluginTex *pit;
168         VarStruct *varstr;
169         int a;
170         
171         pit= MEM_callocN(sizeof(PluginTex), "plugintex");
172         
173         strcpy(pit->name, str);
174         open_plugin_tex(pit);
175         
176         if(pit->doit==NULL) {
177                 if(pit->handle==NULL) {;} //XXX error("no plugin: %s", str);
178                 else {;} //XXX error("in plugin: %s", str);
179                 MEM_freeN(pit);
180                 return NULL;
181         }
182         
183         varstr= pit->varstr;
184         for(a=0; a<pit->vars; a++, varstr++) {
185                 if( (varstr->type & FLO)==FLO)
186                         pit->data[a]= varstr->def;
187                 else if( (varstr->type & INT)==INT)
188                         *((int *)(pit->data+a))= (int) varstr->def;
189         }
190
191         if (pit->instance_init)
192                 pit->instance_init((void *) pit->data);
193
194         return pit;
195 }
196
197 /* ------------------------------------------------------------------------- */
198
199 void free_plugin_tex(PluginTex *pit)
200 {
201         if(pit==NULL) return;
202                 
203         /* no PIL_dynlib_close: same plugin can be opened multiple times, 1 handle */
204         MEM_freeN(pit); 
205 }
206
207 /* ****************** Mapping ******************* */
208
209 TexMapping *add_mapping(void)
210 {
211         TexMapping *texmap= MEM_callocN(sizeof(TexMapping), "Tex map");
212         
213         texmap->size[0]= texmap->size[1]= texmap->size[2]= 1.0f;
214         texmap->max[0]= texmap->max[1]= texmap->max[2]= 1.0f;
215         unit_m4(texmap->mat);
216         
217         return texmap;
218 }
219
220 void init_mapping(TexMapping *texmap)
221 {
222         float eul[3], smat[3][3], rmat[3][3], mat[3][3];
223         
224         size_to_mat3( smat,texmap->size);
225         
226         eul[0]= DEG2RADF(texmap->rot[0]);
227         eul[1]= DEG2RADF(texmap->rot[1]);
228         eul[2]= DEG2RADF(texmap->rot[2]);
229         eul_to_mat3( rmat,eul);
230         
231         mul_m3_m3m3(mat, rmat, smat);
232         
233         copy_m4_m3(texmap->mat, mat);
234         VECCOPY(texmap->mat[3], texmap->loc);
235
236 }
237
238 /* ****************** COLORBAND ******************* */
239
240 void init_colorband(ColorBand *coba, int rangetype)
241 {
242         int a;
243         
244         coba->data[0].pos= 0.0;
245         coba->data[1].pos= 1.0;
246         
247         if(rangetype==0) {
248                 coba->data[0].r= 0.0;
249                 coba->data[0].g= 0.0;
250                 coba->data[0].b= 0.0;
251                 coba->data[0].a= 0.0;
252                 
253                 coba->data[1].r= 1.0;
254                 coba->data[1].g= 1.0;
255                 coba->data[1].b= 1.0;
256                 coba->data[1].a= 1.0;
257         }
258         else {
259                 coba->data[0].r= 0.0;
260                 coba->data[0].g= 0.0;
261                 coba->data[0].b= 0.0;
262                 coba->data[0].a= 1.0;
263                 
264                 coba->data[1].r= 1.0;
265                 coba->data[1].g= 1.0;
266                 coba->data[1].b= 1.0;
267                 coba->data[1].a= 1.0;
268         }
269         
270         for(a=2; a<MAXCOLORBAND; a++) {
271                 coba->data[a].r= 0.5;
272                 coba->data[a].g= 0.5;
273                 coba->data[a].b= 0.5;
274                 coba->data[a].a= 1.0;
275                 coba->data[a].pos= 0.5;
276         }
277         
278         coba->tot= 2;
279         
280 }
281
282 ColorBand *add_colorband(int rangetype)
283 {
284         ColorBand *coba;
285         
286         coba= MEM_callocN( sizeof(ColorBand), "colorband");
287         init_colorband(coba, rangetype);
288         
289         return coba;
290 }
291
292 /* ------------------------------------------------------------------------- */
293
294 int do_colorband(ColorBand *coba, float in, float out[4])
295 {
296         CBData *cbd1, *cbd2, *cbd0, *cbd3;
297         float fac, mfac, t[4];
298         int a;
299         
300         if(coba==NULL || coba->tot==0) return 0;
301         
302         cbd1= coba->data;
303         if(coba->tot==1) {
304                 out[0]= cbd1->r;
305                 out[1]= cbd1->g;
306                 out[2]= cbd1->b;
307                 out[3]= cbd1->a;
308         }
309         else {
310                 if(in <= cbd1->pos && coba->ipotype<2) {
311                         out[0]= cbd1->r;
312                         out[1]= cbd1->g;
313                         out[2]= cbd1->b;
314                         out[3]= cbd1->a;
315                 }
316                 else {
317                         CBData left, right;
318                         
319                         /* we're looking for first pos > in */
320                         for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos > in) break;
321                                 
322                         if(a==coba->tot) {
323                                 cbd2= cbd1-1;
324                                 right= *cbd2;
325                                 right.pos= 1.0f;
326                                 cbd1= &right;
327                         }
328                         else if(a==0) {
329                                 left= *cbd1;
330                                 left.pos= 0.0f;
331                                 cbd2= &left;
332                         }
333                         else cbd2= cbd1-1;
334                         
335                         if(in >= cbd1->pos && coba->ipotype<2) {
336                                 out[0]= cbd1->r;
337                                 out[1]= cbd1->g;
338                                 out[2]= cbd1->b;
339                                 out[3]= cbd1->a;
340                         }
341                         else {
342                 
343                                 if(cbd2->pos!=cbd1->pos)
344                                         fac= (in-cbd1->pos)/(cbd2->pos-cbd1->pos);
345                                 else {
346                                         /* was setting to 0.0 in 2.56 & previous, but this
347                                          * is incorrect for the last element, see [#26732] */
348                                         fac= (a != coba->tot) ? 0.0f : 1.0f;
349                                 }
350                                 
351                                 if (coba->ipotype==4) {
352                                         /* constant */
353                                         out[0]= cbd2->r;
354                                         out[1]= cbd2->g;
355                                         out[2]= cbd2->b;
356                                         out[3]= cbd2->a;
357                                         return 1;
358                                 }
359                                 
360                                 if(coba->ipotype>=2) {
361                                         /* ipo from right to left: 3 2 1 0 */
362                                         
363                                         if(a>=coba->tot-1) cbd0= cbd1;
364                                         else cbd0= cbd1+1;
365                                         if(a<2) cbd3= cbd2;
366                                         else cbd3= cbd2-1;
367                                         
368                                         CLAMP(fac, 0.0f, 1.0f);
369                                         
370                                         if(coba->ipotype==3)
371                                                 key_curve_position_weights(fac, t, KEY_CARDINAL);
372                                         else
373                                                 key_curve_position_weights(fac, t, KEY_BSPLINE);
374
375                                         out[0]= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r;
376                                         out[1]= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g;
377                                         out[2]= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b;
378                                         out[3]= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a;
379                                         CLAMP(out[0], 0.0f, 1.0f);
380                                         CLAMP(out[1], 0.0f, 1.0f);
381                                         CLAMP(out[2], 0.0f, 1.0f);
382                                         CLAMP(out[3], 0.0f, 1.0f);
383                                 }
384                                 else {
385                                 
386                                         if(coba->ipotype==1) {  /* EASE */
387                                                 mfac= fac*fac;
388                                                 fac= 3.0f*mfac-2.0f*mfac*fac;
389                                         }
390                                         mfac= 1.0f-fac;
391                                         
392                                         out[0]= mfac*cbd1->r + fac*cbd2->r;
393                                         out[1]= mfac*cbd1->g + fac*cbd2->g;
394                                         out[2]= mfac*cbd1->b + fac*cbd2->b;
395                                         out[3]= mfac*cbd1->a + fac*cbd2->a;
396                                 }
397                         }
398                 }
399         }
400         return 1;       /* OK */
401 }
402
403 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
404 {
405         int a;
406         
407         *size = CM_TABLE+1;
408         *array = MEM_callocN(sizeof(float)*(*size)*4, "ColorBand");
409
410         for(a=0; a<*size; a++)
411                 do_colorband(coba, (float)a/(float)CM_TABLE, &(*array)[a*4]);
412 }
413
414 int vergcband(const void *a1, const void *a2)
415 {
416         const CBData *x1=a1, *x2=a2;
417
418         if( x1->pos > x2->pos ) return 1;
419         else if( x1->pos < x2->pos) return -1;
420         return 0;
421 }
422
423 CBData *colorband_element_add(struct ColorBand *coba, float position)
424 {
425         int a;
426
427         if(coba->tot==MAXCOLORBAND) {
428                 return NULL;
429         }
430         else if(coba->tot > 0) {
431                 CBData *xnew;
432                 float col[4];
433
434                 do_colorband(coba, position, col);
435
436                 xnew = &coba->data[coba->tot];
437                 xnew->pos = position;
438
439                 xnew->r = col[0];
440                 xnew->g = col[1];
441                 xnew->b = col[2];
442                 xnew->a = col[3];
443         }
444
445         coba->tot++;
446         coba->cur = coba->tot-1;
447
448         for(a = 0; a < coba->tot; a++)
449                 coba->data[a].cur = a;
450
451         qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
452
453         for(a = 0; a < coba->tot; a++) {
454                 if(coba->data[a].cur == coba->cur) {
455                         coba->cur = a;
456                         break;
457                 }
458         }
459
460         return coba->data + coba->cur;
461 }
462
463 int colorband_element_remove(struct ColorBand *coba, int index)
464 {
465         int a;
466
467         if(coba->tot < 2)
468                 return 0;
469
470         if(index < 0 || index >= coba->tot)
471                 return 0;
472
473         for(a = index; a < coba->tot; a++) {
474                 coba->data[a] = coba->data[a + 1];
475         }
476         if(coba->cur) coba->cur--;
477         coba->tot--;
478         return 1;
479 }
480
481 /* ******************* TEX ************************ */
482
483 void free_texture(Tex *tex)
484 {
485         free_plugin_tex(tex->plugin);
486         
487         if(tex->coba) MEM_freeN(tex->coba);
488         if(tex->env) BKE_free_envmap(tex->env);
489         if(tex->pd) BKE_free_pointdensity(tex->pd);
490         if(tex->vd) BKE_free_voxeldata(tex->vd);
491         BKE_free_animdata((struct ID *)tex);
492         
493         BKE_previewimg_free(&tex->preview);
494         BKE_icon_delete((struct ID*)tex);
495         tex->id.icon_id = 0;
496         
497         if(tex->nodetree) {
498                 ntreeFreeTree(tex->nodetree);
499                 MEM_freeN(tex->nodetree);
500         }
501 }
502
503 /* ------------------------------------------------------------------------- */
504
505 void default_tex(Tex *tex)
506 {
507         PluginTex *pit;
508         VarStruct *varstr;
509         int a;
510
511         tex->type= TEX_CLOUDS;
512         tex->stype= 0;
513         tex->flag= TEX_CHECKER_ODD;
514         tex->imaflag= TEX_INTERPOL|TEX_MIPMAP|TEX_USEALPHA;
515         tex->extend= TEX_REPEAT;
516         tex->cropxmin= tex->cropymin= 0.0;
517         tex->cropxmax= tex->cropymax= 1.0;
518         tex->texfilter = TXF_EWA;
519         tex->afmax = 8;
520         tex->xrepeat= tex->yrepeat= 1;
521         tex->fie_ima= 2;
522         tex->sfra= 1;
523         tex->frames= 0;
524         tex->offset= 0;
525         tex->noisesize= 0.25;
526         tex->noisedepth= 2;
527         tex->turbul= 5.0;
528         tex->nabla= 0.025;      // also in do_versions
529         tex->bright= 1.0;
530         tex->contrast= 1.0;
531         tex->saturation= 1.0;
532         tex->filtersize= 1.0;
533         tex->rfac= 1.0;
534         tex->gfac= 1.0;
535         tex->bfac= 1.0;
536         /* newnoise: init. */
537         tex->noisebasis = 0;
538         tex->noisebasis2 = 0;
539         /* musgrave */
540         tex->mg_H = 1.0;
541         tex->mg_lacunarity = 2.0;
542         tex->mg_octaves = 2.0;
543         tex->mg_offset = 1.0;
544         tex->mg_gain = 1.0;
545         tex->ns_outscale = 1.0;
546         /* distnoise */
547         tex->dist_amount = 1.0;
548         /* voronoi */
549         tex->vn_w1 = 1.0;
550         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
551         tex->vn_mexp = 2.5;
552         tex->vn_distm = 0;
553         tex->vn_coltype = 0;
554
555         if (tex->env) {
556                 tex->env->stype=ENV_ANIM;
557                 tex->env->clipsta=0.1;
558                 tex->env->clipend=100;
559                 tex->env->cuberes=600;
560                 tex->env->depth=0;
561         }
562
563         if (tex->pd) {
564                 tex->pd->radius = 0.3f;
565                 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
566         }
567         
568         if (tex->vd) {
569                 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
570                 tex->vd->interp_type=TEX_VD_LINEAR;
571                 tex->vd->file_format=TEX_VD_SMOKE;
572         }
573         pit = tex->plugin;
574         if (pit) {
575                 varstr= pit->varstr;
576                 if(varstr) {
577                         for(a=0; a<pit->vars; a++, varstr++) {
578                                 pit->data[a] = varstr->def;
579                         }
580                 }
581         }
582         
583         tex->iuser.fie_ima= 2;
584         tex->iuser.ok= 1;
585         tex->iuser.frames= 100;
586         tex->iuser.sfra= 1;
587         
588         tex->preview = NULL;
589 }
590
591 void tex_set_type(Tex *tex, int type)
592 {
593         switch(type) {
594                         
595                 case TEX_VOXELDATA:
596                         if (tex->vd == NULL)
597                                 tex->vd = BKE_add_voxeldata();
598                         break;
599                 case TEX_POINTDENSITY:
600                         if (tex->pd == NULL)
601                                 tex->pd = BKE_add_pointdensity();
602                         break;
603                 case TEX_ENVMAP:
604                         if (tex->env == NULL)
605                                 tex->env = BKE_add_envmap();
606                         break;
607         }
608         
609         tex->type = type;
610 }
611
612 /* ------------------------------------------------------------------------- */
613
614 Tex *add_texture(const char *name)
615 {
616         Main *bmain= G.main;
617         Tex *tex;
618
619         tex= alloc_libblock(&bmain->tex, ID_TE, name);
620         
621         default_tex(tex);
622         
623         return tex;
624 }
625
626 /* ------------------------------------------------------------------------- */
627
628 void default_mtex(MTex *mtex)
629 {
630         mtex->texco= TEXCO_ORCO;
631         mtex->mapto= MAP_COL;
632         mtex->object= NULL;
633         mtex->projx= PROJ_X;
634         mtex->projy= PROJ_Y;
635         mtex->projz= PROJ_Z;
636         mtex->mapping= MTEX_FLAT;
637         mtex->ofs[0]= 0.0;
638         mtex->ofs[1]= 0.0;
639         mtex->ofs[2]= 0.0;
640         mtex->size[0]= 1.0;
641         mtex->size[1]= 1.0;
642         mtex->size[2]= 1.0;
643         mtex->tex= NULL;
644         mtex->texflag= MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE;
645         mtex->colormodel= 0;
646         mtex->r= 1.0;
647         mtex->g= 0.0;
648         mtex->b= 1.0;
649         mtex->k= 1.0;
650         mtex->def_var= 1.0;
651         mtex->blendtype= MTEX_BLEND;
652         mtex->colfac= 1.0;
653         mtex->norfac= 1.0;
654         mtex->varfac= 1.0;
655         mtex->dispfac=0.2;
656         mtex->colspecfac= 1.0f;
657         mtex->mirrfac= 1.0f;
658         mtex->alphafac= 1.0f;
659         mtex->difffac= 1.0f;
660         mtex->specfac= 1.0f;
661         mtex->emitfac= 1.0f;
662         mtex->hardfac= 1.0f;
663         mtex->raymirrfac= 1.0f;
664         mtex->translfac= 1.0f;
665         mtex->ambfac= 1.0f;
666         mtex->colemitfac= 1.0f;
667         mtex->colreflfac= 1.0f;
668         mtex->coltransfac= 1.0f;
669         mtex->densfac= 1.0f;
670         mtex->scatterfac= 1.0f;
671         mtex->reflfac= 1.0f;
672         mtex->shadowfac= 1.0f;
673         mtex->zenupfac= 1.0f;
674         mtex->zendownfac= 1.0f;
675         mtex->blendfac= 1.0f;
676         mtex->timefac= 1.0f;
677         mtex->lengthfac= 1.0f;
678         mtex->clumpfac= 1.0f;
679         mtex->kinkfac= 1.0f;
680         mtex->roughfac= 1.0f;
681         mtex->padensfac= 1.0f;
682         mtex->lifefac= 1.0f;
683         mtex->sizefac= 1.0f;
684         mtex->ivelfac= 1.0f;
685         mtex->dampfac= 1.0f;
686         mtex->gravityfac= 1.0f;
687         mtex->fieldfac= 1.0f;
688         mtex->normapspace= MTEX_NSPACE_TANGENT;
689 }
690
691
692 /* ------------------------------------------------------------------------- */
693
694 MTex *add_mtex(void)
695 {
696         MTex *mtex;
697         
698         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
699         
700         default_mtex(mtex);
701         
702         return mtex;
703 }
704
705 /* slot -1 for first free ID */
706 MTex *add_mtex_id(ID *id, int slot)
707 {
708         MTex **mtex_ar;
709         short act;
710
711         give_active_mtex(id, &mtex_ar, &act);
712
713         if(mtex_ar==NULL) {
714                 return NULL;
715         }
716         
717         if(slot==-1) {
718                 /* find first free */
719                 int i;          
720                 for (i= 0; i < MAX_MTEX; i++) {
721                         if (!mtex_ar[i]) {
722                                 slot= i;
723                                 break;
724                         }
725                 }
726                 if(slot == -1) {
727                         return NULL;
728                 }
729         }
730         else {
731                 /* make sure slot is valid */
732                 if(slot < 0 || slot >= MAX_MTEX) {
733                         return NULL;
734                 }
735         }
736
737         if (mtex_ar[slot]) {
738                 id_us_min((ID *)mtex_ar[slot]->tex);
739                 MEM_freeN(mtex_ar[slot]);
740                 mtex_ar[slot]= NULL;
741         }
742
743         mtex_ar[slot]= add_mtex();
744
745         return mtex_ar[slot];
746 }
747
748 /* ------------------------------------------------------------------------- */
749
750 Tex *copy_texture(Tex *tex)
751 {
752         Tex *texn;
753         
754         texn= copy_libblock(tex);
755         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
756         else texn->ima= NULL;
757         
758         if(texn->plugin) {
759                 texn->plugin= MEM_dupallocN(texn->plugin);
760                 open_plugin_tex(texn->plugin);
761         }
762         
763         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
764         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
765         if(texn->pd) texn->pd= MEM_dupallocN(texn->pd);
766         if(texn->vd) texn->vd= MEM_dupallocN(texn->vd);
767         
768         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
769
770         if(tex->nodetree) {
771                 ntreeEndExecTree(tex->nodetree);
772                 texn->nodetree= ntreeCopyTree(tex->nodetree); 
773         }
774         
775         return texn;
776 }
777
778 /* texture copy without adding to main dbase */
779 Tex *localize_texture(Tex *tex)
780 {
781         Tex *texn;
782         
783         texn= copy_libblock(tex);
784         BLI_remlink(&G.main->tex, texn);
785         
786         /* image texture: free_texture also doesn't decrease */
787         
788         if(texn->plugin) {
789                 texn->plugin= MEM_dupallocN(texn->plugin);
790                 open_plugin_tex(texn->plugin);
791         }
792         
793         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
794         if(texn->env) {
795                 texn->env= BKE_copy_envmap(texn->env);
796                 id_us_min(&texn->env->ima->id);
797         }
798         if(texn->pd) texn->pd= MEM_dupallocN(texn->pd);
799         if(texn->vd) {
800                 texn->vd= MEM_dupallocN(texn->vd);
801                 if(texn->vd->dataset)
802                         texn->vd->dataset= MEM_dupallocN(texn->vd->dataset);
803         }
804         
805         texn->preview = NULL;
806         
807         if(tex->nodetree) {
808                 texn->nodetree= ntreeLocalize(tex->nodetree);
809         }
810         
811         return texn;
812 }
813
814
815 /* ------------------------------------------------------------------------- */
816
817 void make_local_texture(Tex *tex)
818 {
819         Main *bmain= G.main;
820         Tex *texn;
821         Material *ma;
822         World *wrld;
823         Lamp *la;
824         Brush *br;
825         ParticleSettings *pa;
826         int a, local=0, lib=0;
827
828         /* - only lib users: do nothing
829                 * - only local users: set flag
830                 * - mixed: make copy
831                 */
832         
833         if(tex->id.lib==NULL) return;
834
835         /* special case: ima always local immediately */
836         if(tex->ima) {
837                 tex->ima->id.lib= NULL;
838                 tex->ima->id.flag= LIB_LOCAL;
839                 new_id(NULL, (ID *)tex->ima, NULL);
840         }
841
842         if(tex->id.us==1) {
843                 tex->id.lib= NULL;
844                 tex->id.flag= LIB_LOCAL;
845                 new_id(NULL, (ID *)tex, NULL);
846
847                 return;
848         }
849         
850         ma= bmain->mat.first;
851         while(ma) {
852                 for(a=0; a<MAX_MTEX; a++) {
853                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
854                                 if(ma->id.lib) lib= 1;
855                                 else local= 1;
856                         }
857                 }
858                 ma= ma->id.next;
859         }
860         la= bmain->lamp.first;
861         while(la) {
862                 for(a=0; a<MAX_MTEX; a++) {
863                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
864                                 if(la->id.lib) lib= 1;
865                                 else local= 1;
866                         }
867                 }
868                 la= la->id.next;
869         }
870         wrld= bmain->world.first;
871         while(wrld) {
872                 for(a=0; a<MAX_MTEX; a++) {
873                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
874                                 if(wrld->id.lib) lib= 1;
875                                 else local= 1;
876                         }
877                 }
878                 wrld= wrld->id.next;
879         }
880         br= bmain->brush.first;
881         while(br) {
882                 if(br->mtex.tex==tex) {
883                         if(br->id.lib) lib= 1;
884                         else local= 1;
885                 }
886                 br= br->id.next;
887         }
888         pa= bmain->particle.first;
889         while(pa) {
890                 for(a=0; a<MAX_MTEX; a++) {
891                         if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
892                                 if(pa->id.lib) lib= 1;
893                                 else local= 1;
894                         }
895                 }
896                 pa= pa->id.next;
897         }
898         
899         if(local && lib==0) {
900                 tex->id.lib= NULL;
901                 tex->id.flag= LIB_LOCAL;
902                 new_id(NULL, (ID *)tex, NULL);
903         }
904         else if(local && lib) {
905                 texn= copy_texture(tex);
906                 texn->id.us= 0;
907                 
908                 ma= bmain->mat.first;
909                 while(ma) {
910                         for(a=0; a<MAX_MTEX; a++) {
911                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
912                                         if(ma->id.lib==NULL) {
913                                                 ma->mtex[a]->tex= texn;
914                                                 texn->id.us++;
915                                                 tex->id.us--;
916                                         }
917                                 }
918                         }
919                         ma= ma->id.next;
920                 }
921                 la= bmain->lamp.first;
922                 while(la) {
923                         for(a=0; a<MAX_MTEX; a++) {
924                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
925                                         if(la->id.lib==NULL) {
926                                                 la->mtex[a]->tex= texn;
927                                                 texn->id.us++;
928                                                 tex->id.us--;
929                                         }
930                                 }
931                         }
932                         la= la->id.next;
933                 }
934                 wrld= bmain->world.first;
935                 while(wrld) {
936                         for(a=0; a<MAX_MTEX; a++) {
937                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
938                                         if(wrld->id.lib==NULL) {
939                                                 wrld->mtex[a]->tex= texn;
940                                                 texn->id.us++;
941                                                 tex->id.us--;
942                                         }
943                                 }
944                         }
945                         wrld= wrld->id.next;
946                 }
947                 br= bmain->brush.first;
948                 while(br) {
949                         if(br->mtex.tex==tex) {
950                                 if(br->id.lib==NULL) {
951                                         br->mtex.tex= texn;
952                                         texn->id.us++;
953                                         tex->id.us--;
954                                 }
955                         }
956                         br= br->id.next;
957                 }
958                 pa= bmain->particle.first;
959                 while(pa) {
960                         for(a=0; a<MAX_MTEX; a++) {
961                                 if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
962                                         if(pa->id.lib==NULL) {
963                                                 pa->mtex[a]->tex= texn;
964                                                 texn->id.us++;
965                                                 tex->id.us--;
966                                         }
967                                 }
968                         }
969                         pa= pa->id.next;
970                 }
971         }
972 }
973
974 /* ------------------------------------------------------------------------- */
975
976 void autotexname(Tex *tex)
977 {
978         Main *bmain= G.main;
979         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
980                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
981                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
982         Image *ima;
983         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
984         
985         if(tex) {
986                 if(tex->use_nodes) {
987                         new_id(&bmain->tex, (ID *)tex, "Noddy");
988                 }
989                 else
990                 if(tex->type==TEX_IMAGE) {
991                         ima= tex->ima;
992                         if(ima) {
993                                 strcpy(di, ima->name);
994                                 BLI_splitdirstring(di, fi);
995                                 strcpy(di, "I.");
996                                 strcat(di, fi);
997                                 new_id(&bmain->tex, (ID *)tex, di);
998                         }
999                         else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1000                 }
1001                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
1002                 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1003         }
1004 }
1005
1006 /* ------------------------------------------------------------------------- */
1007
1008 Tex *give_current_object_texture(Object *ob)
1009 {
1010         Material *ma;
1011         Tex *tex= NULL;
1012         
1013         if(ob==NULL) return NULL;
1014         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return NULL;
1015         
1016         if(ob->type==OB_LAMP) {
1017                 tex= give_current_lamp_texture(ob->data);
1018         } else {
1019                 ma= give_current_material(ob, ob->actcol);
1020                 tex= give_current_material_texture(ma);
1021         }
1022         
1023         return tex;
1024 }
1025
1026 Tex *give_current_lamp_texture(Lamp *la)
1027 {
1028         MTex *mtex= NULL;
1029         Tex *tex= NULL;
1030
1031         if(la) {
1032                 mtex= la->mtex[(int)(la->texact)];
1033                 if(mtex) tex= mtex->tex;
1034         }
1035
1036         return tex;
1037 }
1038
1039 void set_current_lamp_texture(Lamp *la, Tex *newtex)
1040 {
1041         int act= la->texact;
1042
1043         if(la->mtex[act] && la->mtex[act]->tex)
1044                 id_us_min(&la->mtex[act]->tex->id);
1045
1046         if(newtex) {
1047                 if(!la->mtex[act]) {
1048                         la->mtex[act]= add_mtex();
1049                         la->mtex[act]->texco= TEXCO_GLOB;
1050                 }
1051                 
1052                 la->mtex[act]->tex= newtex;
1053                 id_us_plus(&newtex->id);
1054         }
1055         else if(la->mtex[act]) {
1056                 MEM_freeN(la->mtex[act]);
1057                 la->mtex[act]= NULL;
1058         }
1059 }
1060
1061 bNode *give_current_material_texture_node(Material *ma)
1062 {
1063         if(ma && ma->use_nodes && ma->nodetree)
1064                 return nodeGetActiveID(ma->nodetree, ID_TE);
1065         
1066         return NULL;
1067 }
1068
1069 Tex *give_current_material_texture(Material *ma)
1070 {
1071         MTex *mtex= NULL;
1072         Tex *tex= NULL;
1073         bNode *node;
1074         
1075         if(ma && ma->use_nodes && ma->nodetree) {
1076                 /* first check texture, then material, this works together
1077                    with a hack that clears the active ID flag for textures on
1078                    making a material node active */
1079                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1080
1081                 if(node) {
1082                         tex= (Tex *)node->id;
1083                         ma= NULL;
1084                 }
1085                 else {
1086                         node= nodeGetActiveID(ma->nodetree, ID_MA);
1087                         if(node) {
1088                                 ma= (Material*)node->id;
1089                                 if(ma) {
1090                                         mtex= ma->mtex[(int)(ma->texact)];
1091                                         if(mtex) tex= mtex->tex;
1092                                 }
1093                         }
1094                 }
1095                 return tex;
1096         }
1097
1098         if(ma) {
1099                 mtex= ma->mtex[(int)(ma->texact)];
1100                 if(mtex) tex= mtex->tex;
1101         }
1102         
1103         return tex;
1104 }
1105
1106 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1107 {
1108         switch(GS(id->name)) {
1109         case ID_MA:
1110                 *mtex_ar=               ((Material *)id)->mtex;
1111                 if(act) *act=   (((Material *)id)->texact);
1112                 break;
1113         case ID_WO:
1114                 *mtex_ar=               ((World *)id)->mtex;
1115                 if(act) *act=   (((World *)id)->texact);
1116                 break;
1117         case ID_LA:
1118                 *mtex_ar=               ((Lamp *)id)->mtex;
1119                 if(act) *act=   (((Lamp *)id)->texact);
1120                 break;
1121         case ID_PA:
1122                 *mtex_ar=               ((ParticleSettings *)id)->mtex;
1123                 if(act) *act=   (((ParticleSettings *)id)->texact);
1124                 break;
1125         default:
1126                 *mtex_ar = NULL;
1127                 if(act) *act=   0;
1128                 return FALSE;
1129         }
1130
1131         return TRUE;
1132 }
1133
1134 void set_active_mtex(ID *id, short act)
1135 {
1136         if(act<0)                               act= 0;
1137         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
1138
1139         switch(GS(id->name)) {
1140         case ID_MA:
1141                 ((Material *)id)->texact= act;
1142                 break;
1143         case ID_WO:
1144                 ((World *)id)->texact= act;
1145                 break;
1146         case ID_LA:
1147                 ((Lamp *)id)->texact= act;
1148                 break;
1149         case ID_PA:
1150                 ((ParticleSettings *)id)->texact= act;
1151                 break;
1152         }
1153 }
1154
1155 void set_current_material_texture(Material *ma, Tex *newtex)
1156 {
1157         Tex *tex= NULL;
1158         bNode *node;
1159         
1160         if(ma && ma->use_nodes && ma->nodetree) {
1161                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1162
1163                 if(node) {
1164                         tex= (Tex *)node->id;
1165                         id_us_min(&tex->id);
1166                         node->id= &newtex->id;
1167                         id_us_plus(&newtex->id);
1168                         ma= NULL;
1169                 }
1170                 else {
1171                         node= nodeGetActiveID(ma->nodetree, ID_MA);
1172                         if(node)
1173                                 ma= (Material*)node->id;
1174                 }
1175         }
1176         if(ma) {
1177                 int act= (int)ma->texact;
1178
1179                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
1180                 id_us_min(&tex->id);
1181
1182                 if(newtex) {
1183                         if(!ma->mtex[act])
1184                                 ma->mtex[act]= add_mtex();
1185                         
1186                         ma->mtex[act]->tex= newtex;
1187                         id_us_plus(&newtex->id);
1188                 }
1189                 else if(ma->mtex[act]) {
1190                         MEM_freeN(ma->mtex[act]);
1191                         ma->mtex[act]= NULL;
1192                 }
1193         }
1194 }
1195
1196 Tex *give_current_world_texture(World *world)
1197 {
1198         MTex *mtex= NULL;
1199         Tex *tex= NULL;
1200         
1201         if(!world) return NULL;
1202         
1203         mtex= world->mtex[(int)(world->texact)];
1204         if(mtex) tex= mtex->tex;
1205         
1206         return tex;
1207 }
1208
1209 void set_current_world_texture(World *wo, Tex *newtex)
1210 {
1211         int act= wo->texact;
1212
1213         if(wo->mtex[act] && wo->mtex[act]->tex)
1214                 id_us_min(&wo->mtex[act]->tex->id);
1215
1216         if(newtex) {
1217                 if(!wo->mtex[act]) {
1218                         wo->mtex[act]= add_mtex();
1219                         wo->mtex[act]->texco= TEXCO_VIEW;
1220                 }
1221                 
1222                 wo->mtex[act]->tex= newtex;
1223                 id_us_plus(&newtex->id);
1224         }
1225         else if(wo->mtex[act]) {
1226                 MEM_freeN(wo->mtex[act]);
1227                 wo->mtex[act]= NULL;
1228         }
1229 }
1230
1231 Tex *give_current_brush_texture(Brush *br)
1232 {
1233         return br->mtex.tex;
1234 }
1235
1236 void set_current_brush_texture(Brush *br, Tex *newtex)
1237 {
1238         if(br->mtex.tex)
1239                 id_us_min(&br->mtex.tex->id);
1240
1241         if(newtex) {
1242                 br->mtex.tex= newtex;
1243                 id_us_plus(&newtex->id);
1244         }
1245 }
1246
1247 Tex *give_current_particle_texture(ParticleSettings *part)
1248 {
1249         MTex *mtex= NULL;
1250         Tex *tex= NULL;
1251         
1252         if(!part) return NULL;
1253         
1254         mtex= part->mtex[(int)(part->texact)];
1255         if(mtex) tex= mtex->tex;
1256         
1257         return tex;
1258 }
1259
1260 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1261 {
1262         int act= part->texact;
1263
1264         if(part->mtex[act] && part->mtex[act]->tex)
1265                 id_us_min(&part->mtex[act]->tex->id);
1266
1267         if(newtex) {
1268                 if(!part->mtex[act]) {
1269                         part->mtex[act]= add_mtex();
1270                         part->mtex[act]->texco= TEXCO_ORCO;
1271                         part->mtex[act]->blendtype= MTEX_MUL;
1272                 }
1273                 
1274                 part->mtex[act]->tex= newtex;
1275                 id_us_plus(&newtex->id);
1276         }
1277         else if(part->mtex[act]) {
1278                 MEM_freeN(part->mtex[act]);
1279                 part->mtex[act]= NULL;
1280         }
1281 }
1282
1283 /* ------------------------------------------------------------------------- */
1284
1285 EnvMap *BKE_add_envmap(void)
1286 {
1287         EnvMap *env;
1288         
1289         env= MEM_callocN(sizeof(EnvMap), "envmap");
1290         env->type= ENV_CUBE;
1291         env->stype= ENV_ANIM;
1292         env->clipsta= 0.1;
1293         env->clipend= 100.0;
1294         env->cuberes= 600;
1295         env->viewscale = 0.5;
1296         
1297         return env;
1298
1299
1300 /* ------------------------------------------------------------------------- */
1301
1302 EnvMap *BKE_copy_envmap(EnvMap *env)
1303 {
1304         EnvMap *envn;
1305         int a;
1306         
1307         envn= MEM_dupallocN(env);
1308         envn->ok= 0;
1309         for(a=0; a<6; a++) envn->cube[a]= NULL;
1310         if(envn->ima) id_us_plus((ID *)envn->ima);
1311         
1312         return envn;
1313 }
1314
1315 /* ------------------------------------------------------------------------- */
1316
1317 void BKE_free_envmapdata(EnvMap *env)
1318 {
1319         unsigned int part;
1320         
1321         for(part=0; part<6; part++) {
1322                 if(env->cube[part])
1323                         IMB_freeImBuf(env->cube[part]);
1324                 env->cube[part]= NULL;
1325         }
1326         env->ok= 0;
1327 }
1328
1329 /* ------------------------------------------------------------------------- */
1330
1331 void BKE_free_envmap(EnvMap *env)
1332 {
1333         
1334         BKE_free_envmapdata(env);
1335         MEM_freeN(env);
1336         
1337 }
1338
1339 /* ------------------------------------------------------------------------- */
1340
1341 PointDensity *BKE_add_pointdensity(void)
1342 {
1343         PointDensity *pd;
1344         
1345         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1346         pd->flag = 0;
1347         pd->radius = 0.3f;
1348         pd->falloff_type = TEX_PD_FALLOFF_STD;
1349         pd->falloff_softness = 2.0;
1350         pd->source = TEX_PD_PSYS;
1351         pd->point_tree = NULL;
1352         pd->point_data = NULL;
1353         pd->noise_size = 0.5f;
1354         pd->noise_depth = 1;
1355         pd->noise_fac = 1.0f;
1356         pd->noise_influence = TEX_PD_NOISE_STATIC;
1357         pd->coba = add_colorband(1);
1358         pd->speed_scale = 1.0f;
1359         pd->totpoints = 0;
1360         pd->object = NULL;
1361         pd->psys = 0;
1362         pd->psys_cache_space= TEX_PD_WORLDSPACE;
1363         return pd;
1364
1365
1366 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1367 {
1368         PointDensity *pdn;
1369
1370         pdn= MEM_dupallocN(pd);
1371         pdn->point_tree = NULL;
1372         pdn->point_data = NULL;
1373         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1374         
1375         return pdn;
1376 }
1377
1378 void BKE_free_pointdensitydata(PointDensity *pd)
1379 {
1380         if (pd->point_tree) {
1381                 BLI_bvhtree_free(pd->point_tree);
1382                 pd->point_tree = NULL;
1383         }
1384         if (pd->point_data) {
1385                 MEM_freeN(pd->point_data);
1386                 pd->point_data = NULL;
1387         }
1388         if(pd->coba) {
1389                 MEM_freeN(pd->coba);
1390                 pd->coba = NULL;
1391         }
1392 }
1393
1394 void BKE_free_pointdensity(PointDensity *pd)
1395 {
1396         BKE_free_pointdensitydata(pd);
1397         MEM_freeN(pd);
1398 }
1399
1400
1401 void BKE_free_voxeldatadata(struct VoxelData *vd)
1402 {
1403         if (vd->dataset) {
1404                 MEM_freeN(vd->dataset);
1405                 vd->dataset = NULL;
1406         }
1407
1408 }
1409  
1410 void BKE_free_voxeldata(struct VoxelData *vd)
1411 {
1412         BKE_free_voxeldatadata(vd);
1413         MEM_freeN(vd);
1414 }
1415  
1416 struct VoxelData *BKE_add_voxeldata(void)
1417 {
1418         VoxelData *vd;
1419
1420         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1421         vd->dataset = NULL;
1422         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1423         vd->interp_type= TEX_VD_LINEAR;
1424         vd->file_format= TEX_VD_SMOKE;
1425         vd->int_multiplier = 1.0;
1426         vd->extend = TEX_CLIP;
1427         vd->object = NULL;
1428         vd->cachedframe = -1;
1429         vd->ok = 0;
1430         
1431         return vd;
1432  }
1433  
1434 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1435 {
1436         VoxelData *vdn;
1437
1438         vdn= MEM_dupallocN(vd); 
1439         vdn->dataset = NULL;
1440
1441         return vdn;
1442 }
1443
1444
1445 /* ------------------------------------------------------------------------- */
1446 int BKE_texture_dependsOnTime(const struct Tex *texture)
1447 {
1448         if(texture->plugin) {
1449                 // assume all plugins depend on time
1450                 return 1;
1451         } 
1452         else if(        texture->ima && 
1453                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1454                 return 1;
1455         }
1456         else if(texture->adt) {
1457                 // assume anything in adt means the texture is animated
1458                 return 1;
1459         }
1460         return 0;
1461 }
1462
1463 /* ------------------------------------------------------------------------- */