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