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