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