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