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