+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.
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.
+#include <string.h>
+
/* common */
#include "check.h"
#include "xmalloc.h"
Palette* NewMonochromePalette (void)
/* Create and return a palette with two entries (black and white) */
-{
+{
/* Create a new palette */
Palette* P = NewPalette (2);
+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 */
{
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 */