1571166d385ba2afa4648e3fdfec2299e7e22c98
[platform/core/uifw/dali-core.git] / dali / public-api / images / pixel.cpp
1 /*
2  * Copyright (c) 2021 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     {
99       return false;
100     }
101   }
102   return false;
103 }
104
105 uint32_t Pixel::GetBytesPerPixel(Format pixelFormat)
106 {
107   switch(pixelFormat)
108   {
109     case L8:
110     case A8:
111     {
112       return 1;
113     }
114
115     case LA88:
116     case RGB565:
117     case RGBA4444:
118     case RGBA5551:
119     case BGR565:
120     case BGRA4444:
121     case BGRA5551:
122     {
123       return 2;
124     }
125
126     case RGB888:
127     {
128       return 3;
129     }
130
131     case RGB8888:
132     case BGR8888:
133     case RGBA8888:
134     case BGRA8888:
135     case DEPTH_UNSIGNED_INT:
136     case DEPTH_FLOAT:
137     case DEPTH_STENCIL:
138     case R11G11B10F:
139     {
140       return 4;
141     }
142
143     case RGB16F:
144     {
145       return 6;
146     }
147
148     case RGB32F:
149     {
150       return 12;
151     }
152
153     case COMPRESSED_R11_EAC:
154     case COMPRESSED_SIGNED_R11_EAC:
155     case COMPRESSED_RG11_EAC:
156     case COMPRESSED_SIGNED_RG11_EAC:
157     case COMPRESSED_RGB8_ETC2:
158     case COMPRESSED_SRGB8_ETC2:
159     case COMPRESSED_RGB8_ETC1:
160     case COMPRESSED_RGB_PVRTC_4BPPV1:
161     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
162     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
163     case COMPRESSED_RGBA8_ETC2_EAC:
164     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
165     case COMPRESSED_RGBA_ASTC_4x4_KHR:
166     case COMPRESSED_RGBA_ASTC_5x4_KHR:
167     case COMPRESSED_RGBA_ASTC_5x5_KHR:
168     case COMPRESSED_RGBA_ASTC_6x5_KHR:
169     case COMPRESSED_RGBA_ASTC_6x6_KHR:
170     case COMPRESSED_RGBA_ASTC_8x5_KHR:
171     case COMPRESSED_RGBA_ASTC_8x6_KHR:
172     case COMPRESSED_RGBA_ASTC_8x8_KHR:
173     case COMPRESSED_RGBA_ASTC_10x5_KHR:
174     case COMPRESSED_RGBA_ASTC_10x6_KHR:
175     case COMPRESSED_RGBA_ASTC_10x8_KHR:
176     case COMPRESSED_RGBA_ASTC_10x10_KHR:
177     case COMPRESSED_RGBA_ASTC_12x10_KHR:
178     case COMPRESSED_RGBA_ASTC_12x12_KHR:
179     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
180     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
181     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
182     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
183     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
184     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
185     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
186     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
187     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
188     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
189     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
190     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
191     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
192     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
193     case INVALID:
194     {
195       DALI_LOG_ERROR("Pixel formats for compressed images do not have meaningful integer bits per pixel values.\n");
196       return 0;
197     }
198   }
199   return 0;
200 }
201
202 void Pixel::GetAlphaOffsetAndMask(Format pixelFormat, int& byteOffset, int& bitMask)
203 {
204   switch(pixelFormat)
205   {
206     case A8:
207     {
208       byteOffset = 0;
209       bitMask    = 0xFF;
210     }
211     break;
212
213     case L8:
214     case RGB888:
215     case RGB565:
216     case RGB8888:
217     case BGR8888:
218     case BGR565:
219     {
220       byteOffset = 0;
221       bitMask    = 0;
222       break;
223     }
224
225     case LA88:
226     {
227       byteOffset = 1;
228       bitMask    = 0xff;
229       break;
230     }
231
232     case RGBA4444:
233     case BGRA4444:
234     {
235       byteOffset = 1;
236       bitMask    = 0x0f;
237       break;
238     }
239
240     case RGBA5551:
241     case BGRA5551:
242     {
243       byteOffset = 1;
244       bitMask    = 0x01;
245       break;
246     }
247
248     case RGBA8888:
249     case BGRA8888:
250     {
251       byteOffset = 3;
252       bitMask    = 0xff;
253       break;
254     }
255
256     case COMPRESSED_R11_EAC:
257     case COMPRESSED_SIGNED_R11_EAC:
258     case COMPRESSED_RG11_EAC:
259     case COMPRESSED_SIGNED_RG11_EAC:
260     case COMPRESSED_RGB8_ETC2:
261     case COMPRESSED_SRGB8_ETC2:
262     case COMPRESSED_RGB8_ETC1:
263     case COMPRESSED_RGB_PVRTC_4BPPV1:
264     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
265     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
266     case COMPRESSED_RGBA8_ETC2_EAC:
267     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
268     case COMPRESSED_RGBA_ASTC_4x4_KHR:
269     case COMPRESSED_RGBA_ASTC_5x4_KHR:
270     case COMPRESSED_RGBA_ASTC_5x5_KHR:
271     case COMPRESSED_RGBA_ASTC_6x5_KHR:
272     case COMPRESSED_RGBA_ASTC_6x6_KHR:
273     case COMPRESSED_RGBA_ASTC_8x5_KHR:
274     case COMPRESSED_RGBA_ASTC_8x6_KHR:
275     case COMPRESSED_RGBA_ASTC_8x8_KHR:
276     case COMPRESSED_RGBA_ASTC_10x5_KHR:
277     case COMPRESSED_RGBA_ASTC_10x6_KHR:
278     case COMPRESSED_RGBA_ASTC_10x8_KHR:
279     case COMPRESSED_RGBA_ASTC_10x10_KHR:
280     case COMPRESSED_RGBA_ASTC_12x10_KHR:
281     case COMPRESSED_RGBA_ASTC_12x12_KHR:
282     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
283     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
284     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
285     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
286     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
287     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
288     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
289     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
290     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
291     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
292     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
293     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
294     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
295     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
296     case RGB16F:
297     case RGB32F:
298     case DEPTH_UNSIGNED_INT:
299     case DEPTH_FLOAT:
300     case DEPTH_STENCIL:
301     case R11G11B10F:
302     case INVALID:
303     {
304       DALI_LOG_ERROR("Pixel formats are not compatible with simple masking-out of per-pixel alpha.\n");
305       byteOffset = 0;
306       bitMask    = 0;
307       break;
308     }
309   }
310 }
311
312 } // namespace Dali