style cleanup
[blender.git] / source / blender / blenkernel / intern / image.c
index a2b1fb105006c7fdae7055d5952195bc3cd9db32..22995e5a7eed4d7b38fc71f38601e6a97cf8488d 100644 (file)
@@ -32,7 +32,7 @@
 #include <string.h>
 #include <fcntl.h>
 #include <math.h>
-#ifndef WIN32 
+#ifndef WIN32
 #include <unistd.h>
 #else
 #include <io.h>
@@ -47,6 +47,7 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "IMB_colormanagement.h"
 #include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
 
@@ -67,9 +68,9 @@
 #include "BLI_blenlib.h"
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
-#include "BLI_bpath.h"
 
 #include "BKE_bmfont.h"
+#include "BKE_colortools.h"
 #include "BKE_global.h"
 #include "BKE_icons.h"
 #include "BKE_image.h"
@@ -79,7 +80,6 @@
 #include "BKE_scene.h"
 #include "BKE_node.h"
 #include "BKE_sequencer.h" /* seq_foreground_frame_get() */
-#include "BKE_utildefines.h"
 
 #include "BLF_api.h"
 
 
 #include "BLO_sys_types.h" // for intptr_t support
 
+/* for image user iteration */
+#include "DNA_node_types.h"
+#include "DNA_space_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_view3d_types.h"
+
+#include "WM_api.h"
+
+static SpinLock image_spin;
+
 /* max int, to indicate we don't store sequences in ibuf */
 #define IMA_NO_INDEX    0x7FEFEFEF
 
 #define IMA_INDEX_FRAME(index)          (index >> 10)
 #define IMA_INDEX_PASS(index)           (index & ~1023)
 
+void BKE_images_init(void)
+{
+       BLI_spin_init(&image_spin);
+}
+
+void BKE_images_exit(void)
+{
+       BLI_spin_end(&image_spin);
+}
+
 /* ******** IMAGE PROCESSING ************* */
 
 static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
 {
        struct ImBuf *tbuf1, *tbuf2;
-       
+
        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);
-               tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
-               
+               tbuf1 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
+               tbuf2 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
+
                ibuf->x *= 2;
-               
+
                IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
                IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
-               
+
                ibuf->x /= 2;
                IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
                IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
-               
+
                IMB_freeImBuf(tbuf1);
                IMB_freeImBuf(tbuf2);
        }
@@ -132,25 +152,25 @@ static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
 static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
 {
        struct ImBuf *tbuf1, *tbuf2;
-       
+
        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);
-               tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect);
-               
+               tbuf1 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, IB_rect);
+               tbuf2 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, IB_rect);
+
                ibuf->x *= 2;
-               
+
                IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
                IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
-               
+
                ibuf->x /= 2;
                IMB_rectcpy(ibuf, tbuf2, 0, 0, 0, 0, tbuf2->x, tbuf2->y);
                IMB_rectcpy(ibuf, tbuf1, 0, tbuf2->y, 0, 0, tbuf1->x, tbuf1->y);
-               
+
                IMB_freeImBuf(tbuf1);
                IMB_freeImBuf(tbuf2);
        }
@@ -159,13 +179,14 @@ static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
 
 void BKE_image_de_interlace(Image *ima, int odd)
 {
-       ImBuf *ibuf = BKE_image_get_ibuf(ima, NULL);
+       ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
        if (ibuf) {
                if (odd)
                        de_interlace_st(ibuf);
                else
                        de_interlace_ng(ibuf);
        }
+       BKE_image_release_ibuf(ima, ibuf, NULL);
 }
 
 /* ***************** ALLOC & FREE, DATA MANAGING *************** */
@@ -173,27 +194,27 @@ void BKE_image_de_interlace(Image *ima, int odd)
 static void image_free_buffers(Image *ima)
 {
        ImBuf *ibuf;
-       
+
        while ((ibuf = ima->ibufs.first)) {
                BLI_remlink(&ima->ibufs, ibuf);
-               
+
                if (ibuf->userdata) {
                        MEM_freeN(ibuf->userdata);
                        ibuf->userdata = NULL;
                }
                IMB_freeImBuf(ibuf);
        }
-       
+
        if (ima->anim) IMB_free_anim(ima->anim);
        ima->anim = NULL;
 
        if (ima->rr) {
                RE_FreeRenderResult(ima->rr);
                ima->rr = NULL;
-       }       
-       
+       }
+
        GPU_free_image(ima);
-       
+
        ima->ok = IMA_OK;
 }
 
@@ -224,18 +245,23 @@ void BKE_image_free(Image *ima)
 static Image *image_alloc(const char *name, short source, short type)
 {
        Image *ima;
-       
+
        ima = BKE_libblock_alloc(&G.main->image, ID_IM, name);
        if (ima) {
                ima->ok = IMA_OK;
-               
+
                ima->xrep = ima->yrep = 1;
                ima->aspx = ima->aspy = 1.0;
                ima->gen_x = 1024; ima->gen_y = 1024;
                ima->gen_type = 1;   /* no defines yet? */
-               
+
                ima->source = source;
                ima->type = type;
+
+               if (source == IMA_SRC_VIEWER)
+                       ima->flag |= IMA_VIEW_AS_RENDER;
+
+               BKE_color_managed_colorspace_settings_init(&ima->colorspace_settings);
        }
        return ima;
 }
@@ -246,8 +272,9 @@ static ImBuf *image_get_ibuf(Image *ima, int index, int frame)
        /* this function is intended to be thread safe. with IMA_NO_INDEX this
         * should be OK, but when iterating over the list this is more tricky
         * */
-       if (index == IMA_NO_INDEX)
+       if (index == IMA_NO_INDEX) {
                return ima->ibufs.first;
+       }
        else {
                ImBuf *ibuf;
 
@@ -255,9 +282,9 @@ static ImBuf *image_get_ibuf(Image *ima, int index, int frame)
                for (ibuf = ima->ibufs.first; ibuf; ibuf = ibuf->next)
                        if (ibuf->index == index)
                                return ibuf;
-
-               return NULL;
        }
+
+       return NULL;
 }
 
 /* no ima->ibuf anymore, but listbase */
