diff --git a/src/sp65/bitmap.c b/src/sp65/bitmap.c index 50846aa99..28757a4aa 100644 --- a/src/sp65/bitmap.c +++ b/src/sp65/bitmap.c @@ -100,6 +100,46 @@ int ValidBitmapSize (unsigned Width, unsigned Height) +Bitmap* SliceBitmap (const Bitmap* O, unsigned OrigX, unsigned OrigY, + unsigned Width, unsigned Height) +/* Create a slice of the given bitmap. The slice starts at position X/Y of + * the original and has the given width and height. Location 0/0 is at the + * upper left corner. + */ +{ + Bitmap* B; + unsigned Y; + + + /* Check the coordinates and size */ + PRECONDITION (OrigX != 0 && OrigY != 0 && + OrigX + Width <= O->Width && + OrigY + Height <= O->Height); + + /* Create a new bitmap with the given size */ + B = NewBitmap (Width, Height); + + /* Copy fields from the original */ + B->Type = O->Type; + if (SB_GetLen (&O->Name) > 0) { + SB_CopyStr (&B->Name, "Slice of "); + SB_Append (&B->Name, &O->Name); + } + B->Pal = DupPalette (O->Pal); + + /* Copy the pixel data */ + for (Y = 0; Y < Height; ++Y) { + memcpy (B->Data + Y * B->Width, + O->Data + (OrigY + Y) * O->Width + OrigX, + Width * sizeof (O->Data[0])); + } + + /* Return the slice */ + return B; +} + + + Color GetPixelColor (const Bitmap* B, unsigned X, unsigned Y) /* Get the color for a given pixel. For indexed bitmaps, the palette entry * is returned. diff --git a/src/sp65/bitmap.h b/src/sp65/bitmap.h index dd0ce7f94..fd658d0fb 100644 --- a/src/sp65/bitmap.h +++ b/src/sp65/bitmap.h @@ -113,6 +113,14 @@ void FreeBitmap (Bitmap* B); int ValidBitmapSize (unsigned Width, unsigned Height); /* Return true if this is a valid size for a bitmap */ +Bitmap* SliceBitmap (const Bitmap* Original, + unsigned X, unsigned Y, + unsigned Width, unsigned Height); +/* Create a slice of the given bitmap. The slice starts at position X/Y of + * the original and has the given width and height. Location 0/0 is at the + * upper left corner. + */ + Color GetPixelColor (const Bitmap* B, unsigned X, unsigned Y); /* Get the color for a given pixel. For indexed bitmaps, the palette entry * is returned. diff --git a/src/sp65/palette.c b/src/sp65/palette.c index f113d9670..4aa4e7036 100644 --- a/src/sp65/palette.c +++ b/src/sp65/palette.c @@ -33,6 +33,8 @@ +#include + /* common */ #include "check.h" #include "xmalloc.h" @@ -68,7 +70,7 @@ Palette* NewPalette (unsigned Entries) Palette* NewMonochromePalette (void) /* Create and return a palette with two entries (black and white) */ -{ +{ /* Create a new palette */ Palette* P = NewPalette (2); @@ -82,6 +84,21 @@ Palette* NewMonochromePalette (void) +Palette* DupPalette (const Palette* P) +/* Create a copy of a palette */ +{ + /* Create a new palette */ + Palette* N = NewPalette (P->Count); + + /* Copy the palette data */ + memcpy (N->Entries, P->Entries, P->Count * sizeof (P->Entries[0])); + + /* Return the copy */ + return N; +} + + + void FreePalette (Palette* P) /* Free a dynamically allocated palette */ { diff --git a/src/sp65/palette.h b/src/sp65/palette.h index 05a10adf9..925e606ee 100644 --- a/src/sp65/palette.h +++ b/src/sp65/palette.h @@ -69,6 +69,9 @@ Palette* NewPalette (unsigned Entries); Palette* NewMonochromePalette (void); /* Create and return a palette with two entries (black and white) */ +Palette* DupPalette (const Palette* P); +/* Create a copy of a palette */ + void FreePalette (Palette* P); /* Free a dynamically allocated palette */