replace ImBuf.depth with ImBuf.planes to match ImageFormatData.planes & to avoid...
[blender.git] / source / blender / blenkernel / intern / image.c
index a199c1cf738519c8be230d40d00e24413e17e79c..c99396e6939fd54146e6d0561955e78c67759a7d 100644 (file)
@@ -1,7 +1,4 @@
-/*  image.c
- * 
- * $Id$
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/blenkernel/intern/image.c
+ *  \ingroup bke
+ */
+
+
 #include <stdio.h>
 #include <string.h>
 #include <fcntl.h>
 
 #include "DNA_packedFile_types.h"
 #include "DNA_scene_types.h"
+#include "DNA_object_types.h"
 #include "DNA_camera_types.h"
 #include "DNA_sequence_types.h"
 #include "DNA_userdef_types.h"
+#include "DNA_brush_types.h"
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
 
 #include "BLI_blenlib.h"
 #include "BLI_threads.h"
+#include "BLI_utildefines.h"
+#include "BLI_bpath.h"
 
 #include "BKE_bmfont.h"
 #include "BKE_global.h"
@@ -70,8 +78,8 @@
 #include "BKE_packedFile.h"
 #include "BKE_scene.h"
 #include "BKE_node.h"
-
-//XXX #include "BIF_editseq.h"
+#include "BKE_sequencer.h" /* seq_foreground_frame_get() */
+#include "BKE_utildefines.h"
 
 #include "BLF_api.h"
 
