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