Revert " modify license, permission and remove ^M char"
[framework/osp/uifw.git] / src / graphics / FGrp_NonScale.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_NonScale.cpp
20  * @brief       This is the header file for internal util class.
21  *
22  */
23
24 #include <new>
25 #include <memory>
26
27 #include <FBaseByteBuffer.h>
28
29 #include "FGrp_NonScale.h"
30 #include "FGrp_BitmapImpl.h"
31 #include "FGrp_BitmapCoordHolder.h"
32 #include "FGrp_BitmapUtil.h"
33 #include "FGrp_Bitmap.h"
34 #include "util/FGrp_UtilTemplate.h"
35
36
37 namespace // unnamed
38 {
39
40 Tizen::Graphics::_BitmapCoordinateHolder*
41 _GetBitmapCoordinateHolder(const Tizen::Graphics::_BitmapImpl& bitmap)
42 {
43         class BitmapHacked
44                 : public Tizen::Graphics::_BitmapImpl
45         {
46 public:
47                 inline Tizen::Graphics::_BitmapCoordinateHolder* GetBitmapCoordinateHolder(void)
48                 {
49                         return this->_sharedItem->coordHolder.get();
50                 }
51         };
52
53         return ((BitmapHacked*) &bitmap)->GetBitmapCoordinateHolder();
54 }
55
56 }
57
58 namespace Tizen { namespace Graphics
59 {
60
61 _BitmapImpl*
62 _NonScale::CreateBitmapN(const Tizen::Base::ByteBuffer& buffer, const Dimension& rqDim, BitmapPixelFormat pixelFormat)
63 {
64         FloatDimension pcDimF(static_cast<float>(rqDim.width), static_cast<float>(rqDim.height));
65         FloatDimension vcDimF = _ResUtil::ConvertToVirCoord(pcDimF);
66
67         return _NonScale::CreateBitmapN(buffer, rqDim, pixelFormat, vcDimF);
68 }
69
70 _BitmapImpl*
71 _NonScale::CreateBitmapN(const Tizen::Base::ByteBuffer& buffer, const Dimension& rqDim, BitmapPixelFormat pixelFormat,
72                                                  const FloatDimension& logicalSizeF)
73 {
74         result r = E_SUCCESS;
75
76         SysTryReturn(NID_GRP
77                 , rqDim.width > 0 && rqDim.height > 0
78                 , null
79                 , E_INVALID_ARG
80                 , "[E_INVALID_ARG] The reqired size(%d, %d) is invalid.", rqDim.width, rqDim.height);
81
82         SysTryReturn(NID_GRP
83                 , BITMAP_PIXEL_FORMAT_MIN < pixelFormat && pixelFormat < BITMAP_PIXEL_FORMAT_MAX
84                 , null
85                 , E_INVALID_ARG
86                 , "[E_INVALID_ARG] The given pixel format(%d) is invalid.", pixelFormat);
87
88         {
89                 int bytePerPixel = 0;
90
91                 switch (pixelFormat)
92                 {
93                 case Tizen::Graphics::BITMAP_PIXEL_FORMAT_RGB565:
94                         bytePerPixel = 2;
95                         break;
96                 case Tizen::Graphics::BITMAP_PIXEL_FORMAT_ARGB8888:
97                 case Tizen::Graphics::BITMAP_PIXEL_FORMAT_R8G8B8A8:
98                         bytePerPixel = 4;
99                         break;
100                 default:
101                         SysLogException(NID_GRP, E_INVALID_ARG, "The given pixel format(%d) is not supported.", pixelFormat);
102                         return null;
103                 }
104
105                 SysTryReturn(NID_GRP
106                         , bytePerPixel > 0
107                         , null
108                         , E_UNSUPPORTED_FORMAT
109                         , "[E_UNSUPPORTED_FORMAT] The given bytes-per-pixel(%d) is not supported.", bytePerPixel);
110
111                 int numOfBytes = buffer.GetLimit();
112                 int expectedBufferSize = rqDim.width * rqDim.height * bytePerPixel;
113
114                 SysTryReturn(NID_GRP
115                         , expectedBufferSize <= numOfBytes
116                         , null
117                         , E_INVALID_ARG
118                         , "[E_INVALID_ARG] The buffer size is too small. (expected: %d, actual: %d)", expectedBufferSize, numOfBytes);
119         }
120
121         std::auto_ptr <_BitmapImpl> bitmap(new (std::nothrow) _BitmapImpl);
122
123         SysTryReturn(NID_GRP
124                 , bitmap.get()
125                 , null
126                 , E_OUT_OF_MEMORY
127                 , "[E_OUT_OF_MEMORY] _BitmapImpl is not allocated.");
128
129         _Bitmap* pBitmapEx = _GetBitmapEx(*bitmap.get());
130
131         SysTryReturn(NID_GRP
132                 , pBitmapEx
133                 , null
134                 , E_OUT_OF_MEMORY
135                 , "[E_OUT_OF_MEMORY] _Bitmap is not allocated.");
136
137         r = pBitmapEx->Construct(buffer, rqDim, pixelFormat);
138
139         SysTryReturn(NID_GRP
140                 , !IsFailed(r)
141                 , null
142                 , r
143                 , "[%s] _Bitmap::Construct() failed.", GetErrorMessage(r));
144
145         _BitmapCoordinateHolder* pBitmapCoordinateHolder = _GetBitmapCoordinateHolder(*bitmap.get());
146
147         if (pBitmapCoordinateHolder)
148         {
149                 _Util::Dimension<float> vcDimF = { logicalSizeF.width, logicalSizeF.height };
150                 _Util::Dimension<int> vcDim = { _FloatToIntForSize(vcDimF.w), _FloatToIntForSize(vcDimF.h) };
151                 _Util::Dimension<int> pcDim = { rqDim.width, rqDim.height };
152
153                 pBitmapCoordinateHolder->ResetFromPc(pcDim, vcDim, vcDimF);
154         }
155
156         return bitmap.release();
157 }
158
159 _BitmapImpl*
160 _NonScale::CreateBitmapN(const Dimension& rq_dim, BitmapPixelFormat pixelFormat)
161 {
162         std::auto_ptr <_BitmapImpl> bitmap(new (std::nothrow) _BitmapImpl);
163
164         SysTryReturn(NID_GRP
165                 , bitmap.get()
166                 , null
167                 , E_OUT_OF_MEMORY
168                 , "[E_OUT_OF_MEMORY] _BitmapImpl::Construct() failed.");
169
170         Dimension pc_dim = rq_dim;
171         Dimension vc_dim = _ResUtil::ConvertToVirCoord(pc_dim);
172
173         result r = bitmap->Construct(vc_dim, pixelFormat);
174
175         SysTryReturn(NID_GRP
176                 , !IsFailed(r)
177                 , null
178                 , r
179                 , "[%s] _Bitmap::Construct() failed.", GetErrorMessage(r));
180
181         return bitmap.release();
182 }
183
184 }} // Tizen::Graphics