Go to the documentation of this file.
31 return (b && (a > UINT_MAX / b));
34 return (b && (a > UINT64_MAX / b));
51 template<
typename T> T
clip(int64_t val) {
52 static_assert(
sizeof(T) <= 4);
53 if(val < (std::numeric_limits<T>::min)())
54 val = (std::numeric_limits<T>::min)();
55 else if (val > (std::numeric_limits<T>::max)())
56 val = (std::numeric_limits<T>::max)();
60 template<
typename T> T
sat_add(int64_t lhs, int64_t rhs) {
61 return clip<T>(lhs + rhs);
64 template<
typename T> T
sat_add(T lhs, T rhs) {
65 return clip<T>((int64_t)lhs + rhs);
68 template<
typename T> T
sat_sub(T lhs, T rhs) {
69 return clip<T>((int64_t)lhs - rhs);
72 template<
typename T> T
sat_sub(int64_t lhs, int64_t rhs) {
73 return clip<T>(lhs - rhs);
89 std::cout <<
"[" <<
x0 <<
"," <<
y0 <<
"," <<
x1 <<
"," <<
y1 <<
"]"
99 return pt.
x >=
x0 && pt.
y >=
y0 && pt.
x <
x1 && pt.
y <
y1;
152 std::max<T>(
y0,rhs->
y0),
153 std::min<T>(
x1,rhs->
x1),
154 std::min<T>(
y1,rhs->
y1));
159 std::min<T>(
y0,rhs->
y0),
160 std::max<T>(
x1,rhs->
x1),
161 std::max<T>(
y1,rhs->
y1));
167 return (uint64_t)(
x1 -
x0) * (
y1 -
y0);
178 sat_add<T>((int64_t)
y0, (int64_t)y),
179 sat_add<T>((int64_t)
x1, (int64_t)x),
180 sat_add<T>((int64_t)
y1, (int64_t)y));
183 return grow(boundary, boundary,(std::numeric_limits<T>::max)(),(std::numeric_limits<T>::max)());
186 return grow(boundaryx, boundaryy,(std::numeric_limits<T>::max)(),(std::numeric_limits<T>::max)());
189 return grow(boundary, boundary,maxX,maxY);
192 x0 = sat_sub<T>(
x0, boundaryx);
193 y0 = sat_sub<T>(
y0, boundaryy);
194 x1 = sat_add<T>(
x1, boundaryx);
195 y1 = sat_add<T>(
y1, boundaryx);
204 using grk_rect = grk_rectangle<int64_t>;
211 grk_buffer(T *buffer,
size_t off,
size_t length,
bool ownsData) :
buf(buffer),
245 GRK_WARN(
"grk_buf: attempt to increment buffer offset out of bounds");
253 if (
offset < (
size_t)(-off)) {
328 uint64_t data_size_needed = (uint64_t)
stride *
height() *
sizeof(T);
329 if (!data_size_needed)
333 grk::GRK_ERROR(
"Failed to allocate aligned memory buffer of dimensions %u x %u "
338 memset(
data, 0, data_size_needed);
362 void transfer(T** buffer,
bool* owns, uint32_t *strd){
375 if (!inter.non_empty())
378 T* dest =
data + (inter.y0 *
stride + inter.x0);
379 T* src = rhs.
data + ((inter.y0 - rhs.
y0) * rhs.
stride + inter.x0 - rhs.
x0);
380 uint32_t len = inter.width();
381 for (uint32_t j = inter.y0; j < inter.y1; ++j){
382 filter.copy(dest,src, len);
grk_buffer(T *buffer, size_t length, bool ownsData)
Definition: util.h:217
void transfer(T **buffer, bool *owns, uint32_t *strd)
Definition: util.h:362
virtual ~grk_buffer_2d()
Definition: util.h:312
void grk_aligned_free(void *ptr)
Definition: MemManager.cpp:123
grk_buffer(T *buffer, size_t off, size_t length, bool ownsData)
Definition: util.h:211
T height() const
Definition: util.h:172
bool mult_will_overflow(uint32_t a, uint32_t b)
Definition: util.h:30
virtual ~grk_buffer()
Definition: util.h:220
size_t offset
Definition: util.h:271
bool owns_data
Definition: util.h:389
bool isContainedIn(const grk_rectangle< T > rhs) const
Definition: util.h:147
void acquire(T *buffer, uint32_t strd)
Definition: util.h:354
grk_rectangle< T > & grow(T boundaryx, T boundaryy, T maxX, T maxY)
Definition: util.h:191
grk_rectangle(T x0, T y0, T x1, T y1)
Definition: util.h:79
grk_rectangle< T > & grow(T boundaryx, T boundaryy)
Definition: util.h:185
grk_rectangle< int64_t > grk_rect
Definition: util.h:47
grk_rectangle< T > pan(int64_t x, int64_t y) const
Definition: util.h:176
void GRK_WARN(const char *fmt,...)
Definition: logger.cpp:49
grk_buffer_2d(uint32_t w, uint32_t h)
Definition: util.h:287
void attach(T *buffer, uint32_t strd)
Definition: util.h:346
bool alloc(bool clear)
Definition: util.h:325
T ceildivpow2(T a, uint32_t b)
Definition: grk_intmath.h:37
T x1
Definition: util.h:77
bool is_valid(void) const
Definition: util.h:92
grk_buffer_2d & operator=(const grk_buffer_2d &rhs)
Definition: util.h:297
size_t get_remaining_length(void)
Definition: util.h:233
uint32_t ceildiv(T a, T b)
Divide an integer by another integer and round upwards.
Definition: grk_intmath.h:32
bool owns_data
Definition: util.h:273
grk_rectangle(const grk_rectangle &rhs)
Definition: util.h:82
T * curr_ptr()
Definition: util.h:263
grk_rectangle< T > rect_union(const grk_rectangle< T > *rhs) const
Definition: util.h:157
void dealloc()
Definition: util.h:224
uint32_t grk_make_aligned_width(uint32_t width)
Definition: MemManager.cpp:40
grk_point(T _x, T _y)
Definition: util.h:39
grk_rectangle< T > & grow(T boundary)
Definition: util.h:182
T sat_sub(T lhs, T rhs)
Definition: util.h:68
T * buf
Definition: util.h:270
void * grk_aligned_malloc(size_t size)
Allocate memory aligned to a 16 byte boundary.
Definition: MemManager.cpp:119
void copy(grk_buffer_2d &rhs, F filter)
Definition: util.h:373
grk_buffer_2d & operator=(const grk_buffer_2d *rhs)
Definition: util.h:301
grk_buffer_2d(void)
Definition: util.h:294
grk_point()
Definition: util.h:38
uint32_t stride
Definition: util.h:390
void print(void) const
Definition: util.h:88
T width() const
Definition: util.h:169
bool contains(grk_point< T > pt)
Definition: util.h:98
grk_rectangle< T > rect_union(const grk_rectangle< T > &rhs) const
Definition: util.h:163
grk_rectangle< T > intersection(const grk_rectangle< T > *rhs) const
Definition: util.h:150
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: BitIO.cpp:23
size_t len
Definition: util.h:272
uint64_t area(void) const
Definition: util.h:166
T sat_add(int64_t lhs, int64_t rhs)
Definition: util.h:60
grk_rectangle< uint32_t > grk_rect_u32
Definition: util.h:48
void copy_rect(grk_rect_u32 b)
Definition: util.h:318
grk_rectangle< T > intersection(const grk_rectangle< T > rhs) const
Definition: util.h:144
void incr_offset(ptrdiff_t off)
Definition: util.h:237
grk_buffer_2d(uint32_t w, uint32_t strd, uint32_t h)
Definition: util.h:285
grk_rectangle< T > & grow(T boundary, T maxX, T maxY)
Definition: util.h:188
grk_buffer()
Definition: util.h:208
grk_rectangle< T > rectceildiv(uint32_t den) const
Definition: util.h:132
bool operator==(const grk_rectangle< T > &rhs) const
Definition: util.h:112
grk_rectangle< T > rectceildivpow2(uint32_t power) const
Definition: util.h:126
void set_rect(grk_rectangle< T > *rhs)
Definition: util.h:119
void set_rect(grk_rectangle< T > rhs)
Definition: util.h:123
T * data
Definition: util.h:388
grk_rectangle< T > & operator=(const grk_rectangle< T > &rhs)
Definition: util.h:101
T x0
Definition: util.h:77
T y1
Definition: util.h:77
T clip(int64_t val)
Definition: util.h:51
grk_rectangle< T > rectceildiv(uint32_t denx, uint32_t deny) const
Definition: util.h:138
bool mult64_will_overflow(uint64_t a, uint64_t b)
Definition: util.h:33
grk_buffer_2d(T *buffer, bool ownsData, uint32_t w, uint32_t strd, uint32_t h)
Definition: util.h:280
grk_buffer_2d(grk_rect_u32 b)
Definition: util.h:289
const size_t default_align
Definition: MemManager.h:28
T y0
Definition: util.h:77
#define SIZE_MAX
Definition: MemManager.cpp:33
void GRK_ERROR(const char *fmt,...)
Definition: logger.cpp:57
bool non_empty(void) const
Definition: util.h:95
grk_rectangle(void)
Definition: util.h:85