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