31 return (b && (a > UINT64_MAX / b));
58 template<
typename T> T
clip(int64_t val) {
59 static_assert(
sizeof(T) <= 4);
60 if(val < (std::numeric_limits<T>::min)())
61 val = (std::numeric_limits<T>::min)();
62 else if (val > (std::numeric_limits<T>::max)())
63 val = (std::numeric_limits<T>::max)();
67 template<
typename T> T
sat_add(int64_t lhs, int64_t rhs) {
68 return clip<T>(lhs + rhs);
71 template<
typename T> T
sat_add(T lhs, T rhs) {
72 return clip<T>((int64_t)lhs + rhs);
75 template<
typename T> T
sat_sub(T lhs, T rhs) {
76 return clip<T>((int64_t)lhs - rhs);
79 template<
typename T> T
sat_sub(int64_t lhs, int64_t rhs) {
80 return clip<T>(lhs - rhs);
96 std::cout <<
"[" <<
x0 <<
"," <<
y0 <<
"," <<
x1 <<
"," <<
y1 <<
"]"
106 return pt.
x >=
x0 && pt.
y >=
y0 && pt.
x <
x1 && pt.
y <
y1;
159 std::max<T>(
y0,rhs->
y0),
160 std::min<T>(
x1,rhs->
x1),
161 std::min<T>(
y1,rhs->
y1));
166 std::max<T>(
y0,rhs->
y0),
167 std::min<T>(
x1,rhs->
x1),
168 std::min<T>(
y1,rhs->
y1));
173 std::min<T>(
y0,rhs->
y0),
174 std::max<T>(
x1,rhs->
x1),
175 std::max<T>(
y1,rhs->
y1));
181 return (uint64_t)(
x1 -
x0) * (
y1 -
y0);
201 x0 = sat_add<T>((int64_t)
x0, (int64_t)x);
202 y0 = sat_add<T>((int64_t)
y0, (int64_t)y);
203 x1 = sat_add<T>((int64_t)
x1, (int64_t)x);
204 y1 = sat_add<T>((int64_t)
y1, (int64_t)y);
207 return grow(boundary, boundary,(std::numeric_limits<T>::max)(),(std::numeric_limits<T>::max)());
210 return grow(boundaryx, boundaryy,(std::numeric_limits<T>::max)(),(std::numeric_limits<T>::max)());
213 return grow(boundary, boundary,maxX,maxY);
219 return grow(boundary,boundary, bounds);
222 x0 = std::max<T>( sat_sub<T>(
x0, boundaryx), bounds.
x0);
223 y0 = std::max<T>( sat_sub<T>(
y0, boundaryy), bounds.
y0);
224 x1 = std::min<T>( sat_add<T>(
x1, boundaryx), bounds.
x1);
225 y1 = std::min<T>( sat_add<T>(
y1, boundaryy), bounds.
y1);
231 using grk_rect = grk_rectangle<int64_t>;
235 grk_buffer(T *buffer,
size_t off,
size_t length,
bool ownsData) :
buf(buffer),
280 GRK_WARN(
"grk_buf: attempt to increment buffer offset out of bounds");
288 if (
offset < (
size_t)(-off)) {
357 uint64_t data_size_needed = (uint64_t)
stride *
height() *
sizeof(T);
358 if (!data_size_needed)
362 grk::GRK_ERROR(
"Failed to allocate aligned memory buffer of dimensions %u x %u "
367 memset(
data, 0, data_size_needed);
391 void transfer(T** buffer,
bool* owns, uint32_t *strd){
401 bool copy_data(T* dest, uint32_t dest_w, uint32_t dest_h, uint32_t dest_stride)
const{
402 assert(dest_w <=
width());
403 assert(dest_h <=
height());
404 assert(dest_stride <=
stride);
410 auto dest_ptr = dest;
411 for (uint32_t j = 0; j < dest_h; ++j) {
412 memcpy(dest_ptr,src_ptr, dest_w*
sizeof(T));
413 dest_ptr += dest_stride;
422 if (!inter.non_empty())
425 T* dest =
data + (inter.y0 *
stride + inter.x0);
426 T* src = rhs.
data + ((inter.y0 - rhs.
y0) * rhs.
stride + inter.x0 - rhs.
x0);
427 uint32_t len = inter.width();
428 for (uint32_t j = inter.y0; j < inter.y1; ++j){
429 filter.copy(dest,src, len);
#define SIZE_MAX
Definition: MemManager.cpp:33
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: BitIO.cpp:23
grk_rectangle< int64_t > grk_rect
Definition: util.h:55
T clip(int64_t val)
Definition: util.h:58
grk_rect_u32 getTileCompBandWindow(uint8_t num_res, uint8_t resno, uint8_t orientation, grk_rect_u32 unreducedTileCompWindow)
Get band window in tile component coordinates for specified resolution and band orientation.
Definition: util.cpp:32
bool mult_will_overflow(uint64_t a, uint64_t b)
Definition: util.h:30
uint32_t grk_make_aligned_width(uint32_t width)
Definition: MemManager.cpp:40
T sat_sub(T lhs, T rhs)
Definition: util.h:75
uint32_t ceildiv(T a, T b)
Divide an integer by another integer and round upwards.
Definition: grk_intmath.h:32
void GRK_ERROR(const char *fmt,...)
Definition: logger.cpp:57
const size_t default_align
Definition: MemManager.h:34
void GRK_WARN(const char *fmt,...)
Definition: logger.cpp:49
void * grk_aligned_malloc(size_t size)
Allocate memory aligned to a 16 byte boundary.
Definition: MemManager.cpp:119
T sat_add(int64_t lhs, int64_t rhs)
Definition: util.h:67
grk_rectangle< uint32_t > grk_rect_u32
Definition: util.h:56
void grk_aligned_free(void *ptr)
Definition: MemManager.cpp:123
T ceildivpow2(T a, uint32_t b)
Definition: grk_intmath.h:37
bool copy_data(T *dest, uint32_t dest_w, uint32_t dest_h, uint32_t dest_stride) const
Definition: util.h:401
grk_buffer_2d & operator=(const grk_buffer_2d &rhs)
Definition: util.h:330
bool alloc(bool clear)
Definition: util.h:354
void attach(T *buffer, uint32_t strd)
Definition: util.h:375
void copy_rect(grk_rect_u32 b)
Definition: util.h:348
bool owns_data
Definition: util.h:436
T * data
Definition: util.h:435
void copy(grk_buffer_2d &rhs, F filter)
Definition: util.h:420
grk_buffer_2d(T *buffer, bool ownsData, uint32_t w, uint32_t strd, uint32_t h)
Definition: util.h:313
void transfer(T **buffer, bool *owns, uint32_t *strd)
Definition: util.h:391
grk_buffer_2d(uint32_t w, uint32_t strd, uint32_t h)
Definition: util.h:318
virtual ~grk_buffer_2d()
Definition: util.h:344
uint32_t stride
Definition: util.h:437
grk_buffer_2d(grk_rect_u32 b)
Definition: util.h:322
grk_buffer_2d(uint32_t w, uint32_t h)
Definition: util.h:320
grk_buffer_2d(void)
Definition: util.h:327
grk_buffer_2d & operator=(const grk_buffer_2d *rhs)
Definition: util.h:334
void acquire(T *buffer, uint32_t strd)
Definition: util.h:383
virtual ~grk_buffer()
Definition: util.h:247
void alloc(size_t length)
Definition: util.h:251
bool owns_data
Definition: util.h:307
size_t offset
Definition: util.h:305
virtual void dealloc()
Definition: util.h:259
size_t len
Definition: util.h:306
T * buf
Definition: util.h:304
grk_buffer(T *buffer, size_t length, bool ownsData)
Definition: util.h:244
size_t get_remaining_length(void)
Definition: util.h:268
grk_buffer(T *buffer, size_t off, size_t length, bool ownsData)
Definition: util.h:235
void incr_offset(ptrdiff_t off)
Definition: util.h:272
grk_buffer()
Definition: util.h:241
T * curr_ptr()
Definition: util.h:298
T x0
Definition: util.h:45
grk_line(T _x0, T _x1)
Definition: util.h:44
grk_line()
Definition: util.h:43
T x1
Definition: util.h:46
T length()
Definition: util.h:47
grk_point()
Definition: util.h:35
grk_point(T _x, T _y)
Definition: util.h:36
grk_line< T > dimY()
Definition: util.h:192
grk_rectangle< T > & grow(T boundaryx, T boundaryy)
Definition: util.h:209
grk_rectangle< T > rect_union(const grk_rectangle< T > &rhs) const
Definition: util.h:177
void print(void) const
Definition: util.h:95
T x0
Definition: util.h:84
grk_rectangle< T > rect_union(const grk_rectangle< T > *rhs) const
Definition: util.h:171
grk_rectangle< T > & grow(T boundaryx, T boundaryy, T maxX, T maxY)
Definition: util.h:215
void set_rect(grk_rectangle< T > rhs)
Definition: util.h:130
T y1
Definition: util.h:84
bool contains(grk_point< T > pt)
Definition: util.h:105
bool isContainedIn(const grk_rectangle< T > rhs) const
Definition: util.h:154
grk_rectangle(void)
Definition: util.h:92
grk_rectangle< T > & operator=(const grk_rectangle< T > &rhs)
Definition: util.h:108
grk_rectangle< T > & grow(T boundary, T maxX, T maxY)
Definition: util.h:212
grk_rectangle< T > pan(int64_t x, int64_t y) const
Definition: util.h:195
grk_rectangle< T > & grow(T boundary, grk_rectangle< T > bounds)
Definition: util.h:218
bool non_empty(void) const
Definition: util.h:102
grk_rectangle< T > & grow(T boundaryx, T boundaryy, grk_rectangle< T > bounds)
Definition: util.h:221
T y0
Definition: util.h:84
bool operator==(const grk_rectangle< T > &rhs) const
Definition: util.h:119
bool is_valid(void) const
Definition: util.h:99
uint64_t area(void) const
Definition: util.h:180
grk_rectangle< T > rectceildivpow2(uint32_t power) const
Definition: util.h:133
grk_rectangle< T > intersection(const grk_rectangle< T > rhs) const
Definition: util.h:151
grk_rectangle(const grk_rectangle &rhs)
Definition: util.h:89
T height() const
Definition: util.h:186
grk_rectangle< T > rectceildiv(uint32_t den) const
Definition: util.h:139
grk_rectangle< T > rectceildiv(uint32_t denx, uint32_t deny) const
Definition: util.h:145
T width() const
Definition: util.h:183
grk_line< T > dimX()
Definition: util.h:189
grk_rectangle< T > & grow(T boundary)
Definition: util.h:206
grk_rectangle< T > intersection(const grk_rectangle< T > *rhs) const
Definition: util.h:164
T x1
Definition: util.h:84
void clip(const grk_rectangle< T > *rhs)
Definition: util.h:157
void set_rect(grk_rectangle< T > *rhs)
Definition: util.h:126
grk_rectangle(T x0, T y0, T x1, T y1)
Definition: util.h:86
void pan_inplace(int64_t x, int64_t y)
Definition: util.h:200