Color management: fix for clone brush
[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(const bContext *C, 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                 unsigned char *display_buffer;
461                 void *cache_handle;
462
463                 if (sima->flag & SI_USE_ALPHA) {
464                         fdrawcheckerboard(x, y, x + ibuf->x * zoomx, y + ibuf->y * zoomy);
465
466                         glEnable(GL_BLEND);
467                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
468                 }
469
470                 /* we don't draw floats buffers directly but
471                  * convert them, and optionally apply curves */
472                 image_verify_buffer_float(ima, ibuf, color_manage);
473
474                 display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
475
476                 if (display_buffer)
477                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, display_buffer);
478 #if 0
479                 else
480                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
481 #endif
482
483                 IMB_display_buffer_release(cache_handle);
484
485                 if (sima->flag & SI_USE_ALPHA)
486                         glDisable(GL_BLEND);
487         }
488
489         /* reset zoom */
490         glPixelZoom(1.0f, 1.0f);
491 }
492
493 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
494 {
495         unsigned int *rt, *rp, *rectmain;
496         short y, heigth, len;
497
498         /* the right offset in rectot */
499
500         rt = ibuf->rect + (starty * ibuf->x + startx);
501
502         len = (endx - startx);
503         heigth = (endy - starty);
504
505         rp = rectmain = MEM_mallocN(heigth * len * sizeof(int), "rect");
506         
507         for (y = 0; y < heigth; y++) {
508                 memcpy(rp, rt, len * 4);
509                 rt += ibuf->x;
510                 rp += len;
511         }
512         return rectmain;
513 }
514
515 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
516 {
517         unsigned int *rect;
518         int dx, dy, sx, sy, x, y;
519         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
520
521         /* verify valid values, just leave this a while */
522         if (ima->xrep < 1) return;
523         if (ima->yrep < 1) return;
524         
525         glPixelZoom(zoomx, zoomy);
526
527         if (sima->curtile >= ima->xrep * ima->yrep)
528                 sima->curtile = ima->xrep * ima->yrep - 1;
529         
530         /* create char buffer from float if needed */
531         image_verify_buffer_float(ima, ibuf, color_manage);
532
533         /* retrieve part of image buffer */
534         dx = ibuf->x / ima->xrep;
535         dy = ibuf->y / ima->yrep;
536         sx = (sima->curtile % ima->xrep) * dx;
537         sy = (sima->curtile / ima->xrep) * dy;
538         rect = get_part_from_ibuf(ibuf, sx, sy, sx + dx, sy + dy);
539         
540         /* draw repeated */
541         for (sy = 0; sy + dy <= ibuf->y; sy += dy) {
542                 for (sx = 0; sx + dx <= ibuf->x; sx += dx) {
543                         UI_view2d_to_region_no_clip(&ar->v2d, fx + (float)sx / (float)ibuf->x, fy + (float)sy / (float)ibuf->y, &x, &y);
544
545                         glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
546                 }
547         }
548
549         glPixelZoom(1.0f, 1.0f);
550
551         MEM_freeN(rect);
552 }
553
554 static void draw_image_buffer_repeated(const bContext *C, SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
555 {
556         const double time_current = PIL_check_seconds_timer();
557
558         const int xmax = ceil(ar->v2d.cur.xmax);
559         const int ymax = ceil(ar->v2d.cur.ymax);
560         const int xmin = floor(ar->v2d.cur.xmin);
561         const int ymin = floor(ar->v2d.cur.ymin);
562
563         int x;
564
565         for (x = xmin; x < xmax; x++) {
566                 int y;
567                 for (y = ymin; y < ymax; y++) {
568                         if (ima && (ima->tpageflag & IMA_TILES))
569                                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
570                         else
571                                 draw_image_buffer(C, sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
572
573                         /* only draw until running out of time */
574                         if ((PIL_check_seconds_timer() - time_current) > 0.25)
575                                 return;
576                 }
577         }
578 }
579
580 /* draw uv edit */
581
582 /* draw grease pencil */
583 void draw_image_grease_pencil(bContext *C, short onlyv2d)
584 {
585         /* draw in View2D space? */
586         if (onlyv2d) {
587                 /* draw grease-pencil ('image' strokes) */
588                 draw_gpencil_2dimage(C);
589         }
590         else {
591                 /* assume that UI_view2d_restore(C) has been called... */
592                 //SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
593                 
594                 /* draw grease-pencil ('screen' strokes) */
595                 draw_gpencil_view2d(C, 0);
596         }
597 }
598
599 void draw_image_sample_line(SpaceImage *sima)
600 {
601         if (sima->sample_line_hist.flag & HISTO_FLAG_SAMPLELINE) {
602                 Histogram *hist = &sima->sample_line_hist;
603
604                 glBegin(GL_LINES);
605                 glColor3ub(0, 0, 0);
606                 glVertex2fv(hist->co[0]);
607                 glVertex2fv(hist->co[1]);
608                 glEnd();
609
610                 setlinestyle(1);
611                 glBegin(GL_LINES);
612                 glColor3ub(255, 255, 255);
613                 glVertex2fv(hist->co[0]);
614                 glVertex2fv(hist->co[1]);
615                 glEnd();
616                 setlinestyle(0);
617
618         }
619 }
620
621 /* XXX becomes WM paint cursor */
622 #if 0
623 static void draw_image_view_tool(Scene *scene)
624 {
625         ToolSettings *settings = scene->toolsettings;
626         Brush *brush = settings->imapaint.brush;
627         int mval[2];
628         float radius;
629         int draw = 0;
630
631         if (brush) {
632                 if (settings->imapaint.flag & IMAGEPAINT_DRAWING) {
633                         if (settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
634                                 draw = 1;
635                 }
636                 else if (settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
637                         draw = 1;
638                 
639                 if (draw) {
640                         getmouseco_areawin(mval);
641
642                         radius = BKE_brush_size_get(brush) * G.sima->zoom;
643                         fdrawXORcirc(mval[0], mval[1], radius);
644
645                         if (brush->innerradius != 1.0) {
646                                 radius *= brush->innerradius;
647                                 fdrawXORcirc(mval[0], mval[1], radius);
648                         }
649                 }
650         }
651 }
652 #endif
653
654 static unsigned char *get_alpha_clone_image(const bContext *C, Scene *scene, int *width, int *height)
655 {
656         Brush *brush = paint_brush(&scene->toolsettings->imapaint.paint);
657         ImBuf *ibuf;
658         unsigned int size, alpha;
659         unsigned char *display_buffer;
660         unsigned char *rect, *cp;
661         void *cache_handle;
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)
669                 return NULL;
670
671         display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
672
673         if (!display_buffer) {
674                 IMB_display_buffer_release(cache_handle);
675
676                 return NULL;
677         }
678
679         rect = MEM_dupallocN(display_buffer);
680
681         IMB_display_buffer_release(cache_handle);
682
683         if (!rect)
684                 return NULL;
685
686         *width = ibuf->x;
687         *height = ibuf->y;
688
689         size = (*width) * (*height);
690         alpha = (unsigned char)255 * brush->clone.alpha;
691         cp = rect;
692
693         while (size-- > 0) {
694                 cp[3] = alpha;
695                 cp += 4;
696         }
697
698         return rect;
699 }
700
701 static void draw_image_paint_helpers(const bContext *C, ARegion *ar, Scene *scene, float zoomx, float zoomy)
702 {
703         Brush *brush;
704         int x, y, w, h;
705         unsigned char *clonerect;
706
707         brush = paint_brush(&scene->toolsettings->imapaint.paint);
708
709         if (brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
710                 /* this is not very efficient, but glDrawPixels doesn't allow
711                  * drawing with alpha */
712                 clonerect = get_alpha_clone_image(C, scene, &w, &h);
713
714                 if (clonerect) {
715                         UI_view2d_to_region_no_clip(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
716
717                         glPixelZoom(zoomx, zoomy);
718
719                         glEnable(GL_BLEND);
720                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
721                         glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
722                         glDisable(GL_BLEND);
723
724                         glPixelZoom(1.0, 1.0);
725
726                         MEM_freeN(clonerect);
727                 }
728         }
729 }
730
731 /* draw main image area */
732
733 void draw_image_main(const bContext *C, ARegion *ar)
734 {
735         SpaceImage *sima = CTX_wm_space_image(C);
736         Scene *scene = CTX_data_scene(C);
737         Image *ima;
738         ImBuf *ibuf;
739         float zoomx, zoomy;
740         int show_viewer, show_render;
741         void *lock;
742
743         /* XXX can we do this in refresh? */
744 #if 0
745         what_image(sima);
746         
747         if (sima->image) {
748                 ED_image_get_aspect(sima->image, &xuser_asp, &yuser_asp);
749                 
750                 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
751                 if (sima->image->type == IMA_TYPE_COMPOSITE) {
752                         ImageUser *iuser = ntree_get_active_iuser(scene->nodetree);
753                         if (iuser) {
754                                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
755                                 sima->iuser = *iuser;
756                         }
757                 }
758                 /* and we check for spare */
759                 ibuf = ED_space_image_buffer(sima);
760         }
761 #endif
762
763         /* retrieve the image and information about it */
764         ima = ED_space_image(sima);
765         ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
766
767         show_viewer = (ima && ima->source == IMA_SRC_VIEWER);
768         show_render = (show_viewer && ima->type == IMA_TYPE_R_RESULT);
769
770         if (show_viewer) {
771                 /* use locked draw for drawing viewer image buffer since the conpositor
772                  * is running in separated thread and compositor could free this buffers.
773                  * other images are not modifying in such a way so they does not require
774                  * lock (sergey)
775                  */
776                 BLI_lock_thread(LOCK_DRAW_IMAGE);
777         }
778
779         ibuf = ED_space_image_acquire_buffer(sima, &lock);
780
781         /* draw the image or grid */
782         if (ibuf == NULL)
783                 ED_region_grid_draw(ar, zoomx, zoomy);
784         else if (sima->flag & SI_DRAW_TILE)
785                 draw_image_buffer_repeated(C, sima, ar, scene, ima, ibuf, zoomx, zoomy);
786         else if (ima && (ima->tpageflag & IMA_TILES))
787                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
788         else
789                 draw_image_buffer(C, sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
790
791         /* paint helpers */
792         if (sima->mode == SI_MODE_PAINT)
793                 draw_image_paint_helpers(C, ar, scene, zoomx, zoomy);
794
795
796         /* XXX integrate this code */
797 #if 0
798         if (ibuf) {
799                 float xoffs = 0.0f, yoffs = 0.0f;
800                 
801                 if (image_preview_active(sa, &xim, &yim)) {
802                         xoffs = scene->r.disprect.xmin;
803                         yoffs = scene->r.disprect.ymin;
804                         glColor3ub(0, 0, 0);
805                         calc_image_view(sima, 'f');     
806                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
807                         glRectf(0.0f, 0.0f, 1.0f, 1.0f);
808                         glLoadIdentity();
809                 }
810         }
811 #endif
812
813         ED_space_image_release_buffer(sima, lock);
814
815         if (show_viewer) {
816                 BLI_unlock_thread(LOCK_DRAW_IMAGE);
817         }
818
819         /* render info */
820         if (ima && show_render)
821                 draw_render_info(scene, ima, ar);
822 }