| 1 |
/* thin-image.c: thin binary image |
| 2 |
|
| 3 |
Copyright (C) 2001, 2002 Martin Weber |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Lesser General Public License |
| 7 |
as published by the Free Software Foundation; either version 2.1 of |
| 8 |
the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, but |
| 11 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public |
| 16 |
License along with this library; if not, write to the Free Software |
| 17 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 |
USA. */ |
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "mallocvar.h" |
| 25 |
|
| 26 |
#include "thin-image.h" |
| 27 |
#include "logreport.h" |
| 28 |
#include "message.h" |
| 29 |
#include "bitmap.h" |
| 30 |
|
| 31 |
#define PIXEL_SET(p, new) ((void)memcpy((p), (new), sizeof(Pixel))) |
| 32 |
#define PIXEL_EQUAL(p1, p2) \ |
| 33 |
((p1)[0] == (p2)[0] && (p1)[1] == (p2)[1] && (p1)[2] == (p2)[2]) |
| 34 |
|
| 35 |
|
| 36 |
typedef unsigned char Pixel[3]; /* RGB pixel data type */ |
| 37 |
|
| 38 |
|
| 39 |
void thin3(bitmap_type *image, Pixel colour); |
| 40 |
void thin1(bitmap_type *image, unsigned char colour); |
| 41 |
|
| 42 |
|
| 43 |
/* -------------------------------- ThinImage - Thin binary image. --------------------------- * |
| 44 |
* |
| 45 |
* Description: |
| 46 |
* Thins the supplied binary image using Rosenfeld's parallel |
| 47 |
* thinning algorithm. |
| 48 |
* |
| 49 |
* On Entry: |
| 50 |
* image = Image to thin. |
| 51 |
* |
| 52 |
* -------------------------------------------------------------------------------------------- */ |
| 53 |
|
| 54 |
|
| 55 |
/* Direction masks: */ |
| 56 |
/* N S W E */ |
| 57 |
static unsigned int masks[] = { 0200, 0002, 0040, 0010 }; |
| 58 |
|
| 59 |
/* True if pixel neighbor map indicates the pixel is 8-simple and */ |
| 60 |
/* not an end point and thus can be deleted. The neighborhood */ |
| 61 |
/* map is defined as an integer of bits abcdefghi with a non-zero */ |
| 62 |
/* bit representing a non-zero pixel. The bit assignment for the */ |
| 63 |
/* neighborhood is: */ |
| 64 |
/* */ |
| 65 |
/* a b c */ |
| 66 |
/* d e f */ |
| 67 |
/* g h i */ |
| 68 |
|
| 69 |
static unsigned char todelete[512] = { |
| 70 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 71 |
0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, |
| 72 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 73 |
0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, |
| 74 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 75 |
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, |
| 76 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 77 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, |
| 78 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 79 |
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, |
| 80 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 81 |
1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 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0, 0, 0, 0, 0, 0, |
| 101 |
1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; |
| 102 |
|
| 103 |
static pixel background; |
| 104 |
|
| 105 |
|
| 106 |
void |
| 107 |
thin_image(bitmap_type *image, bool bgSpec, pixel bg, |
| 108 |
at_exception_type * exp) |
| 109 |
{ |
| 110 |
/* This is nasty as we need to call thin once for each |
| 111 |
* colour in the image the way I do this is to keep a second |
| 112 |
* copy of the bitmap and to use this to keep |
| 113 |
* track of which colours have not yet been processed, |
| 114 |
* trades time for pathological case memory.....*/ |
| 115 |
long m, n, num_pixels; |
| 116 |
bitmap_type bm; |
| 117 |
unsigned int const spp = image->np; |
| 118 |
unsigned int const width = image->width; |
| 119 |
unsigned int const height = image->height; |
| 120 |
|
| 121 |
if (bgSpec) |
| 122 |
background = bg; |
| 123 |
else |
| 124 |
PPM_ASSIGN(background, 255, 255, 255); |
| 125 |
|
| 126 |
/* Clone the image */ |
| 127 |
bm.height = image->height; |
| 128 |
bm.width = image->width; |
| 129 |
bm.np = image->np; |
| 130 |
MALLOCARRAY(bm.bitmap, height * width * spp); |
| 131 |
if (bm.bitmap == NULL) |
| 132 |
pm_error("Unable to get memory for thin image bitmap clone"); |
| 133 |
memcpy(bm.bitmap, image->bitmap, height * width * spp); |
| 134 |
|
| 135 |
num_pixels = height * width; |
| 136 |
switch (spp) |
| 137 |
{ |
| 138 |
case 3: |
| 139 |
{ |
| 140 |
Pixel *ptr = (Pixel*)bm.bitmap; |
| 141 |
Pixel bg_color; |
| 142 |
bg_color[0] = PPM_GETR(background); |
| 143 |
bg_color[1] = PPM_GETG(background); |
| 144 |
bg_color[2] = PPM_GETB(background); |
| 145 |
|
| 146 |
for (n = num_pixels - 1; n >= 0L; --n) |
| 147 |
{ |
| 148 |
Pixel p; |
| 149 |
|
| 150 |
PIXEL_SET(p, ptr[n]); |
| 151 |
if (!PIXEL_EQUAL(p, bg_color)) |
| 152 |
{ |
| 153 |
/* we have a new colour in the image */ |
| 154 |
LOG3("Thinning colour (%x, %x, %x)\n", p[0], p[1], p[2]); |
| 155 |
for (m = n - 1; m >= 0L; --m) |
| 156 |
{ |
| 157 |
if (PIXEL_EQUAL(ptr[m], p)) |
| 158 |
PIXEL_SET(ptr[m], bg_color); |
| 159 |
} |
| 160 |
thin3(image, p); |
| 161 |
} |
| 162 |
} |
| 163 |
break; |
| 164 |
} |
| 165 |
|
| 166 |
case 1: |
| 167 |
{ |
| 168 |
unsigned char * const ptr = bm.bitmap; |
| 169 |
unsigned char bg_color; |
| 170 |
|
| 171 |
if (PPM_ISGRAY(background)) |
| 172 |
bg_color = PPM_GETR(background); |
| 173 |
else |
| 174 |
bg_color = ppm_luminosity(background); |
| 175 |
|
| 176 |
for (n = num_pixels - 1; n >= 0L; --n) |
| 177 |
{ |
| 178 |
unsigned char c = ptr[n]; |
| 179 |
if (c != bg_color) |
| 180 |
{ |
| 181 |
LOG1 ("Thinning colour %x\n", c); |
| 182 |
for (m = n - 1; m >= 0L; --m) |
| 183 |
if (ptr[m] == c) ptr[m] = bg_color; |
| 184 |
thin1(image, c); |
| 185 |
} |
| 186 |
} |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
default: |
| 191 |
{ |
| 192 |
LOG1 ("thin_image: Don't know how to interpret %u-plane images", spp); |
| 193 |
at_exception_fatal(exp, "thin_image: wrong plane images are passed"); |
| 194 |
goto cleanup; |
| 195 |
} |
| 196 |
} |
| 197 |
cleanup: |
| 198 |
free (bm.bitmap); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
void thin3(bitmap_type *image, Pixel colour) |
| 204 |
{ |
| 205 |
Pixel *ptr, *y_ptr, *y1_ptr; |
| 206 |
Pixel bg_color; |
| 207 |
unsigned int xsize, ysize; /* Image resolution */ |
| 208 |
unsigned int x, y; /* Pixel location */ |
| 209 |
unsigned int i; /* Pass index */ |
| 210 |
unsigned int pc = 0; /* Pass count */ |
| 211 |
unsigned int count = 1; /* Deleted pixel count */ |
| 212 |
unsigned int p, q; /* Neighborhood maps of adjacent*/ |
| 213 |
/* cells */ |
| 214 |
unsigned char *qb; /* Neighborhood maps of previous*/ |
| 215 |
/* scanline */ |
| 216 |
unsigned int m; /* Deletion direction mask */ |
| 217 |
|
| 218 |
bg_color[0] = PPM_GETR(background); |
| 219 |
bg_color[1] = PPM_GETG(background); |
| 220 |
bg_color[2] = PPM_GETB(background); |
| 221 |
|
| 222 |
LOG (" Thinning image.....\n "); |
| 223 |
xsize = image->width; |
| 224 |
ysize = image->height; |
| 225 |
MALLOCARRAY_NOFAIL(qb, xsize); |
| 226 |
qb[xsize-1] = 0; /* Used for lower-right pixel */ |
| 227 |
ptr = (Pixel*)image->bitmap; |
| 228 |
|
| 229 |
while ( count ) { /* Scan image while deletions */ |
| 230 |
pc++; |
| 231 |
count = 0; |
| 232 |
|
| 233 |
for ( i = 0 ; i < 4 ; i++ ) { |
| 234 |
|
| 235 |
m = masks[i]; |
| 236 |
|
| 237 |
/* Build initial previous scan buffer. */ |
| 238 |
p = PIXEL_EQUAL(ptr[0], colour); |
| 239 |
for ( x = 0 ; x < xsize-1 ; x++ ) |
| 240 |
qb[x] = (unsigned char) (p = ((p<<1)&0006) | (unsigned int) PIXEL_EQUAL(ptr[x+1], |
| 241 |
colour)); |
| 242 |
|
| 243 |
/* Scan image for pixel deletion candidates. */ |
| 244 |
y_ptr = ptr; y1_ptr = ptr + xsize; |
| 245 |
for (y = 0; y < ysize - 1; y++, y_ptr += xsize, y1_ptr += xsize) |
| 246 |
{ |
| 247 |
q = qb[0]; |
| 248 |
p = ((q<<2)&0330) | (unsigned int) PIXEL_EQUAL(y1_ptr[0], colour); |
| 249 |
|
| 250 |
for ( x = 0 ; x < xsize-1 ; x++ ) { |
| 251 |
q = qb[x]; |
| 252 |
p = ((p<<1)&0666) | ((q<<3)&0110) | |
| 253 |
(unsigned int) PIXEL_EQUAL(y1_ptr[x+1], colour); |
| 254 |
qb[x] = (unsigned char) p; |
| 255 |
if ((i != 2 || x != 0) && ((p&m) == 0) && todelete[p] ) { |
| 256 |
count++; /* delete the pixel */ |
| 257 |
PIXEL_SET(y_ptr[x], bg_color); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/* Process right edge pixel. */ |
| 262 |
p = (p<<1)&0666; |
| 263 |
if (i != 3 && (p&m) == 0 && todelete[p] ) { |
| 264 |
count++; |
| 265 |
PIXEL_SET(y_ptr[xsize-1], bg_color); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
if (i != 1) |
| 270 |
{ |
| 271 |
/* Process bottom scan line. */ |
| 272 |
q = qb[0]; |
| 273 |
p = ((q<<2)&0330); |
| 274 |
|
| 275 |
y_ptr = ptr + xsize * (ysize - 1); |
| 276 |
for ( x = 0 ; x < xsize ; x++ ) { |
| 277 |
q = qb[x]; |
| 278 |
p = ((p<<1)&0666) | ((q<<3)&0110); |
| 279 |
if ((i != 2 || x != 0) && (p&m) == 0 && todelete[p]) { |
| 280 |
count++; |
| 281 |
PIXEL_SET(y_ptr[x], bg_color); |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
LOG2 ("ThinImage: pass %d, %d pixels deleted\n", pc, count); |
| 287 |
} |
| 288 |
free (qb); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
void thin1(bitmap_type *image, unsigned char colour) |
| 294 |
{ |
| 295 |
unsigned char *ptr, *y_ptr, *y1_ptr; |
| 296 |
unsigned char bg_color; |
| 297 |
unsigned int xsize, ysize; /* Image resolution */ |
| 298 |
unsigned int x, y; /* Pixel location */ |
| 299 |
unsigned int i; /* Pass index */ |
| 300 |
unsigned int pc = 0; /* Pass count */ |
| 301 |
unsigned int count = 1; /* Deleted pixel count */ |
| 302 |
unsigned int p, q; /* Neighborhood maps of adjacent*/ |
| 303 |
/* cells */ |
| 304 |
unsigned char *qb; /* Neighborhood maps of previous*/ |
| 305 |
/* scanline */ |
| 306 |
unsigned int m; /* Deletion direction mask */ |
| 307 |
|
| 308 |
if (PPM_ISGRAY(background)) |
| 309 |
bg_color = PPM_GETR(background); |
| 310 |
else |
| 311 |
bg_color = ppm_luminosity(background); |
| 312 |
|
| 313 |
LOG (" Thinning image.....\n "); |
| 314 |
xsize = image->width; |
| 315 |
ysize = image->height; |
| 316 |
MALLOCARRAY_NOFAIL(qb, xsize); |
| 317 |
qb[xsize-1] = 0; /* Used for lower-right pixel */ |
| 318 |
ptr = image->bitmap; |
| 319 |
|
| 320 |
while ( count ) { /* Scan image while deletions */ |
| 321 |
pc++; |
| 322 |
count = 0; |
| 323 |
|
| 324 |
for ( i = 0 ; i < 4 ; i++ ) { |
| 325 |
|
| 326 |
m = masks[i]; |
| 327 |
|
| 328 |
/* Build initial previous scan buffer. */ |
| 329 |
p = (ptr[0] == colour); |
| 330 |
for ( x = 0 ; x < xsize-1 ; x++ ) |
| 331 |
qb[x] = (unsigned char) (p = ((p<<1)&0006) | (unsigned int)(ptr[x+1] == colour)); |
| 332 |
|
| 333 |
/* Scan image for pixel deletion candidates. */ |
| 334 |
y_ptr = ptr; y1_ptr = ptr + xsize; |
| 335 |
for (y = 0; y < ysize - 1; y++, y_ptr += xsize, y1_ptr += xsize) |
| 336 |
{ |
| 337 |
q = qb[0]; |
| 338 |
p = ((q<<2)&0330) | (y1_ptr[0] == colour); |
| 339 |
|
| 340 |
for ( x = 0 ; x < xsize-1 ; x++ ) { |
| 341 |
q = qb[x]; |
| 342 |
p = ((p<<1)&0666) | ((q<<3)&0110) | (unsigned int) (y1_ptr[x+1]==colour); |
| 343 |
qb[x] = (unsigned char) p; |
| 344 |
if ( ((p&m) == 0) && todelete[p] ) { |
| 345 |
count++; |
| 346 |
y_ptr[x] = bg_color; /* delete the pixel */ |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
/* Process right edge pixel. */ |
| 351 |
p = (p<<1)&0666; |
| 352 |
if ( (p&m) == 0 && todelete[p] ) { |
| 353 |
count++; |
| 354 |
y_ptr[xsize-1] = bg_color; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/* Process bottom scan line. */ |
| 359 |
q = qb[0]; |
| 360 |
p = ((q<<2)&0330); |
| 361 |
|
| 362 |
y_ptr = ptr + xsize * (ysize - 1); |
| 363 |
for ( x = 0 ; x < xsize ; x++ ) { |
| 364 |
q = qb[x]; |
| 365 |
p = ((p<<1)&0666) | ((q<<3)&0110); |
| 366 |
if ( (p&m) == 0 && todelete[p] ) { |
| 367 |
count++; |
| 368 |
y_ptr[x] = bg_color; |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
LOG2("thin1: pass %d, %d pixels deleted\n", pc, count); |
| 373 |
} |
| 374 |
free (qb); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|