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