merge commits of 2.2.1 to public
[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         SysSecureTryReturnResult(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
178
179
180
181 result
182 ImageBuffer::Construct(const Tizen::Base::String &srcImagePath,
183         int destWidth, int destHeight, ImageScalingMethod scalingMethod)
184 {
185         result r = E_SUCCESS;
186
187         SysAssertf(__pImpl == null,
188                 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
189         SysTryReturnResult(NID_MEDIA, !srcImagePath.IsEmpty(), E_INVALID_ARG,"path is empty.");
190
191         SysSecureTryReturnResult(NID_MEDIA, File::IsFileExist(srcImagePath), E_FILE_NOT_FOUND,
192                 "file not found: %ls", srcImagePath.GetPointer());
193
194         _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
195         SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
196
197         r = pImageBufferImpl->Construct(srcImagePath, destWidth, destHeight, scalingMethod);
198         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , GetLastResult(),
199                         "[%s] Construct instance failed.", GetErrorMessage(GetLastResult()));
200
201         __pImpl = pImageBufferImpl;
202         return r;
203
204 CATCH:
205         if (pImageBufferImpl != null)
206         {
207                 delete pImageBufferImpl;
208                 pImageBufferImpl = null;
209         }
210         return r;
211 }
212
213 result
214 ImageBuffer::Construct(const Tizen::Base::ByteBuffer &srcImageBuf,
215         int destWidth, int destHeight, ImageScalingMethod scalingMethod)
216 {
217         result r = E_SUCCESS;
218
219         SysAssertf(__pImpl == null,
220                 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
221         SysTryReturnResult(NID_MEDIA, &srcImageBuf != null, E_INVALID_ARG,
222                 "src buffer is null");
223
224         _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
225         SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
226
227         r = pImageBufferImpl->Construct(srcImageBuf, destWidth, destHeight, scalingMethod);
228         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
229                 "[%s] Construct instance failed.", GetErrorMessage(r));
230
231         __pImpl = pImageBufferImpl;
232         return r;
233
234 CATCH:
235         if (pImageBufferImpl != null)
236         {
237                 delete pImageBufferImpl;
238                 pImageBufferImpl = null;
239         }
240         return r;
241 }
242
243
244 ExifOrientation
245 ImageBuffer::GetExifOrientation(void) const
246 {
247         SysAssertf(__pImpl != null,
248                 "Not yet constructed. Construct() should be called before use.");
249
250         return __pImpl->GetExifOrientation();
251 }
252
253 bool
254 ImageBuffer::Equals(const Object& rhs) const
255 {
256         SysAssertf(__pImpl != null,
257                 "Not yet constructed. Construct() should be called before use.");
258
259         ImageBuffer* pOther = (const_cast<ImageBuffer*>(dynamic_cast <const ImageBuffer*>(&rhs)));
260         SysTryReturn(NID_MEDIA, pOther != null, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND,
261                 "[E_OBJ_NOT_FOUND] RHS ImageBuffer is not found.");
262
263         return __pImpl->Equals(pOther);
264 }
265
266 int
267 ImageBuffer::GetHashCode() const
268 {
269         int hashCode = 0;
270
271         SysAssertf(__pImpl != null,
272                 "Not yet constructed. Construct() should be called before use.");
273
274         hashCode = __pImpl->GetHashCode();
275         return hashCode;
276 }
277
278 int
279 ImageBuffer::GetHeight() const
280 {
281         SysAssertf(__pImpl != null,
282                 "Not yet constructed. Construct() should be called before use.");
283
284         return __pImpl->GetHeight();
285 }
286
287 int
288 ImageBuffer::GetWidth() const
289 {
290         SysAssertf(__pImpl != null,
291                 "Not yet constructed. Construct() should be called before use.");
292
293         return __pImpl->GetWidth();
294 }
295
296 MediaPixelFormat
297 ImageBuffer::GetPixelFormat() const
298 {
299         SysAssertf(__pImpl != null,
300                 "Not yet constructed. Construct() should be called before use.");
301
302         return __pImpl->GetPixelFormat();
303 }
304
305 ByteBuffer*
306 ImageBuffer::EncodeToBufferN(ImageFormat destImageFormat, int quality) const
307 {
308         SysAssertf(__pImpl != null,
309                 "Not yet constructed. Construct() should be called before use.");
310         SysTryReturn(NID_MEDIA, ((destImageFormat == IMG_FORMAT_BMP) ||
311                 (destImageFormat == IMG_FORMAT_JPG) || (destImageFormat == IMG_FORMAT_PNG)),
312                 null, E_UNSUPPORTED_FORMAT, "[E_UNSUPPORTED_FORMAT] %s = %d",
313                 "destImageFormat", destImageFormat);
314         SysTryReturn(NID_MEDIA, (quality > 0), null, E_OUT_OF_RANGE,
315                 "[E_OUT_OF_RANGE] Quality (%d) should be greater than zero", quality);
316         SysTryReturn(NID_MEDIA, (quality <= 100), null, E_OUT_OF_RANGE,
317                 "[E_OUT_OF_RANGE] Quality (%d) should be lesser than 100", quality);
318
319         return __pImpl->EncodeToBufferN(destImageFormat, quality);
320 }
321
322 result
323 ImageBuffer::EncodeToFile(const String &destImagePath, ImageFormat destImageFormat,
324         bool overwrite, int quality) const
325 {
326         SysAssertf(__pImpl != null,
327                 "Not yet constructed. Construct() should be called before use.");
328         SysTryReturn(NID_MEDIA, ((destImageFormat == IMG_FORMAT_BMP) ||
329                 (destImageFormat == IMG_FORMAT_JPG) || (destImageFormat == IMG_FORMAT_PNG)),
330                 E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT, "[E_UNSUPPORTED_FORMAT] %s = %d",
331                 "destImageFormat", destImageFormat);
332         SysTryReturn(NID_MEDIA, (quality > 0), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
333                 "[E_OUT_OF_RANGE] Quality (%d) should be greater than zero", quality);
334         SysTryReturn(NID_MEDIA, (quality <= 100), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
335                 "[E_OUT_OF_RANGE] Quality (%d) should be lesser than 100", quality);
336
337         return __pImpl->EncodeToFile(destImagePath, destImageFormat, overwrite, quality);
338 }
339
340 Bitmap*
341 ImageBuffer::GetBitmapN(BitmapPixelFormat pixelFormat, BufferScaling bufferScaling) const
342 {
343         SysAssertf(__pImpl != null,
344                 "Not yet constructed. Construct() should be called before use.");
345
346         return __pImpl->GetBitmapN(pixelFormat, bufferScaling);
347 }
348
349 Bitmap*
350 ImageBuffer::GetBitmapN(BitmapPixelFormat pixelFormat, float destWidth, float destHeight) const
351 {
352         SysAssertf(__pImpl != null,
353                 "Not yet constructed. Construct() should be called before use.");
354
355         return __pImpl->GetBitmapN(pixelFormat, FloatDimension(destWidth, destHeight));
356 }
357
358 ByteBuffer*
359 ImageBuffer::GetByteBufferN(MediaPixelFormat pixelFormat) const
360 {
361         SysAssertf(__pImpl != null,
362                 "Not yet constructed. Construct() should be called before use.");
363
364         return __pImpl->GetByteBufferN(pixelFormat);
365 }
366
367 ImageBuffer*
368 ImageBuffer::CloneN() const
369 {
370         ImageBuffer* pImgBuffer = null;
371
372         SysAssertf(__pImpl != null,
373                 "Not yet constructed. Construct() should be called before use.");
374
375         pImgBuffer = __pImpl->CloneN();
376
377         return pImgBuffer;
378 }
379
380 ImageBuffer*
381 ImageBuffer::ConvertPixelFormatN(MediaPixelFormat pixelFormat) const
382 {
383         ImageBuffer* pImgBuffer = null;
384
385         SysAssertf(__pImpl != null,
386                 "Not yet constructed. Construct() should be called before use.");
387
388         pImgBuffer = __pImpl->ConvertPixelFormatN(pixelFormat);
389
390         return pImgBuffer;
391 }
392
393 ImageBuffer*
394 ImageBuffer::ResizeN(int width, int height) const
395 {
396         ImageBuffer* pImgBuffer = null;
397
398         SysAssertf(__pImpl != null,
399                 "Not yet constructed. Construct() should be called before use.");
400
401         pImgBuffer = __pImpl->ResizeN(width, height);
402
403         return pImgBuffer;
404 }
405
406 ImageBuffer*
407 ImageBuffer::FlipN(ImageFlipType flipType) const
408 {
409         ImageBuffer* pImgBuffer = null;
410
411         SysAssertf(__pImpl != null,
412                 "Not yet constructed. Construct() should be called before use.");
413
414         pImgBuffer = __pImpl->FlipN(flipType);
415
416         return pImgBuffer;
417 }
418
419 ImageBuffer*
420 ImageBuffer::RotateN(ImageRotationType rotateType) const
421 {
422         ImageBuffer* pImgBuffer = null;
423
424         SysAssertf(__pImpl != null,
425                 "Not yet constructed. Construct() should be called before use.");
426
427         pImgBuffer = __pImpl->RotateN(rotateType);
428
429         return pImgBuffer;
430 }
431
432 ImageBuffer*
433 ImageBuffer::CropN(int x, int y, int width, int height) const
434 {
435         ImageBuffer* pImgBuffer = null;
436
437         SysAssertf(__pImpl != null,
438                 "Not yet constructed. Construct() should be called before use.");
439
440         pImgBuffer = __pImpl->CropN(x, y, width, height);
441
442         return pImgBuffer;
443 }
444
445 result
446 ImageBuffer::GetImageInfo(const String& srcImagePath, ImageFormat& imageFormat,
447         int &width, int &height)
448 {
449         result r = E_SUCCESS;
450
451         SysTryReturn(NID_MEDIA, !srcImagePath.IsEmpty(), E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
452                 "[E_FILE_NOT_FOUND] path is empty");
453
454         SysSecureTryReturn(NID_MEDIA, File::IsFileExist(srcImagePath), E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
455                 "[E_FILE_NOT_FOUND] file not found: %ls", srcImagePath.GetPointer());
456
457         r = _ImageBufferImpl::GetImageInfo(srcImagePath, imageFormat, width, height);
458         return r;
459 }
460
461 result
462 ImageBuffer::GetImageInfo(const ByteBuffer& srcImageBuf, ImageFormat& imageFormat,
463         int &width, int &height)
464 {
465         SysTryReturn(NID_MEDIA, &srcImageBuf != null, E_INVALID_ARG, E_INVALID_ARG,
466                 "[E_INVALID_ARG] srcImageBuf is null");
467
468         return _ImageBufferImpl::GetImageInfo(srcImageBuf, imageFormat, width, height);
469 }
470
471 IListT<MediaPixelFormat>*
472 ImageBuffer::GetSupportedPixelFormatListN()
473 {
474         IListT<MediaPixelFormat> *pList = null;
475
476         pList = _ImageBufferImpl::GetSupportedPixelFormatListN();
477
478         return pList;
479 }
480
481
482
483
484 }} // Tizen::Media