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