1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
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 <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) 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;
}
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);
}
|