@@ -275,10 +302,10 @@ static void image_assign_ibuf(Image *ima, ImBuf *ibuf, int index, int frame)
 {
        if (ibuf) {
                ImBuf *link;
-               
+
                if (index != IMA_NO_INDEX)
                        index = IMA_MAKE_INDEX(frame, index);
-               
+
                /* insert based on index */
                for (link = ima->ibufs.first; link; link = link->next)
                        if (link->index >= index)
@@ -308,7 +335,7 @@ Image *BKE_image_copy(Image *ima)
 
        nima->flag = ima->flag;
        nima->tpageflag = ima->tpageflag;
-       
+
        nima->gen_x = ima->gen_x;
        nima->gen_y = ima->gen_y;
        nima->gen_type = ima->gen_type;
@@ -318,6 +345,8 @@ Image *BKE_image_copy(Image *ima)
        nima->aspx = ima->aspx;
        nima->aspy = ima->aspy;
 
+       BKE_color_managed_colorspace_settings_copy(&nima->colorspace_settings, &ima->colorspace_settings);
+
        return nima;
 }
 
@@ -492,31 +521,35 @@ void BKE_image_make_local(struct Image *ima)
 void BKE_image_merge(Image *dest, Image *source)
 {
        ImBuf *ibuf;
-       
+
        /* sanity check */
        if (dest && source && dest != source) {
-       
+
                while ((ibuf = source->ibufs.first)) {
                        BLI_remlink(&source->ibufs, ibuf);
                        image_assign_ibuf(dest, ibuf, IMA_INDEX_PASS(ibuf->index), IMA_INDEX_FRAME(ibuf->index));
                }
-               
+
                BKE_libblock_free(&G.main->image, source);
        }
 }
 
 /* note, we could be clever and scale all imbuf's but since some are mipmaps its not so simple */
-void BKE_image_scale(Image *image, int width, int height)
+int BKE_image_scale(Image *image, int width, int height)
 {
        ImBuf *ibuf;
        void *lock;
 
        ibuf = BKE_image_acquire_ibuf(image, NULL, &lock);
 
-       IMB_scaleImBuf(ibuf, width, height);
-       ibuf->userflags |= IB_BITMAPDIRTY;
+       if (ibuf) {
+               IMB_scaleImBuf(ibuf, width, height);
+               ibuf->userflags |= IB_BITMAPDIRTY;
+       }
+
+       BKE_image_release_ibuf(image, ibuf, lock);
 
-       BKE_image_release_ibuf(image, lock);
+       return (ibuf != NULL);
 }
 
 Image *BKE_image_load(const char *filepath)
@@ -557,7 +590,7 @@ Image *BKE_image_load_exists(const char *filepath)
 {
        Image *ima;
        char str[FILE_MAX], strtest[FILE_MAX];
-       
+
        BLI_strncpy(str, filepath, sizeof(str));
        BLI_path_abs(str, G.main->name);
 
@@ -566,7 +599,7 @@ Image *BKE_image_load_exists(const char *filepath)
                if (ima->source != IMA_SRC_VIEWER && ima->source != IMA_SRC_GENERATED) {
                        BLI_strncpy(strtest, ima->name, sizeof(ima->name));
                        BLI_path_abs(strtest, G.main->name);
-                       
+
                        if (BLI_path_cmp(strtest, str) == 0) {
                                if (ima->anim == NULL || ima->id.us == 0) {
                                        BLI_strncpy(ima->name, filepath, sizeof(ima->name));    /* for stringcode */
@@ -583,58 +616,80 @@ Image *BKE_image_load_exists(const char *filepath)
        return BKE_image_load(filepath);
 }
 
-static ImBuf *add_ibuf_size(unsigned int width, unsigned int height, const 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 gen_type,
+                            float color[4], ColorManagedColorspaceSettings *colorspace_settings)
 {
        ImBuf *ibuf;
        unsigned char *rect = NULL;
        float *rect_float = NULL;
-       
+
        if (floatbuf) {
                ibuf = IMB_allocImBuf(width, height, depth, IB_rectfloat);
-               rect_float = (float *)ibuf->rect_float;
-               ibuf->profile = IB_PROFILE_LINEAR_RGB;
+               rect_float = ibuf->rect_float;
+
+               if (colorspace_settings->name[0] == '\0') {
+                       const char *colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DEFAULT_FLOAT);
+
+                       BLI_strncpy(colorspace_settings->name, colorspace, sizeof(colorspace_settings->name));
+               }
+
+               IMB_colormanagement_check_is_data(ibuf, colorspace_settings->name);
        }
        else {
                ibuf = IMB_allocImBuf(width, height, depth, IB_rect);
                rect = (unsigned char *)ibuf->rect;
-               ibuf->profile = IB_PROFILE_SRGB;
+
+               if (colorspace_settings->name[0] == '\0') {
+                       const char *colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DEFAULT_BYTE);
+
+                       BLI_strncpy(colorspace_settings->name, colorspace, sizeof(colorspace_settings->name));
+               }
+
+               IMB_colormanagement_assign_rect_colorspace(ibuf, colorspace_settings->name);
        }
-       
+
        BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
        ibuf->userflags |= IB_BITMAPDIRTY;
-       
-       switch (uvtestgrid) {
-               case 1:
+
+       switch (gen_type) {
+               case IMA_GENTYPE_GRID:
                        BKE_image_buf_fill_checker(rect, rect_float, width, height);
                        break;
-               case 2:
+               case IMA_GENTYPE_GRID_COLOR:
                        BKE_image_buf_fill_checker_color(rect, rect_float, width, height);
                        break;
                default:
                        BKE_image_buf_fill_color(rect, rect_float, width, height, color);
        }
 
+       if (rect_float) {
+               /* both byte and float buffers are filling in sRGB space, need to linearize float buffer after BKE_image_buf_fill* functions */
+
+               IMB_buffer_float_from_float(rect_float, rect_float, ibuf->channels, IB_PROFILE_LINEAR_RGB, IB_PROFILE_SRGB,
+                                           ibuf->flags & IB_cm_predivide, ibuf->x, ibuf->y, ibuf->x, ibuf->x);
+       }
+
        return ibuf;
 }
 
 /* adds new image block, creates ImBuf and initializes color */
-Image *BKE_image_add_generated(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
+Image *BKE_image_add_generated(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short gen_type, 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);
-       
+
        if (ima) {
                ImBuf *ibuf;
-               
+
                /* BLI_strncpy(ima->name, name, FILE_MAX); */ /* don't do this, this writes in ain invalid filepath! */
                ima->gen_x = width;
                ima->gen_y = height;
-               ima->gen_type = uvtestgrid;
+               ima->gen_type = gen_type;
                ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
-               
-               ibuf = add_ibuf_size(width, height, ima->name, depth, floatbuf, uvtestgrid, color);
+
+               ibuf = add_ibuf_size(width, height, ima->name, depth, floatbuf, gen_type, color, &ima->colorspace_settings);
                image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
-               
+
                ima->ok = IMA_OK_LOADED;
        }
 
@@ -662,31 +717,31 @@ Image *BKE_image_add_from_imbuf(ImBuf *ibuf)
 void BKE_image_memorypack(Image *ima)
 {
        ImBuf *ibuf = image_get_ibuf(ima, IMA_NO_INDEX, 0);
-       
+
        if (ibuf == NULL)
                return;
        if (ima->packedfile) {
                freePackedFile(ima->packedfile);
                ima->packedfile = NULL;
        }
-       
+
        ibuf->ftype = PNG;
        ibuf->planes = R_IMF_PLANES_RGBA;
-       
+
        IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem);
        if (ibuf->encodedbuffer == NULL) {
                printf("memory save for pack error\n");
        }
        else {
                PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
-               
+
                pf->data = ibuf->encodedbuffer;
                pf->size = ibuf->encodedsize;
                ima->packedfile = pf;
                ibuf->encodedbuffer = NULL;
                ibuf->encodedsize = 0;
                ibuf->userflags &= ~IB_BITMAPDIRTY;
-               
+
                if (ima->source == IMA_SRC_GENERATED) {
                        ima->source = IMA_SRC_FILE;
                        ima->type = IMA_TYPE_IMAGE;
@@ -701,7 +756,7 @@ void BKE_image_tag_time(Image *ima)
 }
 
 #if 0
-static void tag_all_images_time() 
+static void tag_all_images_time()
 {
        Image *ima;
        int ctime = (int)PIL_check_seconds_timer();
@@ -720,18 +775,18 @@ void free_old_images(void)
        Image *ima;
        static int lasttime = 0;
        int ctime = (int)PIL_check_seconds_timer();
-       
-       /* 
-        * Run garbage collector once for every collecting period of time 
+
+       /*
+        * Run garbage collector once for every collecting period of time
         * if textimeout is 0, that's the option to NOT run the collector
         */
        if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
                return;
 
        /* of course not! */
-       if (G.rendering)
+       if (G.is_rendering)
                return;
-       
+
        lasttime = ctime;
 
        ima = G.main->image.first;
@@ -759,11 +814,11 @@ static uintptr_t image_mem_size(Image *ima)
        uintptr_t size = 0;
 
        size = 0;
-       
+
        /* viewers have memory depending on other rules, has no valid rect pointer */
        if (ima->source == IMA_SRC_VIEWER)
                return 0;
-       
+
        for (ibuf = ima->ibufs.first; ibuf; ibuf = ibuf->next) {
                if (ibuf->rect) size += MEM_allocN_len(ibuf->rect);
                else if (ibuf->rect_float) size += MEM_allocN_len(ibuf->rect_float);
@@ -803,25 +858,25 @@ void BKE_image_free_all_textures(void)
        Tex *tex;
        Image *ima;
        /* unsigned int totsize = 0; */
-       
+
        for (ima = G.main->image.first; ima; ima = ima->id.next)
                ima->id.flag &= ~LIB_DOIT;
-       
+
        for (tex = G.main->tex.first; tex; tex = tex->id.next)
                if (tex->ima)
                        tex->ima->id.flag |= LIB_DOIT;
-       
+
        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) {
                                /* escape when image is painted on */
                                if (ibuf->userflags & IB_BITMAPDIRTY)
                                        break;
-                               
+
 #if 0
-                               if (ibuf->mipmap[0]) 
+                               if (ibuf->mipmap[0])
                                        totsize += 1.33 * ibuf->x * ibuf->y * 4;
                                else
                                        totsize += ibuf->x * ibuf->y * 4;
@@ -847,20 +902,20 @@ void BKE_image_free_anim_ibufs(Image *ima, int except_frame)
                        continue;
                if (except_frame != IMA_INDEX_FRAME(ibuf->index)) {
                        BLI_remlink(&ima->ibufs, ibuf);
-                       
+
                        if (ibuf->userdata) {
                                MEM_freeN(ibuf->userdata);
                                ibuf->userdata = NULL;
                        }
                        IMB_freeImBuf(ibuf);
-               }                                       
+               }
        }
 }
 
 void BKE_image_all_free_anim_ibufs(int cfra)
 {
        Image *ima;
-       
+
        for (ima = G.main->image.first; ima; ima = ima->id.next)
                if (ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE))
                        BKE_image_free_anim_ibufs(ima, cfra);
@@ -882,7 +937,7 @@ int BKE_imtype_to_ftype(const char imtype)
                return RADHDR;
 #endif
        else if (imtype == R_IMF_IMTYPE_PNG)
-               return PNG;
+               return PNG | 90;
 #ifdef WITH_DDS
        else if (imtype == R_IMF_IMTYPE_DDS)
                return DDS;
@@ -913,7 +968,7 @@ char BKE_ftype_to_imtype(const int ftype)
 {
        if (ftype == 0)
                return R_IMF_IMTYPE_TARGA;
-       else if (ftype == IMAGIC) 
+       else if (ftype == IMAGIC)
                return R_IMF_IMTYPE_IRIS;
 #ifdef WITH_HDR
        else if (ftype & RADHDR)
@@ -957,7 +1012,6 @@ int BKE_imtype_is_movie(const char imtype)
        switch (imtype) {
                case R_IMF_IMTYPE_AVIRAW:
                case R_IMF_IMTYPE_AVIJPEG:
-               case R_IMF_IMTYPE_AVICODEC:
                case R_IMF_IMTYPE_QUICKTIME:
                case R_IMF_IMTYPE_FFMPEG:
                case R_IMF_IMTYPE_H264:
@@ -999,6 +1053,19 @@ int BKE_imtype_supports_quality(const char imtype)
        return 0;
 }
 
+int BKE_imtype_requires_linear_float(const char imtype)
+{
+       switch (imtype) {
+               case R_IMF_IMTYPE_CINEON:
+               case R_IMF_IMTYPE_DPX:
+               case R_IMF_IMTYPE_RADHDR:
+               case R_IMF_IMTYPE_OPENEXR:
+               case R_IMF_IMTYPE_MULTILAYER:
+                       return TRUE;
+       }
+       return 0;
+}
+
 char BKE_imtype_valid_channels(const char imtype)
 {
        char chan_flag = IMA_CHAN_FLAG_RGB; /* assume all support rgb */
@@ -1016,6 +1083,7 @@ char BKE_imtype_valid_channels(const char imtype)
                case R_IMF_IMTYPE_DDS:
                case R_IMF_IMTYPE_JP2:
                case R_IMF_IMTYPE_QUICKTIME:
+               case R_IMF_IMTYPE_DPX:
                        chan_flag |= IMA_CHAN_FLAG_ALPHA;
        }
 
@@ -1044,10 +1112,11 @@ char BKE_imtype_valid_depths(const char imtype)
                        return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
                case R_IMF_IMTYPE_MULTILAYER:
                        return R_IMF_CHAN_DEPTH_32;
-               /* eeh, cineone does some strange 10bits per channel */
+               /* eeh, cineon does some strange 10bits per channel */
                case R_IMF_IMTYPE_DPX:
+                       return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_10 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
                case R_IMF_IMTYPE_CINEON:
-                       return R_IMF_CHAN_DEPTH_12;
+                       return R_IMF_CHAN_DEPTH_10;
                case R_IMF_IMTYPE_JP2:
                        return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
                /* most formats are 8bit only */
@@ -1072,7 +1141,6 @@ char BKE_imtype_from_arg(const char *imtype_arg)
        else if (!strcmp(imtype_arg, "AVIRAW")) return R_IMF_IMTYPE_AVIRAW;
        else if (!strcmp(imtype_arg, "AVIJPEG")) return R_IMF_IMTYPE_AVIJPEG;
        else if (!strcmp(imtype_arg, "PNG")) return R_IMF_IMTYPE_PNG;
-       else if (!strcmp(imtype_arg, "AVICODEC")) return R_IMF_IMTYPE_AVICODEC;
        else if (!strcmp(imtype_arg, "QUICKTIME")) return R_IMF_IMTYPE_QUICKTIME;
        else if (!strcmp(imtype_arg, "BMP")) return R_IMF_IMTYPE_BMP;
 #ifdef WITH_HDR
@@ -1097,10 +1165,11 @@ char BKE_imtype_from_arg(const char *imtype_arg)
        else return R_IMF_IMTYPE_INVALID;
 }
 
-int BKE_add_image_extension(char *string, const char imtype)
+static int do_add_image_extension(char *string, const char imtype, const ImageFormatData *im_format)
 {
        const char *extension = NULL;
-       
+       (void)im_format;  /* may be unused, depends on build options */
+
        if (imtype == R_IMF_IMTYPE_IRIS) {
                if (!BLI_testextensie(string, ".rgb"))
                        extension = ".rgb";
@@ -1164,11 +1233,25 @@ int BKE_add_image_extension(char *string, const char imtype)
        }
 #ifdef WITH_OPENJPEG
        else if (imtype == R_IMF_IMTYPE_JP2) {
-               if (!BLI_testextensie(string, ".jp2"))
-                       extension = ".jp2";
+               if (im_format) {
+                       if (im_format->jp2_codec == R_IMF_JP2_CODEC_JP2) {
+                               if (!BLI_testextensie(string, ".jp2"))
+                                       extension = ".jp2";
+                       }
+                       else if (im_format->jp2_codec == R_IMF_JP2_CODEC_J2K) {
+                               if (!BLI_testextensie(string, ".j2c"))
+                                       extension = ".j2c";
+                       }
+                       else
+                               BLI_assert(!"Unsupported jp2 codec was specified in im_format->jp2_codec");
+               }
+               else {
+                       if (!BLI_testextensie(string, ".jp2"))
+                               extension = ".jp2";
+               }
        }
 #endif
-       else { //   R_IMF_IMTYPE_AVICODEC, R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90, R_IMF_IMTYPE_QUICKTIME etc
+       else { //   R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90, R_IMF_IMTYPE_QUICKTIME etc
                if (!(BLI_testextensie(string, ".jpg") || BLI_testextensie(string, ".jpeg")))
                        extension = ".jpg";
        }
@@ -1184,13 +1267,23 @@ int BKE_add_image_extension(char *string, const char imtype)
                else {
                        return BLI_ensure_extension(string, FILE_MAX, extension);
                }
-               
+
        }
        else {
                return FALSE;
        }
 }
 
+int BKE_add_image_extension(char *string, const ImageFormatData *im_format)
+{
+       return do_add_image_extension(string, im_format->imtype, im_format);
+}
+
+int BKE_add_image_extension_from_type(char *string, const char imtype)
+{
+       return do_add_image_extension(string, imtype, NULL);
+}
+
 void BKE_imformat_defaults(ImageFormatData *im_format)
 {
        memset(im_format, 0, sizeof(*im_format));
@@ -1198,9 +1291,149 @@ void BKE_imformat_defaults(ImageFormatData *im_format)
        im_format->imtype = R_IMF_IMTYPE_PNG;
        im_format->quality = 90;
        im_format->compress = 90;
+
+       BKE_color_managed_display_settings_init(&im_format->display_settings);
+       BKE_color_managed_view_settings_init(&im_format->view_settings);
+}
+
+void BKE_imbuf_to_image_format(struct ImageFormatData *im_format, const ImBuf *imbuf)
+{
+       int ftype        = imbuf->ftype & ~IB_CUSTOM_FLAGS_MASK;
+       int custom_flags = imbuf->ftype & IB_CUSTOM_FLAGS_MASK;
+
+       BKE_imformat_defaults(im_format);
+
+       /* file type */
+
+       if (ftype == IMAGIC)
+               im_format->imtype = R_IMF_IMTYPE_IRIS;
+
+#ifdef WITH_HDR
+       else if (ftype == RADHDR)
+               im_format->imtype = R_IMF_IMTYPE_RADHDR;
+#endif
+
+       else if (ftype == PNG)
+               im_format->imtype = R_IMF_IMTYPE_PNG;
+
+#ifdef WITH_DDS
+       else if (ftype == DDS)
+               im_format->imtype = R_IMF_IMTYPE_DDS;
+#endif
+
+       else if (ftype == BMP)
+               im_format->imtype = R_IMF_IMTYPE_BMP;
+
+#ifdef WITH_TIFF
+       else if (ftype == TIF) {
+               im_format->imtype = R_IMF_IMTYPE_TIFF;
+               if (custom_flags & TIF_16BIT)
+                       im_format->depth = R_IMF_CHAN_DEPTH_16;
+       }
+#endif
+
+#ifdef WITH_OPENEXR
+       else if (ftype == OPENEXR) {
+               im_format->imtype = R_IMF_IMTYPE_OPENEXR;
+               if (custom_flags & OPENEXR_HALF)
+                       im_format->depth = R_IMF_CHAN_DEPTH_16;
+               if (custom_flags & OPENEXR_COMPRESS)
+                       im_format->exr_codec = R_IMF_EXR_CODEC_ZIP;  // Can't determine compression
+               if (imbuf->zbuf_float)
+                       im_format->flag |= R_IMF_FLAG_ZBUF;
+       }
+#endif
+
+#ifdef WITH_CINEON
+       else if (ftype == CINEON)
+               im_format->imtype = R_IMF_IMTYPE_CINEON;
+       else if (ftype == DPX)
+               im_format->imtype = R_IMF_IMTYPE_DPX;
+#endif
+
+       else if (ftype == TGA) {
+               im_format->imtype = R_IMF_IMTYPE_TARGA;
+       }
+       else if (ftype == RAWTGA) {
+               im_format->imtype = R_IMF_IMTYPE_RAWTGA;
+       }
+
+#ifdef WITH_OPENJPEG
+       else if (ftype & JP2) {
+               im_format->imtype = R_IMF_IMTYPE_JP2;
+               im_format->quality = custom_flags & ~JPG_MSK;
+
+               if (ftype & JP2_16BIT)
+                       im_format->depth = R_IMF_CHAN_DEPTH_16;
+               else if (ftype & JP2_12BIT)
+                       im_format->depth = R_IMF_CHAN_DEPTH_12;
+
+               if (ftype & JP2_YCC)
+                       im_format->jp2_flag |= R_IMF_JP2_FLAG_YCC;
+
+               if (ftype & JP2_CINE) {
+                       im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_PRESET;
+                       if (ftype & JP2_CINE_48FPS)
+                               im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_48;
+               }
+
+               if (ftype & JP2_JP2)
+                       im_format->jp2_codec = R_IMF_JP2_CODEC_JP2;
+               else if (ftype & JP2_J2K)
+                       im_format->jp2_codec = R_IMF_JP2_CODEC_J2K;
+               else
+                       BLI_assert(!"Unsupported jp2 codec was specified in file type");
+       }
+#endif
+
+       else {
+               im_format->imtype = R_IMF_IMTYPE_JPEG90;
+               im_format->quality = custom_flags & ~JPG_MSK;
+       }
+
+       /* planes */
+       switch (imbuf->channels) {
+               case 0:
+               case 4: im_format->planes = R_IMF_PLANES_RGBA;
+                       break;
+               case 3: im_format->planes = R_IMF_PLANES_RGB;
+                       break;
+               case 1: im_format->planes = R_IMF_PLANES_BW;
+                       break;
+               default: im_format->planes = R_IMF_PLANES_RGB;
+                       break;
+       }
+
 }
 
-/* could allow access externally - 512 is for long names, 64 is for id names */
+static void timecode_simple_string(char *text, size_t text_size, const int cfra, int const frs_sec)
+{
+       int f = (int)(cfra % frs_sec);
+       int s = (int)(cfra / frs_sec);
+       int h = 0;
+       int m = 0;
+
+       if (s) {
+               m = (int)(s / 60);
+               s %= 60;
+
+               if (m) {
+                       h = (int)(m / 60);
+                       m %= 60;
+               }
+       }
+
+       if (frs_sec < 100) {
+               BLI_snprintf(text, text_size, "%02d:%02d:%02d.%02d", h, m, s, f);
+       }
+       else {
+               BLI_snprintf(text, text_size, "%02d:%02d:%02d.%03d", h, m, s, f);
+       }
+}
+
+#define STAMP_NAME_SIZE ((MAX_ID_NAME - 2) + 16)
+/* could allow access externally - 512 is for long names,
+ * STAMP_NAME_SIZE is for id names, allowing them some room for description */
 typedef struct StampData {
        char file[512];
        char note[512];
@@ -1208,12 +1441,13 @@ typedef struct StampData {
        char marker[512];
        char time[512];
        char frame[512];
-       char camera[64];
-       char cameralens[64];
-       char scene[64];
-       char strip[64];
-       char rendertime[64];
+       char camera[STAMP_NAME_SIZE];
+       char cameralens[STAMP_NAME_SIZE];
+       char scene[STAMP_NAME_SIZE];
+       char strip[STAMP_NAME_SIZE];
+       char rendertime[STAMP_NAME_SIZE];
 } StampData;
+#undef STAMP_NAME_SIZE
 
 static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int do_prefix)
 {
@@ -1227,7 +1461,7 @@ static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int d
        else {
                stamp_data->file[0] = '\0';
        }
-       
+
        if (scene->r.stamp & R_STAMP_NOTE) {
                /* Never do prefix for Note */
                BLI_snprintf(stamp_data->note, sizeof(stamp_data->note), "%s", scene->r.stamp_udata);
@@ -1235,7 +1469,7 @@ static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int d
        else {
                stamp_data->note[0] = '\0';
        }
-       
+
        if (scene->r.stamp & R_STAMP_DATE) {
                t = time(NULL);
                tl = localtime(&t);
@@ -1245,7 +1479,7 @@ static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int d
        else {
                stamp_data->date[0] = '\0';
        }
-       
+
        if (scene->r.stamp & R_STAMP_MARKER) {
                char *name = BKE_scene_find_last_marker_name(scene, CFRA);
 
@@ -1257,38 +1491,19 @@ static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int d
        else {
                stamp_data->marker[0] = '\0';
        }
-       
-       if (scene->r.stamp & R_STAMP_TIME) {
-               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);
-                       s %= 60;
-
-                       if (m) {
-                               h = (int)(m / 60);
-                               m %= 60;
-                       }
-               }
-
-               if (scene->r.frs_sec < 100)
-                       BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%02d", h, m, s, f);
-               else
-                       BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%03d", h, m, s, f);
 
+       if (scene->r.stamp & R_STAMP_TIME) {
+               timecode_simple_string(text, sizeof(text), scene->r.cfra, scene->r.frs_sec);
                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 fmtstr[32];
                int digits = 1;
-               
+
                if (scene->r.efra > 9)
                        digits = 1 + (int) log10(scene->r.efra);
 
@@ -1324,10 +1539,10 @@ static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int d
        else {
                stamp_data->scene[0] = '\0';
        }
-       
+
        if (scene->r.stamp & R_STAMP_SEQSTRIP) {
-               Sequence *seq = seq_foreground_frame_get(scene, scene->r.cfra);
-       
+               Sequence *seq = BKE_sequencer_foreground_frame_get(scene, scene->r.cfra);
+
                if (seq) BLI_strncpy(text, seq->name + 2, sizeof(text));
                else BLI_strncpy(text, "<none>", sizeof(text));
 
@@ -1359,13 +1574,21 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
        int x, y, y_ofs;
        float h_fixed;
        const int mono = blf_mono_font_render; // XXX
+       struct ColorManagedDisplay *display;
+       const char *display_device;
+
+       /* this could be an argument if we want to operate on non linear float imbuf's
+        * for now though this is only used for renders which use scene settings */
 
 #define BUFF_MARGIN_X 2
 #define BUFF_MARGIN_Y 1
 
        if (!rect && !rectf)
                return;
-       
+
+       display_device = scene->display_settings.display_device;
+       display = IMB_colormanagement_display_get_named(display_device);
+
        stampdata(scene, camera, &stamp_data, 1);
 
        /* TODO, do_versions */
@@ -1374,8 +1597,8 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
 
        /* set before return */
        BLF_size(mono, scene->r.stamp_font_id, 72);
-       
-       BLF_buffer(mono, rectf, rect, width, height, channels);
+
+       BLF_buffer(mono, rectf, rect, width, height, channels, display);
        BLF_buffer_col(mono, scene->r.fg_stamp[0], scene->r.fg_stamp[1], scene->r.fg_stamp[2], 1.0);
        pad = BLF_width_max(mono);
 
@@ -1392,7 +1615,8 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                y -= h;
 
                /* also a little of space to the background. */
-               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);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
+                                 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 + y_ofs, 0.0);
@@ -1408,7 +1632,8 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                y -= h;
 
                /* and space for background. */
-               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);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
+                                 0, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
 
                BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.note);
@@ -1416,14 +1641,15 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                /* the extra pixel for background. */
                y -= BUFF_MARGIN_Y * 2;
        }
-       
+
        /* Top left corner, below File (or Note) */
        if (stamp_data.date[0]) {
                BLF_width_and_height(mono, stamp_data.date, &w, &h); h = h_fixed;
                y -= h;
 
                /* and space for background. */
-               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);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
+                                 0, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
 
                BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.date);
@@ -1438,7 +1664,8 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                y -= h;
 
                /* and space for background. */
-               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);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
+                                 0, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
 
                BLF_position(mono, x, y + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.rendertime);
@@ -1452,7 +1679,8 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                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 - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp,  display,
+                                 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 + y_ofs, 0.0);
@@ -1461,13 +1689,14 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                /* space width. */
                x += w + pad;
        }
