replace ImBuf.depth with ImBuf.planes to match ImageFormatData.planes & to avoid...
[blender.git] / source / blender / blenkernel / intern / image.c
index efa8a824663ca033b1049dcb3aff0b5ca374d445..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,7 +105,7 @@ 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;
        
@@ -125,7 +133,7 @@ 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;
        
@@ -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;
@@ -374,9 +509,7 @@ Image *BKE_add_image_file(const char *name)
        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;
@@ -427,6 +560,7 @@ Image *BKE_add_image_size(unsigned int width, unsigned int height, const char *n
                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);
@@ -664,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
@@ -692,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;
@@ -764,6 +900,76 @@ 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)
 {
        const char *extension= NULL;
@@ -839,9 +1045,15 @@ int BKE_add_image_extension(char *string, int imtype)
 
        if(extension) {
                /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */
-               /* return BLI_replace_extension(string, FILE_MAX, extension); */
-               strcat(string, extension);
-               return TRUE;
+               /* 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;
@@ -857,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.main->name);
-                       else                            sprintf(stamp_data->file, "%s", G.main->name);
-               } 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);
@@ -928,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';
        }
@@ -983,26 +1194,28 @@ 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';
                }
        }
 }
 
-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)
@@ -1013,15 +1226,11 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
        
        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;
@@ -1032,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 */
@@ -1048,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) */
@@ -1063,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 */
@@ -1078,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);
        }
 
@@ -1092,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. */
@@ -1107,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. */
@@ -1121,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. */
@@ -1135,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]) {
@@ -1147,10 +1368,10 @@ 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);
        }
        
@@ -1162,24 +1383,27 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                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);
@@ -1188,6 +1412,7 @@ 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);
@@ -1216,19 +1441,22 @@ int BKE_alphatest_ibuf(ImBuf *ibuf)
        return FALSE;
 }
 
-int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, const char *name, int imtype, int subimtype, int quality)
+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;
-       (void)subimtype; /* quies unused warnings */
 
-       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
@@ -1236,33 +1464,33 @@ int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, const char *name, int imtype, int
                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 */
                
        }
@@ -1286,19 +1514,19 @@ int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, const char *name, int imtype, int
                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;
                }
        }
@@ -1307,13 +1535,10 @@ int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, const char *name, int imtype, int
                /* 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) {
@@ -1323,12 +1548,20 @@ int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, const char *name, int imtype, int
        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, const char *base, int frame, int imtype, const short use_ext, const short use_frames)
+       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.main->name);
+       BLI_path_abs(string, relbase);
 
        if(use_frames)
                BLI_path_frame(string, frame, 4);
@@ -1339,22 +1572,22 @@ void BKE_makepicstring(char *string, const char *base, int frame, int imtype, co
 }
 
 /* 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);
        
@@ -1435,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)
@@ -1446,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;
@@ -1481,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);
                        }
                }
        }
@@ -1619,10 +1854,7 @@ static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
        BLI_stringdec(name, head, tail, &numlen);
        BLI_stringenc(name, head, tail, numlen, frame);
 
-       if(ima->id.lib)
-               BLI_path_abs(name, ima->id.lib->filepath);
-       else
-               BLI_path_abs(name, G.main->name);
+       BLI_path_abs(name, ID_BLEND_PATH(G.main, &ima->id));
        
        flag= IB_rect|IB_multilayer;
        if(ima->flag & IMA_DO_PREMUL)
@@ -1630,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 */
@@ -1727,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.main->name);
-               
-               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);
@@ -1778,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;
@@ -1787,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.main->name);
+               BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
                
                /* read ibuf */
                ibuf = IMB_loadiffname(str, flag);
@@ -1813,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
@@ -2132,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;
                        }
@@ -2149,7 +2388,8 @@ 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) {
@@ -2166,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;
 }
@@ -2189,21 +2426,19 @@ 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 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 imanr;
+               int framenr;
                cfra= cfra - iuser->sfra+1;
 
                /* cyclic */
@@ -2212,31 +2447,55 @@ void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr)
                        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;
+}
+