]> git.sur5r.net Git - cc65/blob - src/sp65/bitmap.c
Implemented main conversion module.
[cc65] / src / sp65 / bitmap.c
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
52 Bitmap* 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->Type     = bmUnknown;
68     B->Name     = EmptyStrBuf;
69     B->Width    = Width;
70     B->Height   = Height;
71     B->Pal      = 0;
72
73     /* Return the bitmap */
74     return B;
75 }
76
77
78
79 void FreeBitmap (Bitmap* B)
80 /* Free a dynamically allocated bitmap */
81 {
82     /* Alloc NULL pointers */
83     if (B != 0) {
84         /* Free the palette and then the bitmap */
85         xfree (B->Pal);
86         xfree(B);
87     }
88 }
89
90
91
92 int 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
104 Bitmap* 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     B->Type = O->Type;
123     if (SB_GetLen (&O->Name) > 0) {
124         SB_CopyStr (&B->Name, "Slice of ");
125         SB_Append (&B->Name, &O->Name);
126     }
127     B->Pal = DupPalette (O->Pal);
128
129     /* Copy the pixel data */
130     for (Y = 0; Y < Height; ++Y) {
131         memcpy (B->Data + Y * B->Width,
132                 O->Data + (OrigY + Y) * O->Width + OrigX,
133                 Width * sizeof (O->Data[0]));
134     }
135
136     /* Return the slice */
137     return B;
138 }
139
140
141
142 Color GetPixelColor (const Bitmap* B, unsigned X, unsigned Y)
143 /* Get the color for a given pixel. For indexed bitmaps, the palette entry
144  * is returned.
145  */
146 {
147     /* Get the pixel at the given coordinates */
148     Pixel P = GetPixel (B, X, Y);
149
150     /* If the bitmap has a palette, return the color from the palette. For
151      * simplicity, we will only check the palette, not the type.
152      */
153     if (B->Pal) {
154         if (P.Index >= B->Pal->Count) {
155             /* Palette index is invalid */
156             Error ("Invalid palette index %u at position %u/%u of \"%*s\"",
157                    P.Index, X, Y, SB_GetLen (&B->Name),
158                    SB_GetConstBuf (&B->Name));
159         }
160         return B->Pal->Entries[P.Index];
161     } else {
162         return P.C;
163     }
164 }
165
166
167
168 Pixel GetPixel (const Bitmap* B, unsigned X, unsigned Y)
169 /* Return a pixel from the bitmap. The returned value may either be a color
170  * or a palette index, depending on the type of the bitmap.
171  */
172 {
173     /* Check the coordinates */
174     PRECONDITION (X < B->Width && Y < B->Height);
175
176     /* Return the pixel */
177     return B->Data[Y * B->Width + X];
178 }
179
180
181
182 unsigned GetBitmapColors (const Bitmap* B)
183 /* Get the number of colors in an image. The function will return the number
184  * of palette entries for indexed bitmaps and 2^24 for non indexed bitmaps.
185  */
186 {
187     switch (B->Type) {
188         case bmMonochrome:      return 2;
189         case bmIndexed:         return B->Pal->Count;
190         case bmRGB:
191         case bmRGBA:            return (1U << 24);
192         default:                Internal ("Unknown bitmap type %u", B->Type);
193     }   
194     /* NOTREACHED */
195     return 0;
196 }
197
198
199