37 #include <grass/linkm.h>
38 #include <grass/bitmap.h>
41 #define BM_col_to_byte(x) ((x)/8)
42 #define BM_col_to_bit(x) ((x)%8)
44 static int Mode = BM_FLAT;
64 if (Mode == BM_SPARSE)
67 if (
NULL == (map = (
struct BM *)malloc(
sizeof(
struct BM))))
70 map->bytes = (x + 7) / 8;
73 (map->data = (
unsigned char *)calloc(map->bytes * y,
sizeof(
char))))
158 fprintf(stderr,
"BM_set_mode: Unknown mode: %d\n", mode);
163 fprintf(stderr,
"BM_set_mode: Bad size: %d\n", size);
187 int BM_set(
struct BM *map,
int x,
int y,
int val)
191 if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
224 if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
250 return map->bytes * map->rows;
278 fwrite(&c,
sizeof(
char),
sizeof(
char), fp);
280 fwrite(BM_TEXT, BM_TEXT_LEN,
sizeof(
char), fp);
283 fwrite(&c,
sizeof(
char),
sizeof(
char), fp);
285 fwrite(&(map->rows),
sizeof(map->rows),
sizeof(
char), fp);
287 fwrite(&(map->cols),
sizeof(map->cols),
sizeof(
char), fp);
289 for (i = 0; i < map->rows; i++)
291 fwrite(&(map->data[i * map->bytes]),
sizeof(
char), map->bytes,
317 char buf[BM_TEXT_LEN + 1];
319 struct BMlink *p =
NULL, *p2;
322 if (
NULL == (map = (
struct BM *)malloc(
sizeof(
struct BM))))
325 fread(&c,
sizeof(
char),
sizeof(
char), fp);
329 fread(buf, BM_TEXT_LEN,
sizeof(
char), fp);
331 fread(&c,
sizeof(
char),
sizeof(
char), fp);
335 fread(&(map->rows),
sizeof(map->rows),
sizeof(
char), fp);
337 fread(&(map->cols),
sizeof(map->cols),
sizeof(
char), fp);
339 map->bytes = (map->cols + 7) / 8;
341 if (map->sparse == BM_SPARSE)
344 if (
NULL == (map->data = (
unsigned char *)malloc(map->bytes * map->rows)))
348 for (i = 0; i < map->rows; i++)
350 fread(&(map->data[i * map->bytes]),
sizeof(
char), map->bytes, fp))
359 map->token =
link_init(
sizeof(
struct BMlink));
362 if (
NULL == (map->data = (
unsigned char *)
363 malloc(
sizeof(
struct BMlink *) * map->rows)))
366 for (y = 0; y < map->rows; y++) {
368 fread(&i,
sizeof(i),
sizeof(
char), fp);
373 for (i = 0; i < cnt; i++) {
374 p2 = (
struct BMlink *)
link_new(map->token);
377 ((
struct BMlink **)(map->data))[y] = p2;
385 fread(&n,
sizeof(n),
sizeof(
char), fp);
388 fread(&n,
sizeof(n),
sizeof(
char), fp);