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