Fix T74586: Image Editor Uses Invalid Display Channels

When using the image editor the display channels attribute can become
invalid when selecting another image/buffer. This patch will check what
display channels are valid and when an invalid channel is selected it
will fall back to the color channel.

To de-duplicate the code it also introduces a
`ED_space_image_get_display_channel_mask` function that will determine
the valid bitflags for the display channel of a given `ImBuf`.
This commit is contained in:
Jeroen Bakker 2020-03-23 13:51:55 +01:00
parent 64982e213f
commit 6a5bd812b5
Notes: blender-bot 2023-02-14 09:48:23 +01:00
Referenced by commit b123aadeee, Fix T85895: Viewing value passes in Image Editor displays the value as alpha
Referenced by issue #85895, Viewing value passes in render view displays the value as alpha
Referenced by issue #74586, Switching between passes in the image editor can make the channel selector invalid.
4 changed files with 66 additions and 28 deletions

View File

@ -61,6 +61,7 @@ bool ED_space_image_color_sample(struct SpaceImage *sima,
int mval[2],
float r_col[3]);
struct ImBuf *ED_space_image_acquire_buffer(struct SpaceImage *sima, void **r_lock, int tile);
int ED_space_image_get_display_channel_mask(struct ImBuf *ibuf);
void ED_space_image_release_buffer(struct SpaceImage *sima, struct ImBuf *ibuf, void *lock);
bool ED_space_image_has_buffer(struct SpaceImage *sima);

View File

