From ef107d1a4d75ffc5645505867efec6b39b114d50 Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Thu, 4 Oct 2012 13:39:08 +0000 Subject: Color Management: fallback to stub ocio implementation in cases when ocio configuration file failed to load This solves issues with infinite NULL-checks to prevent crashes in such situations. Currently only happens if there's no configuration file at all, but could be tweaked further to fallback if this file isn't usable by blender. --- intern/opencolorio/CMakeLists.txt | 31 +- intern/opencolorio/SConscript | 10 +- intern/opencolorio/fallback_impl.cc | 384 ++++++++++++++++++ intern/opencolorio/ocio_capi.cc | 286 ++++++++++++++ intern/opencolorio/ocio_capi.cpp | 546 -------------------------- intern/opencolorio/ocio_capi.h | 9 +- intern/opencolorio/ocio_capi_stub.cpp | 390 ------------------ intern/opencolorio/ocio_impl.cc | 541 +++++++++++++++++++++++++ intern/opencolorio/ocio_impl.h | 240 +++++++++++ source/blender/imbuf/intern/colormanagement.c | 56 +-- 10 files changed, 1492 insertions(+), 1001 deletions(-) create mode 100644 intern/opencolorio/fallback_impl.cc create mode 100644 intern/opencolorio/ocio_capi.cc delete mode 100644 intern/opencolorio/ocio_capi.cpp delete mode 100644 intern/opencolorio/ocio_capi_stub.cpp create mode 100644 intern/opencolorio/ocio_impl.cc create mode 100644 intern/opencolorio/ocio_impl.h diff --git a/intern/opencolorio/CMakeLists.txt b/intern/opencolorio/CMakeLists.txt index 9f5d4cd332c..fb74d5e3f4e 100644 --- a/intern/opencolorio/CMakeLists.txt +++ b/intern/opencolorio/CMakeLists.txt @@ -26,41 +26,40 @@ set(INC . ../guardedalloc + ../../source/blender/blenlib ) set(INC_SYS ) +set(SRC + ocio_capi.cc + fallback_impl.cc + + ocio_capi.h + ocio_impl.h +) if(WITH_OPENCOLORIO) + add_definitions( + -DWITH_OCIO + ) list(APPEND INC_SYS ${OPENCOLORIO_INCLUDE_DIRS} ) + list(APPEND SRC + ocio_impl.cc + ) + if(WIN32 AND NOT MINGW) list(APPEND INC_SYS ${BOOST_INCLUDE_DIR} ) endif() - - set(SRC - ocio_capi.cpp - ocio_capi.h - ) -else() - list(APPEND INC - ../../source/blender/blenlib - ) - - set(SRC - ocio_capi_stub.cpp - ocio_capi.h - ) endif() -add_definitions( -) blender_add_lib(bf_intern_opencolorio "${SRC}" "${INC}" "${INC_SYS}") diff --git a/intern/opencolorio/SConscript b/intern/opencolorio/SConscript index fec07662735..229087a568d 100644 --- a/intern/opencolorio/SConscript +++ b/intern/opencolorio/SConscript @@ -2,18 +2,18 @@ Import('env') -sources = env.Glob('*.cpp') +sources = env.Glob('*.cc') incs = '. ../guardedalloc ../../source/blender/blenlib' +defs = [] if env['WITH_BF_OCIO']: - sources.remove('ocio_capi_stub.cpp') - incs += ' ' + env['BF_OCIO_INC'] + defs.append('WITH_OCIO') if env['OURPLATFORM'] in ('win32-vc', 'win64-vc'): incs += ' ' + env['BF_BOOST_INC'] else: - sources.remove('ocio_capi.cpp') + sources.remove('ocio_capi.cc') -env.BlenderLib( 'bf_intern_opencolorio', sources, Split(incs), [], libtype=['extern','player'], priority=[10, 185]) +env.BlenderLib( 'bf_intern_opencolorio', sources, Split(incs), defs, libtype=['extern','player'], priority=[10, 185]) diff --git a/intern/opencolorio/fallback_impl.cc b/intern/opencolorio/fallback_impl.cc new file mode 100644 index 00000000000..4badcc54ebd --- /dev/null +++ b/intern/opencolorio/fallback_impl.cc @@ -0,0 +1,384 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2012 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Brecht van Lommel + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include + +#include "MEM_guardedalloc.h" +#include "BLI_math_color.h" + +namespace OCIO_NAMESPACE {}; + +#include "ocio_impl.h" + +#define CONFIG_DEFAULT ((ConstConfigRcPtr*)1) + +#define PROCESSOR_LINEAR_TO_SRGB ((ConstProcessorRcPtr*)1) +#define PROCESSOR_SRGB_TO_LINEAR ((ConstProcessorRcPtr*)2) +#define PROCESSOR_UNKNOWN ((ConstProcessorRcPtr*)3) + +#define COLORSPACE_LINEAR ((ConstColorSpaceRcPtr*)1) +#define COLORSPACE_SRGB ((ConstColorSpaceRcPtr*)2) + +typedef struct PackedImageDescription { + float *data; + long width; + long height; + long numChannels; + long chanStrideBytes; + long xStrideBytes; + long yStrideBytes; +} PackedImageDescription; + +ConstConfigRcPtr *FallbackImpl::getCurrentConfig(void) +{ + return CONFIG_DEFAULT; +} + +void FallbackImpl::setCurrentConfig(const ConstConfigRcPtr *) +{ +} + +ConstConfigRcPtr *FallbackImpl::configCreateFromEnv(void) +{ + return CONFIG_DEFAULT; +} + +ConstConfigRcPtr *FallbackImpl::configCreateFromFile(const char *) +{ + return CONFIG_DEFAULT; +} + +void FallbackImpl::configRelease(ConstConfigRcPtr *) +{ +} + +int FallbackImpl::configGetNumColorSpaces(ConstConfigRcPtr *) +{ + return 2; +} + +const char *FallbackImpl::configGetColorSpaceNameByIndex(ConstConfigRcPtr *, int index) +{ + if (index == 0) + return "Linear"; + else if (index == 1) + return "sRGB"; + + return NULL; +} + +ConstColorSpaceRcPtr *FallbackImpl::configGetColorSpace(ConstConfigRcPtr *, const char *name) +{ + if (strcmp(name, "scene_linear") == 0) + return COLORSPACE_LINEAR; + else if (strcmp(name, "color_picking") == 0) + return COLORSPACE_SRGB; + else if (strcmp(name, "texture_paint") == 0) + return COLORSPACE_LINEAR; + else if (strcmp(name, "default_byte") == 0) + return COLORSPACE_SRGB; + else if (strcmp(name, "default_float") == 0) + return COLORSPACE_LINEAR; + else if (strcmp(name, "default_sequencer") == 0) + return COLORSPACE_SRGB; + else if (strcmp(name, "Linear") == 0) + return COLORSPACE_LINEAR; + else if (strcmp(name, "sRGB") == 0) + return COLORSPACE_SRGB; + + return NULL; +} + +int FallbackImpl::configGetIndexForColorSpace(ConstConfigRcPtr *config, const char *name) +{ + ConstColorSpaceRcPtr *cs = configGetColorSpace(config, name); + + if (cs == COLORSPACE_LINEAR) + return 0; + else if (cs == COLORSPACE_SRGB) + return 1; + + return -1; +} + +const char *FallbackImpl::configGetDefaultDisplay(ConstConfigRcPtr *) +{ + return "sRGB"; +} + +int FallbackImpl::configGetNumDisplays(ConstConfigRcPtr* config) +{ + return 1; +} + +const char *FallbackImpl::configGetDisplay(ConstConfigRcPtr *, int index) +{ + if (index == 0) + return "sRGB"; + + return NULL; +} + +const char *FallbackImpl::configGetDefaultView(ConstConfigRcPtr *, const char *) +{ + return "Default"; +} + +int FallbackImpl::configGetNumViews(ConstConfigRcPtr *, const char *) +{ + return 1; +} + +const char *FallbackImpl::configGetView(ConstConfigRcPtr *, const char *, int index) +{ + if (index == 0) + return "Default"; + + return NULL; +} + +const char *FallbackImpl::configGetDisplayColorSpaceName(ConstConfigRcPtr *, const char *, const char *) +{ + return "sRGB"; +} + +int FallbackImpl::colorSpaceIsInvertible(ConstColorSpaceRcPtr *cs) +{ + return 1; +} + +int FallbackImpl::colorSpaceIsData(ConstColorSpaceRcPtr *cs) +{ + return 0; +} + +void FallbackImpl::colorSpaceRelease(ConstColorSpaceRcPtr *cs) +{ +} + +ConstProcessorRcPtr *FallbackImpl::configGetProcessorWithNames(ConstConfigRcPtr *config, const char *srcName, const char *dstName) +{ + ConstColorSpaceRcPtr *cs_src = configGetColorSpace(config, srcName); + ConstColorSpaceRcPtr *cs_dst = configGetColorSpace(config, dstName); + + if (cs_src == COLORSPACE_LINEAR && cs_dst == COLORSPACE_SRGB) + return PROCESSOR_LINEAR_TO_SRGB; + else if (cs_src == COLORSPACE_SRGB && cs_dst == COLORSPACE_LINEAR) + return PROCESSOR_SRGB_TO_LINEAR; + + return 0; +} + +ConstProcessorRcPtr *FallbackImpl::configGetProcessor(ConstConfigRcPtr *, ConstTransformRcPtr *tfm) +{ + return (ConstProcessorRcPtr*)tfm; +} + +void FallbackImpl::processorApply(ConstProcessorRcPtr *processor, PackedImageDesc *img) +{ + /* OCIO_TODO stride not respected, channels must be 3 or 4 */ + PackedImageDescription *desc = (PackedImageDescription*)img; + int channels = desc->numChannels; + float *pixels = desc->data; + int width = desc->width; + int height = desc->height; + int x, y; + + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + float *pixel = pixels + channels * (y * width + x); + + if (channels == 4) + processorApplyRGBA(processor, pixel); + else if (channels == 3) + processorApplyRGB(processor, pixel); + } + } +} + +void FallbackImpl::processorApply_predivide(ConstProcessorRcPtr *processor, PackedImageDesc *img) +{ + /* OCIO_TODO stride not respected, channels must be 3 or 4 */ + PackedImageDescription *desc = (PackedImageDescription*)img; + int channels = desc->numChannels; + float *pixels = desc->data; + int width = desc->width; + int height = desc->height; + int x, y; + + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + float *pixel = pixels + channels * (y * width + x); + + if (channels == 4) + processorApplyRGBA_predivide(processor, pixel); + else if (channels == 3) + processorApplyRGB(processor, pixel); + } + } +} + +void FallbackImpl::processorApplyRGB(ConstProcessorRcPtr *processor, float *pixel) +{ + if (processor == PROCESSOR_LINEAR_TO_SRGB) + linearrgb_to_srgb_v3_v3(pixel, pixel); + else if (processor == PROCESSOR_SRGB_TO_LINEAR) + srgb_to_linearrgb_v3_v3(pixel, pixel); +} + +void FallbackImpl::processorApplyRGBA(ConstProcessorRcPtr *processor, float *pixel) +{ + if (processor == PROCESSOR_LINEAR_TO_SRGB) + linearrgb_to_srgb_v4(pixel, pixel); + else if (processor == PROCESSOR_SRGB_TO_LINEAR) + srgb_to_linearrgb_v4(pixel, pixel); +} + +void FallbackImpl::processorApplyRGBA_predivide(ConstProcessorRcPtr *processor, float *pixel) +{ + if (pixel[3] == 1.0f || pixel[3] == 0.0f) { + processorApplyRGBA(processor, pixel); + } + else { + float alpha, inv_alpha; + + alpha = pixel[3]; + inv_alpha = 1.0f / alpha; + + pixel[0] *= inv_alpha; + pixel[1] *= inv_alpha; + pixel[2] *= inv_alpha; + + processorApplyRGBA(processor, pixel); + + pixel[0] *= alpha; + pixel[1] *= alpha; + pixel[2] *= alpha; + } +} + +void FallbackImpl::processorRelease(ConstProcessorRcPtr *) +{ +} + +const char *FallbackImpl::colorSpaceGetName(ConstColorSpaceRcPtr *cs) +{ + if (cs == COLORSPACE_LINEAR) + return "Linear"; + else if (cs == COLORSPACE_SRGB) + return "sRGB"; + + return NULL; +} + +const char *FallbackImpl::colorSpaceGetDescription(ConstColorSpaceRcPtr *) +{ + return ""; +} + +const char *FallbackImpl::colorSpaceGetFamily(ConstColorSpaceRcPtr *) +{ + return ""; +} + +DisplayTransformRcPtr *FallbackImpl::createDisplayTransform(void) +{ + return (DisplayTransformRcPtr*)PROCESSOR_LINEAR_TO_SRGB; +} + +void FallbackImpl::displayTransformSetInputColorSpaceName(DisplayTransformRcPtr *, const char *) +{ +} + +void FallbackImpl::displayTransformSetDisplay(DisplayTransformRcPtr *, const char *) +{ +} + +void FallbackImpl::displayTransformSetView(DisplayTransformRcPtr *, const char *) +{ +} + +void FallbackImpl::displayTransformSetDisplayCC(DisplayTransformRcPtr *, ConstTransformRcPtr *) +{ +} + +void FallbackImpl::displayTransformSetLinearCC(DisplayTransformRcPtr *, ConstTransformRcPtr *) +{ +} + +void FallbackImpl::displayTransformRelease(DisplayTransformRcPtr *) +{ +} + +PackedImageDesc *FallbackImpl::createPackedImageDesc(float *data, long width, long height, long numChannels, + long chanStrideBytes, long xStrideBytes, long yStrideBytes) +{ + PackedImageDescription *desc = (PackedImageDescription*)MEM_callocN(sizeof(PackedImageDescription), "PackedImageDescription"); + + desc->data = data; + desc->width = width; + desc->height = height; + desc->numChannels = numChannels; + desc->chanStrideBytes = chanStrideBytes; + desc->xStrideBytes = xStrideBytes; + desc->yStrideBytes = yStrideBytes; + + return (PackedImageDesc*)desc; +} + +void FallbackImpl::packedImageDescRelease(PackedImageDesc* id) +{ + MEM_freeN(id); +} + +ExponentTransformRcPtr *FallbackImpl::createExponentTransform(void) +{ + return (ExponentTransformRcPtr*)PROCESSOR_UNKNOWN; +} + +void FallbackImpl::exponentTransformSetValue(ExponentTransformRcPtr *, const float *) +{ +} + +void FallbackImpl::exponentTransformRelease(ExponentTransformRcPtr *) +{ +} + +MatrixTransformRcPtr *FallbackImpl::createMatrixTransform(void) +{ + return (MatrixTransformRcPtr*)PROCESSOR_UNKNOWN; +} + +void FallbackImpl::matrixTransformSetValue(MatrixTransformRcPtr *, const float *, const float *) +{ +} + +void FallbackImpl::matrixTransformRelease(MatrixTransformRcPtr *) +{ +} + +void FallbackImpl::matrixTransformScale(float * , float * , const float *) +{ +} diff --git a/intern/opencolorio/ocio_capi.cc b/intern/opencolorio/ocio_capi.cc new file mode 100644 index 00000000000..20e7cc23daa --- /dev/null +++ b/intern/opencolorio/ocio_capi.cc @@ -0,0 +1,286 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2012 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Sergey Sharybin + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "MEM_guardedalloc.h" + +namespace OCIO_NAMESPACE {}; + +#include "ocio_impl.h" + +static IOCIOImpl *impl = NULL; + +void OCIO_init(void) +{ +#ifdef WITH_OCIO + impl = new OCIOImpl(); +#else + impl = new FallbackImpl(); +#endif +} + +void OCIO_exit(void) +{ + delete impl; + impl = NULL; +} + +ConstConfigRcPtr *OCIO_getCurrentConfig(void) +{ + return impl->getCurrentConfig(); +} + +ConstConfigRcPtr *OCIO_configCreateFallback(void) +{ + delete impl; + impl = new FallbackImpl(); + + return impl->getCurrentConfig(); +} + +void OCIO_setCurrentConfig(const ConstConfigRcPtr *config) +{ + impl->setCurrentConfig(config); +} + +ConstConfigRcPtr *OCIO_configCreateFromEnv(void) +{ + return impl->configCreateFromEnv(); +} + +ConstConfigRcPtr *OCIO_configCreateFromFile(const char *filename) +{ + return impl->configCreateFromFile(filename); +} + +void OCIO_configRelease(ConstConfigRcPtr *config) +{ + impl->configRelease(config); +} + +int OCIO_configGetNumColorSpaces(ConstConfigRcPtr *config) +{ + return impl->configGetNumColorSpaces(config); +} + +const char *OCIO_configGetColorSpaceNameByIndex(ConstConfigRcPtr *config, int index) +{ + return impl->configGetColorSpaceNameByIndex(config, index); +} + +ConstColorSpaceRcPtr *OCIO_configGetColorSpace(ConstConfigRcPtr *config, const char *name) +{ + return impl->configGetColorSpace(config, name); +} + +int OCIO_configGetIndexForColorSpace(ConstConfigRcPtr *config, const char *name) +{ + return impl->configGetIndexForColorSpace(config, name); +} + +const char *OCIO_configGetDefaultDisplay(ConstConfigRcPtr *config) +{ + return impl->configGetDefaultDisplay(config); +} + +int OCIO_configGetNumDisplays(ConstConfigRcPtr* config) +{ + return impl->configGetNumDisplays(config); +} + +const char *OCIO_configGetDisplay(ConstConfigRcPtr *config, int index) +{ + return impl->configGetDisplay(config, index); +} + +const char *OCIO_configGetDefaultView(ConstConfigRcPtr *config, const char *display) +{ + return impl->configGetDefaultView(config, display); +} + +int OCIO_configGetNumViews(ConstConfigRcPtr *config, const char *display) +{ + return impl->configGetNumViews(config, display); +} + +const char *OCIO_configGetView(ConstConfigRcPtr *config, const char *display, int index) +{ + return impl->configGetView(config, display, index); +} + +const char *OCIO_configGetDisplayColorSpaceName(ConstConfigRcPtr *config, const char *display, const char *view) +{ + return impl->configGetDisplayColorSpaceName(config, display, view); +} + +int OCIO_colorSpaceIsInvertible(ConstColorSpaceRcPtr *cs) +{ + return impl->colorSpaceIsInvertible(cs); +} + +int OCIO_colorSpaceIsData(ConstColorSpaceRcPtr *cs) +{ + return impl->colorSpaceIsData(cs); +} + +void OCIO_colorSpaceRelease(ConstColorSpaceRcPtr *cs) +{ + impl->colorSpaceRelease(cs); +} + +ConstProcessorRcPtr *OCIO_configGetProcessorWithNames(ConstConfigRcPtr *config, const char *srcName, const char *dstName) +{ + return impl->configGetProcessorWithNames(config, srcName, dstName); +} + +ConstProcessorRcPtr *OCIO_configGetProcessor(ConstConfigRcPtr *config, ConstTransformRcPtr *transform) +{ + return impl->configGetProcessor(config, transform); +} + +void OCIO_processorApply(ConstProcessorRcPtr *processor, PackedImageDesc *img) +{ + impl->processorApply(processor, img); +} + +void OCIO_processorApply_predivide(ConstProcessorRcPtr *processor, PackedImageDesc *img) +{ + impl->processorApply_predivide(processor, img); +} + +void OCIO_processorApplyRGB(ConstProcessorRcPtr *processor, float *pixel) +{ + impl->processorApplyRGB(processor, pixel); +} + +void OCIO_processorApplyRGBA(ConstProcessorRcPtr *processor, float *pixel) +{ + impl->processorApplyRGBA(processor, pixel); +} + +void OCIO_processorApplyRGBA_predivide(ConstProcessorRcPtr *processor, float *pixel) +{ + impl->processorApplyRGBA_predivide(processor, pixel); +} + +void OCIO_processorRelease(ConstProcessorRcPtr *p) +{ + impl->processorRelease(p); +} + +const char *OCIO_colorSpaceGetName(ConstColorSpaceRcPtr *cs) +{ + return impl->colorSpaceGetName(cs); +} + +const char *OCIO_colorSpaceGetDescription(ConstColorSpaceRcPtr *cs) +{ + return impl->colorSpaceGetDescription(cs); +} + +const char *OCIO_colorSpaceGetFamily(ConstColorSpaceRcPtr *cs) +{ + return impl->colorSpaceGetFamily(cs); +} + +DisplayTransformRcPtr *OCIO_createDisplayTransform(void) +{ + return impl->createDisplayTransform(); +} + +void OCIO_displayTransformSetInputColorSpaceName(DisplayTransformRcPtr *dt, const char *name) +{ + impl->displayTransformSetInputColorSpaceName(dt, name); +} + +void OCIO_displayTransformSetDisplay(DisplayTransformRcPtr *dt, const char *name) +{ + impl->displayTransformSetDisplay(dt, name); +} + +void OCIO_displayTransformSetView(DisplayTransformRcPtr *dt, const char *name) +{ + impl->displayTransformSetView(dt, name); +} + +void OCIO_displayTransformSetDisplayCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *t) +{ + impl->displayTransformSetDisplayCC(dt, t); +} + +void OCIO_displayTransformSetLinearCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *t) +{ + impl->displayTransformSetLinearCC(dt, t); +} + +void OCIO_displayTransformRelease(DisplayTransformRcPtr *dt) +{ + impl->displayTransformRelease(dt); +} + +PackedImageDesc *OCIO_createPackedImageDesc(float *data, long width, long height, long numChannels, + long chanStrideBytes, long xStrideBytes, long yStrideBytes) +{ + return impl->createPackedImageDesc(data, width, height, numChannels, chanStrideBytes, xStrideBytes, yStrideBytes); +} + +void OCIO_packedImageDescRelease(PackedImageDesc* id) +{ + impl->packedImageDescRelease(id); +} + +ExponentTransformRcPtr *OCIO_createExponentTransform(void) +{ + return impl->createExponentTransform(); +} + +void OCIO_exponentTransformSetValue(ExponentTransformRcPtr *et, const float *exponent) +{ + impl->exponentTransformSetValue(et, exponent); +} + +void OCIO_exponentTransformRelease(ExponentTransformRcPtr *et) +{ + impl->exponentTransformRelease(et); +} + +MatrixTransformRcPtr *OCIO_createMatrixTransform(void) +{ + return impl->createMatrixTransform(); +} + +void OCIO_matrixTransformSetValue(MatrixTransformRcPtr *mt, const float *m44, const float *offset4) +{ + impl->matrixTransformSetValue(mt, m44, offset4); +} + +void OCIO_matrixTransformRelease(MatrixTransformRcPtr *mt) +{ + impl->matrixTransformRelease(mt); +} + +void OCIO_matrixTransformScale(float * m44, float * offset4, const float *scale4f) +{ + impl->matrixTransformScale(m44, offset4, scale4f); +} diff --git a/intern/opencolorio/ocio_capi.cpp b/intern/opencolorio/ocio_capi.cpp deleted file mode 100644 index 152b537ab9b..00000000000 --- a/intern/opencolorio/ocio_capi.cpp +++ /dev/null @@ -1,546 +0,0 @@ -/* - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * The Original Code is Copyright (C) 2012 Blender Foundation. - * All rights reserved. - * - * Contributor(s): Xavier Thomas - * Lukas Toene, - * Sergey Sharybin - * - * ***** END GPL LICENSE BLOCK ***** - */ - -#include -#include - -#include - -#include "MEM_guardedalloc.h" - -#define OCIO_CAPI_IMPLEMENTATION -#include "ocio_capi.h" - -#if !defined(WITH_ASSERT_ABORT) -# define OCIO_abort() -#else -# include -# define OCIO_abort() abort() -#endif - -#if defined(_MSC_VER) -# define __func__ __FUNCTION__ -#endif - -#define MEM_NEW(type) new(MEM_mallocN(sizeof(type), __func__)) type() -#define MEM_DELETE(what, type) if(what) { what->~type(); MEM_freeN(what); } (void)0 - -static void OCIO_reportError(const char *err) -{ - std::cerr << "OpenColorIO Error: " << err << std::endl; - - OCIO_abort(); -} - -static void OCIO_reportException(Exception &exception) -{ - OCIO_reportError(exception.what()); -} - -ConstConfigRcPtr *OCIO_getCurrentConfig(void) -{ - ConstConfigRcPtr *config = MEM_NEW(ConstConfigRcPtr); - - try { - *config = GetCurrentConfig(); - - if(*config) - return config; - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - MEM_DELETE(config, ConstConfigRcPtr); - - return NULL; -} - -ConstConfigRcPtr *OCIO_getDefaultConfig(void) -{ - return NULL; -} - -void OCIO_setCurrentConfig(const ConstConfigRcPtr *config) -{ - try { - SetCurrentConfig(*config); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } -} - -ConstConfigRcPtr *OCIO_configCreateFromEnv(void) -{ - ConstConfigRcPtr *config = MEM_NEW(ConstConfigRcPtr); - - try { - *config = Config::CreateFromEnv(); - - if (*config) - return config; - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - MEM_DELETE(config, ConstConfigRcPtr); - - return NULL; -} - - -ConstConfigRcPtr *OCIO_configCreateFromFile(const char *filename) -{ - ConstConfigRcPtr *config = MEM_NEW(ConstConfigRcPtr); - - try { - *config = Config::CreateFromFile(filename); - - if (*config) - return config; - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - MEM_DELETE(config, ConstConfigRcPtr); - - return NULL; -} - -void OCIO_configRelease(ConstConfigRcPtr *config) -{ - MEM_DELETE(config, ConstConfigRcPtr); -} - -int OCIO_configGetNumColorSpaces(ConstConfigRcPtr *config) -{ - try { - return (*config)->getNumColorSpaces(); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return 0; -} - -const char *OCIO_configGetColorSpaceNameByIndex(ConstConfigRcPtr *config, int index) -{ - try { - return (*config)->getColorSpaceNameByIndex(index); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return NULL; -} - -ConstColorSpaceRcPtr *OCIO_configGetColorSpace(ConstConfigRcPtr *config, const char *name) -{ - ConstColorSpaceRcPtr *cs = MEM_NEW(ConstColorSpaceRcPtr); - - try { - *cs = (*config)->getColorSpace(name); - - if (*cs) - return cs; - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - MEM_DELETE(cs, ConstColorSpaceRcPtr); - - return NULL; -} - -int OCIO_configGetIndexForColorSpace(ConstConfigRcPtr *config, const char *name) -{ - try { - return (*config)->getIndexForColorSpace(name); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return -1; -} - -const char *OCIO_configGetDefaultDisplay(ConstConfigRcPtr *config) -{ - try { - return (*config)->getDefaultDisplay(); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return NULL; -} - -int OCIO_configGetNumDisplays(ConstConfigRcPtr* config) -{ - try { - return (*config)->getNumDisplays(); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return 0; -} - -const char *OCIO_configGetDisplay(ConstConfigRcPtr *config, int index) -{ - try { - return (*config)->getDisplay(index); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return NULL; -} - -const char *OCIO_configGetDefaultView(ConstConfigRcPtr *config, const char *display) -{ - try { - return (*config)->getDefaultView(display); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return NULL; -} - -int OCIO_configGetNumViews(ConstConfigRcPtr *config, const char *display) -{ - try { - return (*config)->getNumViews(display); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return 0; -} - -const char *OCIO_configGetView(ConstConfigRcPtr *config, const char *display, int index) -{ - try { - return (*config)->getView(display, index); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return NULL; -} - -const char *OCIO_configGetDisplayColorSpaceName(ConstConfigRcPtr *config, const char *display, const char *view) -{ - try { - return (*config)->getDisplayColorSpaceName(display, view); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return NULL; -} - -int OCIO_colorSpaceIsInvertible(ConstColorSpaceRcPtr *cs) -{ - const char *family = (*cs)->getFamily(); - - if (!strcmp(family, "rrt") || !strcmp(family, "display")) { - /* assume display and rrt transformations are not invertible - * in fact some of them could be, but it doesn't make much sense to allow use them as invertible - */ - return false; - } - - if ((*cs)->isData()) { - /* data color spaces don't have transformation at all */ - return true; - } - - if ((*cs)->getTransform(COLORSPACE_DIR_TO_REFERENCE)) { - /* if there's defined transform to reference space, color space could be converted to scene linear */ - return true; - } - - return true; -} - -int OCIO_colorSpaceIsData(ConstColorSpaceRcPtr *cs) -{ - return ((*cs)->isData()); -} - -void OCIO_colorSpaceRelease(ConstColorSpaceRcPtr *cs) -{ - MEM_DELETE(cs, ConstColorSpaceRcPtr); -} - -ConstProcessorRcPtr *OCIO_configGetProcessorWithNames(ConstConfigRcPtr *config, const char *srcName, const char *dstName) -{ - ConstProcessorRcPtr *p = MEM_NEW(ConstProcessorRcPtr); - - try { - *p = (*config)->getProcessor(srcName, dstName); - - if (*p) - return p; - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - MEM_DELETE(p, ConstProcessorRcPtr); - - return 0; -} - -ConstProcessorRcPtr *OCIO_configGetProcessor(ConstConfigRcPtr *config, ConstTransformRcPtr *transform) -{ - ConstProcessorRcPtr *p = MEM_NEW(ConstProcessorRcPtr); - - try { - *p = (*config)->getProcessor(*transform); - - if (*p) - return p; - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - MEM_DELETE(p, ConstProcessorRcPtr); - - return NULL; -} - -void OCIO_processorApply(ConstProcessorRcPtr *processor, PackedImageDesc *img) -{ - try { - (*processor)->apply(*img); - } - catch (Exception &exception) { - OCIO_reportException(exception); - } -} - -void OCIO_processorApply_predivide(ConstProcessorRcPtr *processor, PackedImageDesc *img) -{ - try { - int channels = img->getNumChannels(); - - if (channels == 4) { - float *pixels = img->getData(); - - int width = img->getWidth(); - int height = img->getHeight(); - - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - float *pixel = pixels + 4 * (y * width + x); - - OCIO_processorApplyRGBA_predivide(processor, pixel); - } - } - } - else { - (*processor)->apply(*img); - } - } - catch (Exception &exception) { - OCIO_reportException(exception); - } -} - -void OCIO_processorApplyRGB(ConstProcessorRcPtr *processor, float *pixel) -{ - (*processor)->applyRGB(pixel); -} - -void OCIO_processorApplyRGBA(ConstProcessorRcPtr *processor, float *pixel) -{ - (*processor)->applyRGBA(pixel); -} - -void OCIO_processorApplyRGBA_predivide(ConstProcessorRcPtr *processor, float *pixel) -{ - if (pixel[3] == 1.0f || pixel[3] == 0.0f) { - (*processor)->applyRGBA(pixel); - } - else { - float alpha, inv_alpha; - - alpha = pixel[3]; - inv_alpha = 1.0f / alpha; - - pixel[0] *= inv_alpha; - pixel[1] *= inv_alpha; - pixel[2] *= inv_alpha; - - (*processor)->applyRGBA(pixel); - - pixel[0] *= alpha; - pixel[1] *= alpha; - pixel[2] *= alpha; - } -} - -void OCIO_processorRelease(ConstProcessorRcPtr *p) -{ - p->~ConstProcessorRcPtr(); - MEM_freeN(p); -} - -const char *OCIO_colorSpaceGetName(ConstColorSpaceRcPtr *cs) -{ - return (*cs)->getName(); -} - -const char *OCIO_colorSpaceGetDescription(ConstColorSpaceRcPtr *cs) -{ - return (*cs)->getDescription(); -} - -const char *OCIO_colorSpaceGetFamily(ConstColorSpaceRcPtr *cs) -{ - return (*cs)->getFamily(); -} - -DisplayTransformRcPtr *OCIO_createDisplayTransform(void) -{ - DisplayTransformRcPtr *dt = MEM_NEW(DisplayTransformRcPtr); - - *dt = DisplayTransform::Create(); - - return dt; -} - -void OCIO_displayTransformSetInputColorSpaceName(DisplayTransformRcPtr *dt, const char *name) -{ - (*dt)->setInputColorSpaceName(name); -} - -void OCIO_displayTransformSetDisplay(DisplayTransformRcPtr *dt, const char *name) -{ - (*dt)->setDisplay(name); -} - -void OCIO_displayTransformSetView(DisplayTransformRcPtr *dt, const char *name) -{ - (*dt)->setView(name); -} - -void OCIO_displayTransformSetDisplayCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *t) -{ - (*dt)->setDisplayCC(*t); -} - -void OCIO_displayTransformSetLinearCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *t) -{ - (*dt)->setLinearCC(*t); -} - -void OCIO_displayTransformRelease(DisplayTransformRcPtr *dt) -{ - MEM_DELETE(dt, DisplayTransformRcPtr); -} - -PackedImageDesc *OCIO_createPackedImageDesc(float *data, long width, long height, long numChannels, - long chanStrideBytes, long xStrideBytes, long yStrideBytes) -{ - try { - void *mem = MEM_mallocN(sizeof(PackedImageDesc), __func__); - PackedImageDesc *id = new(mem) PackedImageDesc(data, width, height, numChannels, chanStrideBytes, xStrideBytes, yStrideBytes); - - return id; - } - catch (Exception &exception) { - OCIO_reportException(exception); - } - - return NULL; -} - -void OCIO_packedImageDescRelease(PackedImageDesc* id) -{ - MEM_DELETE(id, PackedImageDesc); -} - -ExponentTransformRcPtr *OCIO_createExponentTransform(void) -{ - ExponentTransformRcPtr *et = MEM_NEW(ExponentTransformRcPtr); - - *et = ExponentTransform::Create(); - - return et; -} - -void OCIO_exponentTransformSetValue(ExponentTransformRcPtr *et, const float *exponent) -{ - (*et)->setValue(exponent); -} - -void OCIO_exponentTransformRelease(ExponentTransformRcPtr *et) -{ - MEM_DELETE(et, ExponentTransformRcPtr); -} - -MatrixTransformRcPtr *OCIO_createMatrixTransform(void) -{ - MatrixTransformRcPtr *mt = MEM_NEW(MatrixTransformRcPtr); - - *mt = MatrixTransform::Create(); - - return mt; -} - -void OCIO_matrixTransformSetValue(MatrixTransformRcPtr *mt, const float *m44, const float *offset4) -{ - (*mt)->setValue(m44, offset4); -} - -void OCIO_matrixTransformRelease(MatrixTransformRcPtr *mt) -{ - MEM_DELETE(mt, MatrixTransformRcPtr); -} - -void OCIO_matrixTransformScale(float * m44, float * offset4, const float *scale4f) -{ - MatrixTransform::Scale(m44, offset4, scale4f); -} diff --git a/intern/opencolorio/ocio_capi.h b/intern/opencolorio/ocio_capi.h index 0218ccfafcd..f924bffb8e0 100644 --- a/intern/opencolorio/ocio_capi.h +++ b/intern/opencolorio/ocio_capi.h @@ -28,8 +28,6 @@ #ifndef __OCIO_CAPI_H__ #define __OCIO_CAPI_H__ - - #ifdef __cplusplus using namespace OCIO_NAMESPACE; extern "C" { @@ -37,7 +35,6 @@ extern "C" { #define OCIO_DECLARE_HANDLE(name) typedef struct name##__ { int unused; } *name - #ifndef OCIO_CAPI_IMPLEMENTATION #define OCIO_ROLE_SCENE_LINEAR "scene_linear" #define OCIO_ROLE_COLOR_PICKING "color_picking" @@ -57,13 +54,15 @@ extern "C" { OCIO_DECLARE_HANDLE(MatrixTransformRcPtr); #endif +void OCIO_init(void); +void OCIO_exit(void); ConstConfigRcPtr *OCIO_getCurrentConfig(void); -ConstConfigRcPtr *OCIO_getDefaultConfig(void); void OCIO_setCurrentConfig(const ConstConfigRcPtr *config); ConstConfigRcPtr *OCIO_configCreateFromEnv(void); ConstConfigRcPtr *OCIO_configCreateFromFile(const char* filename); +ConstConfigRcPtr *OCIO_configCreateFallback(void); void OCIO_configRelease(ConstConfigRcPtr *config); @@ -127,4 +126,4 @@ void OCIO_matrixTransformScale(float * m44, float * offset4, const float * scale } #endif -#endif //OCIO_CAPI_H +#endif /* OCIO_CAPI_H */ diff --git a/intern/opencolorio/ocio_capi_stub.cpp b/intern/opencolorio/ocio_capi_stub.cpp deleted file mode 100644 index 2112b88ad72..00000000000 --- a/intern/opencolorio/ocio_capi_stub.cpp +++ /dev/null @@ -1,390 +0,0 @@ -/* - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * The Original Code is Copyright (C) 2012 Blender Foundation. - * All rights reserved. - * - * Contributor(s): Brecht van Lommel - * - * ***** END GPL LICENSE BLOCK ***** - */ - -#include - -#include "MEM_guardedalloc.h" -#include "BLI_math_color.h" - -namespace OCIO_NAMESPACE {}; - -#include "ocio_capi.h" - -#define CONFIG_DEFAULT ((ConstConfigRcPtr*)1) - -#define PROCESSOR_LINEAR_TO_SRGB ((ConstProcessorRcPtr*)1) -#define PROCESSOR_SRGB_TO_LINEAR ((ConstProcessorRcPtr*)2) -#define PROCESSOR_UNKNOWN ((ConstProcessorRcPtr*)3) - -#define COLORSPACE_LINEAR ((ConstColorSpaceRcPtr*)1) -#define COLORSPACE_SRGB ((ConstColorSpaceRcPtr*)2) - -typedef struct PackedImageDescription { - float *data; - long width; - long height; - long numChannels; - long chanStrideBytes; - long xStrideBytes; - long yStrideBytes; -} PackedImageDescription; - -ConstConfigRcPtr *OCIO_getCurrentConfig(void) -{ - return CONFIG_DEFAULT; -} - -ConstConfigRcPtr *OCIO_getDefaultConfig(void) -{ - return CONFIG_DEFAULT; -} - -void OCIO_setCurrentConfig(const ConstConfigRcPtr *) -{ -} - -ConstConfigRcPtr *OCIO_configCreateFromEnv(void) -{ - return CONFIG_DEFAULT; -} - -ConstConfigRcPtr *OCIO_configCreateFromFile(const char *) -{ - return CONFIG_DEFAULT; -} - -void OCIO_configRelease(ConstConfigRcPtr *) -{ -} - -int OCIO_configGetNumColorSpaces(ConstConfigRcPtr *) -{ - return 2; -} - -const char *OCIO_configGetColorSpaceNameByIndex(ConstConfigRcPtr *, int index) -{ - if (index == 0) - return "Linear"; - else if (index == 1) - return "sRGB"; - - return NULL; -} - -ConstColorSpaceRcPtr *OCIO_configGetColorSpace(ConstConfigRcPtr *, const char *name) -{ - if (strcmp(name, "scene_linear") == 0) - return COLORSPACE_LINEAR; - else if (strcmp(name, "color_picking") == 0) - return COLORSPACE_SRGB; - else if (strcmp(name, "texture_paint") == 0) - return COLORSPACE_LINEAR; - else if (strcmp(name, "default_byte") == 0) - return COLORSPACE_SRGB; - else if (strcmp(name, "default_float") == 0) - return COLORSPACE_LINEAR; - else if (strcmp(name, "default_sequencer") == 0) - return COLORSPACE_SRGB; - else if (strcmp(name, "Linear") == 0) - return COLORSPACE_LINEAR; - else if (strcmp(name, "sRGB") == 0) - return COLORSPACE_SRGB; - - return NULL; -} - -int OCIO_configGetIndexForColorSpace(ConstConfigRcPtr *config, const char *name) -{ - ConstColorSpaceRcPtr *cs = OCIO_configGetColorSpace(config, name); - - if (cs == COLORSPACE_LINEAR) - return 0; - else if (cs == COLORSPACE_SRGB) - return 1; - - return -1; -} - -const char *OCIO_configGetDefaultDisplay(ConstConfigRcPtr *) -{ - return "sRGB"; -} - -int OCIO_configGetNumDisplays(ConstConfigRcPtr* config) -{ - return 1; -} - -const char *OCIO_configGetDisplay(ConstConfigRcPtr *, int index) -{ - if (index == 0) - return "sRGB"; - - return NULL; -} - -const char *OCIO_configGetDefaultView(ConstConfigRcPtr *, const char *) -{ - return "Default"; -} - -int OCIO_configGetNumViews(ConstConfigRcPtr *, const char *) -{ - return 1; -} - -const char *OCIO_configGetView(ConstConfigRcPtr *, const char *, int index) -{ - if (index == 0) - return "Default"; - - return NULL; -} - -const char *OCIO_configGetDisplayColorSpaceName(ConstConfigRcPtr *, const char *, const char *) -{ - return "sRGB"; -} - -int OCIO_colorSpaceIsInvertible(ConstColorSpaceRcPtr *cs) -{ - return 1; -} - -int OCIO_colorSpaceIsData(ConstColorSpaceRcPtr *cs) -{ - return 0; -} - -void OCIO_colorSpaceRelease(ConstColorSpaceRcPtr *cs) -{ -} - -ConstProcessorRcPtr *OCIO_configGetProcessorWithNames(ConstConfigRcPtr *config, const char *srcName, const char *dstName) -{ - ConstColorSpaceRcPtr *cs_src = OCIO_configGetColorSpace(config, srcName); - ConstColorSpaceRcPtr *cs_dst = OCIO_configGetColorSpace(config, dstName); - - if (cs_src == COLORSPACE_LINEAR && cs_dst == COLORSPACE_SRGB) - return PROCESSOR_LINEAR_TO_SRGB; - else if (cs_src == COLORSPACE_SRGB && cs_dst == COLORSPACE_LINEAR) - return PROCESSOR_SRGB_TO_LINEAR; - - return 0; -} - -ConstProcessorRcPtr *OCIO_configGetProcessor(ConstConfigRcPtr *, ConstTransformRcPtr *tfm) -{ - return (ConstProcessorRcPtr*)tfm; -} - -void OCIO_processorApply(ConstProcessorRcPtr *processor, PackedImageDesc *img) -{ - /* OCIO_TODO stride not respected, channels must be 3 or 4 */ - PackedImageDescription *desc = (PackedImageDescription*)img; - int channels = desc->numChannels; - float *pixels = desc->data; - int width = desc->width; - int height = desc->height; - int x, y; - - for (y = 0; y < height; y++) { - for (x = 0; x < width; x++) { - float *pixel = pixels + channels * (y * width + x); - - if (channels == 4) - OCIO_processorApplyRGBA(processor, pixel); - else if (channels == 3) - OCIO_processorApplyRGB(processor, pixel); - } - } -} - -void OCIO_processorApply_predivide(ConstProcessorRcPtr *processor, PackedImageDesc *img) -{ - /* OCIO_TODO stride not respected, channels must be 3 or 4 */ - PackedImageDescription *desc = (PackedImageDescription*)img; - int channels = desc->numChannels; - float *pixels = desc->data; - int width = desc->width; - int height = desc->height; - int x, y; - - for (y = 0; y < height; y++) { - for (x = 0; x < width; x++) { - float *pixel = pixels + channels * (y * width + x); - - if (channels == 4) - OCIO_processorApplyRGBA_predivide(processor, pixel); - else if (channels == 3) - OCIO_processorApplyRGB(processor, pixel); - } - } -} - -void OCIO_processorApplyRGB(ConstProcessorRcPtr *processor, float *pixel) -{ - if (processor == PROCESSOR_LINEAR_TO_SRGB) - linearrgb_to_srgb_v3_v3(pixel, pixel); - else if (processor == PROCESSOR_SRGB_TO_LINEAR) - srgb_to_linearrgb_v3_v3(pixel, pixel); -} - -void OCIO_processorApplyRGBA(ConstProcessorRcPtr *processor, float *pixel) -{ - if (processor == PROCESSOR_LINEAR_TO_SRGB) - linearrgb_to_srgb_v4(pixel, pixel); - else if (processor == PROCESSOR_SRGB_TO_LINEAR) - srgb_to_linearrgb_v4(pixel, pixel); -} - -void OCIO_processorApplyRGBA_predivide(ConstProcessorRcPtr *processor, float *pixel) -{ - if (pixel[3] == 1.0f || pixel[3] == 0.0f) { - OCIO_processorApplyRGBA(processor, pixel); - } - else { - float alpha, inv_alpha; - - alpha = pixel[3]; - inv_alpha = 1.0f / alpha; - - pixel[0] *= inv_alpha; - pixel[1] *= inv_alpha; - pixel[2] *= inv_alpha; - - OCIO_processorApplyRGBA(processor, pixel); - - pixel[0] *= alpha; - pixel[1] *= alpha; - pixel[2] *= alpha; - } -} - -void OCIO_processorRelease(ConstProcessorRcPtr *) -{ -} - -const char *OCIO_colorSpaceGetName(ConstColorSpaceRcPtr *cs) -{ - if (cs == COLORSPACE_LINEAR) - return "Linear"; - else if (cs == COLORSPACE_SRGB) - return "sRGB"; - - return NULL; -} - -const char *OCIO_colorSpaceGetDescription(ConstColorSpaceRcPtr *) -{ - return ""; -} - -const char *OCIO_colorSpaceGetFamily(ConstColorSpaceRcPtr *) -{ - return ""; -} - -DisplayTransformRcPtr *OCIO_createDisplayTransform(void) -{ - return (DisplayTransformRcPtr*)PROCESSOR_LINEAR_TO_SRGB; -} - -void OCIO_displayTransformSetInputColorSpaceName(DisplayTransformRcPtr *, const char *) -{ -} - -void OCIO_displayTransformSetDisplay(DisplayTransformRcPtr *, const char *) -{ -} - -void OCIO_displayTransformSetView(DisplayTransformRcPtr *, const char *) -{ -} - -void OCIO_displayTransformSetDisplayCC(DisplayTransformRcPtr *, ConstTransformRcPtr *) -{ -} - -void OCIO_displayTransformSetLinearCC(DisplayTransformRcPtr *, ConstTransformRcPtr *) -{ -} - -void OCIO_displayTransformRelease(DisplayTransformRcPtr *) -{ -} - -PackedImageDesc *OCIO_createPackedImageDesc(float *data, long width, long height, long numChannels, - long chanStrideBytes, long xStrideBytes, long yStrideBytes) -{ - PackedImageDescription *desc = (PackedImageDescription*)MEM_callocN(sizeof(PackedImageDescription), "PackedImageDescription"); - - desc->data = data; - desc->width = width; - desc->height = height; - desc->numChannels = numChannels; - desc->chanStrideBytes = chanStrideBytes; - desc->xStrideBytes = xStrideBytes; - desc->yStrideBytes = yStrideBytes; - - return (PackedImageDesc*)desc; -} - -void OCIO_packedImageDescRelease(PackedImageDesc* id) -{ - MEM_freeN(id); -} - -ExponentTransformRcPtr *OCIO_createExponentTransform(void) -{ - return (ExponentTransformRcPtr*)PROCESSOR_UNKNOWN; -} - -void OCIO_exponentTransformSetValue(ExponentTransformRcPtr *, const float *) -{ -} - -void OCIO_exponentTransformRelease(ExponentTransformRcPtr *) -{ -} - -MatrixTransformRcPtr *OCIO_createMatrixTransform(void) -{ - return (MatrixTransformRcPtr*)PROCESSOR_UNKNOWN; -} - -void OCIO_matrixTransformSetValue(MatrixTransformRcPtr *, const float *, const float *) -{ -} - -void OCIO_matrixTransformRelease(MatrixTransformRcPtr *) -{ -} - -void OCIO_matrixTransformScale(float * , float * , const float *) -{ -} - diff --git a/intern/opencolorio/ocio_impl.cc b/intern/opencolorio/ocio_impl.cc new file mode 100644 index 00000000000..5d5c66cd356 --- /dev/null +++ b/intern/opencolorio/ocio_impl.cc @@ -0,0 +1,541 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2012 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Xavier Thomas + * Lukas Toene, + * Sergey Sharybin + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include +#include + +#include + +#include "MEM_guardedalloc.h" + +#define OCIO_CAPI_IMPLEMENTATION +#include "ocio_impl.h" + +#if !defined(WITH_ASSERT_ABORT) +# define OCIO_abort() +#else +# include +# define OCIO_abort() abort() +#endif + +#if defined(_MSC_VER) +# define __func__ __FUNCTION__ +#endif + +#define MEM_NEW(type) new(MEM_mallocN(sizeof(type), __func__)) type() +#define MEM_DELETE(what, type) if(what) { what->~type(); MEM_freeN(what); } (void)0 + +static void OCIO_reportError(const char *err) +{ + std::cerr << "OpenColorIO Error: " << err << std::endl; + + OCIO_abort(); +} + +static void OCIO_reportException(Exception &exception) +{ + OCIO_reportError(exception.what()); +} + +ConstConfigRcPtr *OCIOImpl::getCurrentConfig(void) +{ + ConstConfigRcPtr *config = MEM_NEW(ConstConfigRcPtr); + + try { + *config = GetCurrentConfig(); + + if(*config) + return config; + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + MEM_DELETE(config, ConstConfigRcPtr); + + return NULL; +} + +void OCIOImpl::setCurrentConfig(const ConstConfigRcPtr *config) +{ + try { + SetCurrentConfig(*config); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } +} + +ConstConfigRcPtr *OCIOImpl::configCreateFromEnv(void) +{ + ConstConfigRcPtr *config = MEM_NEW(ConstConfigRcPtr); + + try { + *config = Config::CreateFromEnv(); + + if (*config) + return config; + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + MEM_DELETE(config, ConstConfigRcPtr); + + return NULL; +} + + +ConstConfigRcPtr *OCIOImpl::configCreateFromFile(const char *filename) +{ + ConstConfigRcPtr *config = MEM_NEW(ConstConfigRcPtr); + + try { + *config = Config::CreateFromFile(filename); + + if (*config) + return config; + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + MEM_DELETE(config, ConstConfigRcPtr); + + return NULL; +} + +void OCIOImpl::configRelease(ConstConfigRcPtr *config) +{ + MEM_DELETE(config, ConstConfigRcPtr); +} + +int OCIOImpl::configGetNumColorSpaces(ConstConfigRcPtr *config) +{ + try { + return (*config)->getNumColorSpaces(); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return 0; +} + +const char *OCIOImpl::configGetColorSpaceNameByIndex(ConstConfigRcPtr *config, int index) +{ + try { + return (*config)->getColorSpaceNameByIndex(index); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return NULL; +} + +ConstColorSpaceRcPtr *OCIOImpl::configGetColorSpace(ConstConfigRcPtr *config, const char *name) +{ + ConstColorSpaceRcPtr *cs = MEM_NEW(ConstColorSpaceRcPtr); + + try { + *cs = (*config)->getColorSpace(name); + + if (*cs) + return cs; + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + MEM_DELETE(cs, ConstColorSpaceRcPtr); + + return NULL; +} + +int OCIOImpl::configGetIndexForColorSpace(ConstConfigRcPtr *config, const char *name) +{ + try { + return (*config)->getIndexForColorSpace(name); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return -1; +} + +const char *OCIOImpl::configGetDefaultDisplay(ConstConfigRcPtr *config) +{ + try { + return (*config)->getDefaultDisplay(); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return NULL; +} + +int OCIOImpl::configGetNumDisplays(ConstConfigRcPtr* config) +{ + try { + return (*config)->getNumDisplays(); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return 0; +} + +const char *OCIOImpl::configGetDisplay(ConstConfigRcPtr *config, int index) +{ + try { + return (*config)->getDisplay(index); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return NULL; +} + +const char *OCIOImpl::configGetDefaultView(ConstConfigRcPtr *config, const char *display) +{ + try { + return (*config)->getDefaultView(display); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return NULL; +} + +int OCIOImpl::configGetNumViews(ConstConfigRcPtr *config, const char *display) +{ + try { + return (*config)->getNumViews(display); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return 0; +} + +const char *OCIOImpl::configGetView(ConstConfigRcPtr *config, const char *display, int index) +{ + try { + return (*config)->getView(display, index); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return NULL; +} + +const char *OCIOImpl::configGetDisplayColorSpaceName(ConstConfigRcPtr *config, const char *display, const char *view) +{ + try { + return (*config)->getDisplayColorSpaceName(display, view); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return NULL; +} + +int OCIOImpl::colorSpaceIsInvertible(ConstColorSpaceRcPtr *cs) +{ + const char *family = (*cs)->getFamily(); + + if (!strcmp(family, "rrt") || !strcmp(family, "display")) { + /* assume display and rrt transformations are not invertible + * in fact some of them could be, but it doesn't make much sense to allow use them as invertible + */ + return false; + } + + if ((*cs)->isData()) { + /* data color spaces don't have transformation at all */ + return true; + } + + if ((*cs)->getTransform(COLORSPACE_DIR_TO_REFERENCE)) { + /* if there's defined transform to reference space, color space could be converted to scene linear */ + return true; + } + + return true; +} + +int OCIOImpl::colorSpaceIsData(ConstColorSpaceRcPtr *cs) +{ + return ((*cs)->isData()); +} + +void OCIOImpl::colorSpaceRelease(ConstColorSpaceRcPtr *cs) +{ + MEM_DELETE(cs, ConstColorSpaceRcPtr); +} + +ConstProcessorRcPtr *OCIOImpl::configGetProcessorWithNames(ConstConfigRcPtr *config, const char *srcName, const char *dstName) +{ + ConstProcessorRcPtr *p = MEM_NEW(ConstProcessorRcPtr); + + try { + *p = (*config)->getProcessor(srcName, dstName); + + if (*p) + return p; + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + MEM_DELETE(p, ConstProcessorRcPtr); + + return 0; +} + +ConstProcessorRcPtr *OCIOImpl::configGetProcessor(ConstConfigRcPtr *config, ConstTransformRcPtr *transform) +{ + ConstProcessorRcPtr *p = MEM_NEW(ConstProcessorRcPtr); + + try { + *p = (*config)->getProcessor(*transform); + + if (*p) + return p; + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + MEM_DELETE(p, ConstProcessorRcPtr); + + return NULL; +} + +void OCIOImpl::processorApply(ConstProcessorRcPtr *processor, PackedImageDesc *img) +{ + try { + (*processor)->apply(*img); + } + catch (Exception &exception) { + OCIO_reportException(exception); + } +} + +void OCIOImpl::processorApply_predivide(ConstProcessorRcPtr *processor, PackedImageDesc *img) +{ + try { + int channels = img->getNumChannels(); + + if (channels == 4) { + float *pixels = img->getData(); + + int width = img->getWidth(); + int height = img->getHeight(); + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + float *pixel = pixels + 4 * (y * width + x); + + processorApplyRGBA_predivide(processor, pixel); + } + } + } + else { + (*processor)->apply(*img); + } + } + catch (Exception &exception) { + OCIO_reportException(exception); + } +} + +void OCIOImpl::processorApplyRGB(ConstProcessorRcPtr *processor, float *pixel) +{ + (*processor)->applyRGB(pixel); +} + +void OCIOImpl::processorApplyRGBA(ConstProcessorRcPtr *processor, float *pixel) +{ + (*processor)->applyRGBA(pixel); +} + +void OCIOImpl::processorApplyRGBA_predivide(ConstProcessorRcPtr *processor, float *pixel) +{ + if (pixel[3] == 1.0f || pixel[3] == 0.0f) { + (*processor)->applyRGBA(pixel); + } + else { + float alpha, inv_alpha; + + alpha = pixel[3]; + inv_alpha = 1.0f / alpha; + + pixel[0] *= inv_alpha; + pixel[1] *= inv_alpha; + pixel[2] *= inv_alpha; + + (*processor)->applyRGBA(pixel); + + pixel[0] *= alpha; + pixel[1] *= alpha; + pixel[2] *= alpha; + } +} + +void OCIOImpl::processorRelease(ConstProcessorRcPtr *p) +{ + p->~ConstProcessorRcPtr(); + MEM_freeN(p); +} + +const char *OCIOImpl::colorSpaceGetName(ConstColorSpaceRcPtr *cs) +{ + return (*cs)->getName(); +} + +const char *OCIOImpl::colorSpaceGetDescription(ConstColorSpaceRcPtr *cs) +{ + return (*cs)->getDescription(); +} + +const char *OCIOImpl::colorSpaceGetFamily(ConstColorSpaceRcPtr *cs) +{ + return (*cs)->getFamily(); +} + +DisplayTransformRcPtr *OCIOImpl::createDisplayTransform(void) +{ + DisplayTransformRcPtr *dt = MEM_NEW(DisplayTransformRcPtr); + + *dt = DisplayTransform::Create(); + + return dt; +} + +void OCIOImpl::displayTransformSetInputColorSpaceName(DisplayTransformRcPtr *dt, const char *name) +{ + (*dt)->setInputColorSpaceName(name); +} + +void OCIOImpl::displayTransformSetDisplay(DisplayTransformRcPtr *dt, const char *name) +{ + (*dt)->setDisplay(name); +} + +void OCIOImpl::displayTransformSetView(DisplayTransformRcPtr *dt, const char *name) +{ + (*dt)->setView(name); +} + +void OCIOImpl::displayTransformSetDisplayCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *t) +{ + (*dt)->setDisplayCC(*t); +} + +void OCIOImpl::displayTransformSetLinearCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *t) +{ + (*dt)->setLinearCC(*t); +} + +void OCIOImpl::displayTransformRelease(DisplayTransformRcPtr *dt) +{ + MEM_DELETE(dt, DisplayTransformRcPtr); +} + +PackedImageDesc *OCIOImpl::createPackedImageDesc(float *data, long width, long height, long numChannels, + long chanStrideBytes, long xStrideBytes, long yStrideBytes) +{ + try { + void *mem = MEM_mallocN(sizeof(PackedImageDesc), __func__); + PackedImageDesc *id = new(mem) PackedImageDesc(data, width, height, numChannels, chanStrideBytes, xStrideBytes, yStrideBytes); + + return id; + } + catch (Exception &exception) { + OCIO_reportException(exception); + } + + return NULL; +} + +void OCIOImpl::packedImageDescRelease(PackedImageDesc* id) +{ + MEM_DELETE(id, PackedImageDesc); +} + +ExponentTransformRcPtr *OCIOImpl::createExponentTransform(void) +{ + ExponentTransformRcPtr *et = MEM_NEW(ExponentTransformRcPtr); + + *et = ExponentTransform::Create(); + + return et; +} + +void OCIOImpl::exponentTransformSetValue(ExponentTransformRcPtr *et, const float *exponent) +{ + (*et)->setValue(exponent); +} + +void OCIOImpl::exponentTransformRelease(ExponentTransformRcPtr *et) +{ + MEM_DELETE(et, ExponentTransformRcPtr); +} + +MatrixTransformRcPtr *OCIOImpl::createMatrixTransform(void) +{ + MatrixTransformRcPtr *mt = MEM_NEW(MatrixTransformRcPtr); + + *mt = MatrixTransform::Create(); + + return mt; +} + +void OCIOImpl::matrixTransformSetValue(MatrixTransformRcPtr *mt, const float *m44, const float *offset4) +{ + (*mt)->setValue(m44, offset4); +} + +void OCIOImpl::matrixTransformRelease(MatrixTransformRcPtr *mt) +{ + MEM_DELETE(mt, MatrixTransformRcPtr); +} + +void OCIOImpl::matrixTransformScale(float * m44, float * offset4, const float *scale4f) +{ + MatrixTransform::Scale(m44, offset4, scale4f); +} diff --git a/intern/opencolorio/ocio_impl.h b/intern/opencolorio/ocio_impl.h new file mode 100644 index 00000000000..e05b35648b9 --- /dev/null +++ b/intern/opencolorio/ocio_impl.h @@ -0,0 +1,240 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2012 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Sergey Sharybin + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __OCIO_IMPL_H__ +#define __OCIO_IMPL_H__ + +#include "ocio_capi.h" + +class IOCIOImpl { +public: + virtual ~IOCIOImpl() {}; + + virtual ConstConfigRcPtr *getCurrentConfig(void) = 0; + virtual void setCurrentConfig(const ConstConfigRcPtr *config) = 0; + + virtual ConstConfigRcPtr *configCreateFromEnv(void) = 0; + virtual ConstConfigRcPtr *configCreateFromFile(const char* filename) = 0; + + virtual void configRelease(ConstConfigRcPtr *config) = 0; + + virtual int configGetNumColorSpaces(ConstConfigRcPtr *config) = 0; + virtual const char *configGetColorSpaceNameByIndex(ConstConfigRcPtr *config, int index) = 0; + virtual ConstColorSpaceRcPtr *configGetColorSpace(ConstConfigRcPtr *config, const char *name) = 0; + virtual int configGetIndexForColorSpace(ConstConfigRcPtr *config, const char *name) = 0; + + virtual int colorSpaceIsInvertible(ConstColorSpaceRcPtr *cs) = 0; + virtual int colorSpaceIsData(ConstColorSpaceRcPtr *cs) = 0; + + virtual void colorSpaceRelease(ConstColorSpaceRcPtr *cs) = 0; + + virtual const char *configGetDefaultDisplay(ConstConfigRcPtr *config) = 0; + virtual int configGetNumDisplays(ConstConfigRcPtr *config) = 0; + virtual const char *configGetDisplay(ConstConfigRcPtr *config, int index) = 0; + virtual const char *configGetDefaultView(ConstConfigRcPtr *config, const char *display) = 0; + virtual int configGetNumViews(ConstConfigRcPtr *config, const char *display) = 0; + virtual const char *configGetView(ConstConfigRcPtr *config, const char *display, int index) = 0; + virtual const char *configGetDisplayColorSpaceName(ConstConfigRcPtr *config, const char *display, const char *view) = 0; + + virtual ConstProcessorRcPtr *configGetProcessorWithNames(ConstConfigRcPtr *config, const char *srcName, const char *dstName) = 0; + virtual ConstProcessorRcPtr *configGetProcessor(ConstConfigRcPtr *config, ConstTransformRcPtr *transform) = 0; + + virtual void processorApply(ConstProcessorRcPtr *processor, PackedImageDesc *img) = 0; + virtual void processorApply_predivide(ConstProcessorRcPtr *processor, PackedImageDesc *img) = 0; + virtual void processorApplyRGB(ConstProcessorRcPtr *processor, float *pixel) = 0; + virtual void processorApplyRGBA(ConstProcessorRcPtr *processor, float *pixel) = 0; + virtual void processorApplyRGBA_predivide(ConstProcessorRcPtr *processor, float *pixel) = 0; + + virtual void processorRelease(ConstProcessorRcPtr *p) = 0; + + virtual const char *colorSpaceGetName(ConstColorSpaceRcPtr *cs) = 0; + virtual const char *colorSpaceGetDescription(ConstColorSpaceRcPtr *cs) = 0; + virtual const char *colorSpaceGetFamily(ConstColorSpaceRcPtr *cs) = 0; + + virtual DisplayTransformRcPtr *createDisplayTransform(void) = 0; + virtual void displayTransformSetInputColorSpaceName(DisplayTransformRcPtr *dt, const char *name) = 0; + virtual void displayTransformSetDisplay(DisplayTransformRcPtr *dt, const char *name) = 0; + virtual void displayTransformSetView(DisplayTransformRcPtr *dt, const char *name) = 0; + virtual void displayTransformSetDisplayCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *et) = 0; + virtual void displayTransformSetLinearCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *et) = 0; + virtual void displayTransformRelease(DisplayTransformRcPtr *dt) = 0; + + virtual PackedImageDesc *createPackedImageDesc(float *data, long width, long height, long numChannels, + long chanStrideBytes, long xStrideBytes, long yStrideBytes) = 0; + + virtual void packedImageDescRelease(PackedImageDesc *p) = 0; + + virtual ExponentTransformRcPtr *createExponentTransform(void) = 0; + virtual void exponentTransformSetValue(ExponentTransformRcPtr *et, const float *exponent) = 0; + virtual void exponentTransformRelease(ExponentTransformRcPtr *et) = 0; + + virtual MatrixTransformRcPtr *createMatrixTransform(void) = 0; + virtual void matrixTransformSetValue(MatrixTransformRcPtr *et, const float *m44, const float *offset4) = 0; + virtual void matrixTransformRelease(MatrixTransformRcPtr *mt) = 0; + + virtual void matrixTransformScale(float * m44, float * offset4, const float * scale4) = 0; +}; + +class FallbackImpl : public IOCIOImpl { +public: + FallbackImpl() {}; + + ConstConfigRcPtr *getCurrentConfig(void); + void setCurrentConfig(const ConstConfigRcPtr *config); + + ConstConfigRcPtr *configCreateFromEnv(void); + ConstConfigRcPtr *configCreateFromFile(const char* filename); + + void configRelease(ConstConfigRcPtr *config); + + int configGetNumColorSpaces(ConstConfigRcPtr *config); + const char *configGetColorSpaceNameByIndex(ConstConfigRcPtr *config, int index); + ConstColorSpaceRcPtr *configGetColorSpace(ConstConfigRcPtr *config, const char *name); + int configGetIndexForColorSpace(ConstConfigRcPtr *config, const char *name); + + int colorSpaceIsInvertible(ConstColorSpaceRcPtr *cs); + int colorSpaceIsData(ConstColorSpaceRcPtr *cs); + + void colorSpaceRelease(ConstColorSpaceRcPtr *cs); + + const char *configGetDefaultDisplay(ConstConfigRcPtr *config); + int configGetNumDisplays(ConstConfigRcPtr *config); + const char *configGetDisplay(ConstConfigRcPtr *config, int index); + const char *configGetDefaultView(ConstConfigRcPtr *config, const char *display); + int configGetNumViews(ConstConfigRcPtr *config, const char *display); + const char *configGetView(ConstConfigRcPtr *config, const char *display, int index); + const char *configGetDisplayColorSpaceName(ConstConfigRcPtr *config, const char *display, const char *view); + + ConstProcessorRcPtr *configGetProcessorWithNames(ConstConfigRcPtr *config, const char *srcName, const char *dstName); + ConstProcessorRcPtr *configGetProcessor(ConstConfigRcPtr *config, ConstTransformRcPtr *transform); + + void processorApply(ConstProcessorRcPtr *processor, PackedImageDesc *img); + void processorApply_predivide(ConstProcessorRcPtr *processor, PackedImageDesc *img); + void processorApplyRGB(ConstProcessorRcPtr *processor, float *pixel); + void processorApplyRGBA(ConstProcessorRcPtr *processor, float *pixel); + void processorApplyRGBA_predivide(ConstProcessorRcPtr *processor, float *pixel); + + void processorRelease(ConstProcessorRcPtr *p); + + const char *colorSpaceGetName(ConstColorSpaceRcPtr *cs); + const char *colorSpaceGetDescription(ConstColorSpaceRcPtr *cs); + const char *colorSpaceGetFamily(ConstColorSpaceRcPtr *cs); + + DisplayTransformRcPtr *createDisplayTransform(void); + void displayTransformSetInputColorSpaceName(DisplayTransformRcPtr *dt, const char *name); + void displayTransformSetDisplay(DisplayTransformRcPtr *dt, const char *name); + void displayTransformSetView(DisplayTransformRcPtr *dt, const char *name); + void displayTransformSetDisplayCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *et); + void displayTransformSetLinearCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *et); + void displayTransformRelease(DisplayTransformRcPtr *dt); + + PackedImageDesc *createPackedImageDesc(float *data, long width, long height, long numChannels, + long chanStrideBytes, long xStrideBytes, long yStrideBytes); + + void packedImageDescRelease(PackedImageDesc *p); + + ExponentTransformRcPtr *createExponentTransform(void); + void exponentTransformSetValue(ExponentTransformRcPtr *et, const float *exponent); + void exponentTransformRelease(ExponentTransformRcPtr *et); + + MatrixTransformRcPtr *createMatrixTransform(void); + void matrixTransformSetValue(MatrixTransformRcPtr *et, const float *m44, const float *offset4); + void matrixTransformRelease(MatrixTransformRcPtr *mt); + + void matrixTransformScale(float * m44, float * offset4, const float * scale4); +}; + +#ifdef WITH_OCIO +class OCIOImpl : public IOCIOImpl { +public: + OCIOImpl() {}; + + ConstConfigRcPtr *getCurrentConfig(void); + void setCurrentConfig(const ConstConfigRcPtr *config); + + ConstConfigRcPtr *configCreateFromEnv(void); + ConstConfigRcPtr *configCreateFromFile(const char* filename); + + void configRelease(ConstConfigRcPtr *config); + + int configGetNumColorSpaces(ConstConfigRcPtr *config); + const char *configGetColorSpaceNameByIndex(ConstConfigRcPtr *config, int index); + ConstColorSpaceRcPtr *configGetColorSpace(ConstConfigRcPtr *config, const char *name); + int configGetIndexForColorSpace(ConstConfigRcPtr *config, const char *name); + + int colorSpaceIsInvertible(ConstColorSpaceRcPtr *cs); + int colorSpaceIsData(ConstColorSpaceRcPtr *cs); + + void colorSpaceRelease(ConstColorSpaceRcPtr *cs); + + const char *configGetDefaultDisplay(ConstConfigRcPtr *config); + int configGetNumDisplays(ConstConfigRcPtr *config); + const char *configGetDisplay(ConstConfigRcPtr *config, int index); + const char *configGetDefaultView(ConstConfigRcPtr *config, const char *display); + int configGetNumViews(ConstConfigRcPtr *config, const char *display); + const char *configGetView(ConstConfigRcPtr *config, const char *display, int index); + const char *configGetDisplayColorSpaceName(ConstConfigRcPtr *config, const char *display, const char *view); + + ConstProcessorRcPtr *configGetProcessorWithNames(ConstConfigRcPtr *config, const char *srcName, const char *dstName); + ConstProcessorRcPtr *configGetProcessor(ConstConfigRcPtr *config, ConstTransformRcPtr *transform); + + void processorApply(ConstProcessorRcPtr *processor, PackedImageDesc *img); + void processorApply_predivide(ConstProcessorRcPtr *processor, PackedImageDesc *img); + void processorApplyRGB(ConstProcessorRcPtr *processor, float *pixel); + void processorApplyRGBA(ConstProcessorRcPtr *processor, float *pixel); + void processorApplyRGBA_predivide(ConstProcessorRcPtr *processor, float *pixel); + + void processorRelease(ConstProcessorRcPtr *p); + + const char *colorSpaceGetName(ConstColorSpaceRcPtr *cs); + const char *colorSpaceGetDescription(ConstColorSpaceRcPtr *cs); + const char *colorSpaceGetFamily(ConstColorSpaceRcPtr *cs); + + DisplayTransformRcPtr *createDisplayTransform(void); + void displayTransformSetInputColorSpaceName(DisplayTransformRcPtr *dt, const char *name); + void displayTransformSetDisplay(DisplayTransformRcPtr *dt, const char *name); + void displayTransformSetView(DisplayTransformRcPtr *dt, const char *name); + void displayTransformSetDisplayCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *et); + void displayTransformSetLinearCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *et); + void displayTransformRelease(DisplayTransformRcPtr *dt); + + PackedImageDesc *createPackedImageDesc(float *data, long width, long height, long numChannels, + long chanStrideBytes, long xStrideBytes, long yStrideBytes); + + void packedImageDescRelease(PackedImageDesc *p); + + ExponentTransformRcPtr *createExponentTransform(void); + void exponentTransformSetValue(ExponentTransformRcPtr *et, const float *exponent); + void exponentTransformRelease(ExponentTransformRcPtr *et); + + MatrixTransformRcPtr *createMatrixTransform(void); + void matrixTransformSetValue(MatrixTransformRcPtr *et, const float *m44, const float *offset4); + void matrixTransformRelease(MatrixTransformRcPtr *mt); + + void matrixTransformScale(float * m44, float * offset4, const float * scale4); +}; +#endif + +#endif /* OCIO_IMPL_H */ diff --git a/source/blender/imbuf/intern/colormanagement.c b/source/blender/imbuf/intern/colormanagement.c index 1a86932b0a9..bb1449060dd 100644 --- a/source/blender/imbuf/intern/colormanagement.c +++ b/source/blender/imbuf/intern/colormanagement.c @@ -546,6 +546,8 @@ static void colormanage_free_config(void) /* free views */ BLI_freelistN(&global_views); + + OCIO_exit(); } void colormanagement_init(void) @@ -555,6 +557,8 @@ void colormanagement_init(void) char configfile[FILE_MAX]; ConstConfigRcPtr *config = NULL; + OCIO_init(); + ocio_env = getenv("OCIO"); if (ocio_env && ocio_env[0] != '\0') @@ -571,7 +575,9 @@ void colormanagement_init(void) } if (config == NULL) { - config = OCIO_getDefaultConfig(); + printf("Color management: using fallback mode for management\n"); + + config = OCIO_configCreateFallback(); } if (config) { @@ -638,19 +644,15 @@ static const char *display_transform_get_colorspace_name(const ColorManagedViewS { ConstConfigRcPtr *config = OCIO_getCurrentConfig(); - if (config) { - const char *display = display_settings->display_device; - const char *view = view_settings->view_transform; - const char *colorspace_name; + const char *display = display_settings->display_device; + const char *view = view_settings->view_transform; + const char *colorspace_name; - colorspace_name = OCIO_configGetDisplayColorSpaceName(config, display, view); + colorspace_name = OCIO_configGetDisplayColorSpaceName(config, display, view); - OCIO_configRelease(config); - - return colorspace_name; - } + OCIO_configRelease(config); - return NULL; + return colorspace_name; } static ColorSpace *display_transform_get_colorspace(const ColorManagedViewSettings *view_settings, @@ -671,12 +673,6 @@ static ConstProcessorRcPtr *create_display_buffer_processor(const char *view_tra DisplayTransformRcPtr *dt; ConstProcessorRcPtr *processor; - if (!config) { - /* there's no valid OCIO configuration, can't create processor */ - - return NULL; - } - dt = OCIO_createDisplayTransform(); /* assuming handling buffer was already converted to scene linear space */ @@ -726,12 +722,6 @@ static ConstProcessorRcPtr *create_colorspace_transform_processor(const char *fr ConstConfigRcPtr *config = OCIO_getCurrentConfig(); ConstProcessorRcPtr *processor; - if (!config) { - /* there's no valid OCIO configuration, can't create processor */ - - return NULL; - } - processor = OCIO_configGetProcessorWithNames(config, from_colorspace, to_colorspace); OCIO_configRelease(config); @@ -881,7 +871,7 @@ void colormanage_imbuf_make_linear(ImBuf *ibuf, const char *from_colorspace) { ColorSpace *colorspace = colormanage_colorspace_get_named(from_colorspace); - if (colorspace && colorspace->is_data) { + if (colorspace->is_data) { ibuf->colormanage_flag |= IMB_COLORMANAGE_IS_DATA; return; } @@ -1074,7 +1064,7 @@ void IMB_colormanagement_check_is_data(ImBuf *ibuf, const char *name) { ColorSpace *colorspace = colormanage_colorspace_get_named(name); - if (colorspace && colorspace->is_data) + if (colorspace->is_data) ibuf->colormanage_flag |= IMB_COLORMANAGE_IS_DATA; else ibuf->colormanage_flag &= ~IMB_COLORMANAGE_IS_DATA; @@ -1086,7 +1076,7 @@ void IMB_colormanagement_assign_float_colorspace(ImBuf *ibuf, const char *name) ibuf->float_colorspace = colorspace; - if (colorspace && colorspace->is_data) + if (colorspace->is_data) ibuf->colormanage_flag |= IMB_COLORMANAGE_IS_DATA; else ibuf->colormanage_flag &= ~IMB_COLORMANAGE_IS_DATA; @@ -1098,7 +1088,7 @@ void IMB_colormanagement_assign_rect_colorspace(ImBuf *ibuf, const char *name) ibuf->rect_colorspace = colorspace; - if (colorspace && colorspace->is_data) + if (colorspace->is_data) ibuf->colormanage_flag |= IMB_COLORMANAGE_IS_DATA; else ibuf->colormanage_flag &= ~IMB_COLORMANAGE_IS_DATA; @@ -1898,12 +1888,6 @@ const char *colormanage_display_get_default_name(void) ConstConfigRcPtr *config = OCIO_getCurrentConfig(); const char *display_name; - if (!config) { - /* no valid OCIO configuration, can't get default display */ - - return NULL; - } - display_name = OCIO_configGetDefaultDisplay(config); OCIO_configRelease(config); @@ -2015,12 +1999,6 @@ const char *colormanage_view_get_default_name(const ColorManagedDisplay *display ConstConfigRcPtr *config = OCIO_getCurrentConfig(); const char *name; - if (!config) { - /* no valid OCIO configuration, can't get default view */ - - return NULL; - } - name = OCIO_configGetDefaultView(config, display->name); OCIO_configRelease(config); -- cgit v1.2.3