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