@ -576,12 +576,13 @@ static void draw_image_buffer(const bContext *C,
float zoomy)
{
int x, y;
int sima_flag = sima->flag & ED_space_image_get_display_channel_mask(ibuf);
/* find window pixel coordinates of origin */
UI_view2d_view_to_region(&region->v2d, fx, fy, &x, &y);
/* this part is generic image display */
if (sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels == 1))) {
if (sima_flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels == 1))) {
if (ibuf->zbuf) {
sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf, zoomx, zoomy);
}
@ -597,7 +598,7 @@ static void draw_image_buffer(const bContext *C,
UI_view2d_view_to_region(
&region->v2d, region->v2d.cur.xmax, region->v2d.cur.ymax, &clip_max_x, &clip_max_y);
if (sima->flag & SI_USE_ALPHA) {
if (sima_flag & SI_USE_ALPHA) {
imm_draw_box_checker_2d(x, y, x + ibuf->x * zoomx, y + ibuf->y * zoomy);
GPU_blend(true);
@ -606,7 +607,7 @@ static void draw_image_buffer(const bContext *C,
}
/* If RGBA display with color management */
if ((sima->flag & (SI_SHOW_R | SI_SHOW_G | SI_SHOW_B | SI_SHOW_ALPHA)) == 0) {
if ((sima_flag & (SI_SHOW_R | SI_SHOW_G | SI_SHOW_B | SI_SHOW_ALPHA)) == 0) {
ED_draw_imbuf_ctx_clipping(
C, ibuf, x, y, GL_NEAREST, 0, 0, clip_max_x, clip_max_y, zoomx, zoomy);
@ -618,16 +619,16 @@ static void draw_image_buffer(const bContext *C,
ColorManagedViewSettings *view_settings;
ColorManagedDisplaySettings *display_settings;
if (sima->flag & SI_SHOW_R) {
if (sima_flag & SI_SHOW_R) {
shuffle[0] = 1.0f;
}
else if (sima->flag & SI_SHOW_G) {
else if (sima_flag & SI_SHOW_G) {
shuffle[1] = 1.0f;
}
else if (sima->flag & SI_SHOW_B) {
else if (sima_flag & SI_SHOW_B) {
shuffle[2] = 1.0f;
}
else if (sima->flag & SI_SHOW_ALPHA) {
else if (sima_flag & SI_SHOW_ALPHA) {
shuffle[3] = 1.0f;
}
@ -661,7 +662,7 @@ static void draw_image_buffer(const bContext *C,
IMB_display_buffer_release(cache_handle);
}
if (sima->flag & SI_USE_ALPHA) {
if (sima_flag & SI_USE_ALPHA) {
GPU_blend(false);
}
}

View File

@ -175,6 +175,30 @@ void ED_space_image_release_buffer(SpaceImage *sima, ImBuf *ibuf, void *lock)
}
}
/* Get the SpaceImage flag that is valid for the given ibuf. */
int ED_space_image_get_display_channel_mask(ImBuf *ibuf)
{
int result = (SI_USE_ALPHA | SI_SHOW_ALPHA | SI_SHOW_ZBUF | SI_SHOW_R | SI_SHOW_G | SI_SHOW_B);
if (!ibuf) {
return result;
}
const bool color = ibuf->channels >= 3;
const bool alpha = ibuf->channels == 4;
const bool zbuf = ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels == 1);
if (!alpha) {
result &= ~(SI_USE_ALPHA | SI_SHOW_ALPHA);
}
if (!zbuf) {
result &= ~(SI_SHOW_ZBUF);
}
if (!color) {
result &= ~(SI_SHOW_R | SI_SHOW_G | SI_SHOW_B);
}
return result;
}
bool ED_space_image_has_buffer(SpaceImage *sima)
{
ImBuf *ibuf;

View File

@ -1491,35 +1491,31 @@ static const EnumPropertyItem *rna_SpaceImageEditor_display_channels_itemf(
EnumPropertyItem *item = NULL;
ImBuf *ibuf;
void *lock;
int zbuf, alpha, totitem = 0;
int totitem = 0;
ibuf = ED_space_image_acquire_buffer(sima, &lock, 0);
alpha = ibuf && (ibuf->channels == 4);
zbuf = ibuf && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels == 1));
int mask = ED_space_image_get_display_channel_mask(ibuf);
ED_space_image_release_buffer(sima, ibuf, lock);
if (alpha && zbuf) {
return display_channels_items;
}
if (alpha) {
if (mask & SI_USE_ALPHA) {
RNA_enum_items_add_value(&item, &totitem, display_channels_items, SI_USE_ALPHA);
RNA_enum_items_add_value(&item, &totitem, display_channels_items, 0);
}
RNA_enum_items_add_value(&item, &totitem, display_channels_items, 0);
if (mask & SI_SHOW_ALPHA) {
RNA_enum_items_add_value(&item, &totitem, display_channels_items, SI_SHOW_ALPHA);
}
else if (zbuf) {
RNA_enum_items_add_value(&item, &totitem, display_channels_items, 0);
if (mask & SI_SHOW_ZBUF) {
RNA_enum_items_add_value(&item, &totitem, display_channels_items, SI_SHOW_ZBUF);
}
else {
RNA_enum_items_add_value(&item, &totitem, display_channels_items, 0);
if (mask & SI_SHOW_R) {
RNA_enum_items_add_value(&item, &totitem, display_channels_items, SI_SHOW_R);
}
if (mask & SI_SHOW_G) {
RNA_enum_items_add_value(&item, &totitem, display_channels_items, SI_SHOW_G);
}
if (mask & SI_SHOW_B) {
RNA_enum_items_add_value(&item, &totitem, display_channels_items, SI_SHOW_B);
}
RNA_enum_items_add_value(&item, &totitem, display_channels_items, SI_SHOW_R);
RNA_enum_items_add_value(&item, &totitem, display_channels_items, SI_SHOW_G);
RNA_enum_items_add_value(&item, &totitem, display_channels_items, SI_SHOW_B);
RNA_enum_item_end(&item, &totitem);
*r_free = true;
@ -1527,6 +1523,19 @@ static const EnumPropertyItem *rna_SpaceImageEditor_display_channels_itemf(
return item;
}
static int rna_SpaceImageEditor_display_channels_get(PointerRNA *ptr)
{
SpaceImage *sima = (SpaceImage *)ptr->data;
ImBuf *ibuf;
void *lock;
ibuf = ED_space_image_acquire_buffer(sima, &lock, 0);
int mask = ED_space_image_get_display_channel_mask(ibuf);
ED_space_image_release_buffer(sima, ibuf, lock);
return sima->flag & mask;
}
static void rna_SpaceImageEditor_zoom_get(PointerRNA *ptr, float *values)
{
SpaceImage *sima = (SpaceImage *)ptr->data;
@ -4519,7 +4528,10 @@ static void rna_def_space_image(BlenderRNA *brna)
prop = RNA_def_property(srna, "display_channels", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_bitflag_sdna(prop, NULL, "flag");
RNA_def_property_enum_items(prop, display_channels_items);
RNA_def_property_enum_funcs(prop, NULL, NULL, "rna_SpaceImageEditor_display_channels_itemf");
RNA_def_property_enum_funcs(prop,
"rna_SpaceImageEditor_display_channels_get",
NULL,
"rna_SpaceImageEditor_display_channels_itemf");
RNA_def_property_ui_text(prop, "Display Channels", "Channels of the image to draw");
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_IMAGE, NULL);