Merging r49212 through r49222 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_image / image_draw.c
index 52ed7fd1d0b8b6144b88d118f710ce6a0bab5b51..c02c1ffe867fba12ab0f1ffee1c2cc394d566841 100644 (file)
@@ -50,6 +50,7 @@
 
 #include "IMB_imbuf.h"
 #include "IMB_imbuf_types.h"
+#include "IMB_colormanagement.h"
 
 #include "BKE_context.h"
 #include "BKE_global.h"
@@ -69,6 +70,8 @@
 #include "UI_resources.h"
 #include "UI_view2d.h"
 
+#include "WM_api.h"
+#include "WM_types.h"
 
 #include "RE_pipeline.h"
 
 
 #define HEADER_HEIGHT 18
 
+/* OCIO_TODO: after finishing proper color management pipeline integration
+ *            this wouldn't be needed -- color managed display buffer
+ *            would be used unstead
+ */
 static void image_verify_buffer_float(Image *ima, ImBuf *ibuf, int color_manage)
 {
        /* detect if we need to redo the curve map.
@@ -93,7 +100,12 @@ static void image_verify_buffer_float(Image *ima, ImBuf *ibuf, int color_manage)
                else
                        ibuf->profile = IB_PROFILE_NONE;
 
-               IMB_rect_from_float(ibuf);
+               /* OCIO_TODO: currently only get rid of old-style color managed byte
+                *            buffer calculation to save some time on buffer display,
+                *            but still need to set image buffer's profile to prevent
+                *            comatibility breackage
+                */
+               /* IMB_rect_from_float(ibuf); */
        }
 }
 
@@ -417,7 +429,7 @@ static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rec
        MEM_freeN(rectf);
 }
 
-static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
+static void draw_image_buffer(wmWindow *win, SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
 {
        int x, y;
        int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
@@ -444,6 +456,10 @@ static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, Image
                        sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
        }
        else {
+               const ColorManagedViewSettings *view_settings;
+               unsigned char *display_buffer;
+               void *cache_handle;
+
                if (sima->flag & SI_USE_ALPHA) {
                        fdrawcheckerboard(x, y, x + ibuf->x * zoomx, y + ibuf->y * zoomy);
 
@@ -455,13 +471,18 @@ static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, Image
                 * convert them, and optionally apply curves */
                image_verify_buffer_float(ima, ibuf, color_manage);
 
-               if (ibuf->rect)
-                       glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
+               view_settings = IMB_view_settings_get_effective(win, &sima->view_settings);
+               display_buffer = IMB_display_buffer_acquire(ibuf, view_settings, &win->display_settings, &cache_handle);
+
+               if (display_buffer)
+                       glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, display_buffer);
 #if 0
                else
                        glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
 #endif
-               
+
+               IMB_display_buffer_release(cache_handle);
+
                if (sima->flag & SI_USE_ALPHA)
                        glDisable(GL_BLEND);
        }
@@ -531,7 +552,7 @@ static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene,
        MEM_freeN(rect);
 }
 
-static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
+static void draw_image_buffer_repeated(wmWindow *win, SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
 {
        const double time_current = PIL_check_seconds_timer();
 
@@ -548,7 +569,7 @@ static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *sce
                        if (ima && (ima->tpageflag & IMA_TILES))
                                draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
                        else
-                               draw_image_buffer(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
+                               draw_image_buffer(win, sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
 
                        /* only draw until running out of time */
                        if ((PIL_check_seconds_timer() - time_current) > 0.25)
@@ -697,8 +718,11 @@ static void draw_image_paint_helpers(ARegion *ar, Scene *scene, float zoomx, flo
 
 /* draw main image area */
 
-void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
+void draw_image_main(const bContext *C, ARegion *ar)
 {
+       SpaceImage *sima = CTX_wm_space_image(C);
+       Scene *scene = CTX_data_scene(C);
+       wmWindow *win = CTX_wm_window(C);
        Image *ima;
        ImBuf *ibuf;
        float zoomx, zoomy;
@@ -747,11 +771,11 @@ void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
        if (ibuf == NULL)
                ED_region_grid_draw(ar, zoomx, zoomy);
        else if (sima->flag & SI_DRAW_TILE)
-               draw_image_buffer_repeated(sima, ar, scene, ima, ibuf, zoomx, zoomy);
+               draw_image_buffer_repeated(win, sima, ar, scene, ima, ibuf, zoomx, zoomy);
        else if (ima && (ima->tpageflag & IMA_TILES))
                draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
        else
-               draw_image_buffer(sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
+               draw_image_buffer(win, sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
 
        /* paint helpers */
        if (sima->mode == SI_MODE_PAINT)