Grok  9.7.5
util.h
Go to the documentation of this file.
1 
17 #pragma once
18 
19 #include "grok.h"
20 #include "logger.h"
21 #include <iostream>
22 #include <cstdint>
23 #include "grk_intmath.h"
24 #include <limits>
25 #include <sstream>
26 
27 namespace grk
28 {
29 template<typename T>
30 struct grk_pt
31 {
32  grk_pt() : x(0), y(0) {}
33  grk_pt(T _x, T _y) : x(_x), y(_y) {}
34  T x;
35  T y;
36 };
38 
39 template<typename T>
40 struct grk_line
41 {
42  grk_line() : x0(0), x1(0) {}
43  grk_line(T _x0, T _x1) : x0(_x0), x1(_x1) {}
44  T x0;
45  T x1;
46 
47  T length() const
48  {
49  assert(x1 >= x0);
50  return (T)(x1 - x0);
51  }
52 };
54 
55 template<typename T>
56 struct grk_rect;
59 
60 template<typename T>
61 T clip(int64_t val)
62 {
63  static_assert(sizeof(T) <= 4);
64  if(val < (std::numeric_limits<T>::min)())
65  val = (std::numeric_limits<T>::min)();
66  else if(val > (std::numeric_limits<T>::max)())
67  val = (std::numeric_limits<T>::max)();
68  return (T)val;
69 }
70 
71 template<typename T>
72 T satAdd(int64_t lhs, int64_t rhs)
73 {
74  return clip<T>(lhs + rhs);
75 }
76 
77 template<typename T>
78 T satAdd(T lhs, T rhs)
79 {
80  return clip<T>((int64_t)lhs + rhs);
81 }
82 
83 template<typename T>
84 T satSub(T lhs, T rhs)
85 {
86  return clip<T>((int64_t)lhs - rhs);
87 }
88 
89 template<typename T>
90 T satSub(int64_t lhs, int64_t rhs)
91 {
92  return clip<T>(lhs - rhs);
93 }
94 
95 template<typename T>
96 struct grk_rect
97 {
98  grk_rect(T x0, T y0, T x1, T y1) : x0(x0), y0(y0), x1(x1), y1(y1) {}
99  grk_rect(const grk_rect& rhs) : grk_rect(&rhs) {}
100  grk_rect(const grk_rect* rhs)
101  {
102  x0 = rhs->x0;
103  y0 = rhs->y0;
104  x1 = rhs->x1;
105  y1 = rhs->y1;
106  }
107  grk_rect(void) : x0(0), y0(0), x1(0), y1(0) {}
108  virtual ~grk_rect() = default;
109  T x0, y0, x1, y1;
110 
111  virtual void print(void) const
112  {
113  GRK_INFO("[%u,%u,%u,%u]", x0, y0, x1, y1);
114  }
115  std::string boundsString() const
116  {
117  std::ostringstream os;
118  os << "[" << x0 << "," << y0 << "," << x1 << "," << y1 << "]";
119  return os.str();
120  }
121  bool valid(void) const
122  {
123  return x0 <= x1 && y0 <= y1;
124  }
125  bool empty(void) const
126  {
127  return x0 >= x1 || y0 >= y1;
128  }
130  {
131  return pt.x >= x0 && pt.y >= y0 && pt.x < x1 && pt.y < y1;
132  }
134  {
135  return operator=(&rhs);
136  }
138  {
139  assert(rhs);
140  if(rhs && (this != rhs))
141  { // self-assignment check expected
142  x0 = rhs->x0;
143  y0 = rhs->y0;
144  x1 = rhs->x1;
145  y1 = rhs->y1;
146  }
147  return *this;
148  }
149  bool operator==(const grk_rect<T>& rhs) const
150  {
151  if(this == &rhs)
152  return true;
153  return x0 == rhs.x0 && y0 == rhs.y0 && x1 == rhs.x1 && y1 == rhs.y1;
154  }
155  void set(grk_rect<T>* rhs)
156  {
157  *this = *rhs;
158  }
159  void set(grk_rect<T> rhs)
160  {
161  set(&rhs);
162  }
163  grk_rect<T> scaleDownCeil(uint32_t den) const
164  {
165  return grk_rect<T>(ceildiv(x0, den), ceildiv(y0, den), ceildiv(x1, den), ceildiv(y1, den));
166  }
167  grk_rect<T> scale(uint32_t scalex, uint32_t scaley) const
168  {
169  return grk_rect<T>(x0 * scalex, y0 * scaley, x1 * scalex, y1 * scaley);
170  }
171  grk_rect<T> scaleDown(uint64_t denx, uint64_t deny) const
172  {
173  return grk_rect<T>((T)(x0 / denx), (T)(y0 / deny), (T)ceildiv<uint64_t>(x1, denx),
174  (T)ceildiv<uint64_t>(y1, deny));
175  }
176  grk_rect<T> scaleDownPow2(uint32_t powx, uint32_t powy) const
177  {
178  return grk_rect<T>((T)(x0 >> powx), (T)(y0 >> powy), (T)ceildivpow2<uint64_t>(x1, powx),
179  (T)ceildivpow2<uint64_t>(y1, powy));
180  }
182  {
183  return scaleDownPow2(pow.x, pow.y);
184  }
185  grk_rect<T> scaleDownCeil(uint64_t denx, uint64_t deny) const
186  {
187  return grk_rect<T>(ceildiv<uint64_t>(x0, denx), ceildiv<uint64_t>(y0, deny),
188  ceildiv<uint64_t>(x1, denx), ceildiv<uint64_t>(y1, deny));
189  }
190  grk_rect<T> scaleDownCeilPow2(uint32_t power) const
191  {
192  return grk_rect<T>(ceildivpow2(x0, power), ceildivpow2(y0, power), ceildivpow2(x1, power),
193  ceildivpow2(y1, power));
194  }
195  grk_rect<T> scaleDownCeilPow2(uint32_t powx, uint32_t powy) const
196  {
197  return grk_rect<T>(ceildivpow2<uint64_t>(x0, powx), ceildivpow2<uint64_t>(y0, powy),
198  ceildivpow2<uint64_t>(x1, powx), ceildivpow2<uint64_t>(y1, powy));
199  }
201  {
202  return intersection(&rhs);
203  }
204  bool isContainedIn(const grk_rect<T> rhs) const
205  {
206  return (intersection(&rhs) == *this);
207  }
208  grk_rect<T> clip(const grk_rect<T>* rhs) const
209  {
210  return grk_rect<T>(std::max<T>(x0, rhs->x0), std::max<T>(y0, rhs->y0),
211  std::min<T>(x1, rhs->x1), std::min<T>(y1, rhs->y1));
212  }
213  grk_rect<T> clip(const grk_rect<T>& rhs) const
214  {
215  return clip(&rhs);
216  }
217  // IPL stands for in place
218  void clipIPL(const grk_rect<T>* rhs)
219  {
220  *this = grk_rect<T>(std::max<T>(x0, rhs->x0), std::max<T>(y0, rhs->y0),
221  std::min<T>(x1, rhs->x1), std::min<T>(y1, rhs->y1));
222  }
224  {
225  return grk_rect<T>(std::max<T>(x0, rhs->x0), std::max<T>(y0, rhs->y0),
226  std::min<T>(x1, rhs->x1), std::min<T>(y1, rhs->y1));
227  }
228  inline bool nonEmptyIntersection(const grk_rect<T>* rhs) const
229  {
230  return std::max<T>(x0, rhs->x0) < std::min<T>(x1, rhs->x1) &&
231  std::max<T>(y0, rhs->y0) < std::min<T>(y1, rhs->y1);
232  }
234  {
235  return grk_rect<T>(std::min<T>(x0, rhs->x0), std::min<T>(y0, rhs->y0),
236  std::max<T>(x1, rhs->x1), std::max<T>(y1, rhs->y1));
237  }
239  {
240  return rectUnion(&rhs);
241  }
242  uint64_t area(void) const
243  {
244  return (uint64_t)(x1 - x0) * (y1 - y0);
245  }
246  T width() const
247  {
248  return x1 - x0;
249  }
250  T height() const
251  {
252  return y1 - y0;
253  }
255  {
256  return grk_line<T>(x0, x1);
257  }
259  {
260  return grk_line<T>(y0, y1);
261  }
262  grk_rect<T> pan(int64_t x, int64_t y) const
263  {
264  return grk_rect<T>(satAdd<T>((int64_t)x0, (int64_t)x), satAdd<T>((int64_t)y0, (int64_t)y),
265  satAdd<T>((int64_t)x1, (int64_t)x), satAdd<T>((int64_t)y1, (int64_t)y));
266  }
267  // IPL stands for in place
268  grk_rect<T>& growIPL(T boundary)
269  {
270  return growIPL(boundary, boundary, (std::numeric_limits<T>::max)(),
271  (std::numeric_limits<T>::max)());
272  }
273  grk_rect<T>& growIPL(T boundaryx, T boundaryy)
274  {
275  return growIPL(boundaryx, boundaryy, (std::numeric_limits<T>::max)(),
276  (std::numeric_limits<T>::max)());
277  }
278  grk_rect<T>& growIPL(T boundary, T maxX, T maxY)
279  {
280  return growIPL(boundary, boundary, maxX, maxY);
281  }
282  grk_rect<T>& growIPL(T boundaryx, T boundaryy, T maxX, T maxY)
283  {
284  return growIPL(boundaryx, boundaryy, grk_rect<T>((T)0, (T)0, maxX, maxY));
285  }
286  grk_rect<T>& growIPL(T boundary, grk_rect<T> bounds)
287  {
288  return growIPL(boundary, boundary, bounds);
289  }
290  grk_rect<T>& growIPL(T boundaryx, T boundaryy, grk_rect<T> bounds)
291  {
292  x0 = std::max<T>(satSub<T>(x0, boundaryx), bounds.x0);
293  y0 = std::max<T>(satSub<T>(y0, boundaryy), bounds.y0);
294  x1 = std::min<T>(satAdd<T>(x1, boundaryx), bounds.x1);
295  y1 = std::min<T>(satAdd<T>(y1, boundaryy), bounds.y1);
296 
297  return *this;
298  }
299  T parityX(void) const
300  {
301  return T(x0 & 1);
302  }
303  T parityY(void) const
304  {
305  return T(y0 & 1);
306  }
307 };
308 
309 using grk_rect32 = grk_rect<uint32_t>;
310 
311 } // namespace grk
Copyright (C) 2016-2022 Grok Image Compression Inc.
Definition: ICacheable.h:20
T satAdd(int64_t lhs, int64_t rhs)
Definition: util.h:72
void GRK_INFO(const char *fmt,...)
Definition: logger.cpp:40
uint32_t ceildiv(T a, T b)
Divide an integer by another integer and round upwards.
Definition: grk_intmath.h:33
T satSub(T lhs, T rhs)
Definition: util.h:84
void clip(grk_image_comp *component, uint8_t precision)
Definition: GrkImage_Conversion.cpp:215
grk_rect< uint32_t > grk_rect32
Definition: util.h:57
T ceildivpow2(T a, uint32_t b)
Definition: grk_intmath.h:40
Definition: util.h:41
T x0
Definition: util.h:44
grk_line(T _x0, T _x1)
Definition: util.h:43
grk_line()
Definition: util.h:42
T x1
Definition: util.h:45
T length() const
Definition: util.h:47
Definition: util.h:31
T x
Definition: util.h:34
T y
Definition: util.h:35
grk_pt(T _x, T _y)
Definition: util.h:33
grk_pt()
Definition: util.h:32
Definition: util.h:97
uint64_t area(void) const
Definition: util.h:242
T width() const
Definition: util.h:246
void clipIPL(const grk_rect< T > *rhs)
Definition: util.h:218
grk_rect< T > rectUnion(const grk_rect< T > &rhs) const
Definition: util.h:238
T y1
Definition: util.h:109
grk_rect< T > scaleDownCeilPow2(uint32_t powx, uint32_t powy) const
Definition: util.h:195
T x0
Definition: util.h:109
bool operator==(const grk_rect< T > &rhs) const
Definition: util.h:149
grk_rect< T > & growIPL(T boundaryx, T boundaryy, grk_rect< T > bounds)
Definition: util.h:290
T x1
Definition: util.h:109
grk_rect< T > scaleDownPow2(uint32_t powx, uint32_t powy) const
Definition: util.h:176
void set(grk_rect< T > *rhs)
Definition: util.h:155
grk_rect< T > & operator=(const grk_rect< T > &rhs)
Definition: util.h:133
grk_rect< T > & growIPL(T boundaryx, T boundaryy, T maxX, T maxY)
Definition: util.h:282
T height() const
Definition: util.h:250
grk_rect< T > & growIPL(T boundaryx, T boundaryy)
Definition: util.h:273
bool valid(void) const
Definition: util.h:121
T parityY(void) const
Definition: util.h:303
grk_rect< T > clip(const grk_rect< T > *rhs) const
Definition: util.h:208
grk_rect< T > scaleDownCeilPow2(uint32_t power) const
Definition: util.h:190
grk_rect< T > scaleDown(uint64_t denx, uint64_t deny) const
Definition: util.h:171
grk_rect< T > pan(int64_t x, int64_t y) const
Definition: util.h:262
grk_rect< T > scale(uint32_t scalex, uint32_t scaley) const
Definition: util.h:167
bool nonEmptyIntersection(const grk_rect< T > *rhs) const
Definition: util.h:228
grk_rect< T > clip(const grk_rect< T > &rhs) const
Definition: util.h:213
T parityX(void) const
Definition: util.h:299
grk_rect< T > scaleDownPow2(grk_pt< T > pow) const
Definition: util.h:181
virtual ~grk_rect()=default
grk_rect< T > scaleDownCeil(uint64_t denx, uint64_t deny) const
Definition: util.h:185
grk_rect< T > scaleDownCeil(uint32_t den) const
Definition: util.h:163
grk_rect< T > rectUnion(const grk_rect< T > *rhs) const
Definition: util.h:233
grk_rect< T > intersection(const grk_rect< T > rhs) const
Definition: util.h:200
bool contains(grk_pt< T > pt)
Definition: util.h:129
grk_rect< T > & growIPL(T boundary)
Definition: util.h:268
bool empty(void) const
Definition: util.h:125
grk_rect< T > intersection(const grk_rect< T > *rhs) const
Definition: util.h:223
grk_rect(T x0, T y0, T x1, T y1)
Definition: util.h:98
grk_rect(const grk_rect *rhs)
Definition: util.h:100
void set(grk_rect< T > rhs)
Definition: util.h:159
grk_line< T > dimY() const
Definition: util.h:258
virtual void print(void) const
Definition: util.h:111
grk_rect< T > & growIPL(T boundary, T maxX, T maxY)
Definition: util.h:278
grk_line< T > dimX() const
Definition: util.h:254
grk_rect(void)
Definition: util.h:107
grk_rect< T > & growIPL(T boundary, grk_rect< T > bounds)
Definition: util.h:286
bool isContainedIn(const grk_rect< T > rhs) const
Definition: util.h:204
grk_rect(const grk_rect &rhs)
Definition: util.h:99
std::string boundsString() const
Definition: util.h:115
grk_rect< T > & operator=(const grk_rect< T > *rhs)
Definition: util.h:137
T y0
Definition: util.h:109