-       
+
        /* Left bottom corner */
        if (stamp_data.time[0]) {
                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 - BUFF_MARGIN_X, y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
+                                 x - BUFF_MARGIN_X, y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
 
                /* and pad the text. */
                BLF_position(mono, x, y + y_ofs, 0.0);
@@ -1476,12 +1705,13 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                /* space width. */
                x += w + pad;
        }
-       
+
        if (stamp_data.frame[0]) {
                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 - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
+                                 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 + y_ofs, 0.0);
@@ -1495,7 +1725,8 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                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 - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
+                                 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);
 
@@ -1507,11 +1738,12 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                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);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
+                                 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]) {
                BLF_width_and_height(mono, stamp_data.scene, &w, &h); h = h_fixed;
 
@@ -1519,13 +1751,14 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                x = width - w - 2;
 
                /* 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);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
+                                 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 + y_ofs, 0.0);
                BLF_draw_buffer(mono, stamp_data.scene);
        }
-       
+
        if (stamp_data.strip[0]) {
                BLF_width_and_height(mono, stamp_data.strip, &w, &h); h = h_fixed;
 
@@ -1534,14 +1767,15 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
                y = height - h;
 
                /* 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);
+               buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
+                                 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.strip);
        }
 
        /* cleanup the buffer. */
-       BLF_buffer(mono, NULL, NULL, 0, 0, 0);
+       BLF_buffer(mono, NULL, NULL, 0, 0, 0, FALSE);
 
 #undef BUFF_MARGIN_X
 #undef BUFF_MARGIN_Y
@@ -1552,10 +1786,10 @@ void BKE_imbuf_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, 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);
        if (stamp_data.date[0]) IMB_metadata_change_field(ibuf, "Date",        stamp_data.date);
