134debc32f0b3506757fed24f2f5a5a2b55230f8
[platform/core/uifw/dali-core.git] / dali / internal / common / image-attributes.cpp
1 /*
2  * Copyright (c) 2014 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()
43   {
44   }
45
46   ImageAttributesImpl(const ImageAttributesImpl& rhs)
47   : width( rhs.width ),
48     height( rhs.height ),
49     scaling( rhs.scaling ),
50     filtering( rhs.filtering ),
51     mOrientationCorrection( rhs.mOrientationCorrection )
52   {
53   }
54
55   ImageAttributesImpl& operator=(const ImageAttributesImpl& rhs)
56   {
57     if (this != &rhs)
58     {
59       width = rhs.width;
60       height = rhs.height;
61       scaling = rhs.scaling;
62       filtering = rhs.filtering;
63
64       mOrientationCorrection = rhs.mOrientationCorrection;
65     }
66
67     return *this;
68   }
69
70   unsigned int  width : 16;       ///< image width in pixels
71   unsigned int  height : 16;      ///< image height in pixels
72   ScalingMode   scaling : 3;      ///< scaling option, ShrinkToFit is default
73   FilterMode    filtering : 3;    ///< filtering option. Box is the default
74   bool          mOrientationCorrection : 1; ///< If true, image pixels are reordered according to orientation metadata on load.
75   bool          isDistanceField : 1;  ///< true, if the image is a distancefield. Default is false.
76 };
77
78
79 ImageAttributes::ImageAttributes()
80 : impl( new ImageAttributesImpl() )
81 {
82 }
83
84 ImageAttributes::ImageAttributes(const ImageAttributes& rhs)
85 : impl( new ImageAttributesImpl(*rhs.impl) )
86 {
87 }
88
89 ImageAttributes& ImageAttributes::operator=(const ImageAttributes& rhs)
90 {
91   *impl = *rhs.impl;
92
93   return *this;
94 }
95
96 ImageAttributes::~ImageAttributes()
97 {
98   delete impl;
99 }
100
101 void ImageAttributes::SetSize(unsigned int width, unsigned int height)
102 {
103   impl->width = width;
104   impl->height = height;
105 }
106
107 void ImageAttributes::SetSize( const Size& size )
108 {
109   impl->width = size.width;
110   impl->height = size.height;
111 }
112
113 void ImageAttributes::SetScalingMode( ScalingMode scale )
114 {
115   impl->scaling = scale;
116 }
117
118 void ImageAttributes::SetFilterMode( FilterMode filtering )
119 {
120   impl->filtering = filtering;
121 }
122
123 void ImageAttributes::SetOrientationCorrection(const bool enabled)
124 {
125   impl->mOrientationCorrection = enabled;
126 }
127
128 void ImageAttributes::Reset( ImageDimensions dimensions, ScalingMode scaling, FilterMode sampling, bool orientationCorrection )
129 {
130   impl->width = dimensions.GetWidth();
131   impl->height = dimensions.GetHeight();
132   impl->scaling = scaling;
133   impl->filtering = sampling;
134   impl->mOrientationCorrection = orientationCorrection;
135 }
136
137 unsigned int ImageAttributes::GetWidth() const
138 {
139   return impl->width;
140 }
141
142 unsigned int ImageAttributes::GetHeight() const
143 {
144   return impl->height;
145 }
146
147 Size ImageAttributes::GetSize() const
148 {
149   return Size(impl->width, impl->height);
150 }
151
152 ImageAttributes::ScalingMode ImageAttributes::GetScalingMode() const
153 {
154   return impl->scaling;
155 }
156
157 ImageAttributes::FilterMode ImageAttributes::GetFilterMode() const
158 {
159   return impl->filtering;
160 }
161
162 bool ImageAttributes::GetOrientationCorrection() const
163 {
164   return impl->mOrientationCorrection;
165 }
166
167 ImageAttributes ImageAttributes::New()
168 {
169   return ImageAttributes();
170 }
171
172 ImageAttributes ImageAttributes::New(unsigned int imageWidth, unsigned int imageHeight)
173 {
174   ImageAttributes attributes;
175   attributes.impl->width = imageWidth;
176   attributes.impl->height = imageHeight;
177   return attributes;
178 }
179
180 /**
181  * Less then comparison operator.
182  * @param [in] a parameter tested
183  * @param [in] b parameter tested
184  */
185 bool operator<(const ImageAttributes& a, const ImageAttributes& b)
186 {
187   // Bail out if one is distance field and the other is not.
188   if (a.impl->isDistanceField != b.impl->isDistanceField)
189   {
190     return a.impl->isDistanceField < b.impl->isDistanceField;
191   }
192
193   if (a.impl->width != b.impl->width)
194   {
195     return a.impl->width < b.impl->width;
196   }
197
198   if (a.impl->height != b.impl->height)
199   {
200     return a.impl->height < b.impl->height;
201   }
202
203   if (a.impl->mOrientationCorrection != b.impl->mOrientationCorrection)
204   {
205     return a.impl->mOrientationCorrection < b.impl->mOrientationCorrection;
206   }
207
208   if (a.impl->scaling != b.impl->scaling)
209   {
210     return a.impl->scaling < b.impl->scaling;
211   }
212
213   if (a.impl->filtering != b.impl->filtering)
214   {
215     return a.impl->filtering < b.impl->filtering;
216   }
217
218   // they are equal
219   return false;
220 }
221
222 /**
223  * Equal to comparison operator.
224  * @param [in] a parameter tested for equality
225  * @param [in] b parameter tested for equality
226  */
227 bool operator==(const ImageAttributes& a, const ImageAttributes& b)
228 {
229   return a.impl->width                  == b.impl->width       &&
230          a.impl->height                 == b.impl->height      &&
231          a.impl->mOrientationCorrection == b.impl->mOrientationCorrection &&
232          a.impl->scaling                == b.impl->scaling     &&
233          a.impl->filtering              == b.impl->filtering;
234 }
235
236 /**
237  * Not equal to comparison operator.
238  * @param [in] a parameter tested for equality
239  * @param [in] b parameter tested for equality
240  */
241 bool operator!=(const ImageAttributes& a, const ImageAttributes& b)
242 {
243   return !(a == b);
244 }
245
246 } // namespace Internal
247 } // namespace Dali