Bitmap core patch 2 of 4 - Replace all uses of the Bitmap class with new simpler...
[platform/core/uifw/dali-core.git] / dali / public-api / images / pixel.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/public-api/images/pixel.h>
19 #include <dali/public-api/images/pixel-extras.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 namespace Dali
25 {
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     {
46       return true;
47     }
48
49     case L8:
50     case RGB565:
51     case RGB888:
52     case RGB8888: // alpha not used
53     case BGR8888: // alpha not used
54     case BGR565:
55     case COMPRESSED_R11_EAC:
56     case COMPRESSED_SIGNED_R11_EAC:
57     case COMPRESSED_RG11_EAC:
58     case COMPRESSED_SIGNED_RG11_EAC:
59     case COMPRESSED_RGB8_ETC2:
60     case COMPRESSED_SRGB8_ETC2:
61     case COMPRESSED_RGB8_ETC1:
62     case COMPRESSED_RGB_PVRTC_4BPPV1:
63     {
64       return false;
65     }
66   }
67   return false;
68 }
69
70 unsigned int Pixel::GetBytesPerPixel(Format pixelFormat)
71 {
72   switch (pixelFormat)
73   {
74     case L8:
75     case A8:
76     {
77       return 1;
78     }
79
80     case LA88:
81     case RGB565:
82     case RGBA4444:
83     case RGBA5551:
84     case BGR565:
85     case BGRA4444:
86     case BGRA5551:
87     {
88       return 2;
89     }
90
91     case RGB888:
92     {
93       return 3;
94     }
95
96     case RGB8888:
97     case BGR8888:
98     case RGBA8888:
99     case BGRA8888:
100     {
101       return 4;
102     }
103
104     case COMPRESSED_R11_EAC:
105     case COMPRESSED_SIGNED_R11_EAC:
106     case COMPRESSED_RG11_EAC:
107     case COMPRESSED_SIGNED_RG11_EAC:
108     case COMPRESSED_RGB8_ETC2:
109     case COMPRESSED_SRGB8_ETC2:
110     case COMPRESSED_RGB8_ETC1:
111     case COMPRESSED_RGB_PVRTC_4BPPV1:
112     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
113     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
114     case COMPRESSED_RGBA8_ETC2_EAC:
115     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
116     {
117       DALI_LOG_ERROR("Pixel formats for compressed images do not have meaningful integer bits per pixel values.\n");
118       return 0;
119     }
120   }
121   return 0;
122 }
123
124 bool Pixel::IsEncoded( const Format pixelFormat )
125 {
126   switch (pixelFormat)
127   {
128     // All "normal" formats of addressable packed pixels are not encoded:
129     case L8:
130     case A8:
131     case LA88:
132     case RGB565:
133     case RGBA4444:
134     case RGBA5551:
135     case BGR565:
136     case BGRA4444:
137     case BGRA5551:
138     case RGB888:
139     case RGB8888:
140     case BGR8888:
141     case RGBA8888:
142     case BGRA8888:
143     {
144       return false;
145     }
146
147     // All compressed texture formats are encoded:
148     case COMPRESSED_R11_EAC:
149     case COMPRESSED_SIGNED_R11_EAC:
150     case COMPRESSED_RG11_EAC:
151     case COMPRESSED_SIGNED_RG11_EAC:
152     case COMPRESSED_RGB8_ETC2:
153     case COMPRESSED_SRGB8_ETC2:
154     case COMPRESSED_RGB8_ETC1:
155     case COMPRESSED_RGB_PVRTC_4BPPV1:
156     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
157     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
158     case COMPRESSED_RGBA8_ETC2_EAC:
159     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
160     {
161       // Fall through to default return value:
162       break;
163     }
164   }
165   return true;
166 }
167
168 void Pixel::GetAlphaOffsetAndMask(Format pixelFormat, int& byteOffset, int& bitMask)
169 {
170   switch (pixelFormat)
171   {
172     case A8:
173     {
174       byteOffset = 0;
175       bitMask    = 0xFF;
176     }
177     break;
178
179     case L8:
180     case RGB888:
181     case RGB565:
182     case RGB8888:
183     case BGR8888:
184     case BGR565:
185     {
186       byteOffset=0;
187       bitMask=0;
188       break;
189     }
190
191     case LA88:
192     {
193       byteOffset=1;
194       bitMask=0xff;
195       break;
196     }
197
198     case RGBA4444:
199     case BGRA4444:
200     {
201       byteOffset=1;
202       bitMask=0x0f;
203       break;
204     }
205
206     case RGBA5551:
207     case BGRA5551:
208     {
209       byteOffset=1;
210       bitMask=0x01;
211       break;
212     }
213
214     case RGBA8888:
215     case BGRA8888:
216     {
217       byteOffset=3;
218       bitMask=0xff;
219       break;
220     }
221
222     case COMPRESSED_R11_EAC:
223     case COMPRESSED_SIGNED_R11_EAC:
224     case COMPRESSED_RG11_EAC:
225     case COMPRESSED_SIGNED_RG11_EAC:
226     case COMPRESSED_RGB8_ETC2:
227     case COMPRESSED_SRGB8_ETC2:
228     case COMPRESSED_RGB8_ETC1:
229     case COMPRESSED_RGB_PVRTC_4BPPV1:
230     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
231     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
232     case COMPRESSED_RGBA8_ETC2_EAC:
233     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
234     {
235       DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
236       byteOffset=0;
237       bitMask=0;
238       break;
239     }
240   }
241 }
242
243 } // namespace Dali