Blender V2.61 - r43446

image.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * Contributor(s): Blender Foundation, 2006, full recode
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <fcntl.h>
00034 #include <math.h>
00035 #ifndef WIN32 
00036 #include <unistd.h>
00037 #else
00038 #include <io.h>
00039 #endif
00040 
00041 #include <time.h>
00042 
00043 #ifdef _WIN32
00044 #define open _open
00045 #define close _close
00046 #endif
00047 
00048 #include "MEM_guardedalloc.h"
00049 
00050 #include "IMB_imbuf_types.h"
00051 #include "IMB_imbuf.h"
00052 
00053 #ifdef WITH_OPENEXR
00054 #include "intern/openexr/openexr_multi.h"
00055 #endif
00056 
00057 #include "DNA_packedFile_types.h"
00058 #include "DNA_scene_types.h"
00059 #include "DNA_object_types.h"
00060 #include "DNA_camera_types.h"
00061 #include "DNA_sequence_types.h"
00062 #include "DNA_userdef_types.h"
00063 #include "DNA_brush_types.h"
00064 #include "DNA_mesh_types.h"
00065 #include "DNA_meshdata_types.h"
00066 
00067 #include "BLI_blenlib.h"
00068 #include "BLI_threads.h"
00069 #include "BLI_utildefines.h"
00070 #include "BLI_bpath.h"
00071 
00072 #include "BKE_bmfont.h"
00073 #include "BKE_global.h"
00074 #include "BKE_icons.h"
00075 #include "BKE_image.h"
00076 #include "BKE_library.h"
00077 #include "BKE_main.h"
00078 #include "BKE_packedFile.h"
00079 #include "BKE_scene.h"
00080 #include "BKE_node.h"
00081 #include "BKE_sequencer.h" /* seq_foreground_frame_get() */
00082 #include "BKE_utildefines.h"
00083 
00084 #include "BLF_api.h"
00085 
00086 #include "PIL_time.h"
00087 
00088 #include "RE_pipeline.h"
00089 
00090 #include "GPU_draw.h"
00091 
00092 #include "BLO_sys_types.h" // for intptr_t support
00093 
00094 /* max int, to indicate we don't store sequences in ibuf */
00095 #define IMA_NO_INDEX    0x7FEFEFEF
00096 
00097 /* quick lookup: supports 1 million frames, thousand passes */
00098 #define IMA_MAKE_INDEX(frame, index)    ((frame)<<10)+index
00099 #define IMA_INDEX_FRAME(index)          (index>>10)
00100 #define IMA_INDEX_PASS(index)           (index & ~1023)
00101 
00102 /* ******** IMAGE PROCESSING ************* */
00103 
00104 static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
00105 {
00106     struct ImBuf * tbuf1, * tbuf2;
00107     
00108     if (ibuf == NULL) return;
00109     if (ibuf->flags & IB_fields) return;
00110     ibuf->flags |= IB_fields;
00111     
00112     if (ibuf->rect) {
00113         /* make copies */
00114         tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
00115         tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
00116         
00117         ibuf->x *= 2;
00118         
00119         IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
00120         IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
00121         
00122         ibuf->x /= 2;
00123         IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
00124         IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
00125         
00126         IMB_freeImBuf(tbuf1);
00127         IMB_freeImBuf(tbuf2);
00128     }
00129     ibuf->y /= 2;
00130 }
00131 
00132 static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
00133 {
00134     struct ImBuf * tbuf1, * tbuf2;
00135     
00136     if (ibuf == NULL) return;
00137     if (ibuf->flags & IB_fields) return;
00138     ibuf->flags |= IB_fields;
00139     
00140     if (ibuf->rect) {
00141         /* make copies */
00142         tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect);
00143         tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect);
00144         
00145         ibuf->x *= 2;
00146         
00147         IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
00148         IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
00149         
00150         ibuf->x /= 2;
00151         IMB_rectcpy(ibuf, tbuf2, 0, 0, 0, 0, tbuf2->x, tbuf2->y);
00152         IMB_rectcpy(ibuf, tbuf1, 0, tbuf2->y, 0, 0, tbuf1->x, tbuf1->y);
00153         
00154         IMB_freeImBuf(tbuf1);
00155         IMB_freeImBuf(tbuf2);
00156     }
00157     ibuf->y /= 2;
00158 }
00159 
00160 void image_de_interlace(Image *ima, int odd)
00161 {
00162     ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
00163     if(ibuf) {
00164         if(odd)
00165             de_interlace_st(ibuf);
00166         else
00167             de_interlace_ng(ibuf);
00168     }
00169 }
00170 
00171 /* ***************** ALLOC & FREE, DATA MANAGING *************** */
00172 
00173 static void image_free_buffers(Image *ima)
00174 {
00175     ImBuf *ibuf;
00176     
00177     while((ibuf = ima->ibufs.first)) {
00178         BLI_remlink(&ima->ibufs, ibuf);
00179         
00180         if (ibuf->userdata) {
00181             MEM_freeN(ibuf->userdata);
00182             ibuf->userdata = NULL;
00183         }
00184         IMB_freeImBuf(ibuf);
00185     }
00186     
00187     if(ima->anim) IMB_free_anim(ima->anim);
00188     ima->anim= NULL;
00189 
00190     if(ima->rr) {
00191         RE_FreeRenderResult(ima->rr);
00192         ima->rr= NULL;
00193     }   
00194     
00195     GPU_free_image(ima);
00196     
00197     ima->ok= IMA_OK;
00198 }
00199 
00200 /* called by library too, do not free ima itself */
00201 void free_image(Image *ima)
00202 {
00203     int a;
00204 
00205     image_free_buffers(ima);
00206     if (ima->packedfile) {
00207         freePackedFile(ima->packedfile);
00208         ima->packedfile = NULL;
00209     }
00210     BKE_icon_delete(&ima->id);
00211     ima->id.icon_id = 0;
00212 
00213     BKE_previewimg_free(&ima->preview);
00214 
00215     for(a=0; a<IMA_MAX_RENDER_SLOT; a++) {
00216         if(ima->renders[a]) {
00217             RE_FreeRenderResult(ima->renders[a]);
00218             ima->renders[a]= NULL;
00219         }
00220     }
00221 }
00222 
00223 /* only image block itself */
00224 static Image *image_alloc(const char *name, short source, short type)
00225 {
00226     Image *ima;
00227     
00228     ima= alloc_libblock(&G.main->image, ID_IM, name);
00229     if(ima) {
00230         ima->ok= IMA_OK;
00231         
00232         ima->xrep= ima->yrep= 1;
00233         ima->aspx= ima->aspy= 1.0;
00234         ima->gen_x= 1024; ima->gen_y= 1024;
00235         ima->gen_type= 1;   /* no defines yet? */
00236         
00237         ima->source= source;
00238         ima->type= type;
00239     }
00240     return ima;
00241 }
00242 
00243 /* get the ibuf from an image cache, local use here only */
00244 static ImBuf *image_get_ibuf(Image *ima, int index, int frame)
00245 {
00246     /* this function is intended to be thread safe. with IMA_NO_INDEX this
00247      * should be OK, but when iterating over the list this is more tricky
00248      * */
00249     if(index==IMA_NO_INDEX)
00250         return ima->ibufs.first;
00251     else {
00252         ImBuf *ibuf;
00253 
00254         index= IMA_MAKE_INDEX(frame, index);
00255         for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next)
00256             if(ibuf->index==index)
00257                 return ibuf;
00258 
00259         return NULL;
00260     }
00261 }
00262 
00263 /* no ima->ibuf anymore, but listbase */
00264 static void image_remove_ibuf(Image *ima, ImBuf *ibuf)
00265 {
00266     if(ibuf) {
00267         BLI_remlink(&ima->ibufs, ibuf);
00268         IMB_freeImBuf(ibuf);
00269     }
00270 }
00271 
00272 
00273 /* no ima->ibuf anymore, but listbase */
00274 static void image_assign_ibuf(Image *ima, ImBuf *ibuf, int index, int frame)
00275 {
00276     if(ibuf) {
00277         ImBuf *link;
00278         
00279         if(index!=IMA_NO_INDEX)
00280             index= IMA_MAKE_INDEX(frame, index);
00281         
00282         /* insert based on index */
00283         for(link= ima->ibufs.first; link; link= link->next)
00284             if(link->index>=index)
00285                 break;
00286 
00287         ibuf->index= index;
00288         if(ima->flag & IMA_CM_PREDIVIDE)
00289             ibuf->flags |= IB_cm_predivide;
00290         else
00291             ibuf->flags &= ~IB_cm_predivide;
00292 
00293         /* this function accepts link==NULL */
00294         BLI_insertlinkbefore(&ima->ibufs, link, ibuf);
00295 
00296         /* now we don't want copies? */
00297         if(link && ibuf->index==link->index)
00298             image_remove_ibuf(ima, link);
00299     }
00300 }
00301 
00302 /* empty image block, of similar type and filename */
00303 Image *copy_image(Image *ima)
00304 {
00305     Image *nima= image_alloc(ima->id.name+2, ima->source, ima->type);
00306 
00307     BLI_strncpy(nima->name, ima->name, sizeof(ima->name));
00308 
00309     nima->flag= ima->flag;
00310     nima->tpageflag= ima->tpageflag;
00311     
00312     nima->gen_x= ima->gen_x;
00313     nima->gen_y= ima->gen_y;
00314     nima->gen_type= ima->gen_type;
00315 
00316     nima->animspeed= ima->animspeed;
00317 
00318     nima->aspx= ima->aspx;
00319     nima->aspy= ima->aspy;
00320 
00321     return nima;
00322 }
00323 
00324 static void extern_local_image(Image *UNUSED(ima))
00325 {
00326     /* Nothing to do: images don't link to other IDs. This function exists to
00327        match id_make_local pattern. */
00328 }
00329 
00330 void make_local_image(struct Image *ima)
00331 {
00332     Main *bmain= G.main;
00333     Tex *tex;
00334     Brush *brush;
00335     Mesh *me;
00336     int is_local= FALSE, is_lib= FALSE;
00337 
00338     /* - only lib users: do nothing
00339      * - only local users: set flag
00340      * - mixed: make copy
00341      */
00342 
00343     if(ima->id.lib==NULL) return;
00344 
00345     /* Can't take short cut here: must check meshes at least because of bogus
00346        texface ID refs. - z0r */
00347 #if 0
00348     if(ima->id.us==1) {
00349         id_clear_lib_data(bmain, &ima->id);
00350         extern_local_image(ima);
00351         return;
00352     }
00353 #endif
00354 
00355     for(tex= bmain->tex.first; tex; tex= tex->id.next) {
00356         if(tex->ima == ima) {
00357             if(tex->id.lib) is_lib= TRUE;
00358             else is_local= TRUE;
00359         }
00360     }
00361     for(brush= bmain->brush.first; brush; brush= brush->id.next) {
00362         if(brush->clone.image == ima) {
00363             if(brush->id.lib) is_lib= TRUE;
00364             else is_local= TRUE;
00365         }
00366     }
00367     for(me= bmain->mesh.first; me; me= me->id.next) {
00368         if(me->mtface) {
00369             MTFace *tface;
00370             int a, i;
00371 
00372             for(i=0; i<me->fdata.totlayer; i++) {
00373                 if(me->fdata.layers[i].type == CD_MTFACE) {
00374                     tface= (MTFace*)me->fdata.layers[i].data;
00375 
00376                     for(a=0; a<me->totface; a++, tface++) {
00377                         if(tface->tpage == ima) {
00378                             if(me->id.lib) is_lib= TRUE;
00379                             else is_local= TRUE;
00380                         }
00381                     }
00382                 }
00383             }
00384         }
00385     }
00386 
00387     if(is_local && is_lib == FALSE) {
00388         id_clear_lib_data(bmain, &ima->id);
00389         extern_local_image(ima);
00390     }
00391     else if(is_local && is_lib) {
00392         Image *ima_new= copy_image(ima);
00393 
00394         ima_new->id.us= 0;
00395 
00396         /* Remap paths of new ID using old library as base. */
00397         BKE_id_lib_local_paths(bmain, ima->id.lib, &ima_new->id);
00398 
00399         tex= bmain->tex.first;
00400         while(tex) {
00401             if(tex->id.lib==NULL) {
00402                 if(tex->ima==ima) {
00403                     tex->ima = ima_new;
00404                     ima_new->id.us++;
00405                     ima->id.us--;
00406                 }
00407             }
00408             tex= tex->id.next;
00409         }
00410         brush= bmain->brush.first;
00411         while(brush) {
00412             if(brush->id.lib==NULL) {
00413                 if(brush->clone.image==ima) {
00414                     brush->clone.image = ima_new;
00415                     ima_new->id.us++;
00416                     ima->id.us--;
00417                 }
00418             }
00419             brush= brush->id.next;
00420         }
00421         /* Transfer references in texfaces. Texfaces don't add to image ID
00422            user count *unless* there are no other users. See
00423            readfile.c:lib_link_mtface. */
00424         me= bmain->mesh.first;
00425         while(me) {
00426             if(me->mtface) {
00427                 MTFace *tface;
00428                 int a, i;
00429 
00430                 for(i=0; i<me->fdata.totlayer; i++) {
00431                     if(me->fdata.layers[i].type == CD_MTFACE) {
00432                         tface= (MTFace*)me->fdata.layers[i].data;
00433 
00434                         for(a=0; a<me->totface; a++, tface++) { 
00435                             if(tface->tpage == ima) {
00436                                 tface->tpage = ima_new;
00437                                 if(ima_new->id.us == 0) {
00438                                     tface->tpage->id.us= 1;
00439                                 }
00440                                 id_lib_extern((ID*)ima_new);
00441                             }
00442                         }
00443                     }
00444                 }
00445             }
00446             me= me->id.next;
00447         }
00448     }
00449 }
00450 
00451 void BKE_image_merge(Image *dest, Image *source)
00452 {
00453     ImBuf *ibuf;
00454     
00455     /* sanity check */
00456     if(dest && source && dest!=source) {
00457     
00458         while((ibuf= source->ibufs.first)) {
00459             BLI_remlink(&source->ibufs, ibuf);
00460             image_assign_ibuf(dest, ibuf, IMA_INDEX_PASS(ibuf->index), IMA_INDEX_FRAME(ibuf->index));
00461         }
00462         
00463         free_libblock(&G.main->image, source);
00464     }
00465 }
00466 
00467 
00468 /* checks if image was already loaded, then returns same image */
00469 /* otherwise creates new. */
00470 /* does not load ibuf itself */
00471 /* pass on optional frame for #name images */
00472 Image *BKE_add_image_file(const char *name)
00473 {
00474     Image *ima;
00475     int file, len;
00476     const char *libname;
00477     char str[FILE_MAX], strtest[FILE_MAX];
00478     
00479     BLI_strncpy(str, name, sizeof(str));
00480     BLI_path_abs(str, G.main->name);
00481     
00482     /* exists? */
00483     file= open(str, O_BINARY|O_RDONLY);
00484     if(file== -1) return NULL;
00485     close(file);
00486     
00487     /* first search an identical image */
00488     for(ima= G.main->image.first; ima; ima= ima->id.next) {
00489         if(ima->source!=IMA_SRC_VIEWER && ima->source!=IMA_SRC_GENERATED) {
00490             BLI_strncpy(strtest, ima->name, sizeof(ima->name));
00491             BLI_path_abs(strtest, G.main->name);
00492             
00493             if( strcmp(strtest, str)==0 ) {
00494                 if(ima->anim==NULL || ima->id.us==0) {
00495                     BLI_strncpy(ima->name, name, sizeof(ima->name));    /* for stringcode */
00496                     ima->id.us++;                                       /* officially should not, it doesn't link here! */
00497                     if(ima->ok==0)
00498                         ima->ok= IMA_OK;
00499             /* RETURN! */
00500                     return ima;
00501                 }
00502             }
00503         }
00504     }
00505     /* add new image */
00506     
00507     /* create a short library name */
00508     len= strlen(name);
00509     
00510     while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
00511     libname= name+len;
00512     
00513     ima= image_alloc(libname, IMA_SRC_FILE, IMA_TYPE_IMAGE);
00514     BLI_strncpy(ima->name, name, sizeof(ima->name));
00515     
00516     if(BLI_testextensie_array(name, imb_ext_movie))
00517         ima->source= IMA_SRC_MOVIE;
00518     
00519     return ima;
00520 }
00521 
00522 static ImBuf *add_ibuf_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
00523 {
00524     ImBuf *ibuf;
00525     unsigned char *rect= NULL;
00526     float *rect_float= NULL;
00527     
00528     if (floatbuf) {
00529         ibuf= IMB_allocImBuf(width, height, depth, IB_rectfloat);
00530         rect_float= (float*)ibuf->rect_float;
00531     }
00532     else {
00533         ibuf= IMB_allocImBuf(width, height, depth, IB_rect);
00534         rect= (unsigned char*)ibuf->rect;
00535     }
00536     
00537     BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
00538     ibuf->userflags |= IB_BITMAPDIRTY;
00539     
00540     switch(uvtestgrid) {
00541     case 1:
00542         BKE_image_buf_fill_checker(rect, rect_float, width, height);
00543         break;
00544     case 2:
00545         BKE_image_buf_fill_checker_color(rect, rect_float, width, height);
00546         break;
00547     default:
00548         BKE_image_buf_fill_color(rect, rect_float, width, height, color);
00549     }
00550 
00551     return ibuf;
00552 }
00553 
00554 /* adds new image block, creates ImBuf and initializes color */
00555 Image *BKE_add_image_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
00556 {
00557     /* on save, type is changed to FILE in editsima.c */
00558     Image *ima= image_alloc(name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
00559     
00560     if (ima) {
00561         ImBuf *ibuf;
00562         
00563         /* BLI_strncpy(ima->name, name, FILE_MAX); */ /* dont do this, this writes in ain invalid filepath! */
00564         ima->gen_x= width;
00565         ima->gen_y= height;
00566         ima->gen_type= uvtestgrid;
00567         ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
00568         
00569         ibuf= add_ibuf_size(width, height, ima->name, depth, floatbuf, uvtestgrid, color);
00570         image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
00571         
00572         ima->ok= IMA_OK_LOADED;
00573     }
00574 
00575     return ima;
00576 }
00577 
00578 /* creates an image image owns the imbuf passed */
00579 Image *BKE_add_image_imbuf(ImBuf *ibuf)
00580 {
00581     /* on save, type is changed to FILE in editsima.c */
00582     Image *ima;
00583 
00584     ima= image_alloc(BLI_path_basename(ibuf->name), IMA_SRC_FILE, IMA_TYPE_IMAGE);
00585 
00586     if (ima) {
00587         BLI_strncpy(ima->name, ibuf->name, FILE_MAX);
00588         image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
00589         ima->ok= IMA_OK_LOADED;
00590     }
00591 
00592     return ima;
00593 }
00594 
00595 /* packs rect from memory as PNG */
00596 void BKE_image_memorypack(Image *ima)
00597 {
00598     ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
00599     
00600     if(ibuf==NULL)
00601         return;
00602     if (ima->packedfile) {
00603         freePackedFile(ima->packedfile);
00604         ima->packedfile = NULL;
00605     }
00606     
00607     ibuf->ftype= PNG;
00608     ibuf->planes= R_IMF_PLANES_RGBA;
00609     
00610     IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem);
00611     if(ibuf->encodedbuffer==NULL) {
00612         printf("memory save for pack error\n");
00613     }
00614     else {
00615         PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
00616         
00617         pf->data = ibuf->encodedbuffer;
00618         pf->size = ibuf->encodedsize;
00619         ima->packedfile= pf;
00620         ibuf->encodedbuffer= NULL;
00621         ibuf->encodedsize= 0;
00622         ibuf->userflags &= ~IB_BITMAPDIRTY;
00623         
00624         if(ima->source==IMA_SRC_GENERATED) {
00625             ima->source= IMA_SRC_FILE;
00626             ima->type= IMA_TYPE_IMAGE;
00627         }
00628     }
00629 }
00630 
00631 void tag_image_time(Image *ima)
00632 {
00633     if (ima)
00634         ima->lastused = (int)PIL_check_seconds_timer();
00635 }
00636 
00637 #if 0
00638 static void tag_all_images_time() 
00639 {
00640     Image *ima;
00641     int ctime = (int)PIL_check_seconds_timer();
00642 
00643     ima= G.main->image.first;
00644     while(ima) {
00645         if(ima->bindcode || ima->repbind || ima->ibufs.first) {
00646             ima->lastused = ctime;
00647         }
00648     }
00649 }
00650 #endif
00651 
00652 void free_old_images(void)
00653 {
00654     Image *ima;
00655     static int lasttime = 0;
00656     int ctime = (int)PIL_check_seconds_timer();
00657     
00658     /* 
00659        Run garbage collector once for every collecting period of time 
00660        if textimeout is 0, that's the option to NOT run the collector
00661     */
00662     if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
00663         return;
00664 
00665     /* of course not! */
00666     if (G.rendering)
00667         return;
00668     
00669     lasttime = ctime;
00670 
00671     ima= G.main->image.first;
00672     while(ima) {
00673         if((ima->flag & IMA_NOCOLLECT)==0 && ctime - ima->lastused > U.textimeout) {
00674             /*
00675                If it's in GL memory, deallocate and set time tag to current time
00676                This gives textures a "second chance" to be used before dying.
00677             */
00678             if(ima->bindcode || ima->repbind) {
00679                 GPU_free_image(ima);
00680                 ima->lastused = ctime;
00681             }
00682             /* Otherwise, just kill the buffers */
00683             else if (ima->ibufs.first) {
00684                 image_free_buffers(ima);
00685             }
00686         }
00687         ima = ima->id.next;
00688     }
00689 }
00690 
00691 static uintptr_t image_mem_size(Image *ima)
00692 {
00693     ImBuf *ibuf, *ibufm;
00694     int level;
00695     uintptr_t size = 0;
00696 
00697     size= 0;
00698     
00699     /* viewers have memory depending on other rules, has no valid rect pointer */
00700     if(ima->source==IMA_SRC_VIEWER)
00701         return 0;
00702     
00703     for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
00704         if(ibuf->rect) size += MEM_allocN_len(ibuf->rect);
00705         else if(ibuf->rect_float) size += MEM_allocN_len(ibuf->rect_float);
00706 
00707         for(level=0; level<IB_MIPMAP_LEVELS; level++) {
00708             ibufm= ibuf->mipmap[level];
00709             if(ibufm) {
00710                 if(ibufm->rect) size += MEM_allocN_len(ibufm->rect);
00711                 else if(ibufm->rect_float) size += MEM_allocN_len(ibufm->rect_float);
00712             }
00713         }
00714     }
00715 
00716     return size;
00717 }
00718 
00719 void BKE_image_print_memlist(void)
00720 {
00721     Image *ima;
00722     uintptr_t size, totsize= 0;
00723 
00724     for(ima= G.main->image.first; ima; ima= ima->id.next)
00725         totsize += image_mem_size(ima);
00726 
00727     printf("\ntotal image memory len: %.3f MB\n", (double)totsize/(double)(1024*1024));
00728 
00729     for(ima= G.main->image.first; ima; ima= ima->id.next) {
00730         size= image_mem_size(ima);
00731 
00732         if(size)
00733             printf("%s len: %.3f MB\n", ima->id.name+2, (double)size/(double)(1024*1024));
00734     }
00735 }
00736 
00737 void BKE_image_free_all_textures(void)
00738 {
00739     Tex *tex;
00740     Image *ima;
00741     /* unsigned int totsize= 0; */
00742     
00743     for(ima= G.main->image.first; ima; ima= ima->id.next)
00744         ima->id.flag &= ~LIB_DOIT;
00745     
00746     for(tex= G.main->tex.first; tex; tex= tex->id.next)
00747         if(tex->ima)
00748             tex->ima->id.flag |= LIB_DOIT;
00749     
00750     for(ima= G.main->image.first; ima; ima= ima->id.next) {
00751         if(ima->ibufs.first && (ima->id.flag & LIB_DOIT)) {
00752             ImBuf *ibuf;
00753             
00754             for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
00755                 /* escape when image is painted on */
00756                 if(ibuf->userflags & IB_BITMAPDIRTY)
00757                     break;
00758                 
00759                 /* if(ibuf->mipmap[0]) 
00760                     totsize+= 1.33*ibuf->x*ibuf->y*4;
00761                 else
00762                     totsize+= ibuf->x*ibuf->y*4;*/
00763                 
00764             }
00765             if(ibuf==NULL)
00766                 image_free_buffers(ima);
00767         }
00768     }
00769     /* printf("freed total %d MB\n", totsize/(1024*1024)); */
00770 }
00771 
00772 /* except_frame is weak, only works for seqs without offset... */
00773 void BKE_image_free_anim_ibufs(Image *ima, int except_frame)
00774 {
00775     ImBuf *ibuf, *nbuf;
00776 
00777     for(ibuf= ima->ibufs.first; ibuf; ibuf= nbuf) {
00778         nbuf= ibuf->next;
00779         if(ibuf->userflags & IB_BITMAPDIRTY)
00780             continue;
00781         if(ibuf->index==IMA_NO_INDEX)
00782             continue;
00783         if(except_frame!=IMA_INDEX_FRAME(ibuf->index)) {
00784             BLI_remlink(&ima->ibufs, ibuf);
00785             
00786             if (ibuf->userdata) {
00787                 MEM_freeN(ibuf->userdata);
00788                 ibuf->userdata = NULL;
00789             }
00790             IMB_freeImBuf(ibuf);
00791         }                   
00792     }
00793 }
00794 
00795 void BKE_image_all_free_anim_ibufs(int cfra)
00796 {
00797     Image *ima;
00798     
00799     for(ima= G.main->image.first; ima; ima= ima->id.next)
00800         if(ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE))
00801             BKE_image_free_anim_ibufs(ima, cfra);
00802 }
00803 
00804 
00805 /* *********** READ AND WRITE ************** */
00806 
00807 int BKE_imtype_to_ftype(const char imtype)
00808 {
00809     if(imtype==R_IMF_IMTYPE_TARGA)
00810         return TGA;
00811     else if(imtype==R_IMF_IMTYPE_RAWTGA)
00812         return RAWTGA;
00813     else if(imtype== R_IMF_IMTYPE_IRIS) 
00814         return IMAGIC;
00815 #ifdef WITH_HDR
00816     else if (imtype==R_IMF_IMTYPE_RADHDR)
00817         return RADHDR;
00818 #endif
00819     else if (imtype==R_IMF_IMTYPE_PNG)
00820         return PNG;
00821 #ifdef WITH_DDS
00822     else if (imtype==R_IMF_IMTYPE_DDS)
00823         return DDS;
00824 #endif
00825     else if (imtype==R_IMF_IMTYPE_BMP)
00826         return BMP;
00827 #ifdef WITH_TIFF
00828     else if (imtype==R_IMF_IMTYPE_TIFF)
00829         return TIF;
00830 #endif
00831     else if (imtype==R_IMF_IMTYPE_OPENEXR || imtype==R_IMF_IMTYPE_MULTILAYER)
00832         return OPENEXR;
00833 #ifdef WITH_CINEON
00834     else if (imtype==R_IMF_IMTYPE_CINEON)
00835         return CINEON;
00836     else if (imtype==R_IMF_IMTYPE_DPX)
00837         return DPX;
00838 #endif
00839 #ifdef WITH_OPENJPEG
00840     else if(imtype==R_IMF_IMTYPE_JP2)
00841         return JP2;
00842 #endif
00843     else
00844         return JPG|90;
00845 }
00846 
00847 char BKE_ftype_to_imtype(const int ftype)
00848 {
00849     if(ftype==0)
00850         return R_IMF_IMTYPE_TARGA;
00851     else if(ftype == IMAGIC) 
00852         return R_IMF_IMTYPE_IRIS;
00853 #ifdef WITH_HDR
00854     else if (ftype & RADHDR)
00855         return R_IMF_IMTYPE_RADHDR;
00856 #endif
00857     else if (ftype & PNG)
00858         return R_IMF_IMTYPE_PNG;
00859 #ifdef WITH_DDS
00860     else if (ftype & DDS)
00861         return R_IMF_IMTYPE_DDS;
00862 #endif
00863     else if (ftype & BMP)
00864         return R_IMF_IMTYPE_BMP;
00865 #ifdef WITH_TIFF
00866     else if (ftype & TIF)
00867         return R_IMF_IMTYPE_TIFF;
00868 #endif
00869     else if (ftype & OPENEXR)
00870         return R_IMF_IMTYPE_OPENEXR;
00871 #ifdef WITH_CINEON
00872     else if (ftype & CINEON)
00873         return R_IMF_IMTYPE_CINEON;
00874     else if (ftype & DPX)
00875         return R_IMF_IMTYPE_DPX;
00876 #endif
00877     else if (ftype & TGA)
00878         return R_IMF_IMTYPE_TARGA;
00879     else if(ftype & RAWTGA)
00880         return R_IMF_IMTYPE_RAWTGA;
00881 #ifdef WITH_OPENJPEG
00882     else if(ftype & JP2)
00883         return R_IMF_IMTYPE_JP2;
00884 #endif
00885     else
00886         return R_IMF_IMTYPE_JPEG90;
00887 }
00888 
00889 
00890 int BKE_imtype_is_movie(const char imtype)
00891 {
00892     switch(imtype) {
00893     case R_IMF_IMTYPE_AVIRAW:
00894     case R_IMF_IMTYPE_AVIJPEG:
00895     case R_IMF_IMTYPE_AVICODEC:
00896     case R_IMF_IMTYPE_QUICKTIME:
00897     case R_IMF_IMTYPE_FFMPEG:
00898     case R_IMF_IMTYPE_H264:
00899     case R_IMF_IMTYPE_THEORA:
00900     case R_IMF_IMTYPE_XVID:
00901     case R_IMF_IMTYPE_FRAMESERVER:
00902             return 1;
00903     }
00904     return 0;
00905 }
00906 
00907 int BKE_imtype_supports_zbuf(const char imtype)
00908 {
00909     switch(imtype) {
00910     case R_IMF_IMTYPE_IRIZ:
00911     case R_IMF_IMTYPE_OPENEXR: /* but not R_IMF_IMTYPE_MULTILAYER */
00912             return 1;
00913     }
00914     return 0;
00915 }
00916 
00917 int BKE_imtype_supports_compress(const char imtype)
00918 {
00919     switch(imtype) {
00920     case R_IMF_IMTYPE_PNG:
00921             return 1;
00922     }
00923     return 0;
00924 }
00925 
00926 int BKE_imtype_supports_quality(const char imtype)
00927 {
00928     switch(imtype) {
00929     case R_IMF_IMTYPE_JPEG90:
00930     case R_IMF_IMTYPE_JP2:
00931     case R_IMF_IMTYPE_AVIJPEG:
00932             return 1;
00933     }
00934     return 0;
00935 }
00936 
00937 char BKE_imtype_valid_channels(const char imtype)
00938 {
00939     char chan_flag= IMA_CHAN_FLAG_RGB; /* assume all support rgb */
00940 
00941     /* alpha */
00942     switch(imtype) {
00943     case R_IMF_IMTYPE_TARGA:
00944     case R_IMF_IMTYPE_IRIS:
00945     case R_IMF_IMTYPE_PNG:
00946     /* case R_IMF_IMTYPE_BMP: */ /* read but not write */
00947     case R_IMF_IMTYPE_RADHDR:
00948     case R_IMF_IMTYPE_TIFF:
00949     case R_IMF_IMTYPE_OPENEXR:
00950     case R_IMF_IMTYPE_MULTILAYER:
00951     case R_IMF_IMTYPE_DDS:
00952     case R_IMF_IMTYPE_JP2:
00953     case R_IMF_IMTYPE_QUICKTIME:
00954             chan_flag |= IMA_CHAN_FLAG_ALPHA;
00955     }
00956 
00957     /* bw */
00958     switch(imtype) {
00959     case R_IMF_IMTYPE_PNG:
00960     case R_IMF_IMTYPE_JPEG90:
00961     case R_IMF_IMTYPE_TARGA:
00962     case R_IMF_IMTYPE_RAWTGA:
00963     case R_IMF_IMTYPE_TIFF:
00964     case R_IMF_IMTYPE_IRIS:
00965             chan_flag |= IMA_CHAN_FLAG_BW;
00966     }
00967 
00968     return chan_flag;
00969 }
00970 
00971 char BKE_imtype_valid_depths(const char imtype)
00972 {
00973     switch (imtype) {
00974     case R_IMF_IMTYPE_RADHDR:
00975         return R_IMF_CHAN_DEPTH_32;
00976     case R_IMF_IMTYPE_TIFF:
00977         return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_16;
00978     case R_IMF_IMTYPE_OPENEXR:
00979         return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
00980     case R_IMF_IMTYPE_MULTILAYER:
00981         return R_IMF_CHAN_DEPTH_32;
00982     /* eeh, cineone does some strange 10bits per channel */
00983     case R_IMF_IMTYPE_DPX:
00984     case R_IMF_IMTYPE_CINEON:
00985         return R_IMF_CHAN_DEPTH_12;
00986     case R_IMF_IMTYPE_JP2:
00987         return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
00988     /* most formats are 8bit only */
00989     default:
00990         return R_IMF_CHAN_DEPTH_8;
00991     }
00992 }
00993 
00994 
00995 /* string is from command line --render-format arg, keep in sync with
00996  * creator.c help info */
00997 char BKE_imtype_from_arg(const char *imtype_arg)
00998 {
00999     if      (!strcmp(imtype_arg,"TGA")) return R_IMF_IMTYPE_TARGA;
01000     else if (!strcmp(imtype_arg,"IRIS")) return R_IMF_IMTYPE_IRIS;
01001 #ifdef WITH_DDS
01002     else if (!strcmp(imtype_arg,"DDS")) return R_IMF_IMTYPE_DDS;
01003 #endif
01004     else if (!strcmp(imtype_arg,"JPEG")) return R_IMF_IMTYPE_JPEG90;
01005     else if (!strcmp(imtype_arg,"IRIZ")) return R_IMF_IMTYPE_IRIZ;
01006     else if (!strcmp(imtype_arg,"RAWTGA")) return R_IMF_IMTYPE_RAWTGA;
01007     else if (!strcmp(imtype_arg,"AVIRAW")) return R_IMF_IMTYPE_AVIRAW;
01008     else if (!strcmp(imtype_arg,"AVIJPEG")) return R_IMF_IMTYPE_AVIJPEG;
01009     else if (!strcmp(imtype_arg,"PNG")) return R_IMF_IMTYPE_PNG;
01010     else if (!strcmp(imtype_arg,"AVICODEC")) return R_IMF_IMTYPE_AVICODEC;
01011     else if (!strcmp(imtype_arg,"QUICKTIME")) return R_IMF_IMTYPE_QUICKTIME;
01012     else if (!strcmp(imtype_arg,"BMP")) return R_IMF_IMTYPE_BMP;
01013 #ifdef WITH_HDR
01014     else if (!strcmp(imtype_arg,"HDR")) return R_IMF_IMTYPE_RADHDR;
01015 #endif
01016 #ifdef WITH_TIFF
01017     else if (!strcmp(imtype_arg,"TIFF")) return R_IMF_IMTYPE_TIFF;
01018 #endif
01019 #ifdef WITH_OPENEXR
01020     else if (!strcmp(imtype_arg,"EXR")) return R_IMF_IMTYPE_OPENEXR;
01021     else if (!strcmp(imtype_arg,"MULTILAYER")) return R_IMF_IMTYPE_MULTILAYER;
01022 #endif
01023     else if (!strcmp(imtype_arg,"MPEG")) return R_IMF_IMTYPE_FFMPEG;
01024     else if (!strcmp(imtype_arg,"FRAMESERVER")) return R_IMF_IMTYPE_FRAMESERVER;
01025 #ifdef WITH_CINEON
01026     else if (!strcmp(imtype_arg,"CINEON")) return R_IMF_IMTYPE_CINEON;
01027     else if (!strcmp(imtype_arg,"DPX")) return R_IMF_IMTYPE_DPX;
01028 #endif
01029 #ifdef WITH_OPENJPEG
01030     else if (!strcmp(imtype_arg,"JP2")) return R_IMF_IMTYPE_JP2;
01031 #endif
01032     else return R_IMF_IMTYPE_INVALID;
01033 }
01034 
01035 int BKE_add_image_extension(char *string, const char imtype)
01036 {
01037     const char *extension= NULL;
01038     
01039     if(imtype== R_IMF_IMTYPE_IRIS) {
01040         if(!BLI_testextensie(string, ".rgb"))
01041             extension= ".rgb";
01042     }
01043     else if(imtype==R_IMF_IMTYPE_IRIZ) {
01044         if(!BLI_testextensie(string, ".rgb"))
01045             extension= ".rgb";
01046     }
01047 #ifdef WITH_HDR
01048     else if(imtype==R_IMF_IMTYPE_RADHDR) {
01049         if(!BLI_testextensie(string, ".hdr"))
01050             extension= ".hdr";
01051     }
01052 #endif
01053     else if (ELEM5(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_THEORA, R_IMF_IMTYPE_XVID)) {
01054         if(!BLI_testextensie(string, ".png"))
01055             extension= ".png";
01056     }
01057 #ifdef WITH_DDS
01058     else if(imtype==R_IMF_IMTYPE_DDS) {
01059         if(!BLI_testextensie(string, ".dds"))
01060             extension= ".dds";
01061     }
01062 #endif
01063     else if(imtype==R_IMF_IMTYPE_RAWTGA) {
01064         if(!BLI_testextensie(string, ".tga"))
01065             extension= ".tga";
01066     }
01067     else if(imtype==R_IMF_IMTYPE_BMP) {
01068         if(!BLI_testextensie(string, ".bmp"))
01069             extension= ".bmp";
01070     }
01071 #ifdef WITH_TIFF
01072     else if(imtype==R_IMF_IMTYPE_TIFF) {
01073         if(!BLI_testextensie(string, ".tif") && 
01074             !BLI_testextensie(string, ".tiff")) extension= ".tif";
01075     }
01076 #endif
01077 #ifdef WITH_OPENEXR
01078     else if( ELEM(imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
01079         if(!BLI_testextensie(string, ".exr"))
01080             extension= ".exr";
01081     }
01082 #endif
01083 #ifdef WITH_CINEON
01084     else if(imtype==R_IMF_IMTYPE_CINEON){
01085         if (!BLI_testextensie(string, ".cin"))
01086             extension= ".cin";
01087     }
01088     else if(imtype==R_IMF_IMTYPE_DPX){
01089         if (!BLI_testextensie(string, ".dpx"))
01090             extension= ".dpx";
01091     }
01092 #endif
01093     else if(imtype==R_IMF_IMTYPE_TARGA) {
01094         if(!BLI_testextensie(string, ".tga"))
01095             extension= ".tga";
01096     }
01097 #ifdef WITH_OPENJPEG
01098     else if(imtype==R_IMF_IMTYPE_JP2) {
01099         if(!BLI_testextensie(string, ".jp2"))
01100             extension= ".jp2";
01101     }
01102 #endif
01103     else { //   R_IMF_IMTYPE_AVICODEC, R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90, R_IMF_IMTYPE_QUICKTIME etc
01104         if(!( BLI_testextensie(string, ".jpg") || BLI_testextensie(string, ".jpeg")))
01105             extension= ".jpg";
01106     }
01107 
01108     if(extension) {
01109         /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */
01110         /* remove any other known image extension */
01111         if(BLI_testextensie_array(string, imb_ext_image)
01112                   || (G.have_quicktime && BLI_testextensie_array(string, imb_ext_image_qt))) {
01113             return BLI_replace_extension(string, FILE_MAX, extension);
01114         } else {
01115             return BLI_ensure_extension(string, FILE_MAX, extension);
01116             return TRUE;
01117         }
01118         
01119     }
01120     else {
01121         return FALSE;
01122     }
01123 }
01124 
01125 /* could allow access externally - 512 is for long names, 64 is for id names */
01126 typedef struct StampData {
01127     char    file[512];
01128     char    note[512];
01129     char    date[512];
01130     char    marker[512];
01131     char    time[512];
01132     char    frame[512];
01133     char    camera[64];
01134     char    cameralens[64];
01135     char    scene[64];
01136     char    strip[64];
01137     char    rendertime[64];
01138 } StampData;
01139 
01140 static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int do_prefix)
01141 {
01142     char text[256];
01143     struct tm *tl;
01144     time_t t;
01145 
01146     if (scene->r.stamp & R_STAMP_FILENAME) {
01147         BLI_snprintf(stamp_data->file, sizeof(stamp_data->file), do_prefix ? "File %s":"%s", G.relbase_valid ? G.main->name:"<untitled>");
01148     } else {
01149         stamp_data->file[0] = '\0';
01150     }
01151     
01152     if (scene->r.stamp & R_STAMP_NOTE) {
01153         /* Never do prefix for Note */
01154         BLI_snprintf(stamp_data->note, sizeof(stamp_data->note), "%s", scene->r.stamp_udata);
01155     } else {
01156         stamp_data->note[0] = '\0';
01157     }
01158     
01159     if (scene->r.stamp & R_STAMP_DATE) {
01160         t = time(NULL);
01161         tl = localtime(&t);
01162         BLI_snprintf(text, sizeof(text), "%04d/%02d/%02d %02d:%02d:%02d", tl->tm_year+1900, tl->tm_mon+1, tl->tm_mday, tl->tm_hour, tl->tm_min, tl->tm_sec);
01163         BLI_snprintf(stamp_data->date, sizeof(stamp_data->date), do_prefix ? "Date %s":"%s", text);
01164     } else {
01165         stamp_data->date[0] = '\0';
01166     }
01167     
01168     if (scene->r.stamp & R_STAMP_MARKER) {
01169         char *name = scene_find_last_marker_name(scene, CFRA);
01170 
01171         if (name)   BLI_strncpy(text, name, sizeof(text));
01172         else        BLI_strncpy(text, "<none>", sizeof(text));
01173 
01174         BLI_snprintf(stamp_data->marker, sizeof(stamp_data->marker), do_prefix ? "Marker %s":"%s", text);
01175     } else {
01176         stamp_data->marker[0] = '\0';
01177     }
01178     
01179     if (scene->r.stamp & R_STAMP_TIME) {
01180         int f = (int)(scene->r.cfra % scene->r.frs_sec);
01181         int s = (int)(scene->r.cfra / scene->r.frs_sec);
01182         int h= 0;
01183         int m= 0;
01184 
01185         if (s) {
01186             m = (int)(s / 60);
01187             s %= 60;
01188 
01189             if (m) {
01190                 h = (int)(m / 60);
01191                 m %= 60;
01192             }
01193         }
01194 
01195         if (scene->r.frs_sec < 100)
01196             BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%02d", h, m, s, f);
01197         else
01198             BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%03d", h, m, s, f);
01199 
01200         BLI_snprintf(stamp_data->time, sizeof(stamp_data->time), do_prefix ? "Time %s":"%s", text);
01201     } else {
01202         stamp_data->time[0] = '\0';
01203     }
01204     
01205     if (scene->r.stamp & R_STAMP_FRAME) {
01206         char fmtstr[32];
01207         int digits= 1;
01208         
01209         if(scene->r.efra>9)
01210             digits= 1 + (int) log10(scene->r.efra);
01211 
01212         BLI_snprintf(fmtstr, sizeof(fmtstr), do_prefix ? "Frame %%0%di":"%%0%di", digits);
01213         BLI_snprintf (stamp_data->frame, sizeof(stamp_data->frame), fmtstr, scene->r.cfra);
01214     } else {
01215         stamp_data->frame[0] = '\0';
01216     }
01217 
01218     if (scene->r.stamp & R_STAMP_CAMERA) {
01219         BLI_snprintf(stamp_data->camera, sizeof(stamp_data->camera), do_prefix ? "Camera %s":"%s", camera ? camera->id.name+2 : "<none>");
01220     } else {
01221         stamp_data->camera[0] = '\0';
01222     }
01223 
01224     if (scene->r.stamp & R_STAMP_CAMERALENS) {
01225         if (camera && camera->type == OB_CAMERA) {
01226             BLI_snprintf(text, sizeof(text), "%.2f", ((Camera *)camera->data)->lens);
01227         }
01228         else        BLI_strncpy(text, "<none>", sizeof(text));
01229 
01230         BLI_snprintf(stamp_data->cameralens, sizeof(stamp_data->cameralens), do_prefix ? "Lens %s":"%s", text);
01231     } else {
01232         stamp_data->cameralens[0] = '\0';
01233     }
01234 
01235     if (scene->r.stamp & R_STAMP_SCENE) {
01236         BLI_snprintf(stamp_data->scene, sizeof(stamp_data->scene), do_prefix ? "Scene %s":"%s", scene->id.name+2);
01237     } else {
01238         stamp_data->scene[0] = '\0';
01239     }
01240     
01241     if (scene->r.stamp & R_STAMP_SEQSTRIP) {
01242         Sequence *seq= seq_foreground_frame_get(scene, scene->r.cfra);
01243     
01244         if (seq)    BLI_strncpy(text, seq->name+2, sizeof(text));
01245         else        BLI_strncpy(text, "<none>", sizeof(text));
01246 
01247         BLI_snprintf(stamp_data->strip, sizeof(stamp_data->strip), do_prefix ? "Strip %s":"%s", text);
01248     } else {
01249         stamp_data->strip[0] = '\0';
01250     }
01251 
01252     {
01253         Render *re= RE_GetRender(scene->id.name);
01254         RenderStats *stats= re ? RE_GetStats(re):NULL;
01255 
01256         if (stats && (scene->r.stamp & R_STAMP_RENDERTIME)) {
01257             BLI_timestr(stats->lastframetime, text);
01258 
01259             BLI_snprintf(stamp_data->rendertime, sizeof(stamp_data->rendertime), do_prefix ? "RenderTime %s":"%s", text);
01260         } else {
01261             stamp_data->rendertime[0] = '\0';
01262         }
01263     }
01264 }
01265 
01266 void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rectf, int width, int height, int channels)
01267 {
01268     struct StampData stamp_data;
01269     float w, h, pad;
01270     int x, y, y_ofs;
01271     float h_fixed;
01272     const int mono= blf_mono_font_render; // XXX
01273 
01274 #define BUFF_MARGIN_X 2
01275 #define BUFF_MARGIN_Y 1
01276 
01277     if (!rect && !rectf)
01278         return;
01279     
01280     stampdata(scene, camera, &stamp_data, 1);
01281 
01282     /* TODO, do_versions */
01283     if(scene->r.stamp_font_id < 8)
01284         scene->r.stamp_font_id= 12;
01285 
01286     /* set before return */
01287     BLF_size(mono, scene->r.stamp_font_id, 72);
01288     
01289     BLF_buffer(mono, rectf, rect, width, height, channels);
01290     BLF_buffer_col(mono, scene->r.fg_stamp[0], scene->r.fg_stamp[1], scene->r.fg_stamp[2], 1.0);
01291     pad= BLF_width_max(mono);
01292 
01293     /* use 'h_fixed' rather than 'h', aligns better */
01294     h_fixed= BLF_height_max(mono);
01295     y_ofs = -BLF_descender(mono);
01296 
01297     x= 0;
01298     y= height;
01299 
01300     if (stamp_data.file[0]) {
01301         /* Top left corner */
01302         BLF_width_and_height(mono, stamp_data.file, &w, &h); h= h_fixed;
01303         y -= h;
01304 
01305         /* also a little of space to the background. */
01306         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01307 
01308         /* and draw the text. */
01309         BLF_position(mono, x, y + y_ofs, 0.0);
01310         BLF_draw_buffer(mono, stamp_data.file);
01311 
01312         /* the extra pixel for background. */
01313         y -= BUFF_MARGIN_Y * 2;
01314     }
01315 
01316     /* Top left corner, below File */
01317     if (stamp_data.note[0]) {
01318         BLF_width_and_height(mono, stamp_data.note, &w, &h); h= h_fixed;
01319         y -= h;
01320 
01321         /* and space for background. */
01322         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01323 
01324         BLF_position(mono, x, y + y_ofs, 0.0);
01325         BLF_draw_buffer(mono, stamp_data.note);
01326 
01327         /* the extra pixel for background. */
01328         y -= BUFF_MARGIN_Y * 2;
01329     }
01330     
01331     /* Top left corner, below File (or Note) */
01332     if (stamp_data.date[0]) {
01333         BLF_width_and_height(mono, stamp_data.date, &w, &h); h= h_fixed;
01334         y -= h;
01335 
01336         /* and space for background. */
01337         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01338 
01339         BLF_position(mono, x, y + y_ofs, 0.0);
01340         BLF_draw_buffer(mono, stamp_data.date);
01341 
01342         /* the extra pixel for background. */
01343         y -= BUFF_MARGIN_Y * 2;
01344     }
01345 
01346     /* Top left corner, below File, Date or Note */
01347     if (stamp_data.rendertime[0]) {
01348         BLF_width_and_height(mono, stamp_data.rendertime, &w, &h); h= h_fixed;
01349         y -= h;
01350 
01351         /* and space for background. */
01352         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01353 
01354         BLF_position(mono, x, y + y_ofs, 0.0);
01355         BLF_draw_buffer(mono, stamp_data.rendertime);
01356     }
01357 
01358     x= 0;
01359     y= 0;
01360 
01361     /* Bottom left corner, leaving space for timing */
01362     if (stamp_data.marker[0]) {
01363         BLF_width_and_height(mono, stamp_data.marker, &w, &h); h= h_fixed;
01364 
01365         /* extra space for background. */
01366         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01367 
01368         /* and pad the text. */
01369         BLF_position(mono, x, y + y_ofs, 0.0);
01370         BLF_draw_buffer(mono, stamp_data.marker);
01371 
01372         /* space width. */
01373         x += w + pad;
01374     }
01375     
01376     /* Left bottom corner */
01377     if (stamp_data.time[0]) {
01378         BLF_width_and_height(mono, stamp_data.time, &w, &h); h= h_fixed;
01379 
01380         /* extra space for background */
01381         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01382 
01383         /* and pad the text. */
01384         BLF_position(mono, x, y + y_ofs, 0.0);
01385         BLF_draw_buffer(mono, stamp_data.time);
01386 
01387         /* space width. */
01388         x += w + pad;
01389     }
01390     
01391     if (stamp_data.frame[0]) {
01392         BLF_width_and_height(mono, stamp_data.frame, &w, &h); h= h_fixed;
01393 
01394         /* extra space for background. */
01395         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01396 
01397         /* and pad the text. */
01398         BLF_position(mono, x, y + y_ofs, 0.0);
01399         BLF_draw_buffer(mono, stamp_data.frame);
01400 
01401         /* space width. */
01402         x += w + pad;
01403     }
01404 
01405     if (stamp_data.camera[0]) {
01406         BLF_width_and_height(mono, stamp_data.camera, &w, &h); h= h_fixed;
01407 
01408         /* extra space for background. */
01409         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01410         BLF_position(mono, x, y + y_ofs, 0.0);
01411         BLF_draw_buffer(mono, stamp_data.camera);
01412 
01413         /* space width. */
01414         x += w + pad;
01415     }
01416 
01417     if (stamp_data.cameralens[0]) {
01418         BLF_width_and_height(mono, stamp_data.cameralens, &w, &h); h= h_fixed;
01419 
01420         /* extra space for background. */
01421         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01422         BLF_position(mono, x, y + y_ofs, 0.0);
01423         BLF_draw_buffer(mono, stamp_data.cameralens);
01424     }
01425     
01426     if (stamp_data.scene[0]) {
01427         BLF_width_and_height(mono, stamp_data.scene, &w, &h); h= h_fixed;
01428 
01429         /* Bottom right corner, with an extra space because blenfont is too strict! */
01430         x= width - w - 2;
01431 
01432         /* extra space for background. */
01433         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01434 
01435         /* and pad the text. */
01436         BLF_position(mono, x, y+y_ofs, 0.0);
01437         BLF_draw_buffer(mono, stamp_data.scene);
01438     }
01439     
01440     if (stamp_data.strip[0]) {
01441         BLF_width_and_height(mono, stamp_data.strip, &w, &h); h= h_fixed;
01442 
01443         /* Top right corner, with an extra space because blenfont is too strict! */
01444         x= width - w - pad;
01445         y= height - h;
01446 
01447         /* extra space for background. */
01448         buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
01449 
01450         BLF_position(mono, x, y + y_ofs, 0.0);
01451         BLF_draw_buffer(mono, stamp_data.strip);
01452     }
01453 
01454     /* cleanup the buffer. */
01455     BLF_buffer(mono, NULL, NULL, 0, 0, 0);
01456 
01457 #undef BUFF_MARGIN_X
01458 #undef BUFF_MARGIN_Y
01459 }
01460 
01461 void BKE_stamp_info(Scene *scene, Object *camera, struct ImBuf *ibuf)
01462 {
01463     struct StampData stamp_data;
01464 
01465     if (!ibuf)  return;
01466     
01467     /* fill all the data values, no prefix */
01468     stampdata(scene, camera, &stamp_data, 0);
01469     
01470     if (stamp_data.file[0])     IMB_metadata_change_field (ibuf, "File",        stamp_data.file);
01471     if (stamp_data.note[0])     IMB_metadata_change_field (ibuf, "Note",        stamp_data.note);
01472     if (stamp_data.date[0])     IMB_metadata_change_field (ibuf, "Date",        stamp_data.date);
01473     if (stamp_data.marker[0])   IMB_metadata_change_field (ibuf, "Marker",  stamp_data.marker);
01474     if (stamp_data.time[0])     IMB_metadata_change_field (ibuf, "Time",        stamp_data.time);
01475     if (stamp_data.frame[0])    IMB_metadata_change_field (ibuf, "Frame",   stamp_data.frame);
01476     if (stamp_data.camera[0])   IMB_metadata_change_field (ibuf, "Camera",  stamp_data.camera);
01477     if (stamp_data.cameralens[0]) IMB_metadata_change_field (ibuf, "Lens",  stamp_data.cameralens);
01478     if (stamp_data.scene[0])    IMB_metadata_change_field (ibuf, "Scene",   stamp_data.scene);
01479     if (stamp_data.strip[0])    IMB_metadata_change_field (ibuf, "Strip",   stamp_data.strip);
01480     if (stamp_data.rendertime[0]) IMB_metadata_change_field (ibuf, "RenderTime", stamp_data.rendertime);
01481 }
01482 
01483 int BKE_alphatest_ibuf(ImBuf *ibuf)
01484 {
01485     int tot;
01486     if(ibuf->rect_float) {
01487         float *buf= ibuf->rect_float;
01488         for(tot= ibuf->x * ibuf->y; tot--; buf+=4) {
01489             if(buf[3] < 1.0f) {
01490                 return TRUE;
01491             }
01492         }
01493     }
01494     else if (ibuf->rect) {
01495         unsigned char *buf= (unsigned char *)ibuf->rect;
01496         for(tot= ibuf->x * ibuf->y; tot--; buf+=4) {
01497             if(buf[3] != 255) {
01498                 return TRUE;
01499             }
01500         }
01501     }
01502 
01503     return FALSE;
01504 }
01505 
01506 /* note: imf->planes is ignored here, its assumed the image channels
01507  * are already set */
01508 int BKE_write_ibuf(ImBuf *ibuf, const char *name, ImageFormatData *imf)
01509 {
01510     char imtype= imf->imtype;
01511     char compress= imf->compress;
01512     char quality= imf->quality;
01513 
01514     int ok;
01515 
01516     if(imtype== R_IMF_IMTYPE_IRIS) {
01517         ibuf->ftype= IMAGIC;
01518     }
01519 #ifdef WITH_HDR
01520     else if (imtype==R_IMF_IMTYPE_RADHDR) {
01521         ibuf->ftype= RADHDR;
01522     }
01523 #endif
01524     else if (ELEM5(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_THEORA, R_IMF_IMTYPE_XVID)) {
01525         ibuf->ftype= PNG;
01526 
01527         if(imtype==R_IMF_IMTYPE_PNG)
01528             ibuf->ftype |= compress;
01529 
01530     }
01531 #ifdef WITH_DDS
01532     else if (imtype==R_IMF_IMTYPE_DDS) {
01533         ibuf->ftype= DDS;
01534     }
01535 #endif
01536     else if (imtype==R_IMF_IMTYPE_BMP) {
01537         ibuf->ftype= BMP;
01538     }
01539 #ifdef WITH_TIFF
01540     else if (imtype==R_IMF_IMTYPE_TIFF) {
01541         ibuf->ftype= TIF;
01542 
01543         if(imf->depth == R_IMF_CHAN_DEPTH_16)
01544             ibuf->ftype |= TIF_16BIT;
01545     }
01546 #endif
01547 #ifdef WITH_OPENEXR
01548     else if (imtype==R_IMF_IMTYPE_OPENEXR || imtype==R_IMF_IMTYPE_MULTILAYER) {
01549         ibuf->ftype= OPENEXR;
01550         if(imf->depth == R_IMF_CHAN_DEPTH_16)
01551             ibuf->ftype |= OPENEXR_HALF;
01552         ibuf->ftype |= (imf->exr_codec & OPENEXR_COMPRESS);
01553         
01554         if(!(imf->flag & R_IMF_FLAG_ZBUF))
01555             ibuf->zbuf_float = NULL;    /* signal for exr saving */
01556         
01557     }
01558 #endif
01559 #ifdef WITH_CINEON
01560     else if (imtype==R_IMF_IMTYPE_CINEON) {
01561         ibuf->ftype = CINEON;
01562     }
01563     else if (imtype==R_IMF_IMTYPE_DPX) {
01564         ibuf->ftype = DPX;
01565     }
01566 #endif
01567     else if (imtype==R_IMF_IMTYPE_TARGA) {
01568         ibuf->ftype= TGA;
01569     }
01570     else if(imtype==R_IMF_IMTYPE_RAWTGA) {
01571         ibuf->ftype= RAWTGA;
01572     }
01573 #ifdef WITH_OPENJPEG
01574     else if(imtype==R_IMF_IMTYPE_JP2) {
01575         if(quality < 10) quality= 90;
01576         ibuf->ftype= JP2|quality;
01577         
01578         if (imf->depth == R_IMF_CHAN_DEPTH_16) {
01579             ibuf->ftype |= JP2_16BIT;
01580         } else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
01581             ibuf->ftype |= JP2_12BIT;
01582         }
01583         
01584         if (imf->jp2_flag & R_IMF_JP2_FLAG_YCC) {
01585             ibuf->ftype |= JP2_YCC;
01586         }
01587 
01588         if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_PRESET) {
01589             ibuf->ftype |= JP2_CINE;
01590             if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_48)
01591                 ibuf->ftype |= JP2_CINE_48FPS;
01592         }
01593     }
01594 #endif
01595     else {
01596         /* R_IMF_IMTYPE_JPEG90, etc. default we save jpegs */
01597         if(quality < 10) quality= 90;
01598         ibuf->ftype= JPG|quality;
01599     }
01600     
01601     BLI_make_existing_file(name);
01602     
01603     ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
01604     if (ok == 0) {
01605         perror(name);
01606     }
01607     
01608     return(ok);
01609 }
01610 
01611 /* same as BKE_write_ibuf() but crappy workaround not to perminantly modify
01612  * _some_, values in the imbuf */
01613 int BKE_write_ibuf_as(ImBuf *ibuf, const char *name, ImageFormatData *imf,
01614                       const short save_copy)
01615 {
01616     ImBuf ibuf_back= *ibuf;
01617     int ok;
01618 
01619     /* all data is rgba anyway,
01620      * this just controls how to save for some formats */
01621     ibuf->planes= imf->planes;
01622 
01623     ok= BKE_write_ibuf(ibuf, name, imf);
01624 
01625     if (save_copy) {
01626         /* note that we are not restoring _all_ settings */
01627         ibuf->planes= ibuf_back.planes;
01628         ibuf->ftype=  ibuf_back.ftype;
01629     }
01630 
01631     return ok;
01632 }
01633 
01634 int BKE_write_ibuf_stamp(Scene *scene, struct Object *camera, ImBuf *ibuf, const char *name, struct ImageFormatData *imf)
01635 {
01636     if(scene && scene->r.stamp & R_STAMP_ALL)
01637         BKE_stamp_info(scene, camera, ibuf);
01638 
01639     return BKE_write_ibuf(ibuf, name, imf);
01640 }
01641 
01642 
01643 void BKE_makepicstring(char *string, const char *base, const char *relbase, int frame, const char imtype, const short use_ext, const short use_frames)
01644 {
01645     if (string==NULL) return;
01646     BLI_strncpy(string, base, FILE_MAX - 10);   /* weak assumption */
01647     BLI_path_abs(string, relbase);
01648 
01649     if(use_frames)
01650         BLI_path_frame(string, frame, 4);
01651 
01652     if(use_ext)
01653         BKE_add_image_extension(string, imtype);
01654         
01655 }
01656 
01657 /* used by sequencer too */
01658 struct anim *openanim(const char *name, int flags, int streamindex)
01659 {
01660     struct anim *anim;
01661     struct ImBuf *ibuf;
01662     
01663     anim = IMB_open_anim(name, flags, streamindex);
01664     if (anim == NULL) return NULL;
01665 
01666     ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
01667     if (ibuf == NULL) {
01668         if(BLI_exists(name))
01669             printf("not an anim: %s\n", name);
01670         else
01671             printf("anim file doesn't exist: %s\n", name);
01672         IMB_free_anim(anim);
01673         return NULL;
01674     }
01675     IMB_freeImBuf(ibuf);
01676     
01677     return(anim);
01678 }
01679 
01680 /* ************************* New Image API *************** */
01681 
01682 
01683 /* Notes about Image storage 
01684 - packedfile
01685   -> written in .blend
01686 - filename
01687   -> written in .blend
01688 - movie
01689   -> comes from packedfile or filename
01690 - renderresult
01691   -> comes from packedfile or filename
01692 - listbase
01693   -> ibufs from exrhandle
01694 - flipbook array
01695   -> ibufs come from movie, temporary renderresult or sequence
01696 - ibuf
01697   -> comes from packedfile or filename or generated
01698 
01699 */
01700 
01701 
01702 /* forces existence of 1 Image for renderout or nodes, returns Image */
01703 /* name is only for default, when making new one */
01704 Image *BKE_image_verify_viewer(int type, const char *name)
01705 {
01706     Image *ima;
01707     
01708     for(ima=G.main->image.first; ima; ima= ima->id.next)
01709         if(ima->source==IMA_SRC_VIEWER)
01710             if(ima->type==type)
01711                 break;
01712     
01713     if(ima==NULL)
01714         ima= image_alloc(name, IMA_SRC_VIEWER, type);
01715     
01716     /* happens on reload, imagewindow cannot be image user when hidden*/
01717     if(ima->id.us==0)
01718         id_us_plus(&ima->id);
01719 
01720     return ima;
01721 }
01722 
01723 void BKE_image_assign_ibuf(Image *ima, ImBuf *ibuf)
01724 {
01725     image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
01726 }
01727 
01728 void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
01729 {
01730     if(ima==NULL)
01731         return;
01732     
01733     switch(signal) {
01734     case IMA_SIGNAL_FREE:
01735         image_free_buffers(ima);
01736         if(iuser)
01737             iuser->ok= 1;
01738         break;
01739     case IMA_SIGNAL_SRC_CHANGE:
01740         if(ima->type == IMA_TYPE_UV_TEST)
01741             if(ima->source != IMA_SRC_GENERATED)
01742                 ima->type= IMA_TYPE_IMAGE;
01743 
01744         if(ima->source==IMA_SRC_GENERATED) {
01745             if(ima->gen_x==0 || ima->gen_y==0) {
01746                 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
01747                 if(ibuf) {
01748                     ima->gen_x= ibuf->x;
01749                     ima->gen_y= ibuf->y;
01750                 }
01751             }
01752         }
01753 
01754         /* force reload on first use, but not for multilayer, that makes nodes and buttons in ui drawing fail */
01755         if(ima->type!=IMA_TYPE_MULTILAYER)
01756             image_free_buffers(ima);
01757 
01758         ima->ok= 1;
01759         if(iuser)
01760             iuser->ok= 1;
01761         break;
01762             
01763     case IMA_SIGNAL_RELOAD:
01764         /* try to repack file */
01765         if(ima->packedfile) {
01766             PackedFile *pf;
01767             pf = newPackedFile(NULL, ima->name, ID_BLEND_PATH(G.main, &ima->id));
01768             if (pf) {
01769                 freePackedFile(ima->packedfile);
01770                 ima->packedfile = pf;
01771                 image_free_buffers(ima);
01772             } else {
01773                 printf("ERROR: Image not available. Keeping packed image\n");
01774             }
01775         }
01776         else
01777             image_free_buffers(ima);
01778         
01779         if(iuser)
01780             iuser->ok= 1;
01781         
01782         break;
01783     case IMA_SIGNAL_USER_NEW_IMAGE:
01784         if(iuser) {
01785             iuser->ok= 1;
01786             if(ima->source==IMA_SRC_FILE || ima->source==IMA_SRC_SEQUENCE) {
01787                 if(ima->type==IMA_TYPE_MULTILAYER) {
01788                     iuser->multi_index= 0;
01789                     iuser->layer= iuser->pass= 0;
01790                 }
01791             }
01792         }
01793         break;
01794     }
01795     
01796     /* dont use notifiers because they are not 100% sure to succseed
01797      * this also makes sure all scenes are accounted for. */
01798     {
01799         Scene *scene;
01800         for(scene= G.main->scene.first; scene; scene= scene->id.next) {
01801             if(scene->nodetree) {
01802                 nodeUpdateID(scene->nodetree, &ima->id);
01803             }
01804         }
01805     }
01806 }
01807 
01808 /* if layer or pass changes, we need an index for the imbufs list */
01809 /* note it is called for rendered results, but it doesnt use the index! */
01810 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
01811 RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
01812 {
01813     RenderLayer *rl;
01814     RenderPass *rpass= NULL;
01815     
01816     if(rr==NULL) 
01817         return NULL;
01818     
01819     if(iuser) {
01820         short index= 0, rl_index= 0, rp_index;
01821         
01822         for(rl= rr->layers.first; rl; rl= rl->next, rl_index++) {
01823             rp_index= 0;
01824             for(rpass= rl->passes.first; rpass; rpass= rpass->next, index++, rp_index++)
01825                 if(iuser->layer==rl_index && iuser->pass==rp_index)
01826                     break;
01827             if(rpass)
01828                 break;
01829         }
01830         
01831         if(rpass)
01832             iuser->multi_index= index;
01833         else 
01834             iuser->multi_index= 0;
01835     }
01836     if(rpass==NULL) {
01837         rl= rr->layers.first;
01838         if(rl)
01839             rpass= rl->passes.first;
01840     }
01841     
01842     return rpass;
01843 }
01844 
01845 RenderResult *BKE_image_acquire_renderresult(Scene *scene, Image *ima)
01846 {
01847     if(ima->rr) {
01848         return ima->rr;
01849     }
01850     else if(ima->type==IMA_TYPE_R_RESULT) {
01851         if(ima->render_slot == ima->last_render_slot)
01852             return RE_AcquireResultRead(RE_GetRender(scene->id.name));
01853         else
01854             return ima->renders[ima->render_slot];
01855     }
01856     else
01857         return NULL;
01858 }
01859 
01860 void BKE_image_release_renderresult(Scene *scene, Image *ima)
01861 {
01862     if(ima->rr);
01863     else if(ima->type==IMA_TYPE_R_RESULT) {
01864         if(ima->render_slot == ima->last_render_slot)
01865             RE_ReleaseResult(RE_GetRender(scene->id.name));
01866     }
01867 }
01868 
01869 void BKE_image_backup_render(Scene *scene, Image *ima)
01870 {
01871     /* called right before rendering, ima->renders contains render
01872        result pointers for everything but the current render */
01873     Render *re= RE_GetRender(scene->id.name);
01874     int slot= ima->render_slot, last= ima->last_render_slot;
01875 
01876     if(slot != last) {
01877         if(ima->renders[slot]) {
01878             RE_FreeRenderResult(ima->renders[slot]);
01879             ima->renders[slot]= NULL;
01880         }
01881 
01882         ima->renders[last]= NULL;
01883         RE_SwapResult(re, &ima->renders[last]);
01884     }
01885 
01886     ima->last_render_slot= slot;
01887 }
01888 
01889 /* after imbuf load, openexr type can return with a exrhandle open */
01890 /* in that case we have to build a render-result */
01891 static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
01892 {
01893     
01894     ima->rr= RE_MultilayerConvert(ibuf->userdata, ibuf->x, ibuf->y);
01895 
01896 #ifdef WITH_OPENEXR
01897     IMB_exr_close(ibuf->userdata);
01898 #endif
01899 
01900     ibuf->userdata= NULL;
01901     if(ima->rr)
01902         ima->rr->framenr= framenr;
01903 }
01904 
01905 /* common stuff to do with images after loading */
01906 static void image_initialize_after_load(Image *ima, ImBuf *ibuf)
01907 {
01908     /* preview is NULL when it has never been used as an icon before */
01909     if(G.background==0 && ima->preview==NULL)
01910         BKE_icon_changed(BKE_icon_getid(&ima->id));
01911 
01912     /* fields */
01913     if (ima->flag & IMA_FIELDS) {
01914         if(ima->flag & IMA_STD_FIELD) de_interlace_st(ibuf);
01915         else de_interlace_ng(ibuf);
01916     }
01917     /* timer */
01918     ima->lastused = clock() / CLOCKS_PER_SEC;
01919     
01920     ima->ok= IMA_OK_LOADED;
01921     
01922 }
01923 
01924 static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
01925 {
01926     struct ImBuf *ibuf;
01927     unsigned short numlen;
01928     char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
01929     int flag;
01930     
01931     /* XXX temp stuff? */
01932     if(ima->lastframe != frame)
01933         ima->tpageflag |= IMA_TPAGE_REFRESH;
01934 
01935     ima->lastframe= frame;
01936     BLI_strncpy(name, ima->name, sizeof(name));
01937     BLI_stringdec(name, head, tail, &numlen);
01938     BLI_stringenc(name, head, tail, numlen, frame);
01939 
01940     BLI_path_abs(name, ID_BLEND_PATH(G.main, &ima->id));
01941     
01942     flag= IB_rect|IB_multilayer;
01943     if(ima->flag & IMA_DO_PREMUL)
01944         flag |= IB_premul;
01945 
01946     /* read ibuf */
01947     ibuf = IMB_loadiffname(name, flag);
01948 
01949 #if 0
01950     if(ibuf) {
01951         printf(AT" loaded %s\n", name);
01952     } else {
01953         printf(AT" missed %s\n", name);
01954     }
01955 #endif
01956 
01957     if (ibuf) {
01958 #ifdef WITH_OPENEXR
01959         /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
01960         if (ibuf->ftype==OPENEXR && ibuf->userdata) {
01961             image_create_multilayer(ima, ibuf, frame);  
01962             ima->type= IMA_TYPE_MULTILAYER;
01963             IMB_freeImBuf(ibuf);
01964             ibuf= NULL;
01965         }
01966         else {
01967             image_initialize_after_load(ima, ibuf);
01968             image_assign_ibuf(ima, ibuf, 0, frame);
01969         }
01970 #else
01971         image_initialize_after_load(ima, ibuf);
01972         image_assign_ibuf(ima, ibuf, 0, frame);
01973 #endif
01974     }
01975     else
01976         ima->ok= 0;
01977     
01978     if(iuser)
01979         iuser->ok= ima->ok;
01980     
01981     return ibuf;
01982 }
01983 
01984 static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int frame)
01985 {
01986     struct ImBuf *ibuf= NULL;
01987     
01988     /* either we load from RenderResult, or we have to load a new one */
01989     
01990     /* check for new RenderResult */
01991     if(ima->rr==NULL || frame!=ima->rr->framenr) {
01992         /* copy to survive not found multilayer image */
01993         RenderResult *oldrr= ima->rr;
01994     
01995         ima->rr= NULL;
01996         ibuf = image_load_sequence_file(ima, iuser, frame);
01997         
01998         if(ibuf) { /* actually an error */
01999             ima->type= IMA_TYPE_IMAGE;
02000             printf("error, multi is normal image\n");
02001         }
02002         // printf("loaded new result %p\n", ima->rr);
02003         /* free result if new one found */
02004         if(ima->rr) {
02005             // if(oldrr) printf("freed previous result %p\n", oldrr);
02006             if(oldrr) RE_FreeRenderResult(oldrr);
02007         }
02008         else {
02009             ima->rr= oldrr;
02010         }
02011 
02012     }
02013     if(ima->rr) {
02014         RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
02015         
02016         if(rpass) {
02017             // printf("load from pass %s\n", rpass->name);
02018             /* since we free  render results, we copy the rect */
02019             ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
02020             ibuf->rect_float= MEM_dupallocN(rpass->rect);
02021             ibuf->flags |= IB_rectfloat;
02022             ibuf->mall= IB_rectfloat;
02023             ibuf->channels= rpass->channels;
02024             ibuf->profile = IB_PROFILE_LINEAR_RGB;
02025             
02026             image_initialize_after_load(ima, ibuf);
02027             image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:0, frame);
02028             
02029         }
02030         // else printf("pass not found\n");
02031     }
02032     else
02033         ima->ok= 0;
02034     
02035     if(iuser)
02036         iuser->ok= ima->ok;
02037     
02038     return ibuf;
02039 }
02040 
02041 
02042 static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
02043 {
02044     struct ImBuf *ibuf= NULL;
02045     
02046     ima->lastframe= frame;
02047     
02048     if(ima->anim==NULL) {
02049         char str[FILE_MAX];
02050         
02051         BLI_strncpy(str, ima->name, FILE_MAX);
02052         BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
02053 
02054         /* FIXME: make several stream accessible in image editor, too*/
02055         ima->anim = openanim(str, IB_rect, 0);
02056         
02057         /* let's initialize this user */
02058         if(ima->anim && iuser && iuser->frames==0)
02059             iuser->frames= IMB_anim_get_duration(ima->anim,
02060                                  IMB_TC_RECORD_RUN);
02061     }
02062     
02063     if(ima->anim) {
02064         int dur = IMB_anim_get_duration(ima->anim,
02065                         IMB_TC_RECORD_RUN);
02066         int fra= frame-1;
02067         
02068         if(fra<0) fra = 0;
02069         if(fra>(dur-1)) fra= dur-1;
02070         ibuf = IMB_makeSingleUser(
02071             IMB_anim_absolute(ima->anim, fra,
02072                       IMB_TC_RECORD_RUN,
02073                       IMB_PROXY_NONE));
02074         
02075         if(ibuf) {
02076             image_initialize_after_load(ima, ibuf);
02077             image_assign_ibuf(ima, ibuf, 0, frame);
02078         }
02079         else
02080             ima->ok= 0;
02081     }
02082     else
02083         ima->ok= 0;
02084     
02085     if(iuser)
02086         iuser->ok= ima->ok;
02087     
02088     return ibuf;
02089 }
02090 
02091 /* warning, 'iuser' can be NULL */
02092 static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
02093 {
02094     struct ImBuf *ibuf;
02095     char str[FILE_MAX];
02096     int assign = 0, flag;
02097     
02098     /* always ensure clean ima */
02099     image_free_buffers(ima);
02100     
02101     /* is there a PackedFile with this image ? */
02102     if (ima->packedfile) {
02103         flag = IB_rect|IB_multilayer;
02104         if(ima->flag & IMA_DO_PREMUL) flag |= IB_premul;
02105         
02106         ibuf = IMB_ibImageFromMemory((unsigned char*)ima->packedfile->data, ima->packedfile->size, flag, "<packed data>");
02107     } 
02108     else {
02109         flag= IB_rect|IB_multilayer|IB_metadata;
02110         if(ima->flag & IMA_DO_PREMUL)
02111             flag |= IB_premul;
02112             
02113         /* get the right string */
02114         BLI_strncpy(str, ima->name, sizeof(str));
02115         BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
02116         
02117         /* read ibuf */
02118         ibuf = IMB_loadiffname(str, flag);
02119     }
02120     
02121     if (ibuf) {
02122         /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
02123         if (ibuf->ftype==OPENEXR && ibuf->userdata) {
02124             image_create_multilayer(ima, ibuf, cfra);   
02125             ima->type= IMA_TYPE_MULTILAYER;
02126             IMB_freeImBuf(ibuf);
02127             ibuf= NULL;
02128         }
02129         else {
02130             image_initialize_after_load(ima, ibuf);
02131             assign= 1;
02132 
02133             /* check if the image is a font image... */
02134             detectBitmapFont(ibuf);
02135             
02136             /* make packed file for autopack */
02137             if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK))
02138                 ima->packedfile = newPackedFile(NULL, str, ID_BLEND_PATH(G.main, &ima->id));
02139         }
02140     }
02141     else
02142         ima->ok= 0;
02143     
02144     if(assign)
02145         image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
02146 
02147     if(iuser)
02148         iuser->ok= ima->ok;
02149     
02150     return ibuf;
02151 }
02152 
02153 static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
02154 {
02155     ImBuf *ibuf= NULL;
02156     
02157     if(ima->rr==NULL) {
02158         ibuf = image_load_image_file(ima, iuser, 0);
02159         if(ibuf) { /* actually an error */
02160             ima->type= IMA_TYPE_IMAGE;
02161             return ibuf;
02162         }
02163     }
02164     if(ima->rr) {
02165         RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
02166 
02167         if(rpass) {
02168             ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
02169             
02170             image_initialize_after_load(ima, ibuf);
02171             
02172             ibuf->rect_float= rpass->rect;
02173             ibuf->flags |= IB_rectfloat;
02174             ibuf->channels= rpass->channels;
02175             ibuf->profile = IB_PROFILE_LINEAR_RGB;
02176 
02177             image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
02178         }
02179     }
02180     
02181     if(ibuf==NULL) 
02182         ima->ok= 0;
02183     if(iuser)
02184         iuser->ok= ima->ok;
02185     
02186     return ibuf;
02187 }
02188 
02189 
02190 /* showing RGBA result itself (from compo/sequence) or
02191    like exr, using layers etc */
02192 /* always returns a single ibuf, also during render progress */
02193 static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_r)
02194 {
02195     Render *re;
02196     RenderResult rres;
02197     float *rectf, *rectz;
02198     unsigned int *rect;
02199     float dither;
02200     int channels, layer, pass;
02201     ImBuf *ibuf;
02202     int from_render= (ima->render_slot == ima->last_render_slot);
02203 
02204     if(!(iuser && iuser->scene))
02205         return NULL;
02206 
02207     /* if we the caller is not going to release the lock, don't give the image */
02208     if(!lock_r)
02209         return NULL;
02210 
02211     re= RE_GetRender(iuser->scene->id.name);
02212 
02213     channels= 4;
02214     layer= (iuser)? iuser->layer: 0;
02215     pass= (iuser)? iuser->pass: 0;
02216 
02217     if(from_render) {
02218         RE_AcquireResultImage(re, &rres);
02219     }
02220     else if(ima->renders[ima->render_slot]) {
02221         rres= *(ima->renders[ima->render_slot]);
02222         rres.have_combined= rres.rectf != NULL;
02223     }
02224     else
02225         memset(&rres, 0, sizeof(RenderResult));
02226     
02227     if(!(rres.rectx > 0 && rres.recty > 0)) {
02228         if(from_render)
02229             RE_ReleaseResultImage(re);
02230         return NULL;
02231     }
02232 
02233     /* release is done in BKE_image_release_ibuf using lock_r */
02234     if(from_render) {
02235         BLI_lock_thread(LOCK_VIEWER);
02236         *lock_r= re;
02237     }
02238 
02239     /* this gives active layer, composite or seqence result */
02240     rect= (unsigned int *)rres.rect32;
02241     rectf= rres.rectf;
02242     rectz= rres.rectz;
02243     dither= iuser->scene->r.dither_intensity;
02244 
02245     /* combined layer gets added as first layer */
02246     if(rres.have_combined && layer==0);
02247     else if(rres.layers.first) {
02248         RenderLayer *rl= BLI_findlink(&rres.layers, layer-(rres.have_combined?1:0));
02249         if(rl) {
02250             RenderPass *rpass;
02251 
02252             /* there's no combined pass, is in renderlayer itself */
02253             if(pass==0) {
02254                 rectf= rl->rectf;
02255             }
02256             else {
02257                 rpass= BLI_findlink(&rl->passes, pass-1);
02258                 if(rpass) {
02259                     channels= rpass->channels;
02260                     rectf= rpass->rect;
02261                     dither= 0.0f; /* don't dither passes */
02262                 }
02263             }
02264 
02265             for(rpass= rl->passes.first; rpass; rpass= rpass->next)
02266                 if(rpass->passtype == SCE_PASS_Z)
02267                     rectz= rpass->rect;
02268         }
02269     }
02270 
02271     ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
02272 
02273     /* make ibuf if needed, and initialize it */
02274     if(ibuf==NULL) {
02275         ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, 0);
02276         image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
02277     }
02278 
02279     ibuf->x= rres.rectx;
02280     ibuf->y= rres.recty;
02281     
02282     /* free rect buffer if float buffer changes, so it can be recreated with
02283        the updated result, and also in case we got byte buffer from sequencer,
02284        so we don't keep reference to freed buffer */
02285     if(ibuf->rect_float!=rectf || rect || !rectf)
02286         imb_freerectImBuf(ibuf);
02287 
02288     if(rect)
02289         ibuf->rect= rect;
02290     
02291     if(rectf) {
02292         ibuf->rect_float= rectf;
02293         ibuf->flags |= IB_rectfloat;
02294         ibuf->channels= channels;
02295     }
02296     else {
02297         ibuf->rect_float= NULL;
02298         ibuf->flags &= ~IB_rectfloat;
02299     }
02300 
02301     if(rectz) {
02302         ibuf->zbuf_float= rectz;
02303         ibuf->flags |= IB_zbuffloat;
02304     }
02305     else {
02306         ibuf->zbuf_float= NULL;
02307         ibuf->flags &= ~IB_zbuffloat;
02308     }
02309 
02310     /* since its possible to access the buffer from the image directly, set the profile [#25073] */
02311     ibuf->profile= (iuser->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT) ? IB_PROFILE_LINEAR_RGB : IB_PROFILE_NONE;
02312     ibuf->dither= dither;
02313 
02314     if(iuser->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT_PREDIVIDE) {
02315         ibuf->flags |= IB_cm_predivide;
02316         ima->flag |= IMA_CM_PREDIVIDE;
02317     }
02318     else {
02319         ibuf->flags &= ~IB_cm_predivide;
02320         ima->flag &= ~IMA_CM_PREDIVIDE;
02321     }
02322 
02323     ima->ok= IMA_OK_LOADED;
02324 
02325     return ibuf;
02326 }
02327 
02328 static ImBuf *image_get_ibuf_threadsafe(Image *ima, ImageUser *iuser, int *frame_r, int *index_r)
02329 {
02330     ImBuf *ibuf = NULL;
02331     int frame = 0, index = 0;
02332 
02333     /* see if we already have an appropriate ibuf, with image source and type */
02334     if(ima->source==IMA_SRC_MOVIE) {
02335         frame= iuser?iuser->framenr:ima->lastframe;
02336         ibuf= image_get_ibuf(ima, 0, frame);
02337         /* XXX temp stuff? */
02338         if(ima->lastframe != frame)
02339             ima->tpageflag |= IMA_TPAGE_REFRESH;
02340         ima->lastframe = frame;
02341     }
02342     else if(ima->source==IMA_SRC_SEQUENCE) {
02343         if(ima->type==IMA_TYPE_IMAGE) {
02344             frame= iuser?iuser->framenr:ima->lastframe;
02345             ibuf= image_get_ibuf(ima, 0, frame);
02346             
02347             /* XXX temp stuff? */
02348             if(ima->lastframe != frame) {
02349                 ima->tpageflag |= IMA_TPAGE_REFRESH;
02350             }
02351             ima->lastframe = frame;
02352         }   
02353         else if(ima->type==IMA_TYPE_MULTILAYER) {
02354             frame= iuser?iuser->framenr:ima->lastframe;
02355             index= iuser?iuser->multi_index:IMA_NO_INDEX;
02356             ibuf= image_get_ibuf(ima, index, frame);
02357         }
02358     }
02359     else if(ima->source==IMA_SRC_FILE) {
02360         if(ima->type==IMA_TYPE_IMAGE)
02361             ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
02362         else if(ima->type==IMA_TYPE_MULTILAYER)
02363             ibuf= image_get_ibuf(ima, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
02364     }
02365     else if(ima->source == IMA_SRC_GENERATED) {
02366         ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
02367     }
02368     else if(ima->source == IMA_SRC_VIEWER) {
02369         /* always verify entirely, not that this shouldn't happen
02370          * as part of texture sampling in rendering anyway, so not
02371          * a big bottleneck */
02372     }
02373 
02374     *frame_r = frame;
02375     *index_r = index;
02376 
02377     return ibuf;
02378 }
02379 
02380 /* Checks optional ImageUser and verifies/creates ImBuf. */
02381 /* use this one if you want to get a render result in progress,
02382  * if not, use BKE_image_get_ibuf which doesn't require a release */
02383 ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
02384 {
02385     ImBuf *ibuf= NULL;
02386     float color[] = {0, 0, 0, 1};
02387     int frame= 0, index= 0;
02388 
02389     /* This function is intended to be thread-safe. It postpones the mutex lock
02390      * until it needs to load the image, if the image is already there it
02391      * should just get the pointer and return. The reason is that a lot of mutex
02392      * locks appears to be very slow on certain multicore macs, causing a render
02393      * with image textures to actually slow down as more threads are used.
02394      *
02395      * Note that all the image loading functions should also make sure they do
02396      * things in a threadsafe way for image_get_ibuf_threadsafe to work correct.
02397      * That means, the last two steps must be, 1) add the ibuf to the list and
02398      * 2) set ima/iuser->ok to 0 to IMA_OK_LOADED */
02399     
02400     if(lock_r)
02401         *lock_r= NULL;
02402 
02403     /* quick reject tests */
02404     if(ima==NULL) 
02405         return NULL;
02406     if(iuser) {
02407         if(iuser->ok==0)
02408             return NULL;
02409     }
02410     else if(ima->ok==0)
02411         return NULL;
02412     
02413     /* try to get the ibuf without locking */
02414     ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
02415 
02416     if(ibuf == NULL) {
02417         /* couldn't get ibuf and image is not ok, so let's lock and try to
02418          * load the image */
02419         BLI_lock_thread(LOCK_IMAGE);
02420 
02421         /* need to check ok flag and loading ibuf again, because the situation
02422          * might have changed in the meantime */
02423         if(iuser) {
02424             if(iuser->ok==0) {
02425                 BLI_unlock_thread(LOCK_IMAGE);
02426                 return NULL;
02427             }
02428         }
02429         else if(ima->ok==0) {
02430             BLI_unlock_thread(LOCK_IMAGE);
02431             return NULL;
02432         }
02433 
02434         ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
02435 
02436         if(ibuf == NULL) {
02437             /* we are sure we have to load the ibuf, using source and type */
02438             if(ima->source==IMA_SRC_MOVIE) {
02439                 /* source is from single file, use flipbook to store ibuf */
02440                 ibuf= image_load_movie_file(ima, iuser, frame);
02441             }
02442             else if(ima->source==IMA_SRC_SEQUENCE) {
02443                 if(ima->type==IMA_TYPE_IMAGE) {
02444                     /* regular files, ibufs in flipbook, allows saving */
02445                     ibuf= image_load_sequence_file(ima, iuser, frame);
02446                 }
02447                 /* no else; on load the ima type can change */
02448                 if(ima->type==IMA_TYPE_MULTILAYER) {
02449                     /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
02450                     ibuf= image_load_sequence_multilayer(ima, iuser, frame);
02451                 }
02452             }
02453             else if(ima->source==IMA_SRC_FILE) {
02454                 
02455                 if(ima->type==IMA_TYPE_IMAGE)
02456                     ibuf= image_load_image_file(ima, iuser, frame); /* cfra only for '#', this global is OK */
02457                 /* no else; on load the ima type can change */
02458                 if(ima->type==IMA_TYPE_MULTILAYER)
02459                     /* keeps render result, stores ibufs in listbase, allows saving */
02460                     ibuf= image_get_ibuf_multilayer(ima, iuser);
02461                     
02462             }
02463             else if(ima->source == IMA_SRC_GENERATED) {
02464                 /* generated is: ibuf is allocated dynamically */
02465                 /* UV testgrid or black or solid etc */
02466                 if(ima->gen_x==0) ima->gen_x= 1024;
02467                 if(ima->gen_y==0) ima->gen_y= 1024;
02468                 ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 24, (ima->gen_flag & IMA_GEN_FLOAT) != 0, ima->gen_type, color);
02469                 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
02470                 ima->ok= IMA_OK_LOADED;
02471             }
02472             else if(ima->source == IMA_SRC_VIEWER) {
02473                 if(ima->type==IMA_TYPE_R_RESULT) {
02474                     /* always verify entirely, and potentially
02475                        returns pointer to release later */
02476                     ibuf= image_get_render_result(ima, iuser, lock_r);
02477                 }
02478                 else if(ima->type==IMA_TYPE_COMPOSITE) {
02479                     /* requires lock/unlock, otherwise don't return image */
02480                     if(lock_r) {
02481                         /* unlock in BKE_image_release_ibuf */
02482                         BLI_lock_thread(LOCK_VIEWER);
02483                         *lock_r= ima;
02484 
02485                         /* XXX anim play for viewer nodes not yet supported */
02486                         frame= 0; // XXX iuser?iuser->framenr:0;
02487                         ibuf= image_get_ibuf(ima, 0, frame);
02488 
02489                         if(!ibuf) {
02490                             /* Composite Viewer, all handled in compositor */
02491                             /* fake ibuf, will be filled in compositor */
02492                             ibuf= IMB_allocImBuf(256, 256, 32, IB_rect);
02493                             image_assign_ibuf(ima, ibuf, 0, frame);
02494                         }
02495                     }
02496                 }
02497             }
02498         }
02499 
02500         BLI_unlock_thread(LOCK_IMAGE);
02501     }
02502 
02503     tag_image_time(ima);
02504 
02505     return ibuf;
02506 }
02507 
02508 void BKE_image_release_ibuf(Image *ima, void *lock)
02509 {
02510     /* for getting image during threaded render / compositing, need to release */
02511     if(lock == ima) {
02512         BLI_unlock_thread(LOCK_VIEWER); /* viewer image */
02513     }
02514     else if(lock) {
02515         RE_ReleaseResultImage(lock); /* render result */
02516         BLI_unlock_thread(LOCK_VIEWER); /* view image imbuf */
02517     }
02518 }
02519 
02520 /* warning, this can allocate generated images */
02521 ImBuf *BKE_image_get_ibuf(Image *ima, ImageUser *iuser)
02522 {
02523     /* here (+fie_ima/2-1) makes sure that division happens correctly */
02524     return BKE_image_acquire_ibuf(ima, iuser, NULL);
02525 }
02526 
02527 int BKE_image_user_get_frame(const ImageUser *iuser, int cfra, int fieldnr)
02528 {
02529     const int len= (iuser->fie_ima*iuser->frames)/2;
02530 
02531     if(len==0) {
02532         return 0;
02533     }
02534     else {
02535         int framenr;
02536         cfra= cfra - iuser->sfra+1;
02537 
02538         /* cyclic */
02539         if(iuser->cycl) {
02540             cfra= ( (cfra) % len );
02541             if(cfra < 0) cfra+= len;
02542             if(cfra==0) cfra= len;
02543         }
02544 
02545         if(cfra<0) cfra= 0;
02546         else if(cfra>len) cfra= len;
02547 
02548         /* convert current frame to current field */
02549         cfra= 2*(cfra);
02550         if(fieldnr) cfra++;
02551 
02552         /* transform to images space */
02553         framenr= (cfra+iuser->fie_ima-2)/iuser->fie_ima;
02554         if(framenr>iuser->frames) framenr= iuser->frames;
02555         framenr+= iuser->offset;
02556 
02557         if(iuser->cycl) {
02558             framenr= ( (framenr) % len );
02559             while(framenr < 0) framenr+= len;
02560             if(framenr==0) framenr= len;
02561         }
02562 
02563         return framenr;
02564     }
02565 }
02566 
02567 void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr)
02568 {
02569     const int framenr= BKE_image_user_get_frame(iuser, cfra, fieldnr);
02570 
02571     /* allows image users to handle redraws */
02572     if(iuser->flag & IMA_ANIM_ALWAYS)
02573         if(framenr!=iuser->framenr)
02574             iuser->flag |= IMA_ANIM_REFRESHED;
02575 
02576     iuser->framenr= framenr;
02577     if(iuser->ok==0) iuser->ok= 1;
02578 }
02579 
02580 int BKE_image_has_alpha(struct Image *image)
02581 {
02582     ImBuf *ibuf;
02583     void *lock;
02584     int planes;
02585     
02586     ibuf= BKE_image_acquire_ibuf(image, NULL, &lock);
02587     planes = (ibuf?ibuf->planes:0);
02588     BKE_image_release_ibuf(image, lock);
02589 
02590     if (planes == 32)
02591         return 1;
02592     else
02593         return 0;
02594 }
02595