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