Color Management, Stage 2: Switch color pipeline to use OpenColorIO
[blender.git] / source / blender / imbuf / intern / readimage.c
index 4bde71588b703d880ddafde29938d5f81f29fe18..a1fa05d1098c976da42b1489411e85b42c5a7a30 100644 (file)
 #include "IMB_imbuf.h"
 #include "IMB_filetype.h"
 
-ImBuf *IMB_ibImageFromMemory(unsigned char *mem, size_t size, int flags, const char *descr)
+#include "IMB_colormanagement.h"
+#include "IMB_colormanagement_intern.h"
+
+ImBuf *IMB_ibImageFromMemory(unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE], const char *descr)
 {
        ImBuf *ibuf;
        ImFileType *type;
+       char effective_colorspace[IM_MAX_SPACE] = "";
 
        if (mem == NULL) {
                fprintf(stderr, "%s: NULL pointer\n", __func__);
                return NULL;
        }
 
+       if (colorspace)
+               BLI_strncpy(effective_colorspace, colorspace, sizeof(effective_colorspace));
+
        for (type = IMB_FILE_TYPES; type->is_a; type++) {
                if (type->load) {
-                       ibuf = type->load(mem, size, flags);
+                       ibuf = type->load(mem, size, flags, effective_colorspace);
                        if (ibuf) {
+                               if (colorspace) {
+                                       if (ibuf->rect) {
+                                               /* byte buffer is never internally converted to some standard space,
+                                                * store pointer to it's color space descriptor instead
+                                                */
+                                               ibuf->rect_colorspace = colormanage_colorspace_get_named(effective_colorspace);
+                                       }
+
+                                       BLI_strncpy(colorspace, effective_colorspace, IM_MAX_SPACE);
+                               }
+
+                               /* OCIO_TODO: in some cases it's faster to do threaded conversion,
+                                *            but how to distinguish such cases */
+                               colormanage_imbuf_make_linear(ibuf, effective_colorspace);
+
                                if (flags & IB_premul) {
                                        IMB_premultiply_alpha(ibuf);
                                        ibuf->flags |= IB_premul;
@@ -83,7 +105,7 @@ ImBuf *IMB_ibImageFromMemory(unsigned char *mem, size_t size, int flags, const c
        return NULL;
 }
 
-ImBuf *IMB_loadifffile(int file, int flags, const char *descr)
+ImBuf *IMB_loadifffile(int file, int flags, char colorspace[IM_MAX_SPACE], const char *descr)
 {
        ImBuf *ibuf;
        unsigned char *mem;
@@ -99,7 +121,7 @@ ImBuf *IMB_loadifffile(int file, int flags, const char *descr)
                return NULL;
        }
 
-       ibuf = IMB_ibImageFromMemory(mem, size, flags, descr);
+       ibuf = IMB_ibImageFromMemory(mem, size, flags, colorspace, descr);
 
        if (munmap(mem, size))
                fprintf(stderr, "%s: couldn't unmap file %s\n", __func__, descr);
@@ -122,7 +144,7 @@ static void imb_cache_filename(char *filename, const char *name, int flags)
        BLI_strncpy(filename, name, IB_FILENAME_SIZE);
 }
 
-ImBuf *IMB_loadiffname(const char *filepath, int flags)
+ImBuf *IMB_loadiffname(const char *filepath, int flags, char colorspace[IM_MAX_SPACE])
 {
        ImBuf *ibuf;
        int file, a;
@@ -133,7 +155,7 @@ ImBuf *IMB_loadiffname(const char *filepath, int flags)
        file = BLI_open(filepath_tx, O_BINARY | O_RDONLY, 0);
        if (file < 0) return NULL;
 
-       ibuf = IMB_loadifffile(file, flags, filepath_tx);
+       ibuf = IMB_loadifffile(file, flags, colorspace, filepath_tx);
 
        if (ibuf) {
                BLI_strncpy(ibuf->name, filepath, sizeof(ibuf->name));
@@ -148,7 +170,7 @@ ImBuf *IMB_loadiffname(const char *filepath, int flags)
        return ibuf;
 }
 
-ImBuf *IMB_testiffname(const char *filepath, int flags)
+ImBuf *IMB_testiffname(const char *filepath, int flags, char colorspace[IM_MAX_SPACE])
 {
        ImBuf *ibuf;
        int file;
@@ -159,7 +181,7 @@ ImBuf *IMB_testiffname(const char *filepath, int flags)
        file = BLI_open(filepath_tx, O_BINARY | O_RDONLY, 0);
        if (file < 0) return NULL;
 
-       ibuf = IMB_loadifffile(file, flags | IB_test | IB_multilayer, filepath_tx);
+       ibuf = IMB_loadifffile(file, flags | IB_test | IB_multilayer, colorspace, filepath_tx);
 
        if (ibuf) {
                BLI_strncpy(ibuf->name, filepath, sizeof(ibuf->name));