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