License conversion from Flora to Apache 2.0
[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 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     {
47       return true;
48     }
49
50     case L8:
51     case RGB565:
52     case RGB888:
53     case RGB8888: // alpha not used
54     case BGR8888: // alpha not used
55     case BGR565:
56     case COMPRESSED_R11_EAC:
57     case COMPRESSED_SIGNED_R11_EAC:
58     case COMPRESSED_RG11_EAC:
59     case COMPRESSED_SIGNED_RG11_EAC:
60     case COMPRESSED_RGB8_ETC2:
61     case COMPRESSED_SRGB8_ETC2:
62     case COMPRESSED_RGB8_ETC1:
63     case COMPRESSED_RGB_PVRTC_4BPPV1:
64     {
65       return false;
66     }
67   }
68   return false;
69 }
70
71 unsigned int Pixel::GetBytesPerPixel(Format pixelFormat)
72 {
73   switch (pixelFormat)
74   {
75     case L8:
76     case A8:
77     {
78       return 1;
79     }
80
81     case LA88:
82     case RGB565:
83     case RGBA4444:
84     case RGBA5551:
85     case BGR565:
86     case BGRA4444:
87     case BGRA5551:
88     {
89       return 2;
90     }
91
92     case RGB888:
93     {
94       return 3;
95     }
96
97     case RGB8888:
98     case BGR8888:
99     case RGBA8888:
100     case BGRA8888:
101     {
102       return 4;
103     }
104
105     case COMPRESSED_R11_EAC:
106     case COMPRESSED_SIGNED_R11_EAC:
107     case COMPRESSED_RG11_EAC:
108     case COMPRESSED_SIGNED_RG11_EAC:
109     case COMPRESSED_RGB8_ETC2:
110     case COMPRESSED_SRGB8_ETC2:
111     case COMPRESSED_RGB8_ETC1:
112     case COMPRESSED_RGB_PVRTC_4BPPV1:
113     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
114     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
115     case COMPRESSED_RGBA8_ETC2_EAC:
116     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
117     {
118       DALI_LOG_ERROR("Pixel formats for compressed images do not have meaningful integer bits per pixel values.\n");
119       return 0;
120     }
121   }
122   return 0;
123 }
124
125 void Pixel::GetAlphaOffsetAndMask(Format pixelFormat, int& byteOffset, int& bitMask)
126 {
127   switch (pixelFormat)
128   {
129     case A8:
130     {
131       byteOffset = 0;
132       bitMask    = 0xFF;
133     }
134     break;
135
136     case L8:
137     case RGB888:
138     case RGB565:
139     case RGB8888:
140     case BGR8888:
141     case BGR565:
142     {
143       byteOffset=0;
144       bitMask=0;
145       break;
146     }
147
148     case LA88:
149     {
150       byteOffset=1;
151       bitMask=0xff;
152       break;
153     }
154
155     case RGBA4444:
156     case BGRA4444:
157     {
158       byteOffset=1;
159       bitMask=0x0f;
160       break;
161     }
162
163     case RGBA5551:
164     case BGRA5551:
165     {
166       byteOffset=1;
167       bitMask=0x01;
168       break;
169     }
170
171     case RGBA8888:
172     case BGRA8888:
173     {
174       byteOffset=3;
175       bitMask=0xff;
176       break;
177     }
178
179     case COMPRESSED_R11_EAC:
180     case COMPRESSED_SIGNED_R11_EAC:
181     case COMPRESSED_RG11_EAC:
182     case COMPRESSED_SIGNED_RG11_EAC:
183     case COMPRESSED_RGB8_ETC2:
184     case COMPRESSED_SRGB8_ETC2:
185     case COMPRESSED_RGB8_ETC1:
186     case COMPRESSED_RGB_PVRTC_4BPPV1:
187     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
188     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
189     case COMPRESSED_RGBA8_ETC2_EAC:
190     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
191     {
192       DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
193       byteOffset=0;
194       bitMask=0;
195       break;
196     }
197   }
198 }
199
200 } // namespace Dali