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