Make image drawing code working with core profile
[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 #include "DNA_mask_types.h"
44
45 #include "PIL_time.h"
46
47 #include "BLI_math.h"
48 #include "BLI_rect.h"
49 #include "BLI_threads.h"
50 #include "BLI_string.h"
51 #include "BLI_utildefines.h"
52
53 #include "IMB_imbuf.h"
54 #include "IMB_imbuf_types.h"
55 #include "IMB_colormanagement.h"
56 #include "IMB_moviecache.h"
57
58 #include "BKE_context.h"
59 #include "BKE_global.h"
60 #include "BKE_image.h"
61 #include "BKE_paint.h"
62
63 #include "BIF_glutil.h"
64
65 #include "GPU_immediate.h"
66 #include "GPU_immediate_util.h"
67 #include "GPU_matrix.h"
68
69 #include "BLF_api.h"
70
71 #include "ED_gpencil.h"
72 #include "ED_image.h"
73 #include "ED_mask.h"
74 #include "ED_render.h"
75 #include "ED_screen.h"
76
77 #include "UI_interface.h"
78 #include "UI_resources.h"
79 #include "UI_view2d.h"
80
81 #include "RE_pipeline.h"
82 #include "RE_engine.h"
83
84 #include "image_intern.h"
85
86 static void draw_render_info(const bContext *C,
87                              Scene *scene,
88                              Image *ima,
89                              ARegion *ar,
90                              float zoomx,
91                              float zoomy)
92 {
93         Render *re = RE_GetRender(scene->id.name);
94         RenderData *rd = RE_engine_get_render_data(re);
95         Scene *stats_scene = ED_render_job_get_scene(C);
96         if (stats_scene == NULL) {
97                 stats_scene = CTX_data_scene(C);
98         }
99
100         RenderResult *rr = BKE_image_acquire_renderresult(stats_scene, ima);
101
102         if (rr && rr->text) {
103                 float fill_color[4] = {0.0f, 0.0f, 0.0f, 0.25f};
104                 ED_region_info_draw(ar, rr->text, fill_color, true);
105         }
106
107         BKE_image_release_renderresult(stats_scene, ima);
108
109         if (re) {
110                 int total_tiles;
111                 bool need_free_tiles;
112                 rcti *tiles = RE_engine_get_current_tiles(re, &total_tiles, &need_free_tiles);
113
114                 if (total_tiles) {
115                         /* find window pixel coordinates of origin */
116                         int x, y;
117                         UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &x, &y);
118
119                         gpuPushMatrix();
120                         gpuTranslate2f(x, y);
121                         gpuScale2f(zoomx, zoomy);
122
123                         if (rd->mode & R_BORDER) {
124                                 /* TODO: round or floor instead of casting to int */
125                                 gpuTranslate2f((int)(-rd->border.xmin * rd->xsch * rd->size * 0.01f),
126                                                (int)(-rd->border.ymin * rd->ysch * rd->size * 0.01f));
127                         }
128
129                         unsigned int pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_F32, 2, KEEP_FLOAT);
130                         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
131                         immUniformThemeColor(TH_FACE_SELECT);
132
133                         glLineWidth(1.0f);
134
135                         rcti *tile = tiles;
136                         for (int i = 0; i < total_tiles; i++, tile++) {
137                                 immDrawBorderCorners(pos, tile, zoomx, zoomy);
138                         }
139
140                         immUnbindProgram();
141
142                         if (need_free_tiles) {
143                                 MEM_freeN(tiles);
144                         }
145
146                         gpuPopMatrix();
147                 }
148         }
149 }
150
151 /* used by node view too */
152 void ED_image_draw_info(Scene *scene, ARegion *ar, bool color_manage, bool use_default_view, int channels, int x, int y,
153                         const unsigned char cp[4], const float fp[4], const float linearcol[4], int *zp, float *zpf)
154 {
155         rcti color_rect;
156         char str[256];
157         int dx = 6;
158         const int dy = 0.3f * UI_UNIT_Y;
159         /* text colors */
160         /* XXX colored text not allowed in Blender UI */
161 #if 0
162         unsigned char red[3] = {255, 50, 50};
163         unsigned char green[3] = {0, 255, 0};
164         unsigned char blue[3] = {100, 100, 255};
165 #else
166         unsigned char red[3] = {255, 255, 255};
167         unsigned char green[3] = {255, 255, 255};
168         unsigned char blue[3] = {255, 255, 255};
169 #endif
170         float hue = 0, sat = 0, val = 0, lum = 0, u = 0, v = 0;
171         float col[4], finalcol[4];
172
173         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
174         glEnable(GL_BLEND);
175
176         unsigned int pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_I32, 2, CONVERT_INT_TO_FLOAT);
177         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
178
179         /* noisy, high contrast make impossible to read if lower alpha is used. */
180         immUniformColor4ub(0, 0, 0, 190);
181         immRecti(pos, 0, 0, BLI_rcti_size_x(&ar->winrct) + 1, UI_UNIT_Y);
182
183         immUnbindProgram();
184
185         glDisable(GL_BLEND);
186
187         BLF_size(blf_mono_font, 11 * U.pixelsize, U.dpi);
188
189         BLF_color3ub(blf_mono_font, 255, 255, 255);
190         BLI_snprintf(str, sizeof(str), "X:%-4d  Y:%-4d |", x, y);
191         BLF_position(blf_mono_font, dx, dy, 0);
192         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
193         dx += BLF_width(blf_mono_font, str, sizeof(str));
194
195         if (zp) {
196                 BLF_color3ub(blf_mono_font, 255, 255, 255);
197                 BLI_snprintf(str, sizeof(str), " Z:%-.4f |", 0.5f + 0.5f * (((float)*zp) / (float)0x7fffffff));
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 (zpf) {
203                 BLF_color3ub(blf_mono_font, 255, 255, 255);
204                 BLI_snprintf(str, sizeof(str), " Z:%-.3f |", *zpf);
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
210         if (channels == 1 && (cp != NULL || fp != NULL)) {
211                 if (fp != NULL) {
212                         BLI_snprintf(str, sizeof(str), " Val:%-.3f |", fp[0]);
213                 }
214                 else if (cp != NULL) {
215                         BLI_snprintf(str, sizeof(str), " Val:%-.3f |", cp[0] / 255.0f);
216                 }
217                 BLF_color3ub(blf_mono_font, 255, 255, 255);
218                 BLF_position(blf_mono_font, dx, dy, 0);
219                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
220                 dx += BLF_width(blf_mono_font, str, sizeof(str));
221         }
222
223         if (channels >= 3) {
224                 BLF_color3ubv(blf_mono_font, red);
225                 if (fp)
226                         BLI_snprintf(str, sizeof(str), "  R:%-.5f", fp[0]);
227                 else if (cp)
228                         BLI_snprintf(str, sizeof(str), "  R:%-3d", cp[0]);
229                 else
230                         BLI_snprintf(str, sizeof(str), "  R:-");
231                 BLF_position(blf_mono_font, dx, dy, 0);
232                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
233                 dx += BLF_width(blf_mono_font, str, sizeof(str));
234                 
235                 BLF_color3ubv(blf_mono_font, green);
236                 if (fp)
237                         BLI_snprintf(str, sizeof(str), "  G:%-.5f", fp[1]);
238                 else if (cp)
239                         BLI_snprintf(str, sizeof(str), "  G:%-3d", cp[1]);
240                 else
241                         BLI_snprintf(str, sizeof(str), "  G:-");
242                 BLF_position(blf_mono_font, dx, dy, 0);
243                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
244                 dx += BLF_width(blf_mono_font, str, sizeof(str));
245                 
246                 BLF_color3ubv(blf_mono_font, blue);
247                 if (fp)
248                         BLI_snprintf(str, sizeof(str), "  B:%-.5f", fp[2]);
249                 else if (cp)
250                         BLI_snprintf(str, sizeof(str), "  B:%-3d", cp[2]);
251                 else
252                         BLI_snprintf(str, sizeof(str), "  B:-");
253                 BLF_position(blf_mono_font, dx, dy, 0);
254                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
255                 dx += BLF_width(blf_mono_font, str, sizeof(str));
256                 
257                 if (channels == 4) {
258                         BLF_color3ub(blf_mono_font, 255, 255, 255);
259                         if (fp)
260                                 BLI_snprintf(str, sizeof(str), "  A:%-.4f", fp[3]);
261                         else if (cp)
262                                 BLI_snprintf(str, sizeof(str), "  A:%-3d", cp[3]);
263                         else
264                                 BLI_snprintf(str, sizeof(str), "- ");
265                         BLF_position(blf_mono_font, dx, dy, 0);
266                         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
267                         dx += BLF_width(blf_mono_font, str, sizeof(str));
268                 }
269
270                 if (color_manage) {
271                         float rgba[4];
272
273                         copy_v3_v3(rgba, linearcol);
274                         if (channels == 3)
275                                 rgba[3] = 1.0f;
276                         else
277                                 rgba[3] = linearcol[3];
278
279                         if (use_default_view)
280                                 IMB_colormanagement_pixel_to_display_space_v4(rgba, rgba, NULL, &scene->display_settings);
281                         else
282                                 IMB_colormanagement_pixel_to_display_space_v4(rgba, rgba, &scene->view_settings, &scene->display_settings);
283
284                         BLI_snprintf(str, sizeof(str), "  |  CM  R:%-.4f  G:%-.4f  B:%-.4f", rgba[0], rgba[1], rgba[2]);
285                         BLF_position(blf_mono_font, dx, dy, 0);
286                         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
287                         dx += BLF_width(blf_mono_font, str, sizeof(str));
288                 }
289         }
290         
291         /* color rectangle */
292         if (channels == 1) {
293                 if (fp) {
294                         col[0] = col[1] = col[2] = fp[0];
295                 }
296                 else if (cp) {
297                         col[0] = col[1] = col[2] = (float)cp[0] / 255.0f;
298                 }
299                 else {
300                         col[0] = col[1] = col[2] = 0.0f;
301                 }
302                 col[3] = 1.0f;
303         }
304         else if (channels == 3) {
305                 copy_v3_v3(col, linearcol);
306                 col[3] = 1.0f;
307         }
308         else if (channels == 4) {
309                 copy_v4_v4(col, linearcol);
310         }
311         else {
312                 BLI_assert(0);
313                 zero_v4(col);
314         }
315
316         if (color_manage) {
317                 if (use_default_view)
318                         IMB_colormanagement_pixel_to_display_space_v4(finalcol, col, NULL, &scene->display_settings);
319                 else
320                         IMB_colormanagement_pixel_to_display_space_v4(finalcol, col, &scene->view_settings, &scene->display_settings);
321         }
322         else {
323                 copy_v4_v4(finalcol, col);
324         }
325
326         glDisable(GL_BLEND);
327         dx += 0.25f * UI_UNIT_X;
328
329         BLI_rcti_init(&color_rect, dx, dx + (1.5f * UI_UNIT_X), 0.15f * UI_UNIT_Y, 0.85f * UI_UNIT_Y);
330
331         /* BLF uses immediate mode too, so we must reset our vertex format */
332         pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_I32, 2, CONVERT_INT_TO_FLOAT);
333         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
334
335         if (channels == 4) {
336                 rcti color_rect_half;
337                 int color_quater_x, color_quater_y;
338
339                 color_rect_half = color_rect;
340                 color_rect_half.xmax = BLI_rcti_cent_x(&color_rect);
341                 /* what color ??? */
342                 immRecti(pos, color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
343
344                 color_rect_half = color_rect;
345                 color_rect_half.xmin = BLI_rcti_cent_x(&color_rect);
346
347                 color_quater_x = BLI_rcti_cent_x(&color_rect_half);
348                 color_quater_y = BLI_rcti_cent_y(&color_rect_half);
349
350                 immUniformColor3ub(UI_ALPHA_CHECKER_DARK, UI_ALPHA_CHECKER_DARK, UI_ALPHA_CHECKER_DARK);
351                 immRecti(pos, color_rect_half.xmin, color_rect_half.ymin, color_rect_half.xmax, color_rect_half.ymax);
352
353                 immUniformColor3ub(UI_ALPHA_CHECKER_LIGHT, UI_ALPHA_CHECKER_LIGHT, UI_ALPHA_CHECKER_LIGHT);
354                 immRecti(pos, color_quater_x, color_quater_y, color_rect_half.xmax, color_rect_half.ymax);
355                 immRecti(pos, color_rect_half.xmin, color_rect_half.ymin, color_quater_x, color_quater_y);
356
357                 glEnable(GL_BLEND);
358                 immUniformColor4f(UNPACK3(finalcol), fp ? fp[3] : (cp[3] / 255.0f));
359                 immRecti(pos, color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
360                 glDisable(GL_BLEND);
361         }
362         else {
363                 immUniformColor3fv(finalcol);
364                 immRecti(pos, color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
365         }
366         immUnbindProgram();
367
368         /* draw outline */
369         pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_F32, 2, KEEP_FLOAT);
370         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
371         immUniformColor3ub(128, 128, 128);
372         imm_draw_line_box(pos, color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
373         immUnbindProgram();
374
375         dx += 1.75f * UI_UNIT_X;
376
377         BLF_color3ub(blf_mono_font, 255, 255, 255);
378         if (channels == 1) {
379                 if (fp) {
380                         rgb_to_hsv(fp[0], fp[0], fp[0], &hue, &sat, &val);
381                         rgb_to_yuv(fp[0], fp[0], fp[0], &lum, &u, &v);
382                 }
383                 else if (cp) {
384                         rgb_to_hsv((float)cp[0] / 255.0f, (float)cp[0] / 255.0f, (float)cp[0] / 255.0f, &hue, &sat, &val);
385                         rgb_to_yuv((float)cp[0] / 255.0f, (float)cp[0] / 255.0f, (float)cp[0] / 255.0f, &lum, &u, &v);
386                 }
387                 
388                 BLI_snprintf(str, sizeof(str), "V:%-.4f", val);
389                 BLF_position(blf_mono_font, dx, dy, 0);
390                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
391                 dx += BLF_width(blf_mono_font, str, sizeof(str));
392
393                 BLI_snprintf(str, sizeof(str), "   L:%-.4f", lum);
394                 BLF_position(blf_mono_font, dx, dy, 0);
395                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
396         }
397         else if (channels >= 3) {
398                 rgb_to_hsv(finalcol[0], finalcol[1], finalcol[2], &hue, &sat, &val);
399                 rgb_to_yuv(finalcol[0], finalcol[1], finalcol[2], &lum, &u, &v);
400
401                 BLI_snprintf(str, sizeof(str), "H:%-.4f", hue);
402                 BLF_position(blf_mono_font, dx, dy, 0);
403                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
404                 dx += BLF_width(blf_mono_font, str, sizeof(str));
405
406                 BLI_snprintf(str, sizeof(str), "  S:%-.4f", sat);
407                 BLF_position(blf_mono_font, dx, dy, 0);
408                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
409                 dx += BLF_width(blf_mono_font, str, sizeof(str));
410
411                 BLI_snprintf(str, sizeof(str), "  V:%-.4f", val);
412                 BLF_position(blf_mono_font, dx, dy, 0);
413                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
414                 dx += BLF_width(blf_mono_font, str, sizeof(str));
415
416                 BLI_snprintf(str, sizeof(str), "   L:%-.4f", lum);
417                 BLF_position(blf_mono_font, dx, dy, 0);
418                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
419         }
420 }
421
422 /* image drawing */
423 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *rect,
424                                   float zoomx, float zoomy)
425 {
426         float red[4] = {1.0f, 0.0f, 0.0f, 0.0f};
427
428         /* Slowwww */
429         int *recti = MEM_mallocN(rectx * recty * sizeof(int), "temp");
430         for (int a = rectx * recty - 1; a >= 0; a--) {
431                 /* zbuffer values are signed, so we need to shift color range */
432                 recti[a] = rect[a] * 0.5f + 0.5f;
433         }
434
435         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR);
436         GPU_shader_uniform_vector(state.shader, GPU_shader_get_uniform(state.shader, "shuffle"), 4, 1, red);
437
438         immDrawPixelsTex(&state, x1, y1, rectx, recty, GL_RED, GL_INT, GL_NEAREST, recti, zoomx, zoomy, NULL);
439
440         MEM_freeN(recti);
441 }
442
443 static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rectx, int recty,
444                                        float *rect_float, float zoomx, float zoomy)
445 {
446         float bias, scale, *rectf, clipend;
447         int a;
448         float red[4] = {1.0f, 0.0f, 0.0f, 0.0f};
449
450         if (scene->camera && scene->camera->type == OB_CAMERA) {
451                 bias = ((Camera *)scene->camera->data)->clipsta;
452                 clipend = ((Camera *)scene->camera->data)->clipend;
453                 scale = 1.0f / (clipend - bias);
454         }
455         else {
456                 bias = 0.1f;
457                 scale = 0.01f;
458                 clipend = 100.0f;
459         }
460
461         rectf = MEM_mallocN(rectx * recty * sizeof(float), "temp");
462         for (a = rectx * recty - 1; a >= 0; a--) {
463                 if (rect_float[a] > clipend)
464                         rectf[a] = 0.0f;
465                 else if (rect_float[a] < bias)
466                         rectf[a] = 1.0f;
467                 else {
468                         rectf[a] = 1.0f - (rect_float[a] - bias) * scale;
469                         rectf[a] *= rectf[a];
470                 }
471         }
472
473         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR);
474         GPU_shader_uniform_vector(state.shader, GPU_shader_get_uniform(state.shader, "shuffle"), 4, 1, red);
475
476         immDrawPixelsTex(&state, x1, y1, rectx, recty, GL_RED, GL_FLOAT, GL_NEAREST, rectf, zoomx, zoomy, NULL);
477
478         MEM_freeN(rectf);
479 }
480
481 static void draw_image_buffer(const bContext *C, SpaceImage *sima, ARegion *ar, Scene *scene, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
482 {
483         int x, y;
484
485         glaDefine2DArea(&ar->winrct);
486         
487         /* find window pixel coordinates of origin */
488         UI_view2d_view_to_region(&ar->v2d, fx, fy, &x, &y);
489
490         /* this part is generic image display */
491         if (sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels == 1))) {
492                 if (ibuf->zbuf)
493                         sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf, zoomx, zoomy);
494                 else if (ibuf->zbuf_float)
495                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float, zoomx, zoomy);
496                 else if (ibuf->channels == 1)
497                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float, zoomx, zoomy);
498         }
499         else {
500                 int clip_max_x, clip_max_y;
501                 UI_view2d_view_to_region(&ar->v2d,
502                                          ar->v2d.cur.xmax, ar->v2d.cur.ymax,
503                                          &clip_max_x, &clip_max_y);
504
505                 if (sima->flag & SI_USE_ALPHA) {
506                         imm_draw_checker_box(x, y, x + ibuf->x * zoomx, y + ibuf->y * zoomy);
507
508                         glEnable(GL_BLEND);
509                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
510                 }
511
512                 /* If RGBA display with color management */
513                 if ((sima->flag & (SI_SHOW_R | SI_SHOW_G | SI_SHOW_B | SI_SHOW_ALPHA)) == 0) {
514
515                         glaDrawImBuf_glsl_ctx_clipping(C, ibuf, x, y, GL_NEAREST,
516                                                        0, 0, clip_max_x, clip_max_y, zoomx, zoomy);
517                 }
518                 else {
519                         float shuffle[4] = {0.0f, 0.0f, 0.0f, 0.0f};
520                         unsigned char *display_buffer;
521                         void *cache_handle;
522                         ColorManagedViewSettings *view_settings;
523                         ColorManagedDisplaySettings *display_settings;
524
525                         if (sima->flag & SI_SHOW_R)
526                                 shuffle[0] = 1.0f;
527                         else if (sima->flag & SI_SHOW_G)
528                                 shuffle[1] = 1.0f;
529                         else if (sima->flag & SI_SHOW_B)
530                                 shuffle[2] = 1.0f;
531                         else if (sima->flag & SI_SHOW_ALPHA)
532                                 shuffle[3] = 1.0f;
533
534                         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR);
535                         GPU_shader_uniform_vector(state.shader, GPU_shader_get_uniform(state.shader, "shuffle"), 4, 1, shuffle);
536
537                         IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
538                         display_buffer = IMB_display_buffer_acquire(ibuf, view_settings, display_settings, &cache_handle);
539
540                         if (display_buffer) {
541                                 immDrawPixelsTex_clipping(&state, x, y, ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, display_buffer,
542                                                           0, 0, clip_max_x, clip_max_y, zoomx, zoomy, NULL);
543                         }
544
545                         IMB_display_buffer_release(cache_handle);
546                 }
547
548                 if (sima->flag & SI_USE_ALPHA)
549                         glDisable(GL_BLEND);
550         }
551 }
552
553 static unsigned int *get_part_from_buffer(unsigned int *buffer, int width, short startx, short starty, short endx, short endy)
554 {
555         unsigned int *rt, *rp, *rectmain;
556         short y, heigth, len;
557
558         /* the right offset in rectot */
559
560         rt = buffer + (starty * width + startx);
561
562         len = (endx - startx);
563         heigth = (endy - starty);
564
565         rp = rectmain = MEM_mallocN(heigth * len * sizeof(int), "rect");
566         
567         for (y = 0; y < heigth; y++) {
568                 memcpy(rp, rt, len * 4);
569                 rt += width;
570                 rp += len;
571         }
572         return rectmain;
573 }
574
575 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
576 {
577         unsigned char *display_buffer;
578         unsigned int *rect;
579         int dx, dy, sx, sy, x, y;
580         void *cache_handle;
581         float shuffle[4] = {0.0f, 0.0f, 0.0f, 0.0f};
582
583         /* verify valid values, just leave this a while */
584         if (ima->xrep < 1) return;
585         if (ima->yrep < 1) return;
586
587         if (ima->flag & IMA_VIEW_AS_RENDER)
588                 display_buffer = IMB_display_buffer_acquire(ibuf, &scene->view_settings, &scene->display_settings, &cache_handle);
589         else
590                 display_buffer = IMB_display_buffer_acquire(ibuf, NULL, &scene->display_settings, &cache_handle);
591
592         if (!display_buffer)
593                 return;
594
595         if (sima->curtile >= ima->xrep * ima->yrep)
596                 sima->curtile = ima->xrep * ima->yrep - 1;
597         
598         /* retrieve part of image buffer */
599         dx = max_ii(ibuf->x / ima->xrep, 1);
600         dy = max_ii(ibuf->y / ima->yrep, 1);
601         sx = (sima->curtile % ima->xrep) * dx;
602         sy = (sima->curtile / ima->xrep) * dy;
603         rect = get_part_from_buffer((unsigned int *)display_buffer, ibuf->x, sx, sy, sx + dx, sy + dy);
604         
605         /* draw repeated */
606         if ((sima->flag & (SI_SHOW_R | SI_SHOW_G | SI_SHOW_B | SI_SHOW_ALPHA)) != 0) {
607                 if (sima->flag & SI_SHOW_R)
608                         shuffle[0] = 1.0f;
609                 else if (sima->flag & SI_SHOW_G)
610                         shuffle[1] = 1.0f;
611                 else if (sima->flag & SI_SHOW_B)
612                         shuffle[2] = 1.0f;
613                 else if (sima->flag & SI_SHOW_ALPHA)
614                         shuffle[3] = 1.0f;
615         }
616
617         for (sy = 0; sy + dy <= ibuf->y; sy += dy) {
618                 for (sx = 0; sx + dx <= ibuf->x; sx += dx) {
619                         UI_view2d_view_to_region(&ar->v2d, fx + (float)sx / (float)ibuf->x, fy + (float)sy / (float)ibuf->y, &x, &y);
620
621                         if ((sima->flag & (SI_SHOW_R | SI_SHOW_G | SI_SHOW_B | SI_SHOW_ALPHA)) == 0) {
622                                 IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_COLOR);
623                                 immDrawPixelsTex(&state, x, y, dx, dy, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, rect, zoomx, zoomy, NULL);
624                         }
625                         else {
626                                 IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR);
627                                 GPU_shader_uniform_vector(state.shader, GPU_shader_get_uniform(state.shader, "shuffle"), 4, 1, shuffle);
628
629                                 immDrawPixelsTex(&state, x, y, dx, dy, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, rect, zoomx, zoomy, NULL);
630                         }
631                 }
632         }
633
634         IMB_display_buffer_release(cache_handle);
635
636         MEM_freeN(rect);
637 }
638
639 static void draw_image_buffer_repeated(const bContext *C, SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
640 {
641         const double time_current = PIL_check_seconds_timer();
642
643         const int xmax = ceil(ar->v2d.cur.xmax);
644         const int ymax = ceil(ar->v2d.cur.ymax);
645         const int xmin = floor(ar->v2d.cur.xmin);
646         const int ymin = floor(ar->v2d.cur.ymin);
647
648         int x;
649
650         for (x = xmin; x < xmax; x++) {
651                 int y;
652                 for (y = ymin; y < ymax; y++) {
653                         if (ima && (ima->tpageflag & IMA_TILES))
654                                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
655                         else
656                                 draw_image_buffer(C, sima, ar, scene, ibuf, x, y, zoomx, zoomy);
657
658                         /* only draw until running out of time */
659                         if ((PIL_check_seconds_timer() - time_current) > 0.25)
660                                 return;
661                 }
662         }
663 }
664
665 /* draw uv edit */
666
667 /* draw grease pencil */
668 void draw_image_grease_pencil(bContext *C, bool onlyv2d)
669 {
670         /* draw in View2D space? */
671         if (onlyv2d) {
672                 /* draw grease-pencil ('image' strokes) */
673                 ED_gpencil_draw_2dimage(C);
674         }
675         else {
676                 /* assume that UI_view2d_restore(C) has been called... */
677                 //SpaceImage *sima = (SpaceImage *)CTX_wm_space_data(C);
678                 
679                 /* draw grease-pencil ('screen' strokes) */
680                 ED_gpencil_draw_view2d(C, 0);
681         }
682 }
683
684 void draw_image_sample_line(SpaceImage *sima)
685 {
686         if (sima->sample_line_hist.flag & HISTO_FLAG_SAMPLELINE) {
687                 Histogram *hist = &sima->sample_line_hist;
688
689                 unsigned int pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_F32, 2, KEEP_FLOAT);
690                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
691
692                 immBegin(PRIM_LINES, 2);
693                 immUniformColor3ub(0, 0, 0);
694                 immVertex2fv(pos, hist->co[0]);
695                 immVertex2fv(pos, hist->co[1]);
696                 immEnd();
697
698                 setlinestyle(1);
699                 immBegin(PRIM_LINES, 2);
700                 immUniformColor3ub(255, 255, 255);
701                 immVertex2fv(pos, hist->co[0]);
702                 immVertex2fv(pos, hist->co[1]);
703                 immEnd();
704                 setlinestyle(0);
705
706                 immUnbindProgram();
707         }
708 }
709
710 static void draw_image_paint_helpers(const bContext *C, ARegion *ar, Scene *scene, float zoomx, float zoomy)
711 {
712         Brush *brush;
713         int x, y;
714         ImBuf *ibuf;
715
716         brush = BKE_paint_brush(&scene->toolsettings->imapaint.paint);
717
718         if (brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE) && brush->clone.image) {
719                 ibuf = BKE_image_acquire_ibuf(brush->clone.image, NULL, NULL);
720
721                 if (ibuf) {
722                         void *cache_handle = NULL;
723                         float col[4] = {1.0f, 1.0f, 1.0f, brush->clone.alpha};
724                         UI_view2d_view_to_region(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
725
726                         unsigned char *display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
727
728                         if (!display_buffer) {
729                                 BKE_image_release_ibuf(brush->clone.image, ibuf, NULL);
730                                 IMB_display_buffer_release(cache_handle);
731                                 return;
732                         }
733
734                         glEnable(GL_BLEND);
735                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
736
737                         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_COLOR);
738                         immDrawPixelsTex(&state, x, y, ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, display_buffer, zoomx, zoomy, col);
739
740                         glDisable(GL_BLEND);
741
742                         BKE_image_release_ibuf(brush->clone.image, ibuf, NULL);
743                         IMB_display_buffer_release(cache_handle);
744                 }
745         }
746 }
747
748 /* draw main image region */
749
750 void draw_image_main(const bContext *C, ARegion *ar)
751 {
752         SpaceImage *sima = CTX_wm_space_image(C);
753         Scene *scene = CTX_data_scene(C);
754         Image *ima;
755         ImBuf *ibuf;
756         float zoomx, zoomy;
757         bool show_viewer, show_render, show_paint, show_stereo3d, show_multilayer;
758         void *lock;
759
760         /* XXX can we do this in refresh? */
761 #if 0
762         what_image(sima);
763         
764         if (sima->image) {
765                 ED_image_get_aspect(sima->image, &xuser_asp, &yuser_asp);
766                 
767                 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
768                 if (sima->image->type == IMA_TYPE_COMPOSITE) {
769                         ImageUser *iuser = ntree_get_active_iuser(scene->nodetree);
770                         if (iuser) {
771                                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
772                                 sima->iuser = *iuser;
773                         }
774                 }
775                 /* and we check for spare */
776                 ibuf = ED_space_image_buffer(sima);
777         }
778 #endif
779
780         /* retrieve the image and information about it */
781         ima = ED_space_image(sima);
782         ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
783
784         show_viewer = (ima && ima->source == IMA_SRC_VIEWER) != 0;
785         show_render = (show_viewer && ima->type == IMA_TYPE_R_RESULT) != 0;
786         show_paint = (ima && (sima->mode == SI_MODE_PAINT) && (show_viewer == false) && (show_render == false));
787         show_stereo3d = (ima && BKE_image_is_stereo(ima) && (sima->iuser.flag & IMA_SHOW_STEREO));
788         show_multilayer = ima && BKE_image_is_multilayer(ima);
789
790         if (show_viewer) {
791                 /* use locked draw for drawing viewer image buffer since the compositor
792                  * is running in separated thread and compositor could free this buffers.
793                  * other images are not modifying in such a way so they does not require
794                  * lock (sergey)
795                  */
796                 BLI_lock_thread(LOCK_DRAW_IMAGE);
797         }
798
799         if (show_stereo3d) {
800                 if (show_multilayer)
801                         /* update multiindex and pass for the current eye */
802                         BKE_image_multilayer_index(ima->rr, &sima->iuser);
803                 else
804                         BKE_image_multiview_index(ima, &sima->iuser);
805         }
806
807         ibuf = ED_space_image_acquire_buffer(sima, &lock);
808
809         /* draw the image or grid */
810         if (ibuf == NULL) {
811                 ED_region_grid_draw(ar, zoomx, zoomy);
812         }
813         else {
814
815                 if (sima->flag & SI_DRAW_TILE)
816                         draw_image_buffer_repeated(C, sima, ar, scene, ima, ibuf, zoomx, zoomy);
817                 else if (ima && (ima->tpageflag & IMA_TILES))
818                         draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
819                 else
820                         draw_image_buffer(C, sima, ar, scene, ibuf, 0.0f, 0.0f, zoomx, zoomy);
821                 
822                 if (sima->flag & SI_DRAW_METADATA) {
823                         int x, y;
824                         rctf frame;
825
826                         BLI_rctf_init(&frame, 0.0f, ibuf->x, 0.0f, ibuf->y);
827                         UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &x, &y);
828
829                         ED_region_image_metadata_draw(x, y, ibuf, &frame, zoomx, zoomy);
830                 }
831         }
832
833         ED_space_image_release_buffer(sima, ibuf, lock);
834
835         /* paint helpers */
836         if (show_paint)
837                 draw_image_paint_helpers(C, ar, scene, zoomx, zoomy);
838
839         if (show_viewer) {
840                 BLI_unlock_thread(LOCK_DRAW_IMAGE);
841         }
842
843         /* render info */
844         if (ima && show_render)
845                 draw_render_info(C, sima->iuser.scene, ima, ar, zoomx, zoomy);
846 }
847
848 bool ED_space_image_show_cache(SpaceImage *sima)
849 {
850         Image *image = ED_space_image(sima);
851         Mask *mask = NULL;
852         if (sima->mode == SI_MODE_MASK) {
853                 mask = ED_space_image_get_mask(sima);
854         }
855         if (image == NULL && mask == NULL) {
856                 return false;
857         }
858         if (mask == NULL) {
859                 return ELEM(image->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE);
860         }
861         return true;
862 }
863
864 void draw_image_cache(const bContext *C, ARegion *ar)
865 {
866         SpaceImage *sima = CTX_wm_space_image(C);
867         Scene *scene = CTX_data_scene(C);
868         Image *image = ED_space_image(sima);
869         float x, cfra = CFRA, sfra = SFRA, efra = EFRA, framelen = ar->winx / (efra - sfra + 1);
870         Mask *mask = NULL;
871
872         if (!ED_space_image_show_cache(sima)) {
873                 return;
874         }
875
876         if (sima->mode == SI_MODE_MASK) {
877                 mask = ED_space_image_get_mask(sima);
878         }
879
880         glEnable(GL_BLEND);
881         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
882
883         /* Draw cache background. */
884         ED_region_cache_draw_background(ar);
885
886         /* Draw cached segments. */
887         if (image != NULL && image->cache != NULL && ELEM(image->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
888                 int num_segments = 0;
889                 int *points = NULL;
890
891                 IMB_moviecache_get_cache_segments(image->cache, IMB_PROXY_NONE, 0, &num_segments, &points);
892                 ED_region_cache_draw_cached_segments(ar, num_segments, points, sfra + sima->iuser.offset, efra + sima->iuser.offset);
893         }
894
895         glDisable(GL_BLEND);
896
897         /* Draw current frame. */
898         x = (cfra - sfra) / (efra - sfra + 1) * ar->winx;
899
900         unsigned int pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_I32, 2, CONVERT_INT_TO_FLOAT);
901         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
902         immUniformThemeColor(TH_CFRAME);
903         immRecti(pos, x, 0, x + ceilf(framelen), 8 * UI_DPI_FAC);
904         immUnbindProgram();
905
906         ED_region_cache_draw_curfra_label(cfra, x, 8.0f * UI_DPI_FAC);
907
908         if (mask != NULL) {
909                 ED_mask_draw_frames(mask, ar, cfra, sfra, efra);
910         }
911 }