summaryrefslogtreecommitdiff
path: root/src/jniglue/tests/org/OpenColorIO/ConfigTest.java
blob: a676bae609ab62effe8c484de3cdf6cc83cb2416 (plain)
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
import junit.framework.TestCase;
import org.OpenColorIO.*;
import java.nio.*;

public class ConfigTest extends TestCase {
    
    String SIMPLE_PROFILE = ""
    + "ocio_profile_version: 1\n"
    + "\n"
    + "search_path: luts\n"
    + "strictparsing: false\n"
    + "luma: [0.2126, 0.7152, 0.0722]\n"
    + "\n"
    + "roles:\n"
    + "  default: raw\n"
    + "  scene_linear: lnh\n"
    + "\n"
    + "displays:\n"
    + "  sRGB:\n"
    + "    - !<View> {name: Film1D, colorspace: vd8}\n"
    + "    - !<View> {name: Raw, colorspace: raw}\n"
    + "\n"
    + "active_displays: []\n"
    + "active_views: []\n"
    + "\n"
    + "colorspaces:\n"
    + "  - !<ColorSpace>\n"
    + "    name: raw\n"
    + "    family: raw\n"
    + "    bitdepth: 32f\n"
    + "    description: |\n"
    + "      A raw color space. Conversions to and from this space are no-ops.\n"
    + "      \n"
    + "    isdata: true\n"
    + "    allocation: uniform\n"
    + "\n"
    + "  - !<ColorSpace>\n"
    + "    name: lnh\n"
    + "    family: ln\n"
    + "    bitdepth: 16f\n"
    + "    description: |\n"
    + "      The show reference space. This is a sensor referred linear\n"
    + "      representation of the scene with primaries that correspond to\n"
    + "      scanned film. 0.18 in this space corresponds to a properly\n"
    + "      exposed 18% grey card.\n"
    + "      \n"
    + "    isdata: false\n"
    + "    allocation: lg2\n"
    + "\n"
    + "  - !<ColorSpace>\n"
    + "    name: vd8\n"
    + "    family: vd8\n"
    + "    bitdepth: 8ui\n"
    + "    description: |\n"
    + "      how many transforms can we use?\n"
    + "      \n"
    + "    isdata: false\n"
    + "    allocation: uniform\n"
    + "    to_reference: !<GroupTransform>\n"
    + "      children:\n"
    + "        - !<ExponentTransform> {value: [2.2, 2.2, 2.2, 1]}\n"
    + "        - !<MatrixTransform> {matrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], offset: [0, 0, 0, 0]}\n"
    + "        - !<CDLTransform> {slope: [1, 1, 1], offset: [0.1, 0.3, 0.4], power: [1.1, 1.1, 1.1], saturation: 0.9}\n";
    
    String GLSLResult = ""
    + "\n"
    + "// Generated by OpenColorIO\n"
    + "\n"
    + "vec4 jnitestocio(in vec4 inPixel, \n"
    + "    const sampler3D lut3d) \n"
    + "{\n"
    + "vec4 out_pixel = inPixel; \n"
    + "out_pixel = out_pixel * mat4(1.08749, -0.0794667, -0.00802222, 0, -0.0236222, 1.03164, -0.00802222, 0, -0.0236222, -0.0794667, 1.10309, 0, 0, 0, 0, 1);\n"
    + "out_pixel = pow(max(out_pixel, vec4(0, 0, 0, 0)), vec4(0.909091, 0.909091, 0.909091, 1));\n"
    + "out_pixel = vec4(-0.1, -0.3, -0.4, -0) + out_pixel;\n"
    + "out_pixel = pow(max(out_pixel, vec4(0, 0, 0, 0)), vec4(0.454545, 0.454545, 0.454545, 1));\n"
    + "// OSX segfault work-around: Force a no-op sampling of the 3d lut.\n"
    + "texture3D(lut3d, -inf * out_pixel.rgb + inf).rgb;\n"
    + "return out_pixel;\n"
    + "}\n"
    + "\n";
    
