Add CHROMINANCE pixel format
[platform/core/uifw/dali-core.git] / dali / public-api / images / pixel.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/public-api/images/pixel.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/common/dali-common.h>
24
25 namespace Dali
26 {
27 bool Pixel::HasAlpha(Format pixelformat)
28 {
29   switch(pixelformat)
30   {
31     case RGBA5551:
32     case RGBA8888:
33     case RGBA4444:
34     case BGRA8888:
35     case BGRA4444:
36     case BGRA5551:
37     case A8:
38     case LA88:
39     // Note, Can be used for alpha if you want: COMPRESSED_R11_EAC,                       ///< ETC2 / EAC single-channel, unsigned
40     // Note, Can be used for alpha if you want: COMPRESSED_SIGNED_R11_EAC,                ///< ETC2 / EAC single-channel, signed
41     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
42     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
43     case COMPRESSED_RGBA8_ETC2_EAC:
44     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
45     case COMPRESSED_RGBA_ASTC_4x4_KHR:
46     case COMPRESSED_RGBA_ASTC_5x4_KHR:
47     case COMPRESSED_RGBA_ASTC_5x5_KHR:
48     case COMPRESSED_RGBA_ASTC_6x5_KHR:
49     case COMPRESSED_RGBA_ASTC_6x6_KHR:
50     case COMPRESSED_RGBA_ASTC_8x5_KHR:
51     case COMPRESSED_RGBA_ASTC_8x6_KHR:
52     case COMPRESSED_RGBA_ASTC_8x8_KHR:
53     case COMPRESSED_RGBA_ASTC_10x5_KHR:
54     case COMPRESSED_RGBA_ASTC_10x6_KHR:
55     case COMPRESSED_RGBA_ASTC_10x8_KHR:
56     case COMPRESSED_RGBA_ASTC_10x10_KHR:
57     case COMPRESSED_RGBA_ASTC_12x10_KHR:
58     case COMPRESSED_RGBA_ASTC_12x12_KHR:
59     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
60     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
61     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
62     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
63     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
64     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
65     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
66     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
67     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
68     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
69     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
70     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
71     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
72     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
73     {
74       return true;
75     }
76
77     case L8:
78     case RGB565:
79     case RGB888:
80     case RGB8888: // alpha not used
81     case BGR8888: // alpha not used
82     case BGR565:
83     case COMPRESSED_R11_EAC:
84     case COMPRESSED_SIGNED_R11_EAC:
85     case COMPRESSED_RG11_EAC:
86     case COMPRESSED_SIGNED_RG11_EAC:
87     case COMPRESSED_RGB8_ETC2:
88     case COMPRESSED_SRGB8_ETC2:
89     case COMPRESSED_RGB8_ETC1:
90     case COMPRESSED_RGB_PVRTC_4BPPV1:
91     case RGB16F:
92     case RGB32F:
93     case DEPTH_UNSIGNED_INT:
94     case DEPTH_FLOAT:
95     case DEPTH_STENCIL:
96     case R11G11B10F:
97     case INVALID:
98     case CHROMINANCE_U:
99     case CHROMINANCE_V:
100     {
101       return false;
102     }
103   }
104   return false;
105 }
106
107 uint32_t Pixel::GetBytesPerPixel(Format pixelFormat)
108 {
109   switch(pixelFormat)
110   {
111     case L8:
112     case A8:
113     case CHROMINANCE_U:
114     case CHROMINANCE_V:
115     {
116       return 1;
117     }
118
119     case LA88:
120     case RGB565:
121     case RGBA4444:
122     case RGBA5551:
123     case BGR565:
124     case BGRA4444:
125     case BGRA5551:
126     {
127       return 2;
128     }
129
130     case RGB888:
131     {
132       return 3;
133     }
134
135     case RGB8888:
136     case BGR8888:
137     case RGBA8888:
138     case BGRA8888:
139     case DEPTH_UNSIGNED_INT:
140     case DEPTH_FLOAT:
141     case DEPTH_STENCIL:
142     case R11G11B10F:
143     {
144       return 4;
145     }
146
147     case RGB16F:
148     {
149       return 6;
150     }
151
152     case RGB32F:
153     {
154       return 12;
155     }
156
157     case COMPRESSED_R11_EAC:
158     case COMPRESSED_SIGNED_R11_EAC:
159     case COMPRESSED_RG11_EAC:
160     case COMPRESSED_SIGNED_RG11_EAC:
161     case COMPRESSED_RGB8_ETC2:
162     case COMPRESSED_SRGB8_ETC2:
163     case COMPRESSED_RGB8_ETC1:
164     case COMPRESSED_RGB_PVRTC_4BPPV1:
165     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
166     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
167     case COMPRESSED_RGBA8_ETC2_EAC:
168     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
169     case COMPRESSED_RGBA_ASTC_4x4_KHR:
170     case COMPRESSED_RGBA_ASTC_5x4_KHR:
171     case COMPRESSED_RGBA_ASTC_5x5_KHR:
172     case COMPRESSED_RGBA_ASTC_6x5_KHR:
173     case COMPRESSED_RGBA_ASTC_6x6_KHR:
174     case COMPRESSED_RGBA_ASTC_8x5_KHR:
175     case COMPRESSED_RGBA_ASTC_8x6_KHR:
176     case COMPRESSED_RGBA_ASTC_8x8_KHR:
177     case COMPRESSED_RGBA_ASTC_10x5_KHR:
178     case COMPRESSED_RGBA_ASTC_10x6_KHR:
179     case COMPRESSED_RGBA_ASTC_10x8_KHR:
180     case COMPRESSED_RGBA_ASTC_10x10_KHR:
181     case COMPRESSED_RGBA_ASTC_12x10_KHR:
182     case COMPRESSED_RGBA_ASTC_12x12_KHR:
183     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
184     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
185     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
186     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
187     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
188     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
189     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
190     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
191     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
192     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
193     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
194     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
195     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
196     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
197     case INVALID:
198     {
199       DALI_LOG_ERROR("Pixel formats for compressed images do not have meaningful integer bits per pixel values.\n");
200       return 0;
201     }
202   }
203   return 0;
204 }
205
206 void Pixel::GetAlphaOffsetAndMask(Format pixelFormat, int& byteOffset, int& bitMask)
207 {
208   switch(pixelFormat)
209   {
210     case A8:
211     {
212       byteOffset = 0;
213       bitMask    = 0xFF;
214     }
215     break;
216
217     case L8:
218     case RGB888:
219     case RGB565:
220     case RGB8888:
221     case BGR8888:
222     case BGR565:
223     {
224       byteOffset = 0;
225       bitMask    = 0;
226       break;
227     }
228
229     case LA88:
230     {
231       byteOffset = 1;
232       bitMask    = 0xff;
233       break;
234     }
235
236     case RGBA4444:
237     case BGRA4444:
238     {
239       byteOffset = 1;
240       bitMask    = 0x0f;
241       break;
242     }
243
244     case RGBA5551:
245     case BGRA5551:
246     {
247       byteOffset = 1;
248       bitMask    = 0x01;
249       break;
250     }
251
252     case RGBA8888:
253     case BGRA8888:
254     {
255       byteOffset = 3;
256       bitMask    = 0xff;
257       break;
258     }
259
260     case COMPRESSED_R11_EAC:
261     case COMPRESSED_SIGNED_R11_EAC:
262     case COMPRESSED_RG11_EAC:
263     case COMPRESSED_SIGNED_RG11_EAC:
264     case COMPRESSED_RGB8_ETC2:
265     case COMPRESSED_SRGB8_ETC2:
266     case COMPRESSED_RGB8_ETC1:
267     case COMPRESSED_RGB_PVRTC_4BPPV1:
268     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
269     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
270     case COMPRESSED_RGBA8_ETC2_EAC:
271     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
272     case COMPRESSED_RGBA_ASTC_4x4_KHR:
273     case COMPRESSED_RGBA_ASTC_5x4_KHR:
274     case COMPRESSED_RGBA_ASTC_5x5_KHR:
275     case COMPRESSED_RGBA_ASTC_6x5_KHR:
276     case COMPRESSED_RGBA_ASTC_6x6_KHR:
277     case COMPRESSED_RGBA_ASTC_8x5_KHR:
278     case COMPRESSED_RGBA_ASTC_8x6_KHR:
279     case COMPRESSED_RGBA_ASTC_8x8_KHR:
280     case COMPRESSED_RGBA_ASTC_10x5_KHR:
281     case COMPRESSED_RGBA_ASTC_10x6_KHR:
282     case COMPRESSED_RGBA_ASTC_10x8_KHR:
283     case COMPRESSED_RGBA_ASTC_10x10_KHR:
284     case COMPRESSED_RGBA_ASTC_12x10_KHR:
285     case COMPRESSED_RGBA_ASTC_12x12_KHR:
286     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
287     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
288     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
289     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
290     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
291     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
292     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
293     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
294     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
295     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
296     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
297     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
298     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
299     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
300     case RGB16F:
301     case RGB32F:
302     case DEPTH_UNSIGNED_INT:
303     case DEPTH_FLOAT:
304     case DEPTH_STENCIL:
305     case R11G11B10F:
306     case CHROMINANCE_U:
307     case CHROMINANCE_V:
308     case INVALID:
309     {
310       DALI_LOG_ERROR("Pixel formats are not compatible with simple masking-out of per-pixel alpha.\n");
311       byteOffset = 0;
312       bitMask    = 0;
313       break;
314     }
315   }
316 }
317
318 } // namespace Dali