colorband and image header were ignoring DPI size
[blender.git] / source / blender / editors / space_image / image_draw.c
index ff25a2635d242e9d334bd018554835ba378beff4..112359726eac45a1445ea5360d89cb3b922f06f9 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/editors/space_image/image_draw.c
+ *  \ingroup spimage
+ */
+
+
 #include <math.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "MEM_guardedalloc.h"
 
-#include "DNA_brush_types.h"
 #include "DNA_camera_types.h"
-#include "DNA_image_types.h"
 #include "DNA_object_types.h"
 #include "DNA_space_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
+#include "DNA_brush_types.h"
 
 #include "PIL_time.h"
 
+#include "BLI_math.h"
+#include "BLI_threads.h"
+#include "BLI_string.h"
+#include "BLI_utildefines.h"
+
 #include "IMB_imbuf.h"
 #include "IMB_imbuf_types.h"
 
-#include "BKE_colortools.h"
+#include "BKE_context.h"
 #include "BKE_global.h"
 #include "BKE_image.h"
-#include "BKE_utildefines.h"
+#include "BKE_paint.h"
 
 #include "BIF_gl.h"
 #include "BIF_glutil.h"
 
+#include "BLF_api.h"
+
+#include "ED_gpencil.h"
 #include "ED_image.h"
-#include "ED_screen.h"
 
 #include "UI_interface.h"
 #include "UI_resources.h"
 #include "UI_view2d.h"
 
-#include "WM_api.h"
+
+#include "RE_pipeline.h"
 
 #include "image_intern.h"
 
 #define HEADER_HEIGHT 18
 
