| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
|
| 8 |
#include "mallocvar.h" |
| 9 |
|
| 10 |
#include "message.h" |
| 11 |
#include "bitmap.h" |
| 12 |
#include "logreport.h" |
| 13 |
#include "pxl-outline.h" |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
typedef enum |
| 26 |
{ |
| 27 |
TOP = 1, LEFT = 2, BOTTOM = 3, RIGHT = 0, NO_EDGE = 4 |
| 28 |
} edge_type; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#define START_EDGE top |
| 34 |
|
| 35 |
typedef enum |
| 36 |
{ |
| 37 |
NORTH = 0, NORTHWEST = 1, WEST = 2, SOUTHWEST = 3, SOUTH = 4, |
| 38 |
SOUTHEAST = 5, EAST = 6, NORTHEAST = 7 |
| 39 |
} direction_type; |
| 40 |
|
| 41 |
#define NUM_EDGES NO_EDGE |
| 42 |
|
| 43 |
#define COMPUTE_DELTA(axis, dir) \ |
| 44 |
((dir) % 2 != 0 \ |
| 45 |
? COMPUTE_##axis##_DELTA ((dir) - 1) \ |
| 46 |
+ COMPUTE_##axis##_DELTA (((dir) + 1) % 8) \ |
| 47 |
: COMPUTE_##axis##_DELTA (dir) \ |
| 48 |
) |
| 49 |
|
| 50 |
#define COMPUTE_ROW_DELTA(dir) \ |
| 51 |
((dir) == NORTH ? -1 : (dir) == SOUTH ? +1 : 0) |
| 52 |
|
| 53 |
#define COMPUTE_COL_DELTA(dir) \ |
| 54 |
((dir) == WEST ? -1 : (dir) == EAST ? +1 : 0) |
| 55 |
|
| 56 |
static void append_pixel_outline (pixel_outline_list_type *, |
| 57 |
pixel_outline_type); |
| 58 |
static pixel_outline_type new_pixel_outline (void); |
| 59 |
static void free_pixel_outline (pixel_outline_type *); |
| 60 |
static void concat_pixel_outline (pixel_outline_type *, |
| 61 |
const pixel_outline_type*); |
| 62 |
static bool is_marked_edge (edge_type, unsigned short, unsigned short, bitmap_type); |
| 63 |
|
| 64 |
static void |
| 65 |
mark_edge (edge_type e, unsigned short, unsigned short, bitmap_type *); |
| 66 |
|
| 67 |
static bool is_marked_dir(unsigned short, unsigned short, direction_type, bitmap_type); |
| 68 |
static bool is_other_dir_marked(unsigned short, unsigned short, direction_type, bitmap_type); |
| 69 |
static void mark_dir(unsigned short, unsigned short, direction_type, bitmap_type *); |
| 70 |
|
| 71 |
static unsigned |
| 72 |
num_neighbors(unsigned short, unsigned short, bitmap_type); |
| 73 |
|
| 74 |
#define CHECK_FATAL() if (at_exception_got_fatal(exp)) goto cleanup; |
| 75 |
#define RETURN_IF_FATAL() if (at_exception_got_fatal(exp)) return; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static pixel |
| 81 |
getBitmapColor(bitmap_type const bitmap, |
| 82 |
unsigned int const row, |
| 83 |
unsigned int const col) { |
| 84 |
|
| 85 |
unsigned char * const p = BITMAP_PIXEL(bitmap, row, col); |
| 86 |
|
| 87 |
pixel pix; |
| 88 |
|
| 89 |
if (bitmap.np >= 3) |
| 90 |
PPM_ASSIGN(pix, p[0], p[1], p[2]); |
| 91 |
else |
| 92 |
PPM_ASSIGN(pix, p[0], p[0], p[0]); |
| 93 |
|
| 94 |
return pix; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static void |
| 100 |
append_outline_pixel(pixel_outline_type * const pixelOutlineP, |
| 101 |
pm_pixelcoord const coord) { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
O_LENGTH(*pixelOutlineP)++; |
| 107 |
REALLOCARRAY_NOFAIL(pixelOutlineP->data, O_LENGTH(*pixelOutlineP)); |
| 108 |
|
| 109 |
O_COORDINATE(*pixelOutlineP, O_LENGTH(*pixelOutlineP) - 1) = coord; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static bool |
| 118 |
is_outline_edge (edge_type edge, bitmap_type bitmap, |
| 119 |
unsigned short row, unsigned short col, pixel color, |
| 120 |
at_exception_type * exp) |
| 121 |
{ |
| 122 |
|
| 123 |
if (!PPM_EQUAL (getBitmapColor (bitmap, row, col), color)) |
| 124 |
return false; |
| 125 |
|
| 126 |
switch (edge) |
| 127 |
{ |
| 128 |
case LEFT: |
| 129 |
return (bool) |
| 130 |
(col == 0 || |
| 131 |
!PPM_EQUAL (getBitmapColor (bitmap, row, col - 1), color)); |
| 132 |
|
| 133 |
case TOP: |
| 134 |
return (bool) |
| 135 |
(row == 0 || |
| 136 |
!PPM_EQUAL (getBitmapColor (bitmap, row - 1, col), color)); |
| 137 |
|
| 138 |
case RIGHT: |
| 139 |
return (bool) |
| 140 |
(col == bitmap.width - 1 |
| 141 |
|| !PPM_EQUAL(getBitmapColor(bitmap, row, col + 1), color)); |
| 142 |
|
| 143 |
case BOTTOM: |
| 144 |
return (bool) |
| 145 |
(row == bitmap.height - 1 |
| 146 |
|| !PPM_EQUAL(getBitmapColor (bitmap, row + 1, col), color)); |
| 147 |
|
| 148 |
case NO_EDGE: |
| 149 |
default: |
| 150 |
LOG1 ("is_outline_edge: Bad edge value(%d)", edge); |
| 151 |
at_exception_fatal(exp, "is_outline_edge: Bad edge value"); |
| 152 |
} |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static bool |
| 162 |
is_unmarked_outline_edge(unsigned short row, |
| 163 |
unsigned short col, |
| 164 |
edge_type edge, |
| 165 |
bitmap_type bitmap, |
| 166 |
bitmap_type marked, |
| 167 |
pixel color, |
| 168 |
at_exception_type * exp) |
| 169 |
{ |
| 170 |
return |
| 171 |
(bool)(!is_marked_edge (edge, row, col, marked) |
| 172 |
&& is_outline_edge (edge, bitmap, row, col, color, exp)); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
static bool |
| 178 |
is_valid_dir(unsigned int const row, |
| 179 |
unsigned int const col, |
| 180 |
direction_type const dir, |
| 181 |
bitmap_type const bitmap, |
| 182 |
bitmap_type const marked) { |
| 183 |
|
| 184 |
return(!is_marked_dir(row, col, dir, marked) |
| 185 |
&& COMPUTE_DELTA(ROW, dir)+row > 0 |
| 186 |
&& COMPUTE_DELTA(COL, dir)+col > 0 |
| 187 |
&& BITMAP_VALID_PIXEL(bitmap, |
| 188 |
COMPUTE_DELTA(ROW, dir) + row, |
| 189 |
COMPUTE_DELTA(COL, dir) + col) |
| 190 |
&& PPM_EQUAL(getBitmapColor(bitmap, |
| 191 |
COMPUTE_DELTA(ROW, dir) + row, |
| 192 |
COMPUTE_DELTA(COL, dir) + col), |
| 193 |
getBitmapColor(bitmap, row, col))); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
static bool |
| 199 |
next_unmarked_pixel(unsigned int * const row, |
| 200 |
unsigned int * const col, |
| 201 |
direction_type * const dir, |
| 202 |
bitmap_type const bitmap, |
| 203 |
bitmap_type * const marked) { |
| 204 |
|
| 205 |
unsigned int const orig_row = *row; |
| 206 |
unsigned int const orig_col = *col; |
| 207 |
direction_type const orig_dir = *dir; |
| 208 |
|
| 209 |
direction_type test_dir; |
| 210 |
|
| 211 |
test_dir = *dir; |
| 212 |
|
| 213 |
do { |
| 214 |
if (is_valid_dir(orig_row, orig_col, test_dir, bitmap, *marked)) { |
| 215 |
*row = orig_row + COMPUTE_DELTA(ROW, test_dir); |
| 216 |
*col = orig_col + COMPUTE_DELTA(COL, test_dir); |
| 217 |
*dir = test_dir; |
| 218 |
break; |
| 219 |
} |
| 220 |
|
| 221 |
if (orig_dir == test_dir) |
| 222 |
test_dir = (orig_dir + 2) % 8; |
| 223 |
else if ((orig_dir + 2) % 8 == test_dir) |
| 224 |
test_dir = (orig_dir + 6) % 8; |
| 225 |
else if ((orig_dir + 6) % 8 == test_dir) |
| 226 |
test_dir = (orig_dir + 1) % 8; |
| 227 |
else if ((orig_dir + 1) % 8 == test_dir) |
| 228 |
test_dir = (orig_dir + 7) % 8; |
| 229 |
else if ((orig_dir + 7) % 8 == test_dir) |
| 230 |
test_dir = (orig_dir + 3) % 8; |
| 231 |
else if ((orig_dir + 3) % 8 == test_dir) |
| 232 |
test_dir = (orig_dir + 5) % 8; |
| 233 |
else if ((orig_dir + 5) % 8 == test_dir) |
| 234 |
break; |
| 235 |
} while (1); |
| 236 |
|
| 237 |
if ((*row != orig_row || *col != orig_col) && |
| 238 |
(!(is_other_dir_marked(orig_row, orig_col, test_dir, *marked) && |
| 239 |
is_other_dir_marked(orig_row + COMPUTE_DELTA(ROW, test_dir), |
| 240 |
orig_col + COMPUTE_DELTA(COL, test_dir), |
| 241 |
test_dir,*marked)))) |
| 242 |
return true; |
| 243 |
else |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
static pixel_outline_type |
| 250 |
findOneCenterline(bitmap_type const bitmap, |
| 251 |
direction_type const originalDir, |
| 252 |
unsigned int const originalRow, |
| 253 |
unsigned int const originalCol, |
| 254 |
bitmap_type * const marked) { |
| 255 |
|
| 256 |
direction_type searchDir; |
| 257 |
unsigned int row, col; |
| 258 |
pixel_outline_type outline; |
| 259 |
|
| 260 |
outline = new_pixel_outline(); |
| 261 |
outline.open = false; |
| 262 |
outline.color = getBitmapColor(bitmap, originalRow, originalCol); |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
{ |
| 269 |
pm_pixelcoord pos; |
| 270 |
pos.col = originalCol; pos.row = bitmap.height - originalRow - 1; |
| 271 |
LOG2(" (%d,%d)", pos.col, pos.row); |
| 272 |
append_outline_pixel(&outline, pos); |
| 273 |
} |
| 274 |
searchDir = originalDir; |
| 275 |
row = originalRow; |
| 276 |
col = originalCol; |
| 277 |
|
| 278 |
for ( ; ; ) { |
| 279 |
unsigned int const prevRow = row; |
| 280 |
unsigned int const prevCol = col; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
if (!next_unmarked_pixel(&row, &col, &searchDir, bitmap, marked)) { |
| 286 |
outline.open = true; |
| 287 |
break; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
if (!(prevRow == originalRow && prevCol == originalCol)) |
| 294 |
mark_dir(prevRow, prevCol, searchDir, marked); |
| 295 |
mark_dir(row, col, (searchDir + 4) % 8, marked); |
| 296 |
|
| 297 |
|
| 298 |
if (row == originalRow && col == originalCol) |
| 299 |
break; |
| 300 |
|
| 301 |
|
| 302 |
{ |
| 303 |
|
| 304 |
pm_pixelcoord pos; |
| 305 |
pos.col = col; pos.row = bitmap.height - row - 1; |
| 306 |
LOG2(" (%d,%d)", pos.col, pos.row); |
| 307 |
append_outline_pixel(&outline, pos); |
| 308 |
} |
| 309 |
} |
| 310 |
mark_dir(originalRow, originalCol, originalDir, marked); |
| 311 |
|
| 312 |
return outline; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
static bool |
| 318 |
wrongDirection(unsigned int const row, |
| 319 |
unsigned int const col, |
| 320 |
direction_type const dir, |
| 321 |
bitmap_type const bitmap, |
| 322 |
bitmap_type const marked) { |
| 323 |
|
| 324 |
return (!is_valid_dir(row, col, dir, bitmap, marked) |
| 325 |
|| (!is_valid_dir(COMPUTE_DELTA(ROW, dir) + row, |
| 326 |
COMPUTE_DELTA(COL, dir) + col, |
| 327 |
dir, bitmap, marked) |
| 328 |
&& num_neighbors(row, col, bitmap) > 2) |
| 329 |
|| num_neighbors(row, col, bitmap) > 4 |
| 330 |
|| num_neighbors(COMPUTE_DELTA(ROW, dir) + row, |
| 331 |
COMPUTE_DELTA(COL, dir) + col, bitmap) > 4 |
| 332 |
|| (is_other_dir_marked(row, col, dir, marked) |
| 333 |
&& is_other_dir_marked(row + COMPUTE_DELTA(ROW, dir), |
| 334 |
col + COMPUTE_DELTA(COL, dir), |
| 335 |
dir, marked))); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
pixel_outline_list_type |
| 341 |
find_centerline_pixels(bitmap_type const bitmap, |
| 342 |
pixel const bg_color, |
| 343 |
at_progress_func notify_progress, |
| 344 |
void * const progress_data, |
| 345 |
at_testcancel_func test_cancel, |
| 346 |
void * const testcancel_data, |
| 347 |
at_exception_type * const exp) { |
| 348 |
|
| 349 |
pixel_outline_list_type outline_list; |
| 350 |
signed short row; |
| 351 |
bitmap_type marked = new_bitmap(bitmap.width, bitmap.height); |
| 352 |
unsigned int const max_progress = bitmap.height * bitmap.width; |
| 353 |
|
| 354 |
O_LIST_LENGTH(outline_list) = 0; |
| 355 |
outline_list.data = NULL; |
| 356 |
|
| 357 |
for (row = 0; row < bitmap.height; ++row) { |
| 358 |
signed short col; |
| 359 |
for (col = 0; col < bitmap.width; ) { |
| 360 |
bool const clockwise = false; |
| 361 |
|
| 362 |
direction_type dir; |
| 363 |
pixel_outline_type outline; |
| 364 |
|
| 365 |
if (notify_progress) |
| 366 |
notify_progress((float)(row * bitmap.width + col) / |
| 367 |
((float) max_progress * (float)3.0), |
| 368 |
progress_data); |
| 369 |
|
| 370 |
if (PPM_EQUAL(getBitmapColor(bitmap, row, col), bg_color)) { |
| 371 |
++col; |
| 372 |
continue; |
| 373 |
} |
| 374 |
|
| 375 |
dir = EAST; |
| 376 |
|
| 377 |
if (wrongDirection(row, col, dir, bitmap, marked)) { |
| 378 |
dir = SOUTHEAST; |
| 379 |
if (wrongDirection(row, col, dir, bitmap, marked)) { |
| 380 |
dir = SOUTH; |
| 381 |
if (wrongDirection(row, col, dir, bitmap, marked)) { |
| 382 |
dir = SOUTHWEST; |
| 383 |
if (wrongDirection(row, col, dir, bitmap, marked)) { |
| 384 |
++col; |
| 385 |
continue; |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
LOG2("#%u: (%sclockwise) ", O_LIST_LENGTH(outline_list), |
| 392 |
clockwise ? "" : "counter"); |
| 393 |
|
| 394 |
outline = findOneCenterline(bitmap, dir, row, col, &marked); |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
if (outline.open) { |
| 402 |
pixel_outline_type partial_outline; |
| 403 |
bool okay = false; |
| 404 |
|
| 405 |
if (dir == EAST) { |
| 406 |
dir = SOUTH; |
| 407 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 408 |
if (!okay) { |
| 409 |
dir = SOUTHWEST; |
| 410 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 411 |
if (!okay) { |
| 412 |
dir = SOUTHEAST; |
| 413 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 414 |
} |
| 415 |
} |
| 416 |
} else if (dir == SOUTHEAST) { |
| 417 |
dir = SOUTHWEST; |
| 418 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 419 |
if (!okay) { |
| 420 |
dir = EAST; |
| 421 |
okay=is_valid_dir(row, col, dir, bitmap, marked); |
| 422 |
if (!okay) { |
| 423 |
dir = SOUTH; |
| 424 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 425 |
} |
| 426 |
} |
| 427 |
} else if (dir == SOUTH) { |
| 428 |
dir = EAST; |
| 429 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 430 |
if (!okay) { |
| 431 |
dir = SOUTHEAST; |
| 432 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 433 |
if (!okay) { |
| 434 |
dir = SOUTHWEST; |
| 435 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 436 |
} |
| 437 |
} |
| 438 |
} else if (dir == SOUTHWEST) { |
| 439 |
dir = SOUTHEAST; |
| 440 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 441 |
if (!okay) { |
| 442 |
dir = EAST; |
| 443 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 444 |
if (!okay) { |
| 445 |
dir = SOUTH; |
| 446 |
okay = is_valid_dir(row, col, dir, bitmap, marked); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
if (okay) { |
| 451 |
partial_outline = |
| 452 |
findOneCenterline(bitmap, dir, row, col, &marked); |
| 453 |
concat_pixel_outline(&outline, &partial_outline); |
| 454 |
if (partial_outline.data) |
| 455 |
free(partial_outline.data); |
| 456 |
} else |
| 457 |
++col; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
O_CLOCKWISE(outline) = clockwise; |
| 466 |
if (O_LENGTH(outline) > 1) |
| 467 |
append_pixel_outline(&outline_list, outline); |
| 468 |
LOG1("(%s)", (outline.open ? " open" : " closed")); |
| 469 |
LOG1(" [%u].\n", O_LENGTH(outline)); |
| 470 |
if (O_LENGTH(outline) == 1) |
| 471 |
free_pixel_outline(&outline); |
| 472 |
} |
| 473 |
} |
| 474 |
if (test_cancel && test_cancel(testcancel_data)) { |
| 475 |
if (O_LIST_LENGTH (outline_list) != 0) |
| 476 |
free_pixel_outline_list (&outline_list); |
| 477 |
} |
| 478 |
free_bitmap(&marked); |
| 479 |
flush_log_output(); |
| 480 |
return outline_list; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
static void |
| 488 |
append_pixel_outline (pixel_outline_list_type *outline_list, |
| 489 |
pixel_outline_type outline) |
| 490 |
{ |
| 491 |
O_LIST_LENGTH (*outline_list)++; |
| 492 |
REALLOCARRAY_NOFAIL(outline_list->data, outline_list->length); |
| 493 |
O_LIST_OUTLINE (*outline_list, O_LIST_LENGTH (*outline_list) - 1) = outline; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
void |
| 501 |
free_pixel_outline_list (pixel_outline_list_type *outline_list) |
| 502 |
{ |
| 503 |
unsigned this_outline; |
| 504 |
|
| 505 |
for (this_outline = 0; this_outline < outline_list->length; this_outline++) |
| 506 |
{ |
| 507 |
pixel_outline_type o = outline_list->data[this_outline]; |
| 508 |
free_pixel_outline (&o); |
| 509 |
} |
| 510 |
outline_list->length = 0; |
| 511 |
|
| 512 |
if (outline_list->data != NULL) |
| 513 |
{ |
| 514 |
free (outline_list->data); |
| 515 |
outline_list->data = NULL; |
| 516 |
} |
| 517 |
|
| 518 |
flush_log_output (); |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
static pixel_outline_type |
| 527 |
new_pixel_outline (void) |
| 528 |
{ |
| 529 |
pixel_outline_type pixel_outline; |
| 530 |
|
| 531 |
O_LENGTH (pixel_outline) = 0; |
| 532 |
pixel_outline.data = NULL; |
| 533 |
pixel_outline.open = false; |
| 534 |
|
| 535 |
return pixel_outline; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
static void |
| 541 |
free_pixel_outline (pixel_outline_type * outline) |
| 542 |
{ |
| 543 |
if (outline->data) |
| 544 |
{ |
| 545 |
free (outline->data) ; |
| 546 |
outline->data = NULL; |
| 547 |
outline->length = 0; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
static void |
| 557 |
concat_pixel_outline(pixel_outline_type *o1, const pixel_outline_type *o2) |
| 558 |
{ |
| 559 |
int src, dst; |
| 560 |
unsigned o1_length, o2_length; |
| 561 |
if (!o1 || !o2 || O_LENGTH(*o2) <= 1) return; |
| 562 |
|
| 563 |
o1_length = O_LENGTH(*o1); |
| 564 |
o2_length = O_LENGTH(*o2); |
| 565 |
O_LENGTH(*o1) += o2_length - 1; |
| 566 |
|
| 567 |
|
| 568 |
REALLOCARRAY_NOFAIL(o1->data, O_LENGTH(*o1)); |
| 569 |
|
| 570 |
|
| 571 |
for (src = o1_length - 1, dst = O_LENGTH(*o1) - 1; src >= 0; src--, dst--) |
| 572 |
O_COORDINATE(*o1, dst) = O_COORDINATE(*o1, src); |
| 573 |
|
| 574 |
for (src = o2_length - 1, dst = 0; src > 0; src--, dst++) |
| 575 |
O_COORDINATE(*o1, dst) = O_COORDINATE(*o2, src); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
static void |
| 585 |
mark_edge (edge_type edge, unsigned short row, |
| 586 |
unsigned short col, bitmap_type *marked) |
| 587 |
{ |
| 588 |
*BITMAP_PIXEL (*marked, row, col) |= 1 << edge; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
static void |
| 596 |
mark_dir(unsigned short row, unsigned short col, direction_type dir, bitmap_type *marked) |
| 597 |
{ |
| 598 |
*BITMAP_PIXEL(*marked, row, col) |= 1 << dir; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
static bool |
| 606 |
is_marked_dir(unsigned short row, unsigned short col, direction_type dir, bitmap_type marked) |
| 607 |
{ |
| 608 |
return (bool)((*BITMAP_PIXEL(marked, row, col) & 1 << dir) != 0); |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
static bool |
| 614 |
is_other_dir_marked(unsigned short row, unsigned short col, direction_type dir, bitmap_type marked) |
| 615 |
{ |
| 616 |
return (bool)((*BITMAP_PIXEL(marked, row, col) & (255 - (1 << dir) - (1 << ((dir + 4) % 8))) ) != 0); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
static unsigned |
| 624 |
num_neighbors(unsigned short row, unsigned short col, bitmap_type bitmap) |
| 625 |
{ |
| 626 |
unsigned dir, count = 0; |
| 627 |
pixel color = getBitmapColor(bitmap, row, col); |
| 628 |
for (dir = NORTH; dir <= NORTHEAST; dir++) |
| 629 |
{ |
| 630 |
int delta_r = COMPUTE_DELTA(ROW, dir); |
| 631 |
int delta_c = COMPUTE_DELTA(COL, dir); |
| 632 |
unsigned int test_row = row + delta_r; |
| 633 |
unsigned int test_col = col + delta_c; |
| 634 |
if (BITMAP_VALID_PIXEL(bitmap, test_row, test_col) |
| 635 |
&& PPM_EQUAL(getBitmapColor(bitmap, test_row, test_col), color)) |
| 636 |
++count; |
| 637 |
} |
| 638 |
return count; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
static bool |
| 646 |
is_marked_edge (edge_type edge, unsigned short row, unsigned short col, bitmap_type marked) |
| 647 |
{ |
| 648 |
return |
| 649 |
(bool)(edge == NO_EDGE ? false : (*BITMAP_PIXEL (marked, row, col) & (1 << edge)) != 0); |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
static void |
| 655 |
nextClockwisePointTop(bitmap_type const bitmap, |
| 656 |
edge_type * const edge, |
| 657 |
unsigned int * const row, |
| 658 |
unsigned int * const col, |
| 659 |
pixel const color, |
| 660 |
bitmap_type const marked, |
| 661 |
at_exception_type * const exp, |
| 662 |
pm_pixelcoord * const posP) { |
| 663 |
|
| 664 |
if ((*col >= 1 && !is_marked_edge(TOP, *row, *col-1, marked) && |
| 665 |
is_outline_edge(TOP, bitmap, *row, *col-1, color, exp))) { |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
*edge = TOP; |
| 670 |
--*col; |
| 671 |
posP->col = *col; |
| 672 |
posP->row = bitmap.height - *row; |
| 673 |
return; |
| 674 |
} |
| 675 |
|
| 676 |
RETURN_IF_FATAL(); |
| 677 |
|
| 678 |
if ((*col >= 1 && *row >= 1 && |
| 679 |
!is_marked_edge(RIGHT, *row-1, *col-1, marked) && |
| 680 |
is_outline_edge(RIGHT, bitmap, *row-1, *col-1, |
| 681 |
color, exp)) && |
| 682 |
!(is_marked_edge(LEFT, *row-1, *col, marked) && |
| 683 |
is_marked_edge(TOP, *row,*col-1, marked)) && |
| 684 |
!(is_marked_edge(BOTTOM, *row-1, *col, marked) && |
| 685 |
is_marked_edge(RIGHT, *row, *col-1, marked))) { |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
*edge = RIGHT; |
| 690 |
--*col; |
| 691 |
--*row; |
| 692 |
posP->col = *col + 1; |
| 693 |
posP->row = bitmap.height - *row; |
| 694 |
return; |
| 695 |
} |
| 696 |
|
| 697 |
RETURN_IF_FATAL(); |
| 698 |
|
| 699 |
if ((!is_marked_edge(LEFT, *row, *col, marked) |
| 700 |
&& is_outline_edge(LEFT, bitmap, *row, *col, color, exp))) { |
| 701 |
|
| 702 |
*edge = LEFT; |
| 703 |
posP->col = *col; |
| 704 |
posP->row = bitmap.height - *row - 1; |
| 705 |
return; |
| 706 |
} |
| 707 |
|
| 708 |
RETURN_IF_FATAL(); |
| 709 |
|
| 710 |
*edge = NO_EDGE; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
static void |
| 716 |
nextClockwisePointRight(bitmap_type const bitmap, |
| 717 |
edge_type * const edge, |
| 718 |
unsigned int * const row, |
| 719 |
unsigned int * const col, |
| 720 |
pixel const color, |
| 721 |
bitmap_type const marked, |
| 722 |
at_exception_type * const exp, |
| 723 |
pm_pixelcoord * const posP) { |
| 724 |
|
| 725 |
if ((*row >= 1 && |
| 726 |
!is_marked_edge(RIGHT, *row-1, *col, marked) && |
| 727 |
is_outline_edge(RIGHT, bitmap, *row-1, *col, color, exp))) { |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
*edge = RIGHT; |
| 732 |
--*row; |
| 733 |
posP->col = *col+1; |
| 734 |
posP->row = bitmap.height - *row; |
| 735 |
return; |
| 736 |
} |
| 737 |
|
| 738 |
RETURN_IF_FATAL(); |
| 739 |
|
| 740 |
if ((*col+1 < marked.width && *row >= 1 && |
| 741 |
!is_marked_edge(BOTTOM, *row-1, *col+1, marked) && |
| 742 |
is_outline_edge(BOTTOM, bitmap, *row-1, *col+1, |
| 743 |
color, exp)) && |
| 744 |
!(is_marked_edge(LEFT, *row, *col+1, marked) && |
| 745 |
is_marked_edge(BOTTOM, *row-1, *col, marked)) && |
| 746 |
!(is_marked_edge(TOP, *row, *col+1, marked) && |
| 747 |
is_marked_edge(RIGHT, *row-1, *col, marked))) { |
| 748 |
|
| 749 |
|
| 750 |
*edge = BOTTOM; |
| 751 |
++*col; |
| 752 |
--*row; |
| 753 |
posP->col = *col + 1; |
| 754 |
posP->row = bitmap.height - *row - 1; |
| 755 |
return; |
| 756 |
} |
| 757 |
|
| 758 |
RETURN_IF_FATAL(); |
| 759 |
|
| 760 |
if ((!is_marked_edge(TOP, *row, *col, marked) && |
| 761 |
is_outline_edge(TOP, bitmap, *row, *col, color, exp))) { |
| 762 |
|
| 763 |
*edge = TOP; |
| 764 |
posP->col = *col; |
| 765 |
posP->row = bitmap.height - *row; |
| 766 |
return; |
| 767 |
} |
| 768 |
|
| 769 |
RETURN_IF_FATAL(); |
| 770 |
|
| 771 |
*edge = NO_EDGE; |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
static void |
| 777 |
nextClockwisePointBottom(bitmap_type const bitmap, |
| 778 |
edge_type * const edge, |
| 779 |
unsigned int * const row, |
| 780 |
unsigned int * const col, |
| 781 |
pixel const color, |
| 782 |
bitmap_type const marked, |
| 783 |
at_exception_type * const exp, |
| 784 |
pm_pixelcoord * const posP) { |
| 785 |
|
| 786 |
if ((*col+1 < marked.width && |
| 787 |
!is_marked_edge(BOTTOM, *row, *col+1, marked) && |
| 788 |
is_outline_edge(BOTTOM, bitmap, *row, *col+1, color, exp))) { |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
*edge = BOTTOM; |
| 793 |
++*col; |
| 794 |
posP->col = *col+1; |
| 795 |
posP->row = bitmap.height - *row - 1; |
| 796 |
return; |
| 797 |
} |
| 798 |
|
| 799 |
RETURN_IF_FATAL(); |
| 800 |
|
| 801 |
if ((*col + 1 < marked.width && *row+1 < marked.height && |
| 802 |
!is_marked_edge(LEFT, *row+1,*col+1, marked) && |
| 803 |
is_outline_edge(LEFT, bitmap, *row+1, *col+1, color, exp)) && |
| 804 |
!(is_marked_edge(TOP, *row+1, *col, marked) && |
| 805 |
is_marked_edge(LEFT, *row, *col+1, marked)) && |
| 806 |
!(is_marked_edge(RIGHT, *row+1, *col, marked) && |
| 807 |
is_marked_edge(BOTTOM, *row, *col+1, marked))) { |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
*edge = LEFT; |
| 812 |
++*col; |
| 813 |
++*row; |
| 814 |
posP->col = *col; |
| 815 |
posP->row = bitmap.height - *row - 1; |
| 816 |
return; |
| 817 |
} |
| 818 |
|
| 819 |
RETURN_IF_FATAL(); |
| 820 |
|
| 821 |
if ((!is_marked_edge(RIGHT, *row, *col, marked) && |
| 822 |
is_outline_edge(RIGHT, bitmap, *row, *col, color, exp))) { |
| 823 |
|
| 824 |
*edge = RIGHT; |
| 825 |
posP->col = *col+1; |
| 826 |
posP->row = bitmap.height - *row; |
| 827 |
return; |
| 828 |
} |
| 829 |
|
| 830 |
RETURN_IF_FATAL(); |
| 831 |
|
| 832 |
*edge = NO_EDGE; |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
static void |
| 838 |
nextClockwisePointLeft(bitmap_type const bitmap, |
| 839 |
edge_type * const edge, |
| 840 |
unsigned int * const row, |
| 841 |
unsigned int * const col, |
| 842 |
pixel const color, |
| 843 |
bitmap_type const marked, |
| 844 |
at_exception_type * const exp, |
| 845 |
pm_pixelcoord * const posP) { |
| 846 |
|
| 847 |
if ((*row+1 < marked.height && |
| 848 |
!is_marked_edge(LEFT, *row+1, *col, marked) && |
| 849 |
is_outline_edge(LEFT, bitmap, *row+1, *col, color, exp))) { |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
*edge = LEFT; |
| 854 |
++*row; |
| 855 |
posP->col = *col; |
| 856 |
posP->row = bitmap.height - *row - 1; |
| 857 |
return; |
| 858 |
} |
| 859 |
|
| 860 |
RETURN_IF_FATAL(); |
| 861 |
|
| 862 |
if ((*col >= 1 && *row+1 < marked.height && |
| 863 |
!is_marked_edge(TOP, *row+1, *col-1, marked) && |
| 864 |
is_outline_edge(TOP, bitmap, *row+1, *col-1, color, exp)) && |
| 865 |
!(is_marked_edge(RIGHT, *row, *col-1, marked) && |
| 866 |
is_marked_edge(TOP, *row+1, *col, marked)) && |
| 867 |
!(is_marked_edge(BOTTOM, *row, *col-1, marked) && |
| 868 |
is_marked_edge(LEFT, *row+1, *col, marked))) { |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
*edge = TOP; |
| 873 |
--*col; |
| 874 |
++*row; |
| 875 |
posP->col = *col; |
| 876 |
posP->row = bitmap.height - *row; |
| 877 |
return; |
| 878 |
} |
| 879 |
|
| 880 |
RETURN_IF_FATAL(); |
| 881 |
|
| 882 |
if ((!is_marked_edge(BOTTOM, *row, *col, marked) && |
| 883 |
is_outline_edge(BOTTOM, bitmap, *row, *col, color, exp))) { |
| 884 |
*edge = BOTTOM; |
| 885 |
posP->col = *col+1; |
| 886 |
posP->row = bitmap.height - *row - 1; |
| 887 |
return; |
| 888 |
} |
| 889 |
RETURN_IF_FATAL(); |
| 890 |
|
| 891 |
*edge = NO_EDGE; |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
static void |
| 897 |
nextClockwisePoint(bitmap_type const bitmap, |
| 898 |
edge_type * const edge, |
| 899 |
unsigned int * const row, |
| 900 |
unsigned int * const col, |
| 901 |
pixel const color, |
| 902 |
bitmap_type const marked, |
| 903 |
at_exception_type * const exp, |
| 904 |
pm_pixelcoord * const posP) { |
| 905 |
|
| 906 |
switch (*edge) { |
| 907 |
case TOP: |
| 908 |
nextClockwisePointTop( bitmap, edge, row, col, color, |
| 909 |
marked, exp, posP); |
| 910 |
break; |
| 911 |
case RIGHT: |
| 912 |
nextClockwisePointRight(bitmap, edge, row, col, color, |
| 913 |
marked, exp, posP); |
| 914 |
break; |
| 915 |
case BOTTOM: |
| 916 |
nextClockwisePointBottom(bitmap, edge, row, col, color, |
| 917 |
marked, exp, posP); |
| 918 |
break; |
| 919 |
case LEFT: |
| 920 |
nextClockwisePointLeft( bitmap, edge, row, col, color, |
| 921 |
marked, exp, posP); |
| 922 |
break; |
| 923 |
case NO_EDGE: |
| 924 |
break; |
| 925 |
default: |
| 926 |
*edge = NO_EDGE; |
| 927 |
break; |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
static void |
| 934 |
nextCcwPointTop(bitmap_type const bitmap, |
| 935 |
edge_type * const edge, |
| 936 |
unsigned int * const row, |
| 937 |
unsigned int * const col, |
| 938 |
pixel const color, |
| 939 |
bitmap_type const marked, |
| 940 |
at_exception_type * const exp, |
| 941 |
pm_pixelcoord * const posP) { |
| 942 |
|
| 943 |
if ((!is_marked_edge(LEFT, *row, *col, marked) && |
| 944 |
is_outline_edge(LEFT,bitmap,*row,*col, color, exp))) { |
| 945 |
|
| 946 |
*edge = LEFT; |
| 947 |
posP->col = *col; |
| 948 |
posP->row = bitmap.height - *row - 1; |
| 949 |
return; |
| 950 |
} |
| 951 |
|
| 952 |
RETURN_IF_FATAL(); |
| 953 |
|
| 954 |
if ((*col >= 1 && |
| 955 |
!is_marked_edge(TOP, *row, *col-1, marked) && |
| 956 |
is_outline_edge(TOP, bitmap, *row, *col-1, color, exp))) { |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
*edge = TOP; |
| 961 |
--*col; |
| 962 |
posP->col = *col; |
| 963 |
posP->row = bitmap.height - *row; |
| 964 |
return; |
| 965 |
} |
| 966 |
|
| 967 |
RETURN_IF_FATAL(); |
| 968 |
|
| 969 |
if ((*col >= 1 && *row >= 1 && |
| 970 |
!is_marked_edge(RIGHT, *row-1, *col-1, marked) && |
| 971 |
is_outline_edge(RIGHT, bitmap, *row-1, *col-1, color, exp))) { |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
*edge = RIGHT; |
| 976 |
--*col; |
| 977 |
--*row; |
| 978 |
posP->col = *col + 1; |
| 979 |
posP->row = bitmap.height - *row; |
| 980 |
return; |
| 981 |
} |
| 982 |
|
| 983 |
RETURN_IF_FATAL(); |
| 984 |
|
| 985 |
*edge = NO_EDGE; |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
static void |
| 991 |
nextCcwPointRight(bitmap_type const bitmap, |
| 992 |
edge_type * const edge, |
| 993 |
unsigned int * const row, |
| 994 |
unsigned int * const col, |
| 995 |
pixel const color, |
| 996 |
bitmap_type const marked, |
| 997 |
at_exception_type * const exp, |
| 998 |
pm_pixelcoord * const posP) { |
| 999 |
|
| 1000 |
if ((!is_marked_edge(TOP, *row, *col, marked) && |
| 1001 |
is_outline_edge(TOP, bitmap, *row, *col, color, exp))) { |
| 1002 |
|
| 1003 |
*edge = TOP; |
| 1004 |
posP->col = *col; |
| 1005 |
posP->row = bitmap.height - *row; |
| 1006 |
return; |
| 1007 |
} |
| 1008 |
|
| 1009 |
RETURN_IF_FATAL(); |
| 1010 |
|
| 1011 |
if ((*row >= 1 && |
| 1012 |
!is_marked_edge(RIGHT, *row-1, *col, marked) && |
| 1013 |
is_outline_edge(RIGHT, bitmap, *row-1, *col, color, exp))) { |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
*edge = RIGHT; |
| 1018 |
--*row; |
| 1019 |
posP->col = *col + 1; |
| 1020 |
posP->row = bitmap.height - *row; |
| 1021 |
return; |
| 1022 |
} |
| 1023 |
|
| 1024 |
RETURN_IF_FATAL(); |
| 1025 |
|
| 1026 |
if ((*col + 1 < marked.width && *row >= 1 && |
| 1027 |
!is_marked_edge(BOTTOM, *row-1, *col+1, marked) && |
| 1028 |
is_outline_edge(BOTTOM, bitmap, *row-1, *col+1, color, exp))) { |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
*edge = BOTTOM; |
| 1033 |
++*col; |
| 1034 |
++*row; |
| 1035 |
posP->col = *col + 1; |
| 1036 |
posP->row = bitmap.height - *row - 1; |
| 1037 |
return; |
| 1038 |
} |
| 1039 |
|
| 1040 |
RETURN_IF_FATAL(); |
| 1041 |
|
| 1042 |
*edge = NO_EDGE; |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
static void |
| 1048 |
nextCcwPointBottom(bitmap_type const bitmap, |
| 1049 |
edge_type * const edge, |
| 1050 |
unsigned int * const row, |
| 1051 |
unsigned int * const col, |
| 1052 |
pixel const color, |
| 1053 |
bitmap_type const marked, |
| 1054 |
at_exception_type * const exp, |
| 1055 |
pm_pixelcoord * const posP) { |
| 1056 |
|
| 1057 |
if ((!is_marked_edge(RIGHT, *row, *col, marked) && |
| 1058 |
is_outline_edge(RIGHT, bitmap, *row, *col, color, exp))) { |
| 1059 |
|
| 1060 |
*edge = RIGHT; |
| 1061 |
posP->col = *col + 1; |
| 1062 |
posP->row = bitmap.height - *row; |
| 1063 |
return; |
| 1064 |
} |
| 1065 |
|
| 1066 |
RETURN_IF_FATAL(); |
| 1067 |
|
| 1068 |
if ((*col + 1 < marked.width && |
| 1069 |
!is_marked_edge(BOTTOM, *row, *col+1, marked) && |
| 1070 |
is_outline_edge(BOTTOM, bitmap, *row, *col+1, color, exp))) { |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
*edge = BOTTOM; |
| 1075 |
++*col; |
| 1076 |
posP->col = *col + 1; |
| 1077 |
posP->row = bitmap.height - *row - 1; |
| 1078 |
return; |
| 1079 |
} |
| 1080 |
|
| 1081 |
RETURN_IF_FATAL(); |
| 1082 |
|
| 1083 |
if ((*col + 1 < marked.width && *row + 1 < marked.height && |
| 1084 |
!is_marked_edge(LEFT, *row+1, *col+1, marked) && |
| 1085 |
is_outline_edge(LEFT, bitmap, *row+1, *col+1, color, exp))) { |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
*edge = LEFT; |
| 1090 |
++*col; |
| 1091 |
++*row; |
| 1092 |
posP->col = *col; |
| 1093 |
posP->row = bitmap.height - *row - 1; |
| 1094 |
return; |
| 1095 |
} |
| 1096 |
|
| 1097 |
RETURN_IF_FATAL(); |
| 1098 |
|
| 1099 |
*edge = NO_EDGE; |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
static void |
| 1105 |
nextCcwPointLeft(bitmap_type const bitmap, |
| 1106 |
edge_type * const edge, |
| 1107 |
unsigned int * const row, |
| 1108 |
unsigned int * const col, |
| 1109 |
pixel const color, |
| 1110 |
bitmap_type const marked, |
| 1111 |
at_exception_type * const exp, |
| 1112 |
pm_pixelcoord * const posP) { |
| 1113 |
|
| 1114 |
|
| 1115 |
if ((!is_marked_edge(BOTTOM, *row, *col, marked) && |
| 1116 |
is_outline_edge(BOTTOM, bitmap, *row, *col, color, exp))) { |
| 1117 |
|
| 1118 |
*edge = BOTTOM; |
| 1119 |
posP->col = *col + 1; |
| 1120 |
posP->row = bitmap.height - *row - 1; |
| 1121 |
return; |
| 1122 |
} |
| 1123 |
|
| 1124 |
RETURN_IF_FATAL(); |
| 1125 |
|
| 1126 |
if ((*row + 1 < marked.height && |
| 1127 |
!is_marked_edge(LEFT, *row+1, *col, marked) && |
| 1128 |
is_outline_edge(LEFT, bitmap, *row+1, *col, color, exp))) { |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
*edge = LEFT; |
| 1133 |
++*row; |
| 1134 |
posP->col = *col; |
| 1135 |
posP->row = bitmap.height - *row - 1; |
| 1136 |
return; |
| 1137 |
} |
| 1138 |
|
| 1139 |
RETURN_IF_FATAL(); |
| 1140 |
|
| 1141 |
if ((*col >= 1 && *row + 1 < marked.height && |
| 1142 |
!is_marked_edge(TOP, *row+1, *col-1, marked) && |
| 1143 |
is_outline_edge(TOP, bitmap, *row+1, *col-1, color, exp))) { |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
*edge = TOP; |
| 1148 |
--*col; |
| 1149 |
++*row; |
| 1150 |
posP->col = *col; |
| 1151 |
posP->row = bitmap.height - *row; |
| 1152 |
return; |
| 1153 |
} |
| 1154 |
|
| 1155 |
RETURN_IF_FATAL(); |
| 1156 |
|
| 1157 |
*edge = NO_EDGE; |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
static void |
| 1163 |
nextCounterClockwisePoint(bitmap_type const bitmap, |
| 1164 |
edge_type * const edge, |
| 1165 |
unsigned int * const row, |
| 1166 |
unsigned int * const col, |
| 1167 |
pixel const color, |
| 1168 |
bitmap_type const marked, |
| 1169 |
at_exception_type * const exp, |
| 1170 |
pm_pixelcoord * const posP) { |
| 1171 |
|
| 1172 |
switch (*edge) { |
| 1173 |
case TOP: |
| 1174 |
nextCcwPointTop( bitmap, edge, row, col, color, marked, exp, posP); |
| 1175 |
break; |
| 1176 |
case RIGHT: |
| 1177 |
nextCcwPointRight( bitmap, edge, row, col, color, marked, exp, posP); |
| 1178 |
break; |
| 1179 |
case BOTTOM: |
| 1180 |
nextCcwPointBottom(bitmap, edge, row, col, color, marked, exp, posP); |
| 1181 |
break; |
| 1182 |
case LEFT: |
| 1183 |
nextCcwPointLeft( bitmap, edge, row, col, color, marked, exp, posP); |
| 1184 |
break; |
| 1185 |
case NO_EDGE: |
| 1186 |
break; |
| 1187 |
default: |
| 1188 |
*edge = NO_EDGE; |
| 1189 |
break; |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
static void |
| 1196 |
nextPoint(bitmap_type const bitmap, |
| 1197 |
edge_type * const edge, |
| 1198 |
unsigned int * const row, |
| 1199 |
unsigned int * const col, |
| 1200 |
pm_pixelcoord * const nextPointP, |
| 1201 |
pixel const color, |
| 1202 |
bool const clockwise, |
| 1203 |
bitmap_type const marked, |
| 1204 |
at_exception_type * const exp) { |
| 1205 |
|
| 1206 |
if (!clockwise) |
| 1207 |
nextClockwisePoint( bitmap, edge, row, col, color, |
| 1208 |
marked, exp, nextPointP); |
| 1209 |
else |
| 1210 |
nextCounterClockwisePoint(bitmap, edge, row, col, color, |
| 1211 |
marked, exp, nextPointP); |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
static pixel_outline_type |
| 1217 |
find_one_outline(bitmap_type const bitmap, |
| 1218 |
edge_type const originalEdge, |
| 1219 |
unsigned int const originalRow, |
| 1220 |
unsigned int const originalCol, |
| 1221 |
bitmap_type * const marked, |
| 1222 |
bool const clockwise, |
| 1223 |
bool const ignore, |
| 1224 |
at_exception_type * const exp) { |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
unsigned int row; |
| 1231 |
unsigned int col; |
| 1232 |
edge_type edge; |
| 1233 |
|
| 1234 |
pixel_outline_type outline; |
| 1235 |
pm_pixelcoord pos; |
| 1236 |
|
| 1237 |
outline = new_pixel_outline(); |
| 1238 |
outline.color = getBitmapColor(bitmap, originalRow, originalCol); |
| 1239 |
|
| 1240 |
row = originalRow; |
| 1241 |
col = originalCol; |
| 1242 |
edge = originalEdge; |
| 1243 |
|
| 1244 |
|
| 1245 |
pos.col = col + ((edge == RIGHT) || (edge == BOTTOM) ? 1 : 0); |
| 1246 |
pos.row = bitmap.height - row - 1 + |
| 1247 |
(edge == TOP || edge == RIGHT ? 1 : 0); |
| 1248 |
|
| 1249 |
do { |
| 1250 |
|
| 1251 |
if (!ignore) { |
| 1252 |
LOG2(" (%d,%d)", pos.col, pos.row); |
| 1253 |
append_outline_pixel(&outline, pos); |
| 1254 |
} |
| 1255 |
|
| 1256 |
mark_edge(edge, row, col, marked); |
| 1257 |
nextPoint(bitmap, &edge, &row, &col, &pos, outline.color, clockwise, |
| 1258 |
*marked, exp); |
| 1259 |
|
| 1260 |
} while (edge != NO_EDGE && !at_exception_got_fatal(exp)); |
| 1261 |
|
| 1262 |
if (ignore || at_exception_got_fatal(exp)) |
| 1263 |
free_pixel_outline(&outline); |
| 1264 |
|
| 1265 |
return outline; |
| 1266 |
} |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
pixel_outline_list_type |
| 1271 |
find_outline_pixels(bitmap_type const bitmap, |
| 1272 |
bool const bg_spec, |
| 1273 |
pixel const bg_color, |
| 1274 |
at_progress_func notify_progress, |
| 1275 |
void * const progress_data, |
| 1276 |
at_testcancel_func test_cancel, |
| 1277 |
void * const testcancel_data, |
| 1278 |
at_exception_type * const exp) { |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
unsigned int const max_progress = bitmap.height * bitmap.width; |
| 1291 |
|
| 1292 |
pixel_outline_list_type outline_list; |
| 1293 |
unsigned int row; |
| 1294 |
bitmap_type marked; |
| 1295 |
|
| 1296 |
marked = new_bitmap (bitmap.width, bitmap.height); |
| 1297 |
|
| 1298 |
O_LIST_LENGTH(outline_list) = 0; |
| 1299 |
outline_list.data = NULL; |
| 1300 |
|
| 1301 |
for (row = 0; row < bitmap.height; ++row) { |
| 1302 |
unsigned int col; |
| 1303 |
for (col = 0; col < bitmap.width; ++col) { |
| 1304 |
pixel const color = getBitmapColor(bitmap, row, col); |
| 1305 |
bool const is_background = |
| 1306 |
bg_spec && PPM_EQUAL(color, bg_color); |
| 1307 |
|
| 1308 |
if (notify_progress) |
| 1309 |
notify_progress((float)(row * bitmap.width + col) / |
| 1310 |
((float) max_progress * (float)3.0), |
| 1311 |
progress_data); |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
if (!is_background && |
| 1318 |
is_unmarked_outline_edge(row, col, TOP, |
| 1319 |
bitmap, marked, color, exp)) { |
| 1320 |
pixel_outline_type outline; |
| 1321 |
|
| 1322 |
CHECK_FATAL(); |
| 1323 |
|
| 1324 |
LOG1("#%u: (counterclockwise)", O_LIST_LENGTH(outline_list)); |
| 1325 |
|
| 1326 |
outline = find_one_outline(bitmap, TOP, row, col, &marked, |
| 1327 |
false, false, exp); |
| 1328 |
CHECK_FATAL(); |
| 1329 |
|
| 1330 |
O_CLOCKWISE(outline) = false; |
| 1331 |
append_pixel_outline(&outline_list, outline); |
| 1332 |
|
| 1333 |
LOG1(" [%u].\n", O_LENGTH (outline)); |
| 1334 |
} else |
| 1335 |
CHECK_FATAL (); |
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
if (row > 0) { |
| 1341 |
pixel const colorAbove = getBitmapColor(bitmap, row-1, col); |
| 1342 |
if (!(bg_spec && PPM_EQUAL(colorAbove, bg_color)) && |
| 1343 |
is_unmarked_outline_edge(row-1, col, BOTTOM, |
| 1344 |
bitmap, marked, colorAbove,exp)) { |
| 1345 |
CHECK_FATAL(); |
| 1346 |
|
| 1347 |
|
| 1348 |
if (is_background) { |
| 1349 |
pixel_outline_type outline; |
| 1350 |
|
| 1351 |
LOG1("#%u: (clockwise)", O_LIST_LENGTH(outline_list)); |
| 1352 |
|
| 1353 |
outline = find_one_outline(bitmap, BOTTOM, row-1, col, |
| 1354 |
&marked, true, false, exp); |
| 1355 |
CHECK_FATAL(); |
| 1356 |
|
| 1357 |
O_CLOCKWISE(outline) = true; |
| 1358 |
append_pixel_outline(&outline_list, outline); |
| 1359 |
|
| 1360 |
LOG1(" [%u].\n", O_LENGTH(outline)); |
| 1361 |
} else { |
| 1362 |
find_one_outline(bitmap, BOTTOM, row-1, col, |
| 1363 |
&marked, true, true, exp); |
| 1364 |
CHECK_FATAL(); |
| 1365 |
} |
| 1366 |
} else |
| 1367 |
CHECK_FATAL(); |
| 1368 |
} |
| 1369 |
if (test_cancel && test_cancel(testcancel_data)) { |
| 1370 |
free_pixel_outline_list(&outline_list); |
| 1371 |
goto cleanup; |
| 1372 |
} |
| 1373 |
} |
| 1374 |
} |
| 1375 |
cleanup: |
| 1376 |
free_bitmap(&marked); |
| 1377 |
flush_log_output(); |
| 1378 |
if (at_exception_got_fatal(exp)) |
| 1379 |
free_pixel_outline_list(&outline_list); |
| 1380 |
|
| 1381 |
return outline_list; |
| 1382 |
} |
| 1383 |
|
| 1384 |
|
| 1385 |
|