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 SysTryReturnResult(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;
178 ImageBuffer::Equals(const Object& rhs) const
180 SysAssertf(__pImpl != null,
181 "Not yet constructed. Construct() should be called before use.");
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.");
187 return __pImpl->Equals(pOther);
191 ImageBuffer::GetHashCode() const
195 SysAssertf(__pImpl != null,
196 "Not yet constructed. Construct() should be called before use.");
198 hashCode = __pImpl->GetHashCode();
203 ImageBuffer::GetHeight() const
205 SysAssertf(__pImpl != null,
206 "Not yet constructed. Construct() should be called before use.");
208 return __pImpl->GetHeight();
212 ImageBuffer::GetWidth() const
214 SysAssertf(__pImpl != null,
215 "Not yet constructed. Construct() should be called before use.");
217 return __pImpl->GetWidth();
221 ImageBuffer::GetPixelFormat() const
223 SysAssertf(__pImpl != null,
224 "Not yet constructed. Construct() should be called before use.");
226 return __pImpl->GetPixelFormat();
230 ImageBuffer::EncodeToBufferN(ImageFormat destImageFormat, int quality) const
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);
239 return __pImpl->EncodeToBufferN(destImageFormat, quality);
243 ImageBuffer::EncodeToFile(const String &destImagePath, ImageFormat destImageFormat,
244 bool overwrite, int quality) const
246 SysAssertf(__pImpl != null,
247 "Not yet constructed. Construct() should be called before use.");
249 return __pImpl->EncodeToFile(destImagePath, destImageFormat, overwrite, quality);
253 ImageBuffer::GetBitmapN(BitmapPixelFormat pixelFormat, BufferScaling bufferScaling) const
255 SysAssertf(__pImpl != null,
256 "Not yet constructed. Construct() should be called before use.");
258 return __pImpl->GetBitmapN(pixelFormat, bufferScaling);
262 ImageBuffer::GetByteBufferN(MediaPixelFormat pixelFormat) const
264 SysAssertf(__pImpl != null,
265 "Not yet constructed. Construct() should be called before use.");
267 return __pImpl->GetByteBufferN(pixelFormat);
271 ImageBuffer::CloneN() const
273 ImageBuffer* pImgBuffer = null;
275 SysAssertf(__pImpl != null,
276 "Not yet constructed. Construct() should be called before use.");
278 pImgBuffer = __pImpl->CloneN();
284 ImageBuffer::ConvertPixelFormatN(MediaPixelFormat pixelFormat) const
286 ImageBuffer* pImgBuffer = null;
288 SysAssertf(__pImpl != null,
289 "Not yet constructed. Construct() should be called before use.");
291 pImgBuffer = __pImpl->ConvertPixelFormatN(pixelFormat);
297 ImageBuffer::ResizeN(int width, int height) const
299 ImageBuffer* pImgBuffer = null;
301 SysAssertf(__pImpl != null,
302 "Not yet constructed. Construct() should be called before use.");
304 pImgBuffer = __pImpl->ResizeN(width, height);
310 ImageBuffer::FlipN(ImageFlipType flipType) const
312 ImageBuffer* pImgBuffer = null;
314 SysAssertf(__pImpl != null,
315 "Not yet constructed. Construct() should be called before use.");
317 pImgBuffer = __pImpl->FlipN(flipType);
323 ImageBuffer::RotateN(ImageRotationType rotateType) const
325 ImageBuffer* pImgBuffer = null;
327 SysAssertf(__pImpl != null,
328 "Not yet constructed. Construct() should be called before use.");
330 pImgBuffer = __pImpl->RotateN(rotateType);
336 ImageBuffer::CropN(int x, int y, int width, int height) const
338 ImageBuffer* pImgBuffer = null;
340 SysAssertf(__pImpl != null,
341 "Not yet constructed. Construct() should be called before use.");
343 pImgBuffer = __pImpl->CropN(x, y, width, height);
349 ImageBuffer::GetImageInfo(const String& srcImagePath, ImageFormat& imageFormat,
350 int &width, int &height)
352 result r = E_SUCCESS;
354 SysTryReturn(NID_MEDIA, !srcImagePath.IsEmpty(), E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
355 "[E_FILE_NOT_FOUND] path is empty");
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());
360 r = _ImageBufferImpl::GetImageInfo(srcImagePath, imageFormat, width, height);
365 ImageBuffer::GetImageInfo(const ByteBuffer& srcImageBuf, ImageFormat& imageFormat,
366 int &width, int &height)
368 SysTryReturn(NID_MEDIA, &srcImageBuf != null, E_INVALID_ARG, E_INVALID_ARG,
369 "[E_INVALID_ARG] srcImageBuf is null");
371 return _ImageBufferImpl::GetImageInfo(srcImageBuf, imageFormat, width, height);
374 IListT<MediaPixelFormat>*
375 ImageBuffer::GetSupportedPixelFormatListN()
377 IListT<MediaPixelFormat> *pList = null;
379 pList = _ImageBufferImpl::GetSupportedPixelFormatListN();