-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
681 lines (578 loc) · 17.8 KB
/
config.go
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
package ocio
// #include <stdlib.h>
// #include <string.h>
//
// #include "ocio.h"
//
import "C"
import (
"errors"
"fmt"
"reflect"
"runtime"
"unsafe"
)
// A Config defines all the colorspaces available at runtime.
type Config struct {
ptr *C.Config
}
/*
Config Initialization
*/
func newConfig(p *C.Config) *Config {
cfg := &Config{p}
runtime.SetFinalizer(cfg, deleteConfig)
return cfg
}
func deleteConfig(c *Config) {
if c == nil {
return
}
if c.ptr != nil {
runtime.SetFinalizer(c, nil)
C.deleteConfig(c.ptr)
c.ptr = nil
}
runtime.KeepAlive(c)
}
// Create a new empty Config
func NewConfig() *Config {
return newConfig(C.Config_Create())
}
// Get the current configuration.
// If a current config had not yet been set, it will be automatically
// initialized from the environment.
func CurrentConfig() (*Config, error) {
c, err := C.GetCurrentConfig()
if err = getLastError((*C._HandleContext)(c), err); err != nil {
return nil, err
}
return newConfig(c), nil
}
// Set the current configuration. This will then store a copy of the specified config.
func SetCurrentConfig(config *Config) error {
_, err := C.SetCurrentConfig(config.ptr)
runtime.KeepAlive(config)
return err
}
// Create a Config by checking the OCIO environment variable
func ConfigCreateFromEnv() (*Config, error) {
c, err := C.Config_CreateFromEnv()
if err = getLastError((*C._HandleContext)(c), err); err != nil {
return nil, err
}
return newConfig(c), nil
}
// Create a Config from an existing yaml Config file
func ConfigCreateFromFile(filename string) (*Config, error) {
c_str := C.CString(filename)
defer C.free(unsafe.Pointer(c_str))
c, err := C.Config_CreateFromFile(c_str)
if err = getLastError((*C._HandleContext)(c), err); err != nil {
return nil, err
}
return newConfig(c), nil
}
// Create a Config from a valid yaml string
func ConfigCreateFromData(data string) (*Config, error) {
c_str := C.CString(data)
defer C.free(unsafe.Pointer(c_str))
c, err := C.Config_CreateFromData(c_str)
if err = getLastError((*C._HandleContext)(c), err); err != nil {
return nil, err
}
return newConfig(c), nil
}
// Destroy immediately frees resources for this
// instance instead of waiting for garbage collection
// finalizer to run at some point later
func (c *Config) Destroy() {
deleteConfig(c)
}
func (c *Config) lastError(errno ...error) error {
if c == nil {
return nil
}
err := getLastError(c.ptr, errno...)
runtime.KeepAlive(c)
return err
}
// Create a new editable copy of this Config
func (c *Config) EditableCopy() *Config {
ret := newConfig(C.Config_createEditableCopy(c.ptr))
runtime.KeepAlive(c)
return ret
}
// This will return a non-nil error if the config is malformed.
// The most common error occurs when references are made to colorspaces that do not exist.
func (c *Config) SanityCheck() error {
_, err := C.Config_sanityCheck(c.ptr)
err = c.lastError(err)
runtime.KeepAlive(c)
return err
}
func (c *Config) Serialize() (string, error) {
c_str, err := C.Config_serialize(c.ptr)
if err = c.lastError(err); err != nil {
return "", err
}
defer C.free(unsafe.Pointer(c_str))
runtime.KeepAlive(c)
return C.GoString(c_str), nil
}
/*
This will produce a hash of the all colorspace definitions, etc.
All external references, such as files used in FileTransforms, etc.,
will be incorporated into the cacheID. While the contents of the files
are not read, the file system is queried for relavent information (mtime, inode)
so that the Config’s cacheID will change when the underlying luts are updated.
The current Context will be used.
*/
func (c *Config) CacheID() (string, error) {
id, err := C.Config_getCacheID(c.ptr)
if err = c.lastError(err); err != nil {
return "", err
}
runtime.KeepAlive(c)
return C.GoString(id), nil
}
/*
This will produce a hash of the all colorspace definitions, etc.
All external references, such as files used in FileTransforms, etc.,
will be incorporated into the cacheID. While the contents of the files
are not read, the file system is queried for relavent information (mtime, inode)
so that the Config’s cacheID will change when the underlying luts are updated.
If a nil context is provided, file references will not be taken into account
(this is essentially a hash of Config.Serialize).
*/
func (c *Config) CacheIDWithContext(context *Context) (string, error) {
if context == nil {
context = NewContext()
}
id, err := C.Config_getCacheIDWithContext(c.ptr, context.ptr)
if err = c.lastError(err); err != nil {
return "", err
}
runtime.KeepAlive(c)
runtime.KeepAlive(context)
return C.GoString(id), nil
}
func (c *Config) Description() (string, error) {
d, err := C.Config_getDescription(c.ptr)
if err = c.lastError(err); err != nil {
return "", err
}
runtime.KeepAlive(c)
return C.GoString(d), nil
}
func (c *Config) IsStrictParsingEnabled() bool {
enabled, err := C.Config_isStrictParsingEnabled(c.ptr)
if err = c.lastError(err); err != nil {
return false
}
runtime.KeepAlive(c)
return bool(enabled)
}
func (c *Config) SetStrictParsingEnabled(enabled bool) error {
_, err := C.Config_setStrictParsingEnabled(c.ptr, C.bool(enabled))
err = c.lastError(err)
runtime.KeepAlive(c)
return err
}
/*
Config Resources
*/
func (c *Config) CurrentContext() (*Context, error) {
ptr, err := C.Config_getCurrentContext(c.ptr)
if err = c.lastError(err); err != nil {
return nil, err
}
runtime.KeepAlive(c)
return newContext(ptr), nil
}
// Given a lut src name, where should we find it?
func (c *Config) SearchPath() (string, error) {
path, err := C.Config_getSearchPath(c.ptr)
if err = c.lastError(err); err != nil {
return "", err
}
runtime.KeepAlive(c)
return C.GoString(path), nil
}
// Given a lut src name, where should we find it?
func (c *Config) WorkingDir() (string, error) {
dir, err := C.Config_getWorkingDir(c.ptr)
if err = c.lastError(err); err != nil {
return "", err
}
runtime.KeepAlive(c)
return C.GoString(dir), nil
}
/*
Config Processors
*/
/*
Convert from inputColorSpace to outputColorSpace
There are 4 ways this method may be called:
config.Processor(ctx *Context, src *ColorSpace, dst *ColorSpace)
config.Processor(ctx *Context, src string, dst string)
config.Processor(src *ColorSpace, dst *ColorSpace)
config.Processor(src string, dst string)
String names can be colorspace name, role name, or a combination of both.
This may provide higher fidelity than anticipated due to internal optimizations.
For example, if the inputColorspace and the outputColorSpace are members of the
same family, no conversion will be applied, even though strictly speaking
quantization should be added
*/
func (c *Config) Processor(args ...interface{}) (*Processor, error) {
count := len(args)
if count != 2 && count != 3 {
return nil, fmt.Errorf("Requires either 2 or 3 parameters; Got %d", count)
}
var (
err error
proc C.ProcessorId
)
bad_str := "Error creating processor with src colorspace %v / dst colorspace %v"
if count == 3 {
ct, ok := args[0].(*Context)
if !ok {
return nil, errors.New("1st argument is not a Context*")
}
a1 := reflect.ValueOf(args[1])
a2 := reflect.ValueOf(args[2])
if a1.Kind() == reflect.String && a2.Kind() == reflect.String {
c_a1 := C.CString(a1.String())
c_a2 := C.CString(a2.String())
defer C.free(unsafe.Pointer(c_a1))
defer C.free(unsafe.Pointer(c_a2))
proc, err = C.Config_getProcessor_CT_S_S(c.ptr, ct.ptr, c_a1, c_a2)
if err = c.lastError(err); err != nil {
err = fmt.Errorf("%s: %s", fmt.Sprintf(bad_str, a1, a2), err)
}
return newProcessor(proc), err
}
if a1.Kind() == reflect.Ptr && a2.Kind() == reflect.Ptr {
if aPtr1, ok := args[1].(*ColorSpace); ok {
if aPtr2, ok := args[2].(*ColorSpace); ok {
proc, err = C.Config_getProcessor_CT_CS_CS(c.ptr, ct.ptr, aPtr1.ptr, aPtr2.ptr)
if err = c.lastError(err); err != nil {
err = fmt.Errorf("%s: %s", fmt.Sprintf(bad_str, a1, a2), err)
}
return newProcessor(proc), err
}
}
}
} else if count == 2 {
a1 := reflect.ValueOf(args[0])
a2 := reflect.ValueOf(args[1])
if a1.Kind() == reflect.String && a2.Kind() == reflect.String {
c_a1 := C.CString(a1.String())
c_a2 := C.CString(a2.String())
defer C.free(unsafe.Pointer(c_a1))
defer C.free(unsafe.Pointer(c_a2))
proc, err = C.Config_getProcessor_S_S(c.ptr, c_a1, c_a2)
if err = c.lastError(err); err != nil {
err = fmt.Errorf("%s: %s", fmt.Sprintf(bad_str, a1, a2), err)
}
return newProcessor(proc), err
}
if a1.Kind() == reflect.Ptr && a2.Kind() == reflect.Ptr {
if aPtr1, ok := args[0].(*ColorSpace); ok {
if aPtr2, ok := args[1].(*ColorSpace); ok {
proc, err = C.Config_getProcessor_CS_CS(c.ptr, aPtr1.ptr, aPtr2.ptr)
if err = c.lastError(err); err != nil {
err = fmt.Errorf("%s: %s", fmt.Sprintf(bad_str, a1, a2), err)
}
return newProcessor(proc), err
}
}
}
}
runtime.KeepAlive(c)
return nil, fmt.Errorf("Wrong argument types: %v", args)
}
/*
Get the processor for the specified transform, using the current Config context.
Not often needed, but will allow for the re-use of atomic OCIO functionality
(such as to apply an individual LUT file).
*/
func (c *Config) ProcessorTransform(tx Transform) (*Processor, error) {
ptr, err := C.Config_getProcessor_TX(c.ptr, tx.transformHandle())
if err = c.lastError(err); err != nil {
return nil, err
}
proc := newProcessor(ptr)
runtime.KeepAlive(c)
runtime.KeepAlive(tx)
return proc, nil
}
/*
Get the processor for the specified transform, and a transform direction,
using the current Config context.
Not often needed, but will allow for the re-use of atomic OCIO functionality
(such as to apply an individual LUT file).
*/
func (c *Config) ProcessorTransformDir(tx Transform, dir TransformDirection) (*Processor, error) {
ptr, err := C.Config_getProcessor_TX_D(
c.ptr, tx.transformHandle(), C.TransformDirection(dir))
if err = c.lastError(err); err != nil {
return nil, err
}
proc := newProcessor(ptr)
runtime.KeepAlive(c)
runtime.KeepAlive(tx)
return proc, nil
}
/*
Get the processor for the specified transform, using a specific Context instead of
the current Config context, and a transform direction.
Not often needed, but will allow for the re-use of atomic OCIO functionality
(such as to apply an individual LUT file).
*/
func (c *Config) ProcessorCtxTransformDir(
ctx *Context, tx Transform, dir TransformDirection) (*Processor, error) {
ptr, err := C.Config_getProcessor_CT_TX_D(
c.ptr, ctx.ptr, tx.transformHandle(), C.TransformDirection(dir))
if err = c.lastError(err); err != nil {
return nil, err
}
proc := newProcessor(ptr)
runtime.KeepAlive(c)
runtime.KeepAlive(ctx)
runtime.KeepAlive(tx)
return proc, nil
}
/*
Config ColorSpaces
*/
// This will return null if the specified name is not found.
// Accepts either a color space OR role name. (Colorspace names take precedence over roles.)
func (c *Config) ColorSpace(name string) (*ColorSpace, error) {
c_str := C.CString(name)
defer C.free(unsafe.Pointer(c_str))
cs, err := C.Config_getColorSpace(c.ptr, c_str)
if err = c.lastError(err); err != nil {
err = fmt.Errorf("%q is not a valid ColorSpace: %v", name, err)
return nil, err
}
if cs == 0 {
return nil, fmt.Errorf("%q is not a valid ColorSpace", name)
}
runtime.KeepAlive(c)
return newColorSpace(cs), nil
}
func (c *Config) NumColorSpaces() int {
num, err := C.Config_getNumColorSpaces(c.ptr)
if err != nil {
return 0
}
runtime.KeepAlive(c)
return int(num)
}
// This will null if an invalid index is specified
func (c *Config) ColorSpaceNameByIndex(index int) (string, error) {
name, err := C.Config_getColorSpaceNameByIndex(c.ptr, C.int(index))
if err = c.lastError(err); err != nil {
return "", err
}
runtime.KeepAlive(c)
return C.GoString(name), nil
}
// Accepts either a color space OR role name. (Colorspace names take precedence over roles.)
func (c *Config) IndexForColorSpace(name string) (int, error) {
c_str := C.CString(name)
defer C.free(unsafe.Pointer(c_str))
idx, err := C.Config_getIndexForColorSpace(c.ptr, c_str)
if err = c.lastError(err); err != nil {
return -1, err
}
runtime.KeepAlive(c)
return int(idx), nil
}
// If another color space is already registered with the same name, this will overwrite it.
// This stores a copy of the specified color space.
func (c *Config) AddColorSpace(cs *ColorSpace) error {
_, err := C.Config_addColorSpace(c.ptr, cs.ptr)
err = c.lastError(err)
runtime.KeepAlive(c)
return err
}
func (c *Config) ClearColorSpaces() error {
_, err := C.Config_clearColorSpaces(c.ptr)
err = c.lastError(err)
runtime.KeepAlive(c)
return err
}
/*
Given the specified string, get the longest, right-most, colorspace substring that appears.
* If strict parsing is enabled, and no color space is found, return an empty string.
* If strict parsing is disabled, return ROLE_DEFAULT (if defined).
* If the default role is not defined, return an empty string.
*/
func (c *Config) ParseColorSpaceFromString(str string) (string, error) {
c_str := C.CString(str)
defer C.free(unsafe.Pointer(c_str))
name, err := C.Config_parseColorSpaceFromString(c.ptr, c_str)
if err = c.lastError(err); err != nil {
return "", err
}
runtime.KeepAlive(c)
return C.GoString(name), nil
}
/*
Config Roles
A role is like an alias for a colorspace. You can query the colorspace
corresponding to a role using the normal getColorSpace fcn.
*/
// Setting the colorSpaceName name to a null string unsets it.
func (c *Config) SetRole(role, colorSpaceName string) error {
c_role := C.CString(role)
defer C.free(unsafe.Pointer(c_role))
var c_space *C.char
if colorSpaceName != "" {
c_space = C.CString(colorSpaceName)
defer C.free(unsafe.Pointer(c_space))
}
_, err := C.Config_setRole(c.ptr, c_role, c_space)
err = c.lastError(err)
runtime.KeepAlive(c)
return err
}
func (c *Config) NumRoles() int {
num, err := C.Config_getNumRoles(c.ptr)
if err = c.lastError(err); err != nil {
return 0
}
runtime.KeepAlive(c)
return int(num)
}
// Return true if the role has been defined.
func (c *Config) HasRole(role string) bool {
c_str := C.CString(role)
defer C.free(unsafe.Pointer(c_str))
has, err := C.Config_hasRole(c.ptr, c_str)
if err = c.lastError(err); err != nil {
return false
}
runtime.KeepAlive(c)
return bool(has)
}
// Get the role name at index, this will return values like ‘scene_linear’,
// ‘compositing_log’. Return empty string if index is out of range.
func (c *Config) RoleName(index int) (string, error) {
name, err := C.Config_getRoleName(c.ptr, C.int(index))
if err = c.lastError(err); err != nil {
return "", err
}
runtime.KeepAlive(c)
return C.GoString(name), nil
}
/*
Config Display/View Registration
*/
// Looks is a potentially comma (or colon) delimited list of lookNames,
// Where +/- prefixes are optionally allowed to denote forward/inverse
// look specification. (And forward is assumed in the absense of either)
func (c *Config) DisplayLooks(display, view string) string {
c_disp := C.CString(display)
c_view := C.CString(view)
defer C.free(unsafe.Pointer(c_disp))
defer C.free(unsafe.Pointer(c_view))
ret := C.GoString(C.Config_getDisplayLooks(c.ptr, c_disp, c_view))
runtime.KeepAlive(c)
return ret
}
func (c *Config) DefaultDisplay() string {
ret := C.GoString(C.Config_getDefaultDisplay(c.ptr))
runtime.KeepAlive(c)
return ret
}
func (c *Config) NumDisplays() int {
ret := int(C.Config_getNumDisplays(c.ptr))
runtime.KeepAlive(c)
return ret
}
func (c *Config) Display(index int) string {
ret := C.GoString(C.Config_getDisplay(c.ptr, C.int(index)))
runtime.KeepAlive(c)
return ret
}
func (c *Config) DefaultView(display string) string {
c_disp := C.CString(display)
defer C.free(unsafe.Pointer(c_disp))
ret := C.GoString(C.Config_getDefaultView(c.ptr, c_disp))
runtime.KeepAlive(c)
return ret
}
func (c *Config) NumViews(display string) int {
c_disp := C.CString(display)
defer C.free(unsafe.Pointer(c_disp))
ret := int(C.Config_getNumViews(c.ptr, c_disp))
runtime.KeepAlive(c)
return ret
}
func (c *Config) View(display string, index int) string {
c_disp := C.CString(display)
defer C.free(unsafe.Pointer(c_disp))
ret := C.GoString(C.Config_getView(c.ptr, c_disp, C.int(index)))
runtime.KeepAlive(c)
return ret
}
func (c *Config) DisplayColorSpaceName(display, view string) string {
c_disp := C.CString(display)
c_view := C.CString(view)
defer C.free(unsafe.Pointer(c_disp))
defer C.free(unsafe.Pointer(c_view))
ret := C.GoString(C.Config_getDisplayColorSpaceName(c.ptr, c_disp, c_view))
runtime.KeepAlive(c)
return ret
}
// For the (display,view) combination, specify which colorSpace and look to use.
// If a look is not desired, then just pass an empty string
func (c *Config) AddDisplay(display, view, colorSpace, looks string) error {
c_disp := C.CString(display)
c_view := C.CString(view)
c_cs := C.CString(colorSpace)
c_looks := C.CString(looks)
defer C.free(unsafe.Pointer(c_disp))
defer C.free(unsafe.Pointer(c_view))
defer C.free(unsafe.Pointer(c_cs))
defer C.free(unsafe.Pointer(c_looks))
_, err := C.Config_addDisplay(c.ptr, c_disp, c_view, c_cs, c_looks)
err = c.lastError(err)
runtime.KeepAlive(c)
return err
}
func (c *Config) ClearDisplays() {
C.Config_clearDisplays(c.ptr)
runtime.KeepAlive(c)
}
// Comma-delimited list of display names.
func (c *Config) SetActiveDisplays(displays string) error {
c_disp := C.CString(displays)
defer C.free(unsafe.Pointer(c_disp))
_, err := C.Config_setActiveDisplays(c.ptr, c_disp)
err = c.lastError(err)
runtime.KeepAlive(c)
return err
}
func (c *Config) ActiveDisplays() string {
ret := C.GoString(C.Config_getActiveDisplays(c.ptr))
runtime.KeepAlive(c)
return ret
}
// Comma-delimited list of view names.
func (c *Config) SetActiveViews(views string) error {
c_view := C.CString(views)
defer C.free(unsafe.Pointer(c_view))
_, err := C.Config_setActiveViews(c.ptr, c_view)
err = c.lastError(err)
runtime.KeepAlive(c)
return err
}
func (c *Config) ActiveViews() string {
ret := C.GoString(C.Config_getActiveViews(c.ptr))
runtime.KeepAlive(c)
return ret
}