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