Grok  9.5.0
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 grkPoint
31 {
32  grkPoint() : x(0), y(0) {}
33  grkPoint(T _x, T _y) : x(_x), y(_y) {}
34  T x;
35  T y;
36 };
38 
39 template<typename T>
40 struct grkLine
41 {
42  grkLine() : x0(0), x1(0) {}
43  grkLine(T _x0, T _x1) : x0(_x0), x1(_x1) {}
44  T x0;
45  T x1;
46  T length()
47  {
48  assert(x1 >= x0);
49  return (T)(x1 - x0);
50  }
51 };
53 
54 template<typename T>
55 struct grkRect;
57 
58 template<typename T>
59 T clip(int64_t val)
60 {
61  static_assert(sizeof(T) <= 4);
62  if(val < (std::numeric_limits<T>::min)())
63  val = (std::numeric_limits<T>::min)();
64  else if(val > (std::numeric_limits<T>::max)())
65  val = (std::numeric_limits<T>::max)();
66  return (T)val;
67 }
68 
69 template<typename T>
70 T satAdd(int64_t lhs, int64_t rhs)
71 {
72  return clip<T>(lhs + rhs);
73 }
74 
75 template<typename T>
76 T satAdd(T lhs, T rhs)
77 {
78  return clip<T>((int64_t)lhs + rhs);
79 }
80 
81 template<typename T>
82 T satSub(T lhs, T rhs)
83 {
84  return clip<T>((int64_t)lhs - rhs);
85 }
86 
87 template<typename T>
88 T satSub(int64_t lhs, int64_t rhs)
89 {
90  return clip<T>(lhs - rhs);
91 }
92 
93 template<typename T>
94 struct grkRect
95 {
96  T x0, y0, x1, y1;
97 
98  grkRect(T x0, T y0, T x1, T y1) : x0(x0), y0(y0), x1(x1), y1(y1) {}
99  grkRect(const grkRect& rhs)
100  {
101  x0 = rhs.x0;
102  y0 = rhs.y0;
103  x1 = rhs.x1;
104  y1 = rhs.y1;
105  }
106  grkRect(void) : x0(0), y0(0), x1(0), y1(0) {}
107  void print(void) const
108  {
109  std::cout << "[" << x0 << "," << y0 << "," << x1 << "," << y1 << "]" << std::endl;
110  }
111  std::string boundsString()
112  {
113  std::ostringstream os;
114  os << "[" << x0 << "," << y0 << "," << x1 << "," << y1 << "]";
115  return os.str();
116  }
117  bool is_valid(void) const
118  {
119  return x0 <= x1 && y0 <= y1;
120  }
121  bool non_empty(void) const
122  {
123  return x0 < x1 && y0 < y1;
124  }
126  {
127  return pt.x >= x0 && pt.y >= y0 && pt.x < x1 && pt.y < y1;
128  }
130  {
131  return operator=(&rhs);
132  }
134  {
135  assert(rhs);
136  if(rhs && (this != rhs))
137  { // self-assignment check expected
138  x0 = rhs->x0;
139  y0 = rhs->y0;
140  x1 = rhs->x1;
141  y1 = rhs->y1;
142  }
143  return *this;
144  }
145  bool operator==(const grkRect<T>& rhs) const
146  {
147  if(this == &rhs)
148  return true;
149  return x0 == rhs.x0 && y0 == rhs.y0 && x1 == rhs.x1 && y1 == rhs.y1;
150  }
151  void set(grkRect<T>* rhs)
152  {
153  *this = *rhs;
154  }
155  void set(grkRect<T> rhs)
156  {
157  set(&rhs);
158  }
159  grkRect<T> rectceildivpow2(uint32_t power) const
160  {
161  return grkRect<T>(ceildivpow2(x0, power), ceildivpow2(y0, power), ceildivpow2(x1, power),
162  ceildivpow2(y1, power));
163  }
164  grkRect<T> rectceildiv(uint32_t den) const
165  {
166  return grkRect<T>(ceildiv(x0, den), ceildiv(y0, den), ceildiv(x1, den), ceildiv(y1, den));
167  }
168  grkRect<T> rectceildiv(uint32_t denx, uint32_t deny) const
169  {
170  return grkRect<T>(ceildiv(x0, denx), ceildiv(y0, deny), ceildiv(x1, denx),
171  ceildiv(y1, deny));
172  }
174  {
175  return intersection(&rhs);
176  }
177  bool isContainedIn(const grkRect<T> rhs) const
178  {
179  return (intersection(&rhs) == *this);
180  }
181  void clip(const grkRect<T>* rhs)
182  {
183  *this = grkRect<T>(std::max<T>(x0, rhs->x0), std::max<T>(y0, rhs->y0),
184  std::min<T>(x1, rhs->x1), std::min<T>(y1, rhs->y1));
185  }
187  {
188  return grkRect<T>(std::max<T>(x0, rhs->x0), std::max<T>(y0, rhs->y0),
189  std::min<T>(x1, rhs->x1), std::min<T>(y1, rhs->y1));
190  }
191  inline bool non_empty_intersection(const grkRect<T>* rhs) const
192  {
193  return std::max<T>(x0, rhs->x0) < std::min<T>(x1, rhs->x1) &&
194  std::max<T>(y0, rhs->y0) < std::min<T>(y1, rhs->y1);
195  }
196  grkRect<T> rectUnion(const grkRect<T>* rhs) const
197  {
198  return grkRect<T>(std::min<T>(x0, rhs->x0), std::min<T>(y0, rhs->y0),
199  std::max<T>(x1, rhs->x1), std::max<T>(y1, rhs->y1));
200  }
201  grkRect<T> rectUnion(const grkRect<T>& rhs) const
202  {
203  return rectUnion(&rhs);
204  }
205  uint64_t area(void) const
206  {
207  return (uint64_t)(x1 - x0) * (y1 - y0);
208  }
209  T width() const
210  {
211  return x1 - x0;
212  }
213  T height() const
214  {
215  return y1 - y0;
216  }
218  {
219  return grkLine<T>(x0, x1);
220  }
222  {
223  return grkLine<T>(y0, y1);
224  }
225  grkRect<T> pan(int64_t x, int64_t y) const
226  {
227  auto rc = *this;
228  rc.panInplace(x, y);
229  return rc;
230  }
231  void panInplace(int64_t x, int64_t y)
232  {
233  x0 = satAdd<T>((int64_t)x0, (int64_t)x);
234  y0 = satAdd<T>((int64_t)y0, (int64_t)y);
235  x1 = satAdd<T>((int64_t)x1, (int64_t)x);
236  y1 = satAdd<T>((int64_t)y1, (int64_t)y);
237  }
238  grkRect<T>& grow(T boundary)
239  {
240  return grow(boundary, boundary, (std::numeric_limits<T>::max)(),
241  (std::numeric_limits<T>::max)());
242  }
243  grkRect<T>& grow(T boundaryx, T boundaryy)
244  {
245  return grow(boundaryx, boundaryy, (std::numeric_limits<T>::max)(),
246  (std::numeric_limits<T>::max)());
247  }
248  grkRect<T>& grow(T boundary, T maxX, T maxY)
249  {
250  return grow(boundary, boundary, maxX, maxY);
251  }
252  grkRect<T>& grow(T boundaryx, T boundaryy, T maxX, T maxY)
253  {
254  return grow(boundaryx, boundaryy, grkRect<T>((T)0, (T)0, maxX, maxY));
255  }
256  grkRect<T>& grow(T boundary, grkRect<T> bounds)
257  {
258  return grow(boundary, boundary, bounds);
259  }
260  grkRect<T>& grow(T boundaryx, T boundaryy, grkRect<T> bounds)
261  {
262  x0 = std::max<T>(satSub<T>(x0, boundaryx), bounds.x0);
263  y0 = std::max<T>(satSub<T>(y0, boundaryy), bounds.y0);
264  x1 = std::min<T>(satAdd<T>(x1, boundaryx), bounds.x1);
265  y1 = std::min<T>(satAdd<T>(y1, boundaryy), bounds.y1);
266 
267  return *this;
268  }
269  T parityX(void)
270  {
271  return T(x0 & 1);
272  }
273  T parityY(void)
274  {
275  return T(y0 & 1);
276  }
277 };
278 
279 using grkRectU32 = grkRect<uint32_t>;
280 
281 } // namespace grk
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: ICacheable.h:20
T satAdd(int64_t lhs, int64_t rhs)
Definition: util.h:70
T clip(int64_t val)
Definition: util.h:59
grkRect< uint32_t > grkRectU32
Definition: util.h:56
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:82
T ceildivpow2(T a, uint32_t b)
Definition: grk_intmath.h:40
Definition: util.h:41
T x1
Definition: util.h:45
grkLine(T _x0, T _x1)
Definition: util.h:43
grkLine()
Definition: util.h:42
T length()
Definition: util.h:46
T x0
Definition: util.h:44
Definition: util.h:31
T x
Definition: util.h:34
grkPoint(T _x, T _y)
Definition: util.h:33
grkPoint()
Definition: util.h:32
T y
Definition: util.h:35
Definition: util.h:95
T x1
Definition: util.h:96
grkRect(void)
Definition: util.h:106
T y0
Definition: util.h:96
T parityY(void)
Definition: util.h:273
grkRect< T > pan(int64_t x, int64_t y) const
Definition: util.h:225
grkRect< T > & grow(T boundaryx, T boundaryy, grkRect< T > bounds)
Definition: util.h:260
bool is_valid(void) const
Definition: util.h:117
grkRect< T > rectceildiv(uint32_t den) const
Definition: util.h:164
void set(grkRect< T > *rhs)
Definition: util.h:151
grkRect< T > rectceildiv(uint32_t denx, uint32_t deny) const
Definition: util.h:168
bool non_empty_intersection(const grkRect< T > *rhs) const
Definition: util.h:191
uint64_t area(void) const
Definition: util.h:205
void print(void) const
Definition: util.h:107
grkRect< T > & operator=(const grkRect< T > &rhs)
Definition: util.h:129
grkLine< T > dimX()
Definition: util.h:217
void set(grkRect< T > rhs)
Definition: util.h:155
grkRect< T > & operator=(const grkRect< T > *rhs)
Definition: util.h:133
bool operator==(const grkRect< T > &rhs) const
Definition: util.h:145
T width() const
Definition: util.h:209
bool contains(grkPoint< T > pt)
Definition: util.h:125
grkRect< T > & grow(T boundary)
Definition: util.h:238
T parityX(void)
Definition: util.h:269
grkRect< T > intersection(const grkRect< T > rhs) const
Definition: util.h:173
grkRect< T > intersection(const grkRect< T > *rhs) const
Definition: util.h:186
grkRect< T > rectceildivpow2(uint32_t power) const
Definition: util.h:159
grkRect< T > & grow(T boundaryx, T boundaryy, T maxX, T maxY)
Definition: util.h:252
bool non_empty(void) const
Definition: util.h:121
grkRect< T > & grow(T boundaryx, T boundaryy)
Definition: util.h:243
bool isContainedIn(const grkRect< T > rhs) const
Definition: util.h:177
void panInplace(int64_t x, int64_t y)
Definition: util.h:231
void clip(const grkRect< T > *rhs)
Definition: util.h:181
grkRect< T > rectUnion(const grkRect< T > *rhs) const
Definition: util.h:196
grkRect(T x0, T y0, T x1, T y1)
Definition: util.h:98
grkRect< T > & grow(T boundary, grkRect< T > bounds)
Definition: util.h:256
grkRect< T > rectUnion(const grkRect< T > &rhs) const
Definition: util.h:201
grkRect< T > & grow(T boundary, T maxX, T maxY)
Definition: util.h:248
std::string boundsString()
Definition: util.h:111
T height() const
Definition: util.h:213
T y1
Definition: util.h:96
grkRect(const grkRect &rhs)
Definition: util.h:99
grkLine< T > dimY()
Definition: util.h:221
T x0
Definition: util.h:96