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