9f19735da5b10f00af39aaf47d9ded38a1dbbd40
[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 if(imtype==R_CINEON){
361                 if (!BLI_testextensie(string, ".cin"))
362                         extension= ".cin";
363         }
364         else if(imtype==R_DPX){
365                 if (!BLI_testextensie(string, ".dpx"))
366                         extension= ".dpx";
367         }
368         else {  /* targa default */
369                 if(!BLI_testextensie(string, ".tga"))
370                         extension= ".tga";
371         }
372
373         strcat(string, extension);
374 }
375
376
377 int BKE_write_ibuf(ImBuf *ibuf, char *name, int imtype, int subimtype, int quality)
378 {
379         int ok;
380         
381         if(imtype==0);
382         else if(imtype== R_IRIS) ibuf->ftype= IMAGIC;
383         else if ((imtype==R_RADHDR)) {
384                 ibuf->ftype= RADHDR;
385         }
386         else if ((imtype==R_PNG)) {
387                 ibuf->ftype= PNG;
388         }
389         else if ((imtype==R_BMP)) {
390                 ibuf->ftype= BMP;
391         }
392         else if ((G.have_libtiff) && (imtype==R_TIFF)) {
393                 ibuf->ftype= TIF;
394         }
395 #ifdef WITH_OPENEXR
396         else if (imtype==R_OPENEXR) {
397                 ibuf->ftype= OPENEXR;
398                 if(subimtype & R_OPENEXR_HALF)
399                         ibuf->ftype |= OPENEXR_HALF;
400                 ibuf->ftype |= (quality & OPENEXR_COMPRESS);
401                 
402                 if(!(subimtype & R_OPENEXR_ZBUF))
403                         ibuf->zbuf_float = NULL;        /* signal for exr saving */
404                 
405         }
406 #endif
407         else if (imtype==R_CINEON) {
408                 ibuf->ftype = CINEON;
409         }
410         else if (imtype==R_DPX) {
411                 ibuf->ftype = DPX;
412         }
413         else if (imtype==R_TARGA) {
414                 ibuf->ftype= TGA;
415         }
416         else if(imtype==R_RAWTGA) {
417                 ibuf->ftype= RAWTGA;
418         }
419         else if(imtype==R_HAMX) {
420                 ibuf->ftype= AN_hamx;
421         }
422         else if ELEM(imtype, R_JPEG90, R_MOVIE) {
423                 if(quality < 10) quality= 90;
424                 ibuf->ftype= JPG|quality;
425         }
426         else ibuf->ftype= TGA;
427         
428         BLI_make_existing_file(name);
429         
430         ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
431         if (ok == 0) {
432                 perror(name);
433         }
434         
435         return(ok);
436 }
437
438
439 void BKE_makepicstring(char *string, int frame)
440 {
441         short i,len;
442         char num[10], *extension;
443
444         if (string==0) return;
445
446         extension= "";
447
448         strcpy(string, G.scene->r.pic);
449         BLI_convertstringcode(string, G.sce, G.scene->r.cfra);
450
451         len= strlen(string);
452                         
453         /* can also: sprintf(num, "%04d", frame); */
454
455         i=4-sprintf(num,"%d",frame);
456         for(;i>0;i--){
457                 string[len]='0';
458                 len++;
459         }
460         string[len]=0;
461         strcat(string,num);
462
463         if(G.scene->r.scemode & R_EXTENSION) 
464                 BKE_add_image_extension(string, G.scene->r.imtype);
465                 
466 }
467
468 /* ******** IMAGE WRAPPING INIT ************* */
469
470 /* used by sequencer, texture */
471 void converttopremul(struct ImBuf *ibuf)
472 {
473         int x, y, val;
474         char *cp;
475         
476         if(ibuf==0) return;
477         if(ibuf->depth==24) {   /* put alpha at 255 */
478
479                 cp= (char *)(ibuf->rect);
480                 for(y=0; y<ibuf->y; y++) {
481                         for(x=0; x<ibuf->x; x++, cp+=4) {
482                                 cp[3]= 255;
483                         }
484                 }
485                 return;
486         }
487         
488         cp= (char *)(ibuf->rect);
489         for(y=0; y<ibuf->y; y++) {
490                 for(x=0; x<ibuf->x; x++, cp+=4) {
491                         if(cp[3]==0) {
492                                 cp[0]= cp[1]= cp[2]= 0;
493                         }
494                         else if(cp[3]!=255) {
495                                 val= cp[3];
496                                 cp[0]= (cp[0]*val)>>8;
497                                 cp[1]= (cp[1]*val)>>8;
498                                 cp[2]= (cp[2]*val)>>8;
499                         }
500                 }
501         }
502 }
503
504 /* used by sequencer, texture */
505 struct anim *openanim(char * name, int flags)
506 {
507         struct anim * anim;
508         struct ImBuf * ibuf;
509         
510         anim = IMB_open_anim(name, flags);
511         if (anim == 0) return(0);
512
513         
514         ibuf = IMB_anim_absolute(anim, 0);
515         if (ibuf == NULL) {
516                 printf("not an anim; %s\n", name);
517                 IMB_free_anim(anim);
518                 return(0);
519         }
520         IMB_freeImBuf(ibuf);
521         
522         return(anim);
523 }
524
525
526 /*
527 load_image handles reading the image from disk or from the packedfile.
528 */
529
530 void load_image(Image * ima, int flags, char *relabase, int framenum)
531 {
532         char name[FILE_MAXDIR + FILE_MAXFILE];
533
534         if(ima->id.lib) relabase= ima->id.lib->filename;
535         
536         if (ima->ibuf == NULL) {
537
538                 // is there a PackedFile with this image ?;
539                 if (ima->packedfile) {
540                         ima->ibuf = IMB_ibImageFromMemory((int *) ima->packedfile->data, ima->packedfile->size, flags);
541                 } else {
542                         strcpy(name, ima->name);
543                         BLI_convertstringcode(name, relabase, framenum);
544
545                         ima->ibuf = IMB_loadiffname(name , flags);
546                 }
547                 // check if the image is a font image...
548                 // printf("Checking for font\n");
549
550                 if (ima->ibuf) {
551                         detectBitmapFont(ima->ibuf);
552                 }
553         }
554 }
555
556
557 static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
558 {
559         struct ImBuf * tbuf1, * tbuf2;
560         
561         if (ibuf == 0) return;
562         if (ibuf->flags & IB_fields) return;
563         ibuf->flags |= IB_fields;
564         
565         if (ibuf->rect) {
566                 /* make copies */
567                 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
568                 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
569                 
570                 ibuf->x *= 2;
571                 
572                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
573                 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
574                 
575                 ibuf->x /= 2;
576                 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
577                 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
578                 
579                 IMB_freeImBuf(tbuf1);
580                 IMB_freeImBuf(tbuf2);
581         }
582         ibuf->y /= 2;
583 }
584
585 static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
586 {
587         struct ImBuf * tbuf1, * tbuf2;
588         
589         if (ibuf == 0) return;
590         if (ibuf->flags & IB_fields) return;
591         ibuf->flags |= IB_fields;
592         
593         if (ibuf->rect) {
594                 /* make copies */
595                 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
596                 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
597                 
598                 ibuf->x *= 2;
599                 
600                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
601                 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
602                 
603                 ibuf->x /= 2;
604                 IMB_rectcpy(ibuf, tbuf2, 0, 0, 0, 0, tbuf2->x, tbuf2->y);
605                 IMB_rectcpy(ibuf, tbuf1, 0, tbuf2->y, 0, 0, tbuf1->x, tbuf1->y);
606                 
607                 IMB_freeImBuf(tbuf1);
608                 IMB_freeImBuf(tbuf2);
609         }
610         ibuf->y /= 2;
611 }
612
613 /* important note: all calls here and calls inside can NOT use threadsafe malloc! */
614 /* this entire function is mutex'ed with the same lock as for mallocs */
615 void ima_ibuf_is_nul(Tex *tex, Image *ima)
616 {
617         void (*de_interlacefunc)(struct ImBuf *ibuf);
618         int a, fra, dur;
619         char str[FILE_MAXDIR+FILE_MAXFILE], *cp;
620         
621         if(ima==0) return;
622         
623         strcpy(str, ima->name);
624         if(ima->id.lib)
625                 BLI_convertstringcode(str, ima->id.lib->filename, G.scene->r.cfra);
626         else
627                 BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
628         
629         if(tex->imaflag & TEX_STD_FIELD) de_interlacefunc= de_interlace_st;
630         else de_interlacefunc= de_interlace_ng;
631         
632         if(tex->imaflag & TEX_ANIM5) {
633                 
634                 if(ima->anim==0) ima->anim = openanim(str, IB_cmap | IB_rect);
635                 if (ima->anim) {
636                         dur = IMB_anim_get_duration(ima->anim);
637                         
638                         fra= ima->lastframe-1;
639                         
640                         if(fra<0) fra = 0;
641                         if(fra>(dur-1)) fra= dur-1;
642                         ima->ibuf = IMB_anim_absolute(ima->anim, fra);
643                         
644                         /* patch for textbutton with name ima (B_NAMEIMA) */
645                         if(ima->ibuf) {
646                                 strcpy(ima->ibuf->name, ima->name);
647                                 if (tex->imaflag & TEX_FIELDS) de_interlacefunc(ima->ibuf);
648                         }
649                 }
650                 
651         } else {
652                 // create a packedfile for this image when autopack is on
653                 // for performance (IMB_loadiffname uses mmap) we don't do this by default
654                 if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK)) {
655                         ima->packedfile = newPackedFile(str);
656                 }
657                 
658                 load_image(ima, IB_rect, G.sce, G.scene->r.cfra);
659                 
660                 if (tex->imaflag & TEX_FIELDS) de_interlacefunc(ima->ibuf);
661         }
662         
663         if(ima->ibuf) {
664                 
665                 /* stringcodes also in ibuf. ibuf->name is used as 'undo' (buttons.c) */
666                 strcpy(ima->ibuf->name, ima->name);
667                 
668                 if(ima->ibuf->cmap) {
669                         
670                         if(tex->imaflag & TEX_ANIM5) {
671                                 
672                                 if(tex->imaflag & TEX_MORKPATCH) {
673                                         /**** PATCH TO SET COLOR 2 RIGHT (neogeo..) */
674                                         if(ima->ibuf->maxcol > 4) {
675                                                 cp= (char *)(ima->ibuf->cmap+2);
676                                                 cp[0]= 0x80;
677                                         }
678                                 }
679                                 
680                                 IMB_applycmap(ima->ibuf);
681                                 IMB_convert_rgba_to_abgr(ima->ibuf);
682                                 
683                         }
684                         
685                         converttopremul(ima->ibuf);
686                 }
687                 
688                 if(tex->imaflag & TEX_ANTISCALE) {
689                         IMB_clever_double(ima->ibuf);
690                         IMB_antialias(ima->ibuf);
691                 }
692                 else if(tex->imaflag & TEX_ANTIALI) IMB_antialias(ima->ibuf);
693         }
694         
695         if(ima->ibuf)
696                 ima->lastused = clock() / CLOCKS_PER_SEC;
697         else
698                 ima->ok= 0;
699         
700         for(a=0; a<BLI_ARRAY_NELEMS(ima->mipmap); a++) {
701                 if(ima->mipmap[a]) IMB_freeImBuf(ima->mipmap[a]);
702                 ima->mipmap[a]= 0;
703         }
704
705 }
706
707
708