Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / util / FGrp_UtilScratchpad.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_UtilScratchpad.h
20  * @brief       This is the header file for internal ScratchPad class.
21  *
22  */
23
24 #ifndef _FGRP_INTERNAL_UTIL_SCRATCHPAD_H_
25 #define _FGRP_INTERNAL_UTIL_SCRATCHPAD_H_
26
27
28 namespace Tizen { namespace Graphics
29 {
30
31 namespace _Util
32 {
33 struct ColorKey
34 {
35         ColorKey()
36                 : isValid(false)
37                 , colorKey(0)
38         {
39         }
40
41         explicit ColorKey(unsigned long _colorKey)
42                 : isValid(true)
43                 , colorKey(_colorKey)
44         {
45         }
46
47         explicit ColorKey(unsigned long _a, unsigned long _r, unsigned long _g, unsigned long _b)
48                 : isValid(true)
49         {
50                 if (_a > 255)
51                 {
52                         _a = 255;
53                 }
54
55                 if (_r > 255)
56                 {
57                         _r = 255;
58                 }
59
60                 if (_g > 255)
61                 {
62                         _g = 255;
63                 }
64
65                 if (_b > 255)
66                 {
67                         _b = 255;
68                 }
69
70                 colorKey = (_a << 24) | (_r << 16) | (_g << 8) | (_b << 0);
71         }
72
73         bool isValid;
74         unsigned long colorKey;
75 };
76
77 // external buffer manipulation tool
78 template<typename Pixel>
79 class ScratchPad
80 {
81 public:
82         enum
83         {
84                 SCRATCH_PAD_VER_1_2
85         };
86
87         typedef void (*FillRectFunc)(Pixel* pDest32, int w, int h, int pitch, unsigned long color, unsigned long opacity);
88         typedef void (*BitBltFunc)(Pixel* pDest32, int w, int h, int pitch, Pixel* pSour32, int imagePitch, unsigned long opacity,
89                                                           ColorKey colorKey);
90
91         ScratchPad(Pixel* pBuffer, int width, int height, int pitch, ColorKey colorKey = ColorKey())
92                 : __pBuffer(pBuffer)
93                 , __width(width)
94                 , __height(height)
95                 , __xPitch(sizeof(Pixel))
96                 , __yPitch(pitch)
97                 , __colorKey(colorKey)
98                 , __fnFillRect(__FillRect)
99                 , __fnBitBlt(__BitBlt)
100         {
101                 __depth = sizeof(Pixel) * 8;
102                 __bytesPerLine = pitch * sizeof(Pixel);
103         }
104
105         void DrawPoint(int x, int y, Pixel color)
106         {
107                 if (!__IsInRegion(x, y))
108                 {
109                         return;
110                 }
111
112                 *__GetAddress(x, y) = color;
113         }
114
115         void FillRect(int x, int y, int w, int h, Pixel color, unsigned long opacity = 255)
116         {
117                 if (!__FitToRegion(x, y, w, h))
118                 {
119                         return;
120                 }
121
122                 __fnFillRect(__GetAddress(x, y), w, h, __yPitch, color, opacity);
123         }
124
125         void BitBlt(int xDest, int yDest, ScratchPad* pImage, int xSour, int ySour, int wSour, int hSour, unsigned long opacity = 255)
126         {
127                 if (pImage == 0)
128                 {
129                         return;
130                 }
131
132                 {
133                         int x = xSour;
134                         int y = ySour;
135
136                         if (!pImage->__FitToRegion(xSour, ySour, wSour, hSour))
137                         {
138                                 return;
139                         }
140
141                         xDest += (xSour - x);
142                         yDest += (ySour - y);
143                 }
144
145                 {
146                         int x = xDest;
147                         int y = yDest;
148
149                         if (!__FitToRegion(xDest, yDest, wSour, hSour))
150                         {
151                                 return;
152                         }
153
154                         xSour += (xDest - x);
155                         ySour += (yDest - y);
156                 }
157
158                 int srcPitch = pImage->__bytesPerLine / (pImage->__depth / 8);
159
160                 __fnBitBlt(__GetAddress(xDest, yDest), wSour, hSour, __yPitch, pImage->__GetAddress(xSour,
161                                                                                                                                                                                         ySour), srcPitch, opacity,
162                                    pImage->__colorKey);
163         }
164
165         int GetWidth(void) const
166         {
167                 return __width;
168         }
169
170         int GetHeight(void) const
171         {
172                 return __height;
173         }
174
175         void RegisterFillRect(FillRectFunc fnFillRect)
176         {
177                 __fnFillRect = (fnFillRect) ? fnFillRect : __FillRect;
178         }
179
180         void RegisterBitBlt(BitBltFunc fnBitBlt)
181         {
182                 __fnBitBlt = (fnBitBlt) ? fnBitBlt : __BitBlt;
183         }
184
185         // for internal purposes
186         void GetBuffer(Pixel*& pAddr, int& pitch) const
187         {
188                 pAddr = __pBuffer;
189                 pitch = __yPitch;
190         }
191
192         // for internal purposes
193         ColorKey GetColorKey(void) const
194         {
195                 return __colorKey;
196         }
197
198         // for internal purposes
199         void SetColorKey(ColorKey colorKey)
200         {
201                 __colorKey = colorKey;
202         }
203
204         // for internal purposes
205         void SetPitch(int depth, int bytesPerLine)
206         {
207                 __depth = depth;
208                 __xPitch = depth / 8;
209                 __bytesPerLine = bytesPerLine;
210         }
211
212 private:
213         bool __IsInRegion(int x, int y)
214         {
215                 return (x >= 0 && x < __width && y >= 0 && y < __height);
216         }
217
218         bool __FitToRegion(int& x1, int& y1, int& w, int& h)
219         {
220                 int x2 = x1 + w;
221                 int y2 = y1 + h;
222
223                 x1 = (x1 < 0) ? 0 : x1;
224                 y1 = (y1 < 0) ? 0 : y1;
225                 x2 = (x2 > __width) ? __width : x2;
226                 y2 = (y2 > __height) ? __height : y2;
227
228                 w = x2 - x1;
229                 h = y2 - y1;
230
231                 return (w >= 0) && (h >= 0);
232         }
233
234         Pixel* __GetAddress(int x, int y)
235         {
236                 unsigned char* pBuffer = (unsigned char*) (__pBuffer) + y * __bytesPerLine;
237                 return (Pixel*) (pBuffer + (x * __xPitch));
238         }
239
240         static void __FillRect(Pixel* pDest, int w, int h, int pitch, unsigned long color, unsigned long opacity);
241         static void __BitBlt(Pixel* pDest, int w, int h, int pitch, Pixel* pSour, int imagePitch, unsigned long opacity, ColorKey srcColorKey);
242
243 private:
244         Pixel* __pBuffer;
245         int __width;
246         int __height;
247         int __xPitch;
248         int __yPitch;
249         int __bytesPerLine;
250         int __depth;
251         ColorKey __colorKey;
252
253         FillRectFunc __fnFillRect;
254         BitBltFunc __fnBitBlt;
255 }; // ScratchPad
256
257 } // Tizen::Graphics::_Util
258
259 }} // Tizen::Graphics
260
261 #endif // #ifndef _FGRP_INTERNAL_UTIL_SCRATCHPAD_H_