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