[Bf-blender-cvs] [6a5bd812b56] master: Fix T74586: Image Editor Uses Invalid Display Channels

Jeroen Bakker noreply at git.blender.org
Mon Mar 23 13:57:32 CET 2020


Commit: 6a5bd812b569d5fe1f09bd5610ce9d0c119f1a21
Author: Jeroen Bakker
Date:   Mon Mar 23 13:51:55 2020 +0100
Branches: master
https://developer.blender.org/rB6a5bd812b569d5fe1f09bd5610ce9d0c119f1a21

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`.

===================================================================

M	source/blender/editors/include/ED_image.h
M	source/blender/editors/space_image/image_draw.c
M	source/blender/editors/space_image/image_edit.c
M	source/blender/makesrna/intern/rna_space.c

===================================================================

diff --git a/source/blender/editors/include/ED_image.h b/source/blender/editors/include/ED_image.h
index 0e820a1c2d6..1c260bb826a 100644
--- a/source/blender/editors/include/ED_image.h
+++ b/source/blender/editors/include/ED_image.h
@@ -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);
 
diff --git a/source/blender/editors/space_image/image_draw.c b/source/blender/editors/space_image/image_draw.c
index a58abb2fbd5..85f7f744abc 100644
--- a/source/blender/editors/space_image/image_draw.c
+++ b/source/blender/editors/space_image/image_draw.c
@@ -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);
     }
   }
diff --git a/source/blender/editors/space_image/image_edit.c b/source/blender/editors/space_image/image_edit.c
index 99190fa2d39..8da67effc94 100644
--- a/source/blender/editors/space_image/image_edit.c
+++ b/source/blender/editors/space_image/image_edit.c
@@ -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;
diff --git a/source/blender/makesrna/intern/rna_space.c b/source/blender/makesrna/intern/rna_space.c
index 1b74cb8aece..9826d921dce 100644
--- a/source/blender/makesrna/intern/rna_space.c
+++ b/source/blender/makesrna/intern/rna_space.c
@@ -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);



More information about the Bf-blender-cvs mailing list