c09f68643ce848e311fdef65b238b06d041e1c6c
[blender-staging.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_userdef_types.h"
55
56 #include "BLI_blenlib.h"
57 #include "BLI_arithb.h"
58
59 #include "BKE_bmfont.h"
60 #include "BKE_packedFile.h"
61 #include "BKE_library.h"
62 #include "BKE_global.h"
63 #include "BKE_main.h"
64 #include "BKE_image.h"
65 #include "BKE_scene.h"
66 #include "BKE_texture.h"
67 #include "BKE_utildefines.h"
68
69 #include "PIL_time.h"
70
71 /* bad level; call to free_realtime_image */
72 #include "BKE_bad_level_calls.h"        
73
74 void free_image_buffers(Image *ima)
75 {
76         int a;
77
78         if(ima->ibuf) {
79                 if (ima->ibuf->userdata) {
80                         MEM_freeN(ima->ibuf->userdata);
81                         ima->ibuf->userdata = 0;
82                 }
83                 IMB_freeImBuf(ima->ibuf);
84                 ima->ibuf= 0;
85         }
86         if(ima->anim) IMB_free_anim(ima->anim);
87         ima->anim= 0;
88         
89         for(a=0; a<BLI_ARRAY_NELEMS(ima->mipmap); a++) {
90                 if(ima->mipmap[a]) IMB_freeImBuf(ima->mipmap[a]);
91                 ima->mipmap[a]= 0;
92         }
93         
94         free_realtime_image(ima);
95 }
96
97
98 void free_image(Image *ima)
99 {
100
101         free_image_buffers(ima);
102         if (ima->packedfile) {
103                 freePackedFile(ima->packedfile);
104                 ima->packedfile = NULL;
105         }
106 }
107
108
109 Image *add_image(char *name)
110 {
111         Image *ima;
112         int file, len;
113         char *libname, str[256], strtest[256];
114         
115         strcpy(str, name);
116         BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
117         
118         file= open(str, O_BINARY|O_RDONLY);
119         if(file== -1) return 0;
120         close(file);
121         
122         /* first search an identical image */
123         ima= G.main->image.first;
124         while(ima) {
125                 strcpy(strtest, ima->name);
126                 BLI_convertstringcode(strtest, G.sce, G.scene->r.cfra);
127                 if( strcmp(strtest, str)==0 ) {
128                         if(ima->anim==0 || ima->id.us==0) {
129                                 strcpy(ima->name, name);        /* for stringcode */
130                                 ima->id.us++;
131                                 ima->ok= 1;
132                                 return ima;
133                         }
134                 }
135                 ima= ima->id.next;
136         }
137
138         len= strlen(name);
139         
140         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
141         libname= name+len;
142         
143         ima= alloc_libblock(&G.main->image, ID_IM, libname);
144         strcpy(ima->name, name);
145         ima->ok= 1;
146         
147         ima->xrep= ima->yrep= 1;
148         
149         return ima;
150 }
151
152 Image *new_image(int width, int height, char *name, short uvtestgrid)
153 {
154         Image *ima;
155                         
156         ima = alloc_libblock(&G.main->image, ID_IM, name);
157
158         if (ima)
159         {
160                 ImBuf *ibuf;
161                 unsigned char *rect;
162                 int x, y;
163                 float h=0.0, hoffs=0.0, s=0.9, v=0.6, r, g, b;
164
165                 strcpy(ima->name, "Untitled");
166
167                 ibuf= IMB_allocImBuf(width, height, 24, IB_rect, 0);
168                 strcpy(ibuf->name, "Untitled");
169                 ima->ibuf= ibuf;
170
171                 rect= (unsigned char*)ibuf->rect;
172                 
173                 if (uvtestgrid) {
174                         for(y=0; y<ibuf->y; y++) {
175                                 if (y % 20 == 0) hoffs += 0.125;
176                                 if (y % 160 == 0) hoffs = 0.0;
177                                 
178                                 for(x=0; x<ibuf->x; x++, rect+=4) {
179                                         if (x % 20 == 0) h += 0.125;
180                                         if (x % 160 == 0) h = 0.0;
181                                                                 
182                                         hsv_to_rgb(fabs(h-hoffs), s, v, &r, &g, &b);
183                                         
184                                         rect[0]= (char)(r * 255.0);
185                                         rect[1]= (char)(g * 255.0);
186                                         rect[2]= (char)(b * 255.0);
187                                         rect[3]= 255;
188                                 }
189                         }
190                 } else {        /* blank image */
191                         for(y=0; y<ibuf->y; y++) {
192                                 for(x=0; x<ibuf->x; x++, rect+=4) {
193                                         rect[0]= rect[1]= rect[2]= 0;
194                                         rect[3]= 255;
195                                 }
196                         }
197                 }
198
199                 ima->ok= 1;
200         }
201
202         return ima;
203 }
204
205 void tag_image_time(Image *ima)
206 {
207         if (ima)
208                 ima->lastused = (int)PIL_check_seconds_timer();
209 }
210
211 void tag_all_images_time() {
212         Image *ima;
213         int ctime = (int)PIL_check_seconds_timer();
214
215         ima= G.main->image.first;
216         while(ima) {
217                 if(ima->bindcode || ima->repbind || ima->ibuf) {
218                         ima->lastused = ctime;
219                 }
220         }
221 }
222
223 void free_old_images()
224 {
225         Image *ima;
226         static int lasttime = 0;
227         int ctime = (int)PIL_check_seconds_timer();
228         
229         /* 
230            Run garbage collector once for every collecting period of time 
231            if textimeout is 0, that's the option to NOT run the collector
232         */
233         if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
234                 return;
235
236         lasttime = ctime;
237
238         ima= G.main->image.first;
239         while(ima) {
240                 if((ima->flag & IMA_NOCOLLECT)==0 && ctime - ima->lastused > U.textimeout) {
241                         /*
242                            If it's in GL memory, deallocate and set time tag to current time
243                            This gives textures a "second chance" to be used before dying.
244                         */
245                         if(ima->bindcode || ima->repbind) {
246                                 free_realtime_image(ima);
247                                 ima->lastused = ctime;
248                         }
249                         /* Otherwise, just kill the buffers */
250                         else if (ima->ibuf) {
251                                 free_image_buffers(ima);
252                         }
253                 }
254                 ima = ima->id.next;
255         }
256 }
257
258 void free_unused_animimages()
259 {
260         Image *ima, *nima;
261
262         ima= G.main->image.first;
263         while(ima) {
264                 nima= ima->id.next;
265                 if(ima->id.us==0) {
266                         if(ima->flag & IMA_FROMANIM) free_libblock(&G.main->image, ima);
267                 }
268                 ima= nima;
269         }
270 }
271
272
273 /* *********** READ AND WRITE ************** */
274
275 void makepicstring(char *string, int frame)
276 {
277         short i,len;
278         char num[10], *extension;
279
280         if (string==0) return;
281
282         extension= "";
283
284         strcpy(string, G.scene->r.pic);
285         BLI_convertstringcode(string, G.sce, G.scene->r.cfra);
286
287         len= strlen(string);
288                         
289         /* can also: sprintf(num, "%04d", frame); */
290
291         i=4-sprintf(num,"%d",frame);
292         for(;i>0;i--){
293                 string[len]='0';
294                 len++;
295         }
296         string[len]=0;
297         strcat(string,num);
298
299         if(G.scene->r.scemode & R_EXTENSION) 
300                 addImageExtension(string);
301                 
302 }
303
304 void addImageExtension(char *string)
305 {
306         char *extension="";
307
308         if(G.scene->r.imtype== R_IRIS) {
309                 if(!BLI_testextensie(string, ".rgb"))
310                         extension= ".rgb";
311         }
312         else if(G.scene->r.imtype==R_IRIZ) {
313                 if(!BLI_testextensie(string, ".rgb"))
314                         extension= ".rgb";
315         }
316         else if(G.scene->r.imtype==R_RADHDR) {
317                 if(!BLI_testextensie(string, ".hdr"))
318                         extension= ".hdr";
319         }
320         else if(G.scene->r.imtype==R_PNG) {
321                 if(!BLI_testextensie(string, ".png"))
322                         extension= ".png";
323         }
324         else if(G.scene->r.imtype==R_TARGA) {
325                 if(!BLI_testextensie(string, ".tga"))
326                         extension= ".tga";
327         }
328         else if(G.scene->r.imtype==R_RAWTGA) {
329                 if(!BLI_testextensie(string, ".tga"))
330                         extension= ".tga";
331         }
332         else if(G.scene->r.imtype==R_JPEG90) {
333                 if(!BLI_testextensie(string, ".jpg"))
334                         extension= ".jpg";
335         }
336         else if(G.scene->r.imtype==R_BMP) {
337                 if(!BLI_testextensie(string, ".bmp"))
338                         extension= ".bmp";
339         }
340         else if(G.have_libtiff && (G.scene->r.imtype==R_TIFF)) {
341                 extension= ".tif";
342         }
343         
344         strcat(string, extension);
345 }
346
347
348 /* ******** IMAGE WRAPPING INIT ************* */
349
350 /* used by sequencer, texture */
351 void converttopremul(struct ImBuf *ibuf)
352 {
353         int x, y, val;
354         char *cp;
355         
356         if(ibuf==0) return;
357         if(ibuf->depth==24) {   /* put alpha at 255 */
358
359                 cp= (char *)(ibuf->rect);
360                 for(y=0; y<ibuf->y; y++) {
361                         for(x=0; x<ibuf->x; x++, cp+=4) {
362                                 cp[3]= 255;
363                         }
364                 }
365                 return;
366         }
367         
368         cp= (char *)(ibuf->rect);
369         for(y=0; y<ibuf->y; y++) {
370                 for(x=0; x<ibuf->x; x++, cp+=4) {
371                         if(cp[3]==0) {
372                                 cp[0]= cp[1]= cp[2]= 0;
373                         }
374                         else if(cp[3]!=255) {
375                                 val= cp[3];
376                                 cp[0]= (cp[0]*val)>>8;
377                                 cp[1]= (cp[1]*val)>>8;
378                                 cp[2]= (cp[2]*val)>>8;
379                         }
380                 }
381         }
382 }
383
384 /* used by sequencer, texture */
385 struct anim *openanim(char * name, int flags)
386 {
387         struct anim * anim;
388         struct ImBuf * ibuf;
389         
390         anim = IMB_open_anim(name, flags);
391         if (anim == 0) return(0);
392
393         
394         ibuf = IMB_anim_absolute(anim, 0);
395         if (ibuf == 0) {
396                 printf("anim_absolute 0 failed\n");
397                 IMB_free_anim(anim);
398                 return(0);
399         }
400         IMB_freeImBuf(ibuf);
401         
402         return(anim);
403 }
404
405
406 /*
407 load_image handles reading the image from disk or from the packedfile.
408 */
409
410 void load_image(Image * ima, int flags, char *relabase, int framenum)
411 {
412         char name[FILE_MAXDIR + FILE_MAXFILE];
413
414         if (ima->ibuf == NULL) {
415
416                 // is there a PackedFile with this image ?;
417                 if (ima->packedfile) {
418                         ima->ibuf = IMB_ibImageFromMemory((int *) ima->packedfile->data, ima->packedfile->size, flags);
419                 } else {
420                         strcpy(name, ima->name);
421                         BLI_convertstringcode(name, relabase, framenum);
422
423                         ima->ibuf = IMB_loadiffname(name , flags);
424                 }
425                 // check if the image is a font image...
426                 // printf("Checking for font\n");
427
428                 if (ima->ibuf) {
429                         detectBitmapFont(ima->ibuf);
430                 }
431         }
432 }
433
434
435 static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
436 {
437         struct ImBuf * tbuf1, * tbuf2;
438         
439         if (ibuf == 0) return;
440         if (ibuf->flags & IB_fields) return;
441         ibuf->flags |= IB_fields;
442         
443         if (ibuf->rect) {
444                 /* make copies */
445                 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
446                 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
447                 
448                 ibuf->x *= 2;
449                 /* These rectop calls are broken!!! I added a trailing 0 arg... */
450                 IMB_rectop(tbuf1, ibuf, 0, 0, 0, 0, 32767, 32767, IMB_rectcpy, 0);
451                 IMB_rectop(tbuf2, ibuf, 0, 0, tbuf2->x, 0, 32767, 32767, IMB_rectcpy, 0);
452                 
453                 ibuf->x /= 2;
454                 IMB_rectop(ibuf, tbuf1, 0, 0, 0, 0, 32767, 32767, IMB_rectcpy, 0);
455                 IMB_rectop(ibuf, tbuf2, 0, tbuf2->y, 0, 0, 32767, 32767, IMB_rectcpy, 0);
456                 
457                 IMB_freeImBuf(tbuf1);
458                 IMB_freeImBuf(tbuf2);
459         }
460         ibuf->y /= 2;
461 }
462
463 static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
464 {
465         struct ImBuf * tbuf1, * tbuf2;
466         
467         if (ibuf == 0) return;
468         if (ibuf->flags & IB_fields) return;
469         ibuf->flags |= IB_fields;
470         
471         if (ibuf->rect) {
472                 /* make copies */
473                 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
474                 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
475                 
476                 ibuf->x *= 2;
477                 /* These are brolenm as well... */
478                 IMB_rectop(tbuf1, ibuf, 0, 0, 0, 0, 32767, 32767, IMB_rectcpy, 0);
479                 IMB_rectop(tbuf2, ibuf, 0, 0, tbuf2->x, 0, 32767, 32767, IMB_rectcpy, 0);
480                 
481                 ibuf->x /= 2;
482                 IMB_rectop(ibuf, tbuf2, 0, 0, 0, 0, 32767, 32767, IMB_rectcpy, 0);
483                 IMB_rectop(ibuf, tbuf1, 0, tbuf2->y, 0, 0, 32767, 32767, IMB_rectcpy, 0);
484                 
485                 IMB_freeImBuf(tbuf1);
486                 IMB_freeImBuf(tbuf2);
487         }
488         ibuf->y /= 2;
489 }
490
491
492 void ima_ibuf_is_nul(Tex *tex, Image *ima)
493 {
494         void (*de_interlacefunc)(struct ImBuf *ibuf);
495         int a, fra, dur;
496         char str[FILE_MAXDIR+FILE_MAXFILE], *cp;
497         
498         if(ima==0) return;
499         
500         strcpy(str, ima->name);
501         BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
502         
503         if(tex->imaflag & TEX_STD_FIELD) de_interlacefunc= de_interlace_st;
504         else de_interlacefunc= de_interlace_ng;
505         
506         if(tex->imaflag & TEX_ANIM5) {
507                 
508                 if(ima->anim==0) ima->anim = openanim(str, IB_cmap | IB_rect);
509                 if (ima->anim) {
510                         dur = IMB_anim_get_duration(ima->anim);
511                         
512                         ima->lastquality= R.osa;
513                         fra= ima->lastframe-1;
514                         
515                         if(fra<0) fra = 0;
516                         if(fra>(dur-1)) fra= dur-1;
517                         ima->ibuf = IMB_anim_absolute(ima->anim, fra);
518                         
519                         /* patch for textbutton with name ima (B_NAMEIMA) */
520                         if(ima->ibuf) {
521                                 strcpy(ima->ibuf->name, ima->name);
522                                 if (tex->imaflag & TEX_FIELDS) de_interlacefunc(ima->ibuf);
523                         }
524                 }
525                 else printf("Not an anim");
526                 
527         } else {
528                 // create a packedfile for this image when autopack is on
529                 // for performance (IMB_loadiffname uses mmap) we don't do this by default
530                 if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK)) {
531                         ima->packedfile = newPackedFile(str);
532                 }
533                 
534                 load_image(ima, IB_rect, G.sce, G.scene->r.cfra);
535                 
536                 if (tex->imaflag & TEX_FIELDS) de_interlacefunc(ima->ibuf);
537                 
538                 ima->lastquality= R.osa;
539         }
540         
541         if(ima->ibuf) {
542                 
543                 /* stringcodes also in ibuf. ibuf->name is used as 'undo' (buttons.c) */
544                 strcpy(ima->ibuf->name, ima->name);
545                 
546                 if(ima->ibuf->cmap) {
547                         
548                         if(tex->imaflag & TEX_ANIM5) {
549                                 
550                                 if(tex->imaflag & TEX_MORKPATCH) {
551                                         /**** PATCH TO SET COLOR 2 RIGHT (neogeo..) */
552                                         if(ima->ibuf->maxcol > 4) {
553                                                 cp= (char *)(ima->ibuf->cmap+2);
554                                                 cp[0]= 0x80;
555                                         }
556                                 }
557                                 
558                                 IMB_applycmap(ima->ibuf);
559                                 IMB_convert_rgba_to_abgr(ima->ibuf->x*ima->ibuf->y, ima->ibuf->rect);
560                                 
561                         }
562                         
563                         converttopremul(ima->ibuf);
564                 }
565                 
566                 if(R.osa) {
567                         
568                         if(tex->imaflag & TEX_ANTISCALE) {
569                                 IMB_clever_double(ima->ibuf);
570                                 IMB_antialias(ima->ibuf);
571                         }
572                         else if(tex->imaflag & TEX_ANTIALI) IMB_antialias(ima->ibuf);
573                 }
574         }
575         
576         if(ima->ibuf)
577                 ima->lastused = clock() / CLOCKS_PER_SEC;
578         else
579                 ima->ok= 0;
580         
581         for(a=0; a<BLI_ARRAY_NELEMS(ima->mipmap); a++) {
582                 if(ima->mipmap[a]) IMB_freeImBuf(ima->mipmap[a]);
583                 ima->mipmap[a]= 0;
584         }
585
586 }
587
588
589