bitmap.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* Copyright (C) 2001-2017 Peter Selinger.
  2. This file is part of Potrace. It is free software and it is covered
  3. by the GNU General Public License. See the file COPYING for details. */
  4. #ifndef BITMAP_H
  5. #define BITMAP_H
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <stddef.h>
  10. /* The bitmap type is defined in potracelib.h */
  11. #include "potracelib.h"
  12. /* The present file defines some convenient macros and static inline
  13. functions for accessing bitmaps. Since they only produce inline
  14. code, they can be conveniently shared by the library and frontends,
  15. if desired */
  16. /* ---------------------------------------------------------------------- */
  17. /* some measurements */
  18. #define BM_WORDSIZE ((int)sizeof(potrace_word))
  19. #define BM_WORDBITS (8*BM_WORDSIZE)
  20. #define BM_HIBIT (((potrace_word)1)<<(BM_WORDBITS-1))
  21. #define BM_ALLBITS (~(potrace_word)0)
  22. /* macros for accessing pixel at index (x,y). U* macros omit the
  23. bounds check. */
  24. #define bm_scanline(bm, y) ((bm)->map + (ptrdiff_t)(y)*(ptrdiff_t)(bm)->dy)
  25. #define bm_index(bm, x, y) (&bm_scanline(bm, y)[(x)/BM_WORDBITS])
  26. #define bm_mask(x) (BM_HIBIT >> ((x) & (BM_WORDBITS-1)))
  27. #define bm_range(x, a) ((int)(x) >= 0 && (int)(x) < (a))
  28. #define bm_safe(bm, x, y) (bm_range(x, (bm)->w) && bm_range(y, (bm)->h))
  29. #define BM_UGET(bm, x, y) ((*bm_index(bm, x, y) & bm_mask(x)) != 0)
  30. #define BM_USET(bm, x, y) (*bm_index(bm, x, y) |= bm_mask(x))
  31. #define BM_UCLR(bm, x, y) (*bm_index(bm, x, y) &= ~bm_mask(x))
  32. #define BM_UINV(bm, x, y) (*bm_index(bm, x, y) ^= bm_mask(x))
  33. #define BM_UPUT(bm, x, y, b) ((b) ? BM_USET(bm, x, y) : BM_UCLR(bm, x, y))
  34. #define BM_GET(bm, x, y) (bm_safe(bm, x, y) ? BM_UGET(bm, x, y) : 0)
  35. #define BM_SET(bm, x, y) (bm_safe(bm, x, y) ? BM_USET(bm, x, y) : 0)
  36. #define BM_CLR(bm, x, y) (bm_safe(bm, x, y) ? BM_UCLR(bm, x, y) : 0)
  37. #define BM_INV(bm, x, y) (bm_safe(bm, x, y) ? BM_UINV(bm, x, y) : 0)
  38. #define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0)
  39. /* calculate the size, in bytes, required for the data area of a
  40. bitmap of the given dy and h. Assume h >= 0. Return -1 if the size
  41. does not fit into the ptrdiff_t type. */
  42. static inline ptrdiff_t getsize(int dy, int h) {
  43. ptrdiff_t size;
  44. if (dy < 0) {
  45. dy = -dy;
  46. }
  47. size = (ptrdiff_t)dy * (ptrdiff_t)h * (ptrdiff_t)BM_WORDSIZE;
  48. /* check for overflow error */
  49. if (size < 0 || (h != 0 && dy != 0 && size / h / dy != BM_WORDSIZE)) {
  50. return -1;
  51. }
  52. return size;
  53. }
  54. /* return the size, in bytes, of the data area of the bitmap. Return
  55. -1 if the size does not fit into the ptrdiff_t type; however, this
  56. cannot happen if the bitmap is well-formed, i.e., if created with
  57. bm_new or bm_dup. */
  58. static inline ptrdiff_t bm_size(const potrace_bitmap_t *bm) {
  59. return getsize(bm->dy, bm->h);
  60. }
  61. /* calculate the base address of the bitmap data. Assume that the
  62. bitmap is well-formed, i.e., its size fits into the ptrdiff_t type.
  63. This is the case if created with bm_new or bm_dup. The base address
  64. may differ from bm->map if dy is negative */
  65. static inline potrace_word *bm_base(const potrace_bitmap_t *bm) {
  66. int dy = bm->dy;
  67. if (dy >= 0 || bm->h == 0) {
  68. return bm->map;
  69. } else {
  70. return bm_scanline(bm, bm->h - 1);
  71. }
  72. }
  73. /* free the given bitmap. Leaves errno untouched. */
  74. static inline void bm_free(potrace_bitmap_t *bm) {
  75. if (bm && bm->map) {
  76. free(bm_base(bm));
  77. }
  78. free(bm);
  79. }
  80. /* return new bitmap initialized to 0. NULL with errno on error.
  81. Assumes w, h >= 0. */
  82. static inline potrace_bitmap_t *bm_new(int w, int h) {
  83. potrace_bitmap_t *bm;
  84. int dy = w == 0 ? 0 : (w - 1) / BM_WORDBITS + 1;
  85. ptrdiff_t size;
  86. size = getsize(dy, h);
  87. if (size < 0) {
  88. errno = ENOMEM;
  89. return NULL;
  90. }
  91. if (size == 0) {
  92. size = 1; /* make sure calloc() doesn't return NULL */
  93. }
  94. bm = (potrace_bitmap_t *) malloc(sizeof(potrace_bitmap_t));
  95. if (!bm) {
  96. return NULL;
  97. }
  98. bm->w = w;
  99. bm->h = h;
  100. bm->dy = dy;
  101. bm->map = (potrace_word *) calloc(1, size);
  102. if (!bm->map) {
  103. free(bm);
  104. return NULL;
  105. }
  106. return bm;
  107. }
  108. /* clear the given bitmap. Set all bits to c. Assumes a well-formed
  109. bitmap. */
  110. static inline void bm_clear(potrace_bitmap_t *bm, int c) {
  111. /* Note: if the bitmap was created with bm_new, then it is
  112. guaranteed that size will fit into the ptrdiff_t type. */
  113. ptrdiff_t size = bm_size(bm);
  114. memset(bm_base(bm), c ? -1 : 0, size);
  115. }
  116. /* duplicate the given bitmap. Return NULL on error with errno
  117. set. Assumes a well-formed bitmap. */
  118. static inline potrace_bitmap_t *bm_dup(const potrace_bitmap_t *bm) {
  119. potrace_bitmap_t *bm1 = bm_new(bm->w, bm->h);
  120. int y;
  121. if (!bm1) {
  122. return NULL;
  123. }
  124. for (y=0; y < bm->h; y++) {
  125. memcpy(bm_scanline(bm1, y), bm_scanline(bm, y), (size_t)bm1->dy * (size_t)BM_WORDSIZE);
  126. }
  127. return bm1;
  128. }
  129. /* invert the given bitmap. */
  130. static inline void bm_invert(potrace_bitmap_t *bm) {
  131. int dy = bm->dy;
  132. int y;
  133. int i;
  134. potrace_word *p;
  135. if (dy < 0) {
  136. dy = -dy;
  137. }
  138. for (y=0; y < bm->h; y++) {
  139. p = bm_scanline(bm, y);
  140. for (i=0; i < dy; i++) {
  141. p[i] ^= BM_ALLBITS;
  142. }
  143. }
  144. }
  145. /* turn the given bitmap upside down. This does not move the bitmap
  146. data or change the bm_base() address. */
  147. static inline void bm_flip(potrace_bitmap_t *bm) {
  148. int dy = bm->dy;
  149. if (bm->h == 0 || bm->h == 1) {
  150. return;
  151. }
  152. bm->map = bm_scanline(bm, bm->h - 1);
  153. bm->dy = -dy;
  154. }
  155. /* resize the bitmap to the given new height. The bitmap data remains
  156. bottom-aligned (truncated at the top) when dy >= 0 and top-aligned
  157. (truncated at the bottom) when dy < 0. Return 0 on success, or 1 on
  158. error with errno set. If the new height is <= the old one, no error
  159. should occur. If the new height is larger, the additional bitmap
  160. data is *not* initialized. */
  161. static inline int bm_resize(potrace_bitmap_t *bm, int h) {
  162. int dy = bm->dy;
  163. ptrdiff_t newsize;
  164. potrace_word *newmap;
  165. if (dy < 0) {
  166. bm_flip(bm);
  167. }
  168. newsize = getsize(dy, h);
  169. if (newsize < 0) {
  170. errno = ENOMEM;
  171. goto error;
  172. }
  173. if (newsize == 0) {
  174. newsize = 1; /* make sure realloc() doesn't return NULL */
  175. }
  176. newmap = (potrace_word *)realloc(bm->map, newsize);
  177. if (newmap == NULL) {
  178. goto error;
  179. }
  180. bm->map = newmap;
  181. bm->h = h;
  182. if (dy < 0) {
  183. bm_flip(bm);
  184. }
  185. return 0;
  186. error:
  187. if (dy < 0) {
  188. bm_flip(bm);
  189. }
  190. return 1;
  191. }
  192. #endif /* BITMAP_H */