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