@@ -97,14 +105,14 @@ static void de_interlace_ng(struct ImBuf *ibuf)    /* neogeo fields */
 {
        struct ImBuf * tbuf1, * tbuf2;
        
-       if (ibuf == 0) return;
+       if (ibuf == NULL) return;
        if (ibuf->flags & IB_fields) return;
        ibuf->flags |= IB_fields;
        
        if (ibuf->rect) {
                /* make copies */
-               tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
-               tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
+               tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
+               tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
                
                ibuf->x *= 2;
                
@@ -125,14 +133,14 @@ static void de_interlace_st(struct ImBuf *ibuf)   /* standard fields */
 {
        struct ImBuf * tbuf1, * tbuf2;
        
-       if (ibuf == 0) return;
+       if (ibuf == NULL) return;
        if (ibuf->flags & IB_fields) return;
        ibuf->flags |= IB_fields;
        
        if (ibuf->rect) {
                /* make copies */
-               tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
-               tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
+               tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect);
+               tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect);
                
                ibuf->x *= 2;
                
@@ -309,6 +317,133 @@ Image *copy_image(Image *ima)
        return nima;
 }
 
+static void extern_local_image(Image *UNUSED(ima))
+{
+       /* Nothing to do: images don't link to other IDs. This function exists to
+          match id_make_local pattern. */
+}
+
+void make_local_image(struct Image *ima)
+{
+       Main *bmain= G.main;
+       Tex *tex;
+       Brush *brush;
+       Mesh *me;
+       int is_local= FALSE, is_lib= FALSE;
+
+       /* - only lib users: do nothing
+        * - only local users: set flag
+        * - mixed: make copy
+        */
+
+       if(ima->id.lib==NULL) return;
+
+       /* Can't take short cut here: must check meshes at least because of bogus
+          texface ID refs. - z0r */
+#if 0
+       if(ima->id.us==1) {
+               id_clear_lib_data(bmain, &ima->id);
+               extern_local_image(ima);
+               return;
+       }
+#endif
+
+       for(tex= bmain->tex.first; tex; tex= tex->id.next) {
+               if(tex->ima == ima) {
+                       if(tex->id.lib) is_lib= TRUE;
+                       else is_local= TRUE;
+               }
+       }
+       for(brush= bmain->brush.first; brush; brush= brush->id.next) {
+               if(brush->clone.image == ima) {
+                       if(brush->id.lib) is_lib= TRUE;
+                       else is_local= TRUE;
+               }
+       }
+       for(me= bmain->mesh.first; me; me= me->id.next) {
+               if(me->mtface) {
+                       MTFace *tface;
+                       int a, i;
+
+                       for(i=0; i<me->fdata.totlayer; i++) {
+                               if(me->fdata.layers[i].type == CD_MTFACE) {
+                                       tface= (MTFace*)me->fdata.layers[i].data;
+
+                                       for(a=0; a<me->totface; a++, tface++) {
+                                               if(tface->tpage == ima) {
+                                                       if(me->id.lib) is_lib= TRUE;
+                                                       else is_local= TRUE;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+
+       if(is_local && is_lib == FALSE) {
+               id_clear_lib_data(bmain, &ima->id);
+               extern_local_image(ima);
+       }
+       else if(is_local && is_lib) {
+               Image *iman= copy_image(ima);
+
+               iman->id.us= 0;
+
+               /* Remap paths of new ID using old library as base. */
+               BKE_id_lib_local_paths(bmain, &iman->id);
+
+               tex= bmain->tex.first;
+               while(tex) {
+                       if(tex->id.lib==NULL) {
+                               if(tex->ima==ima) {
+                                       tex->ima = iman;
+                                       iman->id.us++;
+                                       ima->id.us--;
+                               }
+                       }
+                       tex= tex->id.next;
+               }
+               brush= bmain->brush.first;
+               while(brush) {
+                       if(brush->id.lib==NULL) {
+                               if(brush->clone.image==ima) {
+                                       brush->clone.image = iman;
+                                       iman->id.us++;
+                                       ima->id.us--;
+                               }
+                       }
+                       brush= brush->id.next;
+               }
+               /* Transfer references in texfaces. Texfaces don't add to image ID
+                  user count *unless* there are no other users. See
+                  readfile.c:lib_link_mtface. */
+               me= bmain->mesh.first;
+               while(me) {
+                       if(me->mtface) {
+                               MTFace *tface;
+                               int a, i;
+
+                               for(i=0; i<me->fdata.totlayer; i++) {
+                                       if(me->fdata.layers[i].type == CD_MTFACE) {
+                                               tface= (MTFace*)me->fdata.layers[i].data;
+
+                                               for(a=0; a<me->totface; a++, tface++) { 
+                                                       if(tface->tpage == ima) {
+                                                               tface->tpage = iman;
+                                                               if(iman->id.us == 0) {
+                                                                       tface->tpage->id.us= 1;
+                                                               }
+                                                               id_lib_extern((ID*)iman);
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+                       me= me->id.next;
+               }
+       }
+}
+
 void BKE_image_merge(Image *dest, Image *source)
 {
        ImBuf *ibuf;
@@ -330,7 +465,7 @@ void BKE_image_merge(Image *dest, Image *source)
 /* otherwise creates new. */
 /* does not load ibuf itself */
 /* pass on optional frame for #name images */
-Image *BKE_add_image_file(const char *name, int frame)
+Image *BKE_add_image_file(const char *name)
 {
        Image *ima;
        int file, len;
@@ -338,7 +473,7 @@ Image *BKE_add_image_file(const char *name, int frame)
        char str[FILE_MAX], strtest[FILE_MAX];
        
        BLI_strncpy(str, name, sizeof(str));
-       BLI_path_abs(str, G.sce);
+       BLI_path_abs(str, G.main->name);
        
        /* exists? */
        file= open(str, O_BINARY|O_RDONLY);
@@ -349,7 +484,7 @@ Image *BKE_add_image_file(const char *name, int frame)
        for(ima= G.main->image.first; ima; ima= ima->id.next) {
                if(ima->source!=IMA_SRC_VIEWER && ima->source!=IMA_SRC_GENERATED) {
                        BLI_strncpy(strtest, ima->name, sizeof(ima->name));
-                       BLI_path_abs(strtest, G.sce);
+                       BLI_path_abs(strtest, G.main->name);
                        
                        if( strcmp(strtest, str)==0 ) {
                                if(ima->anim==NULL || ima->id.us==0) {
@@ -374,30 +509,28 @@ Image *BKE_add_image_file(const char *name, int frame)
        ima= image_alloc(libname, IMA_SRC_FILE, IMA_TYPE_IMAGE);
        BLI_strncpy(ima->name, name, sizeof(ima->name));
        
-       /* do a wild guess! */
-       if(BLI_testextensie(name, ".avi") || BLI_testextensie(name, ".mov")
-                       || BLI_testextensie(name, ".mpg")  || BLI_testextensie(name, ".mp4"))
+       if(BLI_testextensie_array(name, imb_ext_movie))
                ima->source= IMA_SRC_MOVIE;
        
        return ima;
 }
 
-static ImBuf *add_ibuf_size(int width, int height, char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
+static ImBuf *add_ibuf_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
 {
        ImBuf *ibuf;
        unsigned char *rect= NULL;
        float *rect_float= NULL;
        
        if (floatbuf) {
-               ibuf= IMB_allocImBuf(width, height, depth, IB_rectfloat, 0);
+               ibuf= IMB_allocImBuf(width, height, depth, IB_rectfloat);
                rect_float= (float*)ibuf->rect_float;
        }
        else {
-               ibuf= IMB_allocImBuf(width, height, depth, IB_rect, 0);
+               ibuf= IMB_allocImBuf(width, height, depth, IB_rect);
                rect= (unsigned char*)ibuf->rect;
        }
        
-       strcpy(ibuf->name, "//Untitled");
+       BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
        ibuf->userflags |= IB_BITMAPDIRTY;
        
        switch(uvtestgrid) {
@@ -415,7 +548,7 @@ static ImBuf *add_ibuf_size(int width, int height, char *name, int depth, int fl
 }
 
 /* adds new image block, creates ImBuf and initializes color */
-Image *BKE_add_image_size(int width, int height, char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
+Image *BKE_add_image_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
 {
        /* on save, type is changed to FILE in editsima.c */
        Image *ima= image_alloc(name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
@@ -427,6 +560,7 @@ Image *BKE_add_image_size(int width, int height, char *name, int depth, int floa
                ima->gen_x= width;
                ima->gen_y= height;
                ima->gen_type= uvtestgrid;
+               ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
                
                ibuf= add_ibuf_size(width, height, name, depth, floatbuf, uvtestgrid, color);
                image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
@@ -467,7 +601,7 @@ void BKE_image_memorypack(Image *ima)
        }
        
        ibuf->ftype= PNG;
-       ibuf->depth= 32;
+       ibuf->planes= R_IMF_PLANES_RGBA;
        
        IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem);
        if(ibuf->encodedbuffer==NULL) {
@@ -511,7 +645,7 @@ static void tag_all_images_time()
 }
 #endif
 
-void free_old_images()
+void free_old_images(void)
 {
        Image *ima;
        static int lasttime = 0;
@@ -524,6 +658,10 @@ void free_old_images()
        if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
                return;
 
+       /* of course not! */
+       if (G.rendering)
+               return;
+       
        lasttime = ctime;
 
        ima= G.main->image.first;
@@ -582,7 +720,7 @@ void BKE_image_print_memlist(void)
        for(ima= G.main->image.first; ima; ima= ima->id.next)
                totsize += image_mem_size(ima);
 
-       printf("\ntotal image memory len: %.3lf MB\n", (double)totsize/(double)(1024*1024));
+       printf("\ntotal image memory len: %.3f MB\n", (double)totsize/(double)(1024*1024));
 
        for(ima= G.main->image.first; ima; ima= ima->id.next) {
                size= image_mem_size(ima);
@@ -596,7 +734,7 @@ void BKE_image_free_all_textures(void)
 {
        Tex *tex;
        Image *ima;
-       unsigned int totsize= 0;
+       /* unsigned int totsize= 0; */
        
        for(ima= G.main->image.first; ima; ima= ima->id.next)
                ima->id.flag &= ~LIB_DOIT;
@@ -608,13 +746,20 @@ void BKE_image_free_all_textures(void)
        for(ima= G.main->image.first; ima; ima= ima->id.next) {
                if(ima->ibufs.first && (ima->id.flag & LIB_DOIT)) {
                        ImBuf *ibuf;
+                       
                        for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
-                               if(ibuf->mipmap[0]) 
+                               /* escape when image is painted on */
+                               if(ibuf->userflags & IB_BITMAPDIRTY)
+                                       break;
+                               
+                               /* if(ibuf->mipmap[0]) 
                                        totsize+= 1.33*ibuf->x*ibuf->y*4;
                                else
-                                       totsize+= ibuf->x*ibuf->y*4;
+                                       totsize+= ibuf->x*ibuf->y*4;*/
+                               
                        }
-                       image_free_buffers(ima);
+                       if(ibuf==NULL)
+                               image_free_buffers(ima);
                }
        }
        /* printf("freed total %d MB\n", totsize/(1024*1024)); */
@@ -657,8 +802,10 @@ void BKE_image_all_free_anim_ibufs(int cfra)
 
 int BKE_imtype_to_ftype(int imtype)
 {
-       if(imtype==0)
+       if(imtype==R_TARGA)
                return TGA;
+       else if(imtype==R_RAWTGA)
+               return RAWTGA;
        else if(imtype== R_IRIS) 
                return IMAGIC;
 #ifdef WITH_HDR
@@ -685,10 +832,6 @@ int BKE_imtype_to_ftype(int imtype)
        else if (imtype==R_DPX)
                return DPX;
 #endif
-       else if (imtype==R_TARGA)
-               return TGA;
-       else if(imtype==R_RAWTGA)
-               return RAWTGA;
 #ifdef WITH_OPENJPEG
        else if(imtype==R_JP2)
                return JP2;
@@ -757,9 +900,79 @@ int BKE_imtype_is_movie(int imtype)
        return 0;
 }
 
+int BKE_imtype_is_alpha_ok(int imtype)
+{
+       switch(imtype) {
+       case R_TARGA:
+       case R_IRIS:
+       case R_PNG:
+       case R_BMP:
+       case R_RADHDR:
+       case R_TIFF:
+       case R_OPENEXR:
+       case R_MULTILAYER:
+       case R_DDS:
+       case R_JP2:
+                       return 1;
+       }
+       return 0;
+}
+
+int BKE_imtype_is_zbuf_ok(int imtype)
+{
+       switch(imtype) {
+       case R_IRIZ:
+       case R_OPENEXR: /* but not R_MULTILAYER */
+                       return 1;
+       }
+       return 0;
+}
+
+int BKE_imtype_is_compression_ok(int imtype)
+{
+       switch(imtype) {
+       case R_PNG:
+                       return 1;
+       }
+       return 0;
+}
+
+int BKE_imtype_is_quality_ok(int imtype)
+{
+       switch(imtype) {
+       case R_JPEG90:
+       case R_JP2:
+                       return 1;
+       }
+       return 0;
+}
+
+int BKE_imtype_is_depth_ok(int imtype)
+{
+       switch (imtype) {
+       case R_RADHDR:
+               return R_IMF_CHAN_DEPTH_32;
+       case R_TIFF:
+               return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_16;
+       case R_OPENEXR:
+               return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
+       case R_MULTILAYER:
+               return R_IMF_CHAN_DEPTH_32;
+       /* eeh, cineone does some strange 10bits per channel */
+       case R_DPX:
+       case R_CINEON:
+               return R_IMF_CHAN_DEPTH_12;
+       case R_JP2:
+               return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
+       /* most formats are 8bit only */
+       default:
+               return R_IMF_CHAN_DEPTH_8;
+       }
+}
+
 int BKE_add_image_extension(char *string, int imtype)
 {
-       char *extension= NULL;
+       const char *extension= NULL;
        
        if(imtype== R_IRIS) {
                if(!BLI_testextensie(string, ".rgb"))
@@ -831,7 +1044,16 @@ int BKE_add_image_extension(char *string, int imtype)
        }
 
        if(extension) {
-               return BLI_replace_extension(string, FILE_MAX, extension);
+               /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */
+               /* remove any other known image extension */
+               if(BLI_testextensie_array(string, imb_ext_image)
+                                 || (G.have_quicktime && BLI_testextensie_array(string, imb_ext_image_qt))) {
+                       return BLI_replace_extension(string, FILE_MAX, extension);
+               } else {
+                       return BLI_ensure_extension(string, FILE_MAX, extension);
+                       return TRUE;
+               }
+               
        }
        else {
                return FALSE;
@@ -847,65 +1069,56 @@ typedef struct StampData {
        char    time[512];
        char    frame[512];
        char    camera[64];
+       char    cameralens[64];
        char    scene[64];
        char    strip[64];
        char    rendertime[64];
 } StampData;
 
-static void stampdata(Scene *scene, StampData *stamp_data, int do_prefix)
+static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int do_prefix)
 {
        char text[256];
        struct tm *tl;
        time_t t;
 
        if (scene->r.stamp & R_STAMP_FILENAME) {
-               if (G.relbase_valid) {
-                       if (do_prefix)          sprintf(stamp_data->file, "File %s", G.sce);
-                       else                            sprintf(stamp_data->file, "%s", G.sce);
-               } else {
-                       if (do_prefix)          strcpy(stamp_data->file, "File <untitled>");
-                       else                            strcpy(stamp_data->file, "<untitled>");
-               }
+               BLI_snprintf(stamp_data->file, sizeof(stamp_data->file), do_prefix ? "File %s":"%s", G.relbase_valid ? G.main->name:"<untitled>");
        } else {
                stamp_data->file[0] = '\0';
        }
        
        if (scene->r.stamp & R_STAMP_NOTE) {
                /* Never do prefix for Note */
-               sprintf(stamp_data->note, "%s", scene->r.stamp_udata);
+               BLI_snprintf(stamp_data->note, sizeof(stamp_data->note), "%s", scene->r.stamp_udata);
        } else {
                stamp_data->note[0] = '\0';
        }
        
        if (scene->r.stamp & R_STAMP_DATE) {
-
-               t = time (NULL);
-               tl = localtime (&t);
-               sprintf (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);
-
-               if (do_prefix)          sprintf(stamp_data->date, "Date %s", text);
-               else                            sprintf(stamp_data->date, "%s", text);
+               t = time(NULL);
+               tl = localtime(&t);
+               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);
+               BLI_snprintf(stamp_data->date, sizeof(stamp_data->date), do_prefix ? "Date %s":"%s", text);
        } else {
                stamp_data->date[0] = '\0';
        }
        
        if (scene->r.stamp & R_STAMP_MARKER) {
                char *name = scene_find_last_marker_name(scene, CFRA);
-       
-               if (name)       strcpy(text, name);
+
+               if (name)       BLI_strncpy(text, name, sizeof(text));
                else            strcpy(text, "<none>");
-               
-               if (do_prefix)          sprintf(stamp_data->marker, "Marker %s", text);
-               else                            sprintf(stamp_data->marker, "%s", text);
+
+               BLI_snprintf(stamp_data->marker, sizeof(stamp_data->marker), do_prefix ? "Marker %s":"%s", text);
        } else {
                stamp_data->marker[0] = '\0';
        }
        
        if (scene->r.stamp & R_STAMP_TIME) {
-               int h, m, s, f;
-               h= m= s= f= 0;
-               f = (int)(scene->r.cfra % scene->r.frs_sec);
-               s = (int)(scene->r.cfra / scene->r.frs_sec);
+               int f = (int)(scene->r.cfra % scene->r.frs_sec);
+               int s = (int)(scene->r.cfra / scene->r.frs_sec);
+               int h= 0;
+               int m= 0;
 
                if (s) {
                        m = (int)(s / 60);
@@ -918,50 +1131,58 @@ static void stampdata(Scene *scene, StampData *stamp_data, int do_prefix)
                }
 
                if (scene->r.frs_sec < 100)
-                       sprintf (text, "%02d:%02d:%02d.%02d", h, m, s, f);
+                       BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%02d", h, m, s, f);
                else
-                       sprintf (text, "%02d:%02d:%02d.%03d", h, m, s, f);
-               
-               if (do_prefix)          sprintf(stamp_data->time, "Time %s", text);
-               else                            sprintf(stamp_data->time, "%s", text);
+                       BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%03d", h, m, s, f);
+
+               BLI_snprintf(stamp_data->time, sizeof(stamp_data->time), do_prefix ? "Time %s":"%s", text);
        } else {
                stamp_data->time[0] = '\0';
        }
        
        if (scene->r.stamp & R_STAMP_FRAME) {
                char format[32];
-               if (do_prefix)          sprintf(format, "Frame %%0%di", 1 + (int) log10(scene->r.efra));
-               else                            sprintf(format, "%%0%di", 1 + (int) log10(scene->r.efra));
-               sprintf (stamp_data->frame, format, scene->r.cfra);
+               int digits= 1;
+               
+               if(scene->r.efra>9)
+                       digits= 1 + (int) log10(scene->r.efra);
+
+               BLI_snprintf(format, sizeof(format), do_prefix ? "Frame %%0%di":"%%0%di", digits);
+               BLI_snprintf (stamp_data->frame, sizeof(stamp_data->frame), format, scene->r.cfra);
        } else {
                stamp_data->frame[0] = '\0';
        }
 
        if (scene->r.stamp & R_STAMP_CAMERA) {
-               if (scene->camera) strcpy(text, ((Camera *) scene->camera)->id.name+2);
-               else            strcpy(text, "<none>");
-               
-               if (do_prefix)          sprintf(stamp_data->camera, "Camera %s", text);
-               else                            sprintf(stamp_data->camera, "%s", text);
+               BLI_snprintf(stamp_data->camera, sizeof(stamp_data->camera), do_prefix ? "Camera %s":"%s", camera ? camera->id.name+2 : "<none>");
        } else {
                stamp_data->camera[0] = '\0';
        }
 
+       if (scene->r.stamp & R_STAMP_CAMERALENS) {
+               if (camera && camera->type == OB_CAMERA) {
+                       BLI_snprintf(text, sizeof(text), "%.2f", ((Camera *)camera->data)->lens);
+               }
+               else            strcpy(text, "<none>");
+
+               BLI_snprintf(stamp_data->cameralens, sizeof(stamp_data->cameralens), do_prefix ? "Lens %s":"%s", text);
+       } else {
+               stamp_data->cameralens[0] = '\0';
+       }
+
        if (scene->r.stamp & R_STAMP_SCENE) {
-               if (do_prefix)          sprintf(stamp_data->scene, "Scene %s", scene->id.name+2);
-               else                            sprintf(stamp_data->scene, "%s", scene->id.name+2);
+               BLI_snprintf(stamp_data->scene, sizeof(stamp_data->scene), do_prefix ? "Scene %s":"%s", scene->id.name+2);
        } else {
                stamp_data->scene[0] = '\0';
        }
        
        if (scene->r.stamp & R_STAMP_SEQSTRIP) {
-               Sequence *seq= NULL; //XXX = get_foreground_frame_seq(scene->r.cfra);
+               Sequence *seq= seq_foreground_frame_get(scene, scene->r.cfra);
        
-               if (seq) strcpy(text, seq->name+2);
+               if (seq)        BLI_strncpy(text, seq->name+2, sizeof(text));
                else            strcpy(text, "<none>");
-               
-               if (do_prefix)          sprintf(stamp_data->strip, "Strip %s", text);
-               else                            sprintf(stamp_data->strip, "%s", text);
+
+               BLI_snprintf(stamp_data->strip, sizeof(stamp_data->strip), do_prefix ? "Strip %s":"%s", text);
        } else {
                stamp_data->strip[0] = '\0';
        }
@@ -973,60 +1194,43 @@ static void stampdata(Scene *scene, StampData *stamp_data, int do_prefix)
                if (stats && (scene->r.stamp & R_STAMP_RENDERTIME)) {
                        BLI_timestr(stats->lastframetime, text);
 
-                       if (do_prefix)          sprintf(stamp_data->rendertime, "RenderTime %s", text);
-                       else                            sprintf(stamp_data->rendertime, "%s", text);
+                       BLI_snprintf(stamp_data->rendertime, sizeof(stamp_data->rendertime), do_prefix ? "RenderTime %s":"%s", text);
                } else {
                        stamp_data->rendertime[0] = '\0';
                }
        }
 }
 
-// XXX - Bad level call.
-extern int datatoc_bmonofont_ttf_size;
-extern char datatoc_bmonofont_ttf[];
-
-// XXX - copied from text_font_begin ! Change all the BLF_* here
-static int mono= -1;
-
-int stamp_font_begin(int size)
-{
-       if (mono == -1)
-               mono= BLF_load_mem_unique("monospace", (unsigned char *)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
-
-       BLF_aspect(mono, 1.0);
-       BLF_size(mono, size, 72);
-       return(mono); // XXX This is for image_gen.c!!
-}
-
-void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, int height, int channels)
+void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rectf, int width, int height, int channels)
 {
        struct StampData stamp_data;
        float w, h, pad;
-       int x, y;
+       int x, y, y_ofs;
        float h_fixed;
-       
+       const int mono= blf_mono_font_render; // XXX
+
+#define BUFF_MARGIN_X 2
+#define BUFF_MARGIN_Y 1
+
        if (!rect && !rectf)
                return;
        
-       stampdata(scene, &stamp_data, 1);
+       stampdata(scene, camera, &stamp_data, 1);
 
        /* TODO, do_versions */
        if(scene->r.stamp_font_id < 8)
                scene->r.stamp_font_id= 12;
 
-       stamp_font_begin(scene->r.stamp_font_id);
-
+       /* set before return */
+       BLF_size(mono, scene->r.stamp_font_id, 72);
+       
        BLF_buffer(mono, rectf, rect, width, height, channels);
        BLF_buffer_col(mono, scene->r.fg_stamp[0], scene->r.fg_stamp[1], scene->r.fg_stamp[2], 1.0);
-       pad= BLF_width(mono, "--");
+       pad= BLF_width_max(mono);
 
-       /* use 'h_fixed' rather then 'h', aligns better */
-       // BLF_width_and_height(mono, "^|/_AgPpJjlYy", &w, &h_fixed);
-       {
-               rctf box;
-               BLF_boundbox(mono, "^|/_AgPpJjlYy", &box);
-               h_fixed= box.ymax - box.ymin;
-       }
+       /* use 'h_fixed' rather than 'h', aligns better */
+       h_fixed= BLF_height_max(mono);
+       y_ofs = -BLF_descender(mono);
 
        x= 0;
        y= height;
@@ -1037,14 +1241,14 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                y -= h;
 
                /* also a little of space to the background. */
-               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y-3, w+3, y+h+2);
+               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);
 
                /* and draw the text. */
-               BLF_position(mono, x, y, 0.0);
+               BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.file);
 
                /* the extra pixel for background. */
-               y -= 4;
+               y -= BUFF_MARGIN_Y * 2;
        }
 
        /* Top left corner, below File */
@@ -1053,13 +1257,13 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                y -= h;
 
                /* and space for background. */
-               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-3, w+3, y+h+2);
+               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);
 
-               BLF_position(mono, x, y+1, 0.0);
+               BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.note);
 
                /* the extra pixel for background. */
-               y -= 4;
+               y -= BUFF_MARGIN_Y * 2;
        }
        
        /* Top left corner, below File (or Note) */
@@ -1068,13 +1272,13 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                y -= h;
 
                /* and space for background. */
-               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-3, w+3, y+h+2);
+               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);
 
-               BLF_position(mono, x, y, 0.0);
+               BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.date);
 
                /* the extra pixel for background. */
-               y -= 4;
+               y -= BUFF_MARGIN_Y * 2;
        }
 
        /* Top left corner, below File, Date or Note */
@@ -1083,9 +1287,9 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                y -= h;
 
                /* and space for background. */
-               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-3, w+3, y+h+2);
+               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);
 
-               BLF_position(mono, x, y, 0.0);
+               BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.rendertime);
        }
 
@@ -1097,10 +1301,10 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                BLF_width_and_height(mono, stamp_data.marker, &w, &h); h= h_fixed;
 
                /* extra space for background. */
-               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, w+2, y+h+2);
+               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);
 
                /* and pad the text. */
-               BLF_position(mono, x, y+3, 0.0);
+               BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.marker);
 
                /* space width. */
@@ -1112,10 +1316,10 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                BLF_width_and_height(mono, stamp_data.time, &w, &h); h= h_fixed;
 
                /* extra space for background */
-               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+2, y+h+2);
+               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);
 
                /* and pad the text. */
-               BLF_position(mono, x, y+3, 0.0);
+               BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.time);
 
                /* space width. */
@@ -1126,10 +1330,10 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                BLF_width_and_height(mono, stamp_data.frame, &w, &h); h= h_fixed;
 
                /* extra space for background. */
-               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+2, y+h+2);
+               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);
 
                /* and pad the text. */
