Fix Cycles packed images not handling channel packed alpha correctly

This commit is contained in:
Brecht Van Lommel 2019-05-26 12:16:58 +02:00
parent f18373a9ab
commit 909b0ac16c
Notes: blender-bot 2023-02-14 06:00:46 +01:00
Referenced by issue #72677, Alpha Channel Darkens the Colors
4 changed files with 27 additions and 15 deletions

View File

@ -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) {

View File

@ -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();

View File

@ -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 {

View File

@ -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;