| 1 |
#include "bitmaps.h" |
| 2 |
#include <assert.h> |
| 3 |
#include <string.h> |
| 4 |
#include <stdio.h> |
| 5 |
|
| 6 |
|
| 7 |
unsigned char **allocate_bitmap(int w, int h) |
| 8 |
{ |
| 9 |
unsigned char *data, **result; |
| 10 |
int i; |
| 11 |
|
| 12 |
assert(w > 0 && h > 0); |
| 13 |
|
| 14 |
data = MALLOC(unsigned char, w * h); |
| 15 |
if (!data) return NULL; |
| 16 |
|
| 17 |
result = MALLOC(unsigned char *, h); |
| 18 |
if (!result) |
| 19 |
{ |
| 20 |
FREE(data); |
| 21 |
return NULL; |
| 22 |
} |
| 23 |
|
| 24 |
for (i = 0; i < h; i++) |
| 25 |
{ |
| 26 |
result[i] = data + w * i; |
| 27 |
} |
| 28 |
|
| 29 |
return result; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
void free_bitmap(unsigned char **p) |
| 34 |
{ |
| 35 |
assert(p); |
| 36 |
FREE(p[0]); |
| 37 |
FREE(p); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
unsigned char **allocate_bitmap_with_margins(int w, int h) |
| 42 |
{ |
| 43 |
unsigned char **result = allocate_bitmap(w + 2, h + 2); |
| 44 |
int y; |
| 45 |
|
| 46 |
if (!result) |
| 47 |
return NULL; |
| 48 |
|
| 49 |
/* set the `result' array so that it points to buffer rows (plus margin) */ |
| 50 |
for (y = 0; y < h + 2; y++) |
| 51 |
{ |
| 52 |
result[y]++; |
| 53 |
} |
| 54 |
result++; /* now the first byte in the buffer is result[-1][-1] */ |
| 55 |
return result; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
/* Allocate a w * h bitmap with margins of 1 pixels at each side. |
| 60 |
* Copy `pixels' there and clear the margins. |
| 61 |
*/ |
| 62 |
unsigned char **provide_margins(unsigned char **pixels, |
| 63 |
int w, int h, |
| 64 |
int make_it_0_or_1) |
| 65 |
{ |
| 66 |
unsigned char **result = allocate_bitmap_with_margins(w, h); |
| 67 |
int y; |
| 68 |
|
| 69 |
if (!result) |
| 70 |
return NULL; |
| 71 |
|
| 72 |
/* clear the top and the bottom row */ |
| 73 |
memset(result[-1] - 1, 0, w + 2); |
| 74 |
memset(result[h] - 1, 0, w + 2); |
| 75 |
|
| 76 |
for (y = 0; y < h; y++) |
| 77 |
{ |
| 78 |
unsigned char *src_row = pixels[y]; |
| 79 |
unsigned char *dst_row = result[y]; |
| 80 |
|
| 81 |
/* clear left and right margin */ |
| 82 |
dst_row[-1] = 0; |
| 83 |
dst_row[w] = 0; |
| 84 |
|
| 85 |
if (!make_it_0_or_1) |
| 86 |
memcpy(dst_row, src_row, w); |
| 87 |
else |
| 88 |
{ |
| 89 |
int x; |
| 90 |
for (x = 0; x < w; x++) |
| 91 |
dst_row[x] = (src_row[x] ? 1 : 0); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return result; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
/* Simply undo the work of allocate_bitmap_with_margin(). */ |
| 100 |
void free_bitmap_with_margins(unsigned char **pixels) |
| 101 |
{ |
| 102 |
assert(pixels); |
| 103 |
FREE(&pixels[-1][-1]); /* because we have both left and top margins of 1 pixel */ |
| 104 |
FREE(pixels - 1); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
void assign_bitmap(unsigned char **dst, unsigned char **src, int w, int h) |
| 109 |
{ |
| 110 |
int i; |
| 111 |
for (i = 0; i < h; i++) |
| 112 |
memcpy(dst[i], src[i], w); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
unsigned char **copy_bitmap(unsigned char **src, int w, int h) |
| 117 |
{ |
| 118 |
unsigned char **result = allocate_bitmap(w, h); |
| 119 |
assign_bitmap(result, src, w, h); |
| 120 |
return result; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
void strip_endpoints_4(unsigned char **result, unsigned char **pixels, int w, int h) |
| 125 |
{ |
| 126 |
int x, y; |
| 127 |
|
| 128 |
assert(result != pixels); |
| 129 |
|
| 130 |
for (y = 0; y < h; y++) for (x = 0; x < w; x++) if (pixels[y][x]) |
| 131 |
{ |
| 132 |
int degree = pixels[y - 1][x] + pixels[y + 1][x] |
| 133 |
+ pixels[y][x - 1] + pixels[y][x + 1]; |
| 134 |
|
| 135 |
if(degree != 1) |
| 136 |
result[y][x] = 1; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void strip_endpoints_8(unsigned char **result, unsigned char **pixels, int w, int h) |
| 141 |
{ |
| 142 |
int x, y; |
| 143 |
|
| 144 |
assert(result != pixels); |
| 145 |
|
| 146 |
for (y = 0; y < h; y++) for (x = 0; x < w; x++) if (pixels[y][x]) |
| 147 |
{ |
| 148 |
int degree = pixels[y - 1][x - 1] + pixels[y - 1][x] + pixels[y - 1][x + 1] |
| 149 |
+ pixels[y ][x - 1] + pixels[y ][x + 1] |
| 150 |
+ pixels[y + 1][x - 1] + pixels[y + 1][x] + pixels[y + 1][x + 1]; |
| 151 |
|
| 152 |
if(degree != 1) |
| 153 |
result[y][x] = 1; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
void print_bitmap(unsigned char **pixels, int w, int h) |
| 159 |
{ |
| 160 |
int x, y; |
| 161 |
for (y = 0; y < h; y++) |
| 162 |
{ |
| 163 |
for (x = 0; x < w; x++) |
| 164 |
{ |
| 165 |
putchar(pixels[y][x] ? '@' : '.'); |
| 166 |
} |
| 167 |
putchar('\n'); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
void make_bitmap_0_or_1(unsigned char **pixels, int w, int h) |
| 173 |
{ |
| 174 |
int x, y; |
| 175 |
|
| 176 |
for (y = 0; y < h; y++) |
| 177 |
{ |
| 178 |
unsigned char *row = pixels[y]; |
| 179 |
|
| 180 |
for (x = 0; x < w; x++) |
| 181 |
row[x] = ( row[x] ? 1 : 0 ); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
void invert_bitmap(unsigned char **pixels, int w, int h, int first_make_it_0_or_1) |
| 187 |
{ |
| 188 |
int x, y; |
| 189 |
|
| 190 |
for (y = 0; y < h; y++) |
| 191 |
{ |
| 192 |
unsigned char *row = pixels[y]; |
| 193 |
|
| 194 |
if (first_make_it_0_or_1) |
| 195 |
{ |
| 196 |
for (x = 0; x < w; x++) |
| 197 |
row[x] = ( row[x] ? 0 : 1 ); |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
for (x = 0; x < w; x++) |
| 202 |
row[x] = 1 - row[x]; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
unsigned char **allocate_bitmap_with_white_margins(int w, int h) |
| 209 |
{ |
| 210 |
unsigned char **result = allocate_bitmap_with_margins(w, h); |
| 211 |
int y; |
| 212 |
|
| 213 |
memset(result[-1] - 1, 0, w + 2); |
| 214 |
memset(result[ h] - 1, 0, w + 2); |
| 215 |
for (y = 0; y < h; y++) |
| 216 |
{ |
| 217 |
result[y][-1] = 0; |
| 218 |
result[y][w] = 0; |
| 219 |
} |
| 220 |
|
| 221 |
return result; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
void clear_bitmap(unsigned char **pixels, int w, int h) |
| 226 |
{ |
| 227 |
int y; |
| 228 |
|
| 229 |
for (y = 0; y < h; y++) |
| 230 |
memset(pixels[y], 0, w); |
| 231 |
} |