From 2eb94be7503f0931d02a65243ae116d7879faa9c Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 27 Feb 2018 22:16:45 +0100 Subject: Code cleanup: refactor Cycles image metadata retrieval to use a struct. --- intern/cycles/render/image.cpp | 151 ++++++++++++++++++++++------------------- intern/cycles/render/image.h | 32 +++++---- intern/cycles/render/nodes.cpp | 64 ++++++++--------- 3 files changed, 128 insertions(+), 119 deletions(-) (limited to 'intern/cycles/render') diff --git a/intern/cycles/render/image.cpp b/intern/cycles/render/image.cpp index dbe15a67b9e..de46599e1db 100644 --- a/intern/cycles/render/image.cpp +++ b/intern/cycles/render/image.cpp @@ -84,99 +84,109 @@ bool ImageManager::set_animation_frame_update(int frame) return false; } -ImageDataType ImageManager::get_image_metadata(const string& filename, - void *builtin_data, - bool& is_linear, - bool& builtin_free_cache) +bool ImageManager::get_image_metadata(const string& filename, + void *builtin_data, + ImageMetaData& metadata) { - bool is_float = false, is_half = false; - is_linear = false; - builtin_free_cache = false; - int channels = 4; + memset(&metadata, 0, sizeof(metadata)); if(builtin_data) { if(builtin_image_info_cb) { - int width, height, depth; - builtin_image_info_cb(filename, builtin_data, is_float, width, height, depth, channels, builtin_free_cache); + builtin_image_info_cb(filename, builtin_data, metadata); + } + else { + return false; } - if(is_float) { - is_linear = true; - return (channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT; + if(metadata.is_float) { + metadata.is_linear = true; + metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT; } else { - return (channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE; + metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE; } + + return true; } /* Perform preliminary checks, with meaningful logging. */ if(!path_exists(filename)) { VLOG(1) << "File '" << filename << "' does not exist."; - return IMAGE_DATA_TYPE_BYTE4; + return false; } if(path_is_directory(filename)) { VLOG(1) << "File '" << filename << "' is a directory, can't use as image."; - return IMAGE_DATA_TYPE_BYTE4; + return false; } ImageInput *in = ImageInput::create(filename); - if(in) { - ImageSpec spec; - - if(in->open(filename, spec)) { - /* check the main format, and channel formats; - * if any take up more than one byte, we'll need a float texture slot */ - if(spec.format.basesize() > 1) { - is_float = true; - is_linear = true; - } + if(!in) { + return false; + } - for(size_t channel = 0; channel < spec.channelformats.size(); channel++) { - if(spec.channelformats[channel].basesize() > 1) { - is_float = true; - is_linear = true; - } - } + ImageSpec spec; + if(!in->open(filename, spec)) { + delete in; + return false; + } - /* check if it's half float */ - if(spec.format == TypeDesc::HALF) - is_half = true; + metadata.width = spec.width; + metadata.height = spec.height; + metadata.depth = spec.depth; - channels = spec.nchannels; + /* check the main format, and channel formats; + * if any take up more than one byte, we'll need a float texture slot */ + if(spec.format.basesize() > 1) { + metadata.is_float = true; + metadata.is_linear = true; + } - /* basic color space detection, not great but better than nothing - * before we do OpenColorIO integration */ - if(is_float) { - string colorspace = spec.get_string_attribute("oiio:ColorSpace"); + for(size_t channel = 0; channel < spec.channelformats.size(); channel++) { + if(spec.channelformats[channel].basesize() > 1) { + metadata.is_float = true; + metadata.is_linear = true; + } + } - is_linear = !(colorspace == "sRGB" || - colorspace == "GammaCorrected" || - (colorspace == "" && - (strcmp(in->format_name(), "png") == 0 || - strcmp(in->format_name(), "tiff") == 0 || - strcmp(in->format_name(), "dpx") == 0 || - strcmp(in->format_name(), "jpeg2000") == 0))); - } - else { - is_linear = false; - } + /* check if it's half float */ + if(spec.format == TypeDesc::HALF) + metadata.is_half = true; - in->close(); - } + /* basic color space detection, not great but better than nothing + * before we do OpenColorIO integration */ + if(metadata.is_float) { + string colorspace = spec.get_string_attribute("oiio:ColorSpace"); - delete in; + metadata.is_linear = !(colorspace == "sRGB" || + colorspace == "GammaCorrected" || + (colorspace == "" && + (strcmp(in->format_name(), "png") == 0 || + strcmp(in->format_name(), "tiff") == 0 || + strcmp(in->format_name(), "dpx") == 0 || + strcmp(in->format_name(), "jpeg2000") == 0))); } + else { + metadata.is_linear = false; + } + + /* set type and channels */ + metadata.channels = spec.nchannels; - if(is_half) { - return (channels > 1) ? IMAGE_DATA_TYPE_HALF4 : IMAGE_DATA_TYPE_HALF; + if(metadata.is_half) { + metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_HALF4 : IMAGE_DATA_TYPE_HALF; } - else if(is_float) { - return (channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT; + else if(metadata.is_float) { + metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT; } else { - return (channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE; + metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE; } + + in->close(); + delete in; + + return true; } int ImageManager::max_flattened_slot(ImageDataType type) @@ -237,23 +247,19 @@ int ImageManager::add_image(const string& filename, void *builtin_data, bool animated, float frame, - bool& is_float, - bool& is_linear, InterpolationType interpolation, ExtensionType extension, - bool use_alpha) + bool use_alpha, + ImageMetaData& metadata) { Image *img; size_t slot; - bool builtin_free_cache; - ImageDataType type = get_image_metadata(filename, builtin_data, is_linear, builtin_free_cache); + get_image_metadata(filename, builtin_data, metadata); + ImageDataType type = metadata.type; thread_scoped_lock device_lock(device_mutex); - /* Check whether it's a float texture. */ - is_float = (type == IMAGE_DATA_TYPE_FLOAT || type == IMAGE_DATA_TYPE_FLOAT4); - /* No half textures on OpenCL, use full float instead. */ if(!has_half_images) { if(type == IMAGE_DATA_TYPE_HALF4) { @@ -313,7 +319,7 @@ int ImageManager::add_image(const string& filename, img = new Image(); img->filename = filename; img->builtin_data = builtin_data; - img->builtin_free_cache = builtin_free_cache; + img->builtin_free_cache = metadata.builtin_free_cache; img->need_load = true; img->animated = animated; img->frame = frame; @@ -444,8 +450,13 @@ bool ImageManager::file_load_image_generic(Image *img, if(!builtin_image_info_cb || !builtin_image_pixels_cb) return false; - bool is_float, free_cache; - builtin_image_info_cb(img->filename, img->builtin_data, is_float, width, height, depth, components, free_cache); + ImageMetaData metadata; + builtin_image_info_cb(img->filename, img->builtin_data, metadata); + + width = metadata.width; + height = metadata.height; + depth = metadata.depth; + components = metadata.channels; } /* we only handle certain number of components */ diff --git a/intern/cycles/render/image.h b/intern/cycles/render/image.h index 6fca3ca20d3..989416e089a 100644 --- a/intern/cycles/render/image.h +++ b/intern/cycles/render/image.h @@ -31,6 +31,19 @@ class Device; class Progress; class Scene; +class ImageMetaData { +public: + /* Must be set by image file or builtin callback. */ + bool is_float, is_half; + int channels; + size_t width, height, depth; + bool builtin_free_cache; + + /* Automatically set. */ + ImageDataType type; + bool is_linear; +}; + class ImageManager { public: explicit ImageManager(const DeviceInfo& info); @@ -40,11 +53,10 @@ public: void *builtin_data, bool animated, float frame, - bool& is_float, - bool& is_linear, InterpolationType interpolation, ExtensionType extension, - bool use_alpha); + bool use_alpha, + ImageMetaData& metadata); void remove_image(int flat_slot); void remove_image(const string& filename, void *builtin_data, @@ -56,10 +68,9 @@ public: InterpolationType interpolation, ExtensionType extension, bool use_alpha); - ImageDataType get_image_metadata(const string& filename, - void *builtin_data, - bool& is_linear, - bool& builtin_free_cache); + bool get_image_metadata(const string& filename, + void *builtin_data, + ImageMetaData& metadata); void device_update(Device *device, Scene *scene, @@ -82,12 +93,7 @@ public: */ function builtin_image_info_cb; + ImageMetaData& metadata)> builtin_image_info_cb; functionadd_image(filename.string(), builtin_data, animated, 0, - is_float_bool, - is_linear, interpolation, extension, - use_alpha); - is_float = (int)is_float_bool; + use_alpha, + metadata); + is_float = metadata.is_float; + is_linear = metadata.is_linear; } if(slot != -1) { @@ -363,26 +363,22 @@ void ImageTextureNode::compile(OSLCompiler& compiler) image_manager = compiler.image_manager; if(is_float == -1) { + ImageMetaData metadata; if(builtin_data == NULL) { - ImageDataType type; - bool builtin_free_cache; - type = image_manager->get_image_metadata(filename.string(), NULL, is_linear, builtin_free_cache); - if(type == IMAGE_DATA_TYPE_FLOAT || type == IMAGE_DATA_TYPE_FLOAT4) - is_float = 1; + image_manager->get_image_metadata(filename.string(), NULL, metadata); } else { - bool is_float_bool; slot = image_manager->add_image(filename.string(), builtin_data, animated, 0, - is_float_bool, - is_linear, interpolation, extension, - use_alpha); - is_float = (int)is_float_bool; + use_alpha, + metadata); } + is_float = metadata.is_float; + is_linear = metadata.is_linear; } if(slot == -1) { @@ -501,17 +497,17 @@ void EnvironmentTextureNode::compile(SVMCompiler& compiler) image_manager = compiler.image_manager; if(slot == -1) { - bool is_float_bool; + ImageMetaData metadata; slot = image_manager->add_image(filename.string(), builtin_data, animated, 0, - is_float_bool, - is_linear, interpolation, EXTENSION_REPEAT, - use_alpha); - is_float = (int)is_float_bool; + use_alpha, + metadata); + is_float = metadata.is_float; + is_linear = metadata.is_linear; } if(slot != -1) { @@ -553,26 +549,22 @@ void EnvironmentTextureNode::compile(OSLCompiler& compiler) */ image_manager = compiler.image_manager; if(is_float == -1) { + ImageMetaData metadata; if(builtin_data == NULL) { - ImageDataType type; - bool builtin_free_cache; - type = image_manager->get_image_metadata(filename.string(), NULL, is_linear, builtin_free_cache); - if(type == IMAGE_DATA_TYPE_FLOAT || type == IMAGE_DATA_TYPE_FLOAT4) - is_float = 1; + image_manager->get_image_metadata(filename.string(), NULL, metadata); } else { - bool is_float_bool; slot = image_manager->add_image(filename.string(), builtin_data, animated, 0, - is_float_bool, - is_linear, interpolation, EXTENSION_REPEAT, - use_alpha); - is_float = (int)is_float_bool; + use_alpha, + metadata); } + is_float = metadata.is_float; + is_linear = metadata.is_linear; } if(slot == -1) { @@ -1421,13 +1413,13 @@ void PointDensityTextureNode::compile(SVMCompiler& compiler) if(use_density || use_color) { if(slot == -1) { - bool is_float, is_linear; + ImageMetaData metadata; slot = image_manager->add_image(filename.string(), builtin_data, false, 0, - is_float, is_linear, interpolation, EXTENSION_CLIP, - true); + true, + metadata); } if(slot != -1) { @@ -1473,13 +1465,13 @@ void PointDensityTextureNode::compile(OSLCompiler& compiler) if(use_density || use_color) { if(slot == -1) { - bool is_float, is_linear; + ImageMetaData metadata; slot = image_manager->add_image(filename.string(), builtin_data, false, 0, - is_float, is_linear, interpolation, EXTENSION_CLIP, - true); + true, + metadata); } if(slot != -1) { -- cgit v1.2.3