replace ImBuf.depth with ImBuf.planes to match ImageFormatData.planes & to avoid...
[blender.git] / source / blender / blenkernel / intern / image.c
index bbcc28629004fd7ac5c3e065d7ebba87e5cbe5a3..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
 #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"
@@ -316,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;
@@ -381,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;
@@ -434,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);
@@ -474,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) {
@@ -593,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);
@@ -675,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
@@ -703,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;
@@ -775,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;
@@ -850,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;
@@ -874,7 +1075,7 @@ typedef struct StampData {
        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;
@@ -904,8 +1105,8 @@ static void stampdata(Scene *scene, StampData *stamp_data, int do_prefix)
        
        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>");
 
                BLI_snprintf(stamp_data->marker, sizeof(stamp_data->marker), do_prefix ? "Marker %s":"%s", text);
@@ -953,14 +1154,14 @@ static void stampdata(Scene *scene, StampData *stamp_data, int do_prefix)
        }
 
        if (scene->r.stamp & R_STAMP_CAMERA) {
-               BLI_snprintf(stamp_data->camera, sizeof(stamp_data->camera), do_prefix ? "Camera %s":"%s", scene->camera ? scene->camera->id.name+2 : "<none>");
+               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 (scene->camera && scene->camera->type == OB_CAMERA) {
-                       BLI_snprintf(text, sizeof(text), "%.2f", ((Camera *)scene->camera->data)->lens);
+               if (camera && camera->type == OB_CAMERA) {
+                       BLI_snprintf(text, sizeof(text), "%.2f", ((Camera *)camera->data)->lens);
                }
                else            strcpy(text, "<none>");
 
@@ -978,7 +1179,7 @@ static void stampdata(Scene *scene, StampData *stamp_data, int do_prefix)
        if (scene->r.stamp & R_STAMP_SEQSTRIP) {
                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>");
 
                BLI_snprintf(stamp_data->strip, sizeof(stamp_data->strip), do_prefix ? "Strip %s":"%s", text);
@@ -1000,18 +1201,21 @@ static void stampdata(Scene *scene, StampData *stamp_data, int do_prefix)
        }
 }
 
-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)
@@ -1022,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;
@@ -1041,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 */
@@ -1057,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) */
@@ -1072,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 */
@@ -1087,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);
        }
 
@@ -1101,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. */
@@ -1116,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. */
@@ -1130,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. */
@@ -1144,8 +1344,8 @@ 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. */
@@ -1156,8 +1356,8 @@ void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, i
                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, 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.cameralens);
        }
        
@@ -1168,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);
        }
        
@@ -1183,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);
@@ -1238,13 +1441,16 @@ 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;
@@ -1258,7 +1464,7 @@ 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
@@ -1273,18 +1479,18 @@ int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, const char *name, int imtype, int
        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 */
                
        }
@@ -1308,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;
                }
        }
@@ -1329,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) {
@@ -1345,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);
+
+       return BKE_write_ibuf(ibuf, name, imf);
+}
+
 
-void BKE_makepicstring(char *string, const char *base, int frame, int imtype, const short use_ext, const short use_frames)
+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);
@@ -1361,15 +1572,15 @@ 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);
+       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);
@@ -1470,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;
@@ -1505,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);
                        }
                }
        }
@@ -1643,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)
@@ -1654,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 */
@@ -1751,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);
@@ -1802,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;
@@ -1811,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);
@@ -1837,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
@@ -2156,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;
                        }
@@ -2211,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 */
@@ -2234,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<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;
+}
+