Sync UTC harness
[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::IsCompressed(Format pixelformat)
28 {
29   switch(pixelformat)
30   {
31     case COMPRESSED_R11_EAC:
32     case COMPRESSED_SIGNED_R11_EAC:
33     case COMPRESSED_RG11_EAC:
34     case COMPRESSED_SIGNED_RG11_EAC:
35     case COMPRESSED_RGB8_ETC2:
36     case COMPRESSED_SRGB8_ETC2:
37     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
38     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
39     case COMPRESSED_RGBA8_ETC2_EAC:
40     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
41     case COMPRESSED_RGB8_ETC1:
42     case COMPRESSED_RGB_PVRTC_4BPPV1:
43     case COMPRESSED_RGBA_ASTC_4x4_KHR:
44     case COMPRESSED_RGBA_ASTC_5x4_KHR:
45     case COMPRESSED_RGBA_ASTC_5x5_KHR:
46     case COMPRESSED_RGBA_ASTC_6x5_KHR:
47     case COMPRESSED_RGBA_ASTC_6x6_KHR:
48     case COMPRESSED_RGBA_ASTC_8x5_KHR:
49     case COMPRESSED_RGBA_ASTC_8x6_KHR:
50     case COMPRESSED_RGBA_ASTC_8x8_KHR:
51     case COMPRESSED_RGBA_ASTC_10x5_KHR:
52     case COMPRESSED_RGBA_ASTC_10x6_KHR:
53     case COMPRESSED_RGBA_ASTC_10x8_KHR:
54     case COMPRESSED_RGBA_ASTC_10x10_KHR:
55     case COMPRESSED_RGBA_ASTC_12x10_KHR:
56     case COMPRESSED_RGBA_ASTC_12x12_KHR:
57     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
58     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
59     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
60     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
61     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
62     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
63     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
64     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
65     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
66     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
67     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
68     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
69     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
70     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
71     {
72       return true;
73     }
74     case INVALID:
75     case A8:
76     case L8:
77     case LA88:
78     case RGB565:
79     case BGR565:
80     case RGBA4444:
81     case BGRA4444:
82     case RGBA5551:
83     case BGRA5551:
84     case RGB888:
85     case RGB8888:
86     case BGR8888:
87     case RGBA8888:
88     case BGRA8888:
89     case RGB16F:
90     case RGB32F:
91     case DEPTH_UNSIGNED_INT:
92     case DEPTH_FLOAT:
93     case DEPTH_STENCIL:
94     case R11G11B10F:
95     case CHROMINANCE_U:
96     case CHROMINANCE_V:
97     {
98       return false;
99     }
100   }
101   return false;
102 }
103
104 bool Pixel::HasAlpha(Format pixelformat)
105 {
106   switch(pixelformat)
107   {
108     case RGBA5551:
109     case RGBA8888:
110     case RGBA4444:
111     case BGRA8888:
112     case BGRA4444:
113     case BGRA5551:
114     case A8:
115     case LA88:
116     // Note, Can be used for alpha if you want: COMPRESSED_R11_EAC,                       ///< ETC2 / EAC single-channel, unsigned
117     // Note, Can be used for alpha if you want: COMPRESSED_SIGNED_R11_EAC,                ///< ETC2 / EAC single-channel, signed
118     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
119     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
120     case COMPRESSED_RGBA8_ETC2_EAC:
121     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
122     case COMPRESSED_RGBA_ASTC_4x4_KHR:
123     case COMPRESSED_RGBA_ASTC_5x4_KHR:
124     case COMPRESSED_RGBA_ASTC_5x5_KHR:
125     case COMPRESSED_RGBA_ASTC_6x5_KHR:
126     case COMPRESSED_RGBA_ASTC_6x6_KHR:
127     case COMPRESSED_RGBA_ASTC_8x5_KHR:
128     case COMPRESSED_RGBA_ASTC_8x6_KHR:
129     case COMPRESSED_RGBA_ASTC_8x8_KHR:
130     case COMPRESSED_RGBA_ASTC_10x5_KHR:
131     case COMPRESSED_RGBA_ASTC_10x6_KHR:
132     case COMPRESSED_RGBA_ASTC_10x8_KHR:
133     case COMPRESSED_RGBA_ASTC_10x10_KHR:
134     case COMPRESSED_RGBA_ASTC_12x10_KHR:
135     case COMPRESSED_RGBA_ASTC_12x12_KHR:
136     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
137     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
138     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
139     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
140     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
141     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
142     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
143     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
144     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
145     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
146     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
147     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
148     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
149     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
150     {
151       return true;
152     }
153
154     case L8:
155     case RGB565:
156     case RGB888:
157     case RGB8888: // alpha not used
158     case BGR8888: // alpha not used
159     case BGR565:
160     case COMPRESSED_R11_EAC:
161     case COMPRESSED_SIGNED_R11_EAC:
162     case COMPRESSED_RG11_EAC:
163     case COMPRESSED_SIGNED_RG11_EAC:
164     case COMPRESSED_RGB8_ETC2:
165     case COMPRESSED_SRGB8_ETC2:
166     case COMPRESSED_RGB8_ETC1:
167     case COMPRESSED_RGB_PVRTC_4BPPV1:
168     case RGB16F:
169     case RGB32F:
170     case DEPTH_UNSIGNED_INT:
171     case DEPTH_FLOAT:
172     case DEPTH_STENCIL:
173     case R11G11B10F:
174     case INVALID:
175     case CHROMINANCE_U:
176     case CHROMINANCE_V:
177     {
178       return false;
179     }
180   }
181   return false;
182 }
183
184 uint32_t Pixel::GetBytesPerPixel(Format pixelFormat)
185 {
186   switch(pixelFormat)
187   {
188     case L8:
189     case A8:
190     case CHROMINANCE_U:
191     case CHROMINANCE_V:
192     {
193       return 1;
194     }
195
196     case LA88:
197     case RGB565:
198     case RGBA4444:
199     case RGBA5551:
200     case BGR565:
201     case BGRA4444:
202     case BGRA5551:
203     {
204       return 2;
205     }
206
207     case RGB888:
208     {
209       return 3;
210     }
211
212     case RGB8888:
213     case BGR8888:
214     case RGBA8888:
215     case BGRA8888:
216     case DEPTH_UNSIGNED_INT:
217     case DEPTH_FLOAT:
218     case DEPTH_STENCIL:
219     case R11G11B10F:
220     {
221       return 4;
222     }
223
224     case RGB16F:
225     {
226       return 6;
227     }
228
229     case RGB32F:
230     {
231       return 12;
232     }
233
234     case COMPRESSED_R11_EAC:
235     case COMPRESSED_SIGNED_R11_EAC:
236     case COMPRESSED_RG11_EAC:
237     case COMPRESSED_SIGNED_RG11_EAC:
238     case COMPRESSED_RGB8_ETC2:
239     case COMPRESSED_SRGB8_ETC2:
240     case COMPRESSED_RGB8_ETC1:
241     case COMPRESSED_RGB_PVRTC_4BPPV1:
242     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
243     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
244     case COMPRESSED_RGBA8_ETC2_EAC:
245     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
246     case COMPRESSED_RGBA_ASTC_4x4_KHR:
247     case COMPRESSED_RGBA_ASTC_5x4_KHR:
248     case COMPRESSED_RGBA_ASTC_5x5_KHR:
249     case COMPRESSED_RGBA_ASTC_6x5_KHR:
250     case COMPRESSED_RGBA_ASTC_6x6_KHR:
251     case COMPRESSED_RGBA_ASTC_8x5_KHR:
252     case COMPRESSED_RGBA_ASTC_8x6_KHR:
253     case COMPRESSED_RGBA_ASTC_8x8_KHR:
254     case COMPRESSED_RGBA_ASTC_10x5_KHR:
255     case COMPRESSED_RGBA_ASTC_10x6_KHR:
256     case COMPRESSED_RGBA_ASTC_10x8_KHR:
257     case COMPRESSED_RGBA_ASTC_10x10_KHR:
258     case COMPRESSED_RGBA_ASTC_12x10_KHR:
259     case COMPRESSED_RGBA_ASTC_12x12_KHR:
260     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
261     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
262     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
263     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
264     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
265     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
266     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
267     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
268     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
269     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
270     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
271     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
272     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
273     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
274     case INVALID:
275     {
276       DALI_LOG_ERROR("Pixel formats for compressed images do not have meaningful integer bits per pixel values.\n");
277       return 0;
278     }
279   }
280   return 0;
281 }
282
283 void Pixel::GetAlphaOffsetAndMask(Format pixelFormat, int& byteOffset, int& bitMask)
284 {
285   switch(pixelFormat)
286   {
287     case A8:
288     {
289       byteOffset = 0;
290       bitMask    = 0xFF;
291     }
292     break;
293
294     case L8:
295     case RGB888:
296     case RGB565:
297     case RGB8888:
298     case BGR8888:
299     case BGR565:
300     {
301       byteOffset = 0;
302       bitMask    = 0;
303       break;
304     }
305
306     case LA88:
307     {
308       byteOffset = 1;
309       bitMask    = 0xff;
310       break;
311     }
312
313     case RGBA4444:
314     case BGRA4444:
315     {
316       byteOffset = 1;
317       bitMask    = 0x0f;
318       break;
319     }
320
321     case RGBA5551:
322     case BGRA5551:
323     {
324       byteOffset = 1;
325       bitMask    = 0x01;
326       break;
327     }
328
329     case RGBA8888:
330     case BGRA8888:
331     {
332       byteOffset = 3;
333       bitMask    = 0xff;
334       break;
335     }
336
337     case COMPRESSED_R11_EAC:
338     case COMPRESSED_SIGNED_R11_EAC:
339     case COMPRESSED_RG11_EAC:
340     case COMPRESSED_SIGNED_RG11_EAC:
341     case COMPRESSED_RGB8_ETC2:
342     case COMPRESSED_SRGB8_ETC2:
343     case COMPRESSED_RGB8_ETC1:
344     case COMPRESSED_RGB_PVRTC_4BPPV1:
345     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
346     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
347     case COMPRESSED_RGBA8_ETC2_EAC:
348     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
349     case COMPRESSED_RGBA_ASTC_4x4_KHR:
350     case COMPRESSED_RGBA_ASTC_5x4_KHR:
351     case COMPRESSED_RGBA_ASTC_5x5_KHR:
352     case COMPRESSED_RGBA_ASTC_6x5_KHR:
353     case COMPRESSED_RGBA_ASTC_6x6_KHR:
354     case COMPRESSED_RGBA_ASTC_8x5_KHR:
355     case COMPRESSED_RGBA_ASTC_8x6_KHR:
356     case COMPRESSED_RGBA_ASTC_8x8_KHR:
357     case COMPRESSED_RGBA_ASTC_10x5_KHR:
358     case COMPRESSED_RGBA_ASTC_10x6_KHR:
359     case COMPRESSED_RGBA_ASTC_10x8_KHR:
360     case COMPRESSED_RGBA_ASTC_10x10_KHR:
361     case COMPRESSED_RGBA_ASTC_12x10_KHR:
362     case COMPRESSED_RGBA_ASTC_12x12_KHR:
363     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
364     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
365     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
366     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
367     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
368     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
369     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
370     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
371     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
372     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
373     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
374     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
375     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
376     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
377     case RGB16F:
378     case RGB32F:
379     case DEPTH_UNSIGNED_INT:
380     case DEPTH_FLOAT:
381     case DEPTH_STENCIL:
382     case R11G11B10F:
383     case CHROMINANCE_U:
384     case CHROMINANCE_V:
385     case INVALID:
386     {
387       DALI_LOG_ERROR("Pixel formats are not compatible with simple masking-out of per-pixel alpha.\n");
388       byteOffset = 0;
389       bitMask    = 0;
390       break;
391     }
392   }
393 }
394
395 } // namespace Dali