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 namespace OCIO_NAMESPACE {};
33 #include "ocio_capi.h"
35 #define CONFIG_DEFAULT ((ConstConfigRcPtr*)1)
37 #define PROCESSOR_LINEAR_TO_SRGB ((ConstProcessorRcPtr*)1)
38 #define PROCESSOR_SRGB_TO_LINEAR ((ConstProcessorRcPtr*)2)
39 #define PROCESSOR_UNKNOWN ((ConstProcessorRcPtr*)3)
41 #define COLORSPACE_LINEAR ((ConstColorSpaceRcPtr*)1)
42 #define COLORSPACE_SRGB ((ConstColorSpaceRcPtr*)2)
44 typedef struct PackedImageDescription {
52 } PackedImageDescription;
54 ConstConfigRcPtr *OCIO_getCurrentConfig(void)
56 return CONFIG_DEFAULT;
59 void OCIO_setCurrentConfig(const ConstConfigRcPtr *)
63 ConstConfigRcPtr *OCIO_configCreateFromEnv(void)
65 return CONFIG_DEFAULT;
68 ConstConfigRcPtr *OCIO_configCreateFromFile(const char *)
70 return CONFIG_DEFAULT;
73 void OCIO_configRelease(ConstConfigRcPtr *)
77 int OCIO_configGetNumColorSpaces(ConstConfigRcPtr *)
82 const char *OCIO_configGetColorSpaceNameByIndex(ConstConfigRcPtr *, int index)
92 ConstColorSpaceRcPtr *OCIO_configGetColorSpace(ConstConfigRcPtr *, const char *name)
94 if (strcmp(name, "scene_linear") == 0)
95 return COLORSPACE_LINEAR;
96 else if (strcmp(name, "color_picking") == 0)
97 return COLORSPACE_SRGB;
98 else if (strcmp(name, "texture_paint") == 0)
99 return COLORSPACE_LINEAR;
100 else if (strcmp(name, "default_byte") == 0)
101 return COLORSPACE_SRGB;
102 else if (strcmp(name, "default_float") == 0)
103 return COLORSPACE_LINEAR;
104 else if (strcmp(name, "default_sequencer") == 0)
105 return COLORSPACE_SRGB;
106 else if (strcmp(name, "Linear") == 0)
107 return COLORSPACE_LINEAR;
108 else if (strcmp(name, "sRGB") == 0)
109 return COLORSPACE_SRGB;
114 int OCIO_configGetIndexForColorSpace(ConstConfigRcPtr *config, const char *name)
116 ConstColorSpaceRcPtr *cs = OCIO_configGetColorSpace(config, name);
118 if (cs == COLORSPACE_LINEAR)
120 else if (cs == COLORSPACE_SRGB)
126 const char *OCIO_configGetDefaultDisplay(ConstConfigRcPtr *)
131 int OCIO_configGetNumDisplays(ConstConfigRcPtr* config)
136 const char *OCIO_configGetDisplay(ConstConfigRcPtr *, int index)
144 const char *OCIO_configGetDefaultView(ConstConfigRcPtr *, const char *)
149 int OCIO_configGetNumViews(ConstConfigRcPtr *, const char *)
154 const char *OCIO_configGetView(ConstConfigRcPtr *, const char *, int index)
162 const char *OCIO_configGetDisplayColorSpaceName(ConstConfigRcPtr *, const char *, const char *)
167 int OCIO_colorSpaceIsInvertible(ConstColorSpaceRcPtr *cs)
172 void OCIO_colorSpaceRelease(ConstColorSpaceRcPtr *cs)
176 ConstProcessorRcPtr *OCIO_configGetProcessorWithNames(ConstConfigRcPtr *config, const char *srcName, const char *dstName)
178 ConstColorSpaceRcPtr *cs_src = OCIO_configGetColorSpace(config, srcName);
179 ConstColorSpaceRcPtr *cs_dst = OCIO_configGetColorSpace(config, dstName);
181 if (cs_src == COLORSPACE_LINEAR && cs_dst == COLORSPACE_SRGB)
182 return PROCESSOR_LINEAR_TO_SRGB;
183 else if (cs_src == COLORSPACE_SRGB && cs_dst == COLORSPACE_LINEAR)
184 return PROCESSOR_SRGB_TO_LINEAR;
189 ConstProcessorRcPtr *OCIO_configGetProcessor(ConstConfigRcPtr *, ConstTransformRcPtr *tfm)
191 return (ConstProcessorRcPtr*)tfm;
194 void OCIO_processorApply(ConstProcessorRcPtr *processor, PackedImageDesc *img)
196 /* OCIO_TODO stride not respected, channels must be 3 or 4 */
197 PackedImageDescription *desc = (PackedImageDescription*)img;
198 int channels = desc->numChannels;
199 float *pixels = desc->data;
200 int width = desc->width;
201 int height = desc->height;
204 for (y = 0; y < height; y++) {
205 for (x = 0; x < width; x++) {
206 float *pixel = pixels + channels * (y * width + x);
209 OCIO_processorApplyRGBA(processor, pixel);
210 else if (channels == 3)
211 OCIO_processorApplyRGB(processor, pixel);
216 void OCIO_processorApply_predivide(ConstProcessorRcPtr *processor, PackedImageDesc *img)
218 /* OCIO_TODO stride not respected, channels must be 3 or 4 */
219 PackedImageDescription *desc = (PackedImageDescription*)img;
220 int channels = desc->numChannels;
221 float *pixels = desc->data;
222 int width = desc->width;
223 int height = desc->height;
226 for (y = 0; y < height; y++) {
227 for (x = 0; x < width; x++) {
228 float *pixel = pixels + channels * (y * width + x);
231 OCIO_processorApplyRGBA_predivide(processor, pixel);
232 else if (channels == 3)
233 OCIO_processorApplyRGB(processor, pixel);
238 void OCIO_processorApplyRGB(ConstProcessorRcPtr *processor, float *pixel)
240 if (processor == PROCESSOR_LINEAR_TO_SRGB)
241 linearrgb_to_srgb_v3_v3(pixel, pixel);
242 else if (processor == PROCESSOR_SRGB_TO_LINEAR)
243 srgb_to_linearrgb_v3_v3(pixel, pixel);
246 void OCIO_processorApplyRGBA(ConstProcessorRcPtr *processor, float *pixel)
248 if (processor == PROCESSOR_LINEAR_TO_SRGB)
249 linearrgb_to_srgb_v4(pixel, pixel);
250 else if (processor == PROCESSOR_SRGB_TO_LINEAR)
251 srgb_to_linearrgb_v4(pixel, pixel);
254 void OCIO_processorApplyRGBA_predivide(ConstProcessorRcPtr *processor, float *pixel)
256 if (pixel[3] == 1.0f || pixel[3] == 0.0f) {
257 OCIO_processorApplyRGBA(processor, pixel);
260 float alpha, inv_alpha;
263 inv_alpha = 1.0f / alpha;
265 pixel[0] *= inv_alpha;
266 pixel[1] *= inv_alpha;
267 pixel[2] *= inv_alpha;
269 OCIO_processorApplyRGBA(processor, pixel);
277 void OCIO_processorRelease(ConstProcessorRcPtr *)
281 const char *OCIO_colorSpaceGetName(ConstColorSpaceRcPtr *cs)
283 if (cs == COLORSPACE_LINEAR)
285 else if (cs == COLORSPACE_SRGB)
291 const char *OCIO_colorSpaceGetDescription(ConstColorSpaceRcPtr *)
296 const char *OCIO_colorSpaceGetFamily(ConstColorSpaceRcPtr *)
301 DisplayTransformRcPtr *OCIO_createDisplayTransform(void)
303 return (DisplayTransformRcPtr*)PROCESSOR_LINEAR_TO_SRGB;
306 void OCIO_displayTransformSetInputColorSpaceName(DisplayTransformRcPtr *, const char *)
310 void OCIO_displayTransformSetDisplay(DisplayTransformRcPtr *, const char *)
314 void OCIO_displayTransformSetView(DisplayTransformRcPtr *, const char *)
318 void OCIO_displayTransformSetDisplayCC(DisplayTransformRcPtr *, ConstTransformRcPtr *)
322 void OCIO_displayTransformSetLinearCC(DisplayTransformRcPtr *, ConstTransformRcPtr *)
326 void OCIO_displayTransformRelease(DisplayTransformRcPtr *)
330 PackedImageDesc *OCIO_createPackedImageDesc(float *data, long width, long height, long numChannels,
331 long chanStrideBytes, long xStrideBytes, long yStrideBytes)
333 PackedImageDescription *desc = (PackedImageDescription*)MEM_callocN(sizeof(PackedImageDescription), "PackedImageDescription");
337 desc->height = height;
338 desc->numChannels = numChannels;
339 desc->chanStrideBytes = chanStrideBytes;
340 desc->xStrideBytes = xStrideBytes;
341 desc->yStrideBytes = yStrideBytes;
343 return (PackedImageDesc*)desc;
346 void OCIO_packedImageDescRelease(PackedImageDesc* id)
351 ExponentTransformRcPtr *OCIO_createExponentTransform(void)
353 return (ExponentTransformRcPtr*)PROCESSOR_UNKNOWN;
356 void OCIO_exponentTransformSetValue(ExponentTransformRcPtr *, const float *)
360 void OCIO_exponentTransformRelease(ExponentTransformRcPtr *)
364 MatrixTransformRcPtr *OCIO_createMatrixTransform(void)
366 return (MatrixTransformRcPtr*)PROCESSOR_UNKNOWN;
369 void OCIO_matrixTransformSetValue(MatrixTransformRcPtr *, const float *, const float *)
373 void OCIO_matrixTransformRelease(MatrixTransformRcPtr *)
377 void OCIO_matrixTransformScale(float * , float * , const float *)