-               BLF_position(mono, x, y+3, 0.0);
+               BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.frame);
 
                /* space width. */
@@ -1140,9 +1344,21 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                BLF_width_and_height(mono, stamp_data.camera, &w, &h); h= h_fixed;
 
                /* extra space for background. */
-               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+2, y+h+2);
-               BLF_position(mono, x, y+3, 0.0);
+               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);
+               BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.camera);
+
+               /* space width. */
+               x += w + pad;
+       }
+
+       if (stamp_data.cameralens[0]) {
+               BLF_width_and_height(mono, stamp_data.cameralens, &w, &h); h= h_fixed;
+
+               /* extra space for background. */
+               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);
+               BLF_position(mono, x, y + y_ofs, 0.0);
+               BLF_draw_buffer(mono, stamp_data.cameralens);
        }
        
        if (stamp_data.scene[0]) {
@@ -1152,39 +1368,42 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                x= width - w - 2;
 
                /* extra space for background. */
-               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+3, y+h+2);
+               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);
 
                /* and pad the text. */
-               BLF_position(mono, x, y+3, 0.0);
+               BLF_position(mono, x, y+y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.scene);
        }
        
        if (stamp_data.strip[0]) {
-               BLF_width_and_height(mono, stamp_data.scene, &w, &h); h= h_fixed;
+               BLF_width_and_height(mono, stamp_data.strip, &w, &h); h= h_fixed;
 
                /* Top right corner, with an extra space because blenfont is too strict! */
                x= width - w - pad;
                y= height - h;
 
                /* extra space for background. */
-               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y-3, x+w+pad, y+h+2);
+               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);
 
