GRASS 8 Programmer's Manual 8.6.0dev(2026)-1916102ba5
Loading...
Searching...
No Matches
bitmap.c
Go to the documentation of this file.
1/*
2 ** Bitmap library
3 **
4 ** Written by David Gerdes 12 November 1992
5 ** US Army Construction Engineering Research Laboratories
6 **
7 **
8 ** This library provides basic support for the creation and manipulation
9 ** of two dimensional bitmap arrays.
10 **
11 ** struct BM *
12 ** BM_create (x, y) Create bitmap of specified dimensions
13 **
14 ** BM_set_mode (mode, size) Specify Mode and data size in bits.
15 ** Affects all further calls to BM_create()
16 ** Mode can be BM_FLAT or BM_SPARSE
17 ** Size can only be 1 currently.
18 **
19 ** BM_destroy (map) Destroy bitmap and free memory
20 **
21 ** BM_set (map, x, y, val) Set array position to val [TRUE/FALSE]
22 **
23 ** BM_get (map, x, y) Return value at array position
24 **
25 **
26 ** BM_file_write (fp, map) Write bitmap to file
27 **
28 ** struct BM *
29 ** BM_file_read (fp) Create bitmap and load from file
30 **
31 ** BM_get_map_size (map) returns size in bytes that bitmap is
32 ** taking up. For diagnosis use.
33 */
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <grass/linkm.h>
38#include <grass/bitmap.h>
39
40#define BM_col_to_byte(x) ((x) >> 3) /* x / 8 */
41#define BM_col_to_bit(x) ((x) & 7) /* x % 8 */
42
43static int Mode = BM_FLAT;
44
45/*!
46 * \brief Create bitmap of dimension x/y and return structure token.
47 *
48 * Bitmap is initialized to all zeros
49 *
50 * \param x x dimension
51 * \param y y dimension
52 *
53 * \return pointer to struct BM
54 * \return NULL on error
55 */
56struct BM *BM_create(int x, int y)
57{
58 struct BM *map;
59
60 if (Mode == BM_SPARSE)
61 return BM_create_sparse(x, y);
62
63 if (NULL == (map = (struct BM *)malloc(sizeof(struct BM))))
64 return (NULL);
65
66 map->bytes = (x + 7) / 8;
67
68 if (NULL ==
69 (map->data = (unsigned char *)calloc(map->bytes * y, sizeof(char)))) {
70 free(map);
71 return (NULL);
72 }
73
74 map->rows = y;
75 map->cols = x;
76 map->sparse = 0;
77
78 return map;
79}
80
81/*!
82 * \brief Destroy bitmap and free all associated memory
83 *
84 * \param map
85 * \return int returns 0
86 */
87int BM_destroy(struct BM *map)
88{
89 if (map->sparse)
90 return BM_destroy_sparse(map);
91
92 free(map->data);
93 free(map);
94
95 return 0;
96}
97
98/*
99 ** Caller can specify type of data structure to use for bitmap, as
100 ** well as the size of the data values. Currently since this is
101 ** the 'bitmap' library, size can ONLY have value 1.
102 ** Size is number of bits of storage per cell.
103 **
104 ** Mode:
105 ** BM_FLAT Your basic packed bitmap, eight values are stored per byte
106 ** Thus you get a 1:8 compression over using char arrays
107 ** and a 1:32 compression over using CELL arrays.
108 **
109 **
110 ** BM_SPARSE Linked array of values. Much more efficient for large
111 ** very sparse arrays. Slower access, especially for writing,
112 ** but can save several orders of magnitude of memory on large
113 ** bitmaps since size of FLAT bitmap is O(M*N)
114 **
115 **
116 ** Returns 0 or negative on error;
117 ** If error it will print a warning message to stderr and continue
118 ** continue by running but will not change the option in error.
119 */
120
121/*!
122 * \brief
123 *
124 * Specify the type of data structure to use for bitmap.
125 * 'mode' can be either BM_FLAT or BM_SPARSE:
126 *
127 * BM_FLAT is a basic packed bitmap - eight values stored per byte
128 * thus creating a 1:8 compression over using char arrays and a
129 * 1:32 compression over using CELL arrays.
130 *
131 * BM_SPARSE is a linked array of values. This is much more efficient
132 * for large, very sparse arrays. It is slower to access, especially
133 * for writing, but can save several orders of magnitude of memory on
134 * large bitmaps.
135 *
136 * NOTE: At this time 'size' must be passed a value of 1
137 *
138 * returns 0 on success or -1 on error
139 *
140 * \param mode
141 * \param size
142 * \return int
143 */
144int BM_set_mode(int mode, int size)
145{
146 int ret = 0;
147
148 switch (mode) {
149 case BM_FLAT:
150 case BM_SPARSE:
151 Mode = mode;
152 break;
153 default:
154 fprintf(stderr, "BM_set_mode: Unknown mode: %d\n", mode);
155 ret--;
156 }
157
158 if (size != 1) {
159 fprintf(stderr, "BM_set_mode: Bad size: %d\n", size);
160 ret--;
161 }
162
163 return ret;
164}
165
166/*!
167 * \brief
168 *
169 * Sets bitmap value to 'val' at location 'x' 'y'
170 *
171 * Returns 0 on success
172 *
173 * \param map
174 * \param x
175 * \param y
176 * \param val
177 * \return int
178 */
179int BM_set(struct BM *map, int x, int y, int val)
180{
181 unsigned char byte;
182
183 if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
184 return 0;
185
186 if (map->sparse)
187 return BM_set_sparse(map, x, y, val);
188
189 byte = 0x01 << BM_col_to_bit(x);
190 if (val)
191 map->data[BM_col_to_byte(x) + y * map->bytes] |= byte;
192 else
193 map->data[BM_col_to_byte(x) + y * map->bytes] &= ~byte;
194
195 return 0;
196}
197
198/*!
199 * \brief
200 *
201 * Gets 'val' from the bitmap
202 *
203 * Returns 0 or 1 on success or -1 on error
204 *
205 * \param map
206 * \param x
207 * \param y
208 * \return int
209 */
210int BM_get(struct BM *map, int x, int y)
211{
212 unsigned char byte;
213
214 if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
215 return -1;
216
217 if (map->sparse)
218 return BM_get_sparse(map, x, y);
219
220 byte = map->data[BM_col_to_byte(x) + y * map->bytes];
221
222 return byte >> BM_col_to_bit(x) & 0x01;
223}
224
225/*!
226 * \brief
227 *
228 * Returns size in bytes that bitmap is taking up.
229 *
230 * \param map
231 * \return int
232 */
233size_t BM_get_map_size(struct BM *map)
234{
235 if (map->sparse)
236 return BM_get_map_size_sparse(map);
237
238 return (size_t)map->bytes * map->rows;
239}
240
241/*!
242 * \brief
243 *
244 * Write bitmap out to file
245 *
246 * Expects open file pointer 'fp' and existing map structure.
247 * Caller is responsible to open and close 'fp'.
248 *
249 * Returns 0 or -1 on error
250 *
251 * \param fp
252 * \param map
253 * \return int
254 */
255int BM_file_write(FILE *fp, struct BM *map)
256{
257 char c;
258 int i;
259
260 if (map->sparse)
261 return BM_file_write_sparse(fp, map);
262
263 c = BM_MAGIC;
264 fwrite(&c, sizeof(char), sizeof(char), fp);
265
266 fwrite(BM_TEXT, BM_TEXT_LEN, sizeof(char), fp);
267
268 c = BM_FLAT;
269 fwrite(&c, sizeof(char), sizeof(char), fp);
270
271 fwrite(&(map->rows), sizeof(map->rows), sizeof(char), fp);
272
273 fwrite(&(map->cols), sizeof(map->cols), sizeof(char), fp);
274
275 for (i = 0; i < map->rows; i++)
276 if (map->bytes !=
277 fwrite(&(map->data[i * map->bytes]), sizeof(char), map->bytes, fp))
278 return -1;
279 fflush(fp);
280
281 return 0;
282}
283
284/*!
285 * \brief
286 *
287 * Create map structure and load it from file
288 *
289 * 'fp' should previously been created by <b>BM_file_write()</b>
290 *
291 * Returns struct BM * or NULL on error
292 *
293 * \param fp
294 * \return struct BM
295 */
296struct BM *BM_file_read(FILE *fp)
297{
298 struct BM *map;
299 char c;
300 char buf[BM_TEXT_LEN + 1];
301 int i, y, n;
302 struct BMlink *p = NULL, *p2;
303 int cnt;
304
305 if (NULL == (map = (struct BM *)malloc(sizeof(struct BM))))
306 return (NULL);
307
308 if (fread(&c, sizeof(char), sizeof(char), fp) != sizeof(char)) {
309 free(map);
310 return NULL;
311 }
312
313 if (c != BM_MAGIC) {
314 free(map);
315 return NULL;
316 }
317
318 if (fread(buf, BM_TEXT_LEN, sizeof(char), fp) != sizeof(char)) {
319 free(map);
320 return NULL;
321 }
322
323 if (fread(&c, sizeof(char), sizeof(char), fp) != sizeof(char)) {
324 free(map);
325 return NULL;
326 }
327 map->sparse = c;
328
329 if (fread(&(map->rows), sizeof(map->rows), sizeof(char), fp) !=
330 sizeof(char)) {
331 free(map);
332 return NULL;
333 }
334
335 if (fread(&(map->cols), sizeof(map->cols), sizeof(char), fp) !=
336 sizeof(char)) {
337 free(map);
338 return NULL;
339 }
340
341 map->bytes = (map->cols + 7) / 8;
342
343 if (map->sparse == BM_SPARSE)
344 goto readsparse;
345
346 if (NULL == (map->data = (unsigned char *)malloc(map->bytes * map->rows))) {
347 free(map);
348 return (NULL);
349 }
350
351 for (i = 0; i < map->rows; i++)
352 if (map->bytes !=
353 fread(&(map->data[i * map->bytes]), sizeof(char), map->bytes, fp)) {
354 free(map->data);
355 free(map);
356 return NULL;
357 }
358
359 return map;
360
362
364 map->token = link_init(sizeof(struct BMlink));
365
366 if (NULL == (map->data = (unsigned char *)malloc(sizeof(struct BMlink *) *
367 map->rows))) {
368 free(map);
369 return (NULL);
370 }
371
372 for (y = 0; y < map->rows; y++) {
373 /* first get number of links */
374 if (fread(&i, sizeof(i), sizeof(char), fp) != sizeof(char)) {
375 free(map->data);
376 free(map);
377 return NULL;
378 }
379 cnt = i;
380
381 /* then read them in */
382 for (i = 0; i < cnt; i++) {
383 p2 = (struct BMlink *)link_new(map->token);
384
385 if (i == 0) {
386 ((struct BMlink **)(map->data))[y] = p2;
387 p = p2;
388 }
389 else {
390 p->next = p2;
391 p = p2;
392 }
393
394 if (fread(&n, sizeof(n), sizeof(char), fp) != sizeof(char)) {
395 free(map->data);
396 free(map);
397 return NULL;
398 }
399 p->count = n;
400
401 if (fread(&n, sizeof(n), sizeof(char), fp) != sizeof(char)) {
402 free(map->data);
403 free(map);
404 return NULL;
405 }
406 p->val = n;
407 p->next = NULL;
408 }
409 }
410
411 return map;
412}
struct BM * BM_create(int x, int y)
Create bitmap of dimension x/y and return structure token.
Definition bitmap.c:56
int BM_set(struct BM *map, int x, int y, int val)
Sets bitmap value to 'val' at location 'x' 'y'.
Definition bitmap.c:179
int BM_file_write(FILE *fp, struct BM *map)
Write bitmap out to file.
Definition bitmap.c:255
size_t BM_get_map_size(struct BM *map)
Returns size in bytes that bitmap is taking up.
Definition bitmap.c:233
int BM_get(struct BM *map, int x, int y)
Gets 'val' from the bitmap.
Definition bitmap.c:210
int BM_destroy(struct BM *map)
Destroy bitmap and free all associated memory.
Definition bitmap.c:87
#define BM_col_to_bit(x)
Definition bitmap.c:41
struct BM * BM_file_read(FILE *fp)
Create map structure and load it from file.
Definition bitmap.c:296
int BM_set_mode(int mode, int size)
Specify the type of data structure to use for bitmap. 'mode' can be either BM_FLAT or BM_SPARSE:
Definition bitmap.c:144
#define BM_col_to_byte(x)
Definition bitmap.c:40
#define BM_TEXT
Definition bitmap.h:6
#define BM_FLAT
Definition bitmap.h:9
#define BM_TEXT_LEN
Definition bitmap.h:7
#define BM_SPARSE
Definition bitmap.h:11
#define BM_MAGIC
Definition bitmap.h:4
#define NULL
Definition ccmath.h:32
int BM_set_sparse(struct BM *, int, int, int)
Set sparse bitmap value to 'val' at location 'x'/'y'.
Definition sparse.c:122
struct BM * BM_create_sparse(int, int)
Create a sparse bitmap of dimension 'x'/'y'.
Definition sparse.c:41
size_t BM_get_map_size_sparse(struct BM *)
Returns size of sparse bitmap in bytes.
Definition sparse.c:260
int BM_file_write_sparse(FILE *, struct BM *)
Write sparse bitmap matrix out to disk file 'fp'. NOTE: 'fp' must already be opened and later closed ...
Definition sparse.c:346
int BM_get_sparse(struct BM *, int, int)
Returns sparse bitmap value at location 'x'/'y'.
Definition sparse.c:236
int BM_destroy_sparse(struct BM *)
Destroy sparse bitmap and free all associated memory.
Definition sparse.c:86
VOID_T * link_new(struct link_head *)
Definition new.c:14
void link_set_chunk_size(int)
Definition linkm/init.c:29
struct link_head * link_init(int)
Definition linkm/init.c:39
void * malloc(unsigned)
void free(void *)
Definition bitmap.h:17
int rows
Definition bitmap.h:18
struct link_head * token
Definition bitmap.h:24
int cols
Definition bitmap.h:19
unsigned char * data
Definition bitmap.h:21
size_t bytes
Definition bitmap.h:20
int sparse
Definition bitmap.h:22
#define x