4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation, 2002-2009
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/space_image/image_draw.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_camera_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_space_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_brush_types.h"
48 #include "BLI_math_color.h"
49 #include "BLI_threads.h"
50 #include "BLI_string.h"
51 #include "BLI_utildefines.h"
53 #include "IMB_imbuf.h"
54 #include "IMB_imbuf_types.h"
56 #include "BKE_context.h"
57 #include "BKE_global.h"
58 #include "BKE_image.h"
59 #include "BKE_paint.h"
62 #include "BKE_colortools.h"
66 #include "BIF_glutil.h"
70 #include "ED_gpencil.h"
73 #include "UI_interface.h"
74 #include "UI_resources.h"
75 #include "UI_view2d.h"
78 #include "RE_pipeline.h"
80 #include "image_intern.h"
82 #define HEADER_HEIGHT 18
84 static void image_verify_buffer_float(Image *ima, ImBuf *ibuf, int color_manage)
86 /* detect if we need to redo the curve map.
87 ibuf->rect is zero for compositor and render results after change
88 convert to 32 bits always... drawing float rects isnt supported well (atis)
90 NOTE: if float buffer changes, we have to manually remove the rect
93 if(ibuf->rect_float && (ibuf->rect==NULL || (ibuf->userflags & IB_RECT_INVALID)) ) {
95 if(ima && ima->source == IMA_SRC_VIEWER)
96 ibuf->profile = IB_PROFILE_LINEAR_RGB;
99 ibuf->profile = IB_PROFILE_NONE;
101 IMB_rect_from_float(ibuf);
105 static void draw_render_info(Scene *scene, Image *ima, ARegion *ar)
111 rr= BKE_image_acquire_renderresult(scene, ima);
116 rect.ymin= ar->winrct.ymax - ar->winrct.ymin - HEADER_HEIGHT;
117 rect.xmax= ar->winrct.xmax - ar->winrct.xmin;
118 rect.ymax= ar->winrct.ymax - ar->winrct.ymin;
120 /* clear header rect */
121 UI_GetThemeColor3fv(TH_BACK, colf);
123 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
124 glColor4f(colf[0]+0.1f, colf[1]+0.1f, colf[2]+0.1f, 0.5f);
125 glRecti(rect.xmin, rect.ymin, rect.xmax, rect.ymax+1);
128 UI_ThemeColor(TH_TEXT_HI);
130 UI_DrawString(12, rect.ymin + 5, rr->text);
133 BKE_image_release_renderresult(scene, ima);
136 void draw_image_info(ARegion *ar, int channels, int x, int y, char *cp, float *fp, int *zp, float *zpf)
141 /* XXX colored text not allowed in Blender UI */
143 unsigned char red[3] = {255, 50, 50};
144 unsigned char green[3] = {0, 255, 0};
145 unsigned char blue[3] = {100, 100, 255};
147 unsigned char red[3] = {255, 255, 255};
148 unsigned char green[3] = {255, 255, 255};
149 unsigned char blue[3] = {255, 255, 255};
151 float hue=0, sat=0, val=0, lum=0, u=0, v=0;
153 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
156 /* noisy, high contrast make impossible to read if lower alpha is used. */
157 glColor4ub(0, 0, 0, 190);
158 glRecti(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 20);
161 BLF_size(blf_mono_font, 11, 72);
163 glColor3ub(255, 255, 255);
164 sprintf(str, "X:%-4d Y:%-4d |", x, y);
165 // UI_DrawString(6, 6, str); // works ok but fixed width is nicer.
166 BLF_position(blf_mono_font, dx, 6, 0);
167 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
168 dx += BLF_width(blf_mono_font, str);
171 glColor3ub(255, 255, 255);
172 sprintf(str, " Z:%-.4f |", 0.5f+0.5f*(((float)*zp)/(float)0x7fffffff));
173 BLF_position(blf_mono_font, dx, 6, 0);
174 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
175 dx += BLF_width(blf_mono_font, str);
178 glColor3ub(255, 255, 255);
179 sprintf(str, " Z:%-.3f |", *zpf);
180 BLF_position(blf_mono_font, dx, 6, 0);
181 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
182 dx += BLF_width(blf_mono_font, str);
188 sprintf(str, " R:%-.4f", fp[0]);
190 sprintf(str, " R:%-3d", cp[0]);
192 sprintf(str, " R:-");
193 BLF_position(blf_mono_font, dx, 6, 0);
194 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
195 dx += BLF_width(blf_mono_font, str);
199 sprintf(str, " G:%-.4f", fp[1]);
201 sprintf(str, " G:%-3d", cp[1]);
203 sprintf(str, " G:-");
204 BLF_position(blf_mono_font, dx, 6, 0);
205 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
206 dx += BLF_width(blf_mono_font, str);
210 sprintf(str, " B:%-.4f", fp[2]);
212 sprintf(str, " B:%-3d", cp[2]);
214 sprintf(str, " B:-");
215 BLF_position(blf_mono_font, dx, 6, 0);
216 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
217 dx += BLF_width(blf_mono_font, str);
220 glColor3ub(255, 255, 255);
222 sprintf(str, " A:%-.4f", fp[3]);
224 sprintf(str, " A:%-3d", cp[3]);
227 BLF_position(blf_mono_font, dx, 6, 0);
228 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
229 dx += BLF_width(blf_mono_font, str);
236 glColor3f(fp[0], fp[0], fp[0]);
238 glColor3ub(cp[0], cp[0], cp[0]);
242 else if (channels==3) {
246 glColor3ub(cp[0], cp[1], cp[2]);
250 else if (channels==4) {
254 glColor4ub(cp[0], cp[1], cp[2], cp[3]);
262 glVertex2f(dx+30, 17);
263 glVertex2f(dx+30, 3);
267 glColor3ub(255, 255, 255);
270 rgb_to_hsv(fp[0], fp[0], fp[0], &hue, &sat, &val);
271 rgb_to_yuv(fp[0], fp[0], fp[0], &lum, &u, &v);
274 rgb_to_hsv((float)cp[0]/255.0f, (float)cp[0]/255.0f, (float)cp[0]/255.0f, &hue, &sat, &val);
275 rgb_to_yuv((float)cp[0]/255.0f, (float)cp[0]/255.0f, (float)cp[0]/255.0f, &lum, &u, &v);
278 sprintf(str, "V:%-.4f", val);
279 BLF_position(blf_mono_font, dx, 6, 0);
280 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
281 dx += BLF_width(blf_mono_font, str);
283 sprintf(str, " L:%-.4f", lum);
284 BLF_position(blf_mono_font, dx, 6, 0);
285 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
286 dx += BLF_width(blf_mono_font, str);
288 else if(channels >= 3) {
290 rgb_to_hsv(fp[0], fp[1], fp[2], &hue, &sat, &val);
291 rgb_to_yuv(fp[0], fp[1], fp[2], &lum, &u, &v);
294 rgb_to_hsv((float)cp[0]/255.0f, (float)cp[1]/255.0f, (float)cp[2]/255.0f, &hue, &sat, &val);
295 rgb_to_yuv((float)cp[0]/255.0f, (float)cp[1]/255.0f, (float)cp[2]/255.0f, &lum, &u, &v);
298 sprintf(str, "H:%-.4f", hue);
299 BLF_position(blf_mono_font, dx, 6, 0);
300 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
301 dx += BLF_width(blf_mono_font, str);
303 sprintf(str, " S:%-.4f", sat);
304 BLF_position(blf_mono_font, dx, 6, 0);
305 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
306 dx += BLF_width(blf_mono_font, str);
308 sprintf(str, " V:%-.4f", val);
309 BLF_position(blf_mono_font, dx, 6, 0);
310 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
311 dx += BLF_width(blf_mono_font, str);
313 sprintf(str, " L:%-.4f", lum);
314 BLF_position(blf_mono_font, dx, 6, 0);
315 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
316 dx += BLF_width(blf_mono_font, str);
322 static void draw_image_grid(ARegion *ar, float zoomx, float zoomy)
324 float gridsize, gridstep= 1.0f/32.0f;
328 /* the image is located inside (0,0),(1, 1) as set by view2d */
329 UI_ThemeColorShade(TH_BACK, 20);
331 UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x1, &y1);
332 UI_view2d_to_region_no_clip(&ar->v2d, 1.0f, 1.0f, &x2, &y2);
333 glRectf(x1, y1, x2, y2);
335 /* gridsize adapted to zoom level */
336 gridsize= 0.5f*(zoomx+zoomy);
337 if(gridsize<=0.0f) return;
340 while(gridsize<1.0f) {
346 while(gridsize>=4.0f) {
352 /* the fine resolution level */
353 blendfac= 0.25f*gridsize - floorf(0.25f*gridsize);
354 CLAMP(blendfac, 0.0f, 1.0f);
355 UI_ThemeColorShade(TH_BACK, (int)(20.0f*(1.0f-blendfac)));
360 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
361 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
362 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
363 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
367 /* the large resolution level */
368 UI_ThemeColor(TH_BACK);
372 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
373 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
374 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
375 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
381 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])
383 GLubyte checker_stipple[32*32/8] =
385 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
386 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
387 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
388 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
389 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
390 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
391 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
392 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
396 glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
399 glEnable(GL_POLYGON_STIPPLE);
400 glPolygonStipple(checker_stipple);
401 glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
402 glDisable(GL_POLYGON_STIPPLE);
405 static void sima_draw_alpha_pixels(float x1, float y1, int rectx, int recty, unsigned int *recti)
408 /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
409 if(ENDIAN_ORDER == B_ENDIAN)
410 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
412 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_UNSIGNED_INT, recti);
413 glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
416 static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, float *rectf)
418 float *trectf= MEM_mallocN(rectx*recty*4, "temp");
421 for(a= rectx*recty -1, b= 4*a+3; a>=0; a--, b-=4)
424 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, trectf);
426 /* ogl trick below is slower... (on ATI 9600) */
427 // glColorMask(1, 0, 0, 0);
428 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+3);
429 // glColorMask(0, 1, 0, 0);
430 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+2);
431 // glColorMask(0, 0, 1, 0);
432 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+1);
433 // glColorMask(1, 1, 1, 1);
437 static int sima_draw_colorcorrected_pixels(float x1, float y1, ImBuf *ibuf)
439 colorcorrection_do_ibuf(ibuf, "MONOSCNR.ICM"); /* path is hardcoded here, find some place better */
442 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->crect);
450 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
452 /* zbuffer values are signed, so we need to shift color range */
453 glPixelTransferf(GL_RED_SCALE, 0.5f);
454 glPixelTransferf(GL_GREEN_SCALE, 0.5f);
455 glPixelTransferf(GL_BLUE_SCALE, 0.5f);
456 glPixelTransferf(GL_RED_BIAS, 0.5f);
457 glPixelTransferf(GL_GREEN_BIAS, 0.5f);
458 glPixelTransferf(GL_BLUE_BIAS, 0.5f);
460 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
462 glPixelTransferf(GL_RED_SCALE, 1.0f);
463 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
464 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
465 glPixelTransferf(GL_RED_BIAS, 0.0f);
466 glPixelTransferf(GL_GREEN_BIAS, 0.0f);
467 glPixelTransferf(GL_BLUE_BIAS, 0.0f);
470 static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rectx, int recty, float *rect_float)
472 float bias, scale, *rectf, clipend;
475 if(scene->camera && scene->camera->type==OB_CAMERA) {
476 bias= ((Camera *)scene->camera->data)->clipsta;
477 clipend= ((Camera *)scene->camera->data)->clipend;
478 scale= 1.0f/(clipend-bias);
486 rectf= MEM_mallocN(rectx*recty*4, "temp");
487 for(a= rectx*recty -1; a>=0; a--) {
488 if(rect_float[a]>clipend)
490 else if(rect_float[a]<bias)
493 rectf[a]= 1.0f - (rect_float[a]-bias)*scale;
497 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
502 static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
505 int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
508 glPixelZoom(zoomx, zoomy);
510 /* find window pixel coordinates of origin */
511 UI_view2d_to_region_no_clip(&ar->v2d, fx, fy, &x, &y);
513 /* this part is generic image display */
514 if(sima->flag & SI_SHOW_ALPHA) {
516 sima_draw_alpha_pixels(x, y, ibuf->x, ibuf->y, ibuf->rect);
517 else if(ibuf->rect_float && ibuf->channels==4)
518 sima_draw_alpha_pixelsf(x, y, ibuf->x, ibuf->y, ibuf->rect_float);
520 else if(sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels==1))) {
522 sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf);
523 else if(ibuf->zbuf_float)
524 sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float);
525 else if(ibuf->channels==1)
526 sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
529 else if(sima->flag & SI_COLOR_CORRECTION) {
530 image_verify_buffer_float(ima, ibuf, color_manage);
532 if(sima_draw_colorcorrected_pixels(x, y, ibuf)==0) {
533 unsigned char col1[3]= {100, 0, 100}, col2[3]= {160, 0, 160}; /* pink says 'warning' in blender land */
534 sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
540 if(sima->flag & SI_USE_ALPHA) {
541 unsigned char col1[3]= {100, 100, 100}, col2[3]= {160, 160, 160};
542 sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
545 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
548 /* we don't draw floats buffers directly but
549 * convert them, and optionally apply curves */
550 image_verify_buffer_float(ima, ibuf, color_manage);
553 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
555 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);*/
557 if(sima->flag & SI_USE_ALPHA)
562 glPixelZoom(1.0f, 1.0f);
565 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
567 unsigned int *rt, *rp, *rectmain;
568 short y, heigth, len;
570 /* the right offset in rectot */
572 rt= ibuf->rect+ (starty*ibuf->x+ startx);
575 heigth= (endy-starty);
577 rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
579 for(y=0; y<heigth; y++) {
580 memcpy(rp, rt, len*4);
587 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
590 int dx, dy, sx, sy, x, y;
591 int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
593 /* verify valid values, just leave this a while */
594 if(ima->xrep<1) return;
595 if(ima->yrep<1) return;
597 glPixelZoom(zoomx, zoomy);
599 if(sima->curtile >= ima->xrep*ima->yrep)
600 sima->curtile = ima->xrep*ima->yrep - 1;
602 /* create char buffer from float if needed */
603 image_verify_buffer_float(ima, ibuf, color_manage);
605 /* retrieve part of image buffer */
606 dx= ibuf->x/ima->xrep;
607 dy= ibuf->y/ima->yrep;
608 sx= (sima->curtile % ima->xrep)*dx;
609 sy= (sima->curtile / ima->xrep)*dy;
610 rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
613 for(sy=0; sy+dy<=ibuf->y; sy+= dy) {
614 for(sx=0; sx+dx<=ibuf->x; sx+= dx) {
615 UI_view2d_to_region_no_clip(&ar->v2d, fx + (float)sx/(float)ibuf->x, fy + (float)sy/(float)ibuf->y, &x, &y);
617 glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
621 glPixelZoom(1.0f, 1.0f);
626 static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
628 const double time_current= PIL_check_seconds_timer();
630 const int xmax= ceil(ar->v2d.cur.xmax);
631 const int ymax= ceil(ar->v2d.cur.ymax);
632 const int xmin= floor(ar->v2d.cur.xmin);
633 const int ymin= floor(ar->v2d.cur.ymin);
637 for(x=xmin; x<xmax; x++) {
639 for(y=ymin; y<ymax; y++) {
640 if(ima && (ima->tpageflag & IMA_TILES))
641 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
643 draw_image_buffer(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
645 /* only draw until running out of time */
646 if((PIL_check_seconds_timer() - time_current) > 0.25)
654 /* draw grease pencil */
655 void draw_image_grease_pencil(bContext *C, short onlyv2d)
657 /* draw in View2D space? */
659 /* assume that UI_view2d_ortho(C) has been called... */
660 SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
662 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
664 /* draw grease-pencil ('image' strokes) */
665 //if (sima->flag & SI_DISPGP)
666 draw_gpencil_2dimage(C, ibuf);
668 ED_space_image_release_buffer(sima, lock);
671 /* assume that UI_view2d_restore(C) has been called... */
672 //SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
674 /* draw grease-pencil ('screen' strokes) */
675 //if (sima->flag & SI_DISPGP)
676 draw_gpencil_view2d(C, 0);
680 /* XXX becomes WM paint cursor */
682 static void draw_image_view_tool(Scene *scene)
684 ToolSettings *settings= scene->toolsettings;
685 Brush *brush= settings->imapaint.brush;
691 if(settings->imapaint.flag & IMAGEPAINT_DRAWING) {
692 if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
695 else if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
699 getmouseco_areawin(mval);
701 radius= brush_size(brush)*G.sima->zoom;
702 fdrawXORcirc(mval[0], mval[1], radius);
704 if (brush->innerradius != 1.0) {
705 radius *= brush->innerradius;
706 fdrawXORcirc(mval[0], mval[1], radius);
713 static unsigned char *get_alpha_clone_image(Scene *scene, int *width, int *height)
715 Brush *brush = paint_brush(&scene->toolsettings->imapaint.paint);
717 unsigned int size, alpha;
718 unsigned char *rect, *cp;
720 if(!brush || !brush->clone.image)
723 ibuf= BKE_image_get_ibuf(brush->clone.image, NULL);
725 if(!ibuf || !ibuf->rect)
728 rect= MEM_dupallocN(ibuf->rect);
735 size= (*width)*(*height);
736 alpha= (unsigned char)255*brush->clone.alpha;
747 static void draw_image_paint_helpers(ARegion *ar, Scene *scene, float zoomx, float zoomy)
751 unsigned char *clonerect;
753 brush= paint_brush(&scene->toolsettings->imapaint.paint);
755 if(brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
756 /* this is not very efficient, but glDrawPixels doesn't allow
757 drawing with alpha */
758 clonerect= get_alpha_clone_image(scene, &w, &h);
761 UI_view2d_to_region_no_clip(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
763 glPixelZoom(zoomx, zoomy);
766 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
767 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
770 glPixelZoom(1.0, 1.0);
772 MEM_freeN(clonerect);
777 /* draw main image area */
779 void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
784 int show_viewer, show_render;
787 /* XXX can we do this in refresh? */
792 ED_image_aspect(sima->image, &xuser_asp, &yuser_asp);
794 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
795 if(sima->image->type==IMA_TYPE_COMPOSITE) {
796 ImageUser *iuser= ntree_get_active_iuser(scene->nodetree);
798 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
802 /* and we check for spare */
803 ibuf= ED_space_image_buffer(sima);
807 /* retrieve the image and information about it */
808 ima= ED_space_image(sima);
809 ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
810 ibuf= ED_space_image_acquire_buffer(sima, &lock);
812 show_viewer= (ima && ima->source == IMA_SRC_VIEWER);
813 show_render= (show_viewer && ima->type == IMA_TYPE_R_RESULT);
815 /* draw the image or grid */
817 draw_image_grid(ar, zoomx, zoomy);
818 else if(sima->flag & SI_DRAW_TILE)
819 draw_image_buffer_repeated(sima, ar, scene, ima, ibuf, zoomx, zoomy);
820 else if(ima && (ima->tpageflag & IMA_TILES))
821 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
823 draw_image_buffer(sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
826 if(sima->flag & SI_DRAWTOOL)
827 draw_image_paint_helpers(ar, scene, zoomx, zoomy);
830 /* XXX integrate this code */
833 float xoffs=0.0f, yoffs= 0.0f;
835 if(image_preview_active(sa, &xim, &yim)) {
836 xoffs= scene->r.disprect.xmin;
837 yoffs= scene->r.disprect.ymin;
839 calc_image_view(sima, 'f');
840 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
841 glRectf(0.0f, 0.0f, 1.0f, 1.0f);
847 ED_space_image_release_buffer(sima, lock);
850 if(ima && show_render)
851 draw_render_info(scene, ima, ar);