-               BLF_position(mono, x, y, 0.0);
+               BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.strip);
        }
 
        /* cleanup the buffer. */
        BLF_buffer(mono, NULL, NULL, 0, 0, 0);
+
+#undef BUFF_MARGIN_X
+#undef BUFF_MARGIN_Y
 }
 
-void BKE_stamp_info(Scene *scene, struct ImBuf *ibuf)
+void BKE_stamp_info(Scene *scene, Object *camera, struct ImBuf *ibuf)
 {
        struct StampData stamp_data;
 
        if (!ibuf)      return;
        
        /* fill all the data values, no prefix */
-       stampdata(scene, &stamp_data, 0);
+       stampdata(scene, camera, &stamp_data, 0);
        
        if (stamp_data.file[0])         IMB_metadata_change_field (ibuf, "File",                stamp_data.file);
        if (stamp_data.note[0])         IMB_metadata_change_field (ibuf, "Note",                stamp_data.note);
@@ -1193,23 +1412,51 @@ void BKE_stamp_info(Scene *scene, struct ImBuf *ibuf)
        if (stamp_data.time[0])         IMB_metadata_change_field (ibuf, "Time",                stamp_data.time);
        if (stamp_data.frame[0])        IMB_metadata_change_field (ibuf, "Frame",       stamp_data.frame);
        if (stamp_data.camera[0])       IMB_metadata_change_field (ibuf, "Camera",      stamp_data.camera);
+       if (stamp_data.cameralens[0]) IMB_metadata_change_field (ibuf, "Lens",  stamp_data.cameralens);
        if (stamp_data.scene[0])        IMB_metadata_change_field (ibuf, "Scene",       stamp_data.scene);
        if (stamp_data.strip[0])        IMB_metadata_change_field (ibuf, "Strip",       stamp_data.strip);
        if (stamp_data.rendertime[0]) IMB_metadata_change_field (ibuf, "RenderTime", stamp_data.rendertime);
 }
 
