1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup spimage
22  */
23 
24 #include <math.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "MEM_guardedalloc.h"
29 
30 #include "DNA_brush_types.h"
31 #include "DNA_camera_types.h"
32 #include "DNA_mask_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_space_types.h"
37 
38 #include "PIL_time.h"
39 
40 #include "BLI_listbase.h"
41 #include "BLI_math.h"
42 #include "BLI_rect.h"
43 #include "BLI_string.h"
44 #include "BLI_threads.h"
45 #include "BLI_utildefines.h"
46 
47 #include "IMB_colormanagement.h"
48 #include "IMB_imbuf.h"
49 #include "IMB_imbuf_types.h"
50 #include "IMB_moviecache.h"
51 
52 #include "BKE_context.h"
53 #include "BKE_image.h"
54 #include "BKE_paint.h"
55 
56 #include "BIF_glutil.h"
57 
58 #include "GPU_framebuffer.h"
59 #include "GPU_immediate.h"
60 #include "GPU_immediate_util.h"
61 #include "GPU_matrix.h"
62 #include "GPU_state.h"
63 
64 #include "BLF_api.h"
65 
66 #include "ED_gpencil.h"
67 #include "ED_image.h"
68 #include "ED_mask.h"
69 #include "ED_render.h"
70 #include "ED_screen.h"
71 
72 #include "UI_interface.h"
73 #include "UI_resources.h"
74 #include "UI_view2d.h"
75 
76 #include "RE_engine.h"
77 #include "RE_pipeline.h"
78 
79 #include "image_intern.h"
80 
draw_render_info(const bContext * C,Scene * scene,Image * ima,ARegion * region,float zoomx,float zoomy)81 static void draw_render_info(
82     const bContext *C, Scene *scene, Image *ima, ARegion *region, float zoomx, float zoomy)
83 {
84   Render *re = RE_GetSceneRender(scene);
85   RenderData *rd = RE_engine_get_render_data(re);
86   Scene *stats_scene = ED_render_job_get_scene(C);
87   if (stats_scene == NULL) {
88     stats_scene = CTX_data_scene(C);
89   }
90 
91   RenderResult *rr = BKE_image_acquire_renderresult(stats_scene, ima);
92 
93   if (rr && rr->text) {
94     float fill_color[4] = {0.0f, 0.0f, 0.0f, 0.25f};
95     ED_region_info_draw(region, rr->text, fill_color, true);
96   }
97 
98   BKE_image_release_renderresult(stats_scene, ima);
99 
100   if (re) {
101     int total_tiles;
102     bool need_free_tiles;
103     rcti *tiles = RE_engine_get_current_tiles(re, &total_tiles, &need_free_tiles);
104 
105     if (total_tiles) {
106       /* find window pixel coordinates of origin */
107       int x, y;
108       UI_view2d_view_to_region(&region->v2d, 0.0f, 0.0f, &x, &y);
109 
110       GPU_matrix_push();
111       GPU_matrix_translate_2f(x, y);
112       GPU_matrix_scale_2f(zoomx, zoomy);
113 
114       if (rd->mode & R_BORDER) {
115         /* TODO: round or floor instead of casting to int */
116         GPU_matrix_translate_2f((int)(-rd->border.xmin * rd->xsch * rd->size * 0.01f),
117                                 (int)(-rd->border.ymin * rd->ysch * rd->size * 0.01f));
118       }
119 
120       uint pos = GPU_vertformat_attr_add(
121           immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
122       immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
123       immUniformThemeColor(TH_FACE_SELECT);
124 
125       GPU_line_width(1.0f);
126 
127       rcti *tile = tiles;
128       for (int i = 0; i < total_tiles; i++, tile++) {
129         immDrawBorderCorners(pos, tile, zoomx, zoomy);
130       }
131 
132       immUnbindProgram();
133 
134       if (need_free_tiles) {
135         MEM_freeN(tiles);
136       }
137 
138       GPU_matrix_pop();
139     }
140   }
141 }
142 
143 /* used by node view too */
ED_image_draw_info(Scene * scene,ARegion * region,bool color_manage,bool use_default_view,int channels,int x,int y,const uchar cp[4],const float fp[4],const float linearcol[4],const int * zp,const float * zpf)144 void ED_image_draw_info(Scene *scene,
145                         ARegion *region,
146                         bool color_manage,
147                         bool use_default_view,
148                         int channels,
149                         int x,
150                         int y,
151                         const uchar cp[4],
152                         const float fp[4],
153                         const float linearcol[4],
154                         const int *zp,
155                         const float *zpf)
156 {
157   rcti color_rect;
158   char str[256];
159   int dx = 6;
160   /* local coordinate visible rect inside region, to accommodate overlapping ui */
161   const rcti *rect = ED_region_visible_rect(region);
162   const int ymin = rect->ymin;
163   const int dy = ymin + 0.3f * UI_UNIT_Y;
164 
165   /* text colors */
166   /* XXX colored text not allowed in Blender UI */
167 #if 0
168   uchar red[3] = {255, 50, 50};
169   uchar green[3] = {0, 255, 0};
170   uchar blue[3] = {100, 100, 255};
171 #else
172   const uchar red[3] = {255, 255, 255};
173   const uchar green[3] = {255, 255, 255};
174   const uchar blue[3] = {255, 255, 255};
175 #endif
176   float hue = 0, sat = 0, val = 0, lum = 0, u = 0, v = 0;
177   float col[4], finalcol[4];
178 
179   GPU_blend(GPU_BLEND_ALPHA);
180 
181   uint pos = GPU_vertformat_attr_add(
182       immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
183   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
184 
185   /* noisy, high contrast make impossible to read if lower alpha is used. */
186   immUniformColor4ub(0, 0, 0, 190);
187   immRecti(pos, 0, ymin, BLI_rcti_size_x(&region->winrct) + 1, ymin + UI_UNIT_Y);
188 
189   immUnbindProgram();
190 
191   GPU_blend(GPU_BLEND_NONE);
192 
193   BLF_size(blf_mono_font, 11 * U.pixelsize, U.dpi);
194 
195   BLF_color3ub(blf_mono_font, 255, 255, 255);
196   SNPRINTF(str, "X:%-4d  Y:%-4d |", x, y);
197   BLF_position(blf_mono_font, dx, dy, 0);
198   BLF_draw_ascii(blf_mono_font, str, sizeof(str));
199   dx += BLF_width(blf_mono_font, str, sizeof(str));
200 
201   if (zp) {
202     BLF_color3ub(blf_mono_font, 255, 255, 255);
203     SNPRINTF(str, " Z:%-.4f |", 0.5f + 0.5f * (((float)*zp) / (float)0x7fffffff));
204     BLF_position(blf_mono_font, dx, dy, 0);
205     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
206     dx += BLF_width(blf_mono_font, str, sizeof(str));
207   }
208   if (zpf) {
209     BLF_color3ub(blf_mono_font, 255, 255, 255);
210     SNPRINTF(str, " Z:%-.3f |", *zpf);
211     BLF_position(blf_mono_font, dx, dy, 0);
212     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
213     dx += BLF_width(blf_mono_font, str, sizeof(str));
214   }
215 
216   if (channels == 1 && (cp != NULL || fp != NULL)) {
217     if (fp != NULL) {
218       SNPRINTF(str, " Val:%-.3f |", fp[0]);
219     }
220     else if (cp != NULL) {
221       SNPRINTF(str, " Val:%-.3f |", cp[0] / 255.0f);
222     }
223     BLF_color3ub(blf_mono_font, 255, 255, 255);
224     BLF_position(blf_mono_font, dx, dy, 0);
225     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
226     dx += BLF_width(blf_mono_font, str, sizeof(str));
227   }
228 
229   if (channels >= 3) {
230     BLF_color3ubv(blf_mono_font, red);
231     if (fp) {
232       SNPRINTF(str, "  R:%-.5f", fp[0]);
233     }
234     else if (cp) {
235       SNPRINTF(str, "  R:%-3d", cp[0]);
236     }
237     else {
238       STRNCPY(str, "  R:-");
239     }
240     BLF_position(blf_mono_font, dx, dy, 0);
241     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
242     dx += BLF_width(blf_mono_font, str, sizeof(str));
243 
244     BLF_color3ubv(blf_mono_font, green);
245     if (fp) {
246       SNPRINTF(str, "  G:%-.5f", fp[1]);
247     }
248     else if (cp) {
249       SNPRINTF(str, "  G:%-3d", cp[1]);
250     }
251     else {
252       STRNCPY(str, "  G:-");
253     }
254     BLF_position(blf_mono_font, dx, dy, 0);
255     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
256     dx += BLF_width(blf_mono_font, str, sizeof(str));
257 
258     BLF_color3ubv(blf_mono_font, blue);
259     if (fp) {
260       SNPRINTF(str, "  B:%-.5f", fp[2]);
261     }
262     else if (cp) {
263       SNPRINTF(str, "  B:%-3d", cp[2]);
264     }
265     else {
266       STRNCPY(str, "  B:-");
267     }
268     BLF_position(blf_mono_font, dx, dy, 0);
269     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
270     dx += BLF_width(blf_mono_font, str, sizeof(str));
271 
272     if (channels == 4) {
273       BLF_color3ub(blf_mono_font, 255, 255, 255);
274       if (fp) {
275         SNPRINTF(str, "  A:%-.4f", fp[3]);
276       }
277       else if (cp) {
278         SNPRINTF(str, "  A:%-3d", cp[3]);
279       }
280       else {
281         STRNCPY(str, "- ");
282       }
283       BLF_position(blf_mono_font, dx, dy, 0);
284       BLF_draw_ascii(blf_mono_font, str, sizeof(str));
285       dx += BLF_width(blf_mono_font, str, sizeof(str));
286     }
287 
288     if (color_manage) {
289       float rgba[4];
290 
291       copy_v3_v3(rgba, linearcol);
292       if (channels == 3) {
293         rgba[3] = 1.0f;
294       }
295       else {
296         rgba[3] = linearcol[3];
297       }
298 
299       if (use_default_view) {
300         IMB_colormanagement_pixel_to_display_space_v4(rgba, rgba, NULL, &scene->display_settings);
301       }
302       else {
303         IMB_colormanagement_pixel_to_display_space_v4(
304             rgba, rgba, &scene->view_settings, &scene->display_settings);
305       }
306 
307       SNPRINTF(str, "  |  CM  R:%-.4f  G:%-.4f  B:%-.4f", rgba[0], rgba[1], rgba[2]);
308       BLF_position(blf_mono_font, dx, dy, 0);
309       BLF_draw_ascii(blf_mono_font, str, sizeof(str));
310       dx += BLF_width(blf_mono_font, str, sizeof(str));
311     }
312   }
313 
314   /* color rectangle */
315   if (channels == 1) {
316     if (fp) {
317       col[0] = col[1] = col[2] = fp[0];
318     }
319     else if (cp) {
320       col[0] = col[1] = col[2] = (float)cp[0] / 255.0f;
321     }
322     else {
323       col[0] = col[1] = col[2] = 0.0f;
324     }
325     col[3] = 1.0f;
326   }
327   else if (channels == 3) {
328     copy_v3_v3(col, linearcol);
329     col[3] = 1.0f;
330   }
331   else if (channels == 4) {
332     copy_v4_v4(col, linearcol);
333   }
334   else {
335     BLI_assert(0);
336     zero_v4(col);
337   }
338 
339   if (color_manage) {
340     if (use_default_view) {
341       IMB_colormanagement_pixel_to_display_space_v4(finalcol, col, NULL, &scene->display_settings);
342     }
343     else {
344       IMB_colormanagement_pixel_to_display_space_v4(
345           finalcol, col, &scene->view_settings, &scene->display_settings);
346     }
347   }
348   else {
349     copy_v4_v4(finalcol, col);
350   }
351 
352   GPU_blend(GPU_BLEND_NONE);
353   dx += 0.25f * UI_UNIT_X;
354 
355   BLI_rcti_init(&color_rect,
356                 dx,
357                 dx + (1.5f * UI_UNIT_X),
358                 ymin + 0.15f * UI_UNIT_Y,
359                 ymin + 0.85f * UI_UNIT_Y);
360 
361   /* BLF uses immediate mode too, so we must reset our vertex format */
362   pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
363   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
364 
365   if (channels == 4) {
366     rcti color_rect_half;
367     int color_quater_x, color_quater_y;
368 
369     color_rect_half = color_rect;
370     color_rect_half.xmax = BLI_rcti_cent_x(&color_rect);
371     /* what color ??? */
372     immRecti(pos, color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
373 
374     color_rect_half = color_rect;
375     color_rect_half.xmin = BLI_rcti_cent_x(&color_rect);
376 
377     color_quater_x = BLI_rcti_cent_x(&color_rect_half);
378     color_quater_y = BLI_rcti_cent_y(&color_rect_half);
379 
380     immUniformColor3ub(UI_ALPHA_CHECKER_DARK, UI_ALPHA_CHECKER_DARK, UI_ALPHA_CHECKER_DARK);
381     immRecti(pos,
382              color_rect_half.xmin,
383              color_rect_half.ymin,
384              color_rect_half.xmax,
385              color_rect_half.ymax);
386 
387     immUniformColor3ub(UI_ALPHA_CHECKER_LIGHT, UI_ALPHA_CHECKER_LIGHT, UI_ALPHA_CHECKER_LIGHT);
388     immRecti(pos, color_quater_x, color_quater_y, color_rect_half.xmax, color_rect_half.ymax);
389     immRecti(pos, color_rect_half.xmin, color_rect_half.ymin, color_quater_x, color_quater_y);
390 
391     GPU_blend(GPU_BLEND_ALPHA);
392     immUniformColor3fvAlpha(finalcol, fp ? fp[3] : (cp[3] / 255.0f));
393     immRecti(pos, color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
394     GPU_blend(GPU_BLEND_NONE);
395   }
396   else {
397     immUniformColor3fv(finalcol);
398     immRecti(pos, color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
399   }
400   immUnbindProgram();
401 
402   /* draw outline */
403   pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
404   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
405   immUniformColor3ub(128, 128, 128);
406   imm_draw_box_wire_2d(pos, color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
407   immUnbindProgram();
408 
409   dx += 1.75f * UI_UNIT_X;
410 
411   BLF_color3ub(blf_mono_font, 255, 255, 255);
412   if (channels == 1) {
413     if (fp) {
414       rgb_to_hsv(fp[0], fp[0], fp[0], &hue, &sat, &val);
415       rgb_to_yuv(fp[0], fp[0], fp[0], &lum, &u, &v, BLI_YUV_ITU_BT709);
416     }
417     else if (cp) {
418       rgb_to_hsv(
419           (float)cp[0] / 255.0f, (float)cp[0] / 255.0f, (float)cp[0] / 255.0f, &hue, &sat, &val);
420       rgb_to_yuv((float)cp[0] / 255.0f,
421                  (float)cp[0] / 255.0f,
422                  (float)cp[0] / 255.0f,
423                  &lum,
424                  &u,
425                  &v,
426                  BLI_YUV_ITU_BT709);
427     }
428 
429     SNPRINTF(str, "V:%-.4f", val);
430     BLF_position(blf_mono_font, dx, dy, 0);
431     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
432     dx += BLF_width(blf_mono_font, str, sizeof(str));
433 
434     SNPRINTF(str, "   L:%-.4f", lum);
435     BLF_position(blf_mono_font, dx, dy, 0);
436     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
437   }
438   else if (channels >= 3) {
439     rgb_to_hsv(finalcol[0], finalcol[1], finalcol[2], &hue, &sat, &val);
440     rgb_to_yuv(finalcol[0], finalcol[1], finalcol[2], &lum, &u, &v, BLI_YUV_ITU_BT709);
441 
442     SNPRINTF(str, "H:%-.4f", hue);
443     BLF_position(blf_mono_font, dx, dy, 0);
444     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
445     dx += BLF_width(blf_mono_font, str, sizeof(str));
446 
447     SNPRINTF(str, "  S:%-.4f", sat);
448     BLF_position(blf_mono_font, dx, dy, 0);
449     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
450     dx += BLF_width(blf_mono_font, str, sizeof(str));
451 
452     SNPRINTF(str, "  V:%-.4f", val);
453     BLF_position(blf_mono_font, dx, dy, 0);
454     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
455     dx += BLF_width(blf_mono_font, str, sizeof(str));
456 
457     SNPRINTF(str, "   L:%-.4f", lum);
458     BLF_position(blf_mono_font, dx, dy, 0);
459     BLF_draw_ascii(blf_mono_font, str, sizeof(str));
460   }
461 }
462 
463 /* image drawing */
sima_draw_zbuf_pixels(float x1,float y1,int rectx,int recty,const int * rect,float zoomx,float zoomy)464 static void sima_draw_zbuf_pixels(
465     float x1, float y1, int rectx, int recty, const int *rect, float zoomx, float zoomy)
466 {
467   const float red[4] = {1.0f, 0.0f, 0.0f, 0.0f};
468 
469   /* Slowwww */
470   float *rectf = MEM_mallocN(rectx * recty * sizeof(float), "temp");
471   for (int a = rectx * recty - 1; a >= 0; a--) {
472     /* zbuffer values are signed, so we need to shift color range */
473     rectf[a] = rect[a] * 0.5f + 0.5f;
474   }
475 
476   IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR);
477   GPU_shader_uniform_vector(
478       state.shader, GPU_shader_get_uniform(state.shader, "shuffle"), 4, 1, red);
479 
480   immDrawPixelsTex(&state, x1, y1, rectx, recty, GPU_R16F, false, rectf, zoomx, zoomy, NULL);
481 
482   MEM_freeN(rectf);
483 }
484 
sima_draw_zbuffloat_pixels(Scene * scene,float x1,float y1,int rectx,int recty,const float * rect_float,float zoomx,float zoomy)485 static void sima_draw_zbuffloat_pixels(Scene *scene,
486                                        float x1,
487                                        float y1,
488                                        int rectx,
489                                        int recty,
490                                        const float *rect_float,
491                                        float zoomx,
492                                        float zoomy)
493 {
494   float bias, scale, *rectf, clip_end;
495   int a;
496   const float red[4] = {1.0f, 0.0f, 0.0f, 0.0f};
497 
498   if (scene->camera && scene->camera->type == OB_CAMERA) {
499     bias = ((Camera *)scene->camera->data)->clip_start;
500     clip_end = ((Camera *)scene->camera->data)->clip_end;
501     scale = 1.0f / (clip_end - bias);
502   }
503   else {
504     bias = 0.1f;
505     scale = 0.01f;
506     clip_end = 100.0f;
507   }
508 
509   rectf = MEM_mallocN(rectx * recty * sizeof(float), "temp");
510   for (a = rectx * recty - 1; a >= 0; a--) {
511     if (rect_float[a] > clip_end) {
512       rectf[a] = 0.0f;
513     }
514     else if (rect_float[a] < bias) {
515       rectf[a] = 1.0f;
516     }
517     else {
518       rectf[a] = 1.0f - (rect_float[a] - bias) * scale;
519       rectf[a] *= rectf[a];
520     }
521   }
522 
523   IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR);
524   GPU_shader_uniform_vector(
525       state.shader, GPU_shader_get_uniform(state.shader, "shuffle"), 4, 1, red);
526 
527   immDrawPixelsTex(&state, x1, y1, rectx, recty, GPU_R16F, false, rectf, zoomx, zoomy, NULL);
528 
529   MEM_freeN(rectf);
530 }
531 
draw_udim_label(ARegion * region,float fx,float fy,const char * label)532 static void draw_udim_label(ARegion *region, float fx, float fy, const char *label)
533 {
534   if (label == NULL || !label[0]) {
535     return;
536   }
537 
538   /* find window pixel coordinates of origin */
539   int x, y;
540   UI_view2d_view_to_region(&region->v2d, fx, fy, &x, &y);
541 
542   GPU_blend(GPU_BLEND_ALPHA);
543 
544   int textwidth = BLF_width(blf_mono_font, label, strlen(label)) + 10;
545   float stepx = BLI_rcti_size_x(&region->v2d.mask) / BLI_rctf_size_x(&region->v2d.cur);
546   float opacity;
547   if (textwidth < 0.5f * (stepx - 10)) {
548     opacity = 1.0f;
549   }
550   else if (textwidth < (stepx - 10)) {
551     opacity = 2.0f - 2.0f * (textwidth / (stepx - 10));
552   }
553   else {
554     opacity = 0.0f;
555   }
556   BLF_color4ub(blf_mono_font, 220, 220, 220, 150 * opacity);
557   BLF_position(blf_mono_font, (int)(x + 10), (int)(y + 10), 0);
558   BLF_draw_ascii(blf_mono_font, label, strlen(label));
559 
560   GPU_blend(GPU_BLEND_NONE);
561 }
562 
draw_image_buffer(const bContext * C,SpaceImage * sima,ARegion * region,Scene * scene,ImBuf * ibuf,float fx,float fy,float zoomx,float zoomy)563 static void draw_image_buffer(const bContext *C,
564                               SpaceImage *sima,
565                               ARegion *region,
566                               Scene *scene,
567                               ImBuf *ibuf,
568                               float fx,
569                               float fy,
570                               float zoomx,
571                               float zoomy)
572 {
573   /* Image are still drawn in display space. */
574   GPUFrameBuffer *fb = GPU_framebuffer_active_get();
575   GPU_framebuffer_bind_no_srgb(fb);
576 
577   int x, y;
578   int sima_flag = sima->flag & ED_space_image_get_display_channel_mask(ibuf);
579 
580   /* find window pixel coordinates of origin */
581   UI_view2d_view_to_region(&region->v2d, fx, fy, &x, &y);
582 
583   /* this part is generic image display */
584   if (sima_flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels == 1))) {
585     if (ibuf->zbuf) {
586       sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf, zoomx, zoomy);
587     }
588     else if (ibuf->zbuf_float) {
589       sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float, zoomx, zoomy);
590     }
591     else if (ibuf->channels == 1) {
592       sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float, zoomx, zoomy);
593     }
594   }
595   else {
596     int clip_max_x, clip_max_y;
597     UI_view2d_view_to_region(
598         &region->v2d, region->v2d.cur.xmax, region->v2d.cur.ymax, &clip_max_x, &clip_max_y);
599 
600     if (sima_flag & SI_USE_ALPHA) {
601       imm_draw_box_checker_2d(x, y, x + ibuf->x * zoomx, y + ibuf->y * zoomy);
602 
603       GPU_blend(GPU_BLEND_ALPHA);
604     }
605 
606     /* If RGBA display with color management */
607     if ((sima_flag & (SI_SHOW_R | SI_SHOW_G | SI_SHOW_B | SI_SHOW_ALPHA)) == 0) {
608 
609       ED_draw_imbuf_ctx_clipping(C, ibuf, x, y, false, 0, 0, clip_max_x, clip_max_y, zoomx, zoomy);
610     }
611     else {
612       float shuffle[4] = {0.0f, 0.0f, 0.0f, 0.0f};
613       uchar *display_buffer;
614       void *cache_handle;
615       ColorManagedViewSettings *view_settings;
616       ColorManagedDisplaySettings *display_settings;
617 
618       if (sima_flag & SI_SHOW_R) {
619         shuffle[0] = 1.0f;
620       }
621       else if (sima_flag & SI_SHOW_G) {
622         shuffle[1] = 1.0f;
623       }
624       else if (sima_flag & SI_SHOW_B) {
625         shuffle[2] = 1.0f;
626       }
627       else if (sima_flag & SI_SHOW_ALPHA) {
628         shuffle[3] = 1.0f;
629       }
630 
631       IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR);
632       GPU_shader_uniform_vector(
633           state.shader, GPU_shader_get_uniform(state.shader, "shuffle"), 4, 1, shuffle);
634 
635       IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
636       display_buffer = IMB_display_buffer_acquire(
637           ibuf, view_settings, display_settings, &cache_handle);
638 
639       if (display_buffer) {
640         immDrawPixelsTex_clipping(&state,
641                                   x,
642                                   y,
643                                   ibuf->x,
644                                   ibuf->y,
645                                   GPU_RGBA8,
646                                   false,
647                                   display_buffer,
648                                   0,
649                                   0,
650                                   clip_max_x,
651                                   clip_max_y,
652                                   zoomx,
653                                   zoomy,
654                                   NULL);
655       }
656 
657       IMB_display_buffer_release(cache_handle);
658     }
659 
660     if (sima_flag & SI_USE_ALPHA) {
661       GPU_blend(GPU_BLEND_NONE);
662     }
663   }
664 
665   GPU_framebuffer_bind(fb);
666 }
667 
draw_image_buffer_repeated(const bContext * C,SpaceImage * sima,ARegion * region,Scene * scene,ImBuf * ibuf,float zoomx,float zoomy)668 static void draw_image_buffer_repeated(const bContext *C,
669                                        SpaceImage *sima,
670                                        ARegion *region,
671                                        Scene *scene,
672                                        ImBuf *ibuf,
673                                        float zoomx,
674                                        float zoomy)
675 {
676   const double time_current = PIL_check_seconds_timer();
677 
678   const int xmax = ceil(region->v2d.cur.xmax);
679   const int ymax = ceil(region->v2d.cur.ymax);
680   const int xmin = floor(region->v2d.cur.xmin);
681   const int ymin = floor(region->v2d.cur.ymin);
682 
683   for (int x = xmin; x < xmax; x++) {
684     for (int y = ymin; y < ymax; y++) {
685       draw_image_buffer(C, sima, region, scene, ibuf, x, y, zoomx, zoomy);
686 
687       /* only draw until running out of time */
688       if ((PIL_check_seconds_timer() - time_current) > 0.25) {
689         return;
690       }
691     }
692   }
693 }
694 
695 /* draw uv edit */
696 
697 /* draw grease pencil */
draw_image_grease_pencil(bContext * C,bool onlyv2d)698 void draw_image_grease_pencil(bContext *C, bool onlyv2d)
699 {
700   /* draw in View2D space? */
701   if (onlyv2d) {
702     /* draw grease-pencil ('image' strokes) */
703     ED_annotation_draw_2dimage(C);
704   }
705   else {
706     /* assume that UI_view2d_restore(C) has been called... */
707     // SpaceImage *sima = (SpaceImage *)CTX_wm_space_data(C);
708 
709     /* draw grease-pencil ('screen' strokes) */
710     ED_annotation_draw_view2d(C, 0);
711   }
712 }
713 
draw_image_sample_line(SpaceImage * sima)714 void draw_image_sample_line(SpaceImage *sima)
715 {
716   if (sima->sample_line_hist.flag & HISTO_FLAG_SAMPLELINE) {
717     Histogram *hist = &sima->sample_line_hist;
718 
719     GPUVertFormat *format = immVertexFormat();
720     uint shdr_dashed_pos = GPU_vertformat_attr_add(
721         format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
722 
723     immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
724 
725     float viewport_size[4];
726     GPU_viewport_size_get_f(viewport_size);
727     immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
728 
729     immUniform1i("colors_len", 2); /* Advanced dashes. */
730     immUniformArray4fv(
731         "colors", (float *)(float[][4]){{1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}}, 2);
732     immUniform1f("dash_width", 2.0f);
733     immUniform1f("dash_factor", 0.5f);
734 
735     immBegin(GPU_PRIM_LINES, 2);
736     immVertex2fv(shdr_dashed_pos, hist->co[0]);
737     immVertex2fv(shdr_dashed_pos, hist->co[1]);
738     immEnd();
739 
740     immUnbindProgram();
741   }
742 }
743 
draw_udim_tile_grid(uint pos_attr,uint color_attr,ARegion * region,int x,int y,float stepx,float stepy,const float color[3])744 static void draw_udim_tile_grid(uint pos_attr,
745                                 uint color_attr,
746                                 ARegion *region,
747                                 int x,
748                                 int y,
749                                 float stepx,
750                                 float stepy,
751                                 const float color[3])
752 {
753   float x1, y1;
754   UI_view2d_view_to_region_fl(&region->v2d, x, y, &x1, &y1);
755   const int gridpos[5][2] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
756   for (int i = 0; i < 4; i++) {
757     immAttr3fv(color_attr, color);
758     immVertex2f(pos_attr, x1 + gridpos[i][0] * stepx, y1 + gridpos[i][1] * stepy);
759     immAttr3fv(color_attr, color);
760     immVertex2f(pos_attr, x1 + gridpos[i + 1][0] * stepx, y1 + gridpos[i + 1][1] * stepy);
761   }
762 }
763 
draw_udim_tile_grids(ARegion * region,SpaceImage * sima,Image * ima)764 static void draw_udim_tile_grids(ARegion *region, SpaceImage *sima, Image *ima)
765 {
766   int num_tiles;
767   if (ima != NULL) {
768     num_tiles = BLI_listbase_count(&ima->tiles);
769 
770     if (ima->source != IMA_SRC_TILED) {
771       return;
772     }
773   }
774   else {
775     num_tiles = sima->tile_grid_shape[0] * sima->tile_grid_shape[1];
776   }
777 
778   float stepx = BLI_rcti_size_x(&region->v2d.mask) / BLI_rctf_size_x(&region->v2d.cur);
779   float stepy = BLI_rcti_size_y(&region->v2d.mask) / BLI_rctf_size_y(&region->v2d.cur);
780 
781   GPUVertFormat *format = immVertexFormat();
782   uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
783   uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
784 
785   immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
786   immBegin(GPU_PRIM_LINES, 8 * num_tiles);
787 
788   float theme_color[3], selected_color[3];
789   UI_GetThemeColorShade3fv(TH_GRID, 60.0f, theme_color);
790   UI_GetThemeColor3fv(TH_FACE_SELECT, selected_color);
791 
792   if (ima != NULL) {
793     ImageTile *cur_tile = BLI_findlink(&ima->tiles, ima->active_tile_index);
794 
795     LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
796       if (tile != cur_tile) {
797         int x = (tile->tile_number - 1001) % 10;
798         int y = (tile->tile_number - 1001) / 10;
799         draw_udim_tile_grid(pos, color, region, x, y, stepx, stepy, theme_color);
800       }
801     }
802 
803     if (cur_tile != NULL) {
804       int cur_x = (cur_tile->tile_number - 1001) % 10;
805       int cur_y = (cur_tile->tile_number - 1001) / 10;
806       draw_udim_tile_grid(pos, color, region, cur_x, cur_y, stepx, stepy, selected_color);
807     }
808   }
809   else {
810     for (int y = 0; y < sima->tile_grid_shape[1]; y++) {
811       for (int x = 0; x < sima->tile_grid_shape[0]; x++) {
812         draw_udim_tile_grid(pos, color, region, x, y, stepx, stepy, theme_color);
813       }
814     }
815   }
816 
817   immEnd();
818   immUnbindProgram();
819 }
820 
821 /* draw main image region */
822 
draw_image_main(const bContext * C,ARegion * region)823 void draw_image_main(const bContext *C, ARegion *region)
824 {
825   SpaceImage *sima = CTX_wm_space_image(C);
826   Scene *scene = CTX_data_scene(C);
827   Image *ima;
828   ImBuf *ibuf;
829   float zoomx, zoomy;
830   bool show_viewer, show_stereo3d, show_multilayer;
831   void *lock;
832 
833   /* XXX can we do this in refresh? */
834 #if 0
835   what_image(sima);
836 
837   if (sima->image) {
838     ED_image_get_aspect(sima->image, &xuser_asp, &yuser_asp);
839 
840     /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
841     if (sima->image->type == IMA_TYPE_COMPOSITE) {
842       ImageUser *iuser = ntree_get_active_iuser(scene->nodetree);
843       if (iuser) {
844         BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
845         sima->iuser = *iuser;
846       }
847     }
848     /* and we check for spare */
849     ibuf = ED_space_image_buffer(sima);
850   }
851 #endif
852 
853   /* retrieve the image and information about it */
854   ima = ED_space_image(sima);
855   ED_space_image_get_zoom(sima, region, &zoomx, &zoomy);
856 
857   /* Tag image as in active use for garbage collector. */
858   if (ima) {
859     BKE_image_tag_time(ima);
860   }
861 
862   show_viewer = (ima && ima->source == IMA_SRC_VIEWER) != 0;
863   show_stereo3d = (ima && BKE_image_is_stereo(ima) && (sima->iuser.flag & IMA_SHOW_STEREO));
864   show_multilayer = ima && BKE_image_is_multilayer(ima);
865 
866   if (show_viewer) {
867     /* use locked draw for drawing viewer image buffer since the compositor
868      * is running in separated thread and compositor could free this buffers.
869      * other images are not modifying in such a way so they does not require
870      * lock (sergey)
871      */
872     BLI_thread_lock(LOCK_DRAW_IMAGE);
873   }
874 
875   if (show_stereo3d) {
876     if (show_multilayer) {
877       /* update multiindex and pass for the current eye */
878       BKE_image_multilayer_index(ima->rr, &sima->iuser);
879     }
880     else {
881       BKE_image_multiview_index(ima, &sima->iuser);
882     }
883   }
884 
885   ibuf = ED_space_image_acquire_buffer(sima, &lock, 0);
886 
887   int main_w = 0;
888   int main_h = 0;
889 
890   /* draw the image or grid */
891   if (ibuf == NULL) {
892     if (ima != NULL) {
893       LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
894         int x = (tile->tile_number - 1001) % 10;
895         int y = (tile->tile_number - 1001) / 10;
896         ED_region_grid_draw(region, zoomx, zoomy, x, y);
897       }
898     }
899     else {
900       for (int y = 0; y < sima->tile_grid_shape[1]; y++) {
901         for (int x = 0; x < sima->tile_grid_shape[0]; x++) {
902           ED_region_grid_draw(region, zoomx, zoomy, x, y);
903         }
904       }
905     }
906   }
907   else {
908     if (sima->flag & SI_DRAW_TILE) {
909       draw_image_buffer_repeated(C, sima, region, scene, ibuf, zoomx, zoomy);
910     }
911     else {
912       main_w = ibuf->x;
913       main_h = ibuf->y;
914 
915       draw_image_buffer(C, sima, region, scene, ibuf, 0.0f, 0.0f, zoomx, zoomy);
916       if (ima->source == IMA_SRC_TILED) {
917         ImageTile *tile = BKE_image_get_tile(ima, 0);
918         char label[sizeof(tile->label)];
919         BKE_image_get_tile_label(ima, tile, label, sizeof(label));
920         draw_udim_label(region, 0.0f, 0.0f, label);
921       }
922     }
923 
924     if (sima->flag & SI_DRAW_METADATA) {
925       int x, y;
926       rctf frame;
927 
928       BLI_rctf_init(&frame, 0.0f, ibuf->x, 0.0f, ibuf->y);
929       UI_view2d_view_to_region(&region->v2d, 0.0f, 0.0f, &x, &y);
930 
931       ED_region_image_metadata_draw(x, y, ibuf, &frame, zoomx, zoomy);
932     }
933   }
934 
935   ED_space_image_release_buffer(sima, ibuf, lock);
936 
937   if (ima != NULL && ima->source == IMA_SRC_TILED) {
938     LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
939       if (tile->tile_number == 1001) {
940         continue;
941       }
942 
943       ibuf = ED_space_image_acquire_buffer(sima, &lock, tile->tile_number);
944       if (ibuf != NULL) {
945         int x_pos = (tile->tile_number - 1001) % 10;
946         int y_pos = (tile->tile_number - 1001) / 10;
947         char label[sizeof(tile->label)];
948         BKE_image_get_tile_label(ima, tile, label, sizeof(label));
949 
950         float tile_zoomx = (zoomx * main_w) / ibuf->x;
951         float tile_zoomy = (zoomy * main_h) / ibuf->y;
952         draw_image_buffer(C, sima, region, scene, ibuf, x_pos, y_pos, tile_zoomx, tile_zoomy);
953         draw_udim_label(region, x_pos, y_pos, label);
954       }
955       ED_space_image_release_buffer(sima, ibuf, lock);
956     }
957   }
958 
959   draw_udim_tile_grids(region, sima, ima);
960   draw_image_main_helpers(C, region);
961 
962   if (show_viewer) {
963     BLI_thread_unlock(LOCK_DRAW_IMAGE);
964   }
965 }
966 
draw_image_main_helpers(const bContext * C,ARegion * region)967 void draw_image_main_helpers(const bContext *C, ARegion *region)
968 {
969   SpaceImage *sima = CTX_wm_space_image(C);
970   Image *ima = ED_space_image(sima);
971 
972   const bool show_viewer = (ima && ima->source == IMA_SRC_VIEWER) != 0;
973   const bool show_render = (show_viewer && ima->type == IMA_TYPE_R_RESULT) != 0;
974   if (ima && show_render) {
975     float zoomx, zoomy;
976     ED_space_image_get_zoom(sima, region, &zoomx, &zoomy);
977     draw_render_info(C, sima->iuser.scene, ima, region, zoomx, zoomy);
978   }
979 }
980 
ED_space_image_show_cache(SpaceImage * sima)981 bool ED_space_image_show_cache(SpaceImage *sima)
982 {
983   Image *image = ED_space_image(sima);
984   Mask *mask = NULL;
985   if (sima->mode == SI_MODE_MASK) {
986     mask = ED_space_image_get_mask(sima);
987   }
988   if (image == NULL && mask == NULL) {
989     return false;
990   }
991   if (mask == NULL) {
992     return ELEM(image->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE);
993   }
994   return true;
995 }
996 
draw_image_cache(const bContext * C,ARegion * region)997 void draw_image_cache(const bContext *C, ARegion *region)
998 {
999   SpaceImage *sima = CTX_wm_space_image(C);
1000   Scene *scene = CTX_data_scene(C);
1001   Image *image = ED_space_image(sima);
1002   float x, cfra = CFRA, sfra = SFRA, efra = EFRA, framelen = region->winx / (efra - sfra + 1);
1003   Mask *mask = NULL;
1004 
1005   if (!ED_space_image_show_cache(sima)) {
1006     return;
1007   }
1008 
1009   if (sima->mode == SI_MODE_MASK) {
1010     mask = ED_space_image_get_mask(sima);
1011   }
1012 
1013   /* Local coordinate visible rect inside region, to accommodate overlapping ui. */
1014   const rcti *rect_visible = ED_region_visible_rect(region);
1015   const int region_bottom = rect_visible->ymin;
1016 
1017   GPU_blend(GPU_BLEND_ALPHA);
1018 
1019   /* Draw cache background. */
1020   ED_region_cache_draw_background(region);
1021 
1022   /* Draw cached segments. */
1023   if (image != NULL && image->cache != NULL &&
1024       ELEM(image->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1025     int num_segments = 0;
1026     int *points = NULL;
1027 
1028     IMB_moviecache_get_cache_segments(image->cache, IMB_PROXY_NONE, 0, &num_segments, &points);
1029     ED_region_cache_draw_cached_segments(
1030         region, num_segments, points, sfra + sima->iuser.offset, efra + sima->iuser.offset);
1031   }
1032 
1033   GPU_blend(GPU_BLEND_NONE);
1034 
1035   /* Draw current frame. */
1036   x = (cfra - sfra) / (efra - sfra + 1) * region->winx;
1037 
1038   uint pos = GPU_vertformat_attr_add(
1039       immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
1040   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
1041   immUniformThemeColor(TH_CFRAME);
1042   immRecti(pos, x, region_bottom, x + ceilf(framelen), region_bottom + 8 * UI_DPI_FAC);
1043   immUnbindProgram();
1044 
1045   ED_region_cache_draw_curfra_label(cfra, x, region_bottom + 8.0f * UI_DPI_FAC);
1046 
1047   if (mask != NULL) {
1048     ED_mask_draw_frames(mask, region, cfra, sfra, efra);
1049   }
1050 }
1051