1/*****************************************************************************/
2/* */
3/* bitmap.c */
4/* */
5/* Bitmap definition for the sp65 sprite and bitmap utility */
6/* */
7/* */
8/* */
9/* (C) 2012, Ullrich von Bassewitz */
10/* Roemerstrasse 52 */
11/* D-70794 Filderstadt */
12/* EMail: uz@cc65.org */
13/* */
14/* */
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. */
18/* */
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: */
22/* */
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 */
30/* distribution. */
31/* */
32/*****************************************************************************/
33
34
35
36/* common */
37#include "check.h"
38#include "xmalloc.h"
39
40/* sp65 */
41#include "bitmap.h"
42#include "error.h"
43
44
45
46/*****************************************************************************/
47/* Code */
48/*****************************************************************************/
49
50
51
52Bitmap* NewBitmap (unsigned Width, unsigned Height)
53/* Create a new bitmap. The type is set to unknown and the palette to NULL */
54{
55 Bitmap* B;
56
57 /* Calculate the size of the bitmap in pixels */
58 unsigned long Size = (unsigned long) Width * Height;
59
60 /* Some safety checks */
61 PRECONDITION (Size > 0 && Size <= BM_MAX_SIZE);
62
63 /* Allocate memory */
64 B = xmalloc (sizeof (*B) + (Size - 1) * sizeof (B->Data[0]));
65
66 /* Initialize the data */
67 B->Name = EmptyStrBuf;
68 B->Width = Width;
69 B->Height = Height;
70 B->Pal = 0;
71
72 /* Return the bitmap */
73 return B;
74}
75
76
77
78void FreeBitmap (Bitmap* B)
79/* Free a dynamically allocated bitmap */
80{
81 /* Alloc NULL pointers */
82 if (B != 0) {
83 /* Free name, palette and then the bitmap */
84 SB_Done (&B->Name);
85 xfree (B->Pal);
86 xfree(B);
87 }
88}
89
90
91
92int ValidBitmapSize (unsigned Width, unsigned Height)
93/* Return true if this is a valid size for a bitmap */
94{
95 /* Calculate the size of the bitmap in pixels */
96 unsigned long Size = (unsigned long) Width * Height;
97
98 /* Check the size */
99 return (Size > 0 && Size <= BM_MAX_SIZE);
100}
101
102
103
104Bitmap* SliceBitmap (const Bitmap* O, unsigned OrigX, unsigned OrigY,
105 unsigned Width, unsigned Height)
106/* Create a slice of the given bitmap. The slice starts at position X/Y of
107** the original and has the given width and height. Location 0/0 is at the
108** upper left corner.
109*/
110{
111 Bitmap* B;
112 unsigned Y;
113
114
115 /* Check the coordinates and size */
116 PRECONDITION (OrigX + Width <= O->Width && OrigY + Height <= O->Height);
117
118 /* Create a new bitmap with the given size */
119 B = NewBitmap (Width, Height);
120
121 /* Copy fields from the original */
122 if (SB_GetLen (&O->Name) > 0) {
123 SB_CopyStr (&B->Name, "Slice of ");
124 SB_Append (&B->Name, &O->Name);
125 }
126 B->Pal = DupPalette (O->Pal);
127
128 /* Copy the pixel data */
129 for (Y = 0; Y < Height; ++Y) {
130 memcpy (B->Data + Y * B->Width,
131 O->Data + (OrigY + Y) * O->Width + OrigX,
132 Width * sizeof (O->Data[0]));
133 }
134
135 /* Return the slice */
136 return B;
137}
138
139
140
141Color GetPixelColor (const Bitmap* B, unsigned X, unsigned Y)
142/* Get the color for a given pixel. For indexed bitmaps, the palette entry
143** is returned.
144*/
145{
146 /* Get the pixel at the given coordinates */
147 Pixel P = GetPixel (B, X, Y);
148
149 /* If the bitmap has a palette, return the color from the palette. For
150 ** simplicity, we will only check the palette, not the type.
151 */
152 if (B->Pal) {
153 if (P.Index >= B->Pal->Count) {
154 /* Palette index is invalid */
155 Error ("Invalid palette index %u at position %u/%u of \"%*s\"",
156 P.Index, X, Y, SB_GetLen (&B->Name),
157 SB_GetConstBuf (&B->Name));
158 }
159 return B->Pal->Entries[P.Index];
160 } else {
161 return P.C;
162 }
163}
164
165
166
167Pixel GetPixel (const Bitmap* B, unsigned X, unsigned Y)
168/* Return a pixel from the bitmap. The returned value may either be a color
169** or a palette index, depending on the type of the bitmap.
170*/
171{
172 /* Check the coordinates */
173 PRECONDITION (X < B->Width && Y < B->Height);
174
175 /* Return the pixel */
176 return B->Data[Y * B->Width + X];
177}
178