Blender V5.3
ed_util_imbuf.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include "MEM_guardedalloc.h"
12
14#include "BLI_rect.hh"
15
16#include "BKE_colortools.hh"
17#include "BKE_context.hh"
18#include "BKE_image.hh"
19#include "BKE_screen.hh"
20
21#include "ED_image.hh"
22#include "ED_screen.hh"
23#include "ED_space_api.hh"
24
25#include "GPU_immediate.hh"
26#include "GPU_state.hh"
27
29#include "IMB_imbuf.hh"
30#include "IMB_imbuf_types.hh"
31
32#include "SEQ_render.hh"
33#include "SEQ_sequencer.hh"
34
35#include "UI_view2d.hh"
36
37#include "WM_api.hh"
38#include "WM_types.hh"
39
40#include "sequencer_intern.hh"
41
42/* Own define. */
43#include "ED_util_imbuf.hh"
44
45namespace blender {
46
47/* -------------------------------------------------------------------- */
50
54 int x, y;
56
59
61 float colf[4];
62 float linearcol[4];
63 int z;
64 float zf;
65
67 const float *colfp;
68 int *zp;
69 float *zfp;
70
71 bool draw;
74};
75
77
78/* -------------------------------------------------------------------- */
81
82static void image_sample_pixel_color_ubyte(const ImBuf *ibuf,
83 const int coord[2],
84 uchar r_col[4],
85 float r_col_linear[4])
86{
87 const uchar *cp = ibuf->byte_data() + 4 * (coord[1] * ibuf->x + coord[0]);
88 copy_v4_v4_uchar(r_col, cp);
89 rgba_uchar_to_float(r_col_linear, r_col);
91 r_col_linear, false, ibuf->byte_buffer.colorspace);
92}
93
94static void image_sample_pixel_color_float(ImBuf *ibuf, const int coord[2], float r_col[4])
95{
96 const float *cp = ibuf->float_data() + (ibuf->channels) * (coord[1] * ibuf->x + coord[0]);
97 copy_v4_v4(r_col, cp);
98}
99
101
102/* -------------------------------------------------------------------- */
105
106static void image_sample_rect_color_ubyte(const ImBuf *ibuf,
107 const rcti *rect,
108 uchar r_col[4],
109 float r_col_linear[4])
110{
111 uint col_accum_ub[4] = {0, 0, 0, 0};
112 zero_v4(r_col_linear);
113 int col_tot = 0;
114 int coord[2];
115 for (coord[0] = rect->xmin; coord[0] <= rect->xmax; coord[0]++) {
116 for (coord[1] = rect->ymin; coord[1] <= rect->ymax; coord[1]++) {
117 float col_temp_fl[4];
118 uchar col_temp_ub[4];
119 image_sample_pixel_color_ubyte(ibuf, coord, col_temp_ub, col_temp_fl);
120 add_v4_v4(r_col_linear, col_temp_fl);
121 col_accum_ub[0] += uint(col_temp_ub[0]);
122 col_accum_ub[1] += uint(col_temp_ub[1]);
123 col_accum_ub[2] += uint(col_temp_ub[2]);
124 col_accum_ub[3] += uint(col_temp_ub[3]);
125 col_tot += 1;
126 }
127 }
128 mul_v4_fl(r_col_linear, 1.0 / float(col_tot));
129
130 r_col[0] = std::min<uchar>(col_accum_ub[0] / col_tot, 255);
131 r_col[1] = std::min<uchar>(col_accum_ub[1] / col_tot, 255);
132 r_col[2] = std::min<uchar>(col_accum_ub[2] / col_tot, 255);
133 r_col[3] = std::min<uchar>(col_accum_ub[3] / col_tot, 255);
134}
135
136static void image_sample_rect_color_float(ImBuf *ibuf, const rcti *rect, float r_col[4])
137{
138 zero_v4(r_col);
139 int col_tot = 0;
140 int coord[2];
141 for (coord[0] = rect->xmin; coord[0] <= rect->xmax; coord[0]++) {
142 for (coord[1] = rect->ymin; coord[1] <= rect->ymax; coord[1]++) {
143 float col_temp_fl[4];
144 image_sample_pixel_color_float(ibuf, coord, col_temp_fl);
145 add_v4_v4(r_col, col_temp_fl);
146 col_tot += 1;
147 }
148 }
149 mul_v4_fl(r_col, 1.0 / float(col_tot));
150}
151
153
154/* -------------------------------------------------------------------- */
157
158static void image_sample_apply(bContext *C, wmOperator *op, const wmEvent *event)
159{
161 ARegion *region = CTX_wm_region(C);
162 Image *image = ED_space_image(sima);
163
164 float uv[2];
165 ui::view2d_region_to_view(&region->v2d, event->mval[0], event->mval[1], &uv[0], &uv[1]);
166 int tile = BKE_image_get_tile_from_pos(sima->image, uv, uv, nullptr);
167
168 void *lock;
169 ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock, tile, true);
170 ImageSampleInfo *info = static_cast<ImageSampleInfo *>(op->customdata);
171 Scene *scene = CTX_data_scene(C);
172 CurveMapping *curve_mapping = scene->view_settings.curve_mapping;
173
174 if (ibuf == nullptr) {
176 info->draw = false;
177 return;
178 }
179
180 const float2 offset = flag_is_set(ibuf->flags, ImBufFlags::HasDisplayWindow) ?
181 float2(ibuf->display_offset) :
182 float2(0.0f);
183 int x = int(uv[0] * ibuf->x), y = int(uv[1] * ibuf->y);
184
185 if (x >= offset[0] && y >= offset[1] && x < (ibuf->x + offset[0]) && y < (ibuf->y + offset[1])) {
186 info->width = ibuf->x;
187 info->height = ibuf->y;
188 info->x = x;
189 info->y = y;
190
191 info->draw = true;
192 info->channels = ibuf->channels;
193
194 info->colp = nullptr;
195 info->colfp = nullptr;
196 info->zp = nullptr;
197 info->zfp = nullptr;
198
199 info->use_default_view = (image->flag & IMA_VIEW_AS_RENDER) ? false : true;
200
201 rcti sample_rect;
202 sample_rect.xmin = max_ii(0, x - offset[0] - info->sample_size / 2);
203 sample_rect.ymin = max_ii(0, y - offset[1] - info->sample_size / 2);
204
205 sample_rect.xmax = min_ii(ibuf->x - 1, x - offset[0] + info->sample_size / 2);
206 sample_rect.ymax = min_ii(ibuf->y - 1, y - offset[1] + info->sample_size / 2);
207
208 if (ibuf->byte_data()) {
209 image_sample_rect_color_ubyte(ibuf, &sample_rect, info->col, info->linearcol);
210 rgba_uchar_to_float(info->colf, info->col);
211
212 info->colp = info->col;
213 info->colfp = info->colf;
214 info->color_manage = true;
215 }
216 if (ibuf->float_data()) {
217 image_sample_rect_color_float(ibuf, &sample_rect, info->colf);
218
219 if (ibuf->channels == 4) {
220 /* pass */
221 }
222 else if (ibuf->channels == 3) {
223 info->colf[3] = 1.0f;
224 }
225 else {
226 info->colf[1] = info->colf[0];
227 info->colf[2] = info->colf[0];
228 info->colf[3] = 1.0f;
229 }
230 info->colfp = info->colf;
231
232 copy_v4_v4(info->linearcol, info->colf);
233
234 info->color_manage = true;
235 }
236
237 if (curve_mapping && ibuf->channels == 4) {
238 /* we reuse this callback for set curves point operators */
239 if (RNA_struct_find_property(op->ptr, "point")) {
240 int point = RNA_enum_get(op->ptr, "point");
241
242 if (point == 1) {
243 BKE_curvemapping_set_black_white(curve_mapping, nullptr, info->linearcol);
244 }
245 else if (point == 0) {
246 BKE_curvemapping_set_black_white(curve_mapping, info->linearcol, nullptr);
247 }
249 }
250 }
251
252/* XXX node curve integration. */
253#if 0
254 {
255 ScrArea *area, *cur = curarea;
256
257 node_curvemap_sample(fp); /* sends global to node editor */
258 for (area = G.curscreen->areabase.first; area; area = area->next) {
259 if (area->spacetype == SPACE_NODE) {
260 areawinset(area->win);
261 scrarea_do_windraw(area);
262 }
263 }
264 node_curvemap_sample(nullptr); /* clears global in node editor */
265 curarea = cur;
266 }
267#endif
268 }
269 else {
270 info->draw = false;
271 }
272
275}
276
277static void sequencer_sample_apply(bContext *C, wmOperator *op, const wmEvent *event)
278{
280 ARegion *region = CTX_wm_region(C);
281 ImBuf *ibuf = ed::vse::sequencer_ibuf_get(C, scene->r.cfra, nullptr);
282 ImageSampleInfo *info = static_cast<ImageSampleInfo *>(op->customdata);
283 float fx, fy;
284
285 if (ibuf == nullptr) {
286 info->draw = false;
287 return;
288 }
289
290 ui::view2d_region_to_view(&region->v2d, event->mval[0], event->mval[1], &fx, &fy);
291
292 fx /= scene->r.xasp / scene->r.yasp;
293
294 fx += float(scene->r.xsch) / 2.0f;
295 fy += float(scene->r.ysch) / 2.0f;
296 fx *= float(ibuf->x) / float(scene->r.xsch);
297 fy *= float(ibuf->y) / float(scene->r.ysch);
298
299 if (fx >= 0.0f && fy >= 0.0f && fx < ibuf->x && fy < ibuf->y) {
300 const float *fp;
301 const uchar *cp;
302 int x = int(fx), y = int(fy);
303
304 info->x = x;
305 info->y = y;
306 info->draw = true;
307 info->channels = ibuf->channels;
308
309 info->colp = nullptr;
310 info->colfp = nullptr;
311
312 if (ibuf->byte_data()) {
313 cp = ibuf->byte_data() + 4 * (y * ibuf->x + x);
314
315 info->col[0] = cp[0];
316 info->col[1] = cp[1];
317 info->col[2] = cp[2];
318 info->col[3] = cp[3];
319 info->colp = info->col;
320
321 info->colf[0] = float(cp[0]) / 255.0f;
322 info->colf[1] = float(cp[1]) / 255.0f;
323 info->colf[2] = float(cp[2]) / 255.0f;
324 info->colf[3] = float(cp[3]) / 255.0f;
325 info->colfp = info->colf;
326
327 copy_v4_v4(info->linearcol, info->colf);
329 info->linearcol, false, ibuf->byte_buffer.colorspace);
330
331 info->color_manage = true;
332 }
333 if (ibuf->float_data()) {
334 fp = (ibuf->float_data() + (ibuf->channels) * (y * ibuf->x + x));
335
336 info->colf[0] = fp[0];
337 info->colf[1] = fp[1];
338 info->colf[2] = fp[2];
339 info->colf[3] = fp[3];
340 info->colfp = info->colf;
341
342 copy_v4_v4(info->linearcol, info->colf);
344 info->linearcol, true, ibuf->float_buffer.colorspace);
345
346 info->color_manage = true;
347 }
348 }
349 else {
350 info->draw = false;
351 }
352
353 IMB_freeImBuf(ibuf);
355}
356
357static void ed_imbuf_sample_apply(bContext *C, wmOperator *op, const wmEvent *event)
358{
359 ScrArea *area = CTX_wm_area(C);
360 if (area == nullptr) {
361 return;
362 }
363
364 switch (area->spacetype) {
365 case SPACE_IMAGE: {
366 image_sample_apply(C, op, event);
367 break;
368 }
369 case SPACE_SEQ: {
370 sequencer_sample_apply(C, op, event);
371 break;
372 }
373 }
374}
375
377
378/* -------------------------------------------------------------------- */
383
384void ED_imbuf_sample_draw(const bContext *C, ARegion *region, void *arg_info)
385{
386 ImageSampleInfo *info = static_cast<ImageSampleInfo *>(arg_info);
387 if (!info->draw) {
388 return;
389 }
390
391 Scene *scene = CTX_data_scene(C);
392 ED_image_draw_info(scene,
393 region,
394 info->color_manage,
395 info->use_default_view,
396 info->channels,
397 info->x,
398 info->y,
399 info->colp,
400 info->colfp,
401 info->linearcol);
402
403 if (info->sample_size > 1) {
404 ScrArea *area = CTX_wm_area(C);
405
406 if (area && area->spacetype == SPACE_IMAGE) {
407
408 const wmWindow *win = CTX_wm_window(C);
409 const wmEvent *event = win->runtime->eventstate;
410
412 GPUVertFormat *format = immVertexFormat();
413 uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
414
415 const float color[3] = {1, 1, 1};
418
419 /* TODO(@ideasman42): lock to pixels. */
420 rctf sample_rect_fl;
421 BLI_rctf_init_pt_radius(&sample_rect_fl,
422 float2{float(event->xy[0] - region->winrct.xmin),
423 float(event->xy[1] - region->winrct.ymin)},
424 float(info->sample_size / 2.0f) * sima->zoom);
425
426 /* Use invert blend mode for highly visible border */
428
429 GPU_line_width(1.0f);
431 pos, sample_rect_fl.xmin, sample_rect_fl.ymin, sample_rect_fl.xmax, sample_rect_fl.ymax);
432
434
436 }
437 }
438}
439
441{
442 ImageSampleInfo *info = static_cast<ImageSampleInfo *>(op->customdata);
443
446 MEM_delete(info);
447}
448
450{
451 ARegion *region = CTX_wm_region(C);
452 ScrArea *area = CTX_wm_area(C);
453 if (area) {
454 switch (area->spacetype) {
455 case SPACE_IMAGE: {
456 SpaceImage *sima = area->spacedata.first_as<SpaceImage>();
457 if (region->regiontype == RGN_TYPE_WINDOW) {
458 if (ED_space_image_show_cache_and_mval_over(sima, region, event->mval)) {
460 }
461 }
462 if (!ED_space_image_has_buffer(sima)) {
463 return OPERATOR_CANCELLED;
464 }
465 break;
466 }
467 case SPACE_SEQ: {
468 /* Sequencer checks could be added. */
469 break;
470 }
471 }
472 }
473
474 ImageSampleInfo *info = MEM_new_zeroed<ImageSampleInfo>("ImageSampleInfo");
475
476 info->art = region->runtime->type;
479 info->sample_size = RNA_int_get(op->ptr, "size");
480 op->customdata = info;
481
482 ed_imbuf_sample_apply(C, op, event);
483
485
486 ED_area_hud_region_set_padding_flag(area, region, true);
487
489}
490
492{
493 switch (event->type) {
494 case LEFTMOUSE:
495 case RIGHTMOUSE: /* XXX hardcoded */
496 if (event->val == KM_RELEASE) {
497 ScrArea *area = CTX_wm_area(C);
498 ARegion *region = CTX_wm_region(C);
499
500 if (SpaceImage *sima = CTX_wm_space_image(C)) {
501 if (!ED_space_image_show_cache(sima)) {
503 }
504 }
506 return OPERATOR_CANCELLED;
507 }
508 break;
509 case MOUSEMOVE:
510 ed_imbuf_sample_apply(C, op, event);
511 break;
512 default: {
513 break;
514 }
515 }
516
518}
519
524
526{
527 ScrArea *area = CTX_wm_area(C);
528 if (area == nullptr) {
529 return false;
530 }
531
532 switch (area->spacetype) {
533 case SPACE_IMAGE: {
534 SpaceImage *sima = area->spacedata.first_as<SpaceImage>();
535 Object *obedit = CTX_data_edit_object(C);
536 if (obedit) {
537 /* Disable when UV editing so it doesn't swallow all click events
538 * (use for setting cursor). */
539 if (ED_space_image_show_uvedit(sima, obedit)) {
540 return false;
541 }
542 }
543 else if (sima->mode != SI_MODE_VIEW) {
544 return false;
545 }
546 return true;
547 }
548 case SPACE_SEQ: {
549 SpaceSeq *sseq = area->spacedata.first_as<SpaceSeq>();
550
551 if (sseq->mainb != SEQ_DRAW_IMG_IMBUF) {
552 return false;
553 }
555 return false;
556 }
557 ARegion *region = CTX_wm_region(C);
558 if (!(region && (region->regiontype == RGN_TYPE_PREVIEW))) {
559 return false;
560 }
561 return true;
562 }
563 }
564
565 return false;
566}
567
569
570} // namespace blender
#define REGION_DRAW_POST_PIXEL
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_WINDOW
Definition WM_types.hh:388
volatile int lock
const ccl_global KernelWorkTile * tile
ImBuf * sequencer_ibuf_get(const bContext *C, int timeline_frame, const char *viewname)
Editing * editing_get(const Scene *scene)
Definition sequencer.cc:304
void view2d_region_to_view(const View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
Definition view2d.cc:1651
BMesh Iterators.
int BKE_image_get_tile_from_pos(Image *ima, const float uv[2], float r_uv[2], float r_ofs[2])
ScrArea * CTX_wm_area(const bContext *C)
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
int RNA_enum_get(PointerRNA *ptr, const char *name)
Scene * CTX_data_sequencer_scene(const bContext *C)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool ED_space_image_show_cache_and_mval_over(const SpaceImage *sima, ARegion *region, const int mval[2])
unsigned int uint
Definition BKE_text.h:55
bool ED_space_image_show_cache(const SpaceImage *sima)
void ED_imbuf_sample_exit(bContext *C, wmOperator *op)
void ED_imbuf_sample_draw(const bContext *C, ARegion *region, void *arg_info)
GPUVertFormat * immVertexFormat()
static void sequencer_sample_apply(bContext *C, wmOperator *op, const wmEvent *event)
ImBuf * ED_space_image_acquire_buffer(SpaceImage *sima, void **r_lock, int tile, const bool ensure_host_buffer)
static void image_sample_pixel_color_float(ImBuf *ibuf, const int coord[2], float r_col[4])
int RNA_int_get(PointerRNA *ptr, const char *name)
wmWindow * CTX_wm_window(const bContext *C)
MINLINE int max_ii(int a, int b)
void ED_image_draw_info(Scene *scene, ARegion *region, bool color_manage, bool use_default_view, int channels, int x, int y, const unsigned char cp[4], const float fp[4], const float linearcol[4])
void ED_space_image_release_buffer(SpaceImage *sima, ImBuf *ibuf, void *lock)
void immUnbindProgram()
void GPU_line_width(float width)
Definition gpu_state.cc:151
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_INVERT
Definition GPU_state.hh:100
Global G
Definition blender.cc:57
MINLINE void zero_v4(float r[4])
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
static void image_sample_pixel_color_ubyte(const ImBuf *ibuf, const int coord[2], uchar r_col[4], float r_col_linear[4])
void BLI_rctf_init_pt_radius(struct rctf *rect, const float xy[2], float size)
Definition rct.cc:472
void * ED_region_draw_cb_activate(ARegionType *art, void(*draw)(const bContext *, ARegion *, void *), void *customdata, int type)
static void image_sample_apply(bContext *C, wmOperator *op, const wmEvent *event)
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< float, 2 > float2
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
unsigned char uchar
Definition BKE_text.h:58
bool ED_region_draw_cb_exit(ARegionType *art, void *handle)
MINLINE void add_v4_v4(float r[4], const float a[4])
void ED_imbuf_sample_cancel(bContext *C, wmOperator *op)
Object * CTX_data_edit_object(const bContext *C)
bool ED_space_image_has_buffer(SpaceImage *sima)
static void ed_imbuf_sample_apply(bContext *C, wmOperator *op, const wmEvent *event)
MINLINE void copy_v4_v4_uchar(unsigned char r[4], const unsigned char a[4])
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void IMB_colormanagement_colorspace_to_scene_linear_v4(float pixel[4], bool predivide, const ColorSpace *colorspace)
void BKE_curvemapping_set_black_white(CurveMapping *cumap, const float black[3], const float white[3])
wmOperatorStatus ED_imbuf_sample_modal(bContext *C, wmOperator *op, const wmEvent *event)
static void image_sample_rect_color_float(ImBuf *ibuf, const rcti *rect, float r_col[4])
SpaceImage * CTX_wm_space_image(const bContext *C)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
static void image_sample_rect_color_ubyte(const ImBuf *ibuf, const rcti *rect, uchar r_col[4], float r_col_linear[4])
void imm_draw_box_wire_2d(uint pos, float x1, float y1, float x2, float y2)
void ED_area_hud_region_set_padding_flag(ScrArea *area, ARegion *changed_region, const bool set_padding=false)
Definition area.cc:851
@ KM_RELEASE
Definition WM_types.hh:325
bool ED_imbuf_sample_poll(bContext *C)
Scene * CTX_data_scene(const bContext *C)
MINLINE void copy_v4_v4(float r[4], const float a[4])
void immUniformColor3fv(const float rgb[3])
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
MINLINE void mul_v4_fl(float r[4], float f)
MINLINE int min_ii(int a, int b)
ARegion * CTX_wm_region(const bContext *C)
Image * ED_space_image(const SpaceImage *sima)
Definition image_edit.cc:43
bool ED_space_image_show_uvedit(const SpaceImage *sima, Object *obedit)
wmOperatorStatus ED_imbuf_sample_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void ED_area_tag_redraw(ScrArea *area)
Definition area.cc:784
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bke::ARegionRuntime * runtime
eRegion_Type regiontype
struct CurveMapping * curve_mapping
const ColorSpace * colorspace
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
const float * float_data() const
ColorManagedViewSettings view_settings
struct RenderData r
struct ScrArea * next
ListBaseT< SpaceLink > spacedata
eSpaceImage_Mode mode
eSpaceSeq_RegionType mainb
struct ARegionType * type
wmEventType type
Definition WM_types.hh:817
bke::WindowRuntime * runtime