bitmap.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. *
  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 == (map->data = (unsigned char *)calloc (map->bytes * y, sizeof(char))))
  62. return (NULL);
  63. map->rows = y;
  64. map->cols = x;
  65. map->sparse = 0;
  66. return map;
  67. }
  68. /*!
  69. * \brief Destroy bitmap and free all associated memory
  70. *
  71. * \param struct BM *map
  72. * \return int returns 0
  73. */
  74. int
  75. 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 int mode
  125. * \param int size
  126. * \return int
  127. */
  128. int
  129. BM_set_mode (int mode, int size)
  130. {
  131. int ret = 0;
  132. switch (mode) {
  133. case BM_FLAT:
  134. case BM_SPARSE:
  135. Mode = mode;
  136. default:
  137. fprintf (stderr, "BM_set_mode: Unknown mode: %d\n", mode);
  138. ret--;
  139. }
  140. if (size != 1)
  141. {
  142. fprintf (stderr, "BM_set_mode: Bad size: %d\n", size);
  143. ret--;
  144. }
  145. else
  146. Size = size;
  147. return ret;
  148. }
  149. /*!
  150. * \brief
  151. *
  152. * Sets bitmap value to 'val' at location 'x' 'y'
  153. *
  154. * Returns 0 on success
  155. *
  156. * \param map
  157. * \param x
  158. * \param y
  159. * \param val
  160. * \return int
  161. */
  162. int
  163. BM_set (struct BM *map, int x, int y, int val)
  164. {
  165. unsigned char byte;
  166. if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
  167. return 0;
  168. if (map->sparse)
  169. return BM_set_sparse (map, x, y, val);
  170. byte = 0x01 << BM_col_to_bit(x);
  171. if (val)
  172. map->data[BM_col_to_byte(x) + y * map->bytes] |= byte;
  173. else
  174. map->data[BM_col_to_byte(x) + y * map->bytes] &= ~byte;
  175. return 0;
  176. }
  177. /*!
  178. * \brief
  179. *
  180. * Gets 'val' from the bitmap
  181. *
  182. * Returns 0 or 1 on success or -1 on error
  183. *
  184. * \param map
  185. * \param x
  186. * \param y
  187. * \return int
  188. */
  189. int
  190. BM_get (struct BM *map, int x, int y)
  191. {
  192. unsigned char byte;
  193. if (x < 0 || x >= map->cols || y < 0 || y >= map->rows)
  194. return -1;
  195. if (map->sparse)
  196. return BM_get_sparse (map, x, y);
  197. byte = map->data[BM_col_to_byte(x) + y * map->bytes];
  198. return byte >> BM_col_to_bit(x) & 0x01;
  199. }
  200. /*!
  201. * \brief
  202. *
  203. * Returns size in bytes that bitmap is taking up.
  204. *
  205. * \param map
  206. * \return int
  207. */
  208. int
  209. BM_get_map_size (struct BM *map)
  210. {
  211. if (map->sparse)
  212. return BM_get_map_size_sparse(map);
  213. return map->bytes * map->rows;
  214. }
  215. /*!
  216. * \brief
  217. *
  218. * Write bitmap out to file
  219. *
  220. * Expects open file pointer 'fp' and existing map structure.
  221. * Caller is responsible to open and close 'fp'.
  222. *
  223. * Returns 0 or -1 on error
  224. *
  225. * \param fp
  226. * \param map
  227. * \return int
  228. */
  229. int BM_file_write (FILE *fp, struct BM *map)
  230. {
  231. char c;
  232. int i;
  233. if (map->sparse)
  234. return BM_file_write_sparse (fp, map);
  235. c = BM_MAGIC;
  236. fwrite (&c, sizeof(char), sizeof(char), fp);
  237. fwrite (BM_TEXT, BM_TEXT_LEN, sizeof(char), fp);
  238. c = BM_FLAT;
  239. fwrite (&c, sizeof(char), sizeof(char), fp);
  240. fwrite (&(map->rows), sizeof (map->rows), sizeof(char), fp);
  241. fwrite (&(map->cols), sizeof (map->cols), sizeof(char), fp);
  242. for (i = 0 ; i < map->rows ; i++)
  243. if(map->bytes != fwrite (&(map->data[i*map->bytes]), sizeof(char), map->bytes, fp))
  244. return -1;
  245. fflush (fp);
  246. return 0;
  247. }
  248. /*!
  249. * \brief
  250. *
  251. * Create map structure and load it from file
  252. *
  253. * 'fp' should previously been created by <b>BM_file_write()</b>
  254. *
  255. * Returns struct BM * or NULL on error
  256. *
  257. * \param fp
  258. * \return struct BM
  259. */
  260. struct BM *BM_file_read (FILE *fp)
  261. {
  262. struct BM *map;
  263. char c;
  264. char buf[BM_TEXT_LEN + 1];
  265. int i, y, n;
  266. struct BMlink *p = NULL, *p2;
  267. int cnt;
  268. if (NULL == (map = (struct BM *) malloc (sizeof (struct BM))))
  269. return (NULL);
  270. fread (&c, sizeof(char), sizeof(char), fp);
  271. if (c != BM_MAGIC)
  272. return NULL;
  273. fread (buf, BM_TEXT_LEN, sizeof(char), fp);
  274. fread (&c, sizeof(char), sizeof(char), fp);
  275. map->sparse = c;
  276. fread (&(map->rows), sizeof (map->rows), sizeof(char), fp);
  277. fread (&(map->cols), sizeof (map->cols), sizeof(char), fp);
  278. map->bytes = (map->cols+7)/8;
  279. if (map->sparse == BM_SPARSE)
  280. goto readsparse;
  281. if (NULL == (map->data = (unsigned char *) malloc (map->bytes * map->rows)))
  282. return (NULL);
  283. for (i = 0 ; i < map->rows ; i++)
  284. if(map->bytes != fread (&(map->data[i*map->bytes]), sizeof(char), map->bytes, fp))
  285. return NULL;
  286. return map;
  287. readsparse:
  288. link_set_chunk_size (500);
  289. map->token = link_init (sizeof (struct BMlink));
  290. if (NULL == (map->data = (unsigned char *)
  291. malloc ( sizeof (struct BMlink *) * map->rows )))
  292. return (NULL);
  293. for (y = 0 ; y < map->rows ; y++)
  294. {
  295. /* first get number of links */
  296. fread (&i, sizeof (i), sizeof(char), fp);
  297. cnt = i;
  298. /* then read them in */
  299. for (i = 0 ; i < cnt ; i++)
  300. {
  301. p2 = (struct BMlink *) link_new (map->token);
  302. if (i == 0)
  303. {
  304. ((struct BMlink **) (map->data))[y] = p2;
  305. p = p2;
  306. }
  307. else
  308. {
  309. p->next = p2;
  310. p = p2;
  311. }
  312. fread (&n, sizeof (n), sizeof(char), fp);
  313. p->count = n;
  314. fread (&n, sizeof (n), sizeof(char), fp);
  315. p->val = n;
  316. p->next = NULL;
  317. }
  318. }
  319. return map;
  320. }