2 // Open Service Platform
3 // Copyright (c) 2012-2013 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 FApp_AppResourceBitmap.cpp
20 * @brief This is the implementation for the _AppResourceBitmap class.
24 #include <FBaseSysLog.h>
25 #include <FBaseResult.h>
26 #include <FGrpBitmapCommon.h>
27 #include <FIoDirectory.h>
28 #include <FMediaImageTypes.h>
29 #include <FMedia_ImageUtil.h>
31 #include "FApp_AppResourceBitmap.h"
32 #include "FApp_AppResourceBitmapUtil.h"
35 using namespace Tizen::Base;
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Media;
39 static const int BITMAP_PIXEL_FORMAT_INVALID = BITMAP_PIXEL_FORMAT_MIN;
41 namespace Tizen { namespace App
44 _AppResourceBitmap* _AppResourceBitmap::__pTheInstance = null;
46 _AppResourceBitmap::_AppResourceBitmap(void)
47 : __pAppResourceBitmapUtil(null)
51 _AppResourceBitmap::~_AppResourceBitmap(void)
56 _AppResourceBitmap::InitAppResourceBitmap(void)
58 if (__pTheInstance != null)
63 static _AppResourceBitmap instance;
64 result r = instance.Construct();
65 SysTryReturnVoidResult(NID_APP, r == E_SUCCESS, E_FAILURE, "[E_FAILURE] Failed to construct an _AppResourceBitmapImpl instance.");
67 __pTheInstance = &instance;
71 _AppResourceBitmap::GetInstance(void)
73 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
75 if (__pTheInstance == null)
77 pthread_once(&once_block, InitAppResourceBitmap);
80 return __pTheInstance;
84 _AppResourceBitmap::GetInstanceByAppId(const String& appId)
86 _AppResourceBitmap* pAppResourceBitmap = null;
88 pAppResourceBitmap = new (std::nothrow) _AppResourceBitmap();
89 SysTryReturn(NID_APP, pAppResourceBitmap != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
91 result r = pAppResourceBitmap->Construct(appId);
92 SysTryCatch(NID_APP, r == E_SUCCESS, , E_FAILURE, "[E_FAILURE] Failed to construct an _AppResourceBitmapImpl instance.");
94 return pAppResourceBitmap;
97 delete pAppResourceBitmap;
102 _AppResourceBitmap::Construct(void)
104 SysAssertf(!__pAppResourceBitmapUtil, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
106 unique_ptr<_AppResourceBitmapUtil> pNewAppResourceBitmapUtil(new (std::nothrow) _AppResourceBitmapUtil());
107 SysTryReturnResult(NID_APP, pNewAppResourceBitmapUtil != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
109 result r = pNewAppResourceBitmapUtil->Construct();
110 SysTryReturnResult(NID_APP, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Failed to construct an _AppResourceBitmapUtil instance.");
112 __pAppResourceBitmapUtil = move(pNewAppResourceBitmapUtil);
118 _AppResourceBitmap::Construct(const String& appId)
120 SysAssertf(!__pAppResourceBitmapUtil, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
122 unique_ptr<_AppResourceBitmapUtil> pNewAppResourceBitmapUtil(new (std::nothrow) _AppResourceBitmapUtil());
123 SysTryReturnResult(NID_APP, pNewAppResourceBitmapUtil != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
125 result r = pNewAppResourceBitmapUtil->Construct(appId);
126 SysTryReturnResult(NID_APP, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Failed to construct an _AppResourceBitmapUtil instance.");
128 __pAppResourceBitmapUtil = move(pNewAppResourceBitmapUtil);
135 _AppResourceBitmap::GetBitmapN(const String& imageFileName, BitmapPixelFormat pixelFormat) const
138 SysTryReturn(NID_APP, imageFileName.IsEmpty() == false, null, E_INVALID_ARG, "[%s] Invalid argument is used. (imageFileName = null)", imageFileName.GetPointer());
139 SysTryReturn(NID_APP, (pixelFormat >= BITMAP_PIXEL_FORMAT_MIN && pixelFormat < BITMAP_PIXEL_FORMAT_MAX),
140 null, E_UNSUPPORTED_FORMAT, "[E_UNSUPPORTED_FORMAT] The image file format or specified pixel format(%d) is not supported.", pixelFormat);
143 String resourcePath(null);
144 bool imageScaling(true);
145 _AppResourceBitmapUtil::_Density resourceDensity = _AppResourceBitmapUtil::DENSITY_NONE;
147 result r = __pAppResourceBitmapUtil->GetResourcePath(imageFileName, resourcePath, resourceDensity, imageScaling);
148 SysTryReturn(NID_APP, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
149 SysTryReturn(NID_APP, resourcePath.IsEmpty() == false, null, r = E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] The specified file does not exist.");
153 Bitmap* pBitmap = null;
155 if (pixelFormat == BITMAP_PIXEL_FORMAT_INVALID)
157 if (_ImageUtil::HasAlphaChannel(resourcePath))
159 pBitmap = __pAppResourceBitmapUtil->GetBitmapN(resourcePath, resourceDensity, BITMAP_PIXEL_FORMAT_ARGB8888, imageScaling);
163 const Color& magentaColor = Color::GetColor(COLOR_ID_MAGENTA);
164 pBitmap = __pAppResourceBitmapUtil->GetBitmapN(resourcePath, resourceDensity, BITMAP_PIXEL_FORMAT_RGB565, imageScaling, &magentaColor);
169 pBitmap = __pAppResourceBitmapUtil->GetBitmapN(resourcePath, resourceDensity, pixelFormat, imageScaling);
172 SysLog(NID_APP, "The current value of bitmap path is %S(%S) and pixel format is %d", resourcePath.GetPointer(), imageScaling?L"scaled":L"not scaled", pixelFormat);
176 SysTryReturn(NID_APP, pBitmap != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
187 _OSP_EXPORT_ Tizen::App::_IAppResourceBitmap*
188 _Create_IAppResourceBitmapInstanceN(void)
190 return Tizen::App::_AppResourceBitmap::GetInstance();
193 _OSP_EXPORT_ Tizen::App::_IAppResourceBitmap*
194 _Create_IAppResourceBitmapInstanceByAppIdN(const String& appId)
196 return Tizen::App::_AppResourceBitmap::GetInstanceByAppId(appId);