summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2018-07-28 02:27:53 +1000
committer Vas Crabb <vas@vastheman.com>2018-07-28 02:27:53 +1000
commit29686200984dc74633183583e7391c6fa913b070 (patch)
treede9714f32cc0dae1eeca22e8cc160910d4dfe984 /src/lib/util
parentdbb034ad61477d9072c42ed83c8547d925085beb (diff)
make rectangle work better with constexpr, change many things to use designated getters/setters (nw)
Diffstat (limited to 'src/lib/util')
-rw-r--r--src/lib/util/bitmap.cpp60
-rw-r--r--src/lib/util/bitmap.h50
2 files changed, 55 insertions, 55 deletions
diff --git a/src/lib/util/bitmap.cpp b/src/lib/util/bitmap.cpp
index c8ceb5d3eab..a0a1e92f4d9 100644
--- a/src/lib/util/bitmap.cpp
+++ b/src/lib/util/bitmap.cpp
@@ -129,7 +129,7 @@ bitmap_t::bitmap_t(bitmap_format format, uint8_t bpp, void *base, int width, int
bitmap_t::bitmap_t(bitmap_format format, uint8_t bpp, bitmap_t &source, const rectangle &subrect)
: m_alloc()
, m_allocbytes(0)
- , m_base(source.raw_pixptr(subrect.min_y, subrect.min_x))
+ , m_base(source.raw_pixptr(subrect.top(), subrect.left()))
, m_rowpixels(source.m_rowpixels)
, m_width(subrect.width())
, m_height(subrect.height())
@@ -332,7 +332,7 @@ void bitmap_t::wrap(const bitmap_t &source, const rectangle &subrect)
reset();
// copy relevant fields
- m_base = source.raw_pixptr(subrect.min_y, subrect.min_x);
+ m_base = source.raw_pixptr(subrect.top(), subrect.left());
m_rowpixels = source.m_rowpixels;
m_width = subrect.width();
m_height = subrect.height();
@@ -391,29 +391,29 @@ void bitmap_t::fill(uint32_t color, const rectangle &cliprect)
{
case 8:
// 8bpp always uses memset
- for (int32_t y = fill.min_y; y <= fill.max_y; y++)
- memset(raw_pixptr(y, fill.min_x), (uint8_t)color, fill.width());
+ for (int32_t y = fill.top(); y <= fill.bottom(); y++)
+ memset(raw_pixptr(y, fill.left()), uint8_t(color), fill.width());
break;
case 16:
// 16bpp can use memset if the bytes are equal
- if ((uint8_t)(color >> 8) == (uint8_t)color)
+ if (uint8_t(color >> 8) == uint8_t(color))
{
- for (int32_t y = fill.min_y; y <= fill.max_y; y++)
- memset(raw_pixptr(y, fill.min_x), (uint8_t)color, fill.width() * 2);
+ for (int32_t y = fill.top(); y <= fill.bottom(); y++)
+ memset(raw_pixptr(y, fill.left()), uint8_t(color), fill.width() * 2);
}
else
{
// Fill the first line the hard way
- uint16_t *destrow = &pixt<uint16_t>(fill.min_y);
- for (int32_t x = fill.min_x; x <= fill.max_x; x++)
- destrow[x] = (uint16_t)color;
+ uint16_t *destrow = &pixt<uint16_t>(fill.top());
+ for (int32_t x = fill.left(); x <= fill.right(); x++)
+ destrow[x] = uint16_t(color);
// For the other lines, just copy the first one
- void *destrow0 = &pixt<uint16_t>(fill.min_y, fill.min_x);
- for (int32_t y = fill.min_y + 1; y <= fill.max_y; y++)
+ void *destrow0 = &pixt<uint16_t>(fill.top(), fill.left());
+ for (int32_t y = fill.top() + 1; y <= fill.bottom(); y++)
{
- destrow = &pixt<uint16_t>(y, fill.min_x);
+ destrow = &pixt<uint16_t>(y, fill.left());
memcpy(destrow, destrow0, fill.width() * 2);
}
}
@@ -423,21 +423,21 @@ void bitmap_t::fill(uint32_t color, const rectangle &cliprect)
// 32bpp can use memset if the bytes are equal
if ((uint8_t)(color >> 8) == (uint8_t)color && (uint16_t)(color >> 16) == (uint16_t)color)
{
- for (int32_t y = fill.min_y; y <= fill.max_y; y++)
- memset(&pixt<uint32_t>(y, fill.min_x), (uint8_t)color, fill.width() * 4);
+ for (int32_t y = fill.top(); y <= fill.bottom(); y++)
+ memset(&pixt<uint32_t>(y, fill.left()), uint8_t(color), fill.width() * 4);
}
else
{
// Fill the first line the hard way
- uint32_t *destrow = &pixt<uint32_t>(fill.min_y);
- for (int32_t x = fill.min_x; x <= fill.max_x; x++)
- destrow[x] = (uint32_t)color;
+ uint32_t *destrow = &pixt<uint32_t>(fill.top());
+ for (int32_t x = fill.left(); x <= fill.right(); x++)
+ destrow[x] = uint32_t(color);
// For the other lines, just copy the first one
- uint32_t *destrow0 = &pixt<uint32_t>(fill.min_y, fill.min_x);
- for (int32_t y = fill.min_y + 1; y <= fill.max_y; y++)
+ uint32_t *destrow0 = &pixt<uint32_t>(fill.top(), fill.left());
+ for (int32_t y = fill.top() + 1; y <= fill.bottom(); y++)
{
- destrow = &pixt<uint32_t>(y, fill.min_x);
+ destrow = &pixt<uint32_t>(y, fill.left());
memcpy(destrow, destrow0, fill.width() * 4);
}
}
@@ -445,23 +445,23 @@ void bitmap_t::fill(uint32_t color, const rectangle &cliprect)
case 64:
// 64bpp can use memset if the bytes are equal
- if ((uint8_t)(color >> 8) == (uint8_t)color && (uint16_t)(color >> 16) == (uint16_t)color)
+ if (uint8_t(color >> 8) == uint8_t(color) && uint16_t(color >> 16) == uint16_t(color)) // FIXME: really? wat about the upper bits that would be zeroed when done the "hard way"?
{
- for (int32_t y = fill.min_y; y <= fill.max_y; y++)
- memset(&pixt<uint64_t>(y, fill.min_x), (uint8_t)color, fill.width() * 8);
+ for (int32_t y = fill.top(); y <= fill.bottom(); y++)
+ memset(&pixt<uint64_t>(y, fill.left()), uint8_t(color), fill.width() * 8);
}
else
{
// Fill the first line the hard way
- uint64_t *destrow = &pixt<uint64_t>(fill.min_y);
- for (int32_t x = fill.min_x; x <= fill.max_x; x++)
- destrow[x] = (uint64_t)color;
+ uint64_t *destrow = &pixt<uint64_t>(fill.top());
+ for (int32_t x = fill.left(); x <= fill.right(); x++)
+ destrow[x] = uint64_t(color);
// For the other lines, just copy the first one
- uint64_t *destrow0 = &pixt<uint64_t>(fill.min_y, fill.min_x);
- for (int32_t y = fill.min_y + 1; y <= fill.max_y; y++)
+ uint64_t *destrow0 = &pixt<uint64_t>(fill.top(), fill.left());
+ for (int32_t y = fill.top() + 1; y <= fill.bottom(); y++)
{
- destrow = &pixt<uint64_t>(y, fill.min_x);
+ destrow = &pixt<uint64_t>(y, fill.left());
memcpy(destrow, destrow0, fill.width() * 8);
}
}
diff --git a/src/lib/util/bitmap.h b/src/lib/util/bitmap.h
index 7b0fc34610f..efb18f7e456 100644
--- a/src/lib/util/bitmap.h
+++ b/src/lib/util/bitmap.h
@@ -43,16 +43,16 @@ class rectangle
{
public:
// construction/destruction
- rectangle()
- : min_x(0), max_x(0), min_y(0), max_y(0) { }
- rectangle(int32_t minx, int32_t maxx, int32_t miny, int32_t maxy)
- : min_x(minx), max_x(maxx), min_y(miny), max_y(maxy) { }
+ constexpr rectangle() { }
+ constexpr rectangle(int32_t minx, int32_t maxx, int32_t miny, int32_t maxy)
+ : min_x(minx), max_x(maxx), min_y(miny), max_y(maxy)
+ { }
// getters
- int32_t left() const { return min_x; }
- int32_t right() const { return max_x; }
- int32_t top() const { return min_y; }
- int32_t bottom() const { return max_y; }
+ constexpr int32_t left() const { return min_x; }
+ constexpr int32_t right() const { return max_x; }
+ constexpr int32_t top() const { return min_y; }
+ constexpr int32_t bottom() const { return max_y; }
// compute intersection with another rect
rectangle &operator&=(const rectangle &src)
@@ -75,21 +75,21 @@ public:
}
// comparisons
- bool operator==(const rectangle &rhs) const { return min_x == rhs.min_x && max_x == rhs.max_x && min_y == rhs.min_y && max_y == rhs.max_y; }
- bool operator!=(const rectangle &rhs) const { return min_x != rhs.min_x || max_x != rhs.max_x || min_y != rhs.min_y || max_y != rhs.max_y; }
- bool operator>(const rectangle &rhs) const { return min_x < rhs.min_x && min_y < rhs.min_y && max_x > rhs.max_x && max_y > rhs.max_y; }
- bool operator>=(const rectangle &rhs) const { return min_x <= rhs.min_x && min_y <= rhs.min_y && max_x >= rhs.max_x && max_y >= rhs.max_y; }
- bool operator<(const rectangle &rhs) const { return min_x >= rhs.min_x || min_y >= rhs.min_y || max_x <= rhs.max_x || max_y <= rhs.max_y; }
- bool operator<=(const rectangle &rhs) const { return min_x > rhs.min_x || min_y > rhs.min_y || max_x < rhs.max_x || max_y < rhs.max_y; }
+ constexpr bool operator==(const rectangle &rhs) const { return min_x == rhs.min_x && max_x == rhs.max_x && min_y == rhs.min_y && max_y == rhs.max_y; }
+ constexpr bool operator!=(const rectangle &rhs) const { return min_x != rhs.min_x || max_x != rhs.max_x || min_y != rhs.min_y || max_y != rhs.max_y; }
+ constexpr bool operator>(const rectangle &rhs) const { return min_x < rhs.min_x && min_y < rhs.min_y && max_x > rhs.max_x && max_y > rhs.max_y; }
+ constexpr bool operator>=(const rectangle &rhs) const { return min_x <= rhs.min_x && min_y <= rhs.min_y && max_x >= rhs.max_x && max_y >= rhs.max_y; }
+ constexpr bool operator<(const rectangle &rhs) const { return min_x >= rhs.min_x || min_y >= rhs.min_y || max_x <= rhs.max_x || max_y <= rhs.max_y; }
+ constexpr bool operator<=(const rectangle &rhs) const { return min_x > rhs.min_x || min_y > rhs.min_y || max_x < rhs.max_x || max_y < rhs.max_y; }
// other helpers
- bool empty() const { return (min_x > max_x || min_y > max_y); }
- bool contains(int32_t x, int32_t y) const { return (x >= min_x && x <= max_x && y >= min_y && y <= max_y); }
- bool contains(const rectangle &rect) const { return (min_x <= rect.min_x && max_x >= rect.max_x && min_y <= rect.min_y && max_y >= rect.max_y); }
- int32_t width() const { return max_x + 1 - min_x; }
- int32_t height() const { return max_y + 1 - min_y; }
- int32_t xcenter() const { return (min_x + max_x + 1) / 2; }
- int32_t ycenter() const { return (min_y + max_y + 1) / 2; }
+ constexpr bool empty() const { return (min_x > max_x) || (min_y > max_y); }
+ constexpr bool contains(int32_t x, int32_t y) const { return (x >= min_x) && (x <= max_x) && (y >= min_y) && (y <= max_y); }
+ constexpr bool contains(const rectangle &rect) const { return (min_x <= rect.min_x) && (max_x >= rect.max_x) && (min_y <= rect.min_y) && (max_y >= rect.max_y); }
+ constexpr int32_t width() const { return max_x + 1 - min_x; }
+ constexpr int32_t height() const { return max_y + 1 - min_y; }
+ constexpr int32_t xcenter() const { return (min_x + max_x + 1) / 2; }
+ constexpr int32_t ycenter() const { return (min_y + max_y + 1) / 2; }
// setters
void set(int32_t minx, int32_t maxx, int32_t miny, int32_t maxy) { min_x = minx; max_x = maxx; min_y = miny; max_y = maxy; }
@@ -106,10 +106,10 @@ public:
void offsety(int32_t delta) { min_y += delta; max_y += delta; }
// internal state
- int32_t min_x; // minimum X, or left coordinate
- int32_t max_x; // maximum X, or right coordinate (inclusive)
- int32_t min_y; // minimum Y, or top coordinate
- int32_t max_y; // maximum Y, or bottom coordinate (inclusive)
+ int32_t min_x = 0; // minimum X, or left coordinate
+ int32_t max_x = 0; // maximum X, or right coordinate (inclusive)
+ int32_t min_y = 0; // minimum Y, or top coordinate
+ int32_t max_y = 0; // maximum Y, or bottom coordinate (inclusive)
};