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