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 FMediaImage.cpp
20 * @brief This file contains the implementation of subsystem's Image.
24 #include <FMediaImage.h>
25 #include <FMediaImageUtil.h>
26 #include <FBaseInternalTypes.h>
27 #include <FBaseSysLog.h>
28 #include <FApp_AppInfo.h>
29 #include "FMedia_ImageImpl.h"
30 #include "FMedia_ImageUtilImpl.h"
32 using namespace Tizen::Graphics;
33 using namespace Tizen::Base;
34 using namespace Tizen::Io;
35 using namespace Tizen::App;
37 namespace Tizen { namespace Media
40 static const int MIN_LIMIT_SIZE = 1024;
49 if (__pImageImpl != null)
57 Image::Construct(void)
61 SysAssertf(__pImageImpl == null,
62 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
63 _ImageImpl* pImageImpl = new (std::nothrow) _ImageImpl();
64 SysTryCatch(NID_MEDIA, pImageImpl != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
65 "[E_OUT_OF_MEMORY] Construct Failed ");
67 r = pImageImpl->Construct();
68 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Create instance failed.", GetErrorMessage(r));
70 __pImageImpl = pImageImpl;
83 Image::DecodeN(const String& srcImagePath, BitmapPixelFormat pixelFormat) const
87 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
88 SysTryCatch(NID_MEDIA, !srcImagePath.IsEmpty(), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] srcImagePath is empty");
89 SysTryCatch(NID_MEDIA, File::IsFileExist(srcImagePath), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
90 "[E_FILE_NOT_FOUND] srcImagePath:%ls", srcImagePath.GetPointer());
91 SysTryCatch(NID_MEDIA, ((pixelFormat==BITMAP_PIXEL_FORMAT_RGB565) || (pixelFormat==BITMAP_PIXEL_FORMAT_ARGB8888) || (pixelFormat==BITMAP_PIXEL_FORMAT_R8G8B8A8)),
92 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] pixelFormat:%d", pixelFormat);
93 return _ImageImpl::DecodeToBitmapN(srcImagePath, pixelFormat);
101 Image::DecodeN(const String& srcImagePath, BitmapPixelFormat pixelFormat, int destWidth, int destHeight) const
103 result r = E_SUCCESS;
105 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
106 SysTryCatch(NID_MEDIA, !srcImagePath.IsEmpty(), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] srcImagePath is empty");
107 SysTryCatch(NID_MEDIA, File::IsFileExist(srcImagePath), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
108 "[E_FILE_NOT_FOUND] srcImagePath:%ls", srcImagePath.GetPointer());
109 SysTryCatch(NID_MEDIA, ((pixelFormat==BITMAP_PIXEL_FORMAT_RGB565) || (pixelFormat==BITMAP_PIXEL_FORMAT_ARGB8888) || (pixelFormat==BITMAP_PIXEL_FORMAT_R8G8B8A8)),
110 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] pixelFormat:%d", pixelFormat);
111 SysTryCatch(NID_MEDIA, destWidth > 0, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] destWidth:%d", destWidth);
112 SysTryCatch(NID_MEDIA, destHeight > 0, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] destHeight:%d", destHeight);
114 return _ImageImpl::DecodeToBitmapN(srcImagePath, pixelFormat, Dimension(destWidth, destHeight));
121 Image::DecodeN(const ByteBuffer& srcImageBuf, ImageFormat srcImageFormat,
122 BitmapPixelFormat pixelFormat) const
124 result r = E_SUCCESS;
126 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
127 SysTryCatch(NID_MEDIA, &srcImageBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] srcImageBuf is null");
128 SysTryCatch(NID_MEDIA, ((srcImageFormat>IMG_FORMAT_NONE) && (srcImageFormat<=IMG_FORMAT_WBMP)), r = E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT,
129 "[E_UNSUPPORTED_FORMAT] srcImageFormat:%d", srcImageFormat);
130 SysTryCatch(NID_MEDIA, ((pixelFormat==BITMAP_PIXEL_FORMAT_RGB565) || (pixelFormat==BITMAP_PIXEL_FORMAT_ARGB8888) || (pixelFormat==BITMAP_PIXEL_FORMAT_R8G8B8A8)),
131 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] %s=%d", "pixelFormat", pixelFormat);
133 return _ImageImpl::DecodeToBitmapN(srcImageBuf, pixelFormat);
141 Image::DecodeN(const ByteBuffer& srcImageBuf, ImageFormat imgFormat, BitmapPixelFormat pixelFormat,
142 int destWidth, int destHeight) const
144 result r = E_SUCCESS;
146 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
147 SysTryCatch(NID_MEDIA, &srcImageBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] srcImageBuf is null");
148 SysTryCatch(NID_MEDIA, ((imgFormat>IMG_FORMAT_NONE) && (imgFormat<=IMG_FORMAT_WBMP)), r = E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT,
149 "[E_UNSUPPORTED_FORMAT] imgFormat:%d", imgFormat);
150 SysTryCatch(NID_MEDIA, ((pixelFormat==BITMAP_PIXEL_FORMAT_RGB565) || (pixelFormat==BITMAP_PIXEL_FORMAT_ARGB8888) || (pixelFormat==BITMAP_PIXEL_FORMAT_R8G8B8A8)),
151 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] pixelFormat:%d", pixelFormat);
152 SysTryCatch(NID_MEDIA, destWidth > 0, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] destWidth:%d", destWidth);
153 SysTryCatch(NID_MEDIA, destHeight > 0, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] destHeight:%d", destHeight);
155 return _ImageImpl::DecodeToBitmapN(srcImageBuf, pixelFormat, Dimension(destWidth, destHeight),
156 BUFFER_SCALING_AUTO, imgFormat);
164 Image::EncodeToBufferN(const Bitmap& srcImageBuf, ImageFormat destImageFormat) const
166 result r = E_SUCCESS;
168 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
169 SysTryCatch(NID_MEDIA, &srcImageBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] srcImageBuf is null");
170 SysTryCatch(NID_MEDIA, ((destImageFormat==IMG_FORMAT_BMP) || (destImageFormat==IMG_FORMAT_JPG) || (destImageFormat==IMG_FORMAT_PNG)),
171 r = E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT, "[E_UNSUPPORTED_FORMAT] destImageFormat:%d", destImageFormat);
172 SysTryCatch(NID_MEDIA, srcImageBuf.GetWidth() > 0 && srcImageBuf.GetHeight() > 0, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND,
173 "[E_OBJ_NOT_FOUND] bmp.width=%d,height=%d", srcImageBuf.GetWidth(), srcImageBuf.GetHeight());
176 return _ImageImpl::EncodeToBufferN(srcImageBuf, destImageFormat);
183 Image::EncodeToFile(const Bitmap& srcImageBuf, ImageFormat destImageFormat,
184 const String& destImagePath, bool overwrite) const
186 result r = E_SUCCESS;
188 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
189 SysTryCatch(NID_MEDIA, &srcImageBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] srcImageBuf is null");
190 SysTryCatch(NID_MEDIA, ((destImageFormat==IMG_FORMAT_BMP) || (destImageFormat==IMG_FORMAT_JPG) || (destImageFormat==IMG_FORMAT_PNG)),
191 r = E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT, "[E_UNSUPPORTED_FORMAT] destImageFormat:%d", destImageFormat);
192 SysTryCatch(NID_MEDIA, srcImageBuf.GetWidth() > 0 && srcImageBuf.GetHeight() > 0, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND,
193 "[E_OBJ_NOT_FOUND] bmp.width=%d,height=%d", srcImageBuf.GetWidth(), srcImageBuf.GetHeight());
195 return _ImageImpl::EncodeToFile(srcImageBuf, destImageFormat, destImagePath, overwrite);
202 Image::ConvertN(const String& srcImagePath, ImageFormat destImageFormat) const
204 result r = E_SUCCESS;
206 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
207 SysTryCatch(NID_MEDIA, !srcImagePath.IsEmpty(), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] path is empty");
208 SysTryCatch(NID_MEDIA, File::IsFileExist(srcImagePath), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
209 "[E_FILE_NOT_FOUND] srcImagePath:%ls", srcImagePath.GetPointer());
210 SysTryCatch(NID_MEDIA, ((destImageFormat>IMG_FORMAT_NONE) && (destImageFormat<=IMG_FORMAT_WBMP)), r = E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT,
211 "[E_UNSUPPORTED_FORMAT] destImageFormat:%d", destImageFormat);
213 return _ImageImpl::ConvertToBufferN(srcImagePath, destImageFormat);
221 Image::CompressJpeg(const Tizen::Base::String& srcImagePath, const Tizen::Base::String& destImagePath, int limitSize) const
223 result r = E_SUCCESS;
224 ImageFormat imgFormat;
226 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
227 SysTryCatch(NID_MEDIA, !srcImagePath.IsEmpty(), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] srcImagePath is empty");
228 SysTryCatch(NID_MEDIA, File::IsFileExist(srcImagePath), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
229 "[E_FILE_NOT_FOUND] srcImagePath:%ls", srcImagePath.GetPointer());
231 // CompressJpeg should accept only jpeg encoded images.
232 imgFormat = _ImageUtilImpl::GetImageFormat(srcImagePath);
233 SysTryCatch(NID_MEDIA, imgFormat == IMG_FORMAT_JPG, r = E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT,
234 "[E_UNSUPPORTED_FORMAT] image format(%d) != JPG", imgFormat);
236 SysTryCatch(NID_MEDIA, limitSize > MIN_LIMIT_SIZE, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE,
237 "[E_OUT_OF_RANGE] limitSize:%d < %d", limitSize, MIN_LIMIT_SIZE);
239 return _ImageImpl::CompressJpeg(srcImagePath, destImagePath, limitSize);
245 Tizen::Base::ByteBuffer*
246 Image::CompressJpegN(const Tizen::Base::ByteBuffer& srcImageBuf, int limitSize) const
248 result r = E_SUCCESS;
249 ImageFormat imgFormat;
251 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
252 SysTryCatch(NID_MEDIA, &srcImageBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] srcImageBuf is null");
254 SysTryCatch(NID_MEDIA, limitSize > MIN_LIMIT_SIZE, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE,
255 "[E_OUT_OF_RANGE] limitSize:%d < %d", limitSize, MIN_LIMIT_SIZE);
256 SysTryCatch(NID_MEDIA, srcImageBuf.GetCapacity() > limitSize, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE,
257 "[E_OUT_OF_RANGE] srcSize(%d) <= limitSize(%d)", srcImageBuf.GetCapacity(), limitSize);
259 imgFormat = _ImageUtilImpl::GetImageFormat(srcImageBuf);
260 SysTryCatch(NID_MEDIA, imgFormat == IMG_FORMAT_JPG, r = E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT,
261 "[E_UNSUPPORTED_FORMAT] image format(%d) != JPG", imgFormat);
263 return _ImageImpl::CompressJpegN(srcImageBuf, limitSize);
270 Tizen::Base::ByteBuffer*
271 Image::DecodeToBufferN(const Tizen::Base::ByteBuffer& srcImageBuf, ImageFormat srcImageFormat,
272 Tizen::Graphics::BitmapPixelFormat pixelFormat,
273 int& imageWidth, int& imageHeight) const
276 ByteBuffer* pBuf = null;
277 result r = E_SUCCESS;
279 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
280 SysTryCatch(NID_MEDIA, &srcImageBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] srcImageBuf is null");
281 SysTryCatch(NID_MEDIA, ((srcImageFormat>IMG_FORMAT_NONE) && (srcImageFormat<=IMG_FORMAT_WBMP)), r = E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT,
282 "[E_UNSUPPORTED_FORMAT] srcImageFormat:%d", srcImageFormat);
283 SysTryCatch(NID_MEDIA, ((pixelFormat==BITMAP_PIXEL_FORMAT_RGB565) || (pixelFormat==BITMAP_PIXEL_FORMAT_ARGB8888) || (pixelFormat==BITMAP_PIXEL_FORMAT_R8G8B8A8)),
284 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] pixelFormat:%d", pixelFormat);
286 pBuf = _ImageImpl::DecodeToBufferN(srcImageBuf, dim, Dimension(0,0), pixelFormat, srcImageFormat);
287 SysTryCatch(NID_MEDIA, pBuf != null, r = GetLastResult(), GetLastResult(),
288 "[%s] Propagated.", GetErrorMessage(GetLastResult()));
289 imageWidth = dim.width;
290 imageHeight = dim.height;
299 Tizen::Base::ByteBuffer*
300 Image::DecodeToBufferN(const Tizen::Base::String& srcImagePath,
301 Tizen::Graphics::BitmapPixelFormat pixelFormat,
302 int& imageWidth, int& imageHeight) const
305 ByteBuffer* pBuf = null;
306 result r = E_SUCCESS;
308 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
309 SysTryCatch(NID_MEDIA, !srcImagePath.IsEmpty(), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] srcImagePath is empty");
310 SysTryCatch(NID_MEDIA, File::IsFileExist(srcImagePath), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
311 "[E_FILE_NOT_FOUND] srcImagePath:%ls", srcImagePath.GetPointer());
312 SysTryCatch(NID_MEDIA, ((pixelFormat==BITMAP_PIXEL_FORMAT_RGB565) || (pixelFormat==BITMAP_PIXEL_FORMAT_ARGB8888) || (pixelFormat==BITMAP_PIXEL_FORMAT_R8G8B8A8)),
313 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] pixelFormat:%d", pixelFormat);
315 pBuf = _ImageImpl::DecodeToBufferN(srcImagePath, dim, Dimension(0,0), pixelFormat);
316 SysTryCatch(NID_MEDIA, pBuf != null, r = GetLastResult(), GetLastResult(),
317 "[%s] Propagated.", GetErrorMessage(GetLastResult()));
318 imageWidth = dim.width;
319 imageHeight = dim.height;
329 Image::GetImageFormat(const Tizen::Base::String& srcImagePath) const
332 result r = E_SUCCESS;
334 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
335 SysTryCatch(NID_MEDIA, !srcImagePath.IsEmpty(), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] srcImagePath is empty");
336 SysTryCatch(NID_MEDIA, File::IsFileExist(srcImagePath), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
337 "[E_FILE_NOT_FOUND] srcImagePath:%ls", srcImagePath.GetPointer());
338 fmt = _ImageUtilImpl::GetImageFormat(srcImagePath);
341 if (r == E_INVALID_DATA || r == E_INVALID_ARG)
343 SetLastResult(E_UNSUPPORTED_FORMAT);
349 return IMG_FORMAT_NONE;
354 Image::GetImageFormat(const Tizen::Base::ByteBuffer& srcImageBuf) const
357 result r = E_SUCCESS;
359 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
360 SysTryCatch(NID_MEDIA, &srcImageBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] srcImagePath is null");
362 fmt = _ImageUtilImpl::GetImageFormat(srcImageBuf);
365 if (r == E_INVALID_DATA || r == E_INVALID_ARG)
367 SetLastResult(E_UNSUPPORTED_FORMAT);
373 return IMG_FORMAT_NONE;
377 Image::HasAlphaChannels(const Tizen::Base::String& srcImagePath) const
379 result r = E_SUCCESS;
381 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
382 SysTryCatch(NID_MEDIA, !srcImagePath.IsEmpty(), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] srcImagePath is empty");
383 SysTryCatch(NID_MEDIA, File::IsFileExist(srcImagePath), r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
384 "[E_FILE_NOT_FOUND] srcImagePath:%ls", srcImagePath.GetPointer());
386 return _ImageUtilImpl::HasAlphaChannel(srcImagePath);
394 Image::HasAlphaChannels(const Tizen::Base::ByteBuffer& srcImageBuf) const
396 result r = E_SUCCESS;
398 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
399 SysTryCatch(NID_MEDIA, &srcImageBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] srcImageBuf is null");
401 return _ImageUtilImpl::HasAlphaChannel(srcImageBuf);
408 Image::DecodeUrl(const Tizen::Base::Utility::Uri& srcImageUrl,
409 Tizen::Graphics::BitmapPixelFormat pixelFormat, int destWidth,
410 int destHeight, RequestId& reqId, const IImageDecodeUrlEventListener& listener,
413 result r = E_SUCCESS;
415 SysAssertf(__pImageImpl != null, "Not yet constructed. Construct() should be called before use.");
416 SysTryCatch(NID_MEDIA, ((pixelFormat==BITMAP_PIXEL_FORMAT_RGB565) || (pixelFormat==BITMAP_PIXEL_FORMAT_ARGB8888) || (pixelFormat==BITMAP_PIXEL_FORMAT_R8G8B8A8)),
417 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] pixelFormat:%d", pixelFormat);
418 SysTryCatch(NID_MEDIA, destWidth > 0, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] destWidth:%d", destWidth);
419 SysTryCatch(NID_MEDIA, destHeight > 0, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] destHeight:%d", destHeight);
421 SysTryCatch(NID_MEDIA, timeout == TIMEOUT_INFINITE || timeout > 0, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE,
422 "[E_OUT_OF_RANGE] timeout:%d", timeout);
424 r = __pImageImpl->DecodeUrl(srcImageUrl, pixelFormat, destWidth,destHeight, reqId, listener, timeout);
425 SysTryCatch(NID_MEDIA, r == E_SUCCESS , , r, "[%s] Propagated.", GetErrorMessage(r));