sparse.c 7.3 KB

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