diff options
Diffstat (limited to 'intern/opencolorio')
-rw-r--r-- | intern/opencolorio/CMakeLists.txt | 62 | ||||
-rw-r--r-- | intern/opencolorio/SConscript | 19 | ||||
-rw-r--r-- | intern/opencolorio/ocio_capi.cpp | 525 | ||||
-rw-r--r-- | intern/opencolorio/ocio_capi.h | 128 | ||||
-rw-r--r-- | intern/opencolorio/ocio_capi_stub.cpp | 380 |
5 files changed, 1114 insertions, 0 deletions
diff --git a/intern/opencolorio/CMakeLists.txt b/intern/opencolorio/CMakeLists.txt new file mode 100644 index 00000000000..479bbd3ab0a --- /dev/null +++ b/intern/opencolorio/CMakeLists.txt @@ -0,0 +1,62 @@ +# ***** 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. +# +# The Original Code is: all of this file. +# +# Contributor(s): Sergey Sharybin. +# +# ***** END GPL LICENSE BLOCK ***** + +if(WITH_OPENCOLORIO) + set(INC + . + ${OPENCOLORIO_INCLUDE_DIRS} + ) + + set(SRC + ocio_capi.cpp + ocio_capi.h + ) + + if(WIN32 AND NOT MINGW) + list(APPEND INC + ${BOOST_INCLUDE_DIR} + ) + endif() +else() + set(INC + . + ../../source/blender/blenlib + ) + + set(SRC + ocio_capi_stub.cpp + ocio_capi.h + ) +endif() + +set(INC_SYS + ../guardedalloc +) + +add_definitions( +) + +blender_add_lib(bf_intern_opencolorio "${SRC}" "${INC}" "${INC_SYS}") + diff --git a/intern/opencolorio/SConscript b/intern/opencolorio/SConscript new file mode 100644 index 00000000000..fec07662735 --- /dev/null +++ b/intern/opencolorio/SConscript @@ -0,0 +1,19 @@ +#!/usr/bin/python + +Import('env') + +sources = env.Glob('*.cpp') + +incs = '. ../guardedalloc ../../source/blender/blenlib' + +if env['WITH_BF_OCIO']: + sources.remove('ocio_capi_stub.cpp') + + incs += ' ' + env['BF_OCIO_INC'] + + if env['OURPLATFORM'] in ('win32-vc', 'win64-vc'): + incs += ' ' + env['BF_BOOST_INC'] +else: + sources.remove('ocio_capi.cpp') + +env.BlenderLib( 'bf_intern_opencolorio', sources, Split(incs), [], libtype=['extern','player'], priority=[10, 185]) diff --git a/intern/opencolorio/ocio_capi.cpp b/intern/opencolorio/ocio_capi.cpp new file mode 100644 index 00000000000..ec8012cc30f --- /dev/null +++ b/intern/opencolorio/ocio_capi.cpp @@ -0,0 +1,525 @@ +/* + * ***** 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 <iostream> +#include <string.h> + +#include <OpenColorIO/OpenColorIO.h> + +#include "MEM_guardedalloc.h" + +#define OCIO_CAPI_IMPLEMENTATION +#include "ocio_capi.h" + +#ifdef NDEBUG +# define OCIO_abort() +#else +# include <stdlib.h> +# 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) { 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); + } + + 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); + } + + 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); + } + + 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; +} + +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); + } + + 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); + } + + 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 new file mode 100644 index 00000000000..f0edd25ac14 --- /dev/null +++ b/intern/opencolorio/ocio_capi.h @@ -0,0 +1,128 @@ +/* + * ***** 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 ***** + */ + +#ifndef __OCIO_CAPI_H__ +#define __OCIO_CAPI_H__ + + + +#ifdef __cplusplus +using namespace OCIO_NAMESPACE; +extern "C" { +#endif + +#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" + #define OCIO_ROLE_TEXTURE_PAINT "texture_paint" + #define OCIO_ROLE_DEFAULT_BYTE "default_byte" + #define OCIO_ROLE_DEFAULT_FLOAT "default_float" + #define OCIO_ROLE_DEFAULT_SEQUENCER "default_sequencer" + + OCIO_DECLARE_HANDLE(ConstConfigRcPtr); + OCIO_DECLARE_HANDLE(ConstColorSpaceRcPtr); + OCIO_DECLARE_HANDLE(ConstProcessorRcPtr); + OCIO_DECLARE_HANDLE(ConstContextRcPtr); + OCIO_DECLARE_HANDLE(PackedImageDesc); + OCIO_DECLARE_HANDLE(DisplayTransformRcPtr); + OCIO_DECLARE_HANDLE(ConstTransformRcPtr); + OCIO_DECLARE_HANDLE(ExponentTransformRcPtr); + OCIO_DECLARE_HANDLE(MatrixTransformRcPtr); +#endif + + +ConstConfigRcPtr *OCIO_getCurrentConfig(void); +void OCIO_setCurrentConfig(const ConstConfigRcPtr *config); + +ConstConfigRcPtr *OCIO_configCreateFromEnv(void); +ConstConfigRcPtr *OCIO_configCreateFromFile(const char* filename); + +void OCIO_configRelease(ConstConfigRcPtr *config); + +int OCIO_configGetNumColorSpaces(ConstConfigRcPtr *config); +const char *OCIO_configGetColorSpaceNameByIndex(ConstConfigRcPtr *config, int index); +ConstColorSpaceRcPtr *OCIO_configGetColorSpace(ConstConfigRcPtr *config, const char *name); +int OCIO_configGetIndexForColorSpace(ConstConfigRcPtr *config, const char *name); + +int OCIO_colorSpaceIsInvertible(ConstColorSpaceRcPtr *cs); + +void OCIO_colorSpaceRelease(ConstColorSpaceRcPtr *cs); + +const char *OCIO_configGetDefaultDisplay(ConstConfigRcPtr *config); +int OCIO_configGetNumDisplays(ConstConfigRcPtr *config); +const char *OCIO_configGetDisplay(ConstConfigRcPtr *config, int index); +const char *OCIO_configGetDefaultView(ConstConfigRcPtr *config, const char *display); +int OCIO_configGetNumViews(ConstConfigRcPtr *config, const char *display); +const char *OCIO_configGetView(ConstConfigRcPtr *config, const char *display, int index); +const char *OCIO_configGetDisplayColorSpaceName(ConstConfigRcPtr *config, const char *display, const char *view); + +ConstProcessorRcPtr *OCIO_configGetProcessorWithNames(ConstConfigRcPtr *config, const char *srcName, const char *dstName); +ConstProcessorRcPtr *OCIO_configGetProcessor(ConstConfigRcPtr *config, ConstTransformRcPtr *transform); + +void OCIO_processorApply(ConstProcessorRcPtr *processor, PackedImageDesc *img); +void OCIO_processorApply_predivide(ConstProcessorRcPtr *processor, PackedImageDesc *img); +void OCIO_processorApplyRGB(ConstProcessorRcPtr *processor, float *pixel); +void OCIO_processorApplyRGBA(ConstProcessorRcPtr *processor, float *pixel); +void OCIO_processorApplyRGBA_predivide(ConstProcessorRcPtr *processor, float *pixel); + +void OCIO_processorRelease(ConstProcessorRcPtr *p); + +const char *OCIO_colorSpaceGetName(ConstColorSpaceRcPtr *cs); +const char *OCIO_colorSpaceGetDescription(ConstColorSpaceRcPtr *cs); +const char *OCIO_colorSpaceGetFamily(ConstColorSpaceRcPtr *cs); + +DisplayTransformRcPtr *OCIO_createDisplayTransform(void); +void OCIO_displayTransformSetInputColorSpaceName(DisplayTransformRcPtr *dt, const char *name); +void OCIO_displayTransformSetDisplay(DisplayTransformRcPtr *dt, const char *name); +void OCIO_displayTransformSetView(DisplayTransformRcPtr *dt, const char *name); +void OCIO_displayTransformSetDisplayCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *et); +void OCIO_displayTransformSetLinearCC(DisplayTransformRcPtr *dt, ConstTransformRcPtr *et); +void OCIO_displayTransformRelease(DisplayTransformRcPtr *dt); + +PackedImageDesc *OCIO_createPackedImageDesc(float *data, long width, long height, long numChannels, + long chanStrideBytes, long xStrideBytes, long yStrideBytes); + +void OCIO_packedImageDescRelease(PackedImageDesc *p); + +ExponentTransformRcPtr *OCIO_createExponentTransform(void); +void OCIO_exponentTransformSetValue(ExponentTransformRcPtr *et, const float *exponent); +void OCIO_exponentTransformRelease(ExponentTransformRcPtr *et); + +MatrixTransformRcPtr *OCIO_createMatrixTransform(void); +void OCIO_matrixTransformSetValue(MatrixTransformRcPtr *et, const float *m44, const float *offset4); +void OCIO_matrixTransformRelease(MatrixTransformRcPtr *mt); + +void OCIO_matrixTransformScale(float * m44, float * offset4, const float * scale4); + +#ifdef __cplusplus +} +#endif + +#endif //OCIO_CAPI_H diff --git a/intern/opencolorio/ocio_capi_stub.cpp b/intern/opencolorio/ocio_capi_stub.cpp new file mode 100644 index 00000000000..a601cd3be91 --- /dev/null +++ b/intern/opencolorio/ocio_capi_stub.cpp @@ -0,0 +1,380 @@ +/* + * ***** 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 <string.h> + +#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; +} + +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; +} + +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 *) +{ +} + |