Color Management, Stage 2: Switch color pipeline to use OpenColorIO
[blender.git] / source / blender / editors / space_clip / clip_editor.c
index 5e4ef1aa24a8d2cdcf74f5459936aa5ed1bcbe0a..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;
 
@@ -247,6 +248,48 @@ ImBuf *ED_space_clip_get_stable_buffer(SpaceClip *sc, float loc[2], float *scale
        return NULL;
 }
 
+/* returns color in SRGB */
+/* matching ED_space_image_color_sample() */
+int ED_space_clip_color_sample(SpaceClip *sc, ARegion *ar, int mval[2], float r_col[3])
+{
+       ImBuf *ibuf;
+       float fx, fy, co[2];
+       int ret = FALSE;
+
+       ibuf = ED_space_clip_get_buffer(sc);
+       if (!ibuf) {
+               return FALSE;
+       }
+
+       /* map the mouse coords to the backdrop image space */
+       ED_clip_mouse_pos(sc, ar, mval, co);
+
+       fx = co[0];
+       fy = co[1];
+
+       if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
+               float *fp;
+               unsigned char *cp;
+               int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
+
+               CLAMP(x, 0, ibuf->x - 1);
+               CLAMP(y, 0, ibuf->y - 1);
+
+               if (ibuf->rect_float) {
+                       fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
+                       linearrgb_to_srgb_v3_v3(r_col, fp);
+                       ret = TRUE;
+               }
+               else if (ibuf->rect) {
+                       cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
+                       rgb_uchar_to_float(r_col, cp);
+                       ret = TRUE;
+               }
+       }
+
+       return ret;
+}
+
 void ED_clip_update_frame(const Main *mainp, int cfra)
 {
        wmWindowManager *wm;
@@ -345,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;
@@ -534,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)
@@ -564,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);
@@ -576,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;
@@ -621,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;
@@ -639,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,