Color management: display color managed RGB values in color sample line
[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(Scene *scene, 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                 /* OCIO_TODO: make it fit better to overall color interaction */
226                 if (fp && channels == 4) {
227                         float pixel[4];
228
229                         IMB_display_buffer_pixel(pixel, fp,  &scene->view_settings, &scene->display_settings);
230
231                         BLI_snprintf(str, sizeof(str), "  |  CM  R:%-.4f  G:%-.4f  B:%-.4f", pixel[0], pixel[1], pixel[2]);
232                         BLF_position(blf_mono_font, dx, 6, 0);
233                         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
234                         dx += BLF_width(blf_mono_font, str);
235                 }
236         }
237         
238         /* color rectangle */
239         if (channels == 1) {
240                 if (fp) {
241                         col[0] = col[1] = col[2] = fp[0];
242                 }
243                 else if (cp) {
244                         col[0] = col[1] = col[2] = (float)cp[0] / 255.0f;
245                 }
246                 else {
247                         col[0] = col[1] = col[2] = 0.0f;
248                 }
249                 col[3] = 1.0f;
250         }
251         else if (channels == 3) {
252                 if (fp) {
253                         copy_v3_v3(col, fp);
254                 }
255                 else if (cp) {
256                         rgb_uchar_to_float(col, cp);
257                 }
258                 else {
259                         zero_v3(col);
260                 }
261                 col[3] = 1.0f;
262         }
263         else if (channels == 4) {
264                 if (fp)
265                         copy_v4_v4(col, fp);
266                 else if (cp) {
267                         rgba_uchar_to_float(col, cp);
268                 }
269                 else {
270                         zero_v4(col);
271                 }
272         }
273         else {
274                 BLI_assert(0);
275                 zero_v4(col);
276         }
277
278         if (color_manage) {
279                 linearrgb_to_srgb_v4(finalcol, col);
280         }
281         else {
282                 copy_v4_v4(finalcol, col);
283         }
284         glDisable(GL_BLEND);
285         glColor3fv(finalcol);
286         dx += 5;
287         glBegin(GL_QUADS);
288         glVertex2f(dx, 3);
289         glVertex2f(dx, 17);
290         glVertex2f(dx + 30, 17);
291         glVertex2f(dx + 30, 3);
292         glEnd();
293
294         /* draw outline */
295         glColor3ub(128, 128, 128);
296         glBegin(GL_LINE_LOOP);
297         glVertex2f(dx, 3);
298         glVertex2f(dx, 17);
299         glVertex2f(dx + 30, 17);
300         glVertex2f(dx + 30, 3);
301         glEnd();
302
303         dx += 35;
304
305         glColor3ub(255, 255, 255);
306         if (channels == 1) {
307                 if (fp) {
308                         rgb_to_hsv(fp[0], fp[0], fp[0], &hue, &sat, &val);
309                         rgb_to_yuv(fp[0], fp[0], fp[0], &lum, &u, &v);
310                 }
311                 else if (cp) {
312                         rgb_to_hsv((float)cp[0] / 255.0f, (float)cp[0] / 255.0f, (float)cp[0] / 255.0f, &hue, &sat, &val);
313                         rgb_to_yuv((float)cp[0] / 255.0f, (float)cp[0] / 255.0f, (float)cp[0] / 255.0f, &lum, &u, &v);
314                 }
315                 
316                 BLI_snprintf(str, sizeof(str), "V:%-.4f", val);
317                 BLF_position(blf_mono_font, dx, 6, 0);
318                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
319                 dx += BLF_width(blf_mono_font, str);
320
321                 BLI_snprintf(str, sizeof(str), "   L:%-.4f", lum);
322                 BLF_position(blf_mono_font, dx, 6, 0);
323                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
324                 dx += BLF_width(blf_mono_font, str);
325         }
326         else if (channels >= 3) {
327                 if (fp) {
328                         rgb_to_hsv(fp[0], fp[1], fp[2], &hue, &sat, &val);
329                         rgb_to_yuv(fp[0], fp[1], fp[2], &lum, &u, &v);
330                 }
331                 else if (cp) {
332                         rgb_to_hsv((float)cp[0] / 255.0f, (float)cp[1] / 255.0f, (float)cp[2] / 255.0f, &hue, &sat, &val);
333                         rgb_to_yuv((float)cp[0] / 255.0f, (float)cp[1] / 255.0f, (float)cp[2] / 255.0f, &lum, &u, &v);
334                 }
335
336                 BLI_snprintf(str, sizeof(str), "H:%-.4f", hue);
337                 BLF_position(blf_mono_font, dx, 6, 0);
338                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
339                 dx += BLF_width(blf_mono_font, str);
340
341                 BLI_snprintf(str, sizeof(str), "  S:%-.4f", sat);
342                 BLF_position(blf_mono_font, dx, 6, 0);
343                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
344                 dx += BLF_width(blf_mono_font, str);
345
346                 BLI_snprintf(str, sizeof(str), "  V:%-.4f", val);
347                 BLF_position(blf_mono_font, dx, 6, 0);
348                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
349                 dx += BLF_width(blf_mono_font, str);
350
351                 BLI_snprintf(str, sizeof(str), "   L:%-.4f", lum);
352                 BLF_position(blf_mono_font, dx, 6, 0);
353                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
354                 dx += BLF_width(blf_mono_font, str);
355         }
356
357         (void)dx;
358 }
359
360 /* image drawing */
361
362 static void sima_draw_alpha_pixels(float x1, float y1, int rectx, int recty, unsigned int *recti)
363 {
364         
365         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
366         if (ENDIAN_ORDER == B_ENDIAN)
367                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
368
369         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_UNSIGNED_INT, recti);
370         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
371 }
372
373 static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, float *rectf)
374 {
375         float *trectf = MEM_mallocN(rectx * recty * 4, "temp");
376         int a, b;
377         
378         for (a = rectx * recty - 1, b = 4 * a + 3; a >= 0; a--, b -= 4)
379                 trectf[a] = rectf[b];
380         
381         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, trectf);
382         MEM_freeN(trectf);
383         /* ogl trick below is slower... (on ATI 9600) */
384 //      glColorMask(1, 0, 0, 0);
385 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+3);
386 //      glColorMask(0, 1, 0, 0);
387 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+2);
388 //      glColorMask(0, 0, 1, 0);
389 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+1);
390 //      glColorMask(1, 1, 1, 1);
391 }
392
393 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
394 {
395         /* zbuffer values are signed, so we need to shift color range */
396         glPixelTransferf(GL_RED_SCALE, 0.5f);
397         glPixelTransferf(GL_GREEN_SCALE, 0.5f);
398         glPixelTransferf(GL_BLUE_SCALE, 0.5f);
399         glPixelTransferf(GL_RED_BIAS, 0.5f);
400         glPixelTransferf(GL_GREEN_BIAS, 0.5f);
401         glPixelTransferf(GL_BLUE_BIAS, 0.5f);
402         
403         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
404         
405         glPixelTransferf(GL_RED_SCALE, 1.0f);
406         glPixelTransferf(GL_GREEN_SCALE, 1.0f);
407         glPixelTransferf(GL_BLUE_SCALE, 1.0f);
408         glPixelTransferf(GL_RED_BIAS, 0.0f);
409         glPixelTransferf(GL_GREEN_BIAS, 0.0f);
410         glPixelTransferf(GL_BLUE_BIAS, 0.0f);
411 }
412
413 static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rectx, int recty, float *rect_float)
414 {
415         float bias, scale, *rectf, clipend;
416         int a;
417         
418         if (scene->camera && scene->camera->type == OB_CAMERA) {
419                 bias = ((Camera *)scene->camera->data)->clipsta;
420                 clipend = ((Camera *)scene->camera->data)->clipend;
421                 scale = 1.0f / (clipend - bias);
422         }
423         else {
424                 bias = 0.1f;
425                 scale = 0.01f;
426                 clipend = 100.0f;
427         }
428         
429         rectf = MEM_mallocN(rectx * recty * 4, "temp");
430         for (a = rectx * recty - 1; a >= 0; a--) {
431                 if (rect_float[a] > clipend)
432                         rectf[a] = 0.0f;
433                 else if (rect_float[a] < bias)
434                         rectf[a] = 1.0f;
435                 else {
436                         rectf[a] = 1.0f - (rect_float[a] - bias) * scale;
437                         rectf[a] *= rectf[a];
438                 }
439         }
440         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
441         
442         MEM_freeN(rectf);
443 }
444
445 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)
446 {
447         int x, y;
448         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
449
450         /* set zoom */
451         glPixelZoom(zoomx, zoomy);
452
453         /* find window pixel coordinates of origin */
454         UI_view2d_to_region_no_clip(&ar->v2d, fx, fy, &x, &y);
455
456         /* this part is generic image display */
457         if (sima->flag & SI_SHOW_ALPHA) {
458                 if (ibuf->rect)
459                         sima_draw_alpha_pixels(x, y, ibuf->x, ibuf->y, ibuf->rect);
460                 else if (ibuf->rect_float && ibuf->channels == 4)
461                         sima_draw_alpha_pixelsf(x, y, ibuf->x, ibuf->y, ibuf->rect_float);
462         }
463         else if (sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels == 1))) {
464                 if (ibuf->zbuf)
465                         sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf);
466                 else if (ibuf->zbuf_float)
467                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float);
468                 else if (ibuf->channels == 1)
469                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
470         }
471         else {
472                 unsigned char *display_buffer;
473                 void *cache_handle;
474
475                 if (sima->flag & SI_USE_ALPHA) {
476                         fdrawcheckerboard(x, y, x + ibuf->x * zoomx, y + ibuf->y * zoomy);
477
478                         glEnable(GL_BLEND);
479                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
480                 }
481
482                 /* we don't draw floats buffers directly but
483                  * convert them, and optionally apply curves */
484                 image_verify_buffer_float(ima, ibuf, color_manage);
485
486                 display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
487
488                 if (display_buffer)
489                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, display_buffer);
490 #if 0
491                 else
492                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
493 #endif
494
495                 IMB_display_buffer_release(cache_handle);
496
497                 if (sima->flag & SI_USE_ALPHA)
498                         glDisable(GL_BLEND);
499         }
500
501         /* reset zoom */
502         glPixelZoom(1.0f, 1.0f);
503 }
504
505 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
506 {
507         unsigned int *rt, *rp, *rectmain;
508         short y, heigth, len;
509
510         /* the right offset in rectot */
511
512         rt = ibuf->rect + (starty * ibuf->x + startx);
513
514         len = (endx - startx);
515         heigth = (endy - starty);
516
517         rp = rectmain = MEM_mallocN(heigth * len * sizeof(int), "rect");
518         
519         for (y = 0; y < heigth; y++) {
520                 memcpy(rp, rt, len * 4);
521                 rt += ibuf->x;
522                 rp += len;
523         }
524         return rectmain;
525 }
526
527 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
528 {
529         unsigned int *rect;
530         int dx, dy, sx, sy, x, y;
531         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
532
533         /* verify valid values, just leave this a while */
534         if (ima->xrep < 1) return;
535         if (ima->yrep < 1) return;
536         
537         glPixelZoom(zoomx, zoomy);
538
539         if (sima->curtile >= ima->xrep * ima->yrep)
540                 sima->curtile = ima->xrep * ima->yrep - 1;
541         
542         /* create char buffer from float if needed */
543         image_verify_buffer_float(ima, ibuf, color_manage);
544
545         /* retrieve part of image buffer */
546         dx = ibuf->x / ima->xrep;
547         dy = ibuf->y / ima->yrep;
548         sx = (sima->curtile % ima->xrep) * dx;
549         sy = (sima->curtile / ima->xrep) * dy;
550         rect = get_part_from_ibuf(ibuf, sx, sy, sx + dx, sy + dy);
551         
552         /* draw repeated */
553         for (sy = 0; sy + dy <= ibuf->y; sy += dy) {
554                 for (sx = 0; sx + dx <= ibuf->x; sx += dx) {
555                         UI_view2d_to_region_no_clip(&ar->v2d, fx + (float)sx / (float)ibuf->x, fy + (float)sy / (float)ibuf->y, &x, &y);
556
557                         glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
558                 }
559         }
560
561         glPixelZoom(1.0f, 1.0f);
562
563         MEM_freeN(rect);
564 }
565
566 static void draw_image_buffer_repeated(const bContext *C, SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
567 {
568         const double time_current = PIL_check_seconds_timer();
569
570         const int xmax = ceil(ar->v2d.cur.xmax);
571         const int ymax = ceil(ar->v2d.cur.ymax);
572         const int xmin = floor(ar->v2d.cur.xmin);
573         const int ymin = floor(ar->v2d.cur.ymin);
574
575         int x;
576
577         for (x = xmin; x < xmax; x++) {
578                 int y;
579                 for (y = ymin; y < ymax; y++) {
580                         if (ima && (ima->tpageflag & IMA_TILES))
581                                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
582                         else
583                                 draw_image_buffer(C, sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
584
585                         /* only draw until running out of time */
586                         if ((PIL_check_seconds_timer() - time_current) > 0.25)
587                                 return;
588                 }
589         }
590 }
591
592 /* draw uv edit */
593
594 /* draw grease pencil */
595 void draw_image_grease_pencil(bContext *C, short onlyv2d)
596 {
597         /* draw in View2D space? */
598         if (onlyv2d) {
599                 /* draw grease-pencil ('image' strokes) */
600                 draw_gpencil_2dimage(C);
601         }
602         else {
603                 /* assume that UI_view2d_restore(C) has been called... */
604                 //SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
605                 
606                 /* draw grease-pencil ('screen' strokes) */
607                 draw_gpencil_view2d(C, 0);
608         }
609 }
610
611 void draw_image_sample_line(SpaceImage *sima)
612 {
613         if (sima->sample_line_hist.flag & HISTO_FLAG_SAMPLELINE) {
614                 Histogram *hist = &sima->sample_line_hist;
615
616                 glBegin(GL_LINES);
617                 glColor3ub(0, 0, 0);
618                 glVertex2fv(hist->co[0]);
619                 glVertex2fv(hist->co[1]);
620                 glEnd();
621
622                 setlinestyle(1);
623                 glBegin(GL_LINES);
624                 glColor3ub(255, 255, 255);
625                 glVertex2fv(hist->co[0]);
626                 glVertex2fv(hist->co[1]);
627                 glEnd();
628                 setlinestyle(0);
629
630         }
631 }
632
633 /* XXX becomes WM paint cursor */
634 #if 0
635 static void draw_image_view_tool(Scene *scene)
636 {
637         ToolSettings *settings = scene->toolsettings;
638         Brush *brush = settings->imapaint.brush;
639         int mval[2];
640         float radius;
641         int draw = 0;
642
643         if (brush) {
644                 if (settings->imapaint.flag & IMAGEPAINT_DRAWING) {
645                         if (settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
646                                 draw = 1;
647                 }
648                 else if (settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
649                         draw = 1;
650                 
651                 if (draw) {
652                         getmouseco_areawin(mval);
653
654                         radius = BKE_brush_size_get(brush) * G.sima->zoom;
655                         fdrawXORcirc(mval[0], mval[1], radius);
656
657                         if (brush->innerradius != 1.0) {
658                                 radius *= brush->innerradius;
659                                 fdrawXORcirc(mval[0], mval[1], radius);
660                         }
661                 }
662         }
663 }
664 #endif
665
666 static unsigned char *get_alpha_clone_image(const bContext *C, Scene *scene, int *width, int *height)
667 {
668         Brush *brush = paint_brush(&scene->toolsettings->imapaint.paint);
669         ImBuf *ibuf;
670         unsigned int size, alpha;
671         unsigned char *display_buffer;
672         unsigned char *rect, *cp;
673         void *cache_handle;
674
675         if (!brush || !brush->clone.image)
676                 return NULL;
677         
678         ibuf = BKE_image_get_ibuf(brush->clone.image, NULL);
679
680         if (!ibuf)
681                 return NULL;
682
683         display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
684
685         if (!display_buffer) {
686                 IMB_display_buffer_release(cache_handle);
687
688                 return NULL;
689         }
690
691         rect = MEM_dupallocN(display_buffer);
692
693         IMB_display_buffer_release(cache_handle);
694
695         if (!rect)
696                 return NULL;
697
698         *width = ibuf->x;
699         *height = ibuf->y;
700
701         size = (*width) * (*height);
702         alpha = (unsigned char)255 * brush->clone.alpha;
703         cp = rect;
704
705         while (size-- > 0) {
706                 cp[3] = alpha;
707                 cp += 4;
708         }
709
710         return rect;
711 }
712
713 static void draw_image_paint_helpers(const bContext *C, ARegion *ar, Scene *scene, float zoomx, float zoomy)
714 {
715         Brush *brush;
716         int x, y, w, h;
717         unsigned char *clonerect;
718
719         brush = paint_brush(&scene->toolsettings->imapaint.paint);
720
721         if (brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
722                 /* this is not very efficient, but glDrawPixels doesn't allow
723                  * drawing with alpha */
724                 clonerect = get_alpha_clone_image(C, scene, &w, &h);
725
726                 if (clonerect) {
727                         UI_view2d_to_region_no_clip(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
728
729                         glPixelZoom(zoomx, zoomy);
730
731                         glEnable(GL_BLEND);
732                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
733                         glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
734                         glDisable(GL_BLEND);
735
736                         glPixelZoom(1.0, 1.0);
737
738                         MEM_freeN(clonerect);
739                 }
740         }
741 }
742
743 /* draw main image area */
744
745 void draw_image_main(const bContext *C, ARegion *ar)
746 {
747         SpaceImage *sima = CTX_wm_space_image(C);
748         Scene *scene = CTX_data_scene(C);
749         Image *ima;
750         ImBuf *ibuf;
751         float zoomx, zoomy;
752         int show_viewer, show_render;
753         void *lock;
754
755         /* XXX can we do this in refresh? */
756 #if 0
757         what_image(sima);
758         
759         if (sima->image) {
760                 ED_image_get_aspect(sima->image, &xuser_asp, &yuser_asp);
761                 
762                 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
763                 if (sima->image->type == IMA_TYPE_COMPOSITE) {
764                         ImageUser *iuser = ntree_get_active_iuser(scene->nodetree);
765                         if (iuser) {
766                                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
767                                 sima->iuser = *iuser;
768                         }
769                 }
770                 /* and we check for spare */
771                 ibuf = ED_space_image_buffer(sima);
772         }
773 #endif
774
775         /* retrieve the image and information about it */
776         ima = ED_space_image(sima);
777         ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
778
779         show_viewer = (ima && ima->source == IMA_SRC_VIEWER);
780         show_render = (show_viewer && ima->type == IMA_TYPE_R_RESULT);
781
782         if (show_viewer) {
783                 /* use locked draw for drawing viewer image buffer since the conpositor
784                  * is running in separated thread and compositor could free this buffers.
785                  * other images are not modifying in such a way so they does not require
786                  * lock (sergey)
787                  */
788                 BLI_lock_thread(LOCK_DRAW_IMAGE);
789         }
790
791         ibuf = ED_space_image_acquire_buffer(sima, &lock);
792
793         /* draw the image or grid */
794         if (ibuf == NULL)
795                 ED_region_grid_draw(ar, zoomx, zoomy);
796         else if (sima->flag & SI_DRAW_TILE)
797                 draw_image_buffer_repeated(C, sima, ar, scene, ima, ibuf, zoomx, zoomy);
798         else if (ima && (ima->tpageflag & IMA_TILES))
799                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
800         else
801                 draw_image_buffer(C, sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
802
803         /* paint helpers */
804         if (sima->mode == SI_MODE_PAINT)
805                 draw_image_paint_helpers(C, ar, scene, zoomx, zoomy);
806
807
808         /* XXX integrate this code */
809 #if 0
810         if (ibuf) {
811                 float xoffs = 0.0f, yoffs = 0.0f;
812                 
813                 if (image_preview_active(sa, &xim, &yim)) {
814                         xoffs = scene->r.disprect.xmin;
815                         yoffs = scene->r.disprect.ymin;
816                         glColor3ub(0, 0, 0);
817                         calc_image_view(sima, 'f');     
818                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
819                         glRectf(0.0f, 0.0f, 1.0f, 1.0f);
820                         glLoadIdentity();
821                 }
822         }
823 #endif
824
825         ED_space_image_release_buffer(sima, lock);
826
827         if (show_viewer) {
828                 BLI_unlock_thread(LOCK_DRAW_IMAGE);
829         }
830
831         /* render info */
832         if (ima && show_render)
833                 draw_render_info(scene, ima, ar);
834 }