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