bitmap.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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) >> 3) /* x / 8 */
  39. #define BM_col_to_bit(x) ((x) & 7) /* 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. *
  45. * Bitmap is initialized to all zeros
  46. *
  47. * \param x x dimension
  48. * \param y y dimension
  49. *
  50. * \return pointer to struct BM
  51. * \return NULL on error
  52. */
  53. struct BM *BM_create(int x, int y)
  54. {
  55. struct BM *map;
  56. if (Mode == BM_SPARSE)
  57. return BM_create_sparse(x, y);
  58. if (NULL == (map = (struct BM *)malloc(sizeof(struct BM))))
  59. return (NULL);
  60. map->bytes = (x + 7) / 8;
  61. if (NULL ==
  62. (map->data = (unsigned char *)calloc(map->bytes * y, sizeof(char))))
  63. return (NULL);
  64. map->rows = y;
  65. map->cols = x;
  66. map->sparse = 0;
  67. return map;
  68. }
  69. /*!
  70. * \brief Destroy bitmap and free all associated memory
  71. *
  72. * \param map
  73. * \return int returns 0
  74. */
  75. int BM_destroy(struct BM *map)
  76. {
  77. if (map->sparse)
  78. return BM_destroy_sparse(map);
  79. free(map->data);
  80. free(map);
  81. return 0;
  82. }
  83. /*
  84. ** Caller can specify type of data structure to use for bitmap, as
  85. ** well as the size of the data values. Currently since this is
  86. ** the 'bitmap' library, size can ONLY have value 1.
  87. ** Size is number of bits of storage per cell.
  88. **
  89. ** Mode:
  90. ** BM_FLAT Your basic packed bitmap, eight values are stored per byte
  91. ** Thus you get a 1:8 compression over using char arrays
  92. ** and a 1:32 compression over using CELL arrays.
  93. **
  94. **
  95. ** BM_SPARSE Linked array of values. Much more efficient for large
  96. ** very sparse arrays. Slower access, especially for writing,
  97. ** but can save several orders of magnitude of memory on large
  98. ** bitmaps since size of FLAT bitmap is O(M*N)
  99. **
  100. **
  101. ** Returns 0 or negative on error;
  102. ** If error it will print a warning message to stderr and continue
  103. ** continue by running but will not change the option in error.
  104. */
  105. /*!
  106. * \brief
  107. *
  108. * Specify the type of data structure to use for bitmap.
  109. * 'mode' can be either BM_FLAT or BM_SPARSE:
  110. *
  111. * BM_FLAT is a basic packed bitmap - eight values stored per byte
  112. * thus creating a 1:8 compression over using char arrays and a
  113. * 1:32 compression over using CELL arrays.
  114. *
  115. * BM_SPARSE is a linked array of values. This is much more efficient
  116. * for large, very sparse arrays. It is slower to access, especially
  117. * for writing, but can save several orders of magnitude of memory on
  118. * large bitmaps.
  119. *
  120. * NOTE: At this time 'size' must be passed a value of 1
  121. *
  122. * returns 0 on success or -1 on error
  123. *
  124. * \param mode
  125. * \param size
  126. * \return int
  127. */
  128. int BM_set_mode(int mode, int size)
  129. {
  130. int ret = 0;
  131. switch (mode) {
  132. case BM_FLAT:
  133. case BM_SPARSE:
  134. Mode = mode;
  135. default:
  136. fprintf(stderr, "BM_set_mode: Unknown mode: %d\n", mode);
  137. ret--;
  138. }
  139. if (size != 1) {
  140. fprintf(stderr, "BM_set_mode: Bad size: %d\n", size);
  141. ret--;
  142. }
  143. else
  144. Size = size;
  145. return ret;
  146. }
  147. /*!
  148. * \brief
  149. *
  150. * Sets bitmap value to 'val' at location 'x' 'y'
  151. *
  152. * Returns 0 on success
  153. *
  154. * \param map
  155. * \param x
  156. * \param y
  157. * \param val
  158. * \return int
  159. */
  160. int 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 BM_get(struct BM *map, int x, int y)
  187. {
  188. unsigned char byte;
  189. if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
  190. return -1;
  191. if (map->sparse)
  192. return BM_get_sparse(map, x, y);
  193. byte = map->data[BM_col_to_byte(x) + y * map->bytes];
  194. return byte >> BM_col_to_bit(x) & 0x01;
  195. }
  196. /*!
  197. * \brief
  198. *
  199. * Returns size in bytes that bitmap is taking up.
  200. *
  201. * \param map
  202. * \return int
  203. */
  204. size_t BM_get_map_size(struct BM *map)
  205. {
  206. if (map->sparse)
  207. return BM_get_map_size_sparse(map);
  208. return (size_t) map->bytes * map->rows;
  209. }
  210. /*!
  211. * \brief
  212. *
  213. * Write bitmap out to file
  214. *
  215. * Expects open file pointer 'fp' and existing map structure.
  216. * Caller is responsible to open and close 'fp'.
  217. *
  218. * Returns 0 or -1 on error
  219. *
  220. * \param fp
  221. * \param map
  222. * \return int
  223. */
  224. int BM_file_write(FILE * fp, struct BM *map)
  225. {
  226. char c;
  227. int i;
  228. if (map->sparse)
  229. return BM_file_write_sparse(fp, map);
  230. c = BM_MAGIC;
  231. fwrite(&c, sizeof(char), sizeof(char), fp);
  232. fwrite(BM_TEXT, BM_TEXT_LEN, sizeof(char), fp);
  233. c = BM_FLAT;
  234. fwrite(&c, sizeof(char), sizeof(char), fp);
  235. fwrite(&(map->rows), sizeof(map->rows), sizeof(char), fp);
  236. fwrite(&(map->cols), sizeof(map->cols), sizeof(char), fp);
  237. for (i = 0; i < map->rows; i++)
  238. if (map->bytes !=
  239. fwrite(&(map->data[i * map->bytes]), sizeof(char), map->bytes,
  240. 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 !=
  282. fread(&(map->data[i * map->bytes]), sizeof(char), map->bytes, fp))
  283. return NULL;
  284. return map;
  285. readsparse:
  286. link_set_chunk_size(500);
  287. map->token = link_init(sizeof(struct BMlink));
  288. if (NULL == (map->data = (unsigned char *)
  289. malloc(sizeof(struct BMlink *) * map->rows)))
  290. return (NULL);
  291. for (y = 0; y < map->rows; y++) {
  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. p2 = (struct BMlink *)link_new(map->token);
  298. if (i == 0) {
  299. ((struct BMlink **)(map->data))[y] = p2;
  300. p = p2;
  301. }
  302. else {
  303. p->next = p2;
  304. p = p2;
  305. }
  306. fread(&n, sizeof(n), sizeof(char), fp);
  307. p->count = n;
  308. fread(&n, sizeof(n), sizeof(char), fp);
  309. p->val = n;
  310. p->next = NULL;
  311. }
  312. }
  313. return map;
  314. }