Merge branch 'master' into blender2.8
[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_GetSceneRender(scene);
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 = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_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         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
174         glEnable(GL_BLEND);
175
176         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_I32, 2, GWN_FETCH_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 = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_I32, 2, GWN_FETCH_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                 immUniformColor3fvAlpha(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 = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
370         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
371         immUniformColor3ub(128, 128, 128);
372         imm_draw_box_wire_2d(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, BLI_YUV_ITU_BT709);
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, BLI_YUV_ITU_BT709);
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, BLI_YUV_ITU_BT709);
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         /* find window pixel coordinates of origin */
486         UI_view2d_view_to_region(&ar->v2d, fx, fy, &x, &y);
487
488         /* this part is generic image display */
489         if (sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels == 1))) {
490                 if (ibuf->zbuf)
491                         sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf, zoomx, zoomy);
492                 else if (ibuf->zbuf_float)
493                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float, zoomx, zoomy);
494                 else if (ibuf->channels == 1)
495                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float, zoomx, zoomy);
496         }
497         else {
498                 int clip_max_x, clip_max_y;
499                 UI_view2d_view_to_region(&ar->v2d,
500                                          ar->v2d.cur.xmax, ar->v2d.cur.ymax,
501                                          &clip_max_x, &clip_max_y);
502
503                 if (sima->flag & SI_USE_ALPHA) {
504                         imm_draw_box_checker_2d(x, y, x + ibuf->x * zoomx, y + ibuf->y * zoomy);
505
506                         glEnable(GL_BLEND);
507                         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
508                 }
509
510                 /* If RGBA display with color management */
511                 if ((sima->flag & (SI_SHOW_R | SI_SHOW_G | SI_SHOW_B | SI_SHOW_ALPHA)) == 0) {
512
513                         glaDrawImBuf_glsl_ctx_clipping(C, ibuf, x, y, GL_NEAREST,
514                                                        0, 0, clip_max_x, clip_max_y, zoomx, zoomy);
515                 }
516                 else {
517                         float shuffle[4] = {0.0f, 0.0f, 0.0f, 0.0f};
518                         unsigned char *display_buffer;
519                         void *cache_handle;
520                         ColorManagedViewSettings *view_settings;
521                         ColorManagedDisplaySettings *display_settings;
522
523                         if (sima->flag & SI_SHOW_R)
524                                 shuffle[0] = 1.0f;
525                         else if (sima->flag & SI_SHOW_G)
526                                 shuffle[1] = 1.0f;
527                         else if (sima->flag & SI_SHOW_B)
528                                 shuffle[2] = 1.0f;
529                         else if (sima->flag & SI_SHOW_ALPHA)
530                                 shuffle[3] = 1.0f;
531
532                         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR);
533                         GPU_shader_uniform_vector(state.shader, GPU_shader_get_uniform(state.shader, "shuffle"), 4, 1, shuffle);
534
535                         IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
536                         display_buffer = IMB_display_buffer_acquire(ibuf, view_settings, display_settings, &cache_handle);
537
538                         if (display_buffer) {
539                                 immDrawPixelsTex_clipping(&state, x, y, ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, display_buffer,
540                                                           0, 0, clip_max_x, clip_max_y, zoomx, zoomy, NULL);
541                         }
542
543                         IMB_display_buffer_release(cache_handle);
544                 }
545
546                 if (sima->flag & SI_USE_ALPHA)
547                         glDisable(GL_BLEND);
548         }
549 }
550
551 /* draw uv edit */
552
553 /* draw grease pencil */
554 void draw_image_grease_pencil(bContext *C, bool onlyv2d)
555 {
556         /* draw in View2D space? */
557         if (onlyv2d) {
558                 /* draw grease-pencil ('image' strokes) */
559                 ED_gpencil_draw_2dimage(C);
560         }
561         else {
562                 /* assume that UI_view2d_restore(C) has been called... */
563                 //SpaceImage *sima = (SpaceImage *)CTX_wm_space_data(C);
564
565                 /* draw grease-pencil ('screen' strokes) */
566                 ED_gpencil_draw_view2d(C, 0);
567         }
568 }
569
570 void draw_image_sample_line(SpaceImage *sima)
571 {
572         if (sima->sample_line_hist.flag & HISTO_FLAG_SAMPLELINE) {
573                 Histogram *hist = &sima->sample_line_hist;
574
575                 Gwn_VertFormat *format = immVertexFormat();
576                 unsigned int shdr_dashed_pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
577
578                 immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
579
580                 float viewport_size[4];
581                 glGetFloatv(GL_VIEWPORT, viewport_size);
582                 immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
583
584                 immUniform1i("num_colors", 2);  /* Advanced dashes. */
585                 immUniformArray4fv("colors", (float *)(float[][4]){{1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}}, 2);
586                 immUniform1f("dash_width", 2.0f);
587
588                 immBegin(GWN_PRIM_LINES, 2);
589                 immVertex2fv(shdr_dashed_pos, hist->co[0]);
590                 immVertex2fv(shdr_dashed_pos, hist->co[1]);
591                 immEnd();
592
593                 immUnbindProgram();
594         }
595 }
596
597 static void draw_image_paint_helpers(const bContext *C, ARegion *ar, Scene *scene, float zoomx, float zoomy)
598 {
599         Brush *brush;
600         int x, y;
601         ImBuf *ibuf;
602
603         brush = BKE_paint_brush(&scene->toolsettings->imapaint.paint);
604
605         if (brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE) && brush->clone.image) {
606                 ibuf = BKE_image_acquire_ibuf(brush->clone.image, NULL, NULL);
607
608                 if (ibuf) {
609                         void *cache_handle = NULL;
610                         float col[4] = {1.0f, 1.0f, 1.0f, brush->clone.alpha};
611                         UI_view2d_view_to_region(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
612
613                         unsigned char *display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
614
615                         if (!display_buffer) {
616                                 BKE_image_release_ibuf(brush->clone.image, ibuf, NULL);
617                                 IMB_display_buffer_release(cache_handle);
618                                 return;
619                         }
620
621                         glEnable(GL_BLEND);
622                         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
623
624                         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_COLOR);
625                         immDrawPixelsTex(&state, x, y, ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, display_buffer, zoomx, zoomy, col);
626
627                         glDisable(GL_BLEND);
628
629                         BKE_image_release_ibuf(brush->clone.image, ibuf, NULL);
630                         IMB_display_buffer_release(cache_handle);
631                 }
632         }
633 }
634
635 /* draw main image region */
636
637 void draw_image_main(const bContext *C, ARegion *ar)
638 {
639         SpaceImage *sima = CTX_wm_space_image(C);
640         Scene *scene = CTX_data_scene(C);
641         Image *ima;
642         ImBuf *ibuf;
643         float zoomx, zoomy;
644         bool show_viewer, show_render, show_paint, show_stereo3d, show_multilayer;
645         void *lock;
646
647         /* XXX can we do this in refresh? */
648 #if 0
649         what_image(sima);
650
651         if (sima->image) {
652                 ED_image_get_aspect(sima->image, &xuser_asp, &yuser_asp);
653
654                 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
655                 if (sima->image->type == IMA_TYPE_COMPOSITE) {
656                         ImageUser *iuser = ntree_get_active_iuser(scene->nodetree);
657                         if (iuser) {
658                                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
659                                 sima->iuser = *iuser;
660                         }
661                 }
662                 /* and we check for spare */
663                 ibuf = ED_space_image_buffer(sima);
664         }
665 #endif
666
667         /* retrieve the image and information about it */
668         ima = ED_space_image(sima);
669         ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
670
671         show_viewer = (ima && ima->source == IMA_SRC_VIEWER) != 0;
672         show_render = (show_viewer && ima->type == IMA_TYPE_R_RESULT) != 0;
673         show_paint = (ima && (sima->mode == SI_MODE_PAINT) && (show_viewer == false) && (show_render == false));
674         show_stereo3d = (ima && BKE_image_is_stereo(ima) && (sima->iuser.flag & IMA_SHOW_STEREO));
675         show_multilayer = ima && BKE_image_is_multilayer(ima);
676
677         if (show_viewer) {
678                 /* use locked draw for drawing viewer image buffer since the compositor
679                  * is running in separated thread and compositor could free this buffers.
680                  * other images are not modifying in such a way so they does not require
681                  * lock (sergey)
682                  */
683                 BLI_thread_lock(LOCK_DRAW_IMAGE);
684         }
685
686         if (show_stereo3d) {
687                 if (show_multilayer)
688                         /* update multiindex and pass for the current eye */
689                         BKE_image_multilayer_index(ima->rr, &sima->iuser);
690                 else
691                         BKE_image_multiview_index(ima, &sima->iuser);
692         }
693
694         ibuf = ED_space_image_acquire_buffer(sima, &lock);
695
696         /* draw the image or grid */
697         if (ibuf == NULL) {
698                 ED_region_grid_draw(ar, zoomx, zoomy);
699         }
700         else {
701                 draw_image_buffer(C, sima, ar, scene, ibuf, 0.0f, 0.0f, zoomx, zoomy);
702
703                 if (sima->flag & SI_DRAW_METADATA) {
704                         int x, y;
705                         rctf frame;
706
707                         BLI_rctf_init(&frame, 0.0f, ibuf->x, 0.0f, ibuf->y);
708                         UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &x, &y);
709
710                         ED_region_image_metadata_draw(x, y, ibuf, &frame, zoomx, zoomy);
711                 }
712         }
713
714         ED_space_image_release_buffer(sima, ibuf, lock);
715
716         /* paint helpers */
717         if (show_paint)
718                 draw_image_paint_helpers(C, ar, scene, zoomx, zoomy);
719
720         if (show_viewer) {
721                 BLI_thread_unlock(LOCK_DRAW_IMAGE);
722         }
723
724         /* render info */
725         if (ima && show_render)
726                 draw_render_info(C, sima->iuser.scene, ima, ar, zoomx, zoomy);
727 }
728
729 bool ED_space_image_show_cache(SpaceImage *sima)
730 {
731         Image *image = ED_space_image(sima);
732         Mask *mask = NULL;
733         if (sima->mode == SI_MODE_MASK) {
734                 mask = ED_space_image_get_mask(sima);
735         }
736         if (image == NULL && mask == NULL) {
737                 return false;
738         }
739         if (mask == NULL) {
740                 return ELEM(image->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE);
741         }
742         return true;
743 }
744
745 void draw_image_cache(const bContext *C, ARegion *ar)
746 {
747         SpaceImage *sima = CTX_wm_space_image(C);
748         Scene *scene = CTX_data_scene(C);
749         Image *image = ED_space_image(sima);
750         float x, cfra = CFRA, sfra = SFRA, efra = EFRA, framelen = ar->winx / (efra - sfra + 1);
751         Mask *mask = NULL;
752
753         if (!ED_space_image_show_cache(sima)) {
754                 return;
755         }
756
757         if (sima->mode == SI_MODE_MASK) {
758                 mask = ED_space_image_get_mask(sima);
759         }
760
761         glEnable(GL_BLEND);
762         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
763
764         /* Draw cache background. */
765         ED_region_cache_draw_background(ar);
766
767         /* Draw cached segments. */
768         if (image != NULL && image->cache != NULL && ELEM(image->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
769                 int num_segments = 0;
770                 int *points = NULL;
771
772                 IMB_moviecache_get_cache_segments(image->cache, IMB_PROXY_NONE, 0, &num_segments, &points);
773                 ED_region_cache_draw_cached_segments(ar, num_segments, points, sfra + sima->iuser.offset, efra + sima->iuser.offset);
774         }
775
776         glDisable(GL_BLEND);
777
778         /* Draw current frame. */
779         x = (cfra - sfra) / (efra - sfra + 1) * ar->winx;
780
781         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
782         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
783         immUniformThemeColor(TH_CFRAME);
784         immRecti(pos, x, 0, x + ceilf(framelen), 8 * UI_DPI_FAC);
785         immUnbindProgram();
786
787         ED_region_cache_draw_curfra_label(cfra, x, 8.0f * UI_DPI_FAC);
788
789         if (mask != NULL) {
790                 ED_mask_draw_frames(mask, ar, cfra, sfra, efra);
791         }
792 }