[dali_1.2.60] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / public-api / images / pixel.cpp
1 /*
2  * Copyright (c) 2017 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/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
24
25 namespace Dali
26 {
27
28 bool Pixel::HasAlpha(Format pixelformat)
29 {
30   switch (pixelformat)
31   {
32     case RGBA5551:
33     case RGBA8888:
34     case RGBA4444:
35     case BGRA8888:
36     case BGRA4444:
37     case BGRA5551:
38     case A8:
39     case LA88:
40     // Note, Can be used for alpha if you want: COMPRESSED_R11_EAC,                       ///< ETC2 / EAC single-channel, unsigned
41     // Note, Can be used for alpha if you want: COMPRESSED_SIGNED_R11_EAC,                ///< ETC2 / EAC single-channel, signed
42     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
43     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
44     case COMPRESSED_RGBA8_ETC2_EAC:
45     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
46     case COMPRESSED_RGBA_ASTC_4x4_KHR:
47     case COMPRESSED_RGBA_ASTC_5x4_KHR:
48     case COMPRESSED_RGBA_ASTC_5x5_KHR:
49     case COMPRESSED_RGBA_ASTC_6x5_KHR:
50     case COMPRESSED_RGBA_ASTC_6x6_KHR:
51     case COMPRESSED_RGBA_ASTC_8x5_KHR:
52     case COMPRESSED_RGBA_ASTC_8x6_KHR:
53     case COMPRESSED_RGBA_ASTC_8x8_KHR:
54     case COMPRESSED_RGBA_ASTC_10x5_KHR:
55     case COMPRESSED_RGBA_ASTC_10x6_KHR:
56     case COMPRESSED_RGBA_ASTC_10x8_KHR:
57     case COMPRESSED_RGBA_ASTC_10x10_KHR:
58     case COMPRESSED_RGBA_ASTC_12x10_KHR:
59     case COMPRESSED_RGBA_ASTC_12x12_KHR:
60     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
61     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
62     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
63     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
64     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
65     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
66     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
67     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
68     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
69     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
70     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
71     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
72     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
73     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
74     {
75       return true;
76     }
77
78     case L8:
79     case RGB565:
80     case RGB888:
81     case RGB8888: // alpha not used
82     case BGR8888: // alpha not used
83     case BGR565:
84     case COMPRESSED_R11_EAC:
85     case COMPRESSED_SIGNED_R11_EAC:
86     case COMPRESSED_RG11_EAC:
87     case COMPRESSED_SIGNED_RG11_EAC:
88     case COMPRESSED_RGB8_ETC2:
89     case COMPRESSED_SRGB8_ETC2:
90     case COMPRESSED_RGB8_ETC1:
91     case COMPRESSED_RGB_PVRTC_4BPPV1:
92     case RGB16F:
93     case RGB32F:
94     case INVALID:
95     {
96       return false;
97     }
98   }
99   return false;
100 }
101
102 unsigned int Pixel::GetBytesPerPixel(Format pixelFormat)
103 {
104   switch (pixelFormat)
105   {
106     case L8:
107     case A8:
108     {
109       return 1;
110     }
111
112     case LA88:
113     case RGB565:
114     case RGBA4444:
115     case RGBA5551:
116     case BGR565:
117     case BGRA4444:
118     case BGRA5551:
119     {
120       return 2;
121     }
122
123     case RGB888:
124     {
125       return 3;
126     }
127
128     case RGB8888:
129     case BGR8888:
130     case RGBA8888:
131     case BGRA8888:
132     {
133       return 4;
134     }
135
136     case RGB16F:
137     {
138       return 12;
139     }
140
141     case RGB32F:
142     {
143       return 24;
144     }
145
146     case COMPRESSED_R11_EAC:
147     case COMPRESSED_SIGNED_R11_EAC:
148     case COMPRESSED_RG11_EAC:
149     case COMPRESSED_SIGNED_RG11_EAC:
150     case COMPRESSED_RGB8_ETC2:
151     case COMPRESSED_SRGB8_ETC2:
152     case COMPRESSED_RGB8_ETC1:
153     case COMPRESSED_RGB_PVRTC_4BPPV1:
154     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
155     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
156     case COMPRESSED_RGBA8_ETC2_EAC:
157     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
158     case COMPRESSED_RGBA_ASTC_4x4_KHR:
159     case COMPRESSED_RGBA_ASTC_5x4_KHR:
160     case COMPRESSED_RGBA_ASTC_5x5_KHR:
161     case COMPRESSED_RGBA_ASTC_6x5_KHR:
162     case COMPRESSED_RGBA_ASTC_6x6_KHR:
163     case COMPRESSED_RGBA_ASTC_8x5_KHR:
164     case COMPRESSED_RGBA_ASTC_8x6_KHR:
165     case COMPRESSED_RGBA_ASTC_8x8_KHR:
166     case COMPRESSED_RGBA_ASTC_10x5_KHR:
167     case COMPRESSED_RGBA_ASTC_10x6_KHR:
168     case COMPRESSED_RGBA_ASTC_10x8_KHR:
169     case COMPRESSED_RGBA_ASTC_10x10_KHR:
170     case COMPRESSED_RGBA_ASTC_12x10_KHR:
171     case COMPRESSED_RGBA_ASTC_12x12_KHR:
172     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
173     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
174     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
175     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
176     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
177     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
178     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
179     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
180     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
181     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
182     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
183     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
184     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
185     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
186     case INVALID:
187     {
188       DALI_LOG_ERROR("Pixel formats for compressed images do not have meaningful integer bits per pixel values.\n");
189       return 0;
190     }
191   }
192   return 0;
193 }
194
195 void Pixel::GetAlphaOffsetAndMask(Format pixelFormat, int& byteOffset, int& bitMask)
196 {
197   switch (pixelFormat)
198   {
199     case A8:
200     {
201       byteOffset = 0;
202       bitMask    = 0xFF;
203     }
204     break;
205
206     case L8:
207     case RGB888:
208     case RGB565:
209     case RGB8888:
210     case BGR8888:
211     case BGR565:
212     {
213       byteOffset=0;
214       bitMask=0;
215       break;
216     }
217
218     case LA88:
219     {
220       byteOffset=1;
221       bitMask=0xff;
222       break;
223     }
224
225     case RGBA4444:
226     case BGRA4444:
227     {
228       byteOffset=1;
229       bitMask=0x0f;
230       break;
231     }
232
233     case RGBA5551:
234     case BGRA5551:
235     {
236       byteOffset=1;
237       bitMask=0x01;
238       break;
239     }
240
241     case RGBA8888:
242     case BGRA8888:
243     {
244       byteOffset=3;
245       bitMask=0xff;
246       break;
247     }
248
249     case COMPRESSED_R11_EAC:
250     case COMPRESSED_SIGNED_R11_EAC:
251     case COMPRESSED_RG11_EAC:
252     case COMPRESSED_SIGNED_RG11_EAC:
253     case COMPRESSED_RGB8_ETC2:
254     case COMPRESSED_SRGB8_ETC2:
255     case COMPRESSED_RGB8_ETC1:
256     case COMPRESSED_RGB_PVRTC_4BPPV1:
257     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
258     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
259     case COMPRESSED_RGBA8_ETC2_EAC:
260     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
261     case COMPRESSED_RGBA_ASTC_4x4_KHR:
262     case COMPRESSED_RGBA_ASTC_5x4_KHR:
263     case COMPRESSED_RGBA_ASTC_5x5_KHR:
264     case COMPRESSED_RGBA_ASTC_6x5_KHR:
265     case COMPRESSED_RGBA_ASTC_6x6_KHR:
266     case COMPRESSED_RGBA_ASTC_8x5_KHR:
267     case COMPRESSED_RGBA_ASTC_8x6_KHR:
268     case COMPRESSED_RGBA_ASTC_8x8_KHR:
269     case COMPRESSED_RGBA_ASTC_10x5_KHR:
270     case COMPRESSED_RGBA_ASTC_10x6_KHR:
271     case COMPRESSED_RGBA_ASTC_10x8_KHR:
272     case COMPRESSED_RGBA_ASTC_10x10_KHR:
273     case COMPRESSED_RGBA_ASTC_12x10_KHR:
274     case COMPRESSED_RGBA_ASTC_12x12_KHR:
275     case COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
276     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
277     case COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
278     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
279     case COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
280     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
281     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
282     case COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
283     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
284     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
285     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
286     case COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
287     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
288     case COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
289     case RGB16F:
290     case RGB32F:
291     case INVALID:
292     {
293       DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
294       byteOffset=0;
295       bitMask=0;
296       break;
297     }
298   }
299 }
300
301 } // namespace Dali