1 /*****************************************************************************/
5 /* Bitmap definition for the sp65 sprite and bitmap utility */
9 /* (C) 2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
46 /*****************************************************************************/
48 /*****************************************************************************/
52 Bitmap* NewBitmap (unsigned Width, unsigned Height)
53 /* Create a new bitmap. The type is set to unknown and the palette to NULL */
57 /* Calculate the size of the bitmap in pixels */
58 unsigned long Size = (unsigned long) Width * Height;
60 /* Some safety checks */
61 PRECONDITION (Size > 0 && Size <= BM_MAX_SIZE);
64 B = xmalloc (sizeof (*B) + (Size - 1) * sizeof (B->Data[0]));
66 /* Initialize the data */
68 B->Name = EmptyStrBuf;
73 /* Return the bitmap */
79 void FreeBitmap (Bitmap* B)
80 /* Free a dynamically allocated bitmap */
82 /* Alloc NULL pointers */
84 /* Free name, palette and then the bitmap */
93 int ValidBitmapSize (unsigned Width, unsigned Height)
94 /* Return true if this is a valid size for a bitmap */
96 /* Calculate the size of the bitmap in pixels */
97 unsigned long Size = (unsigned long) Width * Height;
100 return (Size > 0 && Size <= BM_MAX_SIZE);
105 Bitmap* SliceBitmap (const Bitmap* O, unsigned OrigX, unsigned OrigY,
106 unsigned Width, unsigned Height)
107 /* Create a slice of the given bitmap. The slice starts at position X/Y of
108 * the original and has the given width and height. Location 0/0 is at the
116 /* Check the coordinates and size */
117 PRECONDITION (OrigX + Width <= O->Width && OrigY + Height <= O->Height);
119 /* Create a new bitmap with the given size */
120 B = NewBitmap (Width, Height);
122 /* Copy fields from the original */
124 if (SB_GetLen (&O->Name) > 0) {
125 SB_CopyStr (&B->Name, "Slice of ");
126 SB_Append (&B->Name, &O->Name);
128 B->Pal = DupPalette (O->Pal);
130 /* Copy the pixel data */
131 for (Y = 0; Y < Height; ++Y) {
132 memcpy (B->Data + Y * B->Width,
133 O->Data + (OrigY + Y) * O->Width + OrigX,
134 Width * sizeof (O->Data[0]));
137 /* Return the slice */
143 Color GetPixelColor (const Bitmap* B, unsigned X, unsigned Y)
144 /* Get the color for a given pixel. For indexed bitmaps, the palette entry
148 /* Get the pixel at the given coordinates */
149 Pixel P = GetPixel (B, X, Y);
151 /* If the bitmap has a palette, return the color from the palette. For
152 * simplicity, we will only check the palette, not the type.
155 if (P.Index >= B->Pal->Count) {
156 /* Palette index is invalid */
157 Error ("Invalid palette index %u at position %u/%u of \"%*s\"",
158 P.Index, X, Y, SB_GetLen (&B->Name),
159 SB_GetConstBuf (&B->Name));
161 return B->Pal->Entries[P.Index];
169 Pixel GetPixel (const Bitmap* B, unsigned X, unsigned Y)
170 /* Return a pixel from the bitmap. The returned value may either be a color
171 * or a palette index, depending on the type of the bitmap.
174 /* Check the coordinates */
175 PRECONDITION (X < B->Width && Y < B->Height);
177 /* Return the pixel */
178 return B->Data[Y * B->Width + X];
183 unsigned GetBitmapColors (const Bitmap* B)
184 /* Get the number of colors in an image. The function will return the number
185 * of palette entries for indexed bitmaps and 2^24 for non indexed bitmaps.
189 case bmMonochrome: return 2;
190 case bmIndexed: return B->Pal->Count;
192 case bmRGBA: return (1U << 24);
193 default: Internal ("Unknown bitmap type %u", B->Type);