forked from megamarc/Tilengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitmap.c
269 lines (250 loc) · 4.69 KB
/
Bitmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Tilengine - The 2D retro graphics engine with raster effects
* Copyright (C) 2015-2019 Marc Palacios Domenech <mailto:[email protected]>
* All rights reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* */
#ifdef __STRICT_ANSI__
#undef __STRICT_ANSI__
#endif
#include <string.h>
#include "Tilengine.h"
#include "Object.h"
#include "Palette.h"
#include "Bitmap.h"
/*!
* \brief
* Creates a memory bitmap
*
* \param width
* Width in pixels
*
* \param height
* Height in pixels
*
* \param bpp
* Bits per pixel
*
* \returns
* Reference to the created bitmap, or NULL if error
*
* \see
* TLN_SetBGBitmap()
*/
TLN_Bitmap TLN_CreateBitmap (int width, int height, int bpp)
{
TLN_Bitmap bitmap;
int pitch, size;
pitch = (((width * bpp)>>3) + 3) & ~0x03;
size = sizeof(struct Bitmap) + (pitch * height);
bitmap = (TLN_Bitmap)CreateBaseObject (OT_BITMAP, size);
if (bitmap)
{
bitmap->width = width;
bitmap->height = height;
bitmap->bpp = bpp;
bitmap->pitch = pitch;
TLN_SetLastError (TLN_ERR_OK);
return bitmap;
}
else
return NULL;
}
/*!
* \brief
* Creates a copy of a bitmap
*
* \param src
* Reference to the original bitmap
*
* \returns
* Reference to the created bitmap, or NULL if error
*
* \see
* TLN_SetBGBitmap()
*/
TLN_Bitmap TLN_CloneBitmap (TLN_Bitmap src)
{
TLN_Bitmap bitmap;
if (!CheckBaseObject (src, OT_BITMAP))
return NULL;
bitmap = (TLN_Bitmap)CloneBaseObject (src);
if (bitmap)
{
TLN_SetLastError (TLN_ERR_OK);
return bitmap;
}
else
return NULL;
}
/*!
* \brief
* Deletes bitmap and frees resources
*
* \param bitmap
* Reference to bitmap to delete
*
* \see
* TLN_CreateBitmap89, TLN_CloneBitmap()
*/
bool TLN_DeleteBitmap (TLN_Bitmap bitmap)
{
if (CheckBaseObject (bitmap, OT_BITMAP))
{
if (ObjectOwner (bitmap) && bitmap->palette)
TLN_DeletePalette (bitmap->palette);
DeleteBaseObject (bitmap);
TLN_SetLastError (TLN_ERR_OK);
return true;
}
else
return false;
}
/*!
* \brief
* Gets memory access for direct pixel manipulation
*
* \param bitmap
* Reference to bitmap
*
* \param x
* Starting x position [0, width - 1]
*
* \param y
* Starting y position [0, height - 1]
*
* \returns
* Pointer to pixel data starting at x,y
*
* \remarks
* Care must be taken in manipulating memory directly as it can crash the application
*/
uint8_t* TLN_GetBitmapPtr (TLN_Bitmap bitmap, int x, int y)
{
uint8_t *srcptr;
if (!CheckBaseObject (bitmap, OT_BITMAP))
return NULL;
if (x>=bitmap->width || y>=bitmap->height)
{
TLN_SetLastError (TLN_ERR_WRONG_SIZE);
return NULL;
}
TLN_SetLastError (TLN_ERR_OK);
srcptr = get_bitmap_ptr(bitmap, x, y);
return srcptr;
}
/*!
* \brief
* Gets the associated palete of a bitmap
*
* \param bitmap
* Reference to bitmap
*
* \returns
* Reference to the bitmap palette
*
* \see
* TLN_SetBitmapPalette()
*/
TLN_Palette TLN_GetBitmapPalette (TLN_Bitmap bitmap)
{
if (CheckBaseObject (bitmap, OT_BITMAP))
{
TLN_SetLastError (TLN_ERR_OK);
return bitmap->palette;
}
else
return NULL;
}
/*!
* \brief
* Assigns a new palette to the bitmap
*
* \param bitmap
* Reference to the bitmap
*
* \param palette
* Reference to the palette to assign
*
* \see
* TLN_GetBitmapPalette()
*/
bool TLN_SetBitmapPalette (TLN_Bitmap bitmap, TLN_Palette palette)
{
if (!CheckBaseObject (bitmap, OT_BITMAP) || !CheckBaseObject (palette, OT_PALETTE))
return false;
bitmap->palette = palette;
TLN_SetLastError (TLN_ERR_OK);
return true;
}
/*!
* \brief
* Returns the width in pixels
*
* \param bitmap
* Reference to the bitmap
*/
int TLN_GetBitmapWidth (TLN_Bitmap bitmap)
{
if (CheckBaseObject (bitmap, OT_BITMAP))
{
TLN_SetLastError (TLN_ERR_OK);
return bitmap->width;
}
else
return 0;
}
/*!
* \brief
* Returns the height in pixels
*
* \param bitmap
* Reference to the bitmap
*/
int TLN_GetBitmapHeight (TLN_Bitmap bitmap)
{
if (CheckBaseObject (bitmap, OT_BITMAP))
{
TLN_SetLastError (TLN_ERR_OK);
return bitmap->height;
}
else
return 0;
}
/*!
* \brief
* Returns the bits per pixel
*
* \param bitmap
* Reference to the bitmap
*/
int TLN_GetBitmapDepth (TLN_Bitmap bitmap)
{
if (CheckBaseObject (bitmap, OT_BITMAP))
{
TLN_SetLastError (TLN_ERR_OK);
return bitmap->bpp;
}
else
return 0;
}
/*!
* \brief
* Returns the number of bytes per scaline (also known a stride)
*
* \param bitmap
* Reference to the bitmap
*/
int TLN_GetBitmapPitch (TLN_Bitmap bitmap)
{
if (CheckBaseObject (bitmap, OT_BITMAP))
{
TLN_SetLastError (TLN_ERR_OK);
return bitmap->pitch;
}
else
return 0;
}