-
Notifications
You must be signed in to change notification settings - Fork 5
/
color.go
211 lines (185 loc) · 6.29 KB
/
color.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
package oiio
/*
#include "stdlib.h"
#include "color.h"
*/
import "C"
import (
"errors"
"runtime"
"unsafe"
)
// The ColorProcessor encapsulates a baked color transformation, suitable for
// application to raw pixels, or ImageBuf(s). These are generated using
// ColorConfig.CreateColorProcessor, and referenced in ImageBufAlgo
// (amongst other places)
type ColorProcessor struct {
ptr unsafe.Pointer
}
func newColorProcessor(i unsafe.Pointer) *ColorProcessor {
in := &ColorProcessor{i}
runtime.SetFinalizer(in, deleteColorProcessor)
return in
}
func deleteColorProcessor(i *ColorProcessor) {
if i.ptr != nil {
C.deleteColorProcessor(i.ptr)
i.ptr = nil
}
runtime.KeepAlive(i)
}
// Destroy the object immediately instead of waiting for GC.
func (c *ColorProcessor) Destroy() {
runtime.SetFinalizer(c, nil)
deleteColorProcessor(c)
}
// Represents the set of all color transformations that are allowed.
// If OpenColorIO is enabled at build time, this configuration is loaded
// at runtime, allowing the user to have complete control of all color
// transformation math. ($OCIO) (See opencolorio.org for details).
// If OpenColorIO is not enabled at build time, a generic color configuration
// is provided for minimal color support.
//
// NOTE: ColorConfig(s) and ColorProcessor(s) are potentially heavy-weight.
// Their construction / destruction should be kept to a minimum.
type ColorConfig struct {
ptr unsafe.Pointer
}
func newColorConfig(i unsafe.Pointer) *ColorConfig {
in := &ColorConfig{i}
runtime.SetFinalizer(in, deleteColorConfig)
return in
}
func deleteColorConfig(i *ColorConfig) {
if i.ptr != nil {
C.deleteColorConfig(i.ptr)
i.ptr = nil
}
runtime.KeepAlive(i)
}
// Return if OpenImageIO was built with OCIO support
func SupportsOpenColorIO() bool {
return bool(C.supportsOpenColorIO())
}
// If OpenColorIO is enabled at build time, initialize with the current
// color configuration. ($OCIO)
// If OpenColorIO is not enabled, this does nothing.
//
// Multiple calls to this are inexpensive.
func NewColorConfig() (*ColorConfig, error) {
c := newColorConfig(C.New_ColorConfig())
return c, c.error()
}
// If OpenColorIO is enabled at build time, initialize with the
// specified color configuration (.ocio) file
// If OpenColorIO is not enabled, this will result in an error.
//
// Multiple calls to this are potentially expensive.
func NewColorConfigPath(path string) (*ColorConfig, error) {
c_str := C.CString(path)
defer C.free(unsafe.Pointer(c_str))
c := newColorConfig(C.New_ColorConfigPath(c_str))
return c, c.error()
}
// Destroy the object immediately instead of waiting for GC.
func (c *ColorConfig) Destroy() {
runtime.SetFinalizer(c, nil)
deleteColorConfig(c)
}
// Get the number of ColorSpace(s) defined in this configuration
func (c *ColorConfig) NumColorSpaces() int {
ret := int(C.ColorConfig_getNumColorSpaces(c.ptr))
runtime.KeepAlive(c)
return ret
}
// Return the name of the colorspace at a given index
func (c *ColorConfig) ColorSpaceNameByIndex(index int) string {
ret := C.GoString(C.ColorConfig_getColorSpaceNameByIndex(c.ptr, C.int(index)))
runtime.KeepAlive(c)
return ret
}
// Get the number of Looks defined in this configuration
func (c *ColorConfig) NumLooks() int {
ret := int(C.ColorConfig_getNumLooks(c.ptr))
runtime.KeepAlive(c)
return ret
}
// Return the name of the look at a given index
func (c *ColorConfig) LookNameByIndex(index int) string {
ret := C.GoString(C.ColorConfig_getLookNameByIndex(c.ptr, C.int(index)))
runtime.KeepAlive(c)
return ret
}
// Get the number of displays defined in this configuration
func (c *ColorConfig) NumDisplays() int {
ret := int(C.ColorConfig_getNumDisplays(c.ptr))
runtime.KeepAlive(c)
return ret
}
// Return the name of the display at a given index
func (c *ColorConfig) DisplayNameByIndex(index int) string {
ret := C.GoString(C.ColorConfig_getDisplayNameByIndex(c.ptr, C.int(index)))
runtime.KeepAlive(c)
return ret
}
// Get the number of displays defined in this configuration
func (c *ColorConfig) NumViews(displayName string) int {
c_str := C.CString(displayName)
defer C.free(unsafe.Pointer(c_str))
ret := int(C.ColorConfig_getNumViews(c.ptr, c_str))
runtime.KeepAlive(c)
return ret
}
// Get the name of a view at a specific index of a display
func (c *ColorConfig) ViewNameByIndex(displayName string, index int) string {
c_str := C.CString(displayName)
defer C.free(unsafe.Pointer(c_str))
ret := C.GoString(C.ColorConfig_getViewNameByIndex(c.ptr, c_str, C.int(index)))
runtime.KeepAlive(c)
return ret
}
// Get the name of the color space representing the named role,
// or empty string if none could be identified.
func (c *ColorConfig) ColorSpaceNameByRole(role string) string {
c_str := C.CString(role)
defer C.free(unsafe.Pointer(c_str))
ret := C.GoString(C.ColorConfig_getColorSpaceNameByRole(c.ptr, c_str))
runtime.KeepAlive(c)
return ret
}
// Given the specified input and output ColorSpace, construct the
// processor. It is possible that this will return nil and an error, if the
// inputColorSpace doesnt exist, the outputColorSpace doesn't
// exist, or if the specified transformation is illegal (for
// example, it may require the inversion of a 3D-LUT, etc). When
// the user is finished with a ColorProcess, ColorProcess.Destroy()
// should be called. ColorProcessor(s) remain valid even if the
// ColorConfig that created them no longer exists.
//
// Multiple calls to this are potentially expensive, so you should
// call once to create a ColorProcessor to use on an entire image
// (or multiple images), NOT for every scanline or pixel
// separately!
func (c *ColorConfig) CreateColorProcessor(inColorSpace, outColorSpace string) (*ColorProcessor, error) {
c_in := C.CString(inColorSpace)
defer C.free(unsafe.Pointer(c_in))
c_out := C.CString(outColorSpace)
defer C.free(unsafe.Pointer(c_out))
ptr := C.ColorConfig_createColorProcessor(c.ptr, c_in, c_out)
err := c.error()
if err != nil {
return nil, err
}
return newColorProcessor(ptr), nil
}
// This routine will return the error string (and clear any error
// flags). If no error has occurred since the last time GetError()
// was called, it will return an empty string.
func (c *ColorConfig) error() error {
isError := C.ColorConfig_error(c.ptr)
if bool(isError) {
return errors.New(C.GoString(C.ColorConfig_geterror(c.ptr)))
}
runtime.KeepAlive(c)
return nil
}