Revert " modify license, permission and remove ^M char"
[framework/osp/uifw.git] / src / graphics / effect / FGrp_EffectFunc.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /*
19  * @file        FGrp_EffectFunc.h
20  * @brief       This is the header file for internal utility class.
21  *
22  */
23
24 #ifndef _FGRP_INTERNAL_EFFECT_FUNC_H_
25 #define _FGRP_INTERNAL_EFFECT_FUNC_H_
26
27 namespace Tizen { namespace Graphics
28 {
29
30 namespace _Effect
31 {
32
33 ////////////////////////////////////////////////////////////////////////////////
34 // alpha blending rule
35
36 template<typename T>
37 inline T
38 _LocalMin(T a, T b)
39 {
40         return (a < b) ? a : b;
41 }
42
43 // 'Add' rule is more intuitive for 32-bit alpha
44 template<typename T>
45 inline T
46 _BlendAlphaComponent(T srcA, T dstA)
47 {
48         return _LocalMin<T>((srcA) + (dstA), 0xFF);
49 }
50
51 // general rule. The attenuation occurs for destination alpha
52 // Ad = As + Ad * (1-As)
53 template<typename T>
54 inline T
55 _BlendAlphaComponent2(T srcA, T dstA)
56 {
57         return ((srcA) + (((dstA) * (255 - (srcA))) >> 8));
58 }
59
60 template<typename T>
61 inline T
62 _BlendAlphaComponent3(T srcA, T dstA)
63 {
64         return (((dstA) + ((((srcA) - (dstA)) * (srcA)) >> 8)) & 0xFF);
65 }
66
67
68 namespace Func
69 {
70
71 ////////////////////////////////////////////////////////////////////////////
72 // template AlphaBlending()
73
74 template<typename DestPixel, typename SourPixel>
75 inline DestPixel
76 AlphaBlending(DestPixel dest, SourPixel sour, DestPixel ca)
77 {
78         // assert(false);
79 }
80
81 template<>
82 inline unsigned long AlphaBlending <unsigned long, unsigned long
83                                                                         >(unsigned long dest, unsigned long sour, unsigned long ca)
84 {
85         typedef unsigned long DestPixel;
86         typedef unsigned long SourPixel;
87
88         DestPixel srcA = ca * (sour >> 24) >> 8;
89         DestPixel srcR = sour & 0x00FF0000;
90         DestPixel srcG = sour & 0x0000FF00;
91         DestPixel srcB = sour & 0x000000FF;
92
93         DestPixel dstA = (dest & 0xFF000000) >> 24;
94         DestPixel dstR = dest & 0x00FF0000;
95         DestPixel dstG = dest & 0x0000FF00;
96         DestPixel dstB = dest & 0x000000FF;
97
98         dstA = _BlendAlphaComponent(srcA, dstA);
99         srcA += (srcA >> 7);
100         dstR = (dstR + (((srcR - dstR) * srcA) >> 8)) & 0x00FF0000;
101         dstG = (dstG + (((srcG - dstG) * srcA) >> 8)) & 0x0000FF00;
102         dstB = (dstB + (((srcB - dstB) * srcA) >> 8)) & 0x000000FF;
103         dstA = (dstA > 0xFF) ? 0xFF : dstA;
104
105         return (dstA << 24) | dstR | dstG | dstB;
106 }
107
108 template<>
109 inline unsigned long AlphaBlending <unsigned long, unsigned short
110                                                                         >(unsigned long dest, unsigned short sour, unsigned long srcA)
111 {
112         typedef unsigned long DestPixel;
113         typedef unsigned short SourPixel;
114
115         DestPixel srcR = sour & 0xF800;
116         srcR = (srcR << 8 | srcR << 3) & 0x00FF0000;
117         DestPixel srcG = sour & 0x07E0;
118         srcG = (srcG << 5 | srcG >> 1) & 0x0000FF00;
119         DestPixel srcB = sour & 0x001F;
120         srcB = (srcB << 3 | srcB >> 2) & 0x000000FF;
121
122         DestPixel dstA = (dest & 0xFF000000) >> 24;
123         DestPixel dstR = dest & 0x00FF0000;
124         DestPixel dstG = dest & 0x0000FF00;
125         DestPixel dstB = dest & 0x000000FF;
126
127         dstA = _BlendAlphaComponent(srcA, dstA);
128         dstR = (dstR + (((srcR - dstR) * srcA) >> 8)) & 0x00FF0000;
129         dstG = (dstG + (((srcG - dstG) * srcA) >> 8)) & 0x0000FF00;
130         dstB = (dstB + (((srcB - dstB) * srcA) >> 8)) & 0x000000FF;
131         dstA = (dstA > 0xFF) ? 0xFF : dstA;
132
133         return (dstA << 24) | dstR | dstG | dstB;
134 }
135
136 template<>
137 inline unsigned short AlphaBlending <unsigned short, unsigned short
138                                                                         >(unsigned short dest, unsigned short sour, unsigned short ca)
139 {
140         typedef unsigned short DestPixel;
141         typedef unsigned short SourPixel;
142
143         DestPixel srcR = (sour >> 11) & 0x001F;
144         DestPixel srcG = (sour >> 5) & 0x003F;
145         DestPixel srcB = (sour) & 0x001F;
146
147         DestPixel dstR = (dest >> 11) & 0x001F;
148         DestPixel dstG = (dest >> 5) & 0x003F;
149         DestPixel dstB = (dest) & 0x001F;
150
151         dstR = (dstR + (((srcR - dstR) * ca) >> 8)) & 0x001F;
152         dstG = (dstG + (((srcG - dstG) * ca) >> 8)) & 0x003F;
153         dstB = (dstB + (((srcB - dstB) * ca) >> 8)) & 0x001F;
154
155         return DestPixel((dstR << 11) | (dstG << 5) | dstB);
156 }
157
158 template<>
159 inline unsigned short AlphaBlending <unsigned short, unsigned long
160                                                                         >(unsigned short dest, unsigned long sour, unsigned short ca)
161 {
162         typedef unsigned short DestPixel;
163         typedef unsigned long SourPixel;
164
165         DestPixel srcA = DestPixel(ca * (sour >> 24) >> 8);
166         DestPixel srcR = DestPixel((sour & 0x00F80000) >> 19);
167         DestPixel srcG = DestPixel((sour & 0x0000FC00) >> 10);
168         DestPixel srcB = DestPixel((sour & 0x000000F8) >> 3);
169
170         DestPixel dstR = (dest >> 11) & 0x001F;
171         DestPixel dstG = (dest >> 5) & 0x003F;
172         DestPixel dstB = (dest) & 0x001F;
173
174         dstR = (dstR + (((srcR - dstR) * srcA) >> 8)) & 0x001F;
175         dstG = (dstG + (((srcG - dstG) * srcA) >> 8)) & 0x003F;
176         dstB = (dstB + (((srcB - dstB) * srcA) >> 8)) & 0x001F;
177
178         return DestPixel((dstR << 11) | (dstG << 5) | dstB);
179 }
180
181 ////////////////////////////////////////////////////////////////////////////
182 // template AlphaBlendingFast()
183
184 template<typename DestPixel, typename SourPixel>
185 inline DestPixel
186 AlphaBlendingFast(DestPixel dest, SourPixel sour)
187 {
188         return (DestPixel) sour;
189 }
190
191 template<>
192 inline unsigned long AlphaBlendingFast <unsigned long, unsigned long
193                                                                                 >(unsigned long dest, unsigned long sour)
194 {
195         typedef unsigned long DestPixel;
196         typedef unsigned long SourPixel;
197
198         DestPixel srcA = sour >> 24;
199         DestPixel srcR = sour & 0x00FF0000;
200         DestPixel srcG = sour & 0x0000FF00;
201         DestPixel srcB = sour & 0x000000FF;
202
203         DestPixel dstA = (dest & 0xFF000000) >> 24;
204         DestPixel dstR = dest & 0x00FF0000;
205         DestPixel dstG = dest & 0x0000FF00;
206         DestPixel dstB = dest & 0x000000FF;
207
208         dstA = _BlendAlphaComponent(srcA, dstA);
209         srcA += (srcA >> 7);
210         dstR = (dstR + (((srcR - dstR) * srcA) >> 8)) & 0x00FF0000;
211         dstG = (dstG + (((srcG - dstG) * srcA) >> 8)) & 0x0000FF00;
212         dstB = (dstB + (((srcB - dstB) * srcA) >> 8)) & 0x000000FF;
213         dstA = (dstA > 0xFF) ? 0xFF : dstA;
214
215         return (dstA << 24) | dstR | dstG | dstB;
216 }
217
218 template<>
219 inline unsigned long AlphaBlendingFast <unsigned long, unsigned short
220                                                                                 >(unsigned long dest, unsigned short sour)
221 {
222         typedef unsigned short SourPixel;
223         typedef unsigned long DestPixel;
224
225         {
226                 DestPixel r = (sour & 0xF800) >> 8;
227                 DestPixel g = (sour & 0x07E0) >> 3;
228                 DestPixel b = (sour & 0x001F) << 3;
229
230                 r += (r >> 5);
231                 g += (g >> 6);
232                 b += (b >> 5);
233
234                 return (0xFF000000 | (r << 16) | (g << 8) | (b));
235         }
236 }
237
238 template<>
239 inline unsigned short AlphaBlendingFast <unsigned short, unsigned long
240                                                                                 >(unsigned short dest, unsigned long sour)
241 {
242         typedef unsigned short DestPixel;
243         typedef unsigned long SourPixel;
244
245         DestPixel srcA = DestPixel((sour >> 24));
246         DestPixel srcR = DestPixel((sour & 0x00F80000) >> 19);
247         DestPixel srcG = DestPixel((sour & 0x0000FC00) >> 10);
248         DestPixel srcB = DestPixel((sour & 0x000000F8) >> 3);
249
250         DestPixel dstR = (dest >> 11) & 0x001F;
251         DestPixel dstG = (dest >> 5) & 0x003F;
252         DestPixel dstB = (dest) & 0x001F;
253
254         srcA += (srcA >> 7);
255
256         dstR = (dstR + (((srcR - dstR) * srcA) >> 8)) & 0x001F;
257         dstG = (dstG + (((srcG - dstG) * srcA) >> 8)) & 0x003F;
258         dstB = (dstB + (((srcB - dstB) * srcA) >> 8)) & 0x001F;
259
260         return DestPixel((dstR << 11) | (dstG << 5) | dstB);
261 }
262
263 ////////////////////////////////////////////////////////////////////////////
264 // template ConvertColorFormat()
265
266 template<typename DestPixel, typename SourPixel>
267 inline void
268 ConvertColorFormatFast(DestPixel* pDest, SourPixel* pSour)
269 {
270         *pDest = *pSour;
271 }
272
273 template<>
274 inline void
275 ConvertColorFormatFast(unsigned long* pDest, unsigned short* pSour)
276 {
277         typedef unsigned short SourPixel;
278         typedef unsigned long DestPixel;
279
280         {
281                 DestPixel r = (*pSour & 0xF800) >> 8;
282                 DestPixel g = (*pSour & 0x07E0) >> 3;
283                 DestPixel b = (*pSour & 0x001F) << 3;
284
285                 r += (r >> 5);
286                 g += (g >> 6);
287                 b += (b >> 5);
288
289                 *pDest = 0xFF000000 | (r << 16) | (g << 8) | (b);
290         }
291 }
292
293 template<>
294 inline void
295 ConvertColorFormatFast(unsigned short* pDest, unsigned long* pSour)
296 {
297         typedef unsigned long SourPixel;
298         typedef unsigned short DestPixel;
299
300         {
301                 DestPixel r = (DestPixel) ((*pSour & 0xF80000) >> 8);
302                 DestPixel g = (DestPixel) ((*pSour & 0x00FC00) >> 5);
303                 DestPixel b = (DestPixel) ((*pSour & 0x0000F8) >> 3);
304
305                 *pDest = r | g | b;
306         }
307 }
308
309 template<typename DestPixel, typename SourPixel>
310 inline DestPixel*
311 ConvertColorFormat(DestPixel* pDest, SourPixel* pSour, int count)
312 {
313         DestPixel* pDestEnd = pDest + count;
314
315         while (pDest < pDestEnd)
316         {
317                 *pDest++ = *pSour++;
318         }
319
320         return pDestEnd;
321 }
322
323 template<>
324 inline unsigned long*
325 ConvertColorFormat(unsigned long* pDest, unsigned short* pSour, int count)
326 {
327         typedef unsigned short SourPixel;
328         typedef unsigned long DestPixel;
329
330         DestPixel* pDestEnd = pDest + count;
331
332         while (pDest < pDestEnd)
333         {
334                 DestPixel r = (*pSour & 0xF800) >> 8;
335                 DestPixel g = (*pSour & 0x07E0) >> 3;
336                 DestPixel b = (*pSour & 0x001F) << 3;
337
338                 r += (r >> 5);
339                 g += (g >> 6);
340                 b += (b >> 5);
341
342                 *pDest++ = 0xFF000000 | (r << 16) | (g << 8) | (b);
343                 ++pSour;
344         }
345
346         return pDestEnd;
347 }
348
349 template<>
350 inline unsigned short*
351 ConvertColorFormat(unsigned short* pDest, unsigned long* pSour, int count)
352 {
353         typedef unsigned long SourPixel;
354         typedef unsigned short DestPixel;
355
356         DestPixel* pDestEnd = pDest + count;
357
358         while (pDest < pDestEnd)
359         {
360                 DestPixel r = (DestPixel) ((*pSour & 0xF80000) >> 8);
361                 DestPixel g = (DestPixel) ((*pSour & 0x00FC00) >> 5);
362                 DestPixel b = (DestPixel) ((*pSour & 0x0000F8) >> 3);
363
364                 *pDest++ = r | g | b;
365                 ++pSour;
366         }
367
368         return pDestEnd;
369 }
370
371 } // namespace Func
372
373 } // _Effect
374
375 }} // Tizen::Graphics
376
377 #endif // #ifndef _FGRP_INTERNAL_EFFECT_FUNC_H_