37 #include <grass/linkm.h>
38 #include <grass/bitmap.h>
40 #define BM_col_to_byte(x) ((x) >> 3)
41 #define BM_col_to_bit(x) ((x) & 7)
43 static int Mode = BM_FLAT;
61 if (Mode == BM_SPARSE)
64 if (
NULL == (map = (
struct BM *)malloc(
sizeof(
struct BM))))
67 map->bytes = (
x + 7) / 8;
70 (map->data = (
unsigned char *)calloc(map->bytes * y,
sizeof(
char)))) {
155 fprintf(stderr,
"BM_set_mode: Unknown mode: %d\n", mode);
160 fprintf(stderr,
"BM_set_mode: Bad size: %d\n", size);
182 int BM_set(
struct BM *map,
int x,
int y,
int val)
186 if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
217 if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
241 return (
size_t)map->bytes * map->rows;
267 fwrite(&c,
sizeof(
char),
sizeof(
char), fp);
269 fwrite(BM_TEXT, BM_TEXT_LEN,
sizeof(
char), fp);
272 fwrite(&c,
sizeof(
char),
sizeof(
char), fp);
274 fwrite(&(map->rows),
sizeof(map->rows),
sizeof(
char), fp);
276 fwrite(&(map->cols),
sizeof(map->cols),
sizeof(
char), fp);
278 for (i = 0; i < map->rows; i++)
280 fwrite(&(map->data[i * map->bytes]),
sizeof(
char), map->bytes, fp))
303 char buf[BM_TEXT_LEN + 1];
305 struct BMlink *p =
NULL, *p2;
308 if (
NULL == (map = (
struct BM *)malloc(
sizeof(
struct BM))))
311 if (fread(&c,
sizeof(
char),
sizeof(
char), fp) !=
sizeof(
char)) {
321 if (fread(buf, BM_TEXT_LEN,
sizeof(
char), fp) !=
sizeof(
char)) {
326 if (fread(&c,
sizeof(
char),
sizeof(
char), fp) !=
sizeof(
char)) {
332 if (fread(&(map->rows),
sizeof(map->rows),
sizeof(
char), fp) !=
338 if (fread(&(map->cols),
sizeof(map->cols),
sizeof(
char), fp) !=
344 map->bytes = (map->cols + 7) / 8;
346 if (map->sparse == BM_SPARSE)
349 if (
NULL == (map->data = (
unsigned char *)malloc(map->bytes * map->rows))) {
354 for (i = 0; i < map->rows; i++)
356 fread(&(map->data[i * map->bytes]),
sizeof(
char), map->bytes, fp)) {
367 map->token =
link_init(
sizeof(
struct BMlink));
369 if (
NULL == (map->data = (
unsigned char *)malloc(
sizeof(
struct BMlink *) *
375 for (y = 0; y < map->rows; y++) {
377 if (fread(&i,
sizeof(i),
sizeof(
char), fp) !=
sizeof(
char)) {
385 for (i = 0; i < cnt; i++) {
386 p2 = (
struct BMlink *)
link_new(map->token);
389 ((
struct BMlink **)(map->data))[y] = p2;
397 if (fread(&n,
sizeof(n),
sizeof(char), fp) !=
sizeof(char)) {
404 if (fread(&n,
sizeof(n),
sizeof(
char), fp) !=
sizeof(
char)) {
struct BM * BM_create(int x, int y)
Create bitmap of dimension x/y and return structure token.
int BM_set(struct BM *map, int x, int y, int val)
Sets bitmap value to 'val' at location 'x' 'y'.
int BM_file_write(FILE *fp, struct BM *map)
Write bitmap out to file.
size_t BM_get_map_size(struct BM *map)
Returns size in bytes that bitmap is taking up.
int BM_get(struct BM *map, int x, int y)
Gets 'val' from the bitmap.
int BM_destroy(struct BM *map)
Destroy bitmap and free all associated memory.
struct BM * BM_file_read(FILE *fp)
Create map structure and load it from file.
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:
#define BM_col_to_byte(x)
struct link_head * link_init(int size)
void link_set_chunk_size(int size)
VOID_T * link_new(struct link_head *Head)
struct BM * BM_create_sparse(int x, int y)
Create a sparse bitmap of dimension 'x'/'y'.
int BM_file_write_sparse(FILE *fp, struct BM *map)
Write sparse bitmap matrix out to disk file 'fp'. NOTE: 'fp' must already be opened and later closed ...
int BM_get_sparse(struct BM *map, int x, int y)
Returns sparse bitmap value at location 'x'/'y'.
size_t BM_get_map_size_sparse(struct BM *map)
Returns size of sparse bitmap in bytes.
int BM_destroy_sparse(struct BM *map)
Destroy sparse bitmap and free all associated memory.
int BM_set_sparse(struct BM *map, int x, int y, int val)
Set sparse bitmap value to 'val' at location 'x'/'y'.