fix for own error when trying to fix python command line crash,
[blender.git] / source / blender / blenkernel / intern / image.c
1 /*  image.c        
2  * 
3  * $Id$
4  *
5  * ***** BEGIN GPL 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.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22  * All rights reserved.
23  *
24  * The Original Code is: all of this file.
25  *
26  * Contributor(s): Blender Foundation, 2006
27  *
28  * ***** END GPL LICENSE BLOCK *****
29  */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <math.h>
35 #ifndef WIN32 
36 #include <unistd.h>
37 #else
38 #include <io.h>
39 #endif
40
41 #include <time.h>
42
43 #include "MEM_guardedalloc.h"
44
45 #include "IMB_imbuf_types.h"
46 #include "IMB_imbuf.h"
47
48 #ifdef WITH_OPENEXR
49 #include "intern/openexr/openexr_multi.h"
50 #endif
51
52 #include "DNA_image_types.h"
53 #include "DNA_packedFile_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_texture_types.h"
57 #include "DNA_userdef_types.h"
58
59 #include "BLI_arithb.h"
60 #include "BLI_blenlib.h"
61 #include "BLI_threads.h"
62
63 #include "BKE_bmfont.h"
64 #include "BKE_global.h"
65 #include "BKE_icons.h"
66 #include "BKE_image.h"
67 #include "BKE_library.h"
68 #include "BKE_main.h"
69 #include "BKE_packedFile.h"
70 #include "BKE_scene.h"
71 #include "BKE_texture.h"
72 #include "BKE_utildefines.h"
73
74 #include "BIF_editseq.h"
75
76 #include "PIL_time.h"
77
78 #include "RE_pipeline.h"
79
80 /* bad level; call to free_realtime_image */
81 #include "BKE_bad_level_calls.h"        
82
83 /* for stamp drawing to an image */
84 #include "BMF_Api.h"
85
86 #include "blendef.h"
87 #include "BSE_time.h"
88
89 /* max int, to indicate we don't store sequences in ibuf */
90 #define IMA_NO_INDEX    0x7FEFEFEF
91
92 /* quick lookup: supports 1 million frames, thousand passes */
93 #define IMA_MAKE_INDEX(frame, index)    ((frame)<<10)+index
94 #define IMA_INDEX_FRAME(index)                  (index>>10)
95
96 /* ******** IMAGE PROCESSING ************* */
97
98 /* used by sequencer and image premul option - IMA_DO_PREMUL */
99 void converttopremul(struct ImBuf *ibuf)
100 {
101         int x, y;
102         
103         if(ibuf==0) return;
104         if (ibuf->rect) {
105                 int val;
106                 char *cp;
107                 if(ibuf->depth==24) {   /* put alpha at 255 */
108                         cp= (char *)(ibuf->rect);
109                         for(y=0; y<ibuf->y; y++) {
110                                 for(x=0; x<ibuf->x; x++, cp+=4) {
111                                         cp[3]= 255;
112                                 }
113                         }
114                 } else {
115                         cp= (char *)(ibuf->rect);
116                         for(y=0; y<ibuf->y; y++) {
117                                 for(x=0; x<ibuf->x; x++, cp+=4) {
118                                         val= cp[3];
119                                         cp[0]= (cp[0]*val)>>8;
120                                         cp[1]= (cp[1]*val)>>8;
121                                         cp[2]= (cp[2]*val)>>8;
122                                 }
123                         }
124                 }
125         }
126         if (ibuf->rect_float) {
127                 float val;
128                 float *cp;
129                 if(ibuf->depth==24) {   /* put alpha at 1.0 */
130                         cp= ibuf->rect_float;;
131                         for(y=0; y<ibuf->y; y++) {
132                                 for(x=0; x<ibuf->x; x++, cp+=4) {
133                                         cp[3]= 1.0;
134                                 }
135                         }
136                 } else {
137                         cp= ibuf->rect_float;
138                         for(y=0; y<ibuf->y; y++) {
139                                 for(x=0; x<ibuf->x; x++, cp+=4) {
140                                         val= cp[3];
141                                         cp[0]= cp[0]*val;
142                                         cp[1]= cp[1]*val;
143                                         cp[2]= cp[2]*val;
144                                 }
145                         }
146                 }
147         }
148 }
149
150 static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
151 {
152         struct ImBuf * tbuf1, * tbuf2;
153         
154         if (ibuf == 0) return;
155         if (ibuf->flags & IB_fields) return;
156         ibuf->flags |= IB_fields;
157         
158         if (ibuf->rect) {
159                 /* make copies */
160                 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
161                 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
162                 
163                 ibuf->x *= 2;
164                 
165                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
166                 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
167                 
168                 ibuf->x /= 2;
169                 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
170                 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
171                 
172                 IMB_freeImBuf(tbuf1);
173                 IMB_freeImBuf(tbuf2);
174         }
175         ibuf->y /= 2;
176 }
177
178 static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
179 {
180         struct ImBuf * tbuf1, * tbuf2;
181         
182         if (ibuf == 0) return;
183         if (ibuf->flags & IB_fields) return;
184         ibuf->flags |= IB_fields;
185         
186         if (ibuf->rect) {
187                 /* make copies */
188                 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
189                 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
190                 
191                 ibuf->x *= 2;
192                 
193                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
194                 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
195                 
196                 ibuf->x /= 2;
197                 IMB_rectcpy(ibuf, tbuf2, 0, 0, 0, 0, tbuf2->x, tbuf2->y);
198                 IMB_rectcpy(ibuf, tbuf1, 0, tbuf2->y, 0, 0, tbuf1->x, tbuf1->y);
199                 
200                 IMB_freeImBuf(tbuf1);
201                 IMB_freeImBuf(tbuf2);
202         }
203         ibuf->y /= 2;
204 }
205
206 void image_de_interlace(Image *ima, int odd)
207 {
208         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
209         if(ibuf) {
210                 if(odd)
211                         de_interlace_st(ibuf);
212                 else
213                         de_interlace_ng(ibuf);
214         }
215 }
216
217 /* ***************** ALLOC & FREE, DATA MANAGING *************** */
218
219 static void image_free_buffers(Image *ima)
220 {
221         ImBuf *ibuf;
222         
223         while((ibuf = ima->ibufs.first)) {
224                 BLI_remlink(&ima->ibufs, ibuf);
225                 
226                 if (ibuf->userdata) {
227                         MEM_freeN(ibuf->userdata);
228                         ibuf->userdata = NULL;
229                 }
230                 IMB_freeImBuf(ibuf);
231         }
232         
233         if(ima->anim) IMB_free_anim(ima->anim);
234         ima->anim= NULL;
235         
236         if(ima->rr) {
237                 RE_FreeRenderResult(ima->rr);
238                 ima->rr= NULL;
239         }       
240         
241         free_realtime_image(ima);
242         
243         ima->ok= IMA_OK;
244 }
245
246 /* called by library too, do not free ima itself */
247 void free_image(Image *ima)
248 {
249         image_free_buffers(ima);
250         if (ima->packedfile) {
251                 freePackedFile(ima->packedfile);
252                 ima->packedfile = NULL;
253         }
254         BKE_icon_delete(&ima->id);
255         ima->id.icon_id = 0;
256         if (ima->preview) {
257                 BKE_previewimg_free(&ima->preview);
258         }
259 }
260
261 /* only image block itself */
262 static Image *image_alloc(const char *name, short source, short type)
263 {
264         Image *ima;
265         
266         ima= alloc_libblock(&G.main->image, ID_IM, name);
267         if(ima) {
268                 ima->ok= IMA_OK;
269                 
270                 ima->xrep= ima->yrep= 1;
271                 ima->aspx= ima->aspy= 1.0;
272                 ima->gen_x= 256; ima->gen_y= 256;
273                 ima->gen_type= 1;       /* no defines yet? */
274                 
275                 ima->source= source;
276                 ima->type= type;
277         }
278         return ima;
279 }
280
281 /* get the ibuf from an image cache, local use here only */
282 static ImBuf *image_get_ibuf(Image *ima, int index, int frame)
283 {
284         if(index==IMA_NO_INDEX)
285                 return ima->ibufs.first;
286         else {
287                 ImBuf *ibuf;
288                 
289                 index= IMA_MAKE_INDEX(frame, index);
290                 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next)
291                         if(ibuf->index==index)
292                                 return ibuf;
293                 return NULL;
294         }
295 }
296
297 /* no ima->ibuf anymore, but listbase */
298 static void image_remove_ibuf(Image *ima, ImBuf *ibuf)
299 {
300         if(ibuf) {
301                 BLI_remlink(&ima->ibufs, ibuf);
302                 IMB_freeImBuf(ibuf);
303         }
304 }
305
306
307 /* no ima->ibuf anymore, but listbase */
308 static void image_assign_ibuf(Image *ima, ImBuf *ibuf, int index, int frame)
309 {
310         if(ibuf) {
311                 ImBuf *link;
312                 
313                 if(index!=IMA_NO_INDEX)
314                         index= IMA_MAKE_INDEX(frame, index);
315                 
316                 /* insert based on index */
317                 for(link= ima->ibufs.first; link; link= link->next)
318                         if(link->index>=index)
319                                 break;
320                 /* now we don't want copies? */
321                 if(link && ibuf->index==link->index) {
322                         ImBuf *prev= ibuf->prev;
323                         image_remove_ibuf(ima, link);
324                         link= prev;
325                 }
326                 
327                 /* this function accepts link==NULL */
328                 BLI_insertlinkbefore(&ima->ibufs, link, ibuf);
329                 
330                 ibuf->index= index;
331         }
332         
333 }
334
335 /* checks if image was already loaded, then returns same image */
336 /* otherwise creates new. */
337 /* does not load ibuf itself */
338 Image *BKE_add_image_file(const char *name)
339 {
340         Image *ima;
341         int file, len;
342         const char *libname;
343         char str[FILE_MAX], strtest[FILE_MAX];
344         
345         /* escape when name is directory */
346         len= strlen(name);
347         if(len) {
348                 if(name[len-1]=='/' || name[len-1]=='\\')
349                         return NULL;
350         }
351         
352         BLI_strncpy(str, name, sizeof(str));
353         BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
354         
355         /* exists? */
356         file= open(str, O_BINARY|O_RDONLY);
357         if(file== -1) return NULL;
358         close(file);
359         
360         /* first search an identical image */
361         for(ima= G.main->image.first; ima; ima= ima->id.next) {
362                 if(ima->source!=IMA_SRC_VIEWER && ima->source!=IMA_SRC_GENERATED) {
363                         BLI_strncpy(strtest, ima->name, sizeof(ima->name));
364                         BLI_convertstringcode(strtest, G.sce, G.scene->r.cfra);
365                         
366                         if( strcmp(strtest, str)==0 ) {
367                                 if(ima->anim==NULL || ima->id.us==0) {
368                                         BLI_strncpy(ima->name, name, sizeof(ima->name));        /* for stringcode */
369                                         ima->id.us++;                                                                           /* officially should not, it doesn't link here! */
370                                         if(ima->ok==0)
371                                                 ima->ok= IMA_OK;
372                         /* RETURN! */
373                                         return ima;
374                                 }
375                         }
376                 }
377         }
378         /* add new image */
379         
380         /* create a short library name */
381         len= strlen(name);
382         
383         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
384         libname= name+len;
385         
386         ima= image_alloc(libname, IMA_SRC_FILE, IMA_TYPE_IMAGE);
387         BLI_strncpy(ima->name, name, sizeof(ima->name));
388         
389         /* do a wild guess! */
390         if(BLI_testextensie(name, ".avi") || BLI_testextensie(name, ".mov")
391                         || BLI_testextensie(name, ".mpg")  || BLI_testextensie(name, ".mp4"))
392                 ima->source= IMA_SRC_MOVIE;
393         
394         return ima;
395 }
396
397 static ImBuf *add_ibuf_size(int width, int height, char *name, int floatbuf, short uvtestgrid, float color[4])
398 {
399         ImBuf *ibuf;
400         float h=0.0, hoffs=0.0, hue=0.0, s=0.9, v=0.9, r, g, b;
401         unsigned char *rect= NULL;
402         float *rect_float= NULL;
403         int x, y;
404         int checkerwidth=21, dark=1;
405         
406         if (floatbuf) {
407                 ibuf= IMB_allocImBuf(width, height, 24, IB_rectfloat, 0);
408                 rect_float= (float*)ibuf->rect_float;
409         }
410         else {
411                 ibuf= IMB_allocImBuf(width, height, 24, IB_rect, 0);
412                 rect= (unsigned char*)ibuf->rect;
413         }
414         
415         strcpy(ibuf->name, "Untitled");
416         ibuf->userflags |= IB_BITMAPDIRTY;
417         
418         if (uvtestgrid) {
419                 /* these two passes could be combined into one, but it's more readable and 
420                 * easy to tweak like this, speed isn't really that much of an issue in this situation... */
421                 
422                 /* checkers */
423                 for(y=0; y<ibuf->y; y++) {
424                         dark = pow(-1, floor(y / checkerwidth));
425                         
426                         for(x=0; x<ibuf->x; x++) {
427                                 if (x % checkerwidth == 0) dark *= -1;
428                                 
429                                 if (floatbuf) {
430                                         if (dark > 0) {
431                                                 rect_float[0] = rect_float[1] = rect_float[2] = 0.25;
432                                                 rect_float[3] = 1.0;
433                                         } else {
434                                                 rect_float[0] = rect_float[1] = rect_float[2] = 0.58;
435                                                 rect_float[3] = 1.0;
436                                         }
437                                         rect_float+=4;
438                                 }
439                                 else {
440                                         if (dark > 0) {
441                                                 rect[0] = rect[1] = rect[2] = 64;
442                                                 rect[3] = 255;
443                                         } else {
444                                                 rect[0] = rect[1] = rect[2] = 150;
445                                                 rect[3] = 255;
446                                         }
447                                         rect += 4;
448                                 }
449                         }
450                 }
451                 
452                 /* 2nd pass, colored + */
453                 if (floatbuf) rect_float= (float*)ibuf->rect_float;
454                 else rect= (unsigned char*)ibuf->rect;
455                 
456                 for(y=0; y<ibuf->y; y++) {
457                         hoffs = 0.125 * floor(y / checkerwidth);
458                         
459                         for(x=0; x<ibuf->x; x++) {
460                                 h = 0.125 * floor(x / checkerwidth);
461                                 
462                                 if ((fabs((x % checkerwidth) - (checkerwidth / 2)) < 4) &&
463                                         (fabs((y % checkerwidth) - (checkerwidth / 2)) < 4)) {
464                                         
465                                         if ((fabs((x % checkerwidth) - (checkerwidth / 2)) < 1) ||
466                                                 (fabs((y % checkerwidth) - (checkerwidth / 2)) < 1)) {
467                                                 
468                                                 hue = fmod(fabs(h-hoffs), 1.0);
469                                                 hsv_to_rgb(hue, s, v, &r, &g, &b);
470                                                 
471                                                 if (floatbuf) {
472                                                         rect_float[0]= r;
473                                                         rect_float[1]= g;
474                                                         rect_float[2]= b;
475                                                         rect_float[3]= 1.0;
476                                                 }
477                                                 else {
478                                                         rect[0]= (char)(r * 255.0);
479                                                         rect[1]= (char)(g * 255.0);
480                                                         rect[2]= (char)(b * 255.0);
481                                                         rect[3]= 255;
482                                                 }
483                                         }
484                                 }
485
486                                 if (floatbuf)
487                                         rect_float+=4;
488                                 else
489                                         rect+=4;
490                         }
491                 }
492         } else {        /* blank image */
493                 for(y=0; y<ibuf->y; y++) {
494                         for(x=0; x<ibuf->x; x++) {
495                                 if (floatbuf) {
496                                         rect_float[0]= color[0];
497                                         rect_float[1]= color[1];
498                                         rect_float[2]= color[2];
499                                         rect_float[3]= color[3];
500                                         rect_float+=4;
501                                 }
502                                 else {
503                                         rect[0]= (char)(color[0] * 255.0);
504                                         rect[1]= (char)(color[1] * 255.0);
505                                         rect[2]= (char)(color[2] * 255.0);
506                                         rect[3]= (char)(color[3] * 255.0);
507                                         rect+=4;
508                                 }
509                         }
510                 }
511         }
512         return ibuf;
513 }
514
515 /* adds new image block, creates ImBuf and initializes color */
516 Image *BKE_add_image_size(int width, int height, char *name, int floatbuf, short uvtestgrid, float color[4])
517 {
518         Image *ima;
519         
520         /* on save, type is changed to FILE in editsima.c */
521         ima= image_alloc(name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
522         
523         if (ima) {
524                 ImBuf *ibuf;
525                 
526                 BLI_strncpy(ima->name, name, FILE_MAX);
527                 ima->gen_x= width;
528                 ima->gen_y= height;
529                 ima->gen_type= uvtestgrid;
530                 
531                 ibuf= add_ibuf_size(width, height, name, floatbuf, uvtestgrid, color);
532                 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
533                 
534                 ima->ok= IMA_OK_LOADED;
535         }
536
537         return ima;
538 }
539
540 /* packs rect from memory as PNG */
541 void BKE_image_memorypack(Image *ima)
542 {
543         ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
544         
545         if(ibuf==NULL)
546                 return;
547         if (ima->packedfile) {
548                 freePackedFile(ima->packedfile);
549                 ima->packedfile = NULL;
550         }
551         
552         ibuf->ftype= PNG;
553         ibuf->depth= 32;
554         
555         IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem);
556         if(ibuf->encodedbuffer==NULL) {
557                 printf("memory save for pack error\n");
558         }
559         else {
560                 PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
561                 
562                 pf->data = ibuf->encodedbuffer;
563                 pf->size = ibuf->encodedsize;
564                 ima->packedfile= pf;
565                 ibuf->encodedbuffer= NULL;
566                 ibuf->encodedsize= 0;
567                 ibuf->userflags &= ~IB_BITMAPDIRTY;
568                 
569                 if(ima->source==IMA_SRC_GENERATED) {
570                         ima->source= IMA_SRC_FILE;
571                         ima->type= IMA_TYPE_IMAGE;
572                 }
573         }
574 }
575
576 void tag_image_time(Image *ima)
577 {
578         if (ima)
579                 ima->lastused = (int)PIL_check_seconds_timer();
580 }
581
582 void tag_all_images_time() 
583 {
584         Image *ima;
585         int ctime = (int)PIL_check_seconds_timer();
586
587         ima= G.main->image.first;
588         while(ima) {
589                 if(ima->bindcode || ima->repbind || ima->ibufs.first) {
590                         ima->lastused = ctime;
591                 }
592         }
593 }
594
595 void free_old_images()
596 {
597         Image *ima;
598         static int lasttime = 0;
599         int ctime = (int)PIL_check_seconds_timer();
600         
601         /* 
602            Run garbage collector once for every collecting period of time 
603            if textimeout is 0, that's the option to NOT run the collector
604         */
605         if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
606                 return;
607
608         lasttime = ctime;
609
610         ima= G.main->image.first;
611         while(ima) {
612                 if((ima->flag & IMA_NOCOLLECT)==0 && ctime - ima->lastused > U.textimeout) {
613                         /*
614                            If it's in GL memory, deallocate and set time tag to current time
615                            This gives textures a "second chance" to be used before dying.
616                         */
617                         if(ima->bindcode || ima->repbind) {
618                                 free_realtime_image(ima);
619                                 ima->lastused = ctime;
620                         }
621                         /* Otherwise, just kill the buffers */
622                         else if (ima->ibufs.first) {
623                                 image_free_buffers(ima);
624                         }
625                 }
626                 ima = ima->id.next;
627         }
628 }
629
630 static unsigned long image_mem_size(Image *ima)
631 {
632         ImBuf *ibuf, *ibufm;
633         int level;
634         unsigned long size = 0;
635
636         size= 0;
637         for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
638                 if(ibuf->rect) size += MEM_allocN_len(ibuf->rect);
639                 else if(ibuf->rect_float) size += MEM_allocN_len(ibuf->rect_float);
640
641                 for(level=0; level<IB_MIPMAP_LEVELS; level++) {
642                         ibufm= ibuf->mipmap[level];
643                         if(ibufm) {
644                                 if(ibufm->rect) size += MEM_allocN_len(ibufm->rect);
645                                 else if(ibufm->rect_float) size += MEM_allocN_len(ibufm->rect_float);
646                         }
647                 }
648         }
649
650         return size;
651 }
652
653 void BKE_image_print_memlist(void)
654 {
655         Image *ima;
656         unsigned long size, totsize= 0;
657
658         for(ima= G.main->image.first; ima; ima= ima->id.next)
659                 totsize += image_mem_size(ima);
660
661         printf("\ntotal image memory len: %.3lf MB\n", (double)totsize/(double)(1024*1024));
662
663         for(ima= G.main->image.first; ima; ima= ima->id.next) {
664                 size= image_mem_size(ima);
665
666                 if(size)
667                         printf("%s len: %.3f MB\n", ima->id.name+2, (double)size/(double)(1024*1024));
668         }
669 }
670
671 void BKE_image_free_all_textures(void)
672 {
673         Tex *tex;
674         Image *ima;
675         unsigned int totsize= 0;
676         
677         for(ima= G.main->image.first; ima; ima= ima->id.next)
678                 ima->id.flag &= ~LIB_DOIT;
679         
680         for(tex= G.main->tex.first; tex; tex= tex->id.next)
681                 if(tex->ima)
682                         tex->ima->id.flag |= LIB_DOIT;
683         
684         for(ima= G.main->image.first; ima; ima= ima->id.next) {
685                 if(ima->ibufs.first && (ima->id.flag & LIB_DOIT)) {
686                         ImBuf *ibuf;
687                         for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
688                                 if(ibuf->mipmap[0]) 
689                                         totsize+= 1.33*ibuf->x*ibuf->y*4;
690                                 else
691                                         totsize+= ibuf->x*ibuf->y*4;
692                         }
693                         image_free_buffers(ima);
694                 }
695         }
696         /* printf("freed total %d MB\n", totsize/(1024*1024)); */
697 }
698
699 /* except_frame is weak, only works for seqs without offset... */
700 void BKE_image_free_anim_ibufs(Image *ima, int except_frame)
701 {
702         ImBuf *ibuf, *nbuf;
703
704         for(ibuf= ima->ibufs.first; ibuf; ibuf= nbuf) {
705                 nbuf= ibuf->next;
706                 if(ibuf->userflags & IB_BITMAPDIRTY)
707                         continue;
708                 if(ibuf->index==IMA_NO_INDEX)
709                         continue;
710                 if(except_frame!=IMA_INDEX_FRAME(ibuf->index)) {
711                         BLI_remlink(&ima->ibufs, ibuf);
712                         
713                         if (ibuf->userdata) {
714                                 MEM_freeN(ibuf->userdata);
715                                 ibuf->userdata = NULL;
716                         }
717                         IMB_freeImBuf(ibuf);
718                 }                                       
719         }
720 }
721
722 void BKE_image_all_free_anim_ibufs(int cfra)
723 {
724         Image *ima;
725         
726         for(ima= G.main->image.first; ima; ima= ima->id.next)
727                 if(ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE))
728                         BKE_image_free_anim_ibufs(ima, cfra);
729 }
730
731
732 /* *********** READ AND WRITE ************** */
733
734 int BKE_imtype_to_ftype(int imtype)
735 {
736         if(imtype==0)
737                 return TGA;
738         else if(imtype== R_IRIS) 
739                 return IMAGIC;
740         else if (imtype==R_RADHDR)
741                 return RADHDR;
742         else if (imtype==R_PNG)
743                 return PNG;
744 #ifdef WITH_DDS
745         else if (imtype==R_DDS)
746                 return DDS;
747 #endif
748         else if (imtype==R_BMP)
749                 return BMP;
750         else if (imtype==R_TIFF)
751                 return TIF;
752         else if (imtype==R_OPENEXR || imtype==R_MULTILAYER)
753                 return OPENEXR;
754         else if (imtype==R_CINEON)
755                 return CINEON;
756         else if (imtype==R_DPX)
757                 return DPX;
758         else if (imtype==R_TARGA)
759                 return TGA;
760         else if(imtype==R_RAWTGA)
761                 return RAWTGA;
762         else if(imtype==R_HAMX)
763                 return AN_hamx;
764         else
765                 return JPG|90;
766 }
767
768 int BKE_ftype_to_imtype(int ftype)
769 {
770         if(ftype==0)
771                 return TGA;
772         else if(ftype == IMAGIC) 
773                 return R_IRIS;
774         else if (ftype & RADHDR)
775                 return R_RADHDR;
776         else if (ftype & PNG)
777                 return R_PNG;
778 #ifdef WITH_DDS
779         else if (ftype & DDS)
780                 return R_DDS;
781 #endif
782         else if (ftype & BMP)
783                 return R_BMP;
784         else if (ftype & TIF)
785                 return R_TIFF;
786         else if (ftype & OPENEXR)
787                 return R_OPENEXR;
788         else if (ftype & CINEON)
789                 return R_CINEON;
790         else if (ftype & DPX)
791                 return R_DPX;
792         else if (ftype & TGA)
793                 return R_TARGA;
794         else if(ftype & RAWTGA)
795                 return R_RAWTGA;
796         else if(ftype == AN_hamx)
797                 return R_HAMX;
798         else
799                 return R_JPEG90;
800 }
801
802
803 int BKE_imtype_is_movie(int imtype)
804 {
805         switch(imtype) {
806         case R_MOVIE:
807         case R_AVIRAW:
808         case R_AVIJPEG:
809         case R_AVICODEC:
810         case R_QUICKTIME:
811         case R_FFMPEG:
812         case R_FRAMESERVER:
813                         return 1;
814         }
815         return 0;
816 }
817
818 void BKE_add_image_extension(char *string, int imtype)
819 {
820         char *extension="";
821         
822         if(G.scene->r.imtype== R_IRIS) {
823                 if(!BLI_testextensie(string, ".rgb"))
824                         extension= ".rgb";
825         }
826         else if(imtype==R_IRIZ) {
827                 if(!BLI_testextensie(string, ".rgb"))
828                         extension= ".rgb";
829         }
830         else if(imtype==R_RADHDR) {
831                 if(!BLI_testextensie(string, ".hdr"))
832                         extension= ".hdr";
833         }
834         else if(imtype==R_PNG || imtype==R_FFMPEG) {
835                 if(!BLI_testextensie(string, ".png"))
836                         extension= ".png";
837         }
838 #ifdef WITH_DDS
839         else if(imtype==R_DDS) {
840                 if(!BLI_testextensie(string, ".dds"))
841                         extension= ".dds";
842         }
843 #endif
844         else if(imtype==R_RAWTGA) {
845                 if(!BLI_testextensie(string, ".tga"))
846                         extension= ".tga";
847         }
848         else if(ELEM5(imtype, R_MOVIE, R_AVICODEC, R_AVIRAW, R_AVIJPEG, R_JPEG90)) {
849                 if(!( BLI_testextensie(string, ".jpg") || BLI_testextensie(string, ".jpeg")))
850                         extension= ".jpg";
851         }
852         else if(imtype==R_BMP) {
853                 if(!BLI_testextensie(string, ".bmp"))
854                         extension= ".bmp";
855         }
856         else if(G.have_libtiff && (imtype==R_TIFF)) {
857                 if(!BLI_testextensie(string, ".tif"))
858                         extension= ".tif";
859         }
860 #ifdef WITH_OPENEXR
861         else if( ELEM(imtype, R_OPENEXR, R_MULTILAYER)) {
862                 if(!BLI_testextensie(string, ".exr"))
863                         extension= ".exr";
864         }
865 #endif
866         else if(imtype==R_CINEON){
867                 if (!BLI_testextensie(string, ".cin"))
868                         extension= ".cin";
869         }
870         else if(imtype==R_DPX){
871                 if (!BLI_testextensie(string, ".dpx"))
872                         extension= ".dpx";
873         }
874         else {  /* targa default */
875                 if(!BLI_testextensie(string, ".tga"))
876                         extension= ".tga";
877         }
878
879         strcat(string, extension);
880 }
881
882 /* could allow access externally - 512 is for long names, 64 is for id names */
883 typedef struct StampData {
884         char    file[512];
885         char    note[512];
886         char    date[512];
887         char    marker[512];
888         char    time[512];
889         char    frame[512];
890         char    camera[64];
891         char    scene[64];
892         char    strip[64];
893 } StampData;
894
895 static void stampdata(StampData *stamp_data, int do_prefix)
896 {
897         char text[256];
898         
899 #ifndef WIN32
900         struct tm *tl;
901         time_t t;
902 #else
903         char sdate[9];
904 #endif /* WIN32 */
905         
906         if (G.scene->r.stamp & R_STAMP_FILENAME) {
907                 if (G.relbase_valid) {
908                         if (do_prefix)          sprintf(stamp_data->file, "File %s", G.sce);
909                         else                            sprintf(stamp_data->file, "%s", G.sce);
910                 } else {
911                         if (do_prefix)          strcpy(stamp_data->file, "File <untitled>");
912                         else                            strcpy(stamp_data->file, "<untitled>");
913                 }
914                 stamp_data->note[0] = '\0';
915         } else {
916                 stamp_data->file[0] = '\0';
917         }
918         
919         if (G.scene->r.stamp & R_STAMP_NOTE) {
920                 /* Never do prefix for Note */
921                 sprintf(stamp_data->note, "%s", G.scene->r.stamp_udata);
922         } else {
923                 stamp_data->note[0] = '\0';
924         }
925         
926         if (G.scene->r.stamp & R_STAMP_DATE) {
927 #ifdef WIN32
928                 _strdate (sdate);
929                 sprintf (text, "%s", sdate);
930 #else
931                 t = time (NULL);
932                 tl = localtime (&t);
933                 sprintf (text, "%04d-%02d-%02d", tl->tm_year+1900, tl->tm_mon+1, tl->tm_mday);
934 #endif /* WIN32 */
935                 if (do_prefix)          sprintf(stamp_data->date, "Date %s", text);
936                 else                            sprintf(stamp_data->date, "%s", text);
937         } else {
938                 stamp_data->date[0] = '\0';
939         }
940         
941         if (G.scene->r.stamp & R_STAMP_MARKER) {
942                 TimeMarker *marker = get_frame_marker(CFRA);
943         
944                 if (marker) strcpy(text, marker->name);
945                 else            strcpy(text, "<none>");
946                 
947                 if (do_prefix)          sprintf(stamp_data->marker, "Marker %s", text);
948                 else                            sprintf(stamp_data->marker, "%s", text);
949         } else {
950                 stamp_data->marker[0] = '\0';
951         }
952         
953         if (G.scene->r.stamp & R_STAMP_TIME) {
954                 int h, m, s, f;
955                 h= m= s= f= 0;
956                 f = (int)(G.scene->r.cfra % G.scene->r.frs_sec);
957                 s = (int)(G.scene->r.cfra / G.scene->r.frs_sec);
958
959                 if (s) {
960                         m = (int)(s / 60);
961                         s %= 60;
962
963                         if (m) {
964                                 h = (int)(m / 60);
965                                 m %= 60;
966                         }
967                 }
968
969                 if (G.scene->r.frs_sec < 100)
970                         sprintf (text, "%02d:%02d:%02d.%02d", h, m, s, f);
971                 else
972                         sprintf (text, "%02d:%02d:%02d.%03d", h, m, s, f);
973                 
974                 if (do_prefix)          sprintf(stamp_data->time, "Time %s", text);
975                 else                            sprintf(stamp_data->time, "%s", text);
976         } else {
977                 stamp_data->time[0] = '\0';
978         }
979         
980         if (G.scene->r.stamp & R_STAMP_FRAME) {
981                 char format[32];
982                 if (do_prefix)          sprintf(format, "Frame %%0%di\n", 1 + (int) log10(G.scene->r.efra));
983                 else                            sprintf(format, "%%0%di\n", 1 + (int) log10(G.scene->r.efra));
984                 sprintf (stamp_data->frame, format, G.scene->r.cfra);
985         } else {
986                 stamp_data->frame[0] = '\0';
987         }
988
989         if (G.scene->r.stamp & R_STAMP_CAMERA) {
990                 if (do_prefix)          sprintf(stamp_data->camera, "Camera %s", ((Camera *) G.scene->camera)->id.name+2);
991                 else                            sprintf(stamp_data->camera, "%s", ((Camera *) G.scene->camera)->id.name+2);
992         } else {
993                 stamp_data->camera[0] = '\0';
994         }
995
996         if (G.scene->r.stamp & R_STAMP_SCENE) {
997                 if (do_prefix)          sprintf(stamp_data->scene, "Scene %s", G.scene->id.name+2);
998                 else                            sprintf(stamp_data->scene, "%s", G.scene->id.name+2);
999         } else {
1000                 stamp_data->scene[0] = '\0';
1001         }
1002         
1003         if (G.scene->r.stamp & R_STAMP_SEQSTRIP) {
1004                 Sequence *seq = get_forground_frame_seq(CFRA);
1005         
1006                 if (seq) strcpy(text, seq->name+2);
1007                 else            strcpy(text, "<none>");
1008                 
1009                 if (do_prefix)          sprintf(stamp_data->strip, "Strip %s", text);
1010                 else                            sprintf(stamp_data->strip, "%s", text);
1011         } else {
1012                 stamp_data->strip[0] = '\0';
1013         }
1014 }
1015
1016 void BKE_stamp_buf(unsigned char *rect, float *rectf, int width, int height, int channels)
1017 {
1018         struct StampData stamp_data;
1019         
1020         int x=1,y=1;
1021         int font_height;
1022         int text_width;
1023         int text_pad;
1024         struct BMF_Font *font;
1025         
1026         if (!rect && !rectf)
1027                 return;
1028         
1029         stampdata(&stamp_data, 1);
1030         
1031         switch (G.scene->r.stamp_font_id) {
1032         case 1: /* tiny */
1033                 font = BMF_GetFont(BMF_kHelveticaBold8);
1034                 break;
1035         case 2: /* small */
1036                 font = BMF_GetFont(BMF_kHelveticaBold10);
1037                 break;
1038         case 3: /* medium */
1039                 font = BMF_GetFont(BMF_kScreen12);
1040                 break;
1041         case 0: /* large - default */
1042                 font = BMF_GetFont(BMF_kScreen15);
1043                 break;
1044         case 4: /* huge */
1045                 font = BMF_GetFont(BMF_kHelveticaBold14);
1046                 break;
1047         default:
1048                 font = NULL;
1049                 break;
1050         }
1051         
1052         font_height = BMF_GetFontHeight(font);
1053         /* All texts get halfspace+1 pixel on each side and 1 pix
1054         above and below as padding against their backing rectangles */
1055         text_pad = BMF_GetStringWidth(font, " ");
1056         
1057         x = 1; /* Inits for everyone, text position, so 1 for padding, not 0 */
1058         y = height - font_height - 1; /* Also inits for everyone, notice padding pixel */
1059         
1060         if (stamp_data.file[0]) {
1061                 /* Top left corner */
1062                 text_width = BMF_GetStringWidth(font, stamp_data.file);
1063                 buf_rectfill_area(rect, rectf, width, height, G.scene->r.bg_stamp, x-1, y-1, x+text_width+text_pad+1, y+font_height+1);
1064                 BMF_DrawStringBuf(font, stamp_data.file, x+(text_pad/2), y, G.scene->r.fg_stamp, rect, rectf, width, height, channels);
1065                 y -= font_height+2; /* Top and bottom 1 pix padding each */
1066         }
1067
1068         /* Top left corner, below File */
1069         if (stamp_data.note[0]) {
1070                 text_width = BMF_GetStringWidth(font, stamp_data.note);
1071                 buf_rectfill_area(rect, rectf, width, height, G.scene->r.bg_stamp, x-1, y-1, x+text_width+text_pad+1, y+font_height+1);
1072                 BMF_DrawStringBuf(font, stamp_data.note, x+(text_pad/2), y, G.scene->r.fg_stamp, rect, rectf, width, height, channels);
1073                 y -= font_height+2; /* Top and bottom 1 pix padding each */
1074         }
1075         
1076         /* Top left corner, below File (or Note) */
1077         if (stamp_data.date[0]) {
1078                 text_width = BMF_GetStringWidth(font, stamp_data.date);
1079                 buf_rectfill_area(rect, rectf, width, height, G.scene->r.bg_stamp, x-1, y-1, x+text_width+text_pad+1, y+font_height+1);
1080                 BMF_DrawStringBuf(font, stamp_data.date, x+(text_pad/2), y, G.scene->r.fg_stamp, rect, rectf, width, height, channels);
1081         }
1082
1083         /* Bottom left corner, leaving space for timing */
1084         if (stamp_data.marker[0]) {
1085                 x = 1;
1086                 y = font_height+2+1; /* 2 for padding in TIME|FRAME fields below and 1 for padding in this one */
1087                 text_width = BMF_GetStringWidth(font, stamp_data.marker);
1088                 buf_rectfill_area(rect, rectf, width, height, G.scene->r.bg_stamp, x-1, y-1, x+text_width+text_pad+1, y+font_height+1);
1089                 BMF_DrawStringBuf(font, stamp_data.marker, x+(text_pad/2), y, G.scene->r.fg_stamp, rect, rectf, width, height, channels);
1090         }
1091         
1092         /* Left bottom corner */
1093         if (stamp_data.time[0]) {
1094                 x = 1;
1095                 y = 1;
1096                 text_width = BMF_GetStringWidth(font, stamp_data.time);
1097                 buf_rectfill_area(rect, rectf, width, height, G.scene->r.bg_stamp, x-1, y-1, x+text_width+text_pad+1, y+font_height+1);
1098                 BMF_DrawStringBuf(font, stamp_data.time, x+(text_pad/2), y, G.scene->r.fg_stamp, rect, rectf, width, height, channels);
1099                 x += text_width+text_pad+2; /* Both sides have 1 pix additional padding each */
1100         }
1101         
1102         if (stamp_data.frame[0]) {
1103                 text_width = BMF_GetStringWidth(font, stamp_data.frame);
1104                 /* Left bottom corner (after SMPTE if exists) */
1105                 if (!stamp_data.time[0])        x = 1;
1106                 y = 1;
1107                 buf_rectfill_area(rect, rectf, width, height, G.scene->r.bg_stamp, x-1, y-1, x+text_width+text_pad+1, y+font_height+1);
1108                 BMF_DrawStringBuf(font, stamp_data.frame, x+(text_pad/2), y, G.scene->r.fg_stamp, rect, rectf, width, height, channels);
1109         }
1110
1111         if (stamp_data.camera[0]) {
1112                 text_width = BMF_GetStringWidth(font, stamp_data.camera);
1113                 /* Center of bottom edge */
1114                 x = (width/2) - (BMF_GetStringWidth(font, stamp_data.camera)/2);
1115                 y = 1;
1116                 buf_rectfill_area(rect, rectf, width, height, G.scene->r.bg_stamp, x-1, y-1, x+text_width+text_pad+1, y+font_height+1);
1117                 BMF_DrawStringBuf(font, stamp_data.camera, x+(text_pad/2), y, G.scene->r.fg_stamp, rect, rectf, width, height, channels);
1118         }
1119         
1120         if (stamp_data.scene[0]) {
1121                 text_width = BMF_GetStringWidth(font, stamp_data.scene);
1122                 /* Bottom right corner */
1123                 x = width - (text_width+1+text_pad);
1124                 y = 1;
1125                 buf_rectfill_area(rect, rectf, width, height, G.scene->r.bg_stamp, x-1, y-1, x+text_width+text_pad+1, y+font_height+1);
1126                 BMF_DrawStringBuf(font, stamp_data.scene, x+(text_pad/2), y, G.scene->r.fg_stamp, rect, rectf, width, height, channels);
1127         }
1128         
1129         if (stamp_data.strip[0]) {
1130                 text_width = BMF_GetStringWidth(font, stamp_data.strip);
1131                 /* Top right corner */
1132                 x = width - (text_width+1+text_pad);
1133                 y = height - font_height - 1;
1134                 buf_rectfill_area(rect, rectf, width, height, G.scene->r.bg_stamp, x-1, y-1, x+text_width+text_pad+1, y+font_height+1);
1135                 BMF_DrawStringBuf(font, stamp_data.strip, x+(text_pad/2), y, G.scene->r.fg_stamp, rect, rectf, width, height, channels);
1136         }
1137         
1138 }
1139
1140 void BKE_stamp_info(struct ImBuf *ibuf)
1141 {
1142         struct StampData stamp_data;
1143
1144         if (!ibuf)      return;
1145         
1146         /* fill all the data values, no prefix */
1147         stampdata(&stamp_data, 0);
1148         
1149         if (stamp_data.file[0])         IMB_imginfo_change_field (ibuf, "File",         stamp_data.file);
1150         if (stamp_data.note[0])         IMB_imginfo_change_field (ibuf, "Note",         stamp_data.note);
1151         if (stamp_data.date[0])         IMB_imginfo_change_field (ibuf, "Date",         stamp_data.date);
1152         if (stamp_data.marker[0])       IMB_imginfo_change_field (ibuf, "Marker",       stamp_data.marker);
1153         if (stamp_data.time[0])         IMB_imginfo_change_field (ibuf, "Time",         stamp_data.time);
1154         if (stamp_data.frame[0])        IMB_imginfo_change_field (ibuf, "Frame",        stamp_data.frame);
1155         if (stamp_data.camera[0])       IMB_imginfo_change_field (ibuf, "Camera",       stamp_data.camera);
1156         if (stamp_data.scene[0])        IMB_imginfo_change_field (ibuf, "Scene",        stamp_data.scene);
1157         if (stamp_data.strip[0])        IMB_imginfo_change_field (ibuf, "Strip",        stamp_data.strip);
1158 }
1159
1160 int BKE_write_ibuf(ImBuf *ibuf, char *name, int imtype, int subimtype, int quality)
1161 {
1162         int ok;
1163         
1164         if(imtype==0);
1165         else if(imtype== R_IRIS) 
1166                 ibuf->ftype= IMAGIC;
1167         else if ((imtype==R_RADHDR)) {
1168                 ibuf->ftype= RADHDR;
1169         }
1170         else if (imtype==R_PNG || imtype==R_FFMPEG) {
1171                 ibuf->ftype= PNG;
1172         }
1173 #ifdef WITH_DDS
1174         else if ((imtype==R_DDS)) {
1175                 ibuf->ftype= DDS;
1176         }
1177 #endif
1178         else if ((imtype==R_BMP)) {
1179                 ibuf->ftype= BMP;
1180         }
1181         else if ((G.have_libtiff) && (imtype==R_TIFF)) {
1182                 ibuf->ftype= TIF;
1183
1184                 if(subimtype & R_TIFF_16BIT)
1185                         ibuf->ftype |= TIF_16BIT;
1186         }
1187 #ifdef WITH_OPENEXR
1188         else if (imtype==R_OPENEXR || imtype==R_MULTILAYER) {
1189                 ibuf->ftype= OPENEXR;
1190                 if(subimtype & R_OPENEXR_HALF)
1191                         ibuf->ftype |= OPENEXR_HALF;
1192                 ibuf->ftype |= (quality & OPENEXR_COMPRESS);
1193                 
1194                 if(!(subimtype & R_OPENEXR_ZBUF))
1195                         ibuf->zbuf_float = NULL;        /* signal for exr saving */
1196                 
1197         }
1198 #endif
1199         else if (imtype==R_CINEON) {
1200                 ibuf->ftype = CINEON;
1201         }
1202         else if (imtype==R_DPX) {
1203                 ibuf->ftype = DPX;
1204         }
1205         else if (imtype==R_TARGA) {
1206                 ibuf->ftype= TGA;
1207         }
1208         else if(imtype==R_RAWTGA) {
1209                 ibuf->ftype= RAWTGA;
1210         }
1211         else if(imtype==R_HAMX) {
1212                 ibuf->ftype= AN_hamx;
1213         }
1214         else {
1215                 /* R_JPEG90, R_MOVIE, etc. default we save jpegs */
1216                 if(quality < 10) quality= 90;
1217                 ibuf->ftype= JPG|quality;
1218                 if(ibuf->depth==32) ibuf->depth= 24;    /* unsupported feature only confuses other s/w */
1219         }
1220         
1221         BLI_make_existing_file(name);
1222
1223         if(G.scene->r.scemode & R_STAMP_INFO)
1224                 BKE_stamp_info(ibuf);
1225         
1226         ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1227         if (ok == 0) {
1228                 perror(name);
1229         }
1230         
1231         return(ok);
1232 }
1233
1234
1235 void BKE_makepicstring(char *string, char *base, int frame, int imtype)
1236 {
1237         if (string==NULL) return;
1238
1239         BLI_strncpy(string, base, FILE_MAX - 10);       /* weak assumption */
1240         
1241         /* if we dont have any #'s to insert numbers into, use 4 numbers by default */
1242         if (strchr(string, '#')==NULL)
1243                 strcat(string, "####"); /* 4 numbers */
1244         
1245         BLI_convertstringcode(string, G.sce, frame);
1246
1247         if(G.scene->r.scemode & R_EXTENSION) 
1248                 BKE_add_image_extension(string, imtype);
1249                 
1250 }
1251
1252 /* used by sequencer too */
1253 struct anim *openanim(char *name, int flags)
1254 {
1255         struct anim *anim;
1256         struct ImBuf *ibuf;
1257         
1258         anim = IMB_open_anim(name, flags);
1259         if (anim == NULL) return(0);
1260
1261         ibuf = IMB_anim_absolute(anim, 0);
1262         if (ibuf == NULL) {
1263                 printf("not an anim; %s\n", name);
1264                 IMB_free_anim(anim);
1265                 return(0);
1266         }
1267         IMB_freeImBuf(ibuf);
1268         
1269         return(anim);
1270 }
1271
1272 /* ************************* New Image API *************** */
1273
1274
1275 /* Notes about Image storage 
1276 - packedfile
1277   -> written in .blend
1278 - filename
1279   -> written in .blend
1280 - movie
1281   -> comes from packedfile or filename
1282 - renderresult
1283   -> comes from packedfile or filename
1284 - listbase
1285   -> ibufs from exrhandle
1286 - flipbook array
1287   -> ibufs come from movie, temporary renderresult or sequence
1288 - ibuf
1289   -> comes from packedfile or filename or generated
1290
1291 */
1292
1293
1294 /* forces existance of 1 Image for renderout or nodes, returns Image */
1295 /* name is only for default, when making new one */
1296 Image *BKE_image_verify_viewer(int type, const char *name)
1297 {
1298         Image *ima;
1299         
1300         for(ima=G.main->image.first; ima; ima= ima->id.next)
1301                 if(ima->source==IMA_SRC_VIEWER)
1302                         if(ima->type==type)
1303                                 break;
1304         
1305         if(ima==NULL)
1306                 ima= image_alloc(name, IMA_SRC_VIEWER, type);
1307         
1308         /* happens on reload, imagewindow cannot be image user when hidden*/
1309         if(ima->id.us==0)
1310                 id_us_plus(&ima->id);
1311
1312         return ima;
1313 }
1314
1315 void BKE_image_assign_ibuf(Image *ima, ImBuf *ibuf)
1316 {
1317         image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1318 }
1319
1320 void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
1321 {
1322         if(ima==NULL)
1323                 return;
1324         
1325         switch(signal) {
1326         case IMA_SIGNAL_FREE:
1327                 image_free_buffers(ima);
1328                 if(iuser)
1329                         iuser->ok= 1;
1330                 break;
1331         case IMA_SIGNAL_SRC_CHANGE:
1332                 if(ima->type==IMA_TYPE_MULTILAYER)
1333                         image_free_buffers(ima);
1334                 else if(ima->source==IMA_SRC_GENERATED) {
1335                         if(ima->gen_x==0 || ima->gen_y==0) {
1336                                 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1337                                 if(ibuf) {
1338                                         ima->gen_x= ibuf->x;
1339                                         ima->gen_y= ibuf->y;
1340                                 }
1341                         }
1342                 }
1343                 ima->ok= 1;
1344                 if(iuser)
1345                         iuser->ok= 1;
1346                 break;
1347                         
1348         case IMA_SIGNAL_RELOAD:
1349                 /* try to repack file */
1350                 if(ima->packedfile) {
1351                         PackedFile *pf;
1352                         pf = newPackedFile(ima->name);
1353                         if (pf) {
1354                                 freePackedFile(ima->packedfile);
1355                                 ima->packedfile = pf;
1356                                 image_free_buffers(ima);
1357                         } else {
1358                                 printf("ERROR: Image not available. Keeping packed image\n");
1359                         }
1360                 }
1361                 else
1362                         image_free_buffers(ima);
1363                 
1364                 if(iuser)
1365                         iuser->ok= 1;
1366                 
1367                 break;
1368         case IMA_SIGNAL_USER_NEW_IMAGE:
1369                 if(iuser) {
1370                         iuser->ok= 1;
1371                         if(ima->source==IMA_SRC_FILE || ima->source==IMA_SRC_SEQUENCE) {
1372                                 if(ima->type==IMA_TYPE_MULTILAYER) {
1373                                         iuser->multi_index= 0;
1374                                         iuser->layer= iuser->pass= 0;
1375                                 }
1376                         }
1377                 }
1378                 break;
1379         }
1380 }
1381
1382 /* if layer or pass changes, we need an index for the imbufs list */
1383 /* note it is called for rendered results, but it doesnt use the index! */
1384 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
1385 RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
1386 {
1387         RenderLayer *rl;
1388         RenderPass *rpass= NULL;
1389         
1390         if(rr==NULL) 
1391                 return NULL;
1392         
1393         if(iuser) {
1394                 short index= 0, rl_index= 0, rp_index;
1395                 
1396                 for(rl= rr->layers.first; rl; rl= rl->next, rl_index++) {
1397                         rp_index= 0;
1398                         for(rpass= rl->passes.first; rpass; rpass= rpass->next, index++, rp_index++)
1399                                 if(iuser->layer==rl_index && iuser->pass==rp_index)
1400                                         break;
1401                         if(rpass)
1402                                 break;
1403                 }
1404                 
1405                 if(rpass)
1406                         iuser->multi_index= index;
1407                 else 
1408                         iuser->multi_index= 0;
1409         }
1410         if(rpass==NULL) {
1411                 rl= rr->layers.first;
1412                 if(rl)
1413                         rpass= rl->passes.first;
1414         }
1415         
1416         return rpass;
1417 }
1418
1419 RenderResult *BKE_image_get_renderresult(Image *ima)
1420 {
1421         if(ima->rr)
1422                 return ima->rr;
1423         if(ima->type==IMA_TYPE_R_RESULT)
1424                 return RE_GetResult(RE_GetRender(G.scene->id.name));
1425         return NULL;
1426 }
1427
1428 /* after imbuf load, openexr type can return with a exrhandle open */
1429 /* in that case we have to build a render-result */
1430 static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
1431 {
1432         
1433         ima->rr= RE_MultilayerConvert(ibuf->userdata, ibuf->x, ibuf->y);
1434
1435 #ifdef WITH_OPENEXR
1436         IMB_exr_close(ibuf->userdata);
1437 #endif
1438
1439         ibuf->userdata= NULL;
1440         if(ima->rr)
1441                 ima->rr->framenr= framenr;
1442 }
1443
1444 /* common stuff to do with images after loading */
1445 static void image_initialize_after_load(Image *ima, ImBuf *ibuf)
1446 {
1447         
1448         
1449         /* preview is NULL when it has never been used as an icon before */
1450         if(G.background==0 && ima->preview==NULL)
1451                 BKE_icon_changed(BKE_icon_getid(&ima->id));
1452         
1453         /* stringcodes also in ibuf, ibuf->name is used to retrieve original (buttons) */
1454         BLI_strncpy(ibuf->name, ima->name, FILE_MAX);
1455         
1456         /* fields */
1457         if (ima->flag & IMA_FIELDS) {
1458                 if(ima->flag & IMA_STD_FIELD) de_interlace_st(ibuf);
1459                 else de_interlace_ng(ibuf);
1460         }
1461         /* timer */
1462         ima->lastused = clock() / CLOCKS_PER_SEC;
1463         
1464         ima->ok= IMA_OK_LOADED;
1465         
1466 }
1467
1468 static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
1469 {
1470         struct ImBuf *ibuf;
1471         unsigned short numlen;
1472         char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
1473         
1474         ima->lastframe= frame;
1475
1476         BLI_stringdec(ima->name, head, tail, &numlen);
1477         BLI_stringenc(ima->name, head, tail, numlen, frame);
1478         BLI_strncpy(name, ima->name, sizeof(name));
1479         
1480         if(ima->id.lib)
1481                 BLI_convertstringcode(name, ima->id.lib->filename, frame);
1482         else
1483                 BLI_convertstringcode(name, G.sce, frame);
1484         
1485         /* read ibuf */
1486         ibuf = IMB_loadiffname(name, IB_rect|IB_multilayer);
1487         if(G.f & G_DEBUG) printf("loaded %s\n", name);
1488         
1489         if (ibuf) {
1490 #ifdef WITH_OPENEXR
1491                 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
1492                 if (ibuf->ftype==OPENEXR && ibuf->userdata) {
1493                         image_create_multilayer(ima, ibuf, frame);      
1494                         ima->type= IMA_TYPE_MULTILAYER;
1495                         IMB_freeImBuf(ibuf);
1496                         ibuf= NULL;
1497                 }
1498                 else {
1499                         image_assign_ibuf(ima, ibuf, 0, frame);
1500                         image_initialize_after_load(ima, ibuf);
1501                 }
1502 #else
1503                 image_assign_ibuf(ima, ibuf, 0, frame);
1504                 image_initialize_after_load(ima, ibuf);
1505 #endif
1506         }
1507         else
1508                 ima->ok= 0;
1509         
1510         if(iuser)
1511                 iuser->ok= ima->ok;
1512         
1513         return ibuf;
1514 }
1515
1516 static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int frame)
1517 {
1518         struct ImBuf *ibuf= NULL;
1519         
1520         /* either we load from RenderResult, or we have to load a new one */
1521         
1522         /* check for new RenderResult */
1523         if(ima->rr==NULL || frame!=ima->rr->framenr) {
1524                 /* copy to survive not found multilayer image */
1525                 RenderResult *oldrr= ima->rr;
1526         
1527                 ima->rr= NULL;
1528                 ibuf = image_load_sequence_file(ima, iuser, frame);
1529                 
1530                 if(ibuf) { /* actually an error */
1531                         ima->type= IMA_TYPE_IMAGE;
1532                         printf("error, multi is normal image\n");
1533                 }
1534                 // printf("loaded new result %p\n", ima->rr);
1535                 /* free result if new one found */
1536                 if(ima->rr) {
1537                         // if(oldrr) printf("freed previous result %p\n", oldrr);
1538                         if(oldrr) RE_FreeRenderResult(oldrr);
1539                 }
1540                 else
1541                         ima->rr= oldrr;
1542
1543         }
1544         if(ima->rr) {
1545                 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
1546                 
1547                 if(rpass) {
1548                         // printf("load from pass %s\n", rpass->name);
1549                         /* since we free  render results, we copy the rect */
1550                         ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0, 0);
1551                         ibuf->rect_float= MEM_dupallocN(rpass->rect);
1552                         ibuf->flags |= IB_rectfloat;
1553                         ibuf->mall= IB_rectfloat;
1554                         ibuf->channels= rpass->channels;
1555                         
1556                         image_assign_ibuf(ima, ibuf, iuser->multi_index, frame);
1557                         image_initialize_after_load(ima, ibuf);
1558                         
1559                 }
1560                 // else printf("pass not found\n");
1561         }
1562         else
1563                 ima->ok= 0;
1564         
1565         if(iuser)
1566                 iuser->ok= ima->ok;
1567         
1568         return ibuf;
1569 }
1570
1571
1572 static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
1573 {
1574         struct ImBuf *ibuf= NULL;
1575         
1576         ima->lastframe= frame;
1577         
1578         if(ima->anim==NULL) {
1579                 char str[FILE_MAX];
1580                 
1581                 BLI_strncpy(str, ima->name, FILE_MAX);
1582                 if(ima->id.lib)
1583                         BLI_convertstringcode(str, ima->id.lib->filename, 0);
1584                 else
1585                         BLI_convertstringcode(str, G.sce, 0);
1586                 
1587                 ima->anim = openanim(str, IB_cmap | IB_rect);
1588                 
1589                 /* let's initialize this user */
1590                 if(ima->anim && iuser && iuser->frames==0)
1591                         iuser->frames= IMB_anim_get_duration(ima->anim);
1592         }
1593         
1594         if(ima->anim) {
1595                 int dur = IMB_anim_get_duration(ima->anim);
1596                 int fra= frame-1;
1597                 
1598                 if(fra<0) fra = 0;
1599                 if(fra>(dur-1)) fra= dur-1;
1600                 ibuf = IMB_anim_absolute(ima->anim, fra);
1601                 
1602                 if(ibuf) {
1603                         image_assign_ibuf(ima, ibuf, 0, frame);
1604                         image_initialize_after_load(ima, ibuf);
1605                 }
1606                 else
1607                         ima->ok= 0;
1608         }
1609         else
1610                 ima->ok= 0;
1611         
1612         if(iuser)
1613                 iuser->ok= ima->ok;
1614         
1615         return ibuf;
1616 }
1617
1618 /* cfra used for # code, Image can only have this # for all its users */
1619 static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
1620 {
1621         struct ImBuf *ibuf;
1622         char str[FILE_MAX];
1623         
1624         /* always ensure clean ima */
1625         image_free_buffers(ima);
1626         
1627         /* is there a PackedFile with this image ? */
1628         if (ima->packedfile) {
1629                 ibuf = IMB_ibImageFromMemory((int *) ima->packedfile->data, ima->packedfile->size, IB_rect|IB_multilayer);
1630         } 
1631         else {
1632                         
1633                 /* get the right string */
1634                 BLI_strncpy(str, ima->name, sizeof(str));
1635                 if(ima->id.lib)
1636                         BLI_convertstringcode(str, ima->id.lib->filename, cfra);
1637                 else
1638                         BLI_convertstringcode(str, G.sce, cfra);
1639                 
1640                 /* read ibuf */
1641                 ibuf = IMB_loadiffname(str, IB_rect|IB_multilayer|IB_imginfo);
1642         }
1643         
1644         if (ibuf) {
1645                 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
1646                 if (ibuf->ftype==OPENEXR && ibuf->userdata) {
1647                         image_create_multilayer(ima, ibuf, cfra);       
1648                         ima->type= IMA_TYPE_MULTILAYER;
1649                         IMB_freeImBuf(ibuf);
1650                         ibuf= NULL;
1651                 }
1652                 else {
1653                         image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1654                         image_initialize_after_load(ima, ibuf);
1655
1656                         /* check if the image is a font image... */
1657                         detectBitmapFont(ibuf);
1658                         
1659                         /* make packed file for autopack */
1660                         if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK))
1661                                 ima->packedfile = newPackedFile(str);
1662                 }
1663                 
1664                 if(ima->flag & IMA_DO_PREMUL)
1665                         converttopremul(ibuf);
1666         }
1667         else
1668                 ima->ok= 0;
1669         
1670         if(iuser)
1671                 iuser->ok= ima->ok;
1672         
1673         return ibuf;
1674 }
1675
1676 static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
1677 {
1678         ImBuf *ibuf= NULL;
1679         
1680         if(ima->rr==NULL) {
1681                 ibuf = image_load_image_file(ima, iuser, 0);
1682                 if(ibuf) { /* actually an error */
1683                         ima->type= IMA_TYPE_IMAGE;
1684                         return ibuf;
1685                 }
1686         }
1687         if(ima->rr) {
1688                 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
1689
1690                 if(rpass) {
1691                         ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0, 0);
1692                         
1693                         image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
1694                         image_initialize_after_load(ima, ibuf);
1695                         
1696                         ibuf->rect_float= rpass->rect;
1697                         ibuf->flags |= IB_rectfloat;
1698                         ibuf->channels= rpass->channels;
1699                 }
1700         }
1701         
1702         if(ibuf==NULL) 
1703                 ima->ok= 0;
1704         if(iuser)
1705                 iuser->ok= ima->ok;
1706         
1707         return ibuf;
1708 }
1709
1710
1711 /* showing RGBA result itself (from compo/sequence) or
1712    like exr, using layers etc */
1713 static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser)
1714 {
1715         RenderResult *rr= RE_GetResult(RE_GetRender(G.scene->id.name));
1716         
1717         if(rr && iuser) {
1718                 RenderResult rres;
1719                 float *rectf;
1720                 unsigned int *rect;
1721                 int channels= 4, layer= iuser->layer;
1722                 
1723                 /* this gives active layer, composite or seqence result */
1724                 RE_GetResultImage(RE_GetRender(G.scene->id.name), &rres);
1725                 rect= (unsigned int *)rres.rect32;
1726                 rectf= rres.rectf;
1727                 
1728                 /* get compo/seq result by default */
1729                 if(rr->rectf && layer==0);
1730                 else if(rr->layers.first) {
1731                         RenderLayer *rl= BLI_findlink(&rr->layers, iuser->layer-(rr->rectf?1:0));
1732                         if(rl) {
1733                                 /* there's no combined pass, is in renderlayer itself */
1734                                 if(iuser->pass==0) {
1735                                         rectf= rl->rectf;
1736                                 }
1737                                 else {
1738                                         RenderPass *rpass= BLI_findlink(&rl->passes, iuser->pass-1);
1739                                         if(rpass) {
1740                                                 channels= rpass->channels;
1741                                                 rectf= rpass->rect;
1742                                         }
1743                                 }
1744                         }
1745                 }
1746                 
1747                 if(rectf || rect) {
1748                         ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1749
1750                         /* make ibuf if needed, and initialize it */
1751                         if(ibuf==NULL) {
1752                                 ibuf= IMB_allocImBuf(rr->rectx, rr->recty, 32, 0, 0);
1753                                 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1754                         }
1755                         ibuf->x= rr->rectx;
1756                         ibuf->y= rr->recty;
1757                         
1758                         if(ibuf->rect_float!=rectf || rect) /* ensure correct redraw */
1759                                 imb_freerectImBuf(ibuf);
1760                         if(rect)
1761                                 ibuf->rect= rect;
1762                         
1763                         ibuf->rect_float= rectf;
1764                         ibuf->flags |= IB_rectfloat;
1765                         ibuf->channels= channels;
1766                         ibuf->zbuf_float= rres.rectz;
1767                         ibuf->flags |= IB_zbuffloat;
1768                         
1769                         ima->ok= IMA_OK_LOADED;
1770                         return ibuf;
1771                 }
1772         }
1773         
1774         return NULL;
1775 }
1776
1777 /* Checks optional ImageUser and verifies/creates ImBuf. */
1778 /* returns ibuf */
1779 ImBuf *BKE_image_get_ibuf(Image *ima, ImageUser *iuser)
1780 {
1781         ImBuf *ibuf= NULL;
1782         float color[] = {0, 0, 0, 1};
1783
1784         /* quick reject tests */
1785         if(ima==NULL) 
1786                 return NULL;
1787         if(iuser) {
1788                 if(iuser->ok==0)
1789                         return NULL;
1790         }
1791         else if(ima->ok==0)
1792                 return NULL;
1793         
1794         BLI_lock_thread(LOCK_IMAGE);
1795         
1796         /* handle image source and types */
1797         if(ima->source==IMA_SRC_MOVIE) {
1798                 /* source is from single file, use flipbook to store ibuf */
1799                 int frame= iuser?iuser->framenr:ima->lastframe;
1800                 
1801                 ibuf= image_get_ibuf(ima, 0, frame);
1802                 if(ibuf==NULL)
1803                         ibuf= image_load_movie_file(ima, iuser, frame);
1804         }
1805         else if(ima->source==IMA_SRC_SEQUENCE) {
1806                 
1807                 if(ima->type==IMA_TYPE_IMAGE) {
1808                         /* regular files, ibufs in flipbook, allows saving */
1809                         int frame= iuser?iuser->framenr:ima->lastframe;
1810                         
1811                         ibuf= image_get_ibuf(ima, 0, frame);
1812                         if(ibuf==NULL)
1813                                 ibuf= image_load_sequence_file(ima, iuser, frame);
1814                         else
1815                                 BLI_strncpy(ima->name, ibuf->name, sizeof(ima->name));
1816                 }
1817                 /* no else; on load the ima type can change */
1818                 if(ima->type==IMA_TYPE_MULTILAYER) {
1819                         /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
1820                         int frame= iuser?iuser->framenr:ima->lastframe;
1821                         int index= iuser?iuser->multi_index:IMA_NO_INDEX;
1822                         
1823                         ibuf= image_get_ibuf(ima, index, frame);
1824                         if(G.rt) printf("seq multi fra %d id %d ibuf %p %s\n", frame, index, ibuf, ima->id.name);
1825                         if(ibuf==NULL)
1826                                 ibuf= image_load_sequence_multilayer(ima, iuser, frame);
1827                         else
1828                                 BLI_strncpy(ima->name, ibuf->name, sizeof(ima->name));
1829                 }
1830
1831         }
1832         else if(ima->source==IMA_SRC_FILE) {
1833                 
1834                 if(ima->type==IMA_TYPE_IMAGE) {
1835                         ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1836                         if(ibuf==NULL)
1837                                 ibuf= image_load_image_file(ima, iuser, G.scene->r.cfra);       /* cfra only for '#', this global is OK */
1838                 }
1839                 /* no else; on load the ima type can change */
1840                 if(ima->type==IMA_TYPE_MULTILAYER) {
1841                         /* keeps render result, stores ibufs in listbase, allows saving */
1842                         ibuf= image_get_ibuf(ima, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
1843                         if(ibuf==NULL)
1844                                 ibuf= image_get_ibuf_multilayer(ima, iuser);
1845                 }
1846                         
1847         }
1848         else if(ima->source == IMA_SRC_GENERATED) {
1849                 /* generated is: ibuf is allocated dynamically */
1850                 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1851                 
1852                 if(ibuf==NULL) {
1853                         if(ima->type==IMA_TYPE_VERSE) {
1854                                 /* todo */
1855                         }
1856                         else { /* always fall back to IMA_TYPE_UV_TEST */
1857                                 /* UV testgrid or black or solid etc */
1858                                 if(ima->gen_x==0) ima->gen_x= 256;
1859                                 if(ima->gen_y==0) ima->gen_y= 256;
1860                                 ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 0, ima->gen_type, color);
1861                                 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1862                                 ima->ok= IMA_OK_LOADED;
1863                         }
1864                 }
1865         }
1866         else if(ima->source == IMA_SRC_VIEWER) {
1867                 if(ima->type==IMA_TYPE_R_RESULT) {
1868                         /* always verify entirely */
1869                         ibuf= image_get_render_result(ima, iuser);
1870                 }
1871                 else if(ima->type==IMA_TYPE_COMPOSITE) {
1872                         int frame= iuser?iuser->framenr:0;
1873                         
1874                         /* Composite Viewer, all handled in compositor */
1875                         ibuf= image_get_ibuf(ima, 0, frame);
1876                         if(ibuf==NULL) {
1877                                 /* fake ibuf, will be filled in compositor */
1878                                 ibuf= IMB_allocImBuf(256, 256, 32, IB_rect, 0);
1879                                 image_assign_ibuf(ima, ibuf, 0, frame);
1880                         }
1881                 }
1882         }
1883
1884         if(G.rendering==0)
1885                 tag_image_time(ima);
1886
1887         BLI_unlock_thread(LOCK_IMAGE);
1888
1889         return ibuf;
1890 }
1891
1892
1893 void BKE_image_user_calc_imanr(ImageUser *iuser, int cfra, int fieldnr)
1894 {
1895         int imanr, len;
1896         
1897         /* here (+fie_ima/2-1) makes sure that division happens correctly */
1898         len= (iuser->fie_ima*iuser->frames)/2;
1899         
1900         if(len==0) {
1901                 iuser->framenr= 0;
1902         }
1903         else {
1904                 cfra= cfra - iuser->sfra+1;
1905                 
1906                 /* cyclic */
1907                 if(iuser->cycl) {
1908                         cfra= ( (cfra) % len );
1909                         if(cfra < 0) cfra+= len;
1910                         if(cfra==0) cfra= len;
1911                 }
1912                 
1913                 if(cfra<1) cfra= 1;
1914                 else if(cfra>len) cfra= len;
1915                 
1916                 /* convert current frame to current field */
1917                 cfra= 2*(cfra);
1918                 if(fieldnr) cfra++;
1919                 
1920                 /* transform to images space */
1921                 imanr= (cfra+iuser->fie_ima-2)/iuser->fie_ima;
1922                 if(imanr>iuser->frames) imanr= iuser->frames;
1923                 imanr+= iuser->offset;
1924                 
1925                 if(iuser->cycl) {
1926                         imanr= ( (imanr) % len );
1927                         while(imanr < 0) imanr+= len;
1928                         if(imanr==0) imanr= len;
1929                 }
1930         
1931                 /* allows image users to handle redraws */
1932                 if(iuser->flag & IMA_ANIM_ALWAYS)
1933                         if(imanr!=iuser->framenr)
1934                                 iuser->flag |= IMA_ANIM_REFRESHED;
1935                 
1936                 iuser->framenr= imanr;
1937                 if(iuser->ok==0) iuser->ok= 1;
1938         }
1939 }
1940