Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
bitmap.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5/* Copyright (C) 2001-2015 Peter Selinger.
6 This file is part of Potrace. It is free software and it is covered
7 by the GNU General Public License. See the file COPYING for details. */
8
9#ifndef BITMAP_H
10#define BITMAP_H
11
12#include <cstring>
13#include <cstdlib>
14#include <cerrno>
15#include <cstddef>
16
17/* The bitmap type is defined in potracelib.h */
18#include "potracelib.h"
19
20/* The present file defines some convenient macros and static inline
21 functions for accessing bitmaps. Since they only produce inline
22 code, they can be conveniently shared by the library and frontends,
23 if desired */
24
25/* ---------------------------------------------------------------------- */
26/* some measurements */
27
28#define BM_WORDSIZE ((int)sizeof(potrace_word))
29#define BM_WORDBITS (8*BM_WORDSIZE)
30#define BM_HIBIT (((potrace_word)1)<<(BM_WORDBITS-1))
31#define BM_ALLBITS (~(potrace_word)0)
32
33/* macros for accessing pixel at index (x,y). U* macros omit the
34 bounds check. */
35
36#define bm_scanline(bm, y) ((bm)->map + (ptrdiff_t)(y)*(ptrdiff_t)(bm)->dy)
37#define bm_index(bm, x, y) (&bm_scanline(bm, y)[(x)/BM_WORDBITS])
38#define bm_mask(x) (BM_HIBIT >> ((x) & (BM_WORDBITS-1)))
39#define bm_range(x, a) ((int)(x) >= 0 && (int)(x) < (a))
40#define bm_safe(bm, x, y) (bm_range(x, (bm)->w) && bm_range(y, (bm)->h))
41#define BM_UGET(bm, x, y) ((*bm_index(bm, x, y) & bm_mask(x)) != 0)
42#define BM_USET(bm, x, y) (*bm_index(bm, x, y) |= bm_mask(x))
43#define BM_UCLR(bm, x, y) (*bm_index(bm, x, y) &= ~bm_mask(x))
44#define BM_UINV(bm, x, y) (*bm_index(bm, x, y) ^= bm_mask(x))
45#define BM_UPUT(bm, x, y, b) ((b) ? BM_USET(bm, x, y) : BM_UCLR(bm, x, y))
46#define BM_GET(bm, x, y) (bm_safe(bm, x, y) ? BM_UGET(bm, x, y) : 0)
47#define BM_SET(bm, x, y) (bm_safe(bm, x, y) ? BM_USET(bm, x, y) : 0)
48#define BM_CLR(bm, x, y) (bm_safe(bm, x, y) ? BM_UCLR(bm, x, y) : 0)
49#define BM_INV(bm, x, y) (bm_safe(bm, x, y) ? BM_UINV(bm, x, y) : 0)
50#define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0)
51
52/* free the given bitmap. Leaves errno untouched. */
53static inline void bm_free(potrace_bitmap_t *bm) {
54 if (bm) {
55 free(bm->map);
56 }
57 free(bm);
58}
59
60/* return new un-initialized bitmap. NULL with errno on error.
61 Assumes w, h >= 0. */
62static inline potrace_bitmap_t *bm_new(int w, int h) {
63 potrace_bitmap_t *bm;
64 int dy = w == 0 ? 0 : (w - 1) / BM_WORDBITS + 1;
65 ptrdiff_t size = (ptrdiff_t)dy * (ptrdiff_t)h * (ptrdiff_t)BM_WORDSIZE;
66
67 /* check for overflow error */
68 if (size < 0 || (h != 0 && dy != 0 && size / h / dy != BM_WORDSIZE)) {
69 errno = ENOMEM;
70 return nullptr;
71 }
72
73 bm = (potrace_bitmap_t *) malloc(sizeof(potrace_bitmap_t));
74 if (!bm) {
75 return nullptr;
76 }
77 bm->w = w;
78 bm->h = h;
79 bm->dy = dy;
80 bm->map = (potrace_word *) malloc(size);
81 if (!bm->map) {
82 g_warning("bm_new: can not allocate memory for bitmap (%td).", size);
83 free(bm);
84 return nullptr;
85 }
86 return bm;
87}
88
89/* clear the given bitmap. Set all bits to c. */
90static inline void bm_clear(potrace_bitmap_t *bm, int c) {
91 /* Note: if the bitmap was created with bm_new, then it is
92 guaranteed that size will fit into the ptrdiff_t type. */
93 ptrdiff_t size = (ptrdiff_t)bm->dy * (ptrdiff_t)bm->h * (ptrdiff_t)BM_WORDSIZE;
94 memset(bm->map, c ? -1 : 0, size);
95}
96
97/* duplicate the given bitmap. Return NULL on error with errno set. */
98static inline potrace_bitmap_t *bm_dup(const potrace_bitmap_t *bm) {
99 potrace_bitmap_t *bm1 = bm_new(bm->w, bm->h);
100 ptrdiff_t size = (ptrdiff_t)bm->dy * (ptrdiff_t)bm->h * (ptrdiff_t)BM_WORDSIZE;
101 if (!bm1) {
102 return nullptr;
103 }
104 memcpy(bm1->map, bm->map, size);
105 return bm1;
106}
107
108/* invert the given bitmap. */
109static inline void bm_invert(potrace_bitmap_t *bm) {
110 ptrdiff_t i;
111 ptrdiff_t size = (ptrdiff_t)bm->dy * (ptrdiff_t)bm->h;
112
113 for (i = 0; i < size; i++) {
114 bm->map[i] ^= BM_ALLBITS;
115 }
116}
117
118#endif /* BITMAP_H */
const double w
Definition conic-4.cpp:19
Geom::IntPoint size
double c[8][4]
static void bm_invert(potrace_bitmap_t *bm)
Definition bitmap.h:109
static potrace_bitmap_t * bm_new(int w, int h)
Definition bitmap.h:62
static void bm_free(potrace_bitmap_t *bm)
Definition bitmap.h:53
static potrace_bitmap_t * bm_dup(const potrace_bitmap_t *bm)
Definition bitmap.h:98
static void bm_clear(potrace_bitmap_t *bm, int c)
Definition bitmap.h:90