bitmap.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <grass/linkm.h>
  37. #include <grass/bitmap.h>
  38. #define BM_col_to_byte(x) ((x)/8)
  39. #define BM_col_to_bit(x) ((x)%8)
  40. static int Mode = BM_FLAT;
  41. static int Size = 1;
  42. /*!
  43. * \brief Create bitmap of dimension x/y and return structure token.
  44. * Bitmap is initialized to all zeros
  45. *
  46. * \param int x
  47. * \param int y
  48. * \return struct BM or NULL on error
  49. */
  50. struct BM *BM_create (int x, int y)
  51. {
  52. struct BM *map;
  53. if (Mode == BM_SPARSE)
  54. return BM_create_sparse (x, y);
  55. if (NULL == (map = (struct BM *) malloc (sizeof (struct BM))))
  56. return (NULL);
  57. map->bytes = (x+7)/8;
  58. if (NULL == (map->data = (unsigned char *)calloc (map->bytes * y, sizeof(char))))
  59. return (NULL);
  60. map->rows = y;
  61. map->cols = x;
  62. map->sparse = 0;
  63. return map;
  64. }
  65. /*!
  66. * \brief Destroy bitmap and free all associated memory
  67. *
  68. * \param struct BM *map
  69. * \return int returns 0
  70. */
  71. int
  72. BM_destroy (struct BM *map)
  73. {
  74. if (map->sparse)
  75. return BM_destroy_sparse (map);
  76. free (map->data);
  77. free (map);
  78. return 0;
  79. }
  80. /*
  81. ** Caller can specify type of data structure to use for bitmap, as
  82. ** well as the size of the data values. Currently since this is
  83. ** the 'bitmap' library, size can ONLY have value 1.
  84. ** Size is number of bits of storage per cell.
  85. **
  86. ** Mode:
  87. ** BM_FLAT Your basic packed bitmap, eight values are stored per byte
  88. ** Thus you get a 1:8 compression over using char arrays
  89. ** and a 1:32 compression over using CELL arrays.
  90. **
  91. **
  92. ** BM_SPARSE Linked array of values. Much more efficient for large
  93. ** very sparse arrays. Slower access, especially for writing,
  94. ** but can save several orders of magnitude of memory on large
  95. ** bitmaps since size of FLAT bitmap is O(M*N)
  96. **
  97. **
  98. ** Returns 0 or negative on error;
  99. ** If error it will print a warning message to stderr and continue
  100. ** continue by running but will not change the option in error.
  101. */
  102. /*!
  103. * \brief
  104. *
  105. * Specify the type of data structure to use for bitmap.
  106. * 'mode' can be either BM_FLAT or BM_SPARSE:
  107. *
  108. * BM_FLAT is a basic packed bitmap - eight values stored per byte
  109. * thus creating a 1:8 compression over using char arrays and a
  110. * 1:32 compression over using CELL arrays.
  111. *
  112. * BM_SPARSE is a linked array of values. This is much more efficient
  113. * for large, very sparse arrays. It is slower to access, especially
  114. * for writing, but can save several orders of magnitude of memory on
  115. * large bitmaps.
  116. *
  117. * NOTE: At this time 'size' must be passed a value of 1
  118. *
  119. * returns 0 on success or -1 on error
  120. *
  121. * \param int mode
  122. * \param int size
  123. * \return int
  124. */
  125. int
  126. BM_set_mode (int mode, int size)
  127. {
  128. int ret = 0;
  129. switch (mode) {
  130. case BM_FLAT:
  131. case BM_SPARSE:
  132. Mode = mode;
  133. default:
  134. fprintf (stderr, "BM_set_mode: Unknown mode: %d\n", mode);
  135. ret--;
  136. }
  137. if (size != 1)
  138. {
  139. fprintf (stderr, "BM_set_mode: Bad size: %d\n", size);
  140. ret--;
  141. }
  142. else
  143. Size = size;
  144. return ret;
  145. }
  146. /*!
  147. * \brief
  148. *
  149. * Sets bitmap value to 'val' at location 'x' 'y'
  150. *
  151. * Returns 0 on success
  152. *
  153. * \param map
  154. * \param x
  155. * \param y
  156. * \param val
  157. * \return int
  158. */
  159. int
  160. BM_set (struct BM *map, int x, int y, int val)
  161. {
  162. unsigned char byte;
  163. if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
  164. return 0;
  165. if (map->sparse)
  166. return BM_set_sparse (map, x, y, val);
  167. byte = 0x01 << BM_col_to_bit(x);
  168. if (val)
  169. map->data[BM_col_to_byte(x) + y * map->bytes] |= byte;
  170. else
  171. map->data[BM_col_to_byte(x) + y * map->bytes] &= ~byte;
  172. return 0;
  173. }
  174. /*!
  175. * \brief
  176. *
  177. * Gets 'val' from the bitmap
  178. *
  179. * Returns 0 or 1 on success or -1 on error
  180. *
  181. * \param map
  182. * \param x
  183. * \param y
  184. * \return int
  185. */
  186. int
  187. BM_get (struct BM *map, int x, int y)
  188. {
  189. unsigned char byte;
  190. if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
  191. return -1;
  192. if (map->sparse)
  193. return BM_get_sparse (map, x, y);
  194. byte = map->data[BM_col_to_byte(x) + y * map->bytes];
  195. return byte >> BM_col_to_bit(x) & 0x01;
  196. }
  197. /*!
  198. * \brief
  199. *
  200. * Returns size in bytes that bitmap is taking up.
  201. *
  202. * \param map
  203. * \return int
  204. */
  205. int
  206. BM_get_map_size (struct BM *map)
  207. {
  208. if (map->sparse)
  209. return BM_get_map_size_sparse(map);
  210. return map->bytes * map->rows;
  211. }
  212. /*!
  213. * \brief
  214. *
  215. * Write bitmap out to file
  216. *
  217. * Expects open file pointer 'fp' and existing map structure.
  218. * Caller is responsible to open and close 'fp'.
  219. *
  220. * Returns 0 or -1 on error
  221. *
  222. * \param fp
  223. * \param map
  224. * \return int
  225. */
  226. int BM_file_write (FILE *fp, struct BM *map)
  227. {
  228. char c;
  229. int i;
  230. if (map->sparse)
  231. return BM_file_write_sparse (fp, map);
  232. c = BM_MAGIC;
  233. fwrite (&c, sizeof(char), sizeof(char), fp);
  234. fwrite (BM_TEXT, BM_TEXT_LEN, sizeof(char), fp);
  235. c = BM_FLAT;
  236. fwrite (&c, sizeof(char), sizeof(char), fp);
  237. fwrite (&(map->rows), sizeof (map->rows), sizeof(char), fp);
  238. fwrite (&(map->cols), sizeof (map->cols), sizeof(char), fp);
  239. for (i = 0 ; i < map->rows ; i++)
  240. if(map->bytes != fwrite (&(map->data[i*map->bytes]), sizeof(char), map->bytes, fp))
  241. return -1;
  242. fflush (fp);
  243. return 0;
  244. }
  245. /*!
  246. * \brief
  247. *
  248. * Create map structure and load it from file
  249. *
  250. * 'fp' should previously been created by <b>BM_file_write()</b>
  251. *
  252. * Returns struct BM * or NULL on error
  253. *
  254. * \param fp
  255. * \return struct BM
  256. */
  257. struct BM *BM_file_read (FILE *fp)
  258. {
  259. struct BM *map;
  260. char c;
  261. char buf[BM_TEXT_LEN + 1];
  262. int i, y, n;
  263. struct BMlink *p = NULL, *p2;
  264. int cnt;
  265. if (NULL == (map = (struct BM *) malloc (sizeof (struct BM))))
  266. return (NULL);
  267. fread (&c, sizeof(char), sizeof(char), fp);
  268. if (c != BM_MAGIC)
  269. return NULL;
  270. fread (buf, BM_TEXT_LEN, sizeof(char), fp);
  271. fread (&c, sizeof(char), sizeof(char), fp);
  272. map->sparse = c;
  273. fread (&(map->rows), sizeof (map->rows), sizeof(char), fp);
  274. fread (&(map->cols), sizeof (map->cols), sizeof(char), fp);
  275. map->bytes = (map->cols+7)/8;
  276. if (map->sparse == BM_SPARSE)
  277. goto readsparse;
  278. if (NULL == (map->data = (unsigned char *) malloc (map->bytes * map->rows)))
  279. return (NULL);
  280. for (i = 0 ; i < map->rows ; i++)
  281. if(map->bytes != fread (&(map->data[i*map->bytes]), sizeof(char), map->bytes, fp))
  282. return NULL;
  283. return map;
  284. readsparse:
  285. link_set_chunk_size (500);
  286. map->token = link_init (sizeof (struct BMlink));
  287. if (NULL == (map->data = (unsigned char *)
  288. malloc ( sizeof (struct BMlink *) * map->rows )))
  289. return (NULL);
  290. for (y = 0 ; y < map->rows ; y++)
  291. {
  292. /* first get number of links */
  293. fread (&i, sizeof (i), sizeof(char), fp);
  294. cnt = i;
  295. /* then read them in */
  296. for (i = 0 ; i < cnt ; i++)
  297. {
  298. p2 = (struct BMlink *) link_new (map->token);
  299. if (i == 0)
  300. {
  301. ((struct BMlink **) (map->data))[y] = p2;
  302. p = p2;
  303. }
  304. else
  305. {
  306. p->next = p2;
  307. p = p2;
  308. }
  309. fread (&n, sizeof (n), sizeof(char), fp);
  310. p->count = n;
  311. fread (&n, sizeof (n), sizeof(char), fp);
  312. p->val = n;
  313. p->next = NULL;
  314. }
  315. }
  316. return map;
  317. }