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