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