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