-int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, char *name, int imtype, int subimtype, int quality)
+int BKE_alphatest_ibuf(ImBuf *ibuf)
 {
+       int tot;
+       if(ibuf->rect_float) {
+               float *buf= ibuf->rect_float;
+               for(tot= ibuf->x * ibuf->y; tot--; buf+=4) {
+                       if(buf[3] < 1.0f) {
+                               return TRUE;
+                       }
+               }
+       }
+       else if (ibuf->rect) {
+               unsigned char *buf= (unsigned char *)ibuf->rect;
+               for(tot= ibuf->x * ibuf->y; tot--; buf+=4) {
+                       if(buf[3] != 255) {
+                               return TRUE;
+                       }
+               }
+       }
+
+       return FALSE;
+}
+
+int BKE_write_ibuf(ImBuf *ibuf, const char *name, ImageFormatData *imf)
+{
+       char imtype= imf->imtype;
+       char compress= imf->compress;
+       char quality= imf->quality;
+
        int ok;
-       
-       if(imtype==0) {
-               /* pass */
+
+       if(imtype == -1) {
+               /* use whatever existing image type is set by 'ibuf' */
        }
        else if(imtype== R_IRIS) {
                ibuf->ftype= IMAGIC;
        }
 #ifdef WITH_HDR
-       else if ((imtype==R_RADHDR)) {
+       else if (imtype==R_RADHDR) {
                ibuf->ftype= RADHDR;
        }
 #endif
@@ -1217,33 +1464,33 @@ int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, char *name, int imtype, int subimt
                ibuf->ftype= PNG;
 
                if(imtype==R_PNG)
-                       ibuf->ftype |= quality;  /* quality is actually compression 0-100 --> 0-9 */
+                       ibuf->ftype |= compress;
 
        }
 #ifdef WITH_DDS
-       else if ((imtype==R_DDS)) {
+       else if (imtype==R_DDS) {
                ibuf->ftype= DDS;
        }
 #endif
-       else if ((imtype==R_BMP)) {
+       else if (imtype==R_BMP) {
                ibuf->ftype= BMP;
        }
 #ifdef WITH_TIFF
        else if (imtype==R_TIFF) {
                ibuf->ftype= TIF;
 
-               if(subimtype & R_TIFF_16BIT)
+               if(imf->depth == R_IMF_CHAN_DEPTH_16)
                        ibuf->ftype |= TIF_16BIT;
        }
 #endif
 #ifdef WITH_OPENEXR
        else if (imtype==R_OPENEXR || imtype==R_MULTILAYER) {
                ibuf->ftype= OPENEXR;
-               if(subimtype & R_OPENEXR_HALF)
+               if(imf->depth == R_IMF_CHAN_DEPTH_16)
                        ibuf->ftype |= OPENEXR_HALF;
                ibuf->ftype |= (quality & OPENEXR_COMPRESS);
                
-               if(!(subimtype & R_OPENEXR_ZBUF))
+               if(!(imf->flag & R_IMF_FLAG_ZBUF))
                        ibuf->zbuf_float = NULL;        /* signal for exr saving */
                
        }
@@ -1267,19 +1514,19 @@ int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, char *name, int imtype, int subimt
                if(quality < 10) quality= 90;
                ibuf->ftype= JP2|quality;
                
-               if (subimtype & R_JPEG2K_16BIT) {
+               if (imf->depth == R_IMF_CHAN_DEPTH_16) {
                        ibuf->ftype |= JP2_16BIT;
-               } else if (subimtype & R_JPEG2K_12BIT) {
+               } else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
                        ibuf->ftype |= JP2_12BIT;
                }
                
-               if (subimtype & R_JPEG2K_YCC) {
+               if (imf->jp2_flag & R_IMF_JP2_FLAG_YCC) {
                        ibuf->ftype |= JP2_YCC;
                }
-               
-               if (subimtype & R_JPEG2K_CINE_PRESET) {
+
+               if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_PRESET) {
                        ibuf->ftype |= JP2_CINE;
-                       if (subimtype & R_JPEG2K_CINE_48FPS)
+                       if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_48)
                                ibuf->ftype |= JP2_CINE_48FPS;
                }
        }
@@ -1288,13 +1535,10 @@ int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, char *name, int imtype, int subimt
                /* R_JPEG90, etc. default we save jpegs */
                if(quality < 10) quality= 90;
                ibuf->ftype= JPG|quality;
-               if(ibuf->depth==32) ibuf->depth= 24;    /* unsupported feature only confuses other s/w */
+               if(ibuf->planes==32) ibuf->planes= 24;  /* unsupported feature only confuses other s/w */
        }
        
        BLI_make_existing_file(name);
-
-       if(scene && scene->r.stamp & R_STAMP_ALL)
-               BKE_stamp_info(scene, ibuf);
        
        ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
        if (ok == 0) {
@@ -1304,13 +1548,23 @@ int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, char *name, int imtype, int subimt
        return(ok);
 }
 
+int BKE_write_ibuf_stamp(Scene *scene, struct Object *camera, ImBuf *ibuf, const char *name, struct ImageFormatData *imf)
+{
+       if(scene && scene->r.stamp & R_STAMP_ALL)
+               BKE_stamp_info(scene, camera, ibuf);
 
-void BKE_makepicstring(char *string, char *base, int frame, int imtype, int use_ext)
+       return BKE_write_ibuf(ibuf, name, imf);
+}
+
+
+void BKE_makepicstring(char *string, const char *base, const char *relbase, int frame, int imtype, const short use_ext, const short use_frames)
 {
        if (string==NULL) return;
        BLI_strncpy(string, base, FILE_MAX - 10);       /* weak assumption */
-       BLI_path_abs(string, G.sce);
-       BLI_path_frame(string, frame, 4);
+       BLI_path_abs(string, relbase);
+
+       if(use_frames)
+               BLI_path_frame(string, frame, 4);
 
        if(use_ext)
                BKE_add_image_extension(string, imtype);
@@ -1318,22 +1572,22 @@ void BKE_makepicstring(char *string, char *base, int frame, int imtype, int use_
 }
 
 /* used by sequencer too */
-struct anim *openanim(char *name, int flags)
+struct anim *openanim(const char *name, int flags, int streamindex)
 {
        struct anim *anim;
        struct ImBuf *ibuf;
        
-       anim = IMB_open_anim(name, flags);
-       if (anim == NULL) return(0);
+       anim = IMB_open_anim(name, flags, streamindex);
+       if (anim == NULL) return NULL;
 
-       ibuf = IMB_anim_absolute(anim, 0);
+       ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
        if (ibuf == NULL) {
                if(BLI_exists(name))
                        printf("not an anim: %s\n", name);
                else
                        printf("anim file doesn't exist: %s\n", name);
                IMB_free_anim(anim);
-               return(0);
+               return NULL;
        }
        IMB_freeImBuf(ibuf);
        
@@ -1414,7 +1668,9 @@ void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
                        }
                }
 
-               image_free_buffers(ima);
+               /* force reload on first use, but not for multilayer, that makes nodes and buttons in ui drawing fail */
+               if(ima->type!=IMA_TYPE_MULTILAYER)
+                       image_free_buffers(ima);
 
                ima->ok= 1;
                if(iuser)
@@ -1425,7 +1681,7 @@ void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
                /* try to repack file */
                if(ima->packedfile) {
                        PackedFile *pf;
-                       pf = newPackedFile(NULL, ima->name);
+                       pf = newPackedFile(NULL, ima->name, ID_BLEND_PATH(G.main, &ima->id));
                        if (pf) {
                                freePackedFile(ima->packedfile);
                                ima->packedfile = pf;
@@ -1460,7 +1716,7 @@ void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
                Scene *scene;
                for(scene= G.main->scene.first; scene; scene= scene->id.next) {
                        if(scene->nodetree) {
-                               NodeTagIDChanged(scene->nodetree, &ima->id);
+                               nodeUpdateID(scene->nodetree, &ima->id);
                        }
                }
        }
@@ -1566,15 +1822,10 @@ static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
 /* common stuff to do with images after loading */
 static void image_initialize_after_load(Image *ima, ImBuf *ibuf)
 {
-       
-       
        /* preview is NULL when it has never been used as an icon before */
        if(G.background==0 && ima->preview==NULL)
                BKE_icon_changed(BKE_icon_getid(&ima->id));
-       
-       /* stringcodes also in ibuf, ibuf->name is used to retrieve original (buttons) */
-       BLI_strncpy(ibuf->name, ima->name, FILE_MAX);
-       
+
        /* fields */
        if (ima->flag & IMA_FIELDS) {
                if(ima->flag & IMA_STD_FIELD) de_interlace_st(ibuf);
@@ -1599,15 +1850,11 @@ static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
                ima->tpageflag |= IMA_TPAGE_REFRESH;
 
        ima->lastframe= frame;
-
-       BLI_stringdec(ima->name, head, tail, &numlen);
-       BLI_stringenc(ima->name, head, tail, numlen, frame);
        BLI_strncpy(name, ima->name, sizeof(name));
-       
-       if(ima->id.lib)
-               BLI_path_abs(name, ima->id.lib->filepath);
-       else
-               BLI_path_abs(name, G.sce);
+       BLI_stringdec(name, head, tail, &numlen);
+       BLI_stringenc(name, head, tail, numlen, frame);
+
+       BLI_path_abs(name, ID_BLEND_PATH(G.main, &ima->id));
        
        flag= IB_rect|IB_multilayer;
        if(ima->flag & IMA_DO_PREMUL)
@@ -1615,8 +1862,15 @@ static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
 
        /* read ibuf */
        ibuf = IMB_loadiffname(name, flag);
-       if(G.f & G_DEBUG) printf("loaded %s\n", name);
-       
+
+#if 0
+       if(ibuf) {
+               printf(AT" loaded %s\n", name);
+       } else {
+               printf(AT" missed %s\n", name);
+       }
+#endif
+
        if (ibuf) {
 #ifdef WITH_OPENEXR
                /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
@@ -1679,11 +1933,12 @@ static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int f
                if(rpass) {
                        // printf("load from pass %s\n", rpass->name);
                        /* since we free  render results, we copy the rect */
-                       ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0, 0);
+                       ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
                        ibuf->rect_float= MEM_dupallocN(rpass->rect);
                        ibuf->flags |= IB_rectfloat;
                        ibuf->mall= IB_rectfloat;
                        ibuf->channels= rpass->channels;
+                       ibuf->profile = IB_PROFILE_LINEAR_RGB;
                        
                        image_initialize_after_load(ima, ibuf);
                        image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:0, frame);
@@ -1711,25 +1966,28 @@ static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
                char str[FILE_MAX];
                
                BLI_strncpy(str, ima->name, FILE_MAX);
-               if(ima->id.lib)
-                       BLI_path_abs(str, ima->id.lib->filepath);
-               else
-                       BLI_path_abs(str, G.sce);
-               
-               ima->anim = openanim(str, IB_rect);
+               BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
+
+               /* FIXME: make several stream accessible in image editor, too*/
+               ima->anim = openanim(str, IB_rect, 0);
                
                /* let's initialize this user */
                if(ima->anim && iuser && iuser->frames==0)
-                       iuser->frames= IMB_anim_get_duration(ima->anim);
+                       iuser->frames= IMB_anim_get_duration(ima->anim,
+                                                            IMB_TC_RECORD_RUN);
        }
        
        if(ima->anim) {
-               int dur = IMB_anim_get_duration(ima->anim);
+               int dur = IMB_anim_get_duration(ima->anim,
+                                               IMB_TC_RECORD_RUN);
                int fra= frame-1;
                
                if(fra<0) fra = 0;
                if(fra>(dur-1)) fra= dur-1;
-               ibuf = IMB_anim_absolute(ima->anim, fra);
+               ibuf = IMB_makeSingleUser(
+                       IMB_anim_absolute(ima->anim, fra,
+                                         IMB_TC_RECORD_RUN,
+                                         IMB_PROXY_NONE));
                
                if(ibuf) {
                        image_initialize_after_load(ima, ibuf);
@@ -1747,8 +2005,7 @@ static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
        return ibuf;
 }
 
-/* cfra used for # code, Image can only have this # for all its users
- * warning, 'iuser' can be NULL */
+/* warning, 'iuser' can be NULL */
 static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
 {
        struct ImBuf *ibuf;
@@ -1763,7 +2020,7 @@ static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
                flag = IB_rect|IB_multilayer;
                if(ima->flag & IMA_DO_PREMUL) flag |= IB_premul;
                
-               ibuf = IMB_ibImageFromMemory((unsigned char*)ima->packedfile->data, ima->packedfile->size, flag);
+               ibuf = IMB_ibImageFromMemory((unsigned char*)ima->packedfile->data, ima->packedfile->size, flag, "<packed data>");
        } 
        else {
                flag= IB_rect|IB_multilayer|IB_metadata;
@@ -1772,10 +2029,7 @@ static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
                        
                /* get the right string */
                BLI_strncpy(str, ima->name, sizeof(str));
-               if(ima->id.lib)
-                       BLI_path_abs(str, ima->id.lib->filepath);
-               else
-                       BLI_path_abs(str, G.sce);
+               BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
                
                /* read ibuf */
                ibuf = IMB_loadiffname(str, flag);
@@ -1798,7 +2052,7 @@ static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
                        
                        /* make packed file for autopack */
                        if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK))
-                               ima->packedfile = newPackedFile(NULL, str);
+                               ima->packedfile = newPackedFile(NULL, str, ID_BLEND_PATH(G.main, &ima->id));
                }
        }
        else
@@ -1828,7 +2082,7 @@ static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
                RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
 
                if(rpass) {
-                       ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0, 0);
+                       ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
                        
                        image_initialize_after_load(ima, ibuf);
                        
@@ -1935,7 +2189,7 @@ static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_
 
        /* make ibuf if needed, and initialize it */
        if(ibuf==NULL) {
-               ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, 0, 0);
+               ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, 0);
                image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
        }
 
