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