bugfix [#23173] Blender crashes on selecting display color corrected image in image...
[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, unsigned char col1[3], unsigned char col2[3])
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         glColor3ubv(col1);
233         glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
234         glColor3ubv(col2);
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 int 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         if(ibuf->crect) {
279                 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->crect);
280                 return 1;
281         }
282
283         return 0;
284 }
285 #endif
286
287 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
288 {
289         /* zbuffer values are signed, so we need to shift color range */
290         glPixelTransferf(GL_RED_SCALE, 0.5f);
291         glPixelTransferf(GL_GREEN_SCALE, 0.5f);
292         glPixelTransferf(GL_BLUE_SCALE, 0.5f);
293         glPixelTransferf(GL_RED_BIAS, 0.5f);
294         glPixelTransferf(GL_GREEN_BIAS, 0.5f);
295         glPixelTransferf(GL_BLUE_BIAS, 0.5f);
296         
297         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
298         
299         glPixelTransferf(GL_RED_SCALE, 1.0f);
300         glPixelTransferf(GL_GREEN_SCALE, 1.0f);
301         glPixelTransferf(GL_BLUE_SCALE, 1.0f);
302         glPixelTransferf(GL_RED_BIAS, 0.0f);
303         glPixelTransferf(GL_GREEN_BIAS, 0.0f);
304         glPixelTransferf(GL_BLUE_BIAS, 0.0f);
305 }
306
307 static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rectx, int recty, float *rect_float)
308 {
309         float bias, scale, *rectf, clipend;
310         int a;
311         
312         if(scene->camera && scene->camera->type==OB_CAMERA) {
313                 bias= ((Camera *)scene->camera->data)->clipsta;
314                 clipend= ((Camera *)scene->camera->data)->clipend;
315                 scale= 1.0f/(clipend-bias);
316         }
317         else {
318                 bias= 0.1f;
319                 scale= 0.01f;
320                 clipend= 100.0f;
321         }
322         
323         rectf= MEM_mallocN(rectx*recty*4, "temp");
324         for(a= rectx*recty -1; a>=0; a--) {
325                 if(rect_float[a]>clipend)
326                         rectf[a]= 0.0f;
327                 else if(rect_float[a]<bias)
328                         rectf[a]= 1.0f;
329                 else {
330                         rectf[a]= 1.0f - (rect_float[a]-bias)*scale;
331                         rectf[a]*= rectf[a];
332                 }
333         }
334         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
335         
336         MEM_freeN(rectf);
337 }
338
339 static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
340 {
341         int x, y;
342         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
343
344         /* set zoom */
345         glPixelZoom(zoomx, zoomy);
346
347         /* find window pixel coordinates of origin */
348         UI_view2d_to_region_no_clip(&ar->v2d, fx, fy, &x, &y);
349
350         /* this part is generic image display */
351         if(sima->flag & SI_SHOW_ALPHA) {
352                 if(ibuf->rect)
353                         sima_draw_alpha_pixels(x, y, ibuf->x, ibuf->y, ibuf->rect);
354                 else if(ibuf->rect_float && ibuf->channels==4)
355                         sima_draw_alpha_pixelsf(x, y, ibuf->x, ibuf->y, ibuf->rect_float);
356         }
357         else if(sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels==1))) {
358                 if(ibuf->zbuf)
359                         sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf);
360                 else if(ibuf->zbuf_float)
361                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float);
362                 else if(ibuf->channels==1)
363                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
364         }
365 #ifdef WITH_LCMS
366         else if(sima->flag & SI_COLOR_CORRECTION) {
367                 image_verify_buffer_float(sima, ima, ibuf, color_manage);
368                 
369                 if(sima_draw_colorcorrected_pixels(x, y, ibuf)==0) {
370                         unsigned char col1[3]= {100, 0, 100}, col2[3]= {160, 0, 160}; /* pink says 'warning' in blender land */
371                         sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
372                 }
373
374         }
375 #endif
376         else {
377                 if(sima->flag & SI_USE_ALPHA) {
378                         unsigned char col1[3]= {100, 100, 100}, col2[3]= {160, 160, 160};
379                         sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
380
381                         glEnable(GL_BLEND);
382                         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
383                 }
384
385                 /* we don't draw floats buffers directly but
386                  * convert them, and optionally apply curves */
387                 image_verify_buffer_float(sima, ima, ibuf, color_manage);
388
389                 if(ibuf->rect)
390                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
391                 /*else
392                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);*/
393                 
394                 if(sima->flag & SI_USE_ALPHA)
395                         glDisable(GL_BLEND);
396         }
397
398         /* reset zoom */
399         glPixelZoom(1.0f, 1.0f);
400 }
401
402 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
403 {
404         unsigned int *rt, *rp, *rectmain;
405         short y, heigth, len;
406
407         /* the right offset in rectot */
408
409         rt= ibuf->rect+ (starty*ibuf->x+ startx);
410
411         len= (endx-startx);
412         heigth= (endy-starty);
413
414         rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
415         
416         for(y=0; y<heigth; y++) {
417                 memcpy(rp, rt, len*4);
418                 rt+= ibuf->x;
419                 rp+= len;
420         }
421         return rectmain;
422 }
423
424 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
425 {
426         unsigned int *rect;
427         int dx, dy, sx, sy, x, y;
428         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
429
430         /* verify valid values, just leave this a while */
431         if(ima->xrep<1) return;
432         if(ima->yrep<1) return;
433         
434         glPixelZoom(zoomx, zoomy);
435
436         if(sima->curtile >= ima->xrep*ima->yrep) 
437                 sima->curtile = ima->xrep*ima->yrep - 1; 
438         
439         /* create char buffer from float if needed */
440         image_verify_buffer_float(sima, ima, ibuf, color_manage);
441
442         /* retrieve part of image buffer */
443         dx= ibuf->x/ima->xrep;
444         dy= ibuf->y/ima->yrep;
445         sx= (sima->curtile % ima->xrep)*dx;
446         sy= (sima->curtile / ima->xrep)*dy;
447         rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
448         
449         /* draw repeated */
450         for(sy=0; sy+dy<=ibuf->y; sy+= dy) {
451                 for(sx=0; sx+dx<=ibuf->x; sx+= dx) {
452                         UI_view2d_to_region_no_clip(&ar->v2d, fx + (float)sx/(float)ibuf->x, fy + (float)sy/(float)ibuf->y, &x, &y);
453
454                         glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
455                 }
456         }
457
458         glPixelZoom(1.0f, 1.0f);
459
460         MEM_freeN(rect);
461 }
462
463 static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
464 {
465         float x, y;
466         double time_current;
467         
468         time_current = PIL_check_seconds_timer();
469
470         for(x=floor(ar->v2d.cur.xmin); x<ar->v2d.cur.xmax; x += 1.0f) { 
471                 for(y=floor(ar->v2d.cur.ymin); y<ar->v2d.cur.ymax; y += 1.0f) { 
472                         if(ima && (ima->tpageflag & IMA_TILES))
473                                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
474                         else
475                                 draw_image_buffer(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
476
477                         /* only draw until running out of time */
478                         if((PIL_check_seconds_timer() - time_current) > 0.25)
479                                 return;
480                 }
481         }
482 }
483
484 /* draw uv edit */
485
486 /* draw grease pencil */
487 void draw_image_grease_pencil(bContext *C, short onlyv2d)
488 {
489         /* draw in View2D space? */
490         if (onlyv2d) {
491                 /* assume that UI_view2d_ortho(C) has been called... */
492                 SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
493                 void *lock;
494                 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
495                 
496                 /* draw grease-pencil ('image' strokes) */
497                 //if (sima->flag & SI_DISPGP)
498                         draw_gpencil_2dimage(C, ibuf);
499
500                 ED_space_image_release_buffer(sima, lock);
501         }
502         else {
503                 /* assume that UI_view2d_restore(C) has been called... */
504                 //SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
505                 
506                 /* draw grease-pencil ('screen' strokes) */
507                 //if (sima->flag & SI_DISPGP)
508                         draw_gpencil_view2d(C, 0);
509         }
510 }
511
512 /* XXX becomes WM paint cursor */
513 #if 0
514 static void draw_image_view_tool(Scene *scene)
515 {
516         ToolSettings *settings= scene->toolsettings;
517         Brush *brush= settings->imapaint.brush;
518         short mval[2];
519         float radius;
520         int draw= 0;
521
522         if(brush) {
523                 if(settings->imapaint.flag & IMAGEPAINT_DRAWING) {
524                         if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
525                                 draw= 1;
526                 }
527                 else if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
528                         draw= 1;
529                 
530                 if(draw) {
531                         getmouseco_areawin(mval);
532
533                         radius= brush_size(brush)*G.sima->zoom;
534                         fdrawXORcirc(mval[0], mval[1], radius);
535
536                         if (brush->innerradius != 1.0) {
537                                 radius *= brush->innerradius;
538                                 fdrawXORcirc(mval[0], mval[1], radius);
539                         }
540                 }
541         }
542 }
543 #endif
544
545 static unsigned char *get_alpha_clone_image(Scene *scene, int *width, int *height)
546 {
547         Brush *brush = paint_brush(&scene->toolsettings->imapaint.paint);
548         ImBuf *ibuf;
549         unsigned int size, alpha;
550         unsigned char *rect, *cp;
551
552         if(!brush || !brush->clone.image)
553                 return NULL;
554         
555         ibuf= BKE_image_get_ibuf(brush->clone.image, NULL);
556
557         if(!ibuf || !ibuf->rect)
558                 return NULL;
559
560         rect= MEM_dupallocN(ibuf->rect);
561         if(!rect)
562                 return NULL;
563
564         *width= ibuf->x;
565         *height= ibuf->y;
566
567         size= (*width)*(*height);
568         alpha= (unsigned char)255*brush->clone.alpha;
569         cp= rect;
570
571         while(size-- > 0) {
572                 cp[3]= alpha;
573                 cp += 4;
574         }
575
576         return rect;
577 }
578
579 static void draw_image_paint_helpers(SpaceImage *sima, ARegion *ar, Scene *scene, float zoomx, float zoomy)
580 {
581         Brush *brush;
582         int x, y, w, h;
583         unsigned char *clonerect;
584
585         brush= paint_brush(&scene->toolsettings->imapaint.paint);
586
587         if(brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
588                 /* this is not very efficient, but glDrawPixels doesn't allow
589                    drawing with alpha */
590                 clonerect= get_alpha_clone_image(scene, &w, &h);
591
592                 if(clonerect) {
593                         UI_view2d_to_region_no_clip(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
594
595                         glPixelZoom(zoomx, zoomy);
596
597                         glEnable(GL_BLEND);
598                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
599                         glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
600                         glDisable(GL_BLEND);
601
602                         glPixelZoom(1.0, 1.0);
603
604                         MEM_freeN(clonerect);
605                 }
606         }
607 }
608
609 /* draw main image area */
610
611 void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
612 {
613         Image *ima;
614         ImBuf *ibuf;
615         float zoomx, zoomy;
616         int show_viewer, show_render;
617         void *lock;
618
619         /* XXX can we do this in refresh? */
620 #if 0
621         what_image(sima);
622         
623         if(sima->image) {
624                 ED_image_aspect(sima->image, &xuser_asp, &yuser_asp);
625                 
626                 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
627                 if(sima->image->type==IMA_TYPE_COMPOSITE) {
628                         ImageUser *iuser= ntree_get_active_iuser(scene->nodetree);
629                         if(iuser) {
630                                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
631                                 sima->iuser= *iuser;
632                         }
633                 }
634                 /* and we check for spare */
635                 ibuf= ED_space_image_buffer(sima);
636         }
637 #endif
638
639         /* retrieve the image and information about it */
640         ima= ED_space_image(sima);
641         ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
642         ibuf= ED_space_image_acquire_buffer(sima, &lock);
643
644         show_viewer= (ima && ima->source == IMA_SRC_VIEWER);
645         show_render= (show_viewer && ima->type == IMA_TYPE_R_RESULT);
646
647         /* draw the image or grid */
648         if(ibuf==NULL)
649                 draw_image_grid(ar, zoomx, zoomy);
650         else if(sima->flag & SI_DRAW_TILE)
651                 draw_image_buffer_repeated(sima, ar, scene, ima, ibuf, zoomx, zoomy);
652         else if(ima && (ima->tpageflag & IMA_TILES))
653                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
654         else
655                 draw_image_buffer(sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
656
657         /* paint helpers */
658         draw_image_paint_helpers(sima, ar, scene, zoomx, zoomy);
659
660
661         /* XXX integrate this code */
662 #if 0
663         if(ibuf) {
664                 float xoffs=0.0f, yoffs= 0.0f;
665                 
666                 if(image_preview_active(sa, &xim, &yim)) {
667                         xoffs= scene->r.disprect.xmin;
668                         yoffs= scene->r.disprect.ymin;
669                         glColor3ub(0,0,0);
670                         calc_image_view(sima, 'f');     
671                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
672                         glRectf(0.0f, 0.0f, 1.0f, 1.0f);
673                         glLoadIdentity();
674                 }
675         }
676 #endif
677
678         ED_space_image_release_buffer(sima, lock);
679
680         /* render info */
681         if(ima && show_render)
682                 draw_render_info(scene, ima, ar);
683 }
684