]> git.sur5r.net Git - cc65/commitdiff
New function to create a bitmap slice.
authoruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 26 Feb 2012 21:45:42 +0000 (21:45 +0000)
committeruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 26 Feb 2012 21:45:42 +0000 (21:45 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@5557 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/sp65/bitmap.c
src/sp65/bitmap.h
src/sp65/palette.c
src/sp65/palette.h

index 50846aa9928a176c8ae6876afb11380722dc2919..28757a4aa003fab50c042d8379307ef81ef4b451 100644 (file)
@@ -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.
index dd0ce7f94782e5a6b2a2974079be332335cdd2d1..fd658d0fbd6acd11bee2638f0dbfef7a82c978d3 100644 (file)
@@ -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.
index f113d967037a2c022bbb808749a788b664a447a5..4aa4e7036b575659a74d2c6e20eb99ff236d5111 100644 (file)
@@ -33,6 +33,8 @@
 
 
 
+#include <string.h>
+
 /* 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 */
 {
index 05a10adf999d1ba89c35b024587739b8d29cfb70..925e606eeb760f59935f917f7573f938f2b22aba 100644 (file)
@@ -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 */