[SRUK] Initial copy from Tizen 2.2 version
[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 L8:
129     case A8:
130     case RGB888:
131     case RGB565:
132     case RGB8888:
133     case BGR8888:
134     case BGR565:
135     {
136       byteOffset=0;
137       bitMask=0;
138       break;
139     }
140
141     case LA88:
142     {
143       byteOffset=1;
144       bitMask=0xff;
145       break;
146     }
147
148     case RGBA4444:
149     case BGRA4444:
150     {
151       byteOffset=1;
152       bitMask=0x0f;
153       break;
154     }
155
156     case RGBA5551:
157     case BGRA5551:
158     {
159       byteOffset=1;
160       bitMask=0x01;
161       break;
162     }
163
164     case RGBA8888:
165     case BGRA8888:
166     {
167       byteOffset=3;
168       bitMask=0xff;
169       break;
170     }
171
172     case COMPRESSED_R11_EAC:
173     case COMPRESSED_SIGNED_R11_EAC:
174     case COMPRESSED_RG11_EAC:
175     case COMPRESSED_SIGNED_RG11_EAC:
176     case COMPRESSED_RGB8_ETC2:
177     case COMPRESSED_SRGB8_ETC2:
178     case COMPRESSED_RGB8_ETC1:
179     case COMPRESSED_RGB_PVRTC_4BPPV1:
180     case COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
181     case COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
182     case COMPRESSED_RGBA8_ETC2_EAC:
183     case COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
184     {
185       DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
186       byteOffset=0;
187       bitMask=0;
188       break;
189     }
190   }
191 }
192
193 } // namespace Dali