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