2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2012 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): Brecht van Lommel
23 * ***** END GPL LICENSE BLOCK *****
28 #include "MEM_guardedalloc.h"
29 #include "BLI_math_color.h"
31 #include "ocio_impl.h"
33 #define CONFIG_DEFAULT ((OCIO_ConstConfigRcPtr*)1)
35 #define PROCESSOR_LINEAR_TO_SRGB ((OCIO_ConstProcessorRcPtr*)1)
36 #define PROCESSOR_SRGB_TO_LINEAR ((OCIO_ConstProcessorRcPtr*)2)
37 #define PROCESSOR_UNKNOWN ((OCIO_ConstProcessorRcPtr*)3)
39 #define COLORSPACE_LINEAR ((OCIO_ConstColorSpaceRcPtr*)1)
40 #define COLORSPACE_SRGB ((OCIO_ConstColorSpaceRcPtr*)2)
42 typedef struct OCIO_PackedImageDescription {
50 } OCIO_PackedImageDescription;
52 OCIO_ConstConfigRcPtr *FallbackImpl::getCurrentConfig(void)
54 return CONFIG_DEFAULT;
57 void FallbackImpl::setCurrentConfig(const OCIO_ConstConfigRcPtr *)
61 OCIO_ConstConfigRcPtr *FallbackImpl::configCreateFromEnv(void)
63 return CONFIG_DEFAULT;
66 OCIO_ConstConfigRcPtr *FallbackImpl::configCreateFromFile(const char *)
68 return CONFIG_DEFAULT;
71 void FallbackImpl::configRelease(OCIO_ConstConfigRcPtr *)
75 int FallbackImpl::configGetNumColorSpaces(OCIO_ConstConfigRcPtr *)
80 const char *FallbackImpl::configGetColorSpaceNameByIndex(OCIO_ConstConfigRcPtr *, int index)
90 OCIO_ConstColorSpaceRcPtr *FallbackImpl::configGetColorSpace(OCIO_ConstConfigRcPtr *, const char *name)
92 if (strcmp(name, "scene_linear") == 0)
93 return COLORSPACE_LINEAR;
94 else if (strcmp(name, "color_picking") == 0)
95 return COLORSPACE_SRGB;
96 else if (strcmp(name, "texture_paint") == 0)
97 return COLORSPACE_LINEAR;
98 else if (strcmp(name, "default_byte") == 0)
99 return COLORSPACE_SRGB;
100 else if (strcmp(name, "default_float") == 0)
101 return COLORSPACE_LINEAR;
102 else if (strcmp(name, "default_sequencer") == 0)
103 return COLORSPACE_SRGB;
104 else if (strcmp(name, "Linear") == 0)
105 return COLORSPACE_LINEAR;
106 else if (strcmp(name, "sRGB") == 0)
107 return COLORSPACE_SRGB;
112 int FallbackImpl::configGetIndexForColorSpace(OCIO_ConstConfigRcPtr *config, const char *name)
114 OCIO_ConstColorSpaceRcPtr *cs = configGetColorSpace(config, name);
116 if (cs == COLORSPACE_LINEAR)
118 else if (cs == COLORSPACE_SRGB)
124 const char *FallbackImpl::configGetDefaultDisplay(OCIO_ConstConfigRcPtr *)
129 int FallbackImpl::configGetNumDisplays(OCIO_ConstConfigRcPtr* config)
134 const char *FallbackImpl::configGetDisplay(OCIO_ConstConfigRcPtr *, int index)
142 const char *FallbackImpl::configGetDefaultView(OCIO_ConstConfigRcPtr *, const char *)
147 int FallbackImpl::configGetNumViews(OCIO_ConstConfigRcPtr *, const char *)
152 const char *FallbackImpl::configGetView(OCIO_ConstConfigRcPtr *, const char *, int index)
160 const char *FallbackImpl::configGetDisplayColorSpaceName(OCIO_ConstConfigRcPtr *, const char *, const char *)
165 int FallbackImpl::colorSpaceIsInvertible(OCIO_ConstColorSpaceRcPtr *cs)
170 int FallbackImpl::colorSpaceIsData(OCIO_ConstColorSpaceRcPtr *cs)
175 void FallbackImpl::colorSpaceRelease(OCIO_ConstColorSpaceRcPtr *cs)
179 OCIO_ConstProcessorRcPtr *FallbackImpl::configGetProcessorWithNames(OCIO_ConstConfigRcPtr *config, const char *srcName, const char *dstName)
181 OCIO_ConstColorSpaceRcPtr *cs_src = configGetColorSpace(config, srcName);
182 OCIO_ConstColorSpaceRcPtr *cs_dst = configGetColorSpace(config, dstName);
184 if (cs_src == COLORSPACE_LINEAR && cs_dst == COLORSPACE_SRGB)
185 return PROCESSOR_LINEAR_TO_SRGB;
186 else if (cs_src == COLORSPACE_SRGB && cs_dst == COLORSPACE_LINEAR)
187 return PROCESSOR_SRGB_TO_LINEAR;
192 OCIO_ConstProcessorRcPtr *FallbackImpl::configGetProcessor(OCIO_ConstConfigRcPtr *, OCIO_ConstTransformRcPtr *tfm)
194 return (OCIO_ConstProcessorRcPtr*)tfm;
197 void FallbackImpl::processorApply(OCIO_ConstProcessorRcPtr *processor, OCIO_PackedImageDesc *img)
199 /* OCIO_TODO stride not respected, channels must be 3 or 4 */
200 OCIO_PackedImageDescription *desc = (OCIO_PackedImageDescription*)img;
201 int channels = desc->numChannels;
202 float *pixels = desc->data;
203 int width = desc->width;
204 int height = desc->height;
207 for (y = 0; y < height; y++) {
208 for (x = 0; x < width; x++) {
209 float *pixel = pixels + channels * (y * width + x);
212 processorApplyRGBA(processor, pixel);
213 else if (channels == 3)
214 processorApplyRGB(processor, pixel);
219 void FallbackImpl::processorApply_predivide(OCIO_ConstProcessorRcPtr *processor, OCIO_PackedImageDesc *img)
221 /* OCIO_TODO stride not respected, channels must be 3 or 4 */
222 OCIO_PackedImageDescription *desc = (OCIO_PackedImageDescription*)img;
223 int channels = desc->numChannels;
224 float *pixels = desc->data;
225 int width = desc->width;
226 int height = desc->height;
229 for (y = 0; y < height; y++) {
230 for (x = 0; x < width; x++) {
231 float *pixel = pixels + channels * (y * width + x);
234 processorApplyRGBA_predivide(processor, pixel);
235 else if (channels == 3)
236 processorApplyRGB(processor, pixel);
241 void FallbackImpl::processorApplyRGB(OCIO_ConstProcessorRcPtr *processor, float *pixel)
243 if (processor == PROCESSOR_LINEAR_TO_SRGB)
244 linearrgb_to_srgb_v3_v3(pixel, pixel);
245 else if (processor == PROCESSOR_SRGB_TO_LINEAR)
246 srgb_to_linearrgb_v3_v3(pixel, pixel);
249 void FallbackImpl::processorApplyRGBA(OCIO_ConstProcessorRcPtr *processor, float *pixel)
251 if (processor == PROCESSOR_LINEAR_TO_SRGB)
252 linearrgb_to_srgb_v4(pixel, pixel);
253 else if (processor == PROCESSOR_SRGB_TO_LINEAR)
254 srgb_to_linearrgb_v4(pixel, pixel);
257 void FallbackImpl::processorApplyRGBA_predivide(OCIO_ConstProcessorRcPtr *processor, float *pixel)
259 if (pixel[3] == 1.0f || pixel[3] == 0.0f) {
260 processorApplyRGBA(processor, pixel);
263 float alpha, inv_alpha;
266 inv_alpha = 1.0f / alpha;
268 pixel[0] *= inv_alpha;
269 pixel[1] *= inv_alpha;
270 pixel[2] *= inv_alpha;
272 processorApplyRGBA(processor, pixel);
280 void FallbackImpl::processorRelease(OCIO_ConstProcessorRcPtr *)
284 const char *FallbackImpl::colorSpaceGetName(OCIO_ConstColorSpaceRcPtr *cs)
286 if (cs == COLORSPACE_LINEAR)
288 else if (cs == COLORSPACE_SRGB)
294 const char *FallbackImpl::colorSpaceGetDescription(OCIO_ConstColorSpaceRcPtr *)
299 const char *FallbackImpl::colorSpaceGetFamily(OCIO_ConstColorSpaceRcPtr *)
304 OCIO_DisplayTransformRcPtr *FallbackImpl::createDisplayTransform(void)
306 return (OCIO_DisplayTransformRcPtr*)PROCESSOR_LINEAR_TO_SRGB;
309 void FallbackImpl::displayTransformSetInputColorSpaceName(OCIO_DisplayTransformRcPtr *, const char *)
313 void FallbackImpl::displayTransformSetDisplay(OCIO_DisplayTransformRcPtr *, const char *)
317 void FallbackImpl::displayTransformSetView(OCIO_DisplayTransformRcPtr *, const char *)
321 void FallbackImpl::displayTransformSetDisplayCC(OCIO_DisplayTransformRcPtr *, OCIO_ConstTransformRcPtr *)
325 void FallbackImpl::displayTransformSetLinearCC(OCIO_DisplayTransformRcPtr *, OCIO_ConstTransformRcPtr *)
329 void FallbackImpl::displayTransformRelease(OCIO_DisplayTransformRcPtr *)
333 OCIO_PackedImageDesc *FallbackImpl::createOCIO_PackedImageDesc(float *data, long width, long height, long numChannels,
334 long chanStrideBytes, long xStrideBytes, long yStrideBytes)
336 OCIO_PackedImageDescription *desc = (OCIO_PackedImageDescription*)MEM_callocN(sizeof(OCIO_PackedImageDescription), "OCIO_PackedImageDescription");
340 desc->height = height;
341 desc->numChannels = numChannels;
342 desc->chanStrideBytes = chanStrideBytes;
343 desc->xStrideBytes = xStrideBytes;
344 desc->yStrideBytes = yStrideBytes;
346 return (OCIO_PackedImageDesc*)desc;
349 void FallbackImpl::OCIO_PackedImageDescRelease(OCIO_PackedImageDesc* id)
354 OCIO_ExponentTransformRcPtr *FallbackImpl::createExponentTransform(void)
356 return (OCIO_ExponentTransformRcPtr*)PROCESSOR_UNKNOWN;
359 void FallbackImpl::exponentTransformSetValue(OCIO_ExponentTransformRcPtr *, const float *)
363 void FallbackImpl::exponentTransformRelease(OCIO_ExponentTransformRcPtr *)
367 OCIO_MatrixTransformRcPtr *FallbackImpl::createMatrixTransform(void)
369 return (OCIO_MatrixTransformRcPtr*)PROCESSOR_UNKNOWN;
372 void FallbackImpl::matrixTransformSetValue(OCIO_MatrixTransformRcPtr *, const float *, const float *)
376 void FallbackImpl::matrixTransformRelease(OCIO_MatrixTransformRcPtr *)
380 void FallbackImpl::matrixTransformScale(float * , float * , const float *)