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