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