sparse.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. ** BM_create (x, y) Create bitmap of specified dimensions
  12. **
  13. ** BM_destroy (map) Destroy bitmap and free memory
  14. **
  15. ** BM_set (map, x, y, val) Set array position to val [TRUE/FALSE]
  16. **
  17. ** BM_get (map, x, y) Return value at array position
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <grass/linkm.h>
  22. #include <grass/bitmap.h>
  23. #define BM_col_to_byte(x) ((x) >> 3) /* x / 8 */
  24. #define BM_col_to_bit(x) ((x) & 7) /* x % 8 */
  25. static int depth;
  26. /*!
  27. * \brief
  28. *
  29. * Create a sparse bitmap of dimension 'x'/'y'
  30. *
  31. * Returns bitmap structure or NULL on error
  32. *
  33. * \param x
  34. * \param y
  35. * \return struct BM
  36. */
  37. struct BM *BM_create_sparse(int x, int y)
  38. {
  39. struct BM *map;
  40. int i;
  41. if (NULL == (map = (struct BM *)malloc(sizeof(struct BM))))
  42. return (NULL);
  43. map->bytes = (x + 7) / 8;
  44. if (NULL == (map->data = (unsigned char *)
  45. malloc(sizeof(struct BMlink *) * y)))
  46. return (NULL);
  47. map->rows = y;
  48. map->cols = x;
  49. map->sparse = 1;
  50. link_set_chunk_size(500);
  51. map->token = link_init(sizeof(struct BMlink));
  52. for (i = 0; i < map->rows; i++) {
  53. ((struct BMlink **)(map->data))[i] =
  54. (struct BMlink *)link_new(map->token);
  55. ((struct BMlink **)(map->data))[i]->count = x;
  56. ((struct BMlink **)(map->data))[i]->val = 0;
  57. ((struct BMlink **)(map->data))[i]->next = NULL;
  58. }
  59. depth++;
  60. return map;
  61. }
  62. /*!
  63. * \brief
  64. *
  65. * Destroy sparse bitmap and free all associated memory
  66. *
  67. * Returns 0
  68. *
  69. * \param map
  70. * \return int
  71. */
  72. int BM_destroy_sparse(struct BM *map)
  73. {
  74. int i;
  75. struct BMlink *p, *tmp;
  76. for (i = 0; i < map->rows; i++) {
  77. p = ((struct BMlink **)(map->data))[i];
  78. while (p != NULL) {
  79. tmp = p->next;
  80. link_dispose(map->token, (VOID_T *) p);
  81. p = tmp;
  82. }
  83. }
  84. if (--depth == 0)
  85. link_cleanup(map->token);
  86. free(map->data);
  87. free(map);
  88. return 0;
  89. }
  90. /*!
  91. * \brief
  92. *
  93. * Set sparse bitmap value to 'val' at location 'x'/'y'
  94. *
  95. * Returns 0
  96. *
  97. * \param map
  98. * \param x
  99. * \param y
  100. * \param val
  101. * \return int
  102. */
  103. int BM_set_sparse(struct BM *map, int x, int y, int val)
  104. {
  105. struct BMlink *p, *p2, *prev;
  106. int cur_x = 0;
  107. int Tcount, Tval;
  108. int dist_a, dist_b;
  109. val = !(!val); /* set val == 1 or 0 */
  110. p = ((struct BMlink **)(map->data))[y];
  111. prev = NULL;
  112. while (p != NULL) {
  113. if (cur_x + p->count > x) {
  114. if (p->val == val) /* no change */
  115. return 0;
  116. Tcount = p->count; /* save current state */
  117. Tval = p->val;
  118. /* if x is on edge, then we probably want to merge it with
  119. ** its neighbor for efficiency
  120. */
  121. /* dist_a is how far x is from Left edge of group */
  122. /* dist_b is how far x is from right edge of group */
  123. dist_a = x - cur_x;
  124. dist_b = (cur_x + p->count - 1) - x;
  125. /* if on both edges, then we should be able to merge 3 into one */
  126. if (dist_b == 0 && p->next && p->next->val == val) {
  127. if (dist_a == 0 && x > 0) {
  128. if (prev != NULL && prev->val == val) {
  129. prev->count += p->next->count + 1;
  130. prev->next = p->next->next;
  131. link_dispose(map->token, (VOID_T *) p->next);
  132. link_dispose(map->token, (VOID_T *) p);
  133. return 0;
  134. }
  135. }
  136. }
  137. /* handle right edge merge */
  138. if (dist_b == 0 && p->next && p->next->val == val) {
  139. p->count--;
  140. p->next->count++;
  141. if (p->count == 0) {
  142. if (prev) {
  143. prev->next = p->next;
  144. }
  145. else {
  146. ((struct BMlink **)(map->data))[y] = p->next;
  147. }
  148. link_dispose(map->token, (VOID_T *) p);
  149. }
  150. return 0;
  151. }
  152. /* handle left edge merge */
  153. if (dist_a == 0 && x > 0) {
  154. if (prev != NULL && prev->val == val) {
  155. prev->count++;
  156. p->count--;
  157. if (p->count == 0) {
  158. prev->next = p->next;
  159. link_dispose(map->token, (VOID_T *) p);
  160. }
  161. return 0;
  162. }
  163. }
  164. /* if not on edge, have to add link for each side */
  165. if (dist_a > 0) {
  166. p->count = dist_a;
  167. p->val = Tval;
  168. p2 = (struct BMlink *)link_new(map->token);
  169. p2->next = p->next;
  170. p->next = p2;
  171. p = p2;
  172. }
  173. p->count = 1;
  174. p->val = val;
  175. if (dist_b > 0) {
  176. p2 = (struct BMlink *)link_new(map->token);
  177. p2->count = dist_b;
  178. p2->val = Tval;
  179. p2->next = p->next;
  180. p->next = p2;
  181. }
  182. return 0;
  183. }
  184. cur_x += p->count;
  185. prev = p;
  186. p = p->next;
  187. }
  188. return 0;
  189. }
  190. /*!
  191. * \brief
  192. *
  193. * Returns sparse bitmap value at location 'x'/'y'
  194. *
  195. * Returns value or -1 on error
  196. *
  197. * \param map
  198. * \param x
  199. * \param y
  200. * \return int
  201. */
  202. int BM_get_sparse(struct BM *map, int x, int y)
  203. {
  204. struct BMlink *p;
  205. int cur_x = 0;
  206. p = ((struct BMlink **)(map->data))[y];
  207. while (p != NULL) {
  208. if (cur_x + p->count > x)
  209. return (p->val);
  210. cur_x += p->count;
  211. p = p->next;
  212. }
  213. return -1;
  214. }
  215. /*!
  216. * \brief
  217. *
  218. * Returns size of sparse bitmap in bytes
  219. *
  220. * \param map
  221. * \return int
  222. */
  223. size_t BM_get_map_size_sparse(struct BM *map)
  224. {
  225. int i;
  226. size_t size;
  227. struct BMlink *p;
  228. size = (size_t) map->rows * sizeof(struct BMlink *);
  229. for (i = 0; i < map->rows; i++) {
  230. p = ((struct BMlink **)(map->data))[i];
  231. while (p != NULL) {
  232. size += sizeof(struct BMlink);
  233. p = p->next;
  234. }
  235. }
  236. return size;
  237. }
  238. /*!
  239. * \brief
  240. *
  241. * Debugging code to dump out structure of links
  242. *
  243. * Returns 0
  244. *
  245. * \param map
  246. * \return int
  247. */
  248. int BM_dump_map_sparse(struct BM *map)
  249. {
  250. int i;
  251. struct BMlink *p;
  252. for (i = 0; i < map->rows; i++) {
  253. p = ((struct BMlink **)(map->data))[i];
  254. while (p != NULL) {
  255. fprintf(stdout, "(%2d %2d) ", p->count, p->val);
  256. p = p->next;
  257. }
  258. fprintf(stdout, "\n");
  259. }
  260. return 0;
  261. }
  262. /*!
  263. * \brief
  264. *
  265. * Debugging code to dump out structure of links for single row
  266. *
  267. * Returns 0
  268. *
  269. * \param map
  270. * \param y
  271. * \return int
  272. */
  273. int BM_dump_map_row_sparse(struct BM *map, int y)
  274. {
  275. int i;
  276. struct BMlink *p;
  277. i = y;
  278. {
  279. p = ((struct BMlink **)(map->data))[i];
  280. while (p != NULL) {
  281. fprintf(stdout, "(%2d %2d) ", p->count, p->val);
  282. p = p->next;
  283. }
  284. fprintf(stdout, "\n");
  285. }
  286. return 0;
  287. }
  288. /*!
  289. * \brief
  290. *
  291. * Write sparse bitmap matrix out to disk file 'fp'.
  292. * NOTE: 'fp' must already be opened and later closed by user
  293. *
  294. * Returns 0 on success or -1 on error
  295. *
  296. * \param fp
  297. * \param map
  298. * \return int
  299. */
  300. int BM_file_write_sparse(FILE * fp, struct BM *map)
  301. {
  302. char c;
  303. int i, y;
  304. struct BMlink *p;
  305. int cnt;
  306. c = BM_MAGIC;
  307. fwrite(&c, sizeof(char), sizeof(char), fp);
  308. fwrite(BM_TEXT, BM_TEXT_LEN, sizeof(char), fp);
  309. c = BM_SPARSE;
  310. fwrite(&c, sizeof(char), sizeof(char), fp);
  311. fwrite(&(map->rows), sizeof(map->rows), sizeof(char), fp);
  312. fwrite(&(map->cols), sizeof(map->cols), sizeof(char), fp);
  313. for (y = 0; y < map->rows; y++) {
  314. /* first count number of links */
  315. p = ((struct BMlink **)(map->data))[y];
  316. cnt = 0;
  317. while (p != NULL) {
  318. cnt++;
  319. p = p->next;
  320. }
  321. i = cnt;
  322. fwrite(&i, sizeof(i), sizeof(char), fp);
  323. /* then write them out */
  324. p = ((struct BMlink **)(map->data))[y];
  325. while (p != NULL) {
  326. i = p->count;
  327. fwrite(&i, sizeof(i), sizeof(char), fp);
  328. i = p->val;
  329. fwrite(&i, sizeof(i), sizeof(char), fp);
  330. p = p->next;
  331. }
  332. }
  333. fflush(fp);
  334. return 0;
  335. }