@@ -1639,18 +1873,42 @@ int BKE_imbuf_write(ImBuf *ibuf, const char *name, ImageFormatData *imf)
                if (imf->depth == R_IMF_CHAN_DEPTH_16)
                        ibuf->ftype |= OPENEXR_HALF;
                ibuf->ftype |= (imf->exr_codec & OPENEXR_COMPRESS);
-               
+
                if (!(imf->flag & R_IMF_FLAG_ZBUF))
                        ibuf->zbuf_float = NULL;    /* signal for exr saving */
-               
+
        }
 #endif
 #ifdef WITH_CINEON
        else if (imtype == R_IMF_IMTYPE_CINEON) {
                ibuf->ftype = CINEON;
+               if (imf->cineon_flag & R_IMF_CINEON_FLAG_LOG) {
+                       ibuf->ftype |= CINEON_LOG;
+               }
+               if (imf->depth == R_IMF_CHAN_DEPTH_16) {
+                       ibuf->ftype |= CINEON_16BIT;
+               }
+               else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
+                       ibuf->ftype |= CINEON_12BIT;
+               }
+               else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
+                       ibuf->ftype |= CINEON_10BIT;
+               }
        }
        else if (imtype == R_IMF_IMTYPE_DPX) {
                ibuf->ftype = DPX;
+               if (imf->cineon_flag & R_IMF_CINEON_FLAG_LOG) {
+                 ibuf->ftype |= CINEON_LOG;
+               }
+               if (imf->depth == R_IMF_CHAN_DEPTH_16) {
+                       ibuf->ftype |= CINEON_16BIT;
+               }
+               else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
+                       ibuf->ftype |= CINEON_12BIT;
+               }
+               else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
+                       ibuf->ftype |= CINEON_10BIT;
+               }
        }
 #endif
        else if (imtype == R_IMF_IMTYPE_TARGA) {
@@ -1663,14 +1921,14 @@ int BKE_imbuf_write(ImBuf *ibuf, const char *name, ImageFormatData *imf)
        else if (imtype == R_IMF_IMTYPE_JP2) {
                if (quality < 10) quality = 90;
                ibuf->ftype = JP2 | quality;
-               
+
                if (imf->depth == R_IMF_CHAN_DEPTH_16) {
                        ibuf->ftype |= JP2_16BIT;
                }
                else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
                        ibuf->ftype |= JP2_12BIT;
                }
-               
+
                if (imf->jp2_flag & R_IMF_JP2_FLAG_YCC) {
                        ibuf->ftype |= JP2_YCC;
                }
@@ -1680,6 +1938,13 @@ int BKE_imbuf_write(ImBuf *ibuf, const char *name, ImageFormatData *imf)
                        if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_48)
                                ibuf->ftype |= JP2_CINE_48FPS;
                }
