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