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