+
+               if (imf->jp2_codec == R_IMF_JP2_CODEC_JP2)
+                       ibuf->ftype |= JP2_JP2;
+               else if (imf->jp2_codec == R_IMF_JP2_CODEC_J2K)
+                       ibuf->ftype |= JP2_J2K;
+               else
+                       BLI_assert(!"Unsupported jp2 codec was specified in im_format->jp2_codec");
        }
 #endif
        else {
@@ -1687,14 +1952,14 @@ int BKE_imbuf_write(ImBuf *ibuf, const char *name, ImageFormatData *imf)
                if (quality < 10) quality = 90;
                ibuf->ftype = JPG | quality;
        }
-       
+
        BLI_make_existing_file(name);
-       
+
        ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
        if (ok == 0) {
                perror(name);
        }
-       
+
        return(ok);
 }
 
@@ -1730,7 +1995,8 @@ int BKE_imbuf_write_stamp(Scene *scene, struct Object *camera, ImBuf *ibuf, cons
 }
 
 
-void BKE_makepicstring(char *string, const char *base, const char *relbase, int frame, const char imtype, const short use_ext, const short use_frames)
+static void do_makepicstring(char *string, const char *base, const char *relbase, int frame, const char imtype,
+                             const ImageFormatData *im_format, const short use_ext, const short use_frames)
 {
        if (string == NULL) return;
        BLI_strncpy(string, base, FILE_MAX - 10);   /* weak assumption */
@@ -1740,17 +2006,26 @@ void BKE_makepicstring(char *string, const char *base, const char *relbase, int
                BLI_path_frame(string, frame, 4);
 
        if (use_ext)
-               BKE_add_image_extension(string, imtype);
-               
+               do_add_image_extension(string, imtype, im_format);
+}
+
+void BKE_makepicstring(char *string, const char *base, const char *relbase, int frame, const ImageFormatData *im_format, const short use_ext, const short use_frames)
+{
+       do_makepicstring(string, base, relbase, frame, im_format->imtype, im_format, use_ext, use_frames);
+}
+
+void BKE_makepicstring_from_type(char *string, const char *base, const char *relbase, int frame, const char imtype, const short use_ext, const short use_frames)
+{
+       do_makepicstring(string, base, relbase, frame, imtype, NULL, use_ext, use_frames);
 }
 
 /* used by sequencer too */
-struct anim *openanim(const char *name, int flags, int streamindex)
+struct anim *openanim(const char *name, int flags, int streamindex, char colorspace[IMA_MAX_SPACE])
 {
        struct anim *anim;
        struct ImBuf *ibuf;
-       
-       anim = IMB_open_anim(name, flags, streamindex);
+
+       anim = IMB_open_anim(name, flags, streamindex, colorspace);
        if (anim == NULL) return NULL;
 
        ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
@@ -1763,14 +2038,14 @@ struct anim *openanim(const char *name, int flags, int streamindex)
                return NULL;
        }
        IMB_freeImBuf(ibuf);
-       
+
        return(anim);
 }
 
 /* ************************* New Image API *************** */
 
 
-/* Notes about Image storage 
+/* Notes about Image storage
  * - packedfile
  *   -> written in .blend
  * - filename
@@ -1793,15 +2068,15 @@ struct anim *openanim(const char *name, int flags, int streamindex)
 Image *BKE_image_verify_viewer(int type, const char *name)
 {
        Image *ima;
-       
+
        for (ima = G.main->image.first; ima; ima = ima->id.next)
                if (ima->source == IMA_SRC_VIEWER)
                        if (ima->type == type)
                                break;
-       
+
        if (ima == NULL)
                ima = image_alloc(name, IMA_SRC_VIEWER, type);
-       
+
        /* happens on reload, imagewindow cannot be image user when hidden*/
        if (ima->id.us == 0)
                id_us_plus(&ima->id);
@@ -1814,11 +2089,72 @@ void BKE_image_assign_ibuf(Image *ima, ImBuf *ibuf)
        image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
 }
 
