Revert "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 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
20 // INTERNAL INCLUDES
21 #include <dali/public-api/common/dali-common.h>
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 void Pixel::GetAlphaOffsetAndMask(Format pixelFormat, int& byteOffset, int& bitMask)
125 {
126   switch (pixelFormat)
127   {
128     case A8:
129     {
130       byteOffset = 0;
131       bitMask    = 0xFF;
132     }
133     break;
134
135     case L8:
136     case RGB888:
137     case RGB565:
138     case RGB8888:
139     case BGR8888:
140     case BGR565:
141     {
142       byteOffset=0;
143       bitMask=0;
144       break;
145     }
146
147     case LA88:
148     {
149       byteOffset=1;
150       bitMask=0xff;
151       break;
152     }
153
154     case RGBA4444:
155     case BGRA4444:
156     {
157       byteOffset=1;
158       bitMask=0x0f;
159       break;
160     }
161
162     case RGBA5551:
163     case BGRA5551:
164     {
165       byteOffset=1;
166       bitMask=0x01;
167       break;
168     }
169
170     case RGBA8888:
171     case BGRA8888:
172     {
173       byteOffset=3;
174       bitMask=0xff;
175       break;
176     }
177
178     case COMPRESSED_R11_EAC:
179     case COMPRESSED_SIGNED_R11_EAC:
180     case COMPRESSED_RG11_EAC:
181     case COMPRESSED_SIGNED_RG11_EAC:
182     case COMPRESSED_RGB8_ETC2:
183     case COMPRESSED_SRGB8_ETC2:
184     case COMPRESSED_RGB8_ETC1:
185     case COMPRESSED_RGB_PVRTC_4BPPV1:
186     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
187     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
188     case COMPRESSED_RGBA8_ETC2_EAC:
189     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
190     {
191       DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
192       byteOffset=0;
193       bitMask=0;
194       break;
195     }
196   }
197 }
198
199 } // namespace Dali