fix for own error with image info display, also dont draw if the mouse is outside...
[blender.git] / source / blender / editors / space_image / image_draw.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2009
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <math.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_camera_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_space_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_brush_types.h"
40
41 #include "PIL_time.h"
42
43 #include "BLI_threads.h"
44 #include "BLI_string.h"
45 #include "BLI_utildefines.h"
46
47 #include "IMB_imbuf.h"
48 #include "IMB_imbuf_types.h"
49
50 #include "BKE_context.h"
51 #include "BKE_global.h"
52 #include "BKE_image.h"
53 #include "BKE_paint.h"
54
55 #ifdef WITH_LCMS
56 #include "BKE_colortools.h"
57 #endif
58
59 #include "BIF_gl.h"
60 #include "BIF_glutil.h"
61
62 #include "BLF_api.h"
63
64 #include "ED_gpencil.h"
65 #include "ED_image.h"
66
67 #include "UI_interface.h"
68 #include "UI_resources.h"
69 #include "UI_view2d.h"
70
71
72 #include "RE_pipeline.h"
73
74 #include "image_intern.h"
75
76 #define HEADER_HEIGHT 18
77
78 static void image_verify_buffer_float(Image *ima, ImBuf *ibuf, int color_manage)
79 {
80         /* detect if we need to redo the curve map.
81            ibuf->rect is zero for compositor and render results after change 
82            convert to 32 bits always... drawing float rects isnt supported well (atis)
83         
84            NOTE: if float buffer changes, we have to manually remove the rect
85         */
86
87         if(ibuf->rect_float && (ibuf->rect==NULL || (ibuf->userflags & IB_RECT_INVALID)) ) {
88                 if(color_manage) {
89                         if(ima && ima->source == IMA_SRC_VIEWER)
90                                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
91                 }
92                 else
93                         ibuf->profile = IB_PROFILE_NONE;
94
95                 IMB_rect_from_float(ibuf);
96         }
97 }
98
99 static void draw_render_info(Scene *scene, Image *ima, ARegion *ar)
100 {
101         RenderResult *rr;
102         rcti rect;
103         float colf[3];
104         
105         rr= BKE_image_acquire_renderresult(scene, ima);
106
107         if(rr && rr->text) {
108                 rect= ar->winrct;
109                 rect.xmin= 0;
110                 rect.ymin= ar->winrct.ymax - ar->winrct.ymin - HEADER_HEIGHT;
111                 rect.xmax= ar->winrct.xmax - ar->winrct.xmin;
112                 rect.ymax= ar->winrct.ymax - ar->winrct.ymin;
113                 
114                 /* clear header rect */
115                 UI_GetThemeColor3fv(TH_BACK, colf);
116                 glEnable(GL_BLEND);
117                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
118                 glColor4f(colf[0]+0.1f, colf[1]+0.1f, colf[2]+0.1f, 0.5f);
119                 glRecti(rect.xmin, rect.ymin, rect.xmax, rect.ymax+1);
120                 glDisable(GL_BLEND);
121                 
122                 UI_ThemeColor(TH_TEXT_HI);
123
124                 UI_DrawString(12, rect.ymin + 5, rr->text);
125         }
126
127         BKE_image_release_renderresult(scene, ima);
128 }
129
130 void draw_image_info(ARegion *ar, int channels, int x, int y, char *cp, float *fp, int *zp, float *zpf)
131 {
132         char str[256];
133         int ofs= 0;
134
135         ofs += BLI_snprintf(str + ofs, sizeof(str)-ofs, "X: %4d Y: %4d ", x, y);
136         if(cp)
137                 ofs+= BLI_snprintf(str + ofs, sizeof(str)-ofs, "| R: %3d G: %3d B: %3d A: %3d ", cp[0], cp[1], cp[2], cp[3]);
138
139         if(fp) {
140                 if(channels==4)
141                         ofs+= BLI_snprintf(str + ofs, sizeof(str)-ofs, "| R: %.3f G: %.3f B: %.3f A: %.3f ", fp[0], fp[1], fp[2], fp[3]);
142                 else if(channels==1)
143                         ofs+= BLI_snprintf(str + ofs, sizeof(str)-ofs, "| Val: %.3f ", fp[0]);
144                 else if(channels==3)
145                         ofs+= BLI_snprintf(str + ofs, sizeof(str)-ofs, "| R: %.3f G: %.3f B: %.3f ", fp[0], fp[1], fp[2]);
146         }
147
148         if(zp)
149                 ofs+= BLI_snprintf(str + ofs, sizeof(str)-ofs, "| Z: %.4f ", 0.5+0.5*(((float)*zp)/(float)0x7fffffff));
150         if(zpf)
151                 ofs+= BLI_snprintf(str + ofs, sizeof(str)-ofs, "| Z: %.3f ", *zpf);
152         (void)ofs;
153
154         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
155         glEnable(GL_BLEND);
156         
157         glColor4f(.0,.0,.0,.25);
158         glRecti(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 20);
159         glDisable(GL_BLEND);
160         
161         glColor3ub(255, 255, 255);
162         
163         // UI_DrawString(6, 6, str); // works ok but fixed width is nicer.
164         BLF_size(blf_mono_font, 11, 72);
165         BLF_position(blf_mono_font, 6, 6, 0);
166         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
167         
168 }
169
170 /* image drawing */
171
172 static void draw_image_grid(ARegion *ar, float zoomx, float zoomy)
173 {
174         float gridsize, gridstep= 1.0f/32.0f;
175         float fac, blendfac;
176         int x1, y1, x2, y2;
177         
178         /* the image is located inside (0,0),(1, 1) as set by view2d */
179         UI_ThemeColorShade(TH_BACK, 20);
180
181         UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x1, &y1);
182         UI_view2d_to_region_no_clip(&ar->v2d, 1.0f, 1.0f, &x2, &y2);
183         glRectf(x1, y1, x2, y2);
184
185         /* gridsize adapted to zoom level */
186         gridsize= 0.5f*(zoomx+zoomy);
187         if(gridsize<=0.0f) return;
188         
189         if(gridsize<1.0f) {
190                 while(gridsize<1.0f) {
191                         gridsize*= 4.0;
192                         gridstep*= 4.0;
193                 }
194         }
195         else {
196                 while(gridsize>=4.0f) {
197                         gridsize/= 4.0;
198                         gridstep/= 4.0;
199                 }
200         }
201         
202         /* the fine resolution level */
203         blendfac= 0.25*gridsize - floor(0.25*gridsize);
204         CLAMP(blendfac, 0.0, 1.0);
205         UI_ThemeColorShade(TH_BACK, (int)(20.0*(1.0-blendfac)));
206         
207         fac= 0.0f;
208         glBegin(GL_LINES);
209         while(fac<1.0f) {
210                 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
211                 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
212                 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
213                 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
214                 fac+= gridstep;
215         }
216         
217         /* the large resolution level */
218         UI_ThemeColor(TH_BACK);
219         
220         fac= 0.0f;
221         while(fac<1.0f) {
222                 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
223                 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
224                 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
225                 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
226                 fac+= 4.0f*gridstep;
227         }
228         glEnd();
229 }
230
231 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])
232 {
233         GLubyte checker_stipple[32*32/8] =
234         {
235                 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
236                 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
237                 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
238                 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
239                 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
240                 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
241                 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
242                 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
243         };
244         
245         glColor3ubv(col1);
246         glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
247         glColor3ubv(col2);
248
249         glEnable(GL_POLYGON_STIPPLE);
250         glPolygonStipple(checker_stipple);
251         glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
252         glDisable(GL_POLYGON_STIPPLE);
253 }
254
255 static void sima_draw_alpha_pixels(float x1, float y1, int rectx, int recty, unsigned int *recti)
256 {
257         
258         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
259         if(ENDIAN_ORDER == B_ENDIAN)
260                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
261
262         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_UNSIGNED_INT, recti);
263         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
264 }
265
266 static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, float *rectf)
267 {
268         float *trectf= MEM_mallocN(rectx*recty*4, "temp");
269         int a, b;
270         
271         for(a= rectx*recty -1, b= 4*a+3; a>=0; a--, b-=4)
272                 trectf[a]= rectf[b];
273         
274         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, trectf);
275         MEM_freeN(trectf);
276         /* ogl trick below is slower... (on ATI 9600) */
277 //      glColorMask(1, 0, 0, 0);
278 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+3);
279 //      glColorMask(0, 1, 0, 0);
280 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+2);
281 //      glColorMask(0, 0, 1, 0);
282 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+1);
283 //      glColorMask(1, 1, 1, 1);
284 }
285
286 #ifdef WITH_LCMS
287 static int sima_draw_colorcorrected_pixels(float x1, float y1, ImBuf *ibuf)
288 {
289         colorcorrection_do_ibuf(ibuf, "MONOSCNR.ICM"); /* path is hardcoded here, find some place better */
290
291         if(ibuf->crect) {
292                 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->crect);
293                 return 1;
294         }
295
296         return 0;
297 }
298 #endif
299
300 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
301 {
302         /* zbuffer values are signed, so we need to shift color range */
303         glPixelTransferf(GL_RED_SCALE, 0.5f);
304         glPixelTransferf(GL_GREEN_SCALE, 0.5f);
305         glPixelTransferf(GL_BLUE_SCALE, 0.5f);
306         glPixelTransferf(GL_RED_BIAS, 0.5f);
307         glPixelTransferf(GL_GREEN_BIAS, 0.5f);
308         glPixelTransferf(GL_BLUE_BIAS, 0.5f);
309         
310         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
311         
312         glPixelTransferf(GL_RED_SCALE, 1.0f);
313         glPixelTransferf(GL_GREEN_SCALE, 1.0f);
314         glPixelTransferf(GL_BLUE_SCALE, 1.0f);
315         glPixelTransferf(GL_RED_BIAS, 0.0f);
316         glPixelTransferf(GL_GREEN_BIAS, 0.0f);
317         glPixelTransferf(GL_BLUE_BIAS, 0.0f);
318 }
319
320 static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rectx, int recty, float *rect_float)
321 {
322         float bias, scale, *rectf, clipend;
323         int a;
324         
325         if(scene->camera && scene->camera->type==OB_CAMERA) {
326                 bias= ((Camera *)scene->camera->data)->clipsta;
327                 clipend= ((Camera *)scene->camera->data)->clipend;
328                 scale= 1.0f/(clipend-bias);
329         }
330         else {
331                 bias= 0.1f;
332                 scale= 0.01f;
333                 clipend= 100.0f;
334         }
335         
336         rectf= MEM_mallocN(rectx*recty*4, "temp");
337         for(a= rectx*recty -1; a>=0; a--) {
338                 if(rect_float[a]>clipend)
339                         rectf[a]= 0.0f;
340                 else if(rect_float[a]<bias)
341                         rectf[a]= 1.0f;
342                 else {
343                         rectf[a]= 1.0f - (rect_float[a]-bias)*scale;
344                         rectf[a]*= rectf[a];
345                 }
346         }
347         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
348         
349         MEM_freeN(rectf);
350 }
351
352 static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
353 {
354         int x, y;
355         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
356
357         /* set zoom */
358         glPixelZoom(zoomx, zoomy);
359
360         /* find window pixel coordinates of origin */
361         UI_view2d_to_region_no_clip(&ar->v2d, fx, fy, &x, &y);
362
363         /* this part is generic image display */
364         if(sima->flag & SI_SHOW_ALPHA) {
365                 if(ibuf->rect)
366                         sima_draw_alpha_pixels(x, y, ibuf->x, ibuf->y, ibuf->rect);
367                 else if(ibuf->rect_float && ibuf->channels==4)
368                         sima_draw_alpha_pixelsf(x, y, ibuf->x, ibuf->y, ibuf->rect_float);
369         }
370         else if(sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels==1))) {
371                 if(ibuf->zbuf)
372                         sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf);
373                 else if(ibuf->zbuf_float)
374                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float);
375                 else if(ibuf->channels==1)
376                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
377         }
378 #ifdef WITH_LCMS
379         else if(sima->flag & SI_COLOR_CORRECTION) {
380                 image_verify_buffer_float(ima, ibuf, color_manage);
381                 
382                 if(sima_draw_colorcorrected_pixels(x, y, ibuf)==0) {
383                         unsigned char col1[3]= {100, 0, 100}, col2[3]= {160, 0, 160}; /* pink says 'warning' in blender land */
384                         sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
385                 }
386
387         }
388 #endif
389         else {
390                 if(sima->flag & SI_USE_ALPHA) {
391                         unsigned char col1[3]= {100, 100, 100}, col2[3]= {160, 160, 160};
392                         sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
393
394                         glEnable(GL_BLEND);
395                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
396                 }
397
398                 /* we don't draw floats buffers directly but
399                  * convert them, and optionally apply curves */
400                 image_verify_buffer_float(ima, ibuf, color_manage);
401
402                 if(ibuf->rect)
403                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
404                 /*else
405                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);*/
406                 
407                 if(sima->flag & SI_USE_ALPHA)
408                         glDisable(GL_BLEND);
409         }
410
411         /* reset zoom */
412         glPixelZoom(1.0f, 1.0f);
413 }
414
415 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
416 {
417         unsigned int *rt, *rp, *rectmain;
418         short y, heigth, len;
419
420         /* the right offset in rectot */
421
422         rt= ibuf->rect+ (starty*ibuf->x+ startx);
423
424         len= (endx-startx);
425         heigth= (endy-starty);
426
427         rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
428         
429         for(y=0; y<heigth; y++) {
430                 memcpy(rp, rt, len*4);
431                 rt+= ibuf->x;
432                 rp+= len;
433         }
434         return rectmain;
435 }
436
437 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
438 {
439         unsigned int *rect;
440         int dx, dy, sx, sy, x, y;
441         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
442
443         /* verify valid values, just leave this a while */
444         if(ima->xrep<1) return;
445         if(ima->yrep<1) return;
446         
447         glPixelZoom(zoomx, zoomy);
448
449         if(sima->curtile >= ima->xrep*ima->yrep) 
450                 sima->curtile = ima->xrep*ima->yrep - 1; 
451         
452         /* create char buffer from float if needed */
453         image_verify_buffer_float(ima, ibuf, color_manage);
454
455         /* retrieve part of image buffer */
456         dx= ibuf->x/ima->xrep;
457         dy= ibuf->y/ima->yrep;
458         sx= (sima->curtile % ima->xrep)*dx;
459         sy= (sima->curtile / ima->xrep)*dy;
460         rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
461         
462         /* draw repeated */
463         for(sy=0; sy+dy<=ibuf->y; sy+= dy) {
464                 for(sx=0; sx+dx<=ibuf->x; sx+= dx) {
465                         UI_view2d_to_region_no_clip(&ar->v2d, fx + (float)sx/(float)ibuf->x, fy + (float)sy/(float)ibuf->y, &x, &y);
466
467                         glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
468                 }
469         }
470
471         glPixelZoom(1.0f, 1.0f);
472
473         MEM_freeN(rect);
474 }
475
476 static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
477 {
478         const double time_current= PIL_check_seconds_timer();
479
480         const int xmax= ceil(ar->v2d.cur.xmax);
481         const int ymax= ceil(ar->v2d.cur.ymax);
482         const int xmin= floor(ar->v2d.cur.xmin);
483         const int ymin= floor(ar->v2d.cur.ymin);
484
485         int x;
486
487         for(x=xmin; x<xmax; x++) {
488                 int y;
489                 for(y=ymin; y<ymax; y++) { 
490                         if(ima && (ima->tpageflag & IMA_TILES))
491                                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
492                         else
493                                 draw_image_buffer(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
494
495                         /* only draw until running out of time */
496                         if((PIL_check_seconds_timer() - time_current) > 0.25)
497                                 return;
498                 }
499         }
500 }
501
502 /* draw uv edit */
503
504 /* draw grease pencil */
505 void draw_image_grease_pencil(bContext *C, short onlyv2d)
506 {
507         /* draw in View2D space? */
508         if (onlyv2d) {
509                 /* assume that UI_view2d_ortho(C) has been called... */
510                 SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
511                 void *lock;
512                 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
513                 
514                 /* draw grease-pencil ('image' strokes) */
515                 //if (sima->flag & SI_DISPGP)
516                         draw_gpencil_2dimage(C, ibuf);
517
518                 ED_space_image_release_buffer(sima, lock);
519         }
520         else {
521                 /* assume that UI_view2d_restore(C) has been called... */
522                 //SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
523                 
524                 /* draw grease-pencil ('screen' strokes) */
525                 //if (sima->flag & SI_DISPGP)
526                         draw_gpencil_view2d(C, 0);
527         }
528 }
529
530 /* XXX becomes WM paint cursor */
531 #if 0
532 static void draw_image_view_tool(Scene *scene)
533 {
534         ToolSettings *settings= scene->toolsettings;
535         Brush *brush= settings->imapaint.brush;
536         short mval[2];
537         float radius;
538         int draw= 0;
539
540         if(brush) {
541                 if(settings->imapaint.flag & IMAGEPAINT_DRAWING) {
542                         if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
543                                 draw= 1;
544                 }
545                 else if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
546                         draw= 1;
547                 
548                 if(draw) {
549                         getmouseco_areawin(mval);
550
551                         radius= brush_size(brush)*G.sima->zoom;
552                         fdrawXORcirc(mval[0], mval[1], radius);
553
554                         if (brush->innerradius != 1.0) {
555                                 radius *= brush->innerradius;
556                                 fdrawXORcirc(mval[0], mval[1], radius);
557                         }
558                 }
559         }
560 }
561 #endif
562
563 static unsigned char *get_alpha_clone_image(Scene *scene, int *width, int *height)
564 {
565         Brush *brush = paint_brush(&scene->toolsettings->imapaint.paint);
566         ImBuf *ibuf;
567         unsigned int size, alpha;
568         unsigned char *rect, *cp;
569
570         if(!brush || !brush->clone.image)
571                 return NULL;
572         
573         ibuf= BKE_image_get_ibuf(brush->clone.image, NULL);
574
575         if(!ibuf || !ibuf->rect)
576                 return NULL;
577
578         rect= MEM_dupallocN(ibuf->rect);
579         if(!rect)
580                 return NULL;
581
582         *width= ibuf->x;
583         *height= ibuf->y;
584
585         size= (*width)*(*height);
586         alpha= (unsigned char)255*brush->clone.alpha;
587         cp= rect;
588
589         while(size-- > 0) {
590                 cp[3]= alpha;
591                 cp += 4;
592         }
593
594         return rect;
595 }
596
597 static void draw_image_paint_helpers(ARegion *ar, Scene *scene, float zoomx, float zoomy)
598 {
599         Brush *brush;
600         int x, y, w, h;
601         unsigned char *clonerect;
602
603         brush= paint_brush(&scene->toolsettings->imapaint.paint);
604
605         if(brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
606                 /* this is not very efficient, but glDrawPixels doesn't allow
607                    drawing with alpha */
608                 clonerect= get_alpha_clone_image(scene, &w, &h);
609
610                 if(clonerect) {
611                         UI_view2d_to_region_no_clip(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
612
613                         glPixelZoom(zoomx, zoomy);
614
615                         glEnable(GL_BLEND);
616                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
617                         glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
618                         glDisable(GL_BLEND);
619
620                         glPixelZoom(1.0, 1.0);
621
622                         MEM_freeN(clonerect);
623                 }
624         }
625 }
626
627 /* draw main image area */
628
629 void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
630 {
631         Image *ima;
632         ImBuf *ibuf;
633         float zoomx, zoomy;
634         int show_viewer, show_render;
635         void *lock;
636
637         /* XXX can we do this in refresh? */
638 #if 0
639         what_image(sima);
640         
641         if(sima->image) {
642                 ED_image_aspect(sima->image, &xuser_asp, &yuser_asp);
643                 
644                 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
645                 if(sima->image->type==IMA_TYPE_COMPOSITE) {
646                         ImageUser *iuser= ntree_get_active_iuser(scene->nodetree);
647                         if(iuser) {
648                                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
649                                 sima->iuser= *iuser;
650                         }
651                 }
652                 /* and we check for spare */
653                 ibuf= ED_space_image_buffer(sima);
654         }
655 #endif
656
657         /* retrieve the image and information about it */
658         ima= ED_space_image(sima);
659         ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
660         ibuf= ED_space_image_acquire_buffer(sima, &lock);
661
662         show_viewer= (ima && ima->source == IMA_SRC_VIEWER);
663         show_render= (show_viewer && ima->type == IMA_TYPE_R_RESULT);
664
665         /* draw the image or grid */
666         if(ibuf==NULL)
667                 draw_image_grid(ar, zoomx, zoomy);
668         else if(sima->flag & SI_DRAW_TILE)
669                 draw_image_buffer_repeated(sima, ar, scene, ima, ibuf, zoomx, zoomy);
670         else if(ima && (ima->tpageflag & IMA_TILES))
671                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
672         else
673                 draw_image_buffer(sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
674
675         /* paint helpers */
676         if(sima->flag & SI_DRAWTOOL)
677                 draw_image_paint_helpers(ar, scene, zoomx, zoomy);
678
679
680         /* XXX integrate this code */
681 #if 0
682         if(ibuf) {
683                 float xoffs=0.0f, yoffs= 0.0f;
684                 
685                 if(image_preview_active(sa, &xim, &yim)) {
686                         xoffs= scene->r.disprect.xmin;
687                         yoffs= scene->r.disprect.ymin;
688                         glColor3ub(0,0,0);
689                         calc_image_view(sima, 'f');     
690                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
691                         glRectf(0.0f, 0.0f, 1.0f, 1.0f);
692                         glLoadIdentity();
693                 }
694         }
695 #endif
696
697         ED_space_image_release_buffer(sima, lock);
698
699         /* render info */
700         if(ima && show_render)
701                 draw_render_info(scene, ima, ar);
702 }
703