@@ -1967,6 +2221,9 @@ static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_
                ibuf->flags &= ~IB_zbuffloat;
        }
 
+       /* since its possible to access the buffer from the image directly, set the profile [#25073] */
+       ibuf->profile= (iuser->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT) ? IB_PROFILE_LINEAR_RGB : IB_PROFILE_NONE;
+
        ibuf->dither= dither;
 
        ima->ok= IMA_OK_LOADED;
@@ -1996,11 +2253,6 @@ static ImBuf *image_get_ibuf_threadsafe(Image *ima, ImageUser *iuser, int *frame
                        /* XXX temp stuff? */
                        if(ima->lastframe != frame) {
                                ima->tpageflag |= IMA_TPAGE_REFRESH;
-                               if(ibuf) {
-                                       /* without this the image name only updates
-                                        * on first load which is quite confusing */
-                                       BLI_strncpy(ima->name, ibuf->name, sizeof(ima->name));
-                               }
                        }
                        ima->lastframe = frame;
                }       
@@ -2103,9 +2355,6 @@ ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
                                        /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
                                        ibuf= image_load_sequence_multilayer(ima, iuser, frame);
                                }
-
-                               if(ibuf)
-                                       BLI_strncpy(ima->name, ibuf->name, sizeof(ima->name));
                        }
                        else if(ima->source==IMA_SRC_FILE) {
                                
@@ -2122,7 +2371,7 @@ ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
                                /* UV testgrid or black or solid etc */
                                if(ima->gen_x==0) ima->gen_x= 1024;
                                if(ima->gen_y==0) ima->gen_y= 1024;
-                               ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 24, 0, ima->gen_type, color);
+                               ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 24, (ima->gen_flag & IMA_GEN_FLOAT) != 0, ima->gen_type, color);
                                image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
                                ima->ok= IMA_OK_LOADED;
                        }
