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