+void BKE_image_walk_all_users(const Main *mainp, void *customdata,
+                              void callback(Image *ima, ImageUser *iuser, void *customdata))
+{
+       wmWindowManager *wm;
+       wmWindow *win;
+       Tex *tex;
+
+       /* texture users */
+       for (tex = mainp->tex.first; tex; tex = tex->id.next) {
+               if (tex->type == TEX_IMAGE && tex->ima) {
+                       if (ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
+                               callback(tex->ima, &tex->iuser, customdata);
+                       }
+               }
+       }
+
+       /* image window, compo node users */
+       for (wm = mainp->wm.first; wm; wm = wm->id.next) { /* only 1 wm */
+               for (win = wm->windows.first; win; win = win->next) {
+                       ScrArea *sa;
+                       for (sa = win->screen->areabase.first; sa; sa = sa->next) {
+                               if (sa->spacetype == SPACE_VIEW3D) {
+                                       View3D *v3d = sa->spacedata.first;
+                                       BGpic *bgpic;
+                                       for (bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) {
+                                               callback(bgpic->ima, &bgpic->iuser, customdata);
+                                       }
+                               }
+                               else if (sa->spacetype == SPACE_IMAGE) {
+                                       SpaceImage *sima = sa->spacedata.first;
+                                       callback(sima->image, &sima->iuser, customdata);
+                               }
+                               else if (sa->spacetype == SPACE_NODE) {
+                                       SpaceNode *snode = sa->spacedata.first;
+                                       if ((snode->treetype == NTREE_COMPOSIT) && (snode->nodetree)) {
+                                               bNode *node;
+                                               for (node = snode->nodetree->nodes.first; node; node = node->next) {
+                                                       if (node->id && node->type == CMP_NODE_IMAGE) {
+                                                               Image *ima = (Image *)node->id;
+                                                               ImageUser *iuser = node->storage;
+                                                               callback(ima, iuser, customdata);
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+}
+
+static void image_tag_frame_recalc(Image *ima, ImageUser *iuser, void *customdata)
+{
+       Image *changed_image = customdata;
+
+       if (ima == changed_image) {
+               iuser->flag |= IMA_NEED_FRAME_RECALC;
+       }
+}
+
 void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
 {
        if (ima == NULL)
                return;
-       
+
+       BLI_spin_lock(&image_spin);
+
        switch (signal) {
                case IMA_SIGNAL_FREE:
                        image_free_buffers(ima);
@@ -1847,6 +2183,9 @@ void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
                        ima->ok = 1;
                        if (iuser)
                                iuser->ok = 1;
+
+                       BKE_image_walk_all_users(G.main, ima, image_tag_frame_recalc);
+
                        break;
 
                case IMA_SIGNAL_RELOAD:
@@ -1865,10 +2204,10 @@ void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
                        }
                        else
                                image_free_buffers(ima);
-               
+
                        if (iuser)
                                iuser->ok = 1;
-               
+
                        break;
                case IMA_SIGNAL_USER_NEW_IMAGE:
                        if (iuser) {
@@ -1881,8 +2220,19 @@ void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
                                }
                        }
                        break;
+               case IMA_SIGNAL_COLORMANAGE:
+                       image_free_buffers(ima);
+
+                       ima->ok = 1;
+
+                       if (iuser)
+                               iuser->ok = 1;
+
+                       break;
        }
-       
+
+       BLI_spin_unlock(&image_spin);
+
        /* don't use notifiers because they are not 100% sure to succeeded
         * this also makes sure all scenes are accounted for. */
        {
@@ -1902,13 +2252,13 @@ RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
 {
        RenderLayer *rl;
        RenderPass *rpass = NULL;
-       
+
        if (rr == NULL)
                return NULL;
-       
+
        if (iuser) {
                short index = 0, rl_index = 0, rp_index;
-               
+
                for (rl = rr->layers.first; rl; rl = rl->next, rl_index++) {
                        rp_index = 0;
                        for (rpass = rl->passes.first; rpass; rpass = rpass->next, index++, rp_index++)
@@ -1917,10 +2267,10 @@ RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
                        if (rpass)
                                break;
                }
-               
+
                if (rpass)
                        iuser->multi_index = index;
-               else 
+               else
                        iuser->multi_index = 0;
        }
        if (rpass == NULL) {
@@ -1928,7 +2278,7 @@ RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
                if (rl)
                        rpass = rl->passes.first;
        }
-       
+
        return rpass;
 }
 
@@ -1949,7 +2299,9 @@ RenderResult *BKE_image_acquire_renderresult(Scene *scene, Image *ima)
 
 void BKE_image_release_renderresult(Scene *scene, Image *ima)
 {
-       if (ima->rr) ;
+       if (ima->rr) {
+               /* pass */
+       }
        else if (ima->type == IMA_TYPE_R_RESULT) {
                if (ima->render_slot == ima->last_render_slot)
                        RE_ReleaseResult(RE_GetRender(scene->id.name));
@@ -1980,8 +2332,10 @@ void BKE_image_backup_render(Scene *scene, Image *ima)
 /* in that case we have to build a render-result */
 static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
 {
-       
-       ima->rr = RE_MultilayerConvert(ibuf->userdata, ibuf->x, ibuf->y);
+       const char *colorspace = ima->colorspace_settings.name;
+       int predivide = ima->flag & IMA_CM_PREDIVIDE;
+
+       ima->rr = RE_MultilayerConvert(ibuf->userdata, colorspace, predivide, ibuf->x, ibuf->y);
 
 #ifdef WITH_OPENEXR
        IMB_exr_close(ibuf->userdata);
@@ -2006,35 +2360,30 @@ static void image_initialize_after_load(Image *ima, ImBuf *ibuf)
        }
        /* timer */
        ima->lastused = clock() / CLOCKS_PER_SEC;
-       
+
        ima->ok = IMA_OK_LOADED;
-       
+
 }
 
 static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
 {
        struct ImBuf *ibuf;
-       unsigned short numlen;
-       char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
+       char name[FILE_MAX];
        int flag;
-       
+
        /* XXX temp stuff? */
        if (ima->lastframe != frame)
                ima->tpageflag |= IMA_TPAGE_REFRESH;
 
        ima->lastframe = frame;
-       BLI_strncpy(name, ima->name, sizeof(name));
-       BLI_stringdec(name, head, tail, &numlen);
-       BLI_stringenc(name, head, tail, numlen, frame);
+       BKE_image_user_file_path(iuser, ima, name);
 
-       BLI_path_abs(name, ID_BLEND_PATH(G.main, &ima->id));
-       
        flag = IB_rect | IB_multilayer;
        if (ima->flag & IMA_DO_PREMUL)
                flag |= IB_premul;
 
        /* read ibuf */
-       ibuf = IMB_loadiffname(name, flag);
+       ibuf = IMB_loadiffname(name, flag, ima->colorspace_settings.name);
 
 #if 0
        if (ibuf) {
@@ -2047,9 +2396,9 @@ static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
 
        if (ibuf) {
 #ifdef WITH_OPENEXR
-               /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
+               /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_acquire_ibuf */
                if (ibuf->ftype == OPENEXR && ibuf->userdata) {
-                       image_create_multilayer(ima, ibuf, frame);      
+                       image_create_multilayer(ima, ibuf, frame);
                        ima->type = IMA_TYPE_MULTILAYER;
                        IMB_freeImBuf(ibuf);
                        ibuf = NULL;
@@ -2065,27 +2414,27 @@ static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
        }
        else
                ima->ok = 0;
-       
+
        if (iuser)
                iuser->ok = ima->ok;
-       
+
        return ibuf;
 }
 
 static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int frame)
 {
        struct ImBuf *ibuf = NULL;
-       
+
        /* either we load from RenderResult, or we have to load a new one */
-       
+
        /* check for new RenderResult */
        if (ima->rr == NULL || frame != ima->rr->framenr) {
                /* copy to survive not found multilayer image */
                RenderResult *oldrr = ima->rr;
-       
+
                ima->rr = NULL;
                ibuf = image_load_sequence_file(ima, iuser, frame);
-               
+
                if (ibuf) { /* actually an error */
                        ima->type = IMA_TYPE_IMAGE;
                        printf("error, multi is normal image\n");
@@ -2103,7 +2452,7 @@ static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int f
        }
        if (ima->rr) {
                RenderPass *rpass = BKE_image_multilayer_index(ima->rr, iuser);
-               
+
                if (rpass) {
                        // printf("load from pass %s\n", rpass->name);
                        /* since we free  render results, we copy the rect */
@@ -2112,20 +2461,19 @@ static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int f
                        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);
-                       
+
                }
                // else printf("pass not found\n");
        }
        else
                ima->ok = 0;
-       
+
        if (iuser)
                iuser->ok = ima->ok;
-       
+
        return ibuf;
 }
 
@@ -2133,36 +2481,35 @@ static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int f
 static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
 {
        struct ImBuf *ibuf = NULL;
-       
+
        ima->lastframe = frame;
-       
+
        if (ima->anim == NULL) {
                char str[FILE_MAX];
-               
-               BLI_strncpy(str, ima->name, FILE_MAX);
-               BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
+
+               BKE_image_user_file_path(iuser, ima, str);
 
                /* FIXME: make several stream accessible in image editor, too*/
-               ima->anim = openanim(str, IB_rect, 0);
-               
+               ima->anim = openanim(str, IB_rect, 0, ima->colorspace_settings.name);
+
                /* let's initialize this user */
                if (ima->anim && iuser && iuser->frames == 0)
                        iuser->frames = IMB_anim_get_duration(ima->anim,
                                                              IMB_TC_RECORD_RUN);
        }
-       
+
        if (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_makeSingleUser(
                    IMB_anim_absolute(ima->anim, fra,
                                      IMB_TC_RECORD_RUN,
                                      IMB_PROXY_NONE));
-               
+
                if (ibuf) {
                        image_initialize_after_load(ima, ibuf);
                        image_assign_ibuf(ima, ibuf, 0, frame);
@@ -2172,10 +2519,10 @@ static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
        }
        else
                ima->ok = 0;
-       
+
        if (iuser)
                iuser->ok = ima->ok;
-       
+
        return ibuf;
 }
 
@@ -2185,35 +2532,35 @@ static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
        struct ImBuf *ibuf;
        char str[FILE_MAX];
        int assign = 0, flag;
-       
+
        /* always ensure clean ima */
        image_free_buffers(ima);
-       
+
        /* is there a PackedFile with this image ? */
        if (ima->packedfile) {
                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, "<packed data>");
-       } 
+
+               ibuf = IMB_ibImageFromMemory((unsigned char *)ima->packedfile->data, ima->packedfile->size, flag,
+                                            ima->colorspace_settings.name, "<packed data>");
+       }
        else {
                flag = IB_rect | IB_multilayer | IB_metadata;
                if (ima->flag & IMA_DO_PREMUL)
                        flag |= IB_premul;
-                       
+
                /* get the right string */
-               BLI_strncpy(str, ima->name, sizeof(str));
-               BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
-               
+               BKE_image_user_frame_calc(iuser, cfra, 0);
+               BKE_image_user_file_path(iuser, ima, str);
+
                /* read ibuf */
-               ibuf = IMB_loadiffname(str, flag);
+               ibuf = IMB_loadiffname(str, flag, ima->colorspace_settings.name);
        }
-       
+
        if (ibuf) {
-               /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
+               /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_acquire_ibuf */
                if (ibuf->ftype == OPENEXR && ibuf->userdata) {
-                       image_create_multilayer(ima, ibuf, cfra);       
+                       image_create_multilayer(ima, ibuf, cfra);
                        ima->type = IMA_TYPE_MULTILAYER;
                        IMB_freeImBuf(ibuf);
                        ibuf = NULL;
@@ -2224,7 +2571,7 @@ static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
 
                        /* check if the image is a font image... */
                        detectBitmapFont(ibuf);
-                       
+
                        /* make packed file for autopack */
                        if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK))
                                ima->packedfile = newPackedFile(NULL, str, ID_BLEND_PATH(G.main, &ima->id));
@@ -2232,20 +2579,20 @@ static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
        }
        else
                ima->ok = 0;
-       
+
        if (assign)
                image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
 
        if (iuser)
                iuser->ok = ima->ok;
-       
+
        return ibuf;
 }
 
 static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
 {
        ImBuf *ibuf = NULL;
-       
+
        if (ima->rr == NULL) {
                ibuf = image_load_image_file(ima, iuser, 0);
                if (ibuf) { /* actually an error */
@@ -2258,23 +2605,22 @@ static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
 
                if (rpass) {
                        ibuf = IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
-                       
+
                        image_initialize_after_load(ima, ibuf);
-                       
+
                        ibuf->rect_float = rpass->rect;
                        ibuf->flags |= IB_rectfloat;
                        ibuf->channels = rpass->channels;
-                       ibuf->profile = IB_PROFILE_LINEAR_RGB;
 
                        image_assign_ibuf(ima, ibuf, iuser ? iuser->multi_index : IMA_NO_INDEX, 0);
                }
        }
-       
+
        if (ibuf == NULL)
                ima->ok = 0;
        if (iuser)
                iuser->ok = ima->ok;
-       
+
        return ibuf;
 }
 
@@ -2315,7 +2661,7 @@ static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_
        }
        else
                memset(&rres, 0, sizeof(RenderResult));
-       
+
        if (!(rres.rectx > 0 && rres.recty > 0)) {
                if (from_render)
                        RE_ReleaseResultImage(re);
@@ -2328,14 +2674,16 @@ static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_
                *lock_r = re;
        }
 
-       /* this gives active layer, composite or seqence result */
+       /* this gives active layer, composite or sequence result */
        rect = (unsigned int *)rres.rect32;
        rectf = rres.rectf;
        rectz = rres.rectz;
        dither = iuser->scene->r.dither_intensity;
 
        /* combined layer gets added as first layer */
-       if (rres.have_combined && layer == 0) ;
+       if (rres.have_combined && layer == 0) {
+               /* pass */
+       }
        else if (rres.layers.first) {
                RenderLayer *rl = BLI_findlink(&rres.layers, layer - (rres.have_combined ? 1 : 0));
                if (rl) {
@@ -2368,18 +2716,34 @@ static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_
                image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
        }
 
+       /* invalidate color managed buffers if render result changed */
+       BLI_lock_thread(LOCK_COLORMANAGE);
+       if (ibuf->x != rres.rectx || ibuf->y != rres.recty || ibuf->rect_float != rectf) {
+               ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
+       }
+
        ibuf->x = rres.rectx;
        ibuf->y = rres.recty;
-       
+
        /* free rect buffer if float buffer changes, so it can be recreated with
         * the updated result, and also in case we got byte buffer from sequencer,
         * so we don't keep reference to freed buffer */
-       if (ibuf->rect_float != rectf || rect || !rectf)
+       if (ibuf->rect_float != rectf || rect)
                imb_freerectImBuf(ibuf);
 
-       if (rect)
+       if (rect) {
                ibuf->rect = rect;
-       
+       }
+       else {
+               /* byte buffer of render result has been freed, make sure image buffers
+                * does not reference to this buffer anymore
+                * need check for whether byte buffer was allocated and owned by image itself
+                * or if it's reusing buffer from render result
+                */
+               if ((ibuf->mall & IB_rect) == 0)
+                       ibuf->rect = NULL;
+       }
+
        if (rectf) {
                ibuf->rect_float = rectf;
                ibuf->flags |= IB_rectfloat;
@@ -2399,8 +2763,8 @@ 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;
+       BLI_unlock_thread(LOCK_COLORMANAGE);
+
        ibuf->dither = dither;
 
        if (iuser->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT_PREDIVIDE) {
@@ -2435,13 +2799,13 @@ static ImBuf *image_get_ibuf_threadsafe(Image *ima, ImageUser *iuser, int *frame
                if (ima->type == IMA_TYPE_IMAGE) {
                        frame = iuser ? iuser->framenr : ima->lastframe;
                        ibuf = image_get_ibuf(ima, 0, frame);
-                       
+
                        /* XXX temp stuff? */
                        if (ima->lastframe != frame) {
                                ima->tpageflag |= IMA_TPAGE_REFRESH;
                        }
                        ima->lastframe = frame;
-               }       
+               }
                else if (ima->type == IMA_TYPE_MULTILAYER) {
                        frame = iuser ? iuser->framenr : ima->lastframe;
                        index = iuser ? iuser->multi_index : IMA_NO_INDEX;
@@ -2463,133 +2827,104 @@ static ImBuf *image_get_ibuf_threadsafe(Image *ima, ImageUser *iuser, int *frame
                 * a big bottleneck */
        }
 
-       *frame_r = frame;
-       *index_r = index;
+       if (frame_r)
+               *frame_r = frame;
+
+       if (index_r)
+               *index_r = index;
 
        return ibuf;
 }
 
-/* Checks optional ImageUser and verifies/creates ImBuf. */
-/* use this one if you want to get a render result in progress,
- * if not, use BKE_image_get_ibuf which doesn't require a release */
-ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
+/* Checks optional ImageUser and verifies/creates ImBuf.
+ *
+ * not thread-safe, so callee should worry about thread locks
+ */
+static ImBuf *image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
 {
        ImBuf *ibuf = NULL;
        float color[] = {0, 0, 0, 1};
        int frame = 0, index = 0;
 
-       /* This function is intended to be thread-safe. It postpones the mutex lock
-        * until it needs to load the image, if the image is already there it
-        * should just get the pointer and return. The reason is that a lot of mutex
-        * locks appears to be very slow on certain multicore macs, causing a render
-        * with image textures to actually slow down as more threads are used.
-        *
-        * Note that all the image loading functions should also make sure they do
-        * things in a threadsafe way for image_get_ibuf_threadsafe to work correct.
-        * That means, the last two steps must be, 1) add the ibuf to the list and
-        * 2) set ima/iuser->ok to 0 to IMA_OK_LOADED */
-       
        if (lock_r)
                *lock_r = NULL;
 
        /* quick reject tests */
        if (ima == NULL)
                return NULL;
+
        if (iuser) {
                if (iuser->ok == 0)
                        return NULL;
        }
        else if (ima->ok == 0)
                return NULL;
-       
-       /* try to get the ibuf without locking */
+
        ibuf = image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
 
        if (ibuf == NULL) {
-               /* couldn't get ibuf and image is not ok, so let's lock and try to
-                * load the image */
-               BLI_lock_thread(LOCK_IMAGE);
-
-               /* need to check ok flag and loading ibuf again, because the situation
-                * might have changed in the meantime */
-               if (iuser) {
-                       if (iuser->ok == 0) {
-                               BLI_unlock_thread(LOCK_IMAGE);
-                               return NULL;
-                       }
+               /* we are sure we have to load the ibuf, using source and type */
+               if (ima->source == IMA_SRC_MOVIE) {
+                       /* source is from single file, use flipbook to store ibuf */
+                       ibuf = image_load_movie_file(ima, iuser, frame);
                }
-               else if (ima->ok == 0) {
-                       BLI_unlock_thread(LOCK_IMAGE);
-                       return NULL;
+               else if (ima->source == IMA_SRC_SEQUENCE) {
+                       if (ima->type == IMA_TYPE_IMAGE) {
+                               /* regular files, ibufs in flipbook, allows saving */
+                               ibuf = image_load_sequence_file(ima, iuser, frame);
+                       }
+                       /* no else; on load the ima type can change */
+                       if (ima->type == IMA_TYPE_MULTILAYER) {
+                               /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
+                               ibuf = image_load_sequence_multilayer(ima, iuser, frame);
+                       }
                }
+               else if (ima->source == IMA_SRC_FILE) {
 
-               ibuf = image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
+                       if (ima->type == IMA_TYPE_IMAGE)
+                               ibuf = image_load_image_file(ima, iuser, frame);  /* cfra only for '#', this global is OK */
+                       /* no else; on load the ima type can change */
+                       if (ima->type == IMA_TYPE_MULTILAYER)
+                               /* keeps render result, stores ibufs in listbase, allows saving */
+                               ibuf = image_get_ibuf_multilayer(ima, iuser);
 
-               if (ibuf == NULL) {
-                       /* we are sure we have to load the ibuf, using source and type */
-                       if (ima->source == IMA_SRC_MOVIE) {
-                               /* source is from single file, use flipbook to store ibuf */
-                               ibuf = image_load_movie_file(ima, iuser, frame);
-                       }
-                       else if (ima->source == IMA_SRC_SEQUENCE) {
-                               if (ima->type == IMA_TYPE_IMAGE) {
-                                       /* regular files, ibufs in flipbook, allows saving */
-                                       ibuf = image_load_sequence_file(ima, iuser, frame);
-                               }
-                               /* no else; on load the ima type can change */
-                               if (ima->type == IMA_TYPE_MULTILAYER) {
-                                       /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
-                                       ibuf = image_load_sequence_multilayer(ima, iuser, frame);
-                               }
-                       }
-                       else if (ima->source == IMA_SRC_FILE) {
-                               
-                               if (ima->type == IMA_TYPE_IMAGE)
-                                       ibuf = image_load_image_file(ima, iuser, frame);  /* cfra only for '#', this global is OK */
-                               /* no else; on load the ima type can change */
-                               if (ima->type == IMA_TYPE_MULTILAYER)
-                                       /* keeps render result, stores ibufs in listbase, allows saving */
-                                       ibuf = image_get_ibuf_multilayer(ima, iuser);
-                                       
-                       }
-                       else if (ima->source == IMA_SRC_GENERATED) {
-                               /* generated is: ibuf is allocated dynamically */
-                               /* 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, (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;
+               }
+               else if (ima->source == IMA_SRC_GENERATED) {
+                       /* generated is: ibuf is allocated dynamically */
+                       /* 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, (ima->gen_flag & IMA_GEN_FLOAT) != 0, ima->gen_type,
+                                            color, &ima->colorspace_settings);
+                       image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
+                       ima->ok = IMA_OK_LOADED;
+               }
+               else if (ima->source == IMA_SRC_VIEWER) {
+                       if (ima->type == IMA_TYPE_R_RESULT) {
+                               /* always verify entirely, and potentially
+                                * returns pointer to release later */
+                               ibuf = image_get_render_result(ima, iuser, lock_r);
                        }
-                       else if (ima->source == IMA_SRC_VIEWER) {
-                               if (ima->type == IMA_TYPE_R_RESULT) {
-                                       /* always verify entirely, and potentially
-                                        * returns pointer to release later */
-                                       ibuf = image_get_render_result(ima, iuser, lock_r);
-                               }
-                               else if (ima->type == IMA_TYPE_COMPOSITE) {
-                                       /* requires lock/unlock, otherwise don't return image */
-                                       if (lock_r) {
-                                               /* unlock in BKE_image_release_ibuf */
-                                               BLI_lock_thread(LOCK_VIEWER);
-                                               *lock_r = ima;
-
-                                               /* 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);
-                                                       image_assign_ibuf(ima, ibuf, 0, frame);
-                                               }
+                       else if (ima->type == IMA_TYPE_COMPOSITE) {
+                               /* requires lock/unlock, otherwise don't return image */
+                               if (lock_r) {
+                                       /* unlock in BKE_image_release_ibuf */
+                                       BLI_lock_thread(LOCK_VIEWER);
+                                       *lock_r = ima;
+
+                                       /* 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);
+                                               image_assign_ibuf(ima, ibuf, 0, frame);
                                        }
                                }
                        }
                }
-
-               BLI_unlock_thread(LOCK_IMAGE);
        }
 
        BKE_image_tag_time(ima);
@@ -2597,29 +2932,89 @@ ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
        return ibuf;
 }
 
-void BKE_image_release_ibuf(Image *ima, void *lock)
+/* return image buffer for given image and user
+ *
+ * - will lock render result if image type is render result and lock is not NULL
+ * - will return NULL if image type if render or composite result and lock is NULL
+ *
+ * references the result, BKE_image_release_ibuf should be used to de-reference
+ */
+ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
+{
+       ImBuf *ibuf;
+
+       BLI_spin_lock(&image_spin);
+
+       ibuf = image_acquire_ibuf(ima, iuser, lock_r);
+
+       if (ibuf)
+               IMB_refImBuf(ibuf);
+
+       BLI_spin_unlock(&image_spin);
+
+       return ibuf;
+}
+
+void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
 {
-       /* for getting image during threaded render / compositing, need to release */
-       if (lock == ima) {
-               BLI_unlock_thread(LOCK_VIEWER); /* viewer image */
+       if (lock) {
+               /* for getting image during threaded render / compositing, need to release */
+               if (lock == ima) {
+                       BLI_unlock_thread(LOCK_VIEWER); /* viewer image */
+               }
+               else if (lock) {
+                       RE_ReleaseResultImage(lock); /* render result */
+                       BLI_unlock_thread(LOCK_VIEWER); /* view image imbuf */
+               }
        }
-       else if (lock) {
-               RE_ReleaseResultImage(lock); /* render result */
-               BLI_unlock_thread(LOCK_VIEWER); /* view image imbuf */
+
+       if (ibuf) {
+               BLI_spin_lock(&image_spin);
+               IMB_freeImBuf(ibuf);
+               BLI_spin_unlock(&image_spin);
        }
 }
 
-/* warning, this can allocate generated images */
-ImBuf *BKE_image_get_ibuf(Image *ima, ImageUser *iuser)
+/* checks whether there's an image buffer for given image and user */
+int BKE_image_has_ibuf(Image *ima, ImageUser *iuser)
 {
-       /* here (+fie_ima/2-1) makes sure that division happens correctly */
-       return BKE_image_acquire_ibuf(ima, iuser, NULL);
+       ImBuf *ibuf;
+
+       /* quick reject tests */
+       if (ima == NULL)
+               return FALSE;
+
+       if (iuser) {
+               if (iuser->ok == 0)
+                       return FALSE;
+       }
+       else if (ima->ok == 0)
+               return FALSE;
+
+       ibuf = image_get_ibuf_threadsafe(ima, iuser, NULL, NULL);
+
+       if (!ibuf) {
+               BLI_spin_lock(&image_spin);
+
+               ibuf = image_get_ibuf_threadsafe(ima, iuser, NULL, NULL);
+
+               if (!ibuf)
+                       ibuf = image_acquire_ibuf(ima, iuser, NULL);
+
+               BLI_spin_unlock(&image_spin);
+       }
+
+       return ibuf != NULL;
 }
 
-int BKE_image_user_frame_get(const ImageUser *iuser, int cfra, int fieldnr)
+int BKE_image_user_frame_get(const ImageUser *iuser, int cfra, int fieldnr, short *r_is_in_range)
 {
        const int len = (iuser->fie_ima * iuser->frames) / 2;
 
+       if (r_is_in_range) {
+               *r_is_in_range = FALSE;
+       }
+
        if (len == 0) {
                return 0;
        }
@@ -2632,10 +3027,23 @@ int BKE_image_user_frame_get(const ImageUser *iuser, int cfra, int fieldnr)
                        cfra = ((cfra) % len);
                        if (cfra < 0) cfra += len;
                        if (cfra == 0) cfra = len;
+
+                       if (r_is_in_range) {
+                               *r_is_in_range = TRUE;
+                       }
                }
 
-               if (cfra < 0) cfra = 0;
-               else if (cfra > len) cfra = len;
+               if (cfra < 0) {
+                       cfra = 0;
+               }
+               else if (cfra > len) {
+                       cfra = len;
+               }
+               else {
+                       if (r_is_in_range) {
+                               *r_is_in_range = TRUE;
+                       }
+               }
 
                /* convert current frame to current field */
                cfra = 2 * (cfra);
@@ -2644,7 +3052,6 @@ int BKE_image_user_frame_get(const ImageUser *iuser, int cfra, int fieldnr)
                /* transform to images space */
                framenr = (cfra + iuser->fie_ima - 2) / iuser->fie_ima;
                if (framenr > iuser->frames) framenr = iuser->frames;
-               framenr += iuser->offset;
 
                if (iuser->cycl) {
                        framenr = ((framenr) % len);
@@ -2652,21 +3059,59 @@ int BKE_image_user_frame_get(const ImageUser *iuser, int cfra, int fieldnr)
                        if (framenr == 0) framenr = len;
                }
 
+               /* important to apply after else we cant loop on frames 100 - 110 for eg. */
+               framenr += iuser->offset;
+
                return framenr;
        }
 }
 
 void BKE_image_user_frame_calc(ImageUser *iuser, int cfra, int fieldnr)
 {
-       const int framenr = BKE_image_user_frame_get(iuser, cfra, fieldnr);
+       if (iuser) {
+               short is_in_range;
+               const int framenr = BKE_image_user_frame_get(iuser, cfra, fieldnr, &is_in_range);
 
-       /* allows image users to handle redraws */
-       if (iuser->flag & IMA_ANIM_ALWAYS)
-               if (framenr != iuser->framenr)
-                       iuser->flag |= IMA_ANIM_REFRESHED;
+               if (is_in_range) {
+                       iuser->flag |= IMA_USER_FRAME_IN_RANGE;
+               }
+               else {
+                       iuser->flag &= ~IMA_USER_FRAME_IN_RANGE;
+               }
 
-       iuser->framenr = framenr;
-       if (iuser->ok == 0) iuser->ok = 1;
+               /* 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;
+       }
+}
+
+void BKE_image_user_check_frame_calc(ImageUser *iuser, int cfra, int fieldnr)
+{
+       if ((iuser->flag & IMA_ANIM_ALWAYS) || (iuser->flag & IMA_NEED_FRAME_RECALC)) {
+               BKE_image_user_frame_calc(iuser, cfra, fieldnr);
+
+               iuser->flag &= ~IMA_NEED_FRAME_RECALC;
+       }
+}
+
+void BKE_image_user_file_path(ImageUser *iuser, Image *ima, char *filepath)
+{
+       BLI_strncpy(filepath, ima->name, FILE_MAX);
+
+       if (ima->source == IMA_SRC_SEQUENCE) {
+               char head[FILE_MAX], tail[FILE_MAX];
+               unsigned short numlen;
+               int frame = iuser ? iuser->framenr : ima->lastframe;
+
+               BLI_stringdec(filepath, head, tail, &numlen);
+               BLI_stringenc(filepath, head, tail, numlen, frame);
+       }
+
+       BLI_path_abs(filepath, ID_BLEND_PATH(G.main, &ima->id));
 }
 
 int BKE_image_has_alpha(struct Image *image)
@@ -2674,10 +3119,10 @@ 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);
+       BKE_image_release_ibuf(image, ibuf, lock);
 
        if (planes == 32)
                return 1;
@@ -2685,3 +3130,42 @@ int BKE_image_has_alpha(struct Image *image)
                return 0;
 }
 
+void BKE_image_get_size(Image *image, ImageUser *iuser, int *width, int *height)
+{
+       ImBuf *ibuf = NULL;
+       void *lock;
+
+       ibuf = BKE_image_acquire_ibuf(image, iuser, &lock);
+
+       if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
+               *width = ibuf->x;
+               *height = ibuf->y;
+       }
+       else {
+               *width  = IMG_SIZE_FALLBACK;
+               *height = IMG_SIZE_FALLBACK;
+       }
+
+       BKE_image_release_ibuf(image, ibuf, lock);
+}
+
+void BKE_image_get_size_fl(Image *image, ImageUser *iuser, float size[2])
+{
+       int width, height;
+       BKE_image_get_size(image, iuser, &width, &height);
+
+       size[0] = (float)width;
+       size[1] = (float)height;
+
+}
+
+void BKE_image_get_aspect(Image *image, float *aspx, float *aspy)
+{
+       *aspx = 1.0;
+
+       /* x is always 1 */
+       if (image)
+               *aspy = image->aspy / image->aspx;
+       else
+               *aspy = 1.0f;
+}