@@ -2139,13 +2388,14 @@ ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
                                                BLI_lock_thread(LOCK_VIEWER);
                                                *lock_r= ima;
 
-                                               frame= iuser?iuser->framenr:0;
+                                               /* XXX anim play for viewer nodes not yet supported */
+                                               frame= 0; // XXX iuser?iuser->framenr:0;
                                                ibuf= image_get_ibuf(ima, 0, frame);
 
                                                if(!ibuf) {
                                                        /* Composite Viewer, all handled in compositor */
                                                        /* fake ibuf, will be filled in compositor */
-                                                       ibuf= IMB_allocImBuf(256, 256, 32, IB_rect, 0);
+                                                       ibuf= IMB_allocImBuf(256, 256, 32, IB_rect);
                                                        image_assign_ibuf(ima, ibuf, 0, frame);
                                                }
                                        }
@@ -2156,10 +2406,7 @@ ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
                BLI_unlock_thread(LOCK_IMAGE);
        }
 
-       /* we assuming that if it is not rendering, it's also not multithreaded
-        * (a somewhat weak assumption) */
-       if(G.rendering==0)
-               tag_image_time(ima);
+       tag_image_time(ima);
 
        return ibuf;
 }
@@ -2176,55 +2423,79 @@ void BKE_image_release_ibuf(Image *ima, void *lock)
        }
 }
 
