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