    protected void setUp() {
    }
    
    protected void tearDown() {
    }
    
    public void test_interface() {
        
        Config _cfg = new Config().CreateFromStream(SIMPLE_PROFILE);
        Config _cfge = _cfg.createEditableCopy();
        assertEquals("luts", _cfge.getSearchPath());
        _cfge.setSearchPath("otherdir");
        assertEquals("otherdir", _cfge.getSearchPath());
        _cfge.sanityCheck();
        _cfge.setDescription("testdesc");
        assertEquals("testdesc", _cfge.getDescription());
        //assertEquals(SIMPLE_PROFILE, _cfg.serialize());
        //assertEquals("$07d1fb1509eeae1837825fd4242f8a69:$885ad1683add38a11f7bbe34e8bf9ac0",
        //             _cfg.getCacheID());
        Context con = _cfg.getCurrentContext();
        assertNotSame(0, con.getNumStringVars());
        //assertEquals("", _cfg.getCacheID(con)); // test env makes this fail
        //assertEquals("", _cfge.getWorkingDir());
        _cfge.setWorkingDir("/foobar");
        assertEquals("/foobar", _cfge.getWorkingDir());
        assertEquals(3, _cfge.getNumColorSpaces());
        assertEquals("lnh", _cfge.getColorSpaceNameByIndex(1));
        //public native ColorSpace getColorSpace(String name);
        assertEquals(0, _cfge.getIndexForColorSpace("foobar"));
        //public native void addColorSpace(ColorSpace cs);
        //public native void clearColorSpaces();
        //public native String parseColorSpaceFromString(String str);
        if(_cfg.isStrictParsingEnabled()) fail("strict parsing should be off");
        _cfge.setStrictParsingEnabled(true);
        if(!_cfge.isStrictParsingEnabled()) fail("strict parsing should be on");
        assertEquals(2, _cfge.getNumRoles());
        if(_cfg.hasRole("foo")) fail("shouldn't have role foo");
        _cfge.setRole("foo", "dfadfadf");
        assertEquals(3, _cfge.getNumRoles());
        if(!_cfge.hasRole("foo")) fail("should have role foo");
        assertEquals("foo", _cfge.getRoleName(1));
        assertEquals("sRGB", _cfge.getDefaultDisplay());
        assertEquals(1, _cfge.getNumDisplays());
        assertEquals("sRGB", _cfge.getDisplay(0));
        assertEquals("Film1D", _cfge.getDefaultView("sRGB"));
        assertEquals(2, _cfge.getNumViews("sRGB"));
        assertEquals("Raw", _cfge.getView("sRGB", 1));
        assertEquals("vd8", _cfge.getDisplayColorSpaceName("sRGB", "Film1D"));
        assertEquals("", _cfg.getDisplayLooks("sRGB", "Film1D"));
        
        // TODO: seems that 4 string params causes a memory error in the JNI layer?
        //_cfge.addDisplay("foo", "bar", "foo", 0);
        
        _cfge.clearDisplays();
        _cfge.setActiveDisplays("sRGB");
        assertEquals("sRGB", _cfge.getActiveDisplays());
        _cfge.setActiveViews("Film1D");
        assertEquals("Film1D", _cfge.getActiveViews());
        float luma[] = new float[3];
        _cfge.getDefaultLumaCoefs(luma);
        assertEquals(0.2126, luma[0], 1e-8);
        float[] newluma = new float[]{0.1f, 0.2f, 0.3f};
        _cfge.setDefaultLumaCoefs(newluma);
        float tnewluma[] = new float[3];
        _cfge.getDefaultLumaCoefs(tnewluma);
        assertEquals(0.1f, tnewluma[0], 1e-8);
        assertEquals(0, _cfge.getNumLooks());
        Look lk = new Look().Create();
        lk.setName("coollook");
        lk.setProcessSpace("somespace");
        ExponentTransform et = new ExponentTransform().Create();
        et.setValue(new float[]{0.1f, 0.2f, 0.3f, 0.4f});
        lk.setTransform(et);
        ExponentTransform iet = new ExponentTransform().Create();
        iet.setValue(new float[]{-0.1f, -0.2f, -0.3f, -0.4f});
        lk.setInverseTransform(iet);
        _cfge.addLook(lk);
        assertEquals(1, _cfge.getNumLooks());
        assertEquals("coollook", _cfge.getLookNameByIndex(0));
        Look glk = _cfge.getLook("coollook");
        assertEquals("somespace", glk.getProcessSpace());
        _cfge.clearLooks();
        assertEquals(0, _cfge.getNumLooks());
        
        //public native Processor getProcessor(Context context, ColorSpace srcColorSpace, ColorSpace dstColorSpace);
        
        Processor _proc = _cfg.getProcessor("lnh", "vd8");
        assertEquals(false, _proc.isNoOp());
        assertEquals(true, _proc.hasChannelCrosstalk());
        float packedpix[] = new float[]{0.48f, 0.18f, 0.9f, 1.0f,
                                        0.48f, 0.18f, 0.18f, 1.0f,
                                        0.48f, 0.18f, 0.18f, 1.0f,
                                        0.48f, 0.18f, 0.18f, 1.0f };
        FloatBuffer buf = ByteBuffer.allocateDirect(2 * 2 * 4 * Float.SIZE / 8).asFloatBuffer();
        buf.put(packedpix);
        PackedImageDesc foo = new PackedImageDesc(buf, 2, 2, 4);
        _proc.apply(foo);
        FloatBuffer wee = foo.getData();
        assertEquals(-2.4307251581696764E-35f, wee.get(2), 1e-8);
        float rgbfoo[] = new float[]{0.48f, 0.18f, 0.18f};
        _proc.applyRGB(rgbfoo);
        assertEquals(0.6875247f, rgbfoo[0], 1e-8);
        float rgbafoo[] = new float[]{0.48f, 0.18f, 0.18f, 1.f};
        _proc.applyRGBA(rgbafoo);
        assertEquals(1.f, rgbafoo[3], 1e-8);
        //assertEquals("$a92ef63abd9edf61ad5a7855da064648", _proc.getCpuCacheID());
        GpuShaderDesc desc = new GpuShaderDesc();
        desc.setLanguage(GpuLanguage.GPU_LANGUAGE_GLSL_1_3);
        desc.setFunctionName("jnitestocio");
        desc.setLut3DEdgeLen(32);
        String glsl = _proc.getGpuShaderText(desc);
        //assertEquals(GLSLResult, glsl);
        //assertEquals("$1dead2bf42974cd1769164e45a0c9e40", _proc.getGpuShaderTextCacheID(desc));
        int len = desc.getLut3DEdgeLen();
        int size = 3 * len * len * len;
        FloatBuffer lut3d = ByteBuffer.allocateDirect(size * Float.SIZE / 8).asFloatBuffer();
        _proc.getGpuLut3D(lut3d, desc);
        assertEquals(0.0f, lut3d.get(size-1));
        assertEquals("<NULL>", _proc.getGpuLut3DCacheID(desc));
        
        //public native Processor getProcessor(Context context, String srcName, String dstName);
        //public native Processor getProcessor(Transform transform);
        //public native Processor getProcessor(Transform transform, TransformDirection direction);
        //public native Processor getProcessor(Context context, Transform transform, TransformDirection direction);
        
        _cfge.dispose();
        _cfg.dispose();
        
        //System.out.println(_cfge.serialize());
        //_cfge.sanityCheck();
        //System.out.println(_cfge.getNumColorSpaces());
        //System.out.println(_cfg.getCacheID());
        //System.out.println(_cfge.serialize());
        
    }
    
}