40 #define DECLARE_COMPUTE_TYPE(StorageType, ComputeType) \
42 namespace _internal { \
43 template<> class _GetComputeType < StorageType > { \
45 typedef ComputeType Type; \
76 #undef DECLARE_COMPUTE_TYPE
169 template<
typename Storage,
184 typedef shared_ptr<Map2D>
Ref;
218 format(
"Index out of bounds: (%d, %d), w = %d, h = %d",
237 return data[x + y *
w];
251 Compute
bicubic(
const Compute* ctrl,
double s)
const {
256 static const double B[4][4] = {
257 { 0.0, -0.5, 1.0, -0.5},
258 { 1.0, 0.0, -2.5, 1.5},
259 { 0.0, 0.5, 2.0, -1.5},
260 { 0.0, 0.0, -0.5, 0.5}};
272 for (
int c = 0; c < 4; ++c) {
274 for (
int power = 0; power < 4; ++power) {
275 coeff += B[c][power] * S[power];
277 sum += ctrl[c] * coeff;
285 ZERO = Storage(
Compute(Storage()) * 0);
305 if ((newW != w) || (newH != h)) {
319 return m_changed.
value() != 0;
324 m_changed = c ? 1 : 0;
351 return (((uint32)x < w) && ((uint32)y < h));
355 inline bool inBounds(
const Vector2int16& v)
const {
368 if (((uint32)x < w) && ((
uint32)y < h)) {
369 return data[x + y *
w];
373 return const_cast<Type*
>(
this)->
slowGet(x, y,
wrap);
382 inline const Storage&
get(
int x,
int y)
const {
386 inline const Storage&
get(
const Vector2int16& p)
const {
390 inline const Storage&
get(
const Vector2int16& p,
WrapMode wrap)
const {
391 return get(p.x, p.y,
wrap);
395 return const_cast<Storage&
>(
const_cast<const Type*
>(
this)->
get(x, y,
wrap));
403 inline Storage&
get(
int x,
int y) {
404 return const_cast<Storage&
>(
const_cast<const Type*
>(
this)->
get(x, y));
412 inline Storage&
get(
const Vector2int16& p) {
413 return get(p.x, p.y);
417 inline void set(
const Vector2int16& p,
const Storage& v) {
424 if (((uint32)x < w) && ((uint32)y < h)) {
428 const_cast<Storage&
>(
slowGet(x, y, wrap)) = v;
432 void set(
int x,
int y,
const Storage& v) {
433 set(x, y, v, _wrapMode);
438 for(
int i = 0; i < data.
size(); ++i) {
451 for (
int i = 0; i < N; ++i) {
465 int halfHeight = h/2;
467 for (
int y = 0; y < halfHeight; ++
y) {
469 int o2 = (h - y - 1) * w;
470 for (
int x = 0; x < (int)w; ++
x) {
473 Storage temp = d[i1];
482 int halfWidth = w / 2;
484 for (
int x = 0; x < halfWidth; ++
x) {
485 for (
int y = 0; y < (int)h; ++
y) {
487 int i2 = y * w + (w - x - 1);
488 Storage temp = d[i1];
499 virtual void crop(
int newX,
int newY,
int newW,
int newH) {
500 alwaysAssertM(newX + newW <= (
int)w,
"Cannot grow when cropping");
501 alwaysAssertM(newY + newH <= (
int)h,
"Cannot grow when cropping");
502 alwaysAssertM(newX >= 0 && newY >= 0,
"Origin out of bounds.");
507 for (
int y = 0; y < newH; ++
y) {
508 for (
int x = 0; x < newW; ++
x) {
509 data[x + y * newW] = data[(x + newX) + (y + newY) *
w];
527 return Compute(
get(ix, iy, wrap));
530 inline Compute
nearest(
float x,
float y)
const {
531 return nearest(x, y, _wrapMode);
534 inline Compute
nearest(
const Vector2& p)
const {
540 if ((w == 0) || (h == 0)) {
546 Compute rowSum =
ZERO;
547 for (
unsigned int y = 0; y <
h; ++
y) {
550 for (
unsigned int x = 0; x <
w; ++
x) {
551 sum +=
Compute(data[offset + x]);
553 rowSum += sum * (1.0f /
w);
556 return rowSum * (1.0f /
h);
570 const float fX = x - i;
571 const float fY = y - j;
574 const Compute& t0 =
get(i, j,
wrap);
575 const Compute& t1 =
get(i + 1, j,
wrap);
578 const Compute& t2 =
get(i, j + 1,
wrap);
579 const Compute& t3 =
get(i + 1, j + 1,
wrap);
581 const Compute& A =
lerp(t0, t1, fX);
582 const Compute& B =
lerp(t2, t3, fX);
585 return lerp(A, B, fY);
611 for (
int v = 0; v < 4; ++v) {
615 for (
int u = 0; u < 4; ++u) {
616 hsample[u] =
Compute(
get(i + u - 1, j + v - 1, wrap));
619 vsample[v] =
bicubic(hsample, fX);
627 return bicubic(x, y, _wrapMode);
634 inline Compute
bicubic(
const Vector2& p)
const {
662 return data.
size() *
sizeof(Storage) +
sizeof(*
this);
680 #endif // G3D_IMAGE_H
int32 height() const
Definition: Map2D.h:645
Map2D< Storage, Compute > Type
Definition: Map2D.h:183
Compute nearest(float x, float y, WrapMode wrap) const
Definition: Map2D.h:524
Storage ZERO
Definition: Map2D.h:188
void set(int x, int y, const Storage &v, WrapMode wrap)
Definition: Map2D.h:422
int8_t int8
Definition: Define.h:148
Compute bicubic(float x, float y, WrapMode wrap) const
Definition: Map2D.h:604
bool changed()
Definition: Map2D.h:318
size_t sizeInMemory() const
Definition: Map2D.h:661
Rect2D rect2DBounds() const
Definition: Map2D.h:656
void resize(size_t n, bool shrinkIfNecessary=true)
Definition: Array.h:490
float y0() const
Definition: Rect2D.h:184
Compute average() const
Definition: Map2D.h:539
Array< Storage > & getArray()
Definition: Map2D.h:340
int64_t int64
Definition: Define.h:145
#define DECLARE_COMPUTE_TYPE(StorageType, ComputeType)
Definition: Map2D.h:40
Definition: WrapMode.h:60
Vector2int16
Definition: Vector2int16.h:28
Vector2int16 size() const
Definition: Map2D.h:651
Mutual exclusion lock used for synchronization.
Definition: GMutex.h:76
Definition: WrapMode.h:61
Array< Storage > data
Definition: Map2D.h:202
double float64
Definition: g3dmath.h:173
int iRound(double fValue)
Definition: g3dmath.h:226
const Storage * getCArray() const
Definition: Map2D.h:334
T * getCArray()
Definition: Array.h:256
void maybeFlipVertical(bool flip)
Definition: Map2D.h:458
Definition: AtomicInt32.h:29
Definition: WrapMode.h:63
int iFloor(double fValue)
Definition: g3dmath.h:603
RET_TYPE Compute(float x, float y, float center_offset, float size)
Definition: GridDefines.h:177
virtual void crop(const Rect2D &rect)
Definition: Map2D.h:517
Storage Type
Definition: Map2D.h:32
Compute bilinear(float x, float y) const
Definition: Map2D.h:588
Compute bicubic(float x, float y) const
Definition: Map2D.h:626
GMutex mutex
Definition: Map2D.h:296
static Ref create(int w=0, int h=0, WrapMode wrap=WrapMode::ERROR)
Definition: Map2D.h:298
Compute bilinear(const Vector2 &p, WrapMode wrap) const
Definition: Map2D.h:596
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
uint32 h
Definition: Map2D.h:194
void setAll(const Storage &v)
Definition: Map2D.h:437
float y
Definition: Vector2.h:50
float y1() const
Definition: Rect2D.h:188
Definition: ReferenceCount.h:24
const Storage & slowGet(int x, int y, WrapMode wrap)
Definition: Map2D.h:205
int32 width() const
Definition: Map2D.h:639
const Storage & fastGet(int x, int y) const
Definition: Map2D.h:235
Definition: WrapMode.h:59
shared_ptr< Map2D > Ref
Definition: Map2D.h:184
virtual void flipHorizontal()
Definition: Map2D.h:481
Definition: WrapMode.h:62
#define debugAssert(exp)
Definition: debugAssert.h:160
uint32 w
Definition: Map2D.h:191
float float32
Definition: g3dmath.h:172
WrapMode _wrapMode
Definition: Map2D.h:196
void set(int x, int y, const Storage &v)
Definition: Map2D.h:432
int32_t int32
Definition: Define.h:146
void fastSet(int x, int y, const Storage &v)
Definition: Map2D.h:241
Storage StorageType
Definition: Map2D.h:181
uint32_t uint32
Definition: Define.h:150
uint64_t uint64
Definition: Define.h:149
T * data
Definition: Array.h:103
G3D::int16 y
Definition: Vector2int16.h:38
uint16_t uint16
Definition: Define.h:151
float x0() const
Definition: Rect2D.h:176
float x
Definition: Vector2.h:49
Storage * getCArray()
Definition: Map2D.h:329
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
bool inBounds(const Vector2int16 &v) const
Definition: Map2D.h:355
int size() const
Definition: Array.h:430
Compute bicubic(const Vector2 &p, WrapMode wrap) const
Definition: Map2D.h:630
Map2D(int w, int h, WrapMode wrap)
Definition: Map2D.h:284
int32 value() const
Definition: AtomicInt32.h:67
void set(const shared_ptr< Map2D< Storage, T > > &src)
Definition: Map2D.h:446
void setWrapMode(WrapMode m)
Definition: Map2D.h:671
AtomicInt32 m_changed
Definition: Map2D.h:200
void setChanged(bool c)
Definition: Map2D.h:323
int iWrap(int val, int hi)
Definition: g3dmath.h:594
Compute ComputeType
Definition: Map2D.h:182
const Array< Storage > & getArray() const
Definition: Map2D.h:345
WrapMode wrapMode() const
Definition: Map2D.h:666
void set(const Vector2int16 &p, const Storage &v)
Definition: Map2D.h:417
Compute bicubic(const Compute *ctrl, double s) const
Definition: Map2D.h:251
uint8_t uint8
Definition: Define.h:152
Compute bilinear(float x, float y, WrapMode wrap) const
Definition: Map2D.h:566
double lerp(double a, double b, double f)
Definition: g3dmath.h:189
int16_t int16
Definition: Define.h:147
Compute bicubic(const Vector2 &p) const
Definition: Map2D.h:634
static Rect2D xywh(float x, float y, float w, float h)
Definition: Rect2D.h:260
uint32_t uint32
Definition: g3dmath.h:168
G3D::int16 x
Definition: Vector2int16.h:37
bool inBounds(int x, int y) const
Definition: Map2D.h:350
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165
int iClamp(int val, int low, int hi)
Definition: g3dmath.h:545
void resize(uint32 newW, uint32 newH)
Definition: Map2D.h:304
Compute nearest(const Vector2 &p) const
Definition: Map2D.h:534
Compute bilinear(const Vector2 &p) const
Definition: Map2D.h:592
Definition: Vector2int16.h:22
float x1() const
Definition: Rect2D.h:180
virtual void crop(int newX, int newY, int newW, int newH)
Definition: Map2D.h:499
virtual void flipVertical()
Definition: Map2D.h:464
Definition: WrapMode.h:55
Compute nearest(float x, float y) const
Definition: Map2D.h:530