[Bf-blender-cvs] [909b0ac16c2] master: Fix Cycles packed images not handling channel packed alpha correctly

Brecht Van Lommel noreply at git.blender.org
Sun May 26 12:28:08 CEST 2019


Commit: 909b0ac16c26786f864a84e14ec7714c3308d8f0
Author: Brecht Van Lommel
Date:   Sun May 26 12:16:58 2019 +0200
Branches: master
https://developer.blender.org/rB909b0ac16c26786f864a84e14ec7714c3308d8f0

Fix Cycles packed images not handling channel packed alpha correctly

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

M	intern/cycles/blender/blender_session.cpp
M	intern/cycles/blender/blender_session.h
M	intern/cycles/render/image.cpp
M	intern/cycles/render/image.h

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

diff --git a/intern/cycles/blender/blender_session.cpp b/intern/cycles/blender/blender_session.cpp
index 11b6a38c195..bb7c750078f 100644
--- a/intern/cycles/blender/blender_session.cpp
+++ b/intern/cycles/blender/blender_session.cpp
@@ -147,9 +147,9 @@ void BlenderSession::create_session()
   scene->image_manager->builtin_image_info_cb = function_bind(
       &BlenderSession::builtin_image_info, this, _1, _2, _3);
   scene->image_manager->builtin_image_pixels_cb = function_bind(
-      &BlenderSession::builtin_image_pixels, this, _1, _2, _3, _4, _5);
+      &BlenderSession::builtin_image_pixels, this, _1, _2, _3, _4, _5, _6);
   scene->image_manager->builtin_image_float_pixels_cb = function_bind(
-      &BlenderSession::builtin_image_float_pixels, this, _1, _2, _3, _4, _5);
+      &BlenderSession::builtin_image_float_pixels, this, _1, _2, _3, _4, _5, _6);
 
   session->scene = scene;
 
@@ -1223,6 +1223,7 @@ bool BlenderSession::builtin_image_pixels(const string &builtin_name,
                                           void *builtin_data,
                                           unsigned char *pixels,
                                           const size_t pixels_size,
+                                          const bool associate_alpha,
                                           const bool free_cache)
 {
   if (!builtin_data) {
@@ -1272,12 +1273,14 @@ bool BlenderSession::builtin_image_pixels(const string &builtin_name,
     b_image.buffers_free();
   }
 
-  /* Premultiply, byte images are always straight for Blender. */
-  unsigned char *cp = pixels;
-  for (size_t i = 0; i < num_pixels; i++, cp += channels) {
-    cp[0] = (cp[0] * cp[3]) >> 8;
-    cp[1] = (cp[1] * cp[3]) >> 8;
-    cp[2] = (cp[2] * cp[3]) >> 8;
+  if (associate_alpha) {
+    /* Premultiply, byte images are always straight for Blender. */
+    unsigned char *cp = pixels;
+    for (size_t i = 0; i < num_pixels; i++, cp += channels) {
+      cp[0] = (cp[0] * cp[3]) >> 8;
+      cp[1] = (cp[1] * cp[3]) >> 8;
+      cp[2] = (cp[2] * cp[3]) >> 8;
+    }
   }
   return true;
 }
@@ -1286,6 +1289,7 @@ bool BlenderSession::builtin_image_float_pixels(const string &builtin_name,
                                                 void *builtin_data,
                                                 float *pixels,
                                                 const size_t pixels_size,
+                                                const bool,
                                                 const bool free_cache)
 {
   if (!builtin_data) {
diff --git a/intern/cycles/blender/blender_session.h b/intern/cycles/blender/blender_session.h
index f0107d4e0b1..b93273e32ae 100644
--- a/intern/cycles/blender/blender_session.h
+++ b/intern/cycles/blender/blender_session.h
@@ -162,11 +162,13 @@ class BlenderSession {
                             void *builtin_data,
                             unsigned char *pixels,
                             const size_t pixels_size,
+                            const bool associate_alpha,
                             const bool free_cache);
   bool builtin_image_float_pixels(const string &builtin_name,
                                   void *builtin_data,
                                   float *pixels,
                                   const size_t pixels_size,
+                                  const bool associate_alpha,
                                   const bool free_cache);
   void builtin_images_load();
 
diff --git a/intern/cycles/render/image.cpp b/intern/cycles/render/image.cpp
index 160e7d9ff1e..03ecc9bce52 100644
--- a/intern/cycles/render/image.cpp
+++ b/intern/cycles/render/image.cpp
@@ -494,6 +494,14 @@ void ImageManager::tag_reload_image(const string &filename,
   }
 }
 
+static bool image_associate_alpha(ImageManager::Image *img)
+{
+  /* For typical RGBA images we let OIIO convert to associated alpha,
+   * but some types we want to leave the RGB channels untouched. */
+  return !(ColorSpaceManager::colorspace_is_data(img->colorspace) ||
+           img->alpha_type == IMAGE_ALPHA_IGNORE || img->alpha_type == IMAGE_ALPHA_CHANNEL_PACKED);
+}
+
 bool ImageManager::file_load_image_generic(Image *img, unique_ptr<ImageInput> *in)
 {
   if (img->filename == "")
@@ -514,13 +522,7 @@ bool ImageManager::file_load_image_generic(Image *img, unique_ptr<ImageInput> *i
     ImageSpec spec = ImageSpec();
     ImageSpec config = ImageSpec();
 
-    /* For typical RGBA images we let OIIO convert to associated alpha,
-     * but some types we want to leave the RGB channels untouched. */
-    const bool associate_alpha = !(ColorSpaceManager::colorspace_is_data(img->colorspace) ||
-                                   img->alpha_type == IMAGE_ALPHA_IGNORE ||
-                                   img->alpha_type == IMAGE_ALPHA_CHANNEL_PACKED);
-
-    if (!associate_alpha) {
+    if (!image_associate_alpha(img)) {
       config.attribute("oiio:UnassociatedAlpha", 1);
     }
 
@@ -630,6 +632,7 @@ bool ImageManager::file_load_image(Image *img,
                                     img->builtin_data,
                                     (float *)&pixels[0],
                                     num_pixels * components,
+                                    image_associate_alpha(img),
                                     img->metadata.builtin_free_cache);
     }
     else if (FileFormat == TypeDesc::UINT8) {
@@ -637,6 +640,7 @@ bool ImageManager::file_load_image(Image *img,
                               img->builtin_data,
                               (uchar *)&pixels[0],
                               num_pixels * components,
+                              image_associate_alpha(img),
                               img->metadata.builtin_free_cache);
     }
     else {
diff --git a/intern/cycles/render/image.h b/intern/cycles/render/image.h
index ed2780f8471..459cd8c056c 100644
--- a/intern/cycles/render/image.h
+++ b/intern/cycles/render/image.h
@@ -132,12 +132,14 @@ class ImageManager {
                 void *data,
                 unsigned char *pixels,
                 const size_t pixels_size,
+                const bool associate_alpha,
                 const bool free_cache)>
       builtin_image_pixels_cb;
   function<bool(const string &filename,
                 void *data,
                 float *pixels,
                 const size_t pixels_size,
+                const bool associate_alpha,
                 const bool free_cache)>
       builtin_image_float_pixels_cb;



More information about the Bf-blender-cvs mailing list