-#if 0
-static int image_preview_active(SpaceImage *sima, Scene *scene, float *xim, float *yim)
-{
-       /* only when compositor shows, and image handler set */
-       if(sima->image && sima->image->type==IMA_TYPE_COMPOSITE) {
-               /* XXX panels .. */
-#if 0
-               short a;
-       
-               for(a=0; a<SPACE_MAXHANDLER; a+=2) {
-                       if(sima->blockhandler[a] == IMAGE_HANDLER_PREVIEW) {
-                               if(xim) *xim= (scene->r.size*scene->r.xsch)/100;
-                               if(yim) *yim= (scene->r.size*scene->r.ysch)/100;
-                               return 1;
-                       }
-               }
-#endif
-       }
-       return 0;
-}
-#endif
-
-/* are there curves? curves visible? and curves do something? */
-static int image_curves_active(SpaceImage *sima)
-{
-       if(sima->cumap) {
-               if(curvemapping_RGBA_does_something(sima->cumap)) {
-                       /* XXX panels .. */
-#if 0
-                       short a;
-                       for(a=0; a<SPACE_MAXHANDLER; a+=2) {
-                               if(sima->blockhandler[a] == IMAGE_HANDLER_CURVES)
-                                       return 1;
-                       }
-#endif
-               }
-       }
-
-       return 0;
-}
-
-static void image_verify_buffer_float(SpaceImage *sima, Image *ima, ImBuf *ibuf, int color_manage)
+static void image_verify_buffer_float(Image *ima, ImBuf *ibuf, int color_manage)
 {
        /* detect if we need to redo the curve map.
           ibuf->rect is zero for compositor and render results after change 
@@ -115,92 +86,250 @@ static void image_verify_buffer_float(SpaceImage *sima, Image *ima, ImBuf *ibuf,
           NOTE: if float buffer changes, we have to manually remove the rect
        */
 
-       if(ibuf->rect_float) {
-               if(ibuf->rect==NULL) {
-                       if(image_curves_active(sima)) {
-                               curvemapping_do_ibuf(sima->cumap, ibuf);
-                       }
-                       else {
-                               if (color_manage) {
-                                               if (ima && ima->source == IMA_SRC_VIEWER)
-                                                       ibuf->profile = IB_PROFILE_SRGB;
-                               } else {
-                                       ibuf->profile = IB_PROFILE_NONE;
-                               }
-                               IMB_rect_from_float(ibuf);
-                       }
+       if(ibuf->rect_float && (ibuf->rect==NULL || (ibuf->userflags & IB_RECT_INVALID)) ) {
+               if(color_manage) {
+                       if(ima && ima->source == IMA_SRC_VIEWER)
+                               ibuf->profile = IB_PROFILE_LINEAR_RGB;
                }
+               else
+                       ibuf->profile = IB_PROFILE_NONE;
+
+               IMB_rect_from_float(ibuf);
        }
 }
 
-static void draw_render_info(Image *ima, ARegion *ar)
+static void draw_render_info(Scene *scene, Image *ima, ARegion *ar)
 {
+       RenderResult *rr;
        rcti rect;
        float colf[3];
-       int showspare= 0; // XXX BIF_show_render_spare();
        
-       if(ima->render_text==NULL)
-               return;
-       
-       rect= ar->winrct;
-       rect.xmin= 0;
-       rect.ymin= ar->winrct.ymax - ar->winrct.ymin - HEADER_HEIGHT;
-       rect.xmax= ar->winrct.xmax - ar->winrct.xmin;
-       rect.ymax= ar->winrct.ymax - ar->winrct.ymin;
-       
-       /* clear header rect */
-       UI_GetThemeColor3fv(TH_BACK, colf);
-       glEnable(GL_BLEND);
-       glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
-       glColor4f(colf[0]+0.1f, colf[1]+0.1f, colf[2]+0.1f, 0.5f);
-       glRecti(rect.xmin, rect.ymin, rect.xmax, rect.ymax+1);
-       glDisable(GL_BLEND);
-       
-       UI_ThemeColor(TH_TEXT_HI);
+       rr= BKE_image_acquire_renderresult(scene, ima);
+
+       if(rr && rr->text) {
+               rect= ar->winrct;
+               rect.xmin= 0;
+               rect.ymin= ar->winrct.ymax - ar->winrct.ymin - HEADER_HEIGHT;
+               rect.xmax= ar->winrct.xmax - ar->winrct.xmin;
+               rect.ymax= ar->winrct.ymax - ar->winrct.ymin;
+               
+               /* clear header rect */
+               UI_GetThemeColor3fv(TH_BACK, colf);
+               glEnable(GL_BLEND);
+               glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
+               glColor4f(colf[0]+0.1f, colf[1]+0.1f, colf[2]+0.1f, 0.5f);
+               glRecti(rect.xmin, rect.ymin, rect.xmax, rect.ymax+1);
+               glDisable(GL_BLEND);
+               
+               UI_ThemeColor(TH_TEXT_HI);
 
-       if(showspare) {
-               UI_DrawString(12, rect.ymin + 5, "(Previous)");
-               UI_DrawString(72, rect.ymin + 5, ima->render_text);
+               UI_DrawString(12, rect.ymin + 5, rr->text);
        }
-       else
-               UI_DrawString(12, rect.ymin + 5, ima->render_text);
+
+       BKE_image_release_renderresult(scene, ima);
 }
 
-void draw_image_info(ARegion *ar, int channels, int x, int y, char *cp, float *fp, int *zp, float *zpf)
+void draw_image_info(ARegion *ar, int color_manage, int channels, int x, int y, char *cp, float *fp, int *zp, float *zpf)
 {
        char str[256];
-       int ofs;
+       float dx= 6;
+       /* text colors */
+       /* XXX colored text not allowed in Blender UI */
+       #if 0
+       unsigned char red[3] = {255, 50, 50};
+       unsigned char green[3] = {0, 255, 0};
+       unsigned char blue[3] = {100, 100, 255};
+       #else
+       unsigned char red[3] = {255, 255, 255};
+       unsigned char green[3] = {255, 255, 255};
+       unsigned char blue[3] = {255, 255, 255};
+       #endif
+       float hue=0, sat=0, val=0, lum=0, u=0, v=0;
+       float col[4], finalcol[4];
 
-       ED_region_pixelspace(ar);
-       
-       ofs= sprintf(str, "X: %d Y: %d ", x, y);
-       if(cp)
-               ofs+= sprintf(str+ofs, "| R: %d G: %d B: %d A: %d ", cp[0], cp[1], cp[2], cp[3]);
-
-       if(fp) {
-               if(channels==4)
-                       ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f A: %.3f ", fp[0], fp[1], fp[2], fp[3]);
-               else if(channels==1)
-                       ofs+= sprintf(str+ofs, "| Val: %.3f ", fp[0]);
-               else if(channels==3)
-                       ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f ", fp[0], fp[1], fp[2]);
-       }
-
-       if(zp)
-               ofs+= sprintf(str+ofs, "| Z: %.4f ", 0.5+0.5*(((float)*zp)/(float)0x7fffffff));
-       if(zpf)
-               ofs+= sprintf(str+ofs, "| Z: %.3f ", *zpf);
-       
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
        glEnable(GL_BLEND);
-       
-       glColor4f(.0,.0,.0,.25);
-       glRectf(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 30.0);
+
+       /* noisy, high contrast make impossible to read if lower alpha is used. */
+       glColor4ub(0, 0, 0, 190);
+       glRecti(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 20);
        glDisable(GL_BLEND);
-       
+
+       BLF_size(blf_mono_font, 11, 72);
+
        glColor3ub(255, 255, 255);
+       sprintf(str, "X:%-4d  Y:%-4d |", x, y);
+       // UI_DrawString(6, 6, str); // works ok but fixed width is nicer.
+       BLF_position(blf_mono_font, dx, 6, 0);
+       BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+       dx += BLF_width(blf_mono_font, str);
+
+       if(zp) {
+               glColor3ub(255, 255, 255);
+               sprintf(str, " Z:%-.4f |", 0.5f+0.5f*(((float)*zp)/(float)0x7fffffff));
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+       }
+       if(zpf) {
+               glColor3ub(255, 255, 255);
+               sprintf(str, " Z:%-.3f |", *zpf);
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+       }
+
+       if(channels >= 3) {
+               glColor3ubv(red);
+               if (fp)
+                       sprintf(str, "  R:%-.4f", fp[0]);
+               else if (cp)
+                       sprintf(str, "  R:%-3d", cp[0]);
+               else
+                       sprintf(str, "  R:-");
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+               
+               glColor3ubv(green);
+               if (fp)
+                       sprintf(str, "  G:%-.4f", fp[1]);
+               else if (cp)
+                       sprintf(str, "  G:%-3d", cp[1]);
+               else
+                       sprintf(str, "  G:-");
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+               
+               glColor3ubv(blue);
+               if (fp)
+                       sprintf(str, "  B:%-.4f", fp[2]);
+               else if (cp)
+                       sprintf(str, "  B:%-3d", cp[2]);
+               else
+                       sprintf(str, "  B:-");
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+               
+               if(channels == 4) {
+                       glColor3ub(255, 255, 255);
+                       if (fp)
+                               sprintf(str, "  A:%-.4f", fp[3]);
+                       else if (cp)
+                               sprintf(str, "  A:%-3d", cp[3]);
+                       else
+                               sprintf(str, "- ");
+                       BLF_position(blf_mono_font, dx, 6, 0);
+                       BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+                       dx += BLF_width(blf_mono_font, str);
+               }
+       }
        
-       UI_DrawString(10, 10, str);
+       /* color rectangle */
+       if (channels==1) {
+               if (fp)
+                       col[0] = col[1] = col[2] = fp[0];
+               else if (cp)
+                       col[0] = col[1] = col[2] = (float)cp[0]/255.0f;
+               else
+                       col[0] = col[1] = col[2] = 0.0f;
+       }
+       else if (channels==3) {
+               if (fp)
+                       copy_v3_v3(col, fp);
+               else if (cp) {
+                       col[0] = (float)cp[0]/255.0f;
+                       col[1] = (float)cp[1]/255.0f;
+                       col[2] = (float)cp[2]/255.0f;
+               }
+               else
+                       zero_v3(col);
+       }
+       else if (channels==4) {
+               if (fp)
+                       copy_v4_v4(col, fp);
+               else if (cp) {
+                       col[0] = (float)cp[0]/255.0f;
+                       col[1] = (float)cp[1]/255.0f;
+                       col[2] = (float)cp[2]/255.0f;
+                       col[3] = (float)cp[3]/255.0f;
+               }
+               else
+                       zero_v4(col);
+       }
+       if (color_manage) {
+               linearrgb_to_srgb_v3_v3(finalcol, col);
+               finalcol[3] = col[3];
+       }
+       else {
+               copy_v4_v4(finalcol, col);
+       }
+       glDisable(GL_BLEND);
+       glColor3fv(finalcol);
+       dx += 5;
+       glBegin(GL_QUADS);
+       glVertex2f(dx, 3);
+       glVertex2f(dx, 17);
+       glVertex2f(dx+30, 17);
+       glVertex2f(dx+30, 3);
+       glEnd();
+       dx += 35;
+
+       glColor3ub(255, 255, 255);
+       if(channels == 1) {
+               if (fp) {
+                       rgb_to_hsv(fp[0], fp[0], fp[0], &hue, &sat, &val);
+                       rgb_to_yuv(fp[0], fp[0], fp[0], &lum, &u, &v);
+               }
+               else if (cp) {
+                       rgb_to_hsv((float)cp[0]/255.0f, (float)cp[0]/255.0f, (float)cp[0]/255.0f, &hue, &sat, &val);
+                       rgb_to_yuv((float)cp[0]/255.0f, (float)cp[0]/255.0f, (float)cp[0]/255.0f, &lum, &u, &v);
+               }
+               
+               sprintf(str, "V:%-.4f", val);
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+
+               sprintf(str, "   L:%-.4f", lum);
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+       }
+       else if(channels >= 3) {
+               if (fp) {
+                       rgb_to_hsv(fp[0], fp[1], fp[2], &hue, &sat, &val);
+                       rgb_to_yuv(fp[0], fp[1], fp[2], &lum, &u, &v);
+               }
+               else if (cp) {
+                       rgb_to_hsv((float)cp[0]/255.0f, (float)cp[1]/255.0f, (float)cp[2]/255.0f, &hue, &sat, &val);
+                       rgb_to_yuv((float)cp[0]/255.0f, (float)cp[1]/255.0f, (float)cp[2]/255.0f, &lum, &u, &v);
+               }
+
+               sprintf(str, "H:%-.4f", hue);
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+
+               sprintf(str, "  S:%-.4f", sat);
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+
+               sprintf(str, "  V:%-.4f", val);
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+
+               sprintf(str, "   L:%-.4f", lum);
+               BLF_position(blf_mono_font, dx, 6, 0);
+               BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+               dx += BLF_width(blf_mono_font, str);
+       }
+
+       (void)dx;
 }
 
 /* image drawing */
@@ -224,21 +353,21 @@ static void draw_image_grid(ARegion *ar, float zoomx, float zoomy)
        
        if(gridsize<1.0f) {
                while(gridsize<1.0f) {
-                       gridsize*= 4.0;
-                       gridstep*= 4.0;
+                       gridsize*= 4.0f;
+                       gridstep*= 4.0f;
                }
        }
        else {
                while(gridsize>=4.0f) {
-                       gridsize/= 4.0;
-                       gridstep/= 4.0;
+                       gridsize/= 4.0f;
+                       gridstep/= 4.0f;
                }
        }
        
        /* the fine resolution level */
-       blendfac= 0.25*gridsize - floor(0.25*gridsize);
-       CLAMP(blendfac, 0.0, 1.0);
-       UI_ThemeColorShade(TH_BACK, (int)(20.0*(1.0-blendfac)));
+       blendfac= 0.25f*gridsize - floorf(0.25f*gridsize);
+       CLAMP(blendfac, 0.0f, 1.0f);
+       UI_ThemeColorShade(TH_BACK, (int)(20.0f*(1.0f-blendfac)));
        
        fac= 0.0f;
        glBegin(GL_LINES);
@@ -264,7 +393,7 @@ static void draw_image_grid(ARegion *ar, float zoomx, float zoomy)
        glEnd();
 }
 
-static void sima_draw_alpha_backdrop(float x1, float y1, float xsize, float ysize, float zoomx, float zoomy)
+static void sima_draw_alpha_backdrop(float x1, float y1, float xsize, float ysize, float zoomx, float zoomy, unsigned char col1[3], unsigned char col2[3])
 {
        GLubyte checker_stipple[32*32/8] =
        {
@@ -278,9 +407,9 @@ static void sima_draw_alpha_backdrop(float x1, float y1, float xsize, float ysiz
                0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
        };
        
-       glColor3ub(100, 100, 100);
+       glColor3ubv(col1);
        glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
-       glColor3ub(160, 160, 160);
+       glColor3ubv(col2);
 
        glEnable(GL_POLYGON_STIPPLE);
        glPolygonStipple(checker_stipple);
@@ -319,15 +448,6 @@ static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, fl
 //     glColorMask(1, 1, 1, 1);
 }
 
-#ifdef WITH_LCMS
-static void sima_draw_colorcorrected_pixels(float x1, float y1, ImBuf *ibuf)
-{
-       colorcorrection_do_ibuf(ibuf, "MONOSCNR.ICM"); /* path is hardcoded here, find some place better */
-       
-       glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->crect);
-}
-#endif
-
 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
 {
        /* zbuffer values are signed, so we need to shift color range */
@@ -406,25 +526,18 @@ static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, Image
                else if(ibuf->channels==1)
                        sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
        }
-#ifdef WITH_LCMS
-       else if(sima->flag & SI_COLOR_CORRECTION) {
-               image_verify_buffer_float(sima, ima, ibuf, color_manage);
-               
-               sima_draw_colorcorrected_pixels(x, y, ibuf);
-
-       }
-#endif
        else {
                if(sima->flag & SI_USE_ALPHA) {
-                       sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy);
+                       unsigned char col1[3]= {100, 100, 100}, col2[3]= {160, 160, 160};
+                       sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
 
                        glEnable(GL_BLEND);
-                       glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+                       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                }
 
                /* we don't draw floats buffers directly but
                 * convert them, and optionally apply curves */
-               image_verify_buffer_float(sima, ima, ibuf, color_manage);
+               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);
@@ -477,7 +590,7 @@ static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene,
                sima->curtile = ima->xrep*ima->yrep - 1; 
        
        /* create char buffer from float if needed */
-       image_verify_buffer_float(sima, ima, ibuf, color_manage);
+       image_verify_buffer_float(ima, ibuf, color_manage);
 
        /* retrieve part of image buffer */
        dx= ibuf->x/ima->xrep;
@@ -502,13 +615,18 @@ static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene,
 
 static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
 {
-       float x, y;
-       double time_current;
-       
-       time_current = PIL_check_seconds_timer();
+       const double time_current= PIL_check_seconds_timer();
 
-       for(x=floor(ar->v2d.cur.xmin); x<ar->v2d.cur.xmax; x += 1.0f) { 
-               for(y=floor(ar->v2d.cur.ymin); y<ar->v2d.cur.ymax; y += 1.0f) { 
+       const int xmax= ceil(ar->v2d.cur.xmax);
+       const int ymax= ceil(ar->v2d.cur.ymax);
+       const int xmin= floor(ar->v2d.cur.xmin);
+       const int ymin= floor(ar->v2d.cur.ymin);
+
+       int x;
+
+       for(x=xmin; x<xmax; x++) {
+               int y;
+               for(y=ymin; y<ymax; y++) { 
                        if(ima && (ima->tpageflag & IMA_TILES))
                                draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
                        else
@@ -524,22 +642,29 @@ static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *sce
 /* draw uv edit */
 
 /* draw grease pencil */
-
-static void draw_image_grease_pencil(SpaceImage *sima, ImBuf *ibuf)
+void draw_image_grease_pencil(bContext *C, short onlyv2d)
 {
-       /* XXX bring back */
-       /* draw grease-pencil ('image' strokes) */
-       if (sima->flag & SI_DISPGP)
-               ; // XXX draw_gpencil_2dimage(sa, ibuf);
+       /* draw in View2D space? */
+       if (onlyv2d) {
+               /* assume that UI_view2d_ortho(C) has been called... */
+               SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
+               void *lock;
+               ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
+               
+               /* draw grease-pencil ('image' strokes) */
+               //if (sima->flag & SI_DISPGP)
+                       draw_gpencil_2dimage(C, ibuf);
 
-#if 0
-       mywinset(sa->win);      /* restore scissor after gla call... */
-       wmOrtho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
-#endif
-       
-       /* draw grease-pencil (screen strokes) */
-       if (sima->flag & SI_DISPGP)
-               ; // XXX draw_gpencil_2dview(sa, NULL);
+               ED_space_image_release_buffer(sima, lock);
+       }
+       else {
+               /* assume that UI_view2d_restore(C) has been called... */
+               //SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
+               
+               /* draw grease-pencil ('screen' strokes) */
+               //if (sima->flag & SI_DISPGP)
+                       draw_gpencil_view2d(C, 0);
+       }
 }
 
 /* XXX becomes WM paint cursor */
@@ -548,7 +673,7 @@ static void draw_image_view_tool(Scene *scene)
 {
        ToolSettings *settings= scene->toolsettings;
        Brush *brush= settings->imapaint.brush;
-       short mval[2];
+       int mval[2];
        float radius;
        int draw= 0;
 
@@ -563,7 +688,7 @@ static void draw_image_view_tool(Scene *scene)
                if(draw) {
                        getmouseco_areawin(mval);
 
-                       radius= brush->size*G.sima->zoom/2;
+                       radius= brush_size(brush)*G.sima->zoom;
                        fdrawXORcirc(mval[0], mval[1], radius);
 
                        if (brush->innerradius != 1.0) {
@@ -577,7 +702,7 @@ static void draw_image_view_tool(Scene *scene)
 
 static unsigned char *get_alpha_clone_image(Scene *scene, int *width, int *height)
 {
-       Brush *brush = scene->toolsettings->imapaint.brush;
+       Brush *brush = paint_brush(&scene->toolsettings->imapaint.paint);
        ImBuf *ibuf;
        unsigned int size, alpha;
        unsigned char *rect, *cp;
@@ -609,15 +734,15 @@ static unsigned char *get_alpha_clone_image(Scene *scene, int *width, int *heigh
        return rect;
 }
 
-static void draw_image_paint_helpers(SpaceImage *sima, ARegion *ar, Scene *scene, float zoomx, float zoomy)
+static void draw_image_paint_helpers(ARegion *ar, Scene *scene, float zoomx, float zoomy)
 {
        Brush *brush;
        int x, y, w, h;
        unsigned char *clonerect;
 
-       brush= scene->toolsettings->imapaint.brush;
+       brush= paint_brush(&scene->toolsettings->imapaint.paint);
 
-       if(brush && (scene->toolsettings->imapaint.tool == PAINT_TOOL_CLONE)) {
+       if(brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
                /* this is not very efficient, but glDrawPixels doesn't allow
                   drawing with alpha */
                clonerect= get_alpha_clone_image(scene, &w, &h);
@@ -647,6 +772,7 @@ void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
        ImBuf *ibuf;
        float zoomx, zoomy;
        int show_viewer, show_render;
+       void *lock;
 
        /* XXX can we do this in refresh? */
 #if 0
@@ -668,12 +794,10 @@ void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
        }
 #endif
 
-       /* put scene context variable in iuser */
-       sima->iuser.scene= scene;
        /* retrieve the image and information about it */
        ima= ED_space_image(sima);
-       ibuf= ED_space_image_buffer(sima);
        ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
+       ibuf= ED_space_image_acquire_buffer(sima, &lock);
 
        show_viewer= (ima && ima->source == IMA_SRC_VIEWER);
        show_render= (show_viewer && ima->type == IMA_TYPE_R_RESULT);
@@ -688,15 +812,10 @@ void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
        else
                draw_image_buffer(sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
 
-       /* grease pencil */
-       draw_image_grease_pencil(sima, ibuf);
-
        /* paint helpers */
-       draw_image_paint_helpers(sima, ar, scene, zoomx, zoomy);
+       if(sima->flag & SI_DRAWTOOL)
+               draw_image_paint_helpers(ar, scene, zoomx, zoomy);
 
-       /* render info */
-       if(ibuf && ima && show_render)
-               draw_render_info(ima, ar);
 
        /* XXX integrate this code */
 #if 0
@@ -714,5 +833,11 @@ void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
                }
        }
 #endif
+
+       ED_space_image_release_buffer(sima, lock);
+
+       /* render info */
+       if(ima && show_render)
+               draw_render_info(scene, ima, ar);
 }