summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/bitmap.cpp
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2022-08-23 05:01:26 +1000
committer Vas Crabb <vas@vastheman.com>2022-08-23 05:01:26 +1000
commit53d32b27d14738140e1b3488c86819385c3c8f71 (patch)
tree8cef00323db75b1619a37e97e048cee2febce09b /src/lib/util/bitmap.cpp
parent7bd9db575b27db74e8bee8bd3ed4517b0e21b414 (diff)
Lua scripting enhancements:
Added minimal support for manipulating bitmaps and drawing them in render containers. Y'CbCr 4:2:2, RGB and ARGB are supported. Argument order doesn't always match the underlying classes to make the Lua interface more consistent with render bounds and render containers. Added bindings for device_palette_interface. Fixed some errors in the documentation as well as documenting new functionality.
Diffstat (limited to 'src/lib/util/bitmap.cpp')
-rw-r--r--src/lib/util/bitmap.cpp54
1 files changed, 31 insertions, 23 deletions
diff --git a/src/lib/util/bitmap.cpp b/src/lib/util/bitmap.cpp
index 8757d928fff..5f4d2adfacb 100644
--- a/src/lib/util/bitmap.cpp
+++ b/src/lib/util/bitmap.cpp
@@ -36,7 +36,7 @@ inline int32_t bitmap_t::compute_rowpixels(int width, int xslop)
inline void bitmap_t::compute_base(int xslop, int yslop)
{
- m_base = m_alloc.get() + (m_rowpixels * yslop + xslop) * (m_bpp / 8);
+ m_base = &m_alloc[(m_rowpixels * yslop + xslop) * (m_bpp / 8)];
}
@@ -234,24 +234,28 @@ void bitmap_t::allocate(int width, int height, int xslop, int yslop)
reset();
// handle empty requests cleanly
- if (width <= 0 || height <= 0)
- return;
-
- // initialize fields
- m_rowpixels = compute_rowpixels(width, xslop);
- m_width = width;
- m_height = height;
- m_cliprect.set(0, width - 1, 0, height - 1);
-
- // allocate memory for the bitmap itself
- m_allocbytes = m_rowpixels * (m_height + 2 * yslop) * m_bpp / 8;
- m_alloc.reset(new uint8_t[m_allocbytes]);
-
- // clear to 0 by default
- memset(m_alloc.get(), 0, m_allocbytes);
-
- // compute the base
- compute_base(xslop, yslop);
+ if ((0 < width) && (0 < height))
+ {
+ // allocate memory for the bitmap itself
+ int32_t const new_rowpixels = compute_rowpixels(width, xslop);
+ uint32_t const new_allocbytes = new_rowpixels * (height + 2 * yslop) * m_bpp / 8;
+ m_alloc.reset(new (std::nothrow) uint8_t[new_allocbytes]);
+ if (m_alloc)
+ {
+ // initialize fields
+ m_allocbytes = new_allocbytes;
+ m_rowpixels = new_rowpixels;
+ m_width = width;
+ m_height = height;
+ m_cliprect.set(0, width - 1, 0, height - 1);
+
+ // clear to 0 by default
+ memset(m_alloc.get(), 0, m_allocbytes);
+
+ // compute the base
+ compute_base(xslop, yslop);
+ }
+ }
}
/**
@@ -274,12 +278,12 @@ void bitmap_t::resize(int width, int height, int xslop, int yslop)
assert(m_bpp == 8 || m_bpp == 16 || m_bpp == 32 || m_bpp == 64);
// handle empty requests cleanly
- if (width <= 0 || height <= 0)
+ if ((width <= 0) || (height <= 0))
width = height = 0;
// determine how much memory we need for the new bitmap
- int new_rowpixels = compute_rowpixels(width, xslop);
- uint32_t new_allocbytes = new_rowpixels * (height + 2 * yslop) * m_bpp / 8;
+ int32_t const new_rowpixels = compute_rowpixels(width, xslop);
+ uint32_t const new_allocbytes = new_rowpixels * (height + 2 * yslop) * m_bpp / 8;
if (new_allocbytes > m_allocbytes)
{
@@ -290,7 +294,6 @@ void bitmap_t::resize(int width, int height, int xslop, int yslop)
}
else
{
-
// otherwise, reconfigure
m_rowpixels = new_rowpixels;
m_width = width;
@@ -318,6 +321,7 @@ void bitmap_t::reset()
m_base = nullptr;
// reset all fields
+ m_allocbytes = 0;
m_rowpixels = 0;
m_width = 0;
m_height = 0;
@@ -339,6 +343,9 @@ void bitmap_t::reset()
void bitmap_t::wrap(void *base, int width, int height, int rowpixels)
{
+ assert(base || (!width && !height));
+ assert(!m_alloc || (&m_alloc[0] > base) || (&m_alloc[m_allocbytes] <= base));
+
// delete any existing stuff
reset();
@@ -364,6 +371,7 @@ void bitmap_t::wrap(void *base, int width, int height, int rowpixels)
void bitmap_t::wrap(bitmap_t &source, const rectangle &subrect)
{
+ assert(&source != this);
assert(m_format == source.m_format);
assert(m_bpp == source.m_bpp);
assert(source.cliprect().contains(subrect));