00c1204c646a11f2b451da1a95742f911c75d22f
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-bitmap-loader.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <dali/public-api/object/base-object.h>
18 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
19 #include <cstring>
20
21 using namespace Dali;
22
23 namespace Dali
24 {
25 class Adaptor;
26
27 namespace Internal
28 {
29
30 class BitmapLoader : public BaseObject
31 {
32 public:
33   static IntrusivePtr<BitmapLoader> New( const std::string& url,
34                                          ImageDimensions size,
35                                          FittingMode::Type fittingMode,
36                                          SamplingMode::Type samplingMode,
37                                          bool orientationCorrection)
38   {
39     IntrusivePtr<BitmapLoader> internal = new BitmapLoader( url, size, fittingMode, samplingMode, orientationCorrection );
40     return internal;
41   }
42
43   BitmapLoader(const std::string& url,
44                ImageDimensions size,
45                FittingMode::Type fittingMode,
46                SamplingMode::Type samplingMode,
47                bool orientationCorrection)
48   : mSize(size),
49     mPixelData(),
50     mUrl(url)
51   {
52   }
53
54   ~BitmapLoader(){}
55
56   void Load()
57   {
58     size_t bufferSize  = mSize.GetWidth() * mSize.GetHeight() * 4;
59     unsigned char* buffer = static_cast<unsigned char*>(malloc(bufferSize));
60     memset(buffer, 0, bufferSize);
61
62     mPixelData = PixelData::New( buffer, mSize.GetWidth(), mSize.GetHeight(), Pixel::RGBA8888, PixelData::FREE);
63   }
64
65   PixelDataPtr GetPixelData() const
66   {
67     return mPixelData;
68   }
69
70   const std::string& GetUrl() const
71   {
72     return mUrl;
73   }
74
75   bool IsLoaded()
76   {
77     return mPixelData ? true : false;
78   }
79
80   ImageDimensions mSize;
81   PixelDataPtr mPixelData;
82   const std::string mUrl;
83 };
84
85 } // internal
86
87 inline Internal::BitmapLoader& GetImplementation(Dali::BitmapLoader& handle)
88 {
89   DALI_ASSERT_ALWAYS( handle && "handle is empty" );
90   BaseObject& object = handle.GetBaseObject();
91   return static_cast<Internal::BitmapLoader&>(object);
92 }
93
94 inline const Internal::BitmapLoader& GetImplementation(const Dali::BitmapLoader& handle)
95 {
96   DALI_ASSERT_ALWAYS( handle && "handle is empty" );
97   const BaseObject& object = handle.GetBaseObject();
98   return static_cast<const Internal::BitmapLoader&>(object);
99 }
100
101 Dali::BitmapLoader Dali::BitmapLoader::New(std::string const&url, Dali::Uint16Pair size, Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode, bool orientationCorrection)
102 {
103   IntrusivePtr<Internal::BitmapLoader> internal = Internal::BitmapLoader::New(url, size, fittingMode, samplingMode, orientationCorrection);
104   return BitmapLoader( internal.Get() );
105 }
106
107 Dali::BitmapLoader::BitmapLoader(Dali::BitmapLoader const& handle)
108 :BaseHandle(handle)
109 {
110 }
111 Dali::BitmapLoader::BitmapLoader(Internal::BitmapLoader* internal)
112 :BaseHandle(internal)
113 {
114 }
115 Dali::BitmapLoader::~BitmapLoader()
116 {
117 }
118 void Dali::BitmapLoader::Load()
119 {
120   GetImplementation(*this).Load();
121 }
122 PixelDataPtr Dali::BitmapLoader::GetPixelData() const
123 {
124   return GetImplementation(*this).GetPixelData();
125 }
126 std::string Dali::BitmapLoader::GetUrl() const
127 {
128   return GetImplementation(*this).GetUrl();
129 }
130 bool Dali::BitmapLoader::IsLoaded()
131 {
132   return GetImplementation(*this).IsLoaded();
133 }
134
135 } // Dali