]> git.sur5r.net Git - cc65/blob - src/sp65/bitmap.h
Added the write routine.
[cc65] / src / sp65 / bitmap.h
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 type */
64 enum BitmapType {
65     bmUnknown,
66     bmMonochrome,
67     bmIndexed,
68     bmRGB,
69     bmRGBA
70 };
71 typedef enum BitmapType BitmapType;
72
73 /* Bitmap structure */
74 typedef struct Bitmap Bitmap;
75 struct Bitmap {
76
77     /* Type of the bitmap */
78     BitmapType  Type;
79
80     /* Name of the bitmap. This is used for error messages and should be
81      * something that allows the user to identify which bitmap the message
82      * refers to. For bitmaps loaded from a file, using the file name is
83      * a good idea.
84      */
85     StrBuf      Name;
86
87     /* Size of the bitmap */
88     unsigned    Width;
89     unsigned    Height;
90
91     /* Palette for monochrome and indexed bitmap types, otherwise NULL */
92     Palette*    Pal;
93
94     /* Pixel data, dynamically allocated */
95     Pixel       Data[1];
96 };
97
98
99
100 /*****************************************************************************/
101 /*                                   Code                                    */
102 /*****************************************************************************/
103
104
105
106 Bitmap* NewBitmap (unsigned Width, unsigned Height);
107 /* Create a new bitmap. The type is set to unknown and the palette to NULL */
108
109 void FreeBitmap (Bitmap* B);
110 /* Free a dynamically allocated bitmap */
111
112 int ValidBitmapSize (unsigned Width, unsigned Height);
113 /* Return true if this is a valid size for a bitmap */
114
115 Bitmap* SliceBitmap (const Bitmap* Original,
116                      unsigned X, unsigned Y,
117                      unsigned Width, unsigned Height);
118 /* Create a slice of the given bitmap. The slice starts at position X/Y of
119  * the original and has the given width and height. Location 0/0 is at the
120  * upper left corner.
121  */
122
123 Color GetPixelColor (const Bitmap* B, unsigned X, unsigned Y);
124 /* Get the color for a given pixel. For indexed bitmaps, the palette entry
125  * is returned.
126  */
127
128 Pixel GetPixel (const Bitmap* B, unsigned X, unsigned Y);
129 /* Return a pixel from the bitmap. The returned value may either be a color
130  * or a palette index, depending on the type of the bitmap.
131  */
132
133 #if defined(HAVE_INLINE)
134 INLINE BitmapType GetBitmapType (const Bitmap* B)
135 /* Get the type of a bitmap */
136 {
137     return B->Type;
138 }
139 #else
140 #  define GetBitmapType(B)      ((B)->Type)
141 #endif
142
143 #if defined(HAVE_INLINE)
144 INLINE unsigned GetBitmapWidth (const Bitmap* B)
145 /* Get the width of a bitmap */
146 {
147     return B->Width;
148 }
149 #else
150 #  define GetBitmapWidth(B)     ((B)->Width)
151 #endif
152
153 #if defined(HAVE_INLINE)
154 INLINE unsigned GetBitmapHeight (const Bitmap* B)
155 /* Get the height of a bitmap */
156 {
157     return B->Height;
158 }
159 #else
160 #  define GetBitmapHeight(B)    ((B)->Height)
161 #endif
162
163 unsigned GetBitmapColors (const Bitmap* B);
164 /* Get the number of colors in an image. The function will return the number
165  * of palette entries for indexed bitmaps and 2^24 for non indexed bitmaps.
166  */
167
168
169
170 /* End of bitmap.h */
171
172 #endif
173
174
175