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