Color Management, Stage 2: Switch color pipeline to use OpenColorIO
[blender.git] / source / blender / editors / space_clip / clip_editor.c
index 5b4849a425fa0681e5ab8d7c197378c1a2d722fe..f2a48f299cfd1d50c8324d150ffa352fe13313b1 100644 (file)
@@ -44,6 +44,7 @@
 
 #include "BLI_utildefines.h"
 #include "BLI_math.h"
+#include "BLI_rect.h"
 
 #include "GPU_extensions.h"
 
@@ -148,8 +149,8 @@ void ED_space_clip_get_zoom(SpaceClip *sc, ARegion *ar, float *zoomx, float *zoo
 
        ED_space_clip_get_size(sc, &width, &height);
 
-       *zoomx = (float)(ar->winrct.xmax - ar->winrct.xmin + 1) / (float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin) * width);
-       *zoomy = (float)(ar->winrct.ymax - ar->winrct.ymin + 1) / (float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin) * height);
+       *zoomx = (float)(BLI_RCT_SIZE_X(&ar->winrct) + 1) / (float)(BLI_RCT_SIZE_X(&ar->v2d.cur) * width);
+       *zoomy = (float)(BLI_RCT_SIZE_Y(&ar->winrct) + 1) / (float)(BLI_RCT_SIZE_Y(&ar->v2d.cur) * height);
 }
 
 void ED_space_clip_get_aspect(SpaceClip *sc, float *aspx, float *aspy)
@@ -157,7 +158,7 @@ void ED_space_clip_get_aspect(SpaceClip *sc, float *aspx, float *aspy)
        MovieClip *clip = ED_space_clip_get_clip(sc);
 
        if (clip)
-               BKE_movieclip_aspect(clip, aspx, aspy);
+               BKE_movieclip_get_aspect(clip, aspx, aspy);
        else
                *aspx = *aspy = 1.0f;
 
@@ -276,13 +277,7 @@ int ED_space_clip_color_sample(SpaceClip *sc, ARegion *ar, int mval[2], float r_
 
                if (ibuf->rect_float) {
                        fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
-                       /* IB_PROFILE_NONE is default but infact its linear */
-                       if (ELEM(ibuf->profile, IB_PROFILE_LINEAR_RGB, IB_PROFILE_NONE)) {
-                               linearrgb_to_srgb_v3_v3(r_col, fp);
-                       }
-                       else {
-                               copy_v3_v3(r_col, fp);
-                       }
+                       linearrgb_to_srgb_v3_v3(r_col, fp);
                        ret = TRUE;
                }
                else if (ibuf->rect) {
@@ -393,8 +388,8 @@ int ED_clip_view_selection(const bContext *C, ARegion *ar, int fit)
 
                ED_space_clip_get_aspect(sc, &aspx, &aspy);
 
-               width = ar->winrct.xmax - ar->winrct.xmin + 1;
-               height = ar->winrct.ymax - ar->winrct.ymin + 1;
+               width  = BLI_RCT_SIZE_X(&ar->winrct) + 1;
+               height = BLI_RCT_SIZE_Y(&ar->winrct) + 1;
 
                zoomx = (float)width / w / aspx;
                zoomy = (float)height / h / aspy;
@@ -582,12 +577,15 @@ typedef struct SpaceClipDrawContext {
        GLuint texture;                 /* OGL texture ID */
        short texture_allocated;        /* flag if texture was allocated by glGenTextures */
        struct ImBuf *texture_ibuf;     /* image buffer for which texture was created */
+       const unsigned char *display_buffer; /* display buffer for which texture was created */
        int image_width, image_height;  /* image width and height for which texture was created */
        unsigned last_texture;          /* ID of previously used texture, so it'll be restored after clip drawing */
 
        /* fields to check if cache is still valid */
        int framenr, start_frame, frame_offset;
        short render_size, render_flag;
+
+       char colorspace[64];
 } SpaceClipDrawContext;
 
 int ED_space_clip_texture_buffer_supported(SpaceClip *sc)
@@ -612,7 +610,7 @@ int ED_space_clip_texture_buffer_supported(SpaceClip *sc)
        return context->buffers_supported;
 }
 
-int ED_space_clip_load_movieclip_buffer(SpaceClip *sc, ImBuf *ibuf)
+int ED_space_clip_load_movieclip_buffer(SpaceClip *sc, ImBuf *ibuf, const unsigned char *display_buffer)
 {
        SpaceClipDrawContext *context = sc->draw_context;
        MovieClip *clip = ED_space_clip_get_clip(sc);
@@ -624,12 +622,22 @@ int ED_space_clip_load_movieclip_buffer(SpaceClip *sc, ImBuf *ibuf)
         * assuming displaying happens of footage frames only on which painting doesn't heppen.
         * so not changed image buffer pointer means unchanged image content */
        need_rebind |= context->texture_ibuf != ibuf;
+       need_rebind |= context->display_buffer != display_buffer;
        need_rebind |= context->framenr != sc->user.framenr;
        need_rebind |= context->render_size != sc->user.render_size;
        need_rebind |= context->render_flag != sc->user.render_flag;
        need_rebind |= context->start_frame != clip->start_frame;
        need_rebind |= context->frame_offset != clip->frame_offset;
 
+       if (!need_rebind) {
+               /* OCIO_TODO: not entirely nice, but currently it seems to be easiest way
+                *            to deal with changing input color space settings
+                *            pointer-based check could fail due to new buffers could be
+                *            be allocated on on old memory
+                */
+               need_rebind = strcmp(context->colorspace, clip->colorspace_settings.name) != 0;
+       }
+
        if (need_rebind) {
                int width = ibuf->x, height = ibuf->y;
                int need_recreate = 0;
@@ -669,16 +677,12 @@ int ED_space_clip_load_movieclip_buffer(SpaceClip *sc, ImBuf *ibuf)
                        glBindTexture(GL_TEXTURE_2D, context->texture);
                }
 
-               if (ibuf->rect_float) {
-                       if (ibuf->rect == NULL)
-                               IMB_rect_from_float(ibuf);
-               }
-
-               if (ibuf->rect)
-                       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
+               if (display_buffer)
+                       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, display_buffer);
 
                /* store settings */
                context->texture_allocated = 1;
+               context->display_buffer = display_buffer;
                context->texture_ibuf = ibuf;
                context->image_width = ibuf->x;
                context->image_height = ibuf->y;
@@ -687,6 +691,8 @@ int ED_space_clip_load_movieclip_buffer(SpaceClip *sc, ImBuf *ibuf)
                context->render_flag = sc->user.render_flag;
                context->start_frame = clip->start_frame;
                context->frame_offset = clip->frame_offset;
+
+               strcpy(context->colorspace, clip->colorspace_settings.name);
        }
        else {
                /* displaying exactly the same image which was loaded t oa texture,