2007-03-08 04:03:40 +08:00
|
|
|
/**********************************************************************
|
2017-07-03 05:35:47 +08:00
|
|
|
* File: scanedg.cpp (Formerly scanedge.c)
|
2007-03-08 04:03:40 +08:00
|
|
|
* Description: Raster scanning crack based edge extractor.
|
2018-10-12 17:20:38 +08:00
|
|
|
* Author: Ray Smith
|
2007-03-08 04:03:40 +08:00
|
|
|
*
|
|
|
|
* (C) Copyright 1991, Hewlett-Packard Ltd.
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
2014-01-10 01:36:42 +08:00
|
|
|
#include "scanedg.h"
|
|
|
|
|
2017-07-15 00:30:14 +08:00
|
|
|
#include <memory> // std::unique_ptr
|
2017-05-10 21:23:49 +08:00
|
|
|
|
2014-01-10 01:36:42 +08:00
|
|
|
#include "allheaders.h"
|
|
|
|
#include "edgloop.h"
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
#define WHITE_PIX 1 /*thresholded colours */
|
|
|
|
#define BLACK_PIX 0
|
2017-07-15 00:30:14 +08:00
|
|
|
// Flips between WHITE_PIX and BLACK_PIX.
|
2007-03-08 04:03:40 +08:00
|
|
|
#define FLIP_COLOUR(pix) (1-(pix))
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* block_edges
|
|
|
|
*
|
|
|
|
* Extract edges from a PDBLK.
|
|
|
|
**********************************************************************/
|
|
|
|
|
2014-01-10 01:36:42 +08:00
|
|
|
void block_edges(Pix *t_pix, // thresholded image
|
2010-11-24 02:34:14 +08:00
|
|
|
PDBLK *block, // block in image
|
|
|
|
C_OUTLINE_IT* outline_it) {
|
|
|
|
ICOORD bleft; // bounding box
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORD tright;
|
2010-11-24 02:34:14 +08:00
|
|
|
BLOCK_LINE_IT line_it = block; // line iterator
|
2014-01-10 01:36:42 +08:00
|
|
|
|
|
|
|
int width = pixGetWidth(t_pix);
|
|
|
|
int height = pixGetHeight(t_pix);
|
|
|
|
int wpl = pixGetWpl(t_pix);
|
2010-11-24 02:34:14 +08:00
|
|
|
// lines in progress
|
2018-05-22 22:55:45 +08:00
|
|
|
std::unique_ptr<CRACKEDGE*[]> ptrline(new CRACKEDGE*[width + 1]);
|
2016-12-13 00:23:03 +08:00
|
|
|
CRACKEDGE *free_cracks = nullptr;
|
2010-11-24 02:34:14 +08:00
|
|
|
|
|
|
|
block->bounding_box(bleft, tright); // block box
|
2018-06-09 19:52:25 +08:00
|
|
|
ASSERT_HOST(tright.x() <= width);
|
|
|
|
ASSERT_HOST(tright.y() <= height);
|
2014-01-10 01:36:42 +08:00
|
|
|
int block_width = tright.x() - bleft.x();
|
|
|
|
for (int x = block_width; x >= 0; x--)
|
2016-12-13 00:23:03 +08:00
|
|
|
ptrline[x] = nullptr; // no lines in progress
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2018-05-22 22:55:45 +08:00
|
|
|
std::unique_ptr<uint8_t[]> bwline(new uint8_t[width]);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-05-26 23:05:04 +08:00
|
|
|
const uint8_t margin = WHITE_PIX;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2014-01-10 01:36:42 +08:00
|
|
|
for (int y = tright.y() - 1; y >= bleft.y() - 1; y--) {
|
|
|
|
if (y >= bleft.y() && y < tright.y()) {
|
|
|
|
// Get the binary pixels from the image.
|
|
|
|
l_uint32* line = pixGetData(t_pix) + wpl * (height - 1 - y);
|
|
|
|
for (int x = 0; x < block_width; ++x) {
|
|
|
|
bwline[x] = GET_DATA_BIT(line, x + bleft.x()) ^ 1;
|
|
|
|
}
|
2018-05-22 22:55:45 +08:00
|
|
|
make_margins(block, &line_it, bwline.get(), margin, bleft.x(), tright.x(), y);
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
2018-05-22 22:55:45 +08:00
|
|
|
memset(bwline.get(), margin, block_width * sizeof(bwline[0]));
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2014-01-10 01:36:42 +08:00
|
|
|
line_edges(bleft.x(), y, block_width,
|
2018-05-22 22:55:45 +08:00
|
|
|
margin, bwline.get(), ptrline.get(), &free_cracks, outline_it);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
free_crackedges(free_cracks); // really free them
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* make_margins
|
|
|
|
*
|
|
|
|
* Get an image line and set to margin non-text pixels.
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
void make_margins( //get a line
|
|
|
|
PDBLK *block, //block in image
|
|
|
|
BLOCK_LINE_IT *line_it, //for old style
|
Use POSIX data types and macros (#878)
* api: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* cutil: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* training: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract data types by POSIX data types
Now all Tesseract data types which are no longer needed can be removed
from ccutil/host.h.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* lstm: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Remove the macros which are now unused from ccutil/host.h.
Remove also the obsolete history comments.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* Fix build error caused by ambiguous ClipToRange
Error message vom Appveyor CI:
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2672: 'ClipToRange': no matching overloaded function found [C:\projects\tesseract\build\libtesseract.vcxproj]
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2782: 'T ClipToRange(const T &,const T &,const T &)': template parameter 'T' is ambiguous [C:\projects\tesseract\build\libtesseract.vcxproj]
c:\projects\tesseract\ccutil\helpers.h(122): note: see declaration of 'ClipToRange'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: could be 'char'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: or 'int'
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* unittest: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* arch: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2018-03-14 04:36:30 +08:00
|
|
|
uint8_t *pixels, //pixels to strip
|
|
|
|
uint8_t margin, //white-out pixel
|
|
|
|
int16_t left, //block edges
|
|
|
|
int16_t right,
|
|
|
|
int16_t y //line coord
|
2007-03-08 04:03:40 +08:00
|
|
|
) {
|
|
|
|
ICOORDELT_IT seg_it;
|
Use POSIX data types and macros (#878)
* api: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* cutil: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* training: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract data types by POSIX data types
Now all Tesseract data types which are no longer needed can be removed
from ccutil/host.h.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* lstm: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Remove the macros which are now unused from ccutil/host.h.
Remove also the obsolete history comments.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* Fix build error caused by ambiguous ClipToRange
Error message vom Appveyor CI:
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2672: 'ClipToRange': no matching overloaded function found [C:\projects\tesseract\build\libtesseract.vcxproj]
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2782: 'T ClipToRange(const T &,const T &,const T &)': template parameter 'T' is ambiguous [C:\projects\tesseract\build\libtesseract.vcxproj]
c:\projects\tesseract\ccutil\helpers.h(122): note: see declaration of 'ClipToRange'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: could be 'char'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: or 'int'
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* unittest: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* arch: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2018-03-14 04:36:30 +08:00
|
|
|
int32_t start; //of segment
|
|
|
|
int16_t xext; //of segment
|
2007-03-08 04:03:40 +08:00
|
|
|
int xindex; //index to pixel
|
|
|
|
|
2016-12-13 00:23:03 +08:00
|
|
|
if (block->poly_block () != nullptr) {
|
2018-05-22 22:55:45 +08:00
|
|
|
std::unique_ptr<PB_LINE_IT> lines(new PB_LINE_IT (block->poly_block ()));
|
2017-07-15 00:30:14 +08:00
|
|
|
const std::unique_ptr</*non-const*/ ICOORDELT_LIST> segments(
|
|
|
|
lines->get_line(y));
|
2007-03-08 04:03:40 +08:00
|
|
|
if (!segments->empty ()) {
|
2017-07-15 00:30:14 +08:00
|
|
|
seg_it.set_to_list(segments.get());
|
2007-03-08 04:03:40 +08:00
|
|
|
seg_it.mark_cycle_pt ();
|
|
|
|
start = seg_it.data ()->x ();
|
|
|
|
xext = seg_it.data ()->y ();
|
|
|
|
for (xindex = left; xindex < right; xindex++) {
|
|
|
|
if (xindex >= start && !seg_it.cycled_list ()) {
|
|
|
|
xindex = start + xext - 1;
|
|
|
|
seg_it.forward ();
|
|
|
|
start = seg_it.data ()->x ();
|
|
|
|
xext = seg_it.data ()->y ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pixels[xindex - left] = margin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (xindex = left; xindex < right; xindex++)
|
|
|
|
pixels[xindex - left] = margin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
start = line_it->get_line (y, xext);
|
|
|
|
for (xindex = left; xindex < start; xindex++)
|
|
|
|
pixels[xindex - left] = margin;
|
|
|
|
for (xindex = start + xext; xindex < right; xindex++)
|
|
|
|
pixels[xindex - left] = margin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* line_edges
|
|
|
|
*
|
|
|
|
* Scan a line for edges and update the edges in progress.
|
|
|
|
* When edges close into loops, send them for approximation.
|
|
|
|
**********************************************************************/
|
|
|
|
|
Use POSIX data types and macros (#878)
* api: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* cutil: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* training: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract data types by POSIX data types
Now all Tesseract data types which are no longer needed can be removed
from ccutil/host.h.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* lstm: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Remove the macros which are now unused from ccutil/host.h.
Remove also the obsolete history comments.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* Fix build error caused by ambiguous ClipToRange
Error message vom Appveyor CI:
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2672: 'ClipToRange': no matching overloaded function found [C:\projects\tesseract\build\libtesseract.vcxproj]
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2782: 'T ClipToRange(const T &,const T &,const T &)': template parameter 'T' is ambiguous [C:\projects\tesseract\build\libtesseract.vcxproj]
c:\projects\tesseract\ccutil\helpers.h(122): note: see declaration of 'ClipToRange'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: could be 'char'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: or 'int'
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* unittest: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* arch: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2018-03-14 04:36:30 +08:00
|
|
|
void line_edges(int16_t x, // coord of line start
|
|
|
|
int16_t y, // coord of line
|
|
|
|
int16_t xext, // width of line
|
|
|
|
uint8_t uppercolour, // start of prev line
|
|
|
|
uint8_t * bwpos, // thresholded line
|
2010-11-24 02:34:14 +08:00
|
|
|
CRACKEDGE ** prevline, // edges in progress
|
|
|
|
CRACKEDGE **free_cracks,
|
|
|
|
C_OUTLINE_IT* outline_it) {
|
|
|
|
CrackPos pos = {free_cracks, x, y };
|
|
|
|
int xmax; // max x coord
|
|
|
|
int prevcolour; // of previous pixel
|
|
|
|
CRACKEDGE *current; // current h edge
|
|
|
|
CRACKEDGE *newcurrent; // new h edge
|
|
|
|
|
|
|
|
xmax = x + xext; // max allowable coord
|
|
|
|
prevcolour = uppercolour; // forced plain margin
|
2016-12-13 00:23:03 +08:00
|
|
|
current = nullptr; // nothing yet
|
2010-11-24 02:34:14 +08:00
|
|
|
|
|
|
|
// do each pixel
|
|
|
|
for (; pos.x < xmax; pos.x++, prevline++) {
|
2019-05-26 23:05:04 +08:00
|
|
|
const int colour = *bwpos++; // current pixel
|
2016-12-13 00:23:03 +08:00
|
|
|
if (*prevline != nullptr) {
|
2010-11-24 02:34:14 +08:00
|
|
|
// changed above
|
|
|
|
// change colour
|
|
|
|
uppercolour = FLIP_COLOUR(uppercolour);
|
2007-03-08 04:03:40 +08:00
|
|
|
if (colour == prevcolour) {
|
|
|
|
if (colour == uppercolour) {
|
2010-11-24 02:34:14 +08:00
|
|
|
// finish a line
|
|
|
|
join_edges(current, *prevline, free_cracks, outline_it);
|
2016-12-13 00:23:03 +08:00
|
|
|
current = nullptr; // no edge now
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
|
|
|
// new horiz edge
|
|
|
|
current = h_edge(uppercolour - colour, *prevline, &pos);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2016-12-13 00:23:03 +08:00
|
|
|
*prevline = nullptr; // no change this time
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
2007-03-08 04:03:40 +08:00
|
|
|
if (colour == uppercolour)
|
2010-11-24 02:34:14 +08:00
|
|
|
*prevline = v_edge(colour - prevcolour, *prevline, &pos);
|
|
|
|
// 8 vs 4 connection
|
2007-03-08 04:03:40 +08:00
|
|
|
else if (colour == WHITE_PIX) {
|
2010-11-24 02:34:14 +08:00
|
|
|
join_edges(current, *prevline, free_cracks, outline_it);
|
2016-12-13 00:23:03 +08:00
|
|
|
current = h_edge(uppercolour - colour, nullptr, &pos);
|
2010-11-24 02:34:14 +08:00
|
|
|
*prevline = v_edge(colour - prevcolour, current, &pos);
|
|
|
|
} else {
|
|
|
|
newcurrent = h_edge(uppercolour - colour, *prevline, &pos);
|
|
|
|
*prevline = v_edge(colour - prevcolour, current, &pos);
|
|
|
|
current = newcurrent; // right going h edge
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
prevcolour = colour; // remember new colour
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
2007-03-08 04:03:40 +08:00
|
|
|
if (colour != prevcolour) {
|
2010-11-24 02:34:14 +08:00
|
|
|
*prevline = current = v_edge(colour - prevcolour, current, &pos);
|
2007-03-08 04:03:40 +08:00
|
|
|
prevcolour = colour;
|
|
|
|
}
|
|
|
|
if (colour != uppercolour)
|
2010-11-24 02:34:14 +08:00
|
|
|
current = h_edge(uppercolour - colour, current, &pos);
|
2007-03-08 04:03:40 +08:00
|
|
|
else
|
2016-12-13 00:23:03 +08:00
|
|
|
current = nullptr; // no edge now
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 00:23:03 +08:00
|
|
|
if (current != nullptr) {
|
2010-11-24 02:34:14 +08:00
|
|
|
// out of block
|
2016-12-13 00:23:03 +08:00
|
|
|
if (*prevline != nullptr) { // got one to join to?
|
2010-11-24 02:34:14 +08:00
|
|
|
join_edges(current, *prevline, free_cracks, outline_it);
|
2016-12-13 00:23:03 +08:00
|
|
|
*prevline = nullptr; // tidy now
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
|
|
|
// fake vertical
|
|
|
|
*prevline = v_edge(FLIP_COLOUR(prevcolour)-prevcolour, current, &pos);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2016-12-13 00:23:03 +08:00
|
|
|
} else if (*prevline != nullptr) {
|
2007-03-08 04:03:40 +08:00
|
|
|
//continue fake
|
2010-11-24 02:34:14 +08:00
|
|
|
*prevline = v_edge(FLIP_COLOUR(prevcolour)-prevcolour, *prevline, &pos);
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* h_edge
|
|
|
|
*
|
|
|
|
* Create a new horizontal CRACKEDGE and join it to the given edge.
|
|
|
|
**********************************************************************/
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
CRACKEDGE *h_edge(int sign, // sign of edge
|
|
|
|
CRACKEDGE* join, // edge to join to
|
|
|
|
CrackPos* pos) {
|
|
|
|
CRACKEDGE *newpt; // return value
|
|
|
|
|
2016-12-13 00:23:03 +08:00
|
|
|
if (*pos->free_cracks != nullptr) {
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt = *pos->free_cracks;
|
|
|
|
*pos->free_cracks = newpt->next; // get one fast
|
|
|
|
} else {
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt = new CRACKEDGE;
|
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt->pos.set_y(pos->y + 1); // coords of pt
|
|
|
|
newpt->stepy = 0; // edge is horizontal
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
if (sign > 0) {
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt->pos.set_x(pos->x + 1); // start location
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt->stepx = -1;
|
|
|
|
newpt->stepdir = 0;
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
|
|
|
newpt->pos.set_x(pos->x); // start location
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt->stepx = 1;
|
|
|
|
newpt->stepdir = 2;
|
|
|
|
}
|
|
|
|
|
2016-12-13 00:23:03 +08:00
|
|
|
if (join == nullptr) {
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt->next = newpt; // ptrs to other ends
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt->prev = newpt;
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
|
|
|
if (newpt->pos.x() + newpt->stepx == join->pos.x()
|
|
|
|
&& newpt->pos.y() == join->pos.y()) {
|
|
|
|
newpt->prev = join->prev; // update other ends
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt->prev->next = newpt;
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt->next = join; // join up
|
2007-03-08 04:03:40 +08:00
|
|
|
join->prev = newpt;
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
|
|
|
newpt->next = join->next; // update other ends
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt->next->prev = newpt;
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt->prev = join; // join up
|
2007-03-08 04:03:40 +08:00
|
|
|
join->next = newpt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* v_edge
|
|
|
|
*
|
|
|
|
* Create a new vertical CRACKEDGE and join it to the given edge.
|
|
|
|
**********************************************************************/
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
CRACKEDGE *v_edge(int sign, // sign of edge
|
|
|
|
CRACKEDGE* join,
|
|
|
|
CrackPos* pos) {
|
|
|
|
CRACKEDGE *newpt; // return value
|
|
|
|
|
2016-12-13 00:23:03 +08:00
|
|
|
if (*pos->free_cracks != nullptr) {
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt = *pos->free_cracks;
|
|
|
|
*pos->free_cracks = newpt->next; // get one fast
|
|
|
|
} else {
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt = new CRACKEDGE;
|
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt->pos.set_x(pos->x); // coords of pt
|
|
|
|
newpt->stepx = 0; // edge is vertical
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
if (sign > 0) {
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt->pos.set_y(pos->y); // start location
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt->stepy = 1;
|
|
|
|
newpt->stepdir = 3;
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
|
|
|
newpt->pos.set_y(pos->y + 1); // start location
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt->stepy = -1;
|
|
|
|
newpt->stepdir = 1;
|
|
|
|
}
|
|
|
|
|
2016-12-13 00:23:03 +08:00
|
|
|
if (join == nullptr) {
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt->next = newpt; //ptrs to other ends
|
|
|
|
newpt->prev = newpt;
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
|
|
|
if (newpt->pos.x() == join->pos.x()
|
|
|
|
&& newpt->pos.y() + newpt->stepy == join->pos.y()) {
|
|
|
|
newpt->prev = join->prev; // update other ends
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt->prev->next = newpt;
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt->next = join; // join up
|
2007-03-08 04:03:40 +08:00
|
|
|
join->prev = newpt;
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
|
|
|
newpt->next = join->next; // update other ends
|
2007-03-08 04:03:40 +08:00
|
|
|
newpt->next->prev = newpt;
|
2010-11-24 02:34:14 +08:00
|
|
|
newpt->prev = join; // join up
|
2007-03-08 04:03:40 +08:00
|
|
|
join->next = newpt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* join_edges
|
|
|
|
*
|
|
|
|
* Join 2 edges together. Send the outline for approximation when a
|
|
|
|
* closed loop is formed.
|
|
|
|
**********************************************************************/
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void join_edges(CRACKEDGE *edge1, // edges to join
|
|
|
|
CRACKEDGE *edge2, // no specific order
|
|
|
|
CRACKEDGE **free_cracks,
|
|
|
|
C_OUTLINE_IT* outline_it) {
|
|
|
|
if (edge1->pos.x() + edge1->stepx != edge2->pos.x()
|
|
|
|
|| edge1->pos.y() + edge1->stepy != edge2->pos.y()) {
|
|
|
|
CRACKEDGE *tempedge = edge1;
|
2017-07-15 00:30:14 +08:00
|
|
|
edge1 = edge2; // swap around
|
2007-03-08 04:03:40 +08:00
|
|
|
edge2 = tempedge;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edge1->next == edge2) {
|
2010-11-24 02:34:14 +08:00
|
|
|
// already closed
|
|
|
|
complete_edge(edge1, outline_it);
|
|
|
|
// attach freelist to end
|
|
|
|
edge1->prev->next = *free_cracks;
|
|
|
|
*free_cracks = edge1; // and free list
|
|
|
|
} else {
|
|
|
|
// update opposite ends
|
2007-03-08 04:03:40 +08:00
|
|
|
edge2->prev->next = edge1->next;
|
|
|
|
edge1->next->prev = edge2->prev;
|
2010-11-24 02:34:14 +08:00
|
|
|
edge1->next = edge2; // make joins
|
2007-03-08 04:03:40 +08:00
|
|
|
edge2->prev = edge1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* free_crackedges
|
|
|
|
*
|
|
|
|
* Really free the CRACKEDGEs by giving them back to delete.
|
|
|
|
**********************************************************************/
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void free_crackedges(CRACKEDGE *start) {
|
|
|
|
CRACKEDGE *current; // current edge to free
|
|
|
|
CRACKEDGE *next; // next one to free
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2016-12-13 00:23:03 +08:00
|
|
|
for (current = start; current != nullptr; current = next) {
|
2007-03-08 04:03:40 +08:00
|
|
|
next = current->next;
|
2010-11-24 02:34:14 +08:00
|
|
|
delete current; // delete them all
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|