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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/*
** Bitmap library
**
** Written by David Gerdes 12 November 1992
** US Army Construction Engineering Research Laboratories
**
**
** This library provides basic support for the creation and manipulation
** of two dimensional bitmap arrays.
**
** struct BM *
** BM_create (x, y) Create bitmap of specified dimensions
**
** BM_set_mode (mode, size) Specify Mode and data size in bits.
** Affects all further calls to BM_create()
** Mode can be BM_FLAT or BM_SPARSE
** Size can only be 1 currently.
**
** BM_destroy (map) Destroy bitmap and free memory
**
** BM_set (map, x, y, val) Set array position to val [TRUE/FALSE]
**
** BM_get (map, x, y) Return value at array position
**
**
** BM_file_write (fp, map) Write bitmap to file
**
** struct BM *
** BM_file_read (fp) Create bitmap and load from file
**
** BM_get_map_size (map) returns size in bytes that bitmap is
** taking up. For diagnosis use.
*/
#include <stdio.h>
#include <stdlib.h>
#include "linkm.h"
#include "bitmap.h"
#define BM_col_to_byte(x) ((x)/8)
#define BM_col_to_bit(x) ((x)%8)
static int Mode = BM_FLAT;
static int Size = 1;
/*!
* \brief Create bitmap of dimension x/y and return structure token.
* Bitmap is initialized to all zeros
*
* \param int x
* \param int y
* \return struct BM or NULL on error
*/
struct BM *BM_create (int x, int y)
{
struct BM *map;
if (Mode == BM_SPARSE)
return BM_create_sparse (x, y);
if (NULL == (map = (struct BM *) malloc (sizeof (struct BM))))
return (NULL);
map->bytes = (x+7)/8;
if (NULL == (map->data = (unsigned char *) calloc ( map->bytes * y, 1 )))
return (NULL);
map->rows = y;
map->cols = x;
map->sparse = 0;
return map;
}
/*!
* \brief Destroy bitmap and free all associated memory
*
* \param struct BM *map
* \return int returns 0
*/
int
BM_destroy (struct BM *map)
{
if (map->sparse)
return BM_destroy_sparse (map);
free (map->data);
free (map);
return 0;
}
/*
** Caller can specify type of data structure to use for bitmap, as
** well as the size of the data values. Currently since this is
** the 'bitmap' library, size can ONLY have value 1.
** Size is number of bits of storage per cell.
**
** Mode:
** BM_FLAT Your basic packed bitmap, eight values are stored per byte
** Thus you get a 1:8 compression over using char arrays
** and a 1:32 compression over using CELL arrays.
**
**
** BM_SPARSE Linked array of values. Much more efficient for large
** very sparse arrays. Slower access, especially for writing,
** but can save several orders of magnitude of memory on large
** bitmaps since size of FLAT bitmap is O(M*N)
**
**
** Returns 0 or negative on error;
** If error it will print a warning message to stderr and continue
** continue by running but will not change the option in error.
*/
int
BM_set_mode (int mode, int size)
{
int ret = 0;
switch (mode) {
case BM_FLAT:
case BM_SPARSE:
Mode = mode;
default:
fprintf (stderr, "BM_set_mode: Unknown mode: %d\n", mode);
ret--;
}
if (size != 1)
{
fprintf (stderr, "BM_set_mode: Bad size: %d\n", size);
ret--;
}
else
Size = size;
return ret;
}
/*
** Set array value
**
** returns 0;
*/
int
BM_set (struct BM *map, int x, int y, int val)
{
unsigned char byte;
if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
return 0;
if (map->sparse)
return BM_set_sparse (map, x, y, val);
byte = 0x01 << BM_col_to_bit(x);
if (val)
map->data[BM_col_to_byte(x) + y * map->bytes] |= byte;
else
map->data[BM_col_to_byte(x) + y * map->bytes] &= ~byte;
return 0;
}
/*
** return array value
**
** returns 0 or 1 or -1 on error
*/
int
BM_get (struct BM *map, int x, int y)
{
unsigned char byte;
if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
return -1;
if (map->sparse)
return BM_get_sparse (map, x, y);
byte = map->data[BM_col_to_byte(x) + y * map->bytes];
return byte >> BM_col_to_bit(x) & 0x01;
}
/*
** returns size in bytes that bitmap is taking up.
*/
int
BM_get_map_size (struct BM *map)
{
if (map->sparse)
return BM_get_map_size_sparse(map);
return map->bytes * map->rows;
}
/*
** Write bitmap out to file.
** Expects open file pointer fp and existing map structure
** caller is responsible to open and close fp
**
** returns 0 or -1 on error
*/
int BM_file_write (FILE *fp, struct BM *map)
{
char c;
int i;
if (map->sparse)
return BM_file_write_sparse (fp, map);
c = BM_MAGIC;
fwrite (&c, 1, 1, fp);
fwrite (BM_TEXT, BM_TEXT_LEN, 1, fp);
c = BM_FLAT;
fwrite (&c, 1, 1, fp);
fwrite (&(map->rows), sizeof (map->rows), 1, fp);
fwrite (&(map->cols), sizeof (map->cols), 1, fp);
for (i = 0 ; i < map->rows ; i++)
if(map->bytes != fwrite (&(map->data[i*map->bytes]), 1, map->bytes, fp))
return -1;
fflush (fp);
return 0;
}
/*
** Create map structure and load it from file
** File should previously been created by BM_file_write()
**
** returns struct BM * or NULL on error.
*/
struct BM *BM_file_read (FILE *fp)
{
struct BM *map;
char c;
char buf[BM_TEXT_LEN + 1];
int i, y, n;
struct BMlink *p = NULL, *p2;
int cnt;
if (NULL == (map = (struct BM *) malloc (sizeof (struct BM))))
return (NULL);
fread (&c, 1, 1, fp);
if (c != BM_MAGIC)
return NULL;
fread (buf, BM_TEXT_LEN, 1, fp);
fread (&c, 1, 1, fp);
map->sparse = c;
fread (&(map->rows), sizeof (map->rows), 1, fp);
fread (&(map->cols), sizeof (map->cols), 1, fp);
map->bytes = (map->cols+7)/8;
if (map->sparse == BM_SPARSE)
goto readsparse;
if (NULL == (map->data = (unsigned char *) malloc (map->bytes * map->rows)))
return (NULL);
for (i = 0 ; i < map->rows ; i++)
if(map->bytes != fread (&(map->data[i*map->bytes]), 1, map->bytes, fp))
return NULL;
return map;
readsparse:
link_set_chunk_size (500);
map->token = link_init (sizeof (struct BMlink));
if (NULL == (map->data = (unsigned char *)
malloc ( sizeof (struct BMlink *) * map->rows )))
return (NULL);
for (y = 0 ; y < map->rows ; y++)
{
/* first get number of links */
fread (&i, sizeof (i), 1, fp);
cnt = i;
/* then read them in */
for (i = 0 ; i < cnt ; i++)
{
p2 = (struct BMlink *) link_new (map->token);
if (i == 0)
{
((struct BMlink **) (map->data))[y] = p2;
p = p2;
}
else
{
p->next = p2;
p = p2;
}
fread (&n, sizeof (n), 1, fp);
p->count = n;
fread (&n, sizeof (n), 1, fp);
p->val = n;
p->next = NULL;
}
}
return map;
}
|