2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FMediaImageBuffer.cpp
20 * @brief This file contains the implementation of subsystem's ImageBuffer.
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"
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;
38 namespace Tizen { namespace Media
41 ImageBuffer::ImageBuffer(void)
46 ImageBuffer::~ImageBuffer(void)
56 ImageBuffer::Construct(int width, int height, MediaPixelFormat pixelFormat,
57 const byte* pData, int length)
61 SysAssertf(__pImpl == null,
62 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
64 _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
65 SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
67 r = pImageBufferImpl->Construct(width, height, pixelFormat, pData, length);
68 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , GetLastResult(),
69 "[%s] Construct instance failed.", GetErrorMessage(GetLastResult()));
71 __pImpl = pImageBufferImpl;
75 if (pImageBufferImpl != null)
77 delete pImageBufferImpl;
78 pImageBufferImpl = null;
85 ImageBuffer::Construct(const Tizen::Base::String &srcImagePath,
86 const Tizen::Graphics::Rectangle *pDecodingRegion, bool autoRotate)
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.");
94 SysSecureTryReturnResult(NID_MEDIA, File::IsFileExist(srcImagePath), E_FILE_NOT_FOUND,
95 "file not found: %ls", srcImagePath.GetPointer());
97 _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
98 SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
100 r = pImageBufferImpl->Construct(srcImagePath, pDecodingRegion, autoRotate);
101 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , GetLastResult(),
102 "[%s] Construct instance failed.", GetErrorMessage(GetLastResult()));
104 __pImpl = pImageBufferImpl;
108 if (pImageBufferImpl != null)
110 delete pImageBufferImpl;
111 pImageBufferImpl = null;
117 ImageBuffer::Construct(const Tizen::Base::ByteBuffer &srcImageBuf,
118 const Tizen::Graphics::Rectangle *pDecodingRegion, bool autoRotate)
120 result r = E_SUCCESS;
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");
127 _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
128 SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
130 r = pImageBufferImpl->Construct(srcImageBuf, pDecodingRegion, autoRotate);
131 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
132 "[%s] Construct instance failed.", GetErrorMessage(r));
134 __pImpl = pImageBufferImpl;
138 if (pImageBufferImpl != null)
140 delete pImageBufferImpl;
141 pImageBufferImpl = null;
147 ImageBuffer::Construct(const Tizen::Graphics::Bitmap &srcBitmap)
149 result r = E_SUCCESS;
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());
158 _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
159 SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
161 r = pImageBufferImpl->Construct(srcBitmap);
162 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
163 "[%s] Construct instance failed.", GetErrorMessage(r));
165 __pImpl = pImageBufferImpl;
169 if (pImageBufferImpl != null)
171 delete pImageBufferImpl;
172 pImageBufferImpl = null;
182 ImageBuffer::Construct(const Tizen::Base::String &srcImagePath,
183 int destWidth, int destHeight, ImageScalingMethod scalingMethod)
185 result r = E_SUCCESS;
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.");
191 SysSecureTryReturnResult(NID_MEDIA, File::IsFileExist(srcImagePath), E_FILE_NOT_FOUND,
192 "file not found: %ls", srcImagePath.GetPointer());
194 _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
195 SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
197 r = pImageBufferImpl->Construct(srcImagePath, destWidth, destHeight, scalingMethod);
198 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , GetLastResult(),
199 "[%s] Construct instance failed.", GetErrorMessage(GetLastResult()));
201 __pImpl = pImageBufferImpl;
205 if (pImageBufferImpl != null)
207 delete pImageBufferImpl;
208 pImageBufferImpl = null;
214 ImageBuffer::Construct(const Tizen::Base::ByteBuffer &srcImageBuf,
215 int destWidth, int destHeight, ImageScalingMethod scalingMethod)
217 result r = E_SUCCESS;
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");
224 _ImageBufferImpl* pImageBufferImpl = new (std::nothrow) _ImageBufferImpl();
225 SysTryReturnResult(NID_MEDIA, pImageBufferImpl != null, E_OUT_OF_MEMORY, "Create instance failed.");
227 r = pImageBufferImpl->Construct(srcImageBuf, destWidth, destHeight, scalingMethod);
228 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
229 "[%s] Construct instance failed.", GetErrorMessage(r));
231 __pImpl = pImageBufferImpl;
235 if (pImageBufferImpl != null)
237 delete pImageBufferImpl;
238 pImageBufferImpl = null;
245 ImageBuffer::GetExifOrientation(void) const
247 SysAssertf(__pImpl != null,
248 "Not yet constructed. Construct() should be called before use.");
250 return __pImpl->GetExifOrientation();
254 ImageBuffer::Equals(const Object& rhs) const
256 SysAssertf(__pImpl != null,
257 "Not yet constructed. Construct() should be called before use.");
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.");
263 return __pImpl->Equals(pOther);
267 ImageBuffer::GetHashCode() const
271 SysAssertf(__pImpl != null,
272 "Not yet constructed. Construct() should be called before use.");
274 hashCode = __pImpl->GetHashCode();
279 ImageBuffer::GetHeight() const
281 SysAssertf(__pImpl != null,
282 "Not yet constructed. Construct() should be called before use.");
284 return __pImpl->GetHeight();
288 ImageBuffer::GetWidth() const
290 SysAssertf(__pImpl != null,
291 "Not yet constructed. Construct() should be called before use.");
293 return __pImpl->GetWidth();
297 ImageBuffer::GetPixelFormat() const
299 SysAssertf(__pImpl != null,
300 "Not yet constructed. Construct() should be called before use.");
302 return __pImpl->GetPixelFormat();
306 ImageBuffer::EncodeToBufferN(ImageFormat destImageFormat, int quality) const
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);
319 return __pImpl->EncodeToBufferN(destImageFormat, quality);
323 ImageBuffer::EncodeToFile(const String &destImagePath, ImageFormat destImageFormat,
324 bool overwrite, int quality) const
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);
337 return __pImpl->EncodeToFile(destImagePath, destImageFormat, overwrite, quality);
341 ImageBuffer::GetBitmapN(BitmapPixelFormat pixelFormat, BufferScaling bufferScaling) const
343 SysAssertf(__pImpl != null,
344 "Not yet constructed. Construct() should be called before use.");
346 return __pImpl->GetBitmapN(pixelFormat, bufferScaling);
350 ImageBuffer::GetBitmapN(BitmapPixelFormat pixelFormat, float destWidth, float destHeight) const
352 SysAssertf(__pImpl != null,
353 "Not yet constructed. Construct() should be called before use.");
355 return __pImpl->GetBitmapN(pixelFormat, FloatDimension(destWidth, destHeight));
359 ImageBuffer::GetByteBufferN(MediaPixelFormat pixelFormat) const
361 SysAssertf(__pImpl != null,
362 "Not yet constructed. Construct() should be called before use.");
364 return __pImpl->GetByteBufferN(pixelFormat);
368 ImageBuffer::CloneN() const
370 ImageBuffer* pImgBuffer = null;
372 SysAssertf(__pImpl != null,
373 "Not yet constructed. Construct() should be called before use.");
375 pImgBuffer = __pImpl->CloneN();
381 ImageBuffer::ConvertPixelFormatN(MediaPixelFormat pixelFormat) const
383 ImageBuffer* pImgBuffer = null;
385 SysAssertf(__pImpl != null,
386 "Not yet constructed. Construct() should be called before use.");
388 pImgBuffer = __pImpl->ConvertPixelFormatN(pixelFormat);
394 ImageBuffer::ResizeN(int width, int height) const
396 ImageBuffer* pImgBuffer = null;
398 SysAssertf(__pImpl != null,
399 "Not yet constructed. Construct() should be called before use.");
401 pImgBuffer = __pImpl->ResizeN(width, height);
407 ImageBuffer::FlipN(ImageFlipType flipType) const
409 ImageBuffer* pImgBuffer = null;
411 SysAssertf(__pImpl != null,
412 "Not yet constructed. Construct() should be called before use.");
414 pImgBuffer = __pImpl->FlipN(flipType);
420 ImageBuffer::RotateN(ImageRotationType rotateType) const
422 ImageBuffer* pImgBuffer = null;
424 SysAssertf(__pImpl != null,
425 "Not yet constructed. Construct() should be called before use.");
427 pImgBuffer = __pImpl->RotateN(rotateType);
433 ImageBuffer::CropN(int x, int y, int width, int height) const
435 ImageBuffer* pImgBuffer = null;
437 SysAssertf(__pImpl != null,
438 "Not yet constructed. Construct() should be called before use.");
440 pImgBuffer = __pImpl->CropN(x, y, width, height);
446 ImageBuffer::GetImageInfo(const String& srcImagePath, ImageFormat& imageFormat,
447 int &width, int &height)
449 result r = E_SUCCESS;
451 SysTryReturn(NID_MEDIA, !srcImagePath.IsEmpty(), E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
452 "[E_FILE_NOT_FOUND] path is empty");
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());
457 r = _ImageBufferImpl::GetImageInfo(srcImagePath, imageFormat, width, height);
462 ImageBuffer::GetImageInfo(const ByteBuffer& srcImageBuf, ImageFormat& imageFormat,
463 int &width, int &height)
465 SysTryReturn(NID_MEDIA, &srcImageBuf != null, E_INVALID_ARG, E_INVALID_ARG,
466 "[E_INVALID_ARG] srcImageBuf is null");
468 return _ImageBufferImpl::GetImageInfo(srcImageBuf, imageFormat, width, height);
471 IListT<MediaPixelFormat>*
472 ImageBuffer::GetSupportedPixelFormatListN()
474 IListT<MediaPixelFormat> *pList = null;
476 pList = _ImageBufferImpl::GetSupportedPixelFormatListN();