1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* bitmap.h */ |
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 | #ifndef BITMAP_H |
37 | #define BITMAP_H |
38 | |
39 | |
40 | |
41 | /* common */ |
42 | #include "strbuf.h" |
43 | |
44 | /* sp65 */ |
45 | #include "palette.h" |
46 | #include "pixel.h" |
47 | |
48 | |
49 | |
50 | /*****************************************************************************/ |
51 | /* Data */ |
52 | /*****************************************************************************/ |
53 | |
54 | |
55 | |
56 | /* Safety limit for the bitmap sizes */ |
57 | #define BM_MAX_WIDTH 4096U |
58 | #define BM_MAX_HEIGHT 4096U |
59 | |
60 | /* Safety limit for the size of the bitmap in pixels */ |
61 | #define BM_MAX_SIZE 4194304UL |
62 | |
63 | /* Bitmap structure */ |
64 | typedef struct Bitmap Bitmap; |
65 | struct Bitmap { |
66 | |
67 | /* Name of the bitmap. This is used for error messages and should be |
68 | ** something that allows the user to identify which bitmap the message |
69 | ** refers to. For bitmaps loaded from a file, using the file name is |
70 | ** a good idea. |
71 | */ |
72 | StrBuf Name; |
73 | |
74 | /* Size of the bitmap */ |
75 | unsigned Width; |
76 | unsigned Height; |
77 | |
78 | /* Palette for indexed bitmap types, otherwise NULL */ |
79 | Palette* Pal; |
80 | |
81 | /* Pixel data, dynamically allocated */ |
82 | Pixel Data[1]; |
83 | }; |
84 | |
85 | |
86 | |
87 | /*****************************************************************************/ |
88 | /* Code */ |
89 | /*****************************************************************************/ |
90 | |
91 | |
92 | |
93 | Bitmap* NewBitmap (unsigned Width, unsigned Height); |
94 | /* Create a new bitmap. The type is set to unknown and the palette to NULL */ |
95 | |
96 | void FreeBitmap (Bitmap* B); |
97 | /* Free a dynamically allocated bitmap */ |
98 | |
99 | int ValidBitmapSize (unsigned Width, unsigned Height); |
100 | /* Return true if this is a valid size for a bitmap */ |
101 | |
102 | Bitmap* SliceBitmap (const Bitmap* Original, |
103 | unsigned X, unsigned Y, |
104 | unsigned Width, unsigned Height); |
105 | /* Create a slice of the given bitmap. The slice starts at position X/Y of |
106 | ** the original and has the given width and height. Location 0/0 is at the |
107 | ** upper left corner. |
108 | */ |
109 | |
110 | Color GetPixelColor (const Bitmap* B, unsigned X, unsigned Y); |
111 | /* Get the color for a given pixel. For indexed bitmaps, the palette entry |
112 | ** is returned. |
113 | */ |
114 | |
115 | Pixel GetPixel (const Bitmap* B, unsigned X, unsigned Y); |
116 | /* Return a pixel from the bitmap. The returned value may either be a color |
117 | ** or a palette index, depending on the type of the bitmap. |
118 | */ |
119 | |
120 | #if defined(HAVE_INLINE) |
121 | INLINE int BitmapIsIndexed (const Bitmap* B) |
122 | /* Return true if this is an indexed bitmap */ |
123 | { |
124 | return (B->Pal != 0); |
125 | } |
126 | #else |
127 | # define BitmapIsIndexed(B) ((B)->Pal != 0) |
128 | #endif |
129 | |
130 | #if defined(HAVE_INLINE) |
131 | INLINE unsigned GetBitmapWidth (const Bitmap* B) |
132 | /* Get the width of a bitmap */ |
133 | { |
134 | return B->Width; |
135 | } |
136 | #else |
137 | # define GetBitmapWidth(B) ((B)->Width) |
138 | #endif |
139 | |
140 | #if defined(HAVE_INLINE) |
141 | INLINE unsigned GetBitmapHeight (const Bitmap* B) |
142 | /* Get the height of a bitmap */ |
143 | { |
144 | return B->Height; |
145 | } |
146 | #else |
147 | # define GetBitmapHeight(B) ((B)->Height) |
148 | #endif |
149 | |
150 | #if defined(HAVE_INLINE) |
151 | INLINE const Palette* GetBitmapPalette (const Bitmap* B) |
152 | /* Get the palette of a bitmap */ |
153 | { |
154 | return B->Pal; |
155 | } |
156 | #else |
157 | # define GetBitmapPalette(B) ((B)->Pal) |
158 | #endif |
159 | |
160 | #if defined(HAVE_INLINE) |
161 | INLINE const StrBuf* GetBitmapName (const Bitmap* B) |
162 | /* Get the name of a bitmap */ |
163 | { |
164 | return &B->Name; |
165 | } |
166 | #else |
167 | # define GetBitmapName(B) (&(B)->Name) |
168 | #endif |
169 | |
170 | #if defined(HAVE_INLINE) |
171 | INLINE unsigned GetBitmapColors (const Bitmap* B) |
172 | /* Get the number of colors in an image. The function will return the number |
173 | ** of palette entries for indexed bitmaps and 2^24 for non indexed bitmaps. |
174 | */ |
175 | { |
176 | return B->Pal? B->Pal->Count : (1U << 24); |
177 | } |
178 | #else |
179 | # define GetBitmapColors(B) ((B)->Pal? (B)->Pal->Count : (1U << 24)) |
180 | #endif |
181 | |
182 | |
183 | |
184 | /* End of bitmap.h */ |
185 | |
186 | #endif |
187 | |