2cafc4e94ce0d2ca5cb44651cebd8453051fd0a5
[blender.git] / source / blender / blenkernel / intern / image.c
1 /*  image.c        
2  * 
3  * $Id$
4  *
5  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version. The Blender
11  * Foundation also sells licenses for use in proprietary software under
12  * the Blender License.  See http://www.blender.org/BL/ for information
13  * about this.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  *
24  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
25  * All rights reserved.
26  *
27  * The Original Code is: all of this file.
28  *
29  * Contributor(s): none yet.
30  *
31  * ***** END GPL/BL DUAL LICENSE BLOCK *****
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <math.h>
38 #ifndef WIN32 
39 #include <unistd.h>
40 #else
41 #include <io.h>
42 #endif
43
44 #include <time.h>
45
46 #include "MEM_guardedalloc.h"
47
48 #include "IMB_imbuf_types.h"
49 #include "IMB_imbuf.h"
50
51 #include "DNA_image_types.h"
52 #include "DNA_texture_types.h"
53 #include "DNA_packedFile_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_userdef_types.h"
56
57 #include "BLI_blenlib.h"
58 #include "BLI_arithb.h"
59
60 #include "BKE_bmfont.h"
61 #include "BKE_packedFile.h"
62 #include "BKE_library.h"
63 #include "BKE_global.h"
64 #include "BKE_main.h"
65 #include "BKE_icons.h"
66 #include "BKE_image.h"
67 #include "BKE_scene.h"
68 #include "BKE_texture.h"
69 #include "BKE_utildefines.h"
70
71 #include "PIL_time.h"
72
73 /* bad level; call to free_realtime_image */
74 #include "BKE_bad_level_calls.h"        
75
76 void free_image_buffers(Image *ima)
77 {
78         int a;
79
80         if(ima->ibuf) {
81                 if (ima->ibuf->userdata) {
82                         MEM_freeN(ima->ibuf->userdata);
83                         ima->ibuf->userdata = NULL;
84                 }
85                 IMB_freeImBuf(ima->ibuf);
86                 ima->ibuf= NULL;
87         }
88         if(ima->anim) IMB_free_anim(ima->anim);
89         ima->anim= NULL;
90         
91         for(a=0; a<BLI_ARRAY_NELEMS(ima->mipmap); a++) {
92                 if(ima->mipmap[a]) IMB_freeImBuf(ima->mipmap[a]);
93                 ima->mipmap[a]= NULL;
94         }
95         
96         free_realtime_image(ima);
97 }
98
99
100 void free_image(Image *ima)
101 {
102
103         free_image_buffers(ima);
104         if (ima->packedfile) {
105                 freePackedFile(ima->packedfile);
106                 ima->packedfile = NULL;
107         }
108         BKE_icon_delete(&ima->id);
109         ima->id.icon_id = 0;
110 }
111
112
113 Image *add_image(char *name)
114 {
115         Image *ima;
116         int file, len;
117         char *libname, str[256], strtest[256];
118         
119         strcpy(str, name);
120         BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
121         
122         file= open(str, O_BINARY|O_RDONLY);
123         if(file== -1) return 0;
124         close(file);
125         
126         /* first search an identical image */
127         ima= G.main->image.first;
128         while(ima) {
129                 strcpy(strtest, ima->name);
130                 BLI_convertstringcode(strtest, G.sce, G.scene->r.cfra);
131                 if( strcmp(strtest, str)==0 ) {
132                         if(ima->anim==0 || ima->id.us==0) {
133                                 strcpy(ima->name, name);        /* for stringcode */
134                                 ima->id.us++;
135                                 ima->ok= 1;
136                                 return ima;
137                         }
138                 }
139                 ima= ima->id.next;
140         }
141
142         len= strlen(name);
143         
144         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
145         libname= name+len;
146         
147         ima= alloc_libblock(&G.main->image, ID_IM, libname);
148         strcpy(ima->name, name);
149         ima->ok= 1;
150         
151         ima->xrep= ima->yrep= 1;
152         
153         return ima;
154 }
155
156 Image *new_image(int width, int height, char *name, short uvtestgrid)
157 {
158         Image *ima;
159                         
160         ima = alloc_libblock(&G.main->image, ID_IM, name);
161
162         if (ima)
163         {
164                 ImBuf *ibuf;
165                 unsigned char *rect;
166                 int x, y;
167                 float h=0.0, hoffs=0.0, s=0.9, v=0.6, r, g, b;
168
169                 strcpy(ima->name, "Untitled");
170
171                 ibuf= IMB_allocImBuf(width, height, 24, IB_rect, 0);
172                 strcpy(ibuf->name, "Untitled");
173                 ima->ibuf= ibuf;
174
175                 rect= (unsigned char*)ibuf->rect;
176                 
177                 if (uvtestgrid) {
178                         for(y=0; y<ibuf->y; y++) {
179                                 if (y % 20 == 0) hoffs += 0.125;
180                                 if (y % 160 == 0) hoffs = 0.0;
181                                 
182                                 for(x=0; x<ibuf->x; x++, rect+=4) {
183                                         if (x % 20 == 0) h += 0.125;
184                                         if (x % 160 == 0) h = 0.0;
185                                                                 
186                                         hsv_to_rgb(fabs(h-hoffs), s, v, &r, &g, &b);
187                                         
188                                         rect[0]= (char)(r * 255.0);
189                                         rect[1]= (char)(g * 255.0);
190                                         rect[2]= (char)(b * 255.0);
191                                         rect[3]= 255;
192                                 }
193                         }
194                 } else {        /* blank image */
195                         for(y=0; y<ibuf->y; y++) {
196                                 for(x=0; x<ibuf->x; x++, rect+=4) {
197                                         rect[0]= rect[1]= rect[2]= 0;
198                                         rect[3]= 255;
199                                 }
200                         }
201                 }
202
203                 ima->ok= 1;
204         }
205
206         return ima;
207 }
208
209 void tag_image_time(Image *ima)
210 {
211         if (ima)
212                 ima->lastused = (int)PIL_check_seconds_timer();
213 }
214
215 void tag_all_images_time() {
216         Image *ima;
217         int ctime = (int)PIL_check_seconds_timer();
218
219         ima= G.main->image.first;
220         while(ima) {
221                 if(ima->bindcode || ima->repbind || ima->ibuf) {
222                         ima->lastused = ctime;
223                 }
224         }
225 }
226
227 void free_old_images()
228 {
229         Image *ima;
230         static int lasttime = 0;
231         int ctime = (int)PIL_check_seconds_timer();
232         
233         /* 
234            Run garbage collector once for every collecting period of time 
235            if textimeout is 0, that's the option to NOT run the collector
236         */
237         if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
238                 return;
239
240         lasttime = ctime;
241
242         ima= G.main->image.first;
243         while(ima) {
244                 if((ima->flag & IMA_NOCOLLECT)==0 && ctime - ima->lastused > U.textimeout) {
245                         /*
246                            If it's in GL memory, deallocate and set time tag to current time
247                            This gives textures a "second chance" to be used before dying.
248                         */
249                         if(ima->bindcode || ima->repbind) {
250                                 free_realtime_image(ima);
251                                 ima->lastused = ctime;
252                         }
253                         /* Otherwise, just kill the buffers */
254                         else if (ima->ibuf) {
255                                 free_image_buffers(ima);
256                         }
257                 }
258                 ima = ima->id.next;
259         }
260 }
261
262 void free_unused_animimages()
263 {
264         Image *ima, *nima;
265
266         ima= G.main->image.first;
267         while(ima) {
268                 nima= ima->id.next;
269                 if(ima->id.us==0) {
270                         if(ima->flag & IMA_FROMANIM) free_libblock(&G.main->image, ima);
271                 }
272                 ima= nima;
273         }
274 }
275
276 void free_all_imagetextures(void)
277 {
278         Tex *tex;
279         Image *ima;
280         unsigned int totsize= 0;
281         
282         for(ima= G.main->image.first; ima; ima= ima->id.next)
283                 ima->id.flag &= ~LIB_DOIT;
284         
285         for(tex= G.main->tex.first; tex; tex= tex->id.next)
286                 if(tex->ima)
287                         tex->ima->id.flag |= LIB_DOIT;
288         
289         for(ima= G.main->image.first; ima; ima= ima->id.next) {
290                 if(ima->ibuf && (ima->id.flag & LIB_DOIT)) {
291                         if(ima->mipmap[0]) 
292                                 totsize+= 1.33*ima->ibuf->x*ima->ibuf->y*4;
293                         else
294                                 totsize+= ima->ibuf->x*ima->ibuf->y*4;
295                         free_image_buffers(ima);
296                 }
297         }
298         printf("freed total %d MB\n", totsize/(1024*1024));
299 }
300
301 /* *********** READ AND WRITE ************** */
302
303 int BKE_imtype_is_movie(int imtype)
304 {
305         switch(imtype) {
306         case R_MOVIE:
307         case R_AVIRAW:
308         case R_AVIJPEG:
309         case R_AVICODEC:
310         case R_QUICKTIME:
311         case R_FFMPEG:
312         case R_FRAMESERVER:
313                         return 1;
314         }
315         return 0;
316 }
317
318 void BKE_add_image_extension(char *string, int imtype)
319 {
320         char *extension="";
321         
322         if(G.scene->r.imtype== R_IRIS) {
323                 if(!BLI_testextensie(string, ".rgb"))
324                         extension= ".rgb";
325         }
326         else if(imtype==R_IRIZ) {
327                 if(!BLI_testextensie(string, ".rgb"))
328                         extension= ".rgb";
329         }
330         else if(imtype==R_RADHDR) {
331                 if(!BLI_testextensie(string, ".hdr"))
332                         extension= ".hdr";
333         }
334         else if(imtype==R_PNG) {
335                 if(!BLI_testextensie(string, ".png"))
336                         extension= ".png";
337         }
338         else if(imtype==R_RAWTGA) {
339                 if(!BLI_testextensie(string, ".tga"))
340                         extension= ".tga";
341         }
342         else if(ELEM5(imtype, R_MOVIE, R_AVICODEC, R_AVIRAW, R_AVIJPEG, R_JPEG90)) {
343                 if(!BLI_testextensie(string, ".jpg"))
344                         extension= ".jpg";
345         }
346         else if(imtype==R_BMP) {
347                 if(!BLI_testextensie(string, ".bmp"))
348                         extension= ".bmp";
349         }
350         else if(G.have_libtiff && (imtype==R_TIFF)) {
351                 if(!BLI_testextensie(string, ".tif"))
352                         extension= ".tif";
353         }
354 #ifdef WITH_OPENEXR
355         else if(imtype==R_OPENEXR) {
356                 if(!BLI_testextensie(string, ".exr"))
357                         extension= ".exr";
358         }
359 #endif
360         else {  /* targa default */
361                 if(!BLI_testextensie(string, ".tga"))
362                         extension= ".tga";
363         }
364
365         strcat(string, extension);
366 }
367
368
369 int BKE_write_ibuf(ImBuf *ibuf, char *name, int imtype, int subimtype, int quality)
370 {
371         int ok;
372         
373         if(imtype==0);
374         else if(imtype== R_IRIS) ibuf->ftype= IMAGIC;
375         else if ((imtype==R_RADHDR)) {
376                 ibuf->ftype= RADHDR;
377         }
378         else if ((imtype==R_PNG)) {
379                 ibuf->ftype= PNG;
380         }
381         else if ((imtype==R_BMP)) {
382                 ibuf->ftype= BMP;
383         }
384         else if ((G.have_libtiff) && (imtype==R_TIFF)) {
385                 ibuf->ftype= TIF;
386         }
387 #ifdef WITH_OPENEXR
388         else if (imtype==R_OPENEXR) {
389                 ibuf->ftype= OPENEXR;
390                 if(subimtype & R_OPENEXR_HALF)
391                         ibuf->ftype |= OPENEXR_HALF;
392                 ibuf->ftype |= (quality & OPENEXR_COMPRESS);
393                 
394                 if(!(subimtype & R_OPENEXR_ZBUF))
395                         ibuf->zbuf_float = NULL;        /* signal for exr saving */
396                 
397         }
398 #endif
399         else if (imtype==R_TARGA) {
400                 ibuf->ftype= TGA;
401         }
402         else if(imtype==R_RAWTGA) {
403                 ibuf->ftype= RAWTGA;
404         }
405         else if(imtype==R_HAMX) {
406                 ibuf->ftype= AN_hamx;
407         }
408         else if ELEM(imtype, R_JPEG90, R_MOVIE) {
409                 if(quality < 10) quality= 90;
410                 ibuf->ftype= JPG|quality;
411         }
412         else ibuf->ftype= TGA;
413         
414         BLI_make_existing_file(name);
415         
416         ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
417         if (ok == 0) {
418                 perror(name);
419         }
420         
421         return(ok);
422 }
423
424
425 void BKE_makepicstring(char *string, int frame)
426 {
427         short i,len;
428         char num[10], *extension;
429
430         if (string==0) return;
431
432         extension= "";
433
434         strcpy(string, G.scene->r.pic);
435         BLI_convertstringcode(string, G.sce, G.scene->r.cfra);
436
437         len= strlen(string);
438                         
439         /* can also: sprintf(num, "%04d", frame); */
440
441         i=4-sprintf(num,"%d",frame);
442         for(;i>0;i--){
443                 string[len]='0';
444                 len++;
445         }
446         string[len]=0;
447         strcat(string,num);
448
449         if(G.scene->r.scemode & R_EXTENSION) 
450                 BKE_add_image_extension(string, G.scene->r.imtype);
451                 
452 }
453
454 /* ******** IMAGE WRAPPING INIT ************* */
455
456 /* used by sequencer, texture */
457 void converttopremul(struct ImBuf *ibuf)
458 {
459         int x, y, val;
460         char *cp;
461         
462         if(ibuf==0) return;
463         if(ibuf->depth==24) {   /* put alpha at 255 */
464
465                 cp= (char *)(ibuf->rect);
466                 for(y=0; y<ibuf->y; y++) {
467                         for(x=0; x<ibuf->x; x++, cp+=4) {
468                                 cp[3]= 255;
469                         }
470                 }
471                 return;
472         }
473         
474         cp= (char *)(ibuf->rect);
475         for(y=0; y<ibuf->y; y++) {
476                 for(x=0; x<ibuf->x; x++, cp+=4) {
477                         if(cp[3]==0) {
478                                 cp[0]= cp[1]= cp[2]= 0;
479                         }
480                         else if(cp[3]!=255) {
481                                 val= cp[3];
482                                 cp[0]= (cp[0]*val)>>8;
483                                 cp[1]= (cp[1]*val)>>8;
484                                 cp[2]= (cp[2]*val)>>8;
485                         }
486                 }
487         }
488 }
489
490 /* used by sequencer, texture */
491 struct anim *openanim(char * name, int flags)
492 {
493         struct anim * anim;
494         struct ImBuf * ibuf;
495         
496         anim = IMB_open_anim(name, flags);
497         if (anim == 0) return(0);
498
499         
500         ibuf = IMB_anim_absolute(anim, 0);
501         if (ibuf == NULL) {
502                 printf("not an anim; %s\n", name);
503                 IMB_free_anim(anim);
504                 return(0);
505         }
506         IMB_freeImBuf(ibuf);
507         
508         return(anim);
509 }
510
511
512 /*
513 load_image handles reading the image from disk or from the packedfile.
514 */
515
516 void load_image(Image * ima, int flags, char *relabase, int framenum)
517 {
518         char name[FILE_MAXDIR + FILE_MAXFILE];
519
520         if(ima->id.lib) relabase= ima->id.lib->filename;
521         
522         if (ima->ibuf == NULL) {
523
524                 // is there a PackedFile with this image ?;
525                 if (ima->packedfile) {
526                         ima->ibuf = IMB_ibImageFromMemory((int *) ima->packedfile->data, ima->packedfile->size, flags);
527                 } else {
528                         strcpy(name, ima->name);
529                         BLI_convertstringcode(name, relabase, framenum);
530
531                         ima->ibuf = IMB_loadiffname(name , flags);
532                 }
533                 // check if the image is a font image...
534                 // printf("Checking for font\n");
535
536                 if (ima->ibuf) {
537                         detectBitmapFont(ima->ibuf);
538                 }
539         }
540 }
541
542
543 static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
544 {
545         struct ImBuf * tbuf1, * tbuf2;
546         
547         if (ibuf == 0) return;
548         if (ibuf->flags & IB_fields) return;
549         ibuf->flags |= IB_fields;
550         
551         if (ibuf->rect) {
552                 /* make copies */
553                 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
554                 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
555                 
556                 ibuf->x *= 2;
557                 
558                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
559                 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
560                 
561                 ibuf->x /= 2;
562                 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
563                 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
564                 
565                 IMB_freeImBuf(tbuf1);
566                 IMB_freeImBuf(tbuf2);
567         }
568         ibuf->y /= 2;
569 }
570
571 static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
572 {
573         struct ImBuf * tbuf1, * tbuf2;
574         
575         if (ibuf == 0) return;
576         if (ibuf->flags & IB_fields) return;
577         ibuf->flags |= IB_fields;
578         
579         if (ibuf->rect) {
580                 /* make copies */
581                 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
582                 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
583                 
584                 ibuf->x *= 2;
585                 
586                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
587                 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
588                 
589                 ibuf->x /= 2;
590                 IMB_rectcpy(ibuf, tbuf2, 0, 0, 0, 0, tbuf2->x, tbuf2->y);
591                 IMB_rectcpy(ibuf, tbuf1, 0, tbuf2->y, 0, 0, tbuf1->x, tbuf1->y);
592                 
593                 IMB_freeImBuf(tbuf1);
594                 IMB_freeImBuf(tbuf2);
595         }
596         ibuf->y /= 2;
597 }
598
599 /* important note: all calls here and calls inside can NOT use threadsafe malloc! */
600 /* this entire function is mutex'ed with the same lock as for mallocs */
601 void ima_ibuf_is_nul(Tex *tex, Image *ima)
602 {
603         void (*de_interlacefunc)(struct ImBuf *ibuf);
604         int a, fra, dur;
605         char str[FILE_MAXDIR+FILE_MAXFILE], *cp;
606         
607         if(ima==0) return;
608         
609         strcpy(str, ima->name);
610         if(ima->id.lib)
611                 BLI_convertstringcode(str, ima->id.lib->filename, G.scene->r.cfra);
612         else
613                 BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
614         
615         if(tex->imaflag & TEX_STD_FIELD) de_interlacefunc= de_interlace_st;
616         else de_interlacefunc= de_interlace_ng;
617         
618         if(tex->imaflag & TEX_ANIM5) {
619                 
620                 if(ima->anim==0) ima->anim = openanim(str, IB_cmap | IB_rect);
621                 if (ima->anim) {
622                         dur = IMB_anim_get_duration(ima->anim);
623                         
624                         fra= ima->lastframe-1;
625                         
626                         if(fra<0) fra = 0;
627                         if(fra>(dur-1)) fra= dur-1;
628                         ima->ibuf = IMB_anim_absolute(ima->anim, fra);
629                         
630                         /* patch for textbutton with name ima (B_NAMEIMA) */
631                         if(ima->ibuf) {
632                                 strcpy(ima->ibuf->name, ima->name);
633                                 if (tex->imaflag & TEX_FIELDS) de_interlacefunc(ima->ibuf);
634                         }
635                 }
636                 
637         } else {
638                 // create a packedfile for this image when autopack is on
639                 // for performance (IMB_loadiffname uses mmap) we don't do this by default
640                 if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK)) {
641                         ima->packedfile = newPackedFile(str);
642                 }
643                 
644                 load_image(ima, IB_rect, G.sce, G.scene->r.cfra);
645                 
646                 if (tex->imaflag & TEX_FIELDS) de_interlacefunc(ima->ibuf);
647         }
648         
649         if(ima->ibuf) {
650                 
651                 /* stringcodes also in ibuf. ibuf->name is used as 'undo' (buttons.c) */
652                 strcpy(ima->ibuf->name, ima->name);
653                 
654                 if(ima->ibuf->cmap) {
655                         
656                         if(tex->imaflag & TEX_ANIM5) {
657                                 
658                                 if(tex->imaflag & TEX_MORKPATCH) {
659                                         /**** PATCH TO SET COLOR 2 RIGHT (neogeo..) */
660                                         if(ima->ibuf->maxcol > 4) {
661                                                 cp= (char *)(ima->ibuf->cmap+2);
662                                                 cp[0]= 0x80;
663                                         }
664                                 }
665                                 
666                                 IMB_applycmap(ima->ibuf);
667                                 IMB_convert_rgba_to_abgr(ima->ibuf);
668                                 
669                         }
670                         
671                         converttopremul(ima->ibuf);
672                 }
673                 
674                 if(tex->imaflag & TEX_ANTISCALE) {
675                         IMB_clever_double(ima->ibuf);
676                         IMB_antialias(ima->ibuf);
677                 }
678                 else if(tex->imaflag & TEX_ANTIALI) IMB_antialias(ima->ibuf);
679         }
680         
681         if(ima->ibuf)
682                 ima->lastused = clock() / CLOCKS_PER_SEC;
683         else
684                 ima->ok= 0;
685         
686         for(a=0; a<BLI_ARRAY_NELEMS(ima->mipmap); a++) {
687                 if(ima->mipmap[a]) IMB_freeImBuf(ima->mipmap[a]);
688                 ima->mipmap[a]= 0;
689         }
690
691 }
692
693
694