summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/bitmap.c
diff options
context:
space:
mode:
author Aaron Giles <aaron@aarongiles.com>2011-12-31 07:52:26 +0000
committer Aaron Giles <aaron@aarongiles.com>2011-12-31 07:52:26 +0000
commit9cae38e0d82a71a20619c5ba49dce4eaa0bb0cc2 (patch)
tree56135caff0d5610acb3b1fc34b640853fe27b104 /src/lib/util/bitmap.c
parent384b14a64b7e2a732b9b3429fc6d7aa443486c6c (diff)
Converted bitmap_t and rectangle into proper classes. Replaced BITMAP_ADDR*
macros with bitmap->pix* functions, and moved bitmap_fill() to bitmap->fill() among other similar changes. Bitmap fields now only available via accessors. Replaced sect_rect with &= and union_rect with |= operators for rectangle classes. Some general cleanup as a result of these changes. [Aaron Giles]
Diffstat (limited to 'src/lib/util/bitmap.c')
-rw-r--r--src/lib/util/bitmap.c476
1 files changed, 140 insertions, 336 deletions
diff --git a/src/lib/util/bitmap.c b/src/lib/util/bitmap.c
index 860bc5c07ee..cf870dc632c 100644
--- a/src/lib/util/bitmap.c
+++ b/src/lib/util/bitmap.c
@@ -46,420 +46,230 @@
#endif
-/***************************************************************************
- BITMAP ALLOCATION/CONFIGURATION
-***************************************************************************/
+//**************************************************************************
+// BITMAP ALLOCATION/CONFIGURATION
+//**************************************************************************
-#ifdef __cplusplus
-
-/*-------------------------------------------------
- bitmap_t - basic constructor
--------------------------------------------------*/
+//-------------------------------------------------
+// bitmap_t - basic constructor
+//-------------------------------------------------
bitmap_t::bitmap_t()
+ : m_alloc(NULL),
+ m_base(NULL),
+ m_rowpixels(0),
+ m_width(0),
+ m_height(0),
+ m_format(BITMAP_FORMAT_INVALID),
+ m_bpp(0),
+ m_palette(NULL),
+ m_cliprect(0, 0, 0, 0)
{
- /* initialize base fields by hand */
- alloc = NULL;
- base = NULL;
- rowpixels = 0;
- width = 0;
- height = 0;
- format = BITMAP_FORMAT_INVALID;
- bpp = 0;
- palette = NULL;
- cliprect.min_x = cliprect.min_y = 0;
- cliprect.max_x = cliprect.max_y = 0;
}
-bitmap_t::bitmap_t(int _width, int _height, bitmap_format _format, int _xslop, int _yslop)
+//-------------------------------------------------
+// bitmap_t - basic constructor
+//-------------------------------------------------
+
+bitmap_t::bitmap_t(int width, int height, bitmap_format format, int xslop, int yslop)
+ : m_alloc(NULL),
+ m_base(NULL),
+ m_rowpixels((width + 2 * xslop + 7) & ~7),
+ m_width(width),
+ m_height(height),
+ m_format(format),
+ m_bpp(format_to_bpp(format)),
+ m_palette(NULL),
+ m_cliprect(0, width - 1, 0, height - 1)
{
- /* initialize base fields by hand */
- alloc = NULL;
- base = NULL;
- rowpixels = (_width + 2 * _xslop + 7) & ~7;
- width = _width;
- height = _height;
- format = _format;
- bpp = bitmap_format_to_bpp(_format);
- palette = NULL;
- cliprect.min_x = cliprect.min_y = 0;
- cliprect.max_x = width - 1;
- cliprect.max_y = height - 1;
-
- /* fail if invalid format */
- if (bpp == 0)
+ // fail if invalid format
+ if (m_bpp == 0)
throw std::bad_alloc();
- /* allocate memory for the bitmap itself */
- size_t allocbytes = rowpixels * (height + 2 * _yslop) * bpp / 8;
- alloc = malloc(allocbytes);
- if (alloc == NULL)
- throw std::bad_alloc();
+ // allocate memory for the bitmap itself
+ size_t allocbytes = m_rowpixels * (m_height + 2 * yslop) * m_bpp / 8;
+ m_alloc = new UINT8[allocbytes];
- /* clear to 0 by default */
- memset(alloc, 0, allocbytes);
+ // clear to 0 by default
+ memset(m_alloc, 0, allocbytes);
- /* compute the base */
- base = (UINT8 *)alloc + (rowpixels * _yslop + _xslop) * (bpp / 8);
+ // compute the base
+ m_base = m_alloc + (m_rowpixels * yslop + xslop) * (m_bpp / 8);
}
-bitmap_t::bitmap_t(void *_base, int _width, int _height, int _rowpixels, bitmap_format _format)
+//-------------------------------------------------
+// bitmap_t - basic constructor
+//-------------------------------------------------
+
+bitmap_t::bitmap_t(void *base, int width, int height, int rowpixels, bitmap_format format)
+ : m_alloc(NULL),
+ m_base(base),
+ m_rowpixels(rowpixels),
+ m_width(width),
+ m_height(height),
+ m_format(format),
+ m_bpp(format_to_bpp(format)),
+ m_palette(NULL),
+ m_cliprect(0, width - 1, 0, height - 1)
{
- /* initialize base fields by hand */
- alloc = NULL;
- base = _base;
- rowpixels = _rowpixels;
- width = _width;
- height = _height;
- format = _format;
- bpp = bitmap_format_to_bpp(_format);
- palette = NULL;
- cliprect.min_x = cliprect.min_y = 0;
- cliprect.max_x = width - 1;
- cliprect.max_y = height - 1;
-
- /* fail if invalid format */
- if (bpp == 0)
+ // fail if invalid format
+ if (m_bpp == 0)
throw std::bad_alloc();
}
-/*-------------------------------------------------
- ~bitmap_t - basic destructor
--------------------------------------------------*/
+//-------------------------------------------------
+// ~bitmap_t - basic destructor
+//-------------------------------------------------
bitmap_t::~bitmap_t()
{
- /* dereference the palette */
- if (palette != NULL)
- palette_deref(palette);
-
- /* free any allocated memory */
- if (alloc != NULL)
- free(alloc);
-}
-
-
-/*-------------------------------------------------
- bitmap_alloc -- allocate memory for a new
- bitmap of the given format
--------------------------------------------------*/
-
-bitmap_t *bitmap_alloc(int width, int height, bitmap_format format)
-{
- return new bitmap_t(width, height, format);
-}
-
-
-/*-------------------------------------------------
- bitmap_alloc_slop -- allocate a new bitmap with
- additional slop on the borders
--------------------------------------------------*/
-
-bitmap_t *bitmap_alloc_slop(int width, int height, int xslop, int yslop, bitmap_format format)
-{
- return new bitmap_t(width, height, format, xslop, yslop);
-}
-
-
-/*-------------------------------------------------
- bitmap_wrap -- wrap an existing memory buffer
- as a bitmap
--------------------------------------------------*/
-
-bitmap_t *bitmap_wrap(void *base, int width, int height, int rowpixels, bitmap_format format)
-{
- return new bitmap_t(base, width, height, rowpixels, format);
-}
-
-
-/*-------------------------------------------------
- bitmap_free -- release memory allocated for
- a bitmap
--------------------------------------------------*/
-
-void bitmap_free(bitmap_t *bitmap)
-{
- delete bitmap;
-}
-
-#else
-
-/*-------------------------------------------------
- bitmap_alloc -- allocate memory for a new
- bitmap of the given format
--------------------------------------------------*/
-
-bitmap_t *bitmap_alloc(int width, int height, bitmap_format format)
-{
- return bitmap_alloc_slop(width, height, 0, 0, format);
-}
-
-
-/*-------------------------------------------------
- bitmap_alloc_slop -- allocate a new bitmap with
- additional slop on the borders
--------------------------------------------------*/
-
-bitmap_t *bitmap_alloc_slop(int width, int height, int xslop, int yslop, bitmap_format format)
-{
- int bpp = bitmap_format_to_bpp(format);
- size_t allocbytes;
- bitmap_t *bitmap;
- int rowpixels;
-
- /* fail if invalid format */
- if (bpp == 0)
- return NULL;
-
- /* allocate the bitmap itself */
- bitmap = (bitmap_t *)malloc(sizeof(*bitmap));
- if (bitmap == NULL)
- return NULL;
- memset(bitmap, 0, sizeof(*bitmap));
-
- /* round the width to a multiple of 8 and add some padding */
- rowpixels = (width + 2 * xslop + 7) & ~7;
-
- /* allocate memory for the bitmap itself */
- allocbytes = rowpixels * (height + 2 * yslop) * bpp / 8;
- bitmap->alloc = malloc(allocbytes);
- if (bitmap->alloc == NULL)
- {
- free(bitmap);
- return NULL;
- }
+ // dereference the palette
+ if (m_palette != NULL)
+ palette_deref(m_palette);
- /* clear to 0 by default */
- memset(bitmap->alloc, 0, allocbytes);
-
- /* fill in the data */
- bitmap->format = format;
- bitmap->width = width;
- bitmap->height = height;
- bitmap->bpp = bpp;
- bitmap->rowpixels = rowpixels;
- bitmap->base = (UINT8 *)bitmap->alloc + (rowpixels * yslop + xslop) * (bpp / 8);
- bitmap->cliprect.min_x = 0;
- bitmap->cliprect.max_x = width - 1;
- bitmap->cliprect.min_y = 0;
- bitmap->cliprect.max_y = height - 1;
-
- return bitmap;
+ // free any allocated memory
+ delete[] m_alloc;
}
-/*-------------------------------------------------
- bitmap_wrap -- wrap an existing memory buffer
- as a bitmap
--------------------------------------------------*/
+//-------------------------------------------------
+// clone_existing -- clone an existing bitmap by
+// copying its fields; the target bitmap does not
+// own the memory
+//-------------------------------------------------
-bitmap_t *bitmap_wrap(void *base, int width, int height, int rowpixels, bitmap_format format)
+void bitmap_t::clone_existing(const bitmap_t &srcbitmap)
{
- int bpp = bitmap_format_to_bpp(format);
- bitmap_t *bitmap;
-
- /* fail if invalid format */
- if (bpp == 0)
- return NULL;
-
- /* allocate memory */
- bitmap = (bitmap_t *)malloc(sizeof(*bitmap));
- if (bitmap == NULL)
- return NULL;
- memset(bitmap, 0, sizeof(*bitmap));
-
- /* fill in the data */
- bitmap->format = format;
- bitmap->width = width;
- bitmap->height = height;
- bitmap->bpp = bpp;
- bitmap->rowpixels = rowpixels;
- bitmap->base = base;
- bitmap->cliprect.min_x = 0;
- bitmap->cliprect.max_x = width - 1;
- bitmap->cliprect.min_y = 0;
- bitmap->cliprect.max_y = height - 1;
-
- return bitmap;
+ // free any allocated storage
+ delete[] m_alloc;
+ m_alloc = NULL;
+
+ // copy relevant fields
+ m_base = srcbitmap.m_base;
+ m_rowpixels = srcbitmap.m_rowpixels;
+ m_width = srcbitmap.m_width;
+ m_height = srcbitmap.m_height;
+ m_format = srcbitmap.m_format;
+ m_bpp = srcbitmap.m_bpp;
+ m_palette = srcbitmap.m_palette;
+ m_cliprect = srcbitmap.m_cliprect;
}
-/*-------------------------------------------------
- bitmap_free -- release memory allocated for
- a bitmap
--------------------------------------------------*/
+//-------------------------------------------------
+// set_palette -- associate a palette with a
+// bitmap
+//-------------------------------------------------
-void bitmap_free(bitmap_t *bitmap)
+void bitmap_t::set_palette(palette_t *palette)
{
- /* dereference the palette */
- if (bitmap->palette != NULL)
- palette_deref(bitmap->palette);
-
- /* free any allocated memory */
- if (bitmap->alloc != NULL)
- free(bitmap->alloc);
-
- /* free the bitmap */
- free(bitmap);
-}
-
-#endif
-
-
-/*-------------------------------------------------
- bitmap_clone_existing -- clone an existing
- bitmap by copying its fields; the target
- bitmap does not own the memory
--------------------------------------------------*/
-
-void bitmap_clone_existing(bitmap_t *bitmap, const bitmap_t *srcbitmap)
-{
- if (bitmap->alloc != NULL)
- free(bitmap->alloc);
- bitmap->alloc = NULL;
-
- bitmap->base = srcbitmap->base;
- bitmap->rowpixels = srcbitmap->rowpixels;
- bitmap->width = srcbitmap->width;
- bitmap->height = srcbitmap->height;
- bitmap->format = srcbitmap->format;
- bitmap->bpp = srcbitmap->bpp;
- bitmap->palette = srcbitmap->palette;
- bitmap->cliprect = srcbitmap->cliprect;
-}
-
-
-/*-------------------------------------------------
- bitmap_set_palette -- associate a palette with
- a bitmap
--------------------------------------------------*/
-
-void bitmap_set_palette(bitmap_t *bitmap, palette_t *palette)
-{
- /* first dereference any existing palette */
- if (bitmap->palette != NULL)
+ // first dereference any existing palette
+ if (m_palette != NULL)
{
- palette_deref(bitmap->palette);
- bitmap->palette = NULL;
+ palette_deref(m_palette);
+ m_palette = NULL;
}
- /* then reference any new palette */
+ // then reference any new palette
if (palette != NULL)
{
palette_ref(palette);
- bitmap->palette = palette;
+ m_palette = palette;
}
}
+//-------------------------------------------------
+// fill -- fill a bitmap with a solid color
+//-------------------------------------------------
-/***************************************************************************
- BITMAP DRAWING
-***************************************************************************/
-
-/*-------------------------------------------------
- bitmap_fill -- fill a bitmap with a solid
- color
--------------------------------------------------*/
-
-void bitmap_fill(bitmap_t *dest, const rectangle *cliprect, UINT32 color)
+void bitmap_t::fill(UINT32 color, const rectangle &cliprect)
{
- rectangle fill = dest->cliprect;
- int x, y;
-
- /* if we have a cliprect, intersect with that */
- if (cliprect != NULL)
- sect_rect(&fill, cliprect);
-
- /* early out if nothing to do */
- if (fill.min_x > fill.max_x || fill.min_y > fill.max_y)
+ // if we have a cliprect, intersect with that
+ rectangle fill = cliprect;
+ fill &= m_cliprect;
+ if (fill.empty())
return;
- /* based on the bpp go from there */
- switch (dest->bpp)
+ // based on the bpp go from there
+ switch (m_bpp)
{
case 8:
- /* 8bpp always uses memset */
- for (y = fill.min_y; y <= fill.max_y; y++)
- memset(BITMAP_ADDR8(dest, y, fill.min_x), (UINT8)color, fill.max_x + 1 - fill.min_x);
+ // 8bpp always uses memset
+ for (INT32 y = fill.min_y; y <= fill.max_y; y++)
+ memset(&pix8(y, fill.min_x), (UINT8)color, fill.max_x + 1 - fill.min_x);
break;
case 16:
- /* 16bpp can use memset if the bytes are equal */
+ // 16bpp can use memset if the bytes are equal
if ((UINT8)(color >> 8) == (UINT8)color)
{
- for (y = fill.min_y; y <= fill.max_y; y++)
- memset(BITMAP_ADDR16(dest, y, fill.min_x), (UINT8)color, (fill.max_x + 1 - fill.min_x) * 2);
+ for (INT32 y = fill.min_y; y <= fill.max_y; y++)
+ memset(&pix16(y, fill.min_x), (UINT8)color, (fill.max_x + 1 - fill.min_x) * 2);
}
else
{
- UINT16 *destrow, *destrow0;
-
- /* Fill the first line the hard way */
- destrow = BITMAP_ADDR16(dest, fill.min_y, 0);
- for (x = fill.min_x; x <= fill.max_x; x++)
+ // Fill the first line the hard way
+ UINT16 *destrow = &pix16(fill.min_y);
+ for (INT32 x = fill.min_x; x <= fill.max_x; x++)
destrow[x] = (UINT16)color;
- /* For the other lines, just copy the first one */
- destrow0 = BITMAP_ADDR16(dest, fill.min_y, fill.min_x);
- for (y = fill.min_y + 1; y <= fill.max_y; y++)
+ // For the other lines, just copy the first one
+ UINT16 *destrow0 = &pix16(fill.min_y, fill.min_x);
+ for (INT32 y = fill.min_y + 1; y <= fill.max_y; y++)
{
- destrow = BITMAP_ADDR16(dest, y, fill.min_x);
+ destrow = &pix16(y, fill.min_x);
memcpy(destrow, destrow0, (fill.max_x + 1 - fill.min_x) * 2);
}
}
break;
case 32:
- /* 32bpp can use memset if the bytes are equal */
+ // 32bpp can use memset if the bytes are equal
if ((UINT8)(color >> 8) == (UINT8)color && (UINT16)(color >> 16) == (UINT16)color)
{
- for (y = fill.min_y; y <= fill.max_y; y++)
- memset(BITMAP_ADDR32(dest, y, fill.min_x), (UINT8)color, (fill.max_x + 1 - fill.min_x) * 4);
+ for (INT32 y = fill.min_y; y <= fill.max_y; y++)
+ memset(&pix32(y, fill.min_x), (UINT8)color, (fill.max_x + 1 - fill.min_x) * 4);
}
else
{
- UINT32 *destrow, *destrow0;
-
- /* Fill the first line the hard way */
- destrow = BITMAP_ADDR32(dest, fill.min_y, 0);
- for (x = fill.min_x; x <= fill.max_x; x++)
+ // Fill the first line the hard way
+ UINT32 *destrow = &pix32(fill.min_y);
+ for (INT32 x = fill.min_x; x <= fill.max_x; x++)
destrow[x] = (UINT32)color;
- /* For the other lines, just copy the first one */
- destrow0 = BITMAP_ADDR32(dest, fill.min_y, fill.min_x);
- for (y = fill.min_y + 1; y <= fill.max_y; y++)
+ // For the other lines, just copy the first one
+ UINT32 *destrow0 = &pix32(fill.min_y, fill.min_x);
+ for (INT32 y = fill.min_y + 1; y <= fill.max_y; y++)
{
- destrow = BITMAP_ADDR32(dest, y, fill.min_x);
+ destrow = &pix32(y, fill.min_x);
memcpy(destrow, destrow0, (fill.max_x + 1 - fill.min_x) * 4);
}
}
break;
case 64:
- /* 64bpp can use memset if the bytes are equal */
+ // 64bpp can use memset if the bytes are equal
if ((UINT8)(color >> 8) == (UINT8)color && (UINT16)(color >> 16) == (UINT16)color)
{
- for (y = fill.min_y; y <= fill.max_y; y++)
- memset(BITMAP_ADDR64(dest, y, fill.min_x), (UINT8)color, (fill.max_x + 1 - fill.min_x) * 4);
+ for (INT32 y = fill.min_y; y <= fill.max_y; y++)
+ memset(&pix64(y, fill.min_x), (UINT8)color, (fill.max_x + 1 - fill.min_x) * 8);
}
else
{
- UINT64 *destrow, *destrow0;
-
- /* Fill the first line the hard way */
- destrow = BITMAP_ADDR64(dest, fill.min_y, 0);
- for (x = fill.min_x; x <= fill.max_x; x++)
+ // Fill the first line the hard way
+ UINT64 *destrow = &pix64(fill.min_y);
+ for (INT32 x = fill.min_x; x <= fill.max_x; x++)
destrow[x] = (UINT64)color;
- /* For the other lines, just copy the first one */
- destrow0 = BITMAP_ADDR64(dest, fill.min_y, fill.min_x);
- for (y = fill.min_y + 1; y <= fill.max_y; y++)
+ // For the other lines, just copy the first one
+ UINT64 *destrow0 = &pix64(fill.min_y, fill.min_x);
+ for (INT32 y = fill.min_y + 1; y <= fill.max_y; y++)
{
- destrow = BITMAP_ADDR64(dest, y, fill.min_x);
+ destrow = &pix64(y, fill.min_x);
memcpy(destrow, destrow0, (fill.max_x + 1 - fill.min_x) * 4);
}
}
@@ -468,19 +278,13 @@ void bitmap_fill(bitmap_t *dest, const rectangle *cliprect, UINT32 color)
}
+//-------------------------------------------------
+// format_to_bpp - given a format, return the bpp
+//-------------------------------------------------
-/***************************************************************************
- BITMAP UTILITIES
-***************************************************************************/
-
-/*-------------------------------------------------
- bitmap_format_to_bpp - given a format, return
- the bpp
--------------------------------------------------*/
-
-int bitmap_format_to_bpp(bitmap_format format)
+UINT8 bitmap_t::format_to_bpp(bitmap_format format)
{
- /* choose a depth for the format */
+ // choose a depth for the format
switch (format)
{
case BITMAP_FORMAT_INDEXED8: