Merge "use modern construct '= default' for special functions." into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / common / image-attributes.cpp
1 /*
2  * Copyright (c) 2018 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
18 // CLASS HEADER
19 #include <dali/internal/common/image-attributes.h>
20
21 // EXTERNAL INCLUDES
22 #include <cmath>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28
29 const ImageAttributes ImageAttributes::DEFAULT_ATTRIBUTES;
30
31 struct ImageAttributes::ImageAttributesImpl
32 {
33   ImageAttributesImpl()
34   :  width(0),
35      height(0),
36      scaling(Dali::FittingMode::SHRINK_TO_FIT),
37      filtering(SamplingMode::BOX),
38      mOrientationCorrection(false)
39   {
40   }
41
42   ~ImageAttributesImpl() = default;
43
44   ImageAttributesImpl(const ImageAttributesImpl& rhs) = default;
45
46   ImageAttributesImpl& operator=(const ImageAttributesImpl& rhs)
47   {
48     if (this != &rhs)
49     {
50       width = rhs.width;
51       height = rhs.height;
52       scaling = rhs.scaling;
53       filtering = rhs.filtering;
54
55       mOrientationCorrection = rhs.mOrientationCorrection;
56     }
57
58     return *this;
59   }
60
61   uint16_t  width;       ///< image width in pixels
62   uint16_t  height;      ///< image height in pixels
63   ScalingMode   scaling : 3;      ///< scaling option, ShrinkToFit is default
64   FilterMode    filtering : 4;    ///< filtering option. Box is the default
65   bool          mOrientationCorrection : 1; ///< If true, image pixels are reordered according to orientation metadata on load.
66 };
67
68
69 ImageAttributes::ImageAttributes()
70 : impl( new ImageAttributesImpl() )
71 {
72 }
73
74 ImageAttributes::ImageAttributes(const ImageAttributes& rhs)
75 : impl( new ImageAttributesImpl(*rhs.impl) )
76 {
77 }
78
79 ImageAttributes& ImageAttributes::operator=(const ImageAttributes& rhs)
80 {
81   *impl = *rhs.impl;
82
83   return *this;
84 }
85
86 ImageAttributes::~ImageAttributes()
87 {
88   delete impl;
89 }
90
91 void ImageAttributes::SetSize(uint32_t width, uint32_t height)
92 {
93   impl->width = static_cast<uint16_t>( width ); // truncated
94   impl->height = static_cast<uint16_t>( height ); // truncated
95 }
96
97 void ImageAttributes::SetSize( const Size& size )
98 {
99   impl->width = static_cast<uint16_t>( size.width ); // truncated
100   impl->height = static_cast<uint16_t>( size.height ); // truncated
101 }
102
103 void ImageAttributes::SetScalingMode( ScalingMode scale )
104 {
105   impl->scaling = scale;
106 }
107
108 void ImageAttributes::SetFilterMode( FilterMode filtering )
109 {
110   impl->filtering = filtering;
111 }
112
113 void ImageAttributes::SetOrientationCorrection(const bool enabled)
114 {
115   impl->mOrientationCorrection = enabled;
116 }
117
118 void ImageAttributes::Reset( ImageDimensions dimensions, ScalingMode scaling, FilterMode sampling, bool orientationCorrection )
119 {
120   impl->width = dimensions.GetWidth();
121   impl->height = dimensions.GetHeight();
122   impl->scaling = scaling;
123   impl->filtering = sampling;
124   impl->mOrientationCorrection = orientationCorrection;
125 }
126
127 uint32_t ImageAttributes::GetWidth() const
128 {
129   return impl->width;
130 }
131
132 uint32_t ImageAttributes::GetHeight() const
133 {
134   return impl->height;
135 }
136
137 Size ImageAttributes::GetSize() const
138 {
139   return Size(impl->width, impl->height);
140 }
141
142 ImageAttributes::ScalingMode ImageAttributes::GetScalingMode() const
143 {
144   return impl->scaling;
145 }
146
147 ImageAttributes::FilterMode ImageAttributes::GetFilterMode() const
148 {
149   return impl->filtering;
150 }
151
152 bool ImageAttributes::GetOrientationCorrection() const
153 {
154   return impl->mOrientationCorrection;
155 }
156
157 ImageAttributes ImageAttributes::New()
158 {
159   return ImageAttributes();
160 }
161
162 /**
163  * Less then comparison operator.
164  * @param [in] a parameter tested
165  * @param [in] b parameter tested
166  */
167 bool operator<(const ImageAttributes& a, const ImageAttributes& b)
168 {
169   if (a.impl->width != b.impl->width)
170   {
171     return a.impl->width < b.impl->width;
172   }
173
174   if (a.impl->height != b.impl->height)
175   {
176     return a.impl->height < b.impl->height;
177   }
178
179   if (a.impl->mOrientationCorrection != b.impl->mOrientationCorrection)
180   {
181     return a.impl->mOrientationCorrection < b.impl->mOrientationCorrection;
182   }
183
184   if (a.impl->scaling != b.impl->scaling)
185   {
186     return a.impl->scaling < b.impl->scaling;
187   }
188
189   if (a.impl->filtering != b.impl->filtering)
190   {
191     return a.impl->filtering < b.impl->filtering;
192   }
193
194   // they are equal
195   return false;
196 }
197
198 /**
199  * Equal to comparison operator.
200  * @param [in] a parameter tested for equality
201  * @param [in] b parameter tested for equality
202  */
203 bool operator==(const ImageAttributes& a, const ImageAttributes& b)
204 {
205   return a.impl->width                  == b.impl->width       &&
206          a.impl->height                 == b.impl->height      &&
207          a.impl->mOrientationCorrection == b.impl->mOrientationCorrection &&
208          a.impl->scaling                == b.impl->scaling     &&
209          a.impl->filtering              == b.impl->filtering;
210 }
211
212 /**
213  * Not equal to comparison operator.
214  * @param [in] a parameter tested for equality
215  * @param [in] b parameter tested for equality
216  */
217 bool operator!=(const ImageAttributes& a, const ImageAttributes& b)
218 {
219   return !(a == b);
220 }
221
222 } // namespace Internal
223 } // namespace Dali