aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/opencl/CLImageFormat.java
blob: 1d8af3458b03a39d409311302d7c48d6f3b485e0 (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
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
/*
 * Copyright 2009 - 2010 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */

package com.jogamp.opencl;

import com.jogamp.opencl.llb.impl.CLImageFormatImpl;

import static com.jogamp.opencl.llb.CL.*;

/**
 * Represents the OpenCL image format with its channeltype and order.
 * @author Michael Bien
 * @see CLContext#getSupportedImage2dFormats(com.jogamp.opencl.CLMemory.Mem[])
 * @see CLContext#getSupportedImage3dFormats(com.jogamp.opencl.CLMemory.Mem[])
 */
public final class CLImageFormat {

    private final CLImageFormatImpl format;

    CLImageFormat() {
        format = CLImageFormatImpl.create();
    }

    CLImageFormat(final CLImageFormatImpl format) {
        this.format = format;
    }

    public CLImageFormat(final ChannelOrder order, final ChannelType type) {
        format = CLImageFormatImpl.create();
        setImageChannelOrder(order);
        setImageChannelDataType(type);
    }

    public CLImageFormat setImageChannelOrder(final ChannelOrder order) {
        format.setImageChannelOrder(order.ORDER);
        return this;
    }

    public CLImageFormat setImageChannelDataType(final ChannelType type) {
        format.setImageChannelDataType(type.TYPE);
        return this;
    }

    public ChannelOrder getImageChannelOrder() {
        return ChannelOrder.valueOf(format.getImageChannelOrder());
    }

    public ChannelType getImageChannelDataType() {
        return ChannelType.valueOf(format.getImageChannelDataType());
    }

    /**
     * Returns the struct accessor for the cl_image_format struct.
     */
    public CLImageFormatImpl getFormatImpl() {
        return format;
    }

    @Override
    public String toString() {
        return "CLImageFormat["+getImageChannelOrder()+" "+getImageChannelDataType()+"]";
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CLImageFormat other = (CLImageFormat) obj;
        if (this.getImageChannelDataType() != other.getImageChannelDataType()) {
            return false;
        }
        if (this.getImageChannelOrder() != other.getImageChannelOrder()) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 47 * hash + (this.getImageChannelDataType() != null ? this.getImageChannelDataType().hashCode() : 0);
        hash = 47 * hash + (this.getImageChannelOrder() != null ? this.getImageChannelOrder().hashCode() : 0);
        return hash;
    }

    /**
     * Specifies the number of channels and the channel layout i.e. the memory
     * layout in which channels are stored in the image.
     */
    public enum ChannelOrder {

        /**
         *
         */
        R(CL_R),

        /**
         *
         */
        Rx(CL_Rx),

        /**
         *
         */
        A(CL_A),

        /**
         *
         */
        RG(CL_RG),

        /**
         *
         */
        RGx(CL_RGx),

        /**
         *
         */
        RA(CL_RA),

        /**
         * This format can only be used if channel data type is one of the following values:
         * {@link ChannelType#UNORM_SHORT_565}, {@link ChannelType#UNORM_SHORT_555}
         * or {@link ChannelType#UNORM_INT_101010}.
         */
        RGB(CL_RGB),

        /**
         *
         */
        RGBx(CL_RGBx),

        /**
         *
         */
        RGBA(CL_RGBA),

        /**
         * This format can only be used if channel data type is one of the following values:
         * {@link ChannelType#UNORM_INT8}, {@link ChannelType#SNORM_INT8}, {@link ChannelType#SIGNED_INT8}
         * or {@link ChannelType#UNSIGNED_INT8}.
         */
        ARGB(CL_ARGB),

        /**
         * @see #ARGB
         */
        BGRA(CL_BGRA),

        /**
         * This format can only be used if channel data type is one of the following values:
         * {@link ChannelType#UNORM_INT8}, {@link ChannelType#UNORM_INT16}, {@link ChannelType#SNORM_INT8},
         * {@link ChannelType#SNORM_INT16}, {@link ChannelType#HALF_FLOAT}, or {@link ChannelType#FLOAT}.
         */
        INTENSITY(CL_INTENSITY),

        /**
         * This format can only be used if channel data type is one of the following values:
         * {@link ChannelType#UNORM_INT8}, {@link ChannelType#UNORM_INT16}, {@link ChannelType#SNORM_INT8},
         * {@link ChannelType#SNORM_INT16}, {@link ChannelType#HALF_FLOAT}, or {@link ChannelType#FLOAT}.
         */
        LUMINANCE(CL_LUMINANCE);


        /**
         * Value of wrapped OpenCL flag.
         */
        public final int ORDER;

        private ChannelOrder(final int order) {
            this.ORDER = order;
        }

        public static ChannelOrder valueOf(final int orderFlag) {
            switch (orderFlag) {
                case CL_R:
                    return R;
                case CL_Rx:
                    return Rx;
                case CL_A:
                    return A;
                case CL_INTENSITY:
                    return INTENSITY;
                case CL_LUMINANCE:
                    return LUMINANCE;
                case CL_RG:
                    return RG;
                case CL_RGx:
                    return RGx;
                case CL_RA:
                    return RA;
                case CL_RGB:
                    return RGB;
                case CL_RGBx:
                    return RGBx;
                case CL_RGBA:
                    return RGBA;
                case CL_ARGB:
                    return ARGB;
                case CL_BGRA:
                    return BGRA;
            }
            return null;
        }

    }


    /**
     * Describes the size of the channel data type.
     */
    public enum ChannelType {

        /**
         * Each channel component is a normalized signed 8-bit integer value.
         */
        SNORM_INT8(CL_SNORM_INT8),

        /**
         * Each channel component is a normalized signed 16-bit integer value.
         */
        SNORM_INT16(CL_SNORM_INT16),

        /**
         * Each channel component is a normalized unsigned 8-bit integer value.
         */
        UNORM_INT8(CL_UNORM_INT8),

        /**
         * Each channel component is a normalized unsigned 16-bit integer value.
         */
        UNORM_INT16(CL_UNORM_INT16),

        /**
         * Represents a normalized 5-6-5 3-channel RGB image. The channel order must
         * be {@link ChannelOrder#RGB}.
         */
        UNORM_SHORT_565(CL_UNORM_SHORT_565),

        /**
         * Represents a normalized x-5-5-5 4-channel xRGB image. The channel order must
         * be {@link ChannelOrder#RGB}.
         */
        UNORM_SHORT_555(CL_UNORM_SHORT_555),

        /**
         * Represents a normalized x-10-10-10 4-channel xRGB image. The channel order
         * must be {@link ChannelOrder#RGB}.
         */
        UNORM_INT_101010(CL_UNORM_INT_101010),

        /**
         * Each channel component is an unnormalized signed 8-bit integer value.
         */
        SIGNED_INT8(CL_SIGNED_INT8),

        /**
         * Each channel component is an unnormalized signed 16-bit integer value.
         */
        SIGNED_INT16(CL_SIGNED_INT16),

        /**
         * Each channel component is an unnormalized signed 32-bit integer value.
         */
        SIGNED_INT32(CL_SIGNED_INT32),

        /**
         * Each channel component is an unnormalized unsigned 8-bit integer value.
         */
        UNSIGNED_INT8(CL_UNSIGNED_INT8),

        /**
         * Each channel component is an unnormalized unsigned 16-bit integer value.
         */
        UNSIGNED_INT16(CL_UNSIGNED_INT16),

        /**
         * Each channel component is an unnormalized unsigned 32-bit integer value.
         */
        UNSIGNED_INT32(CL_UNSIGNED_INT32),

        /**
         * Each channel component is a 16-bit half-float value.
         */
        HALF_FLOAT(CL_HALF_FLOAT),

        /**
         * Each channel component is a single precision floating-point value.
         */
        FLOAT(CL_FLOAT);

        /**
         * Value of wrapped OpenCL flag.
         */
        public final int TYPE;

        private ChannelType(final int channel) {
            this.TYPE = channel;
        }

        public static ChannelType valueOf(final int channelFlag) {
            switch (channelFlag) {
                case CL_SNORM_INT8:
                    return SNORM_INT8;
                case CL_SNORM_INT16:
                    return SNORM_INT16;
                case CL_UNORM_INT8:
                    return UNORM_INT8;
                case CL_UNORM_INT16:
                    return UNORM_INT16;
                case CL_UNORM_SHORT_565:
                    return UNORM_SHORT_565;
                case CL_UNORM_SHORT_555:
                    return UNORM_SHORT_555;
                case CL_UNORM_INT_101010:
                    return UNORM_INT_101010;
                case CL_SIGNED_INT8:
                    return SIGNED_INT8;
                case CL_SIGNED_INT16:
                    return SIGNED_INT16;
                case CL_SIGNED_INT32:
                    return SIGNED_INT32;
                case CL_UNSIGNED_INT8:
                    return UNSIGNED_INT8;
                case CL_UNSIGNED_INT16:
                    return UNSIGNED_INT16;
                case CL_UNSIGNED_INT32:
                    return UNSIGNED_INT32;
                case CL_HALF_FLOAT:
                    return HALF_FLOAT;
                case CL_FLOAT:
                    return FLOAT;
            }
            return null;
        }

    }
}