Tizen 2.1 base
[platform/framework/native/image.git] / src / FMediaImageBuffer.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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   FMediaImageBuffer.cpp
20  * @brief  This file contains the implementation of subsystem's ImageBuffer.
21  */
22
23 #include <FIoFile.h>
24 #include <FMediaImageBuffer.h>
25 #include <FMediaImageUtil.h>
26 #include <FBaseInternalTypes.h>
27 #include <FBaseSysLog.h>
28 #include <FApp_AppInfo.h>
29 #include "FMedia_ImageBufferImpl.h"
30 #include "FMedia_ImageUtilImpl.h"
31
32 using namespace Tizen::Graphics;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Io;
36 using namespace Tizen::App;
37
38 namespace Tizen { namespace Media
39 {
40
41 ImageBuffer::ImageBuffer(void)
42 {
43         __pImpl = null;
44 }
45
46 ImageBuffer::~ImageBuffer(void)
47 {
48         if (__pImpl != null)
49         {
50                 delete __pImpl;
51                 __pImpl = null;
52         }
53 }
54
55 result
56 ImageBuffer::Construct(int width, int height, MediaPixelFormat pixelFormat,
57         const byte* pData, int length)
58 {
59         result r = E_SUCCESS;
60
61         SysAssertf(__pImpl == null,
62                 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
63
64         _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
65         SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
66
67         r = pImageBufferImpl->Construct(width, height, pixelFormat, pData, length);
68         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , GetLastResult(),
69                         "[%s] Construct instance failed.", GetErrorMessage(GetLastResult()));
70
71         __pImpl = pImageBufferImpl;
72         return r;
73
74 CATCH:
75         if (pImageBufferImpl != null)
76         {
77                 delete pImageBufferImpl;
78                 pImageBufferImpl = null;
79         }
80         return r;
81
82 }
83
84 result
85 ImageBuffer::Construct(const Tizen::Base::String &srcImagePath,
86         const Tizen::Graphics::Rectangle *pDecodingRegion, bool autoRotate)
87 {
88         result r = E_SUCCESS;
89
90         SysAssertf(__pImpl == null,
91                 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
92         SysTryReturnResult(NID_MEDIA, !srcImagePath.IsEmpty(), E_INVALID_ARG,"path is empty.");
93
94         SysTryReturnResult(NID_MEDIA, File::IsFileExist(srcImagePath), E_FILE_NOT_FOUND,
95                 "file not found: %ls", srcImagePath.GetPointer());
96
97         _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
98         SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
99
100         r = pImageBufferImpl->Construct(srcImagePath, pDecodingRegion, autoRotate);
101         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , GetLastResult(),
102                         "[%s] Construct instance failed.", GetErrorMessage(GetLastResult()));
103
104         __pImpl = pImageBufferImpl;
105         return r;
106
107 CATCH:
108         if (pImageBufferImpl != null)
109         {
110                 delete pImageBufferImpl;
111                 pImageBufferImpl = null;
112         }
113         return r;
114 }
115
116 result
117 ImageBuffer::Construct(const Tizen::Base::ByteBuffer &srcImageBuf,
118         const Tizen::Graphics::Rectangle *pDecodingRegion, bool autoRotate)
119 {
120         result r = E_SUCCESS;
121
122         SysAssertf(__pImpl == null,
123                 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
124         SysTryReturnResult(NID_MEDIA, &srcImageBuf != null, E_INVALID_ARG,
125                 "src buffer is null");
126
127         _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
128         SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
129
130         r = pImageBufferImpl->Construct(srcImageBuf, pDecodingRegion, autoRotate);
131         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
132                 "[%s] Construct instance failed.", GetErrorMessage(r));
133
134         __pImpl = pImageBufferImpl;
135         return r;
136
137 CATCH:
138         if (pImageBufferImpl != null)
139         {
140                 delete pImageBufferImpl;
141                 pImageBufferImpl = null;
142         }
143         return r;
144 }
145
146 result
147 ImageBuffer::Construct(const Tizen::Graphics::Bitmap &srcBitmap)
148 {
149         result r = E_SUCCESS;
150
151         SysAssertf(__pImpl == null,
152                 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
153         SysTryReturnResult(NID_MEDIA, (srcBitmap.GetWidth() > 0) && (srcBitmap.GetHeight() > 0)
154                 && (srcBitmap.GetBitsPerPixel() > 0), E_INVALID_ARG,
155                 "Source bitmap is invalid: (%d x %d), bpp = %d.",
156                 srcBitmap.GetWidth(), srcBitmap.GetHeight(), srcBitmap.GetBitsPerPixel());
157
158         _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
159         SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
160
161         r = pImageBufferImpl->Construct(srcBitmap);
162         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
163                 "[%s] Construct instance failed.", GetErrorMessage(r));
164
165         __pImpl = pImageBufferImpl;
166         return r;
167
168 CATCH:
169         if (pImageBufferImpl != null)
170         {
171                 delete pImageBufferImpl;
172                 pImageBufferImpl = null;
173         }
174         return r;
175 }
176
177 bool
178 ImageBuffer::Equals(const Object& rhs) const
179 {
180         SysAssertf(__pImpl != null,
181                 "Not yet constructed. Construct() should be called before use.");
182
183         ImageBuffer* pOther = (const_cast<ImageBuffer*>(dynamic_cast <const ImageBuffer*>(&rhs)));
184         SysTryReturn(NID_MEDIA, pOther != null, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND,
185                 "[E_OBJ_NOT_FOUND] RHS ImageBuffer is not found.");
186
187         return __pImpl->Equals(pOther);
188 }
189
190 int
191 ImageBuffer::GetHashCode() const
192 {
193         int hashCode = 0;
194
195         SysAssertf(__pImpl != null,
196                 "Not yet constructed. Construct() should be called before use.");
197
198         hashCode = __pImpl->GetHashCode();
199         return hashCode;
200 }
201
202 int
203 ImageBuffer::GetHeight() const
204 {
205         SysAssertf(__pImpl != null,
206                 "Not yet constructed. Construct() should be called before use.");
207
208         return __pImpl->GetHeight();
209 }
210
211 int
212 ImageBuffer::GetWidth() const
213 {
214         SysAssertf(__pImpl != null,
215                 "Not yet constructed. Construct() should be called before use.");
216
217         return __pImpl->GetWidth();
218 }
219
220 MediaPixelFormat
221 ImageBuffer::GetPixelFormat() const
222 {
223         SysAssertf(__pImpl != null,
224                 "Not yet constructed. Construct() should be called before use.");
225
226         return __pImpl->GetPixelFormat();
227 }
228
229 ByteBuffer*
230 ImageBuffer::EncodeToBufferN(ImageFormat destImageFormat, int quality) const
231 {
232         SysAssertf(__pImpl != null,
233                 "Not yet constructed. Construct() should be called before use.");
234         SysTryReturn(NID_MEDIA, ((destImageFormat == IMG_FORMAT_BMP) ||
235                 (destImageFormat == IMG_FORMAT_JPG) || (destImageFormat == IMG_FORMAT_PNG)),
236                 null, E_UNSUPPORTED_FORMAT, "[E_UNSUPPORTED_FORMAT] %s = %d",
237                 "destImageFormat", destImageFormat);
238
239         return __pImpl->EncodeToBufferN(destImageFormat, quality);
240 }
241
242 result
243 ImageBuffer::EncodeToFile(const String &destImagePath, ImageFormat destImageFormat,
244         bool overwrite, int quality) const
245 {
246         SysAssertf(__pImpl != null,
247                 "Not yet constructed. Construct() should be called before use.");
248
249         return __pImpl->EncodeToFile(destImagePath, destImageFormat, overwrite, quality);
250 }
251
252 Bitmap*
253 ImageBuffer::GetBitmapN(BitmapPixelFormat pixelFormat, BufferScaling bufferScaling) const
254 {
255         SysAssertf(__pImpl != null,
256                 "Not yet constructed. Construct() should be called before use.");
257
258         return __pImpl->GetBitmapN(pixelFormat, bufferScaling);
259 }
260
261 ByteBuffer*
262 ImageBuffer::GetByteBufferN(MediaPixelFormat pixelFormat) const
263 {
264         SysAssertf(__pImpl != null,
265                 "Not yet constructed. Construct() should be called before use.");
266
267         return __pImpl->GetByteBufferN(pixelFormat);
268 }
269
270 ImageBuffer*
271 ImageBuffer::CloneN() const
272 {
273         ImageBuffer* pImgBuffer = null;
274
275         SysAssertf(__pImpl != null,
276                 "Not yet constructed. Construct() should be called before use.");
277
278         pImgBuffer = __pImpl->CloneN();
279
280         return pImgBuffer;
281 }
282
283 ImageBuffer*
284 ImageBuffer::ConvertPixelFormatN(MediaPixelFormat pixelFormat) const
285 {
286         ImageBuffer* pImgBuffer = null;
287
288         SysAssertf(__pImpl != null,
289                 "Not yet constructed. Construct() should be called before use.");
290
291         pImgBuffer = __pImpl->ConvertPixelFormatN(pixelFormat);
292
293         return pImgBuffer;
294 }
295
296 ImageBuffer*
297 ImageBuffer::ResizeN(int width, int height) const
298 {
299         ImageBuffer* pImgBuffer = null;
300
301         SysAssertf(__pImpl != null,
302                 "Not yet constructed. Construct() should be called before use.");
303
304         pImgBuffer = __pImpl->ResizeN(width, height);
305
306         return pImgBuffer;
307 }
308
309 ImageBuffer*
310 ImageBuffer::FlipN(ImageFlipType flipType) const
311 {
312         ImageBuffer* pImgBuffer = null;
313
314         SysAssertf(__pImpl != null,
315                 "Not yet constructed. Construct() should be called before use.");
316
317         pImgBuffer = __pImpl->FlipN(flipType);
318
319         return pImgBuffer;
320 }
321
322 ImageBuffer*
323 ImageBuffer::RotateN(ImageRotationType rotateType) const
324 {
325         ImageBuffer* pImgBuffer = null;
326
327         SysAssertf(__pImpl != null,
328                 "Not yet constructed. Construct() should be called before use.");
329
330         pImgBuffer = __pImpl->RotateN(rotateType);
331
332         return pImgBuffer;
333 }
334
335 ImageBuffer*
336 ImageBuffer::CropN(int x, int y, int width, int height) const
337 {
338         ImageBuffer* pImgBuffer = null;
339
340         SysAssertf(__pImpl != null,
341                 "Not yet constructed. Construct() should be called before use.");
342
343         pImgBuffer = __pImpl->CropN(x, y, width, height);
344
345         return pImgBuffer;
346 }
347
348 result
349 ImageBuffer::GetImageInfo(const String& srcImagePath, ImageFormat& imageFormat,
350         int &width, int &height)
351 {
352         result r = E_SUCCESS;
353
354         SysTryReturn(NID_MEDIA, !srcImagePath.IsEmpty(), E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
355                 "[E_FILE_NOT_FOUND] path is empty");
356
357         SysTryReturn(NID_MEDIA, File::IsFileExist(srcImagePath), E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
358                 "[E_FILE_NOT_FOUND] file not found: %ls", srcImagePath.GetPointer());
359
360         r = _ImageBufferImpl::GetImageInfo(srcImagePath, imageFormat, width, height);
361         return r;
362 }
363
364 result
365 ImageBuffer::GetImageInfo(const ByteBuffer& srcImageBuf, ImageFormat& imageFormat,
366         int &width, int &height)
367 {
368         SysTryReturn(NID_MEDIA, &srcImageBuf != null, E_INVALID_ARG, E_INVALID_ARG,
369                 "[E_INVALID_ARG] srcImageBuf is null");
370
371         return _ImageBufferImpl::GetImageInfo(srcImageBuf, imageFormat, width, height);
372 }
373
374 IListT<MediaPixelFormat>*
375 ImageBuffer::GetSupportedPixelFormatListN()
376 {
377         IListT<MediaPixelFormat> *pList = null;
378
379         pList = _ImageBufferImpl::GetSupportedPixelFormatListN();
380
381         return pList;
382 }
383
384
385
386
387 }} // Tizen::Media