Fork for IVI: mesa fixing
[profile/ivi/uifw.git] / src / graphics / FGrp_CanvasRasterOp.cpp
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_CanvasRasterOp.cpp
20  * @brief       This is the implementation file for set of raster operation.
21  *
22  */
23
24 #include "FGrp_CanvasRasterOp.h"
25 #include "util/FGrp_UtilTemplate.h"
26
27
28 namespace Tizen { namespace Graphics
29 {
30
31 namespace _RasterOp
32 {
33
34 // 'Add' rule is more intuitive for 32-bit alpha
35 template<typename T>
36 inline T
37 _BlendAlphaComponentAdd(T srcA, T dstA)
38 {
39         return _Util::Min<T>(srcA + dstA, 0xFF);
40 }
41
42 template<typename T>
43 inline T
44 _BlendAlphaComponentAddEx(T srcA, T dstA)
45 {
46         return _Util::Min<T>(srcA + dstA - ((srcA * dstA) >> 8), 0xFF);
47 }
48
49 void
50 FnFillRectAlpha32Bit(unsigned long* pDest32, int w, int h, int pitch, unsigned long color, unsigned long opacity)
51 {
52         typedef unsigned long Pixel;
53
54         opacity += (opacity >> 7);
55         opacity = ((opacity * (color >> 24)) << 16) & 0xFF000000;
56
57         color &= 0x00FFFFFF;
58
59         if (opacity >= 0xFF000000)
60         {
61                 while (--h >= 0)
62                 {
63                         int copy = w;
64
65                         while (--copy >= 0)
66                         {
67                                 *pDest32++ = Pixel((color & 0x00FFFFFF) | opacity);
68                         }
69
70                         pDest32 += (pitch - w);
71                 }
72         }
73         else if (opacity > 0)
74         {
75                 Pixel srcA = opacity >> 24;
76                 Pixel srcR = color & 0x00FF0000;
77                 Pixel srcG = color & 0x0000FF00;
78                 Pixel srcB = color & 0x000000FF;
79
80                 srcA += (srcA >> 7);
81
82                 while (--h >= 0)
83                 {
84                         int copy = w;
85
86                         while (--copy >= 0)
87                         {
88                                 Pixel dstA = (*pDest32 & 0xFF000000) >> 24;
89                                 Pixel dstR = *pDest32 & 0x00FF0000;
90                                 Pixel dstG = *pDest32 & 0x0000FF00;
91                                 Pixel dstB = *pDest32 & 0x000000FF;
92
93                                 dstA = _BlendAlphaComponentAdd(srcA, dstA);
94                                 dstR = (dstR + (((srcR - dstR) * srcA) >> 8)) & 0x00FF0000;
95                                 dstG = (dstG + (((srcG - dstG) * srcA) >> 8)) & 0x0000FF00;
96                                 dstB = (dstB + (((srcB - dstB) * srcA) >> 8)) & 0x000000FF;
97                                 dstA = (dstA > 0xFF) ? 0xFF : dstA;
98
99                                 *pDest32++ = (dstA << 24) | dstR | dstG | dstB;
100                         }
101
102                         pDest32 += (pitch - w);
103                 }
104         }
105 }
106
107 void
108 FnFillRectFont32Bit(unsigned long* pDest32, int w, int h, int pitch, unsigned long color, unsigned long opacity)
109 {
110         typedef unsigned long Pixel;
111
112         Pixel srcR = (color >> 16) & 0xFF;
113         Pixel srcG = (color >> 8) & 0xFF;
114         Pixel srcB = (color) & 0xFF;
115         Pixel alpha = (color >> 24) & 0xFF;
116
117         alpha += (alpha >> 7);
118         alpha = (alpha * opacity) >> 8;
119         alpha += (alpha >> 7);
120
121         if (alpha > 0)
122         {
123                 while (--h >= 0)
124                 {
125                         int copy = w;
126
127                         while (--copy >= 0)
128                         {
129                                 Pixel dstA = (*pDest32 >> 24) & 0xFF;
130                                 Pixel dstR = (*pDest32 >> 16) & 0xFF;
131                                 Pixel dstG = (*pDest32 >> 8) & 0xFF;
132                                 Pixel dstB = (*pDest32) & 0xFF;
133
134                                 dstA = _BlendAlphaComponentAddEx(alpha, dstA);
135                                 dstR = (dstR + (((srcR - dstR) * alpha) >> 8)) & 0xFF;
136                                 dstG = (dstG + (((srcG - dstG) * alpha) >> 8)) & 0xFF;
137                                 dstB = (dstB + (((srcB - dstB) * alpha) >> 8)) & 0xFF;
138
139                                 *pDest32++ = (dstA << 24) | (dstR << 16) | (dstG << 8) | dstB;
140                         }
141
142                         pDest32 += (pitch - w);
143                 }
144         }
145 }
146
147 void
148 FnBitBltFont32Bit(unsigned long* pDest32, int w, int h, int pitch, unsigned long* pSour08, int imagePitch, unsigned long opacity,
149                                   _Util::ColorKey srcColorKey)
150 {
151         typedef unsigned long TPixel;
152
153         TPixel srcR;
154         TPixel srcG;
155         TPixel srcB;
156         TPixel alpha;
157
158         TPixel dstR;
159         TPixel dstG;
160         TPixel dstB;
161
162         srcR = (opacity >> 16) & 0xFF;
163         srcG = (opacity >> 8) & 0xFF;
164         srcB = (opacity) & 0xFF;
165
166         {
167                 while (--h >= 0)
168                 {
169                         int copy = w;
170
171                         while (--copy >= 0)
172                         {
173                                 alpha = *pSour08++;
174
175                                 if (alpha)
176                                 {
177                                         dstR = (*pDest32 >> 16) & 0xFF;
178                                         dstG = (*pDest32 >> 8) & 0xFF;
179                                         dstB = (*pDest32) & 0xFF;
180
181                                         dstR = (dstR + (((srcR - dstR) * alpha) >> 8)) & 0xFF;
182                                         dstG = (dstG + (((srcG - dstG) * alpha) >> 8)) & 0xFF;
183                                         dstB = (dstB + (((srcB - dstB) * alpha) >> 8)) & 0xFF;
184
185                                         *pDest32++ = 0xFF000000 | (dstR << 16) | (dstG << 8) | dstB;
186
187                                         continue;
188                                 }
189
190                                 ++pDest32;
191                         }
192
193                         pDest32 += (pitch - w);
194                         pSour08 += (imagePitch - w);
195                 }
196         }
197 }
198
199 void
200 FnBitBlt32BitCopyReverse(unsigned long* pDest32, int w, int h, int pitch, unsigned long* pSour32, int imagePitch,
201                                                  unsigned long opacity,
202                                                  _Util::ColorKey srcColorKey)
203 {
204         // opacity and srcColorKey should be ignored
205
206         pSour32 += (imagePitch * h + w - 1);
207         pDest32 += (pitch * h + w - 1);
208
209         while (--h >= 0)
210         {
211                 int copy = w;
212
213                 while (--copy >= 0)
214                 {
215                         *pDest32-- = *pSour32--;
216                 }
217
218                 pDest32 -= (pitch - w);
219                 pSour32 -= (imagePitch - w);
220         }
221 }
222
223 } // _RasterOp
224
225 }} // Tizen::Graphics