+/* warning, this can allocate generated images */
 ImBuf *BKE_image_get_ibuf(Image *ima, ImageUser *iuser)
 {
+       /* here (+fie_ima/2-1) makes sure that division happens correctly */
        return BKE_image_acquire_ibuf(ima, iuser, NULL);
 }
 
-void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr)
+int BKE_image_user_get_frame(const ImageUser *iuser, int cfra, int fieldnr)
 {
-       int imanr, len;
-       
-       /* here (+fie_ima/2-1) makes sure that division happens correctly */
-       len= (iuser->fie_ima*iuser->frames)/2;
-       
+       const int len= (iuser->fie_ima*iuser->frames)/2;
+
        if(len==0) {
-               iuser->framenr= 0;
+               return 0;
        }
        else {
+               int framenr;
                cfra= cfra - iuser->sfra+1;
-               
+
                /* cyclic */
                if(iuser->cycl) {
                        cfra= ( (cfra) % len );
                        if(cfra < 0) cfra+= len;
                        if(cfra==0) cfra= len;
                }
-               
-               if(cfra<1) cfra= 1;
+
+               if(cfra<0) cfra= 0;
                else if(cfra>len) cfra= len;
-               
+
                /* convert current frame to current field */
                cfra= 2*(cfra);
                if(fieldnr) cfra++;
-               
+
                /* transform to images space */
-               imanr= (cfra+iuser->fie_ima-2)/iuser->fie_ima;
-               if(imanr>iuser->frames) imanr= iuser->frames;
-               imanr+= iuser->offset;
-               
+               framenr= (cfra+iuser->fie_ima-2)/iuser->fie_ima;
+               if(framenr>iuser->frames) framenr= iuser->frames;
+               framenr+= iuser->offset;
+
                if(iuser->cycl) {
-                       imanr= ( (imanr) % len );
-                       while(imanr < 0) imanr+= len;
-                       if(imanr==0) imanr= len;
+                       framenr= ( (framenr) % len );
+                       while(framenr < 0) framenr+= len;
+                       if(framenr==0) framenr= len;
                }
-       
-               /* allows image users to handle redraws */
-               if(iuser->flag & IMA_ANIM_ALWAYS)
-                       if(imanr!=iuser->framenr)
-                               iuser->flag |= IMA_ANIM_REFRESHED;
-               
-               iuser->framenr= imanr;
-               if(iuser->ok==0) iuser->ok= 1;
+
+               return framenr;
        }
 }
+
+void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr)
+{
+       const int framenr= BKE_image_user_get_frame(iuser, cfra, fieldnr);
+
+       /* allows image users to handle redraws */
+       if(iuser->flag & IMA_ANIM_ALWAYS)
+               if(framenr!=iuser->framenr)
+                       iuser->flag |= IMA_ANIM_REFRESHED;
+
+       iuser->framenr= framenr;
+       if(iuser->ok==0) iuser->ok= 1;
+}
+
+int BKE_image_has_alpha(struct Image *image)
+{
+       ImBuf *ibuf;
+       void *lock;
+       int planes;
+       
+       ibuf= BKE_image_acquire_ibuf(image, NULL, &lock);
+       planes = (ibuf?ibuf->planes:0);
+       BKE_image_release_ibuf(image, lock);
+
+       if (planes == 32)
+               return 1;
+       else
+               return 0;
+}
+