2007-03-08 04:03:40 +08:00
|
|
|
/* -*-C-*-
|
|
|
|
********************************************************************************
|
|
|
|
*
|
|
|
|
* File: blobs.h (Formerly blobs.h)
|
|
|
|
* Description: Blob definition
|
|
|
|
* Author: Mark Seaman, OCR Technology
|
|
|
|
* Created: Fri Oct 27 15:39:52 1989
|
|
|
|
* Modified: Thu Mar 28 15:33:38 1991 (Mark Seaman) marks@hpgrlt
|
|
|
|
* Language: C
|
|
|
|
* Package: N/A
|
|
|
|
* Status: Experimental (Do Not Distribute)
|
|
|
|
*
|
|
|
|
* (c) Copyright 1989, Hewlett-Packard Company.
|
|
|
|
** 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.
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#ifndef BLOBS_H
|
|
|
|
#define BLOBS_H
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
I n c l u d e s
|
|
|
|
----------------------------------------------------------------------*/
|
2012-02-02 11:06:39 +08:00
|
|
|
#include "clst.h"
|
2010-11-24 02:34:14 +08:00
|
|
|
#include "rect.h"
|
|
|
|
#include "vecfuncs.h"
|
|
|
|
|
2010-11-30 08:56:39 +08:00
|
|
|
class BLOCK;
|
2010-11-24 02:34:14 +08:00
|
|
|
class C_BLOB;
|
|
|
|
class DENORM;
|
|
|
|
class ROW;
|
|
|
|
class WERD;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
T y p e s
|
|
|
|
----------------------------------------------------------------------*/
|
2010-11-24 02:34:14 +08:00
|
|
|
#define EDGEPTFLAGS 4 /*concavity,length etc. */
|
|
|
|
|
2007-03-08 04:03:40 +08:00
|
|
|
typedef struct
|
|
|
|
{ /* Widths of pieces */
|
|
|
|
int num_chars;
|
|
|
|
int widths[1];
|
|
|
|
} WIDTH_RECORD;
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
struct TPOINT {
|
2012-02-02 11:06:39 +08:00
|
|
|
TPOINT(): x(0), y(0) {}
|
|
|
|
TPOINT(inT16 vx, inT16 vy) : x(vx), y(vy) {}
|
|
|
|
TPOINT(const ICOORD &ic) : x(ic.x()), y(ic.y()) {}
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void operator+=(const TPOINT& other) {
|
|
|
|
x += other.x;
|
|
|
|
y += other.y;
|
|
|
|
}
|
|
|
|
void operator/=(int divisor) {
|
|
|
|
x /= divisor;
|
|
|
|
y /= divisor;
|
|
|
|
}
|
|
|
|
|
|
|
|
inT16 x; // absolute x coord.
|
|
|
|
inT16 y; // absolute y coord.
|
|
|
|
};
|
|
|
|
typedef TPOINT VECTOR; // structure for coordinates.
|
|
|
|
|
|
|
|
struct EDGEPT {
|
|
|
|
EDGEPT() : next(NULL), prev(NULL) {
|
|
|
|
memset(flags, 0, EDGEPTFLAGS * sizeof(flags[0]));
|
|
|
|
}
|
|
|
|
EDGEPT(const EDGEPT& src) : next(NULL), prev(NULL) {
|
|
|
|
CopyFrom(src);
|
|
|
|
}
|
|
|
|
EDGEPT& operator=(const EDGEPT& src) {
|
|
|
|
CopyFrom(src);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// Copies the data elements, but leaves the pointers untouched.
|
|
|
|
void CopyFrom(const EDGEPT& src) {
|
|
|
|
pos = src.pos;
|
|
|
|
vec = src.vec;
|
|
|
|
memcpy(flags, src.flags, EDGEPTFLAGS * sizeof(flags[0]));
|
|
|
|
}
|
|
|
|
// Accessors to hide or reveal a cut edge from feature extractors.
|
|
|
|
void Hide() {
|
|
|
|
flags[0] = true;
|
|
|
|
}
|
|
|
|
void Reveal() {
|
|
|
|
flags[0] = false;
|
|
|
|
}
|
|
|
|
bool IsHidden() const {
|
|
|
|
return flags[0] != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TPOINT pos; // position
|
|
|
|
VECTOR vec; // vector to next point
|
|
|
|
// TODO(rays) Remove flags and replace with
|
|
|
|
// is_hidden, runlength, dir, and fixed. The only use
|
|
|
|
// of the flags other than is_hidden is in polyaprx.cpp.
|
|
|
|
char flags[EDGEPTFLAGS]; // concavity, length etc
|
|
|
|
EDGEPT* next; // anticlockwise element
|
|
|
|
EDGEPT* prev; // clockwise element
|
|
|
|
};
|
|
|
|
|
2012-02-02 11:06:39 +08:00
|
|
|
// For use in chop and findseam to keep a list of which EDGEPTs were inserted.
|
|
|
|
CLISTIZEH(EDGEPT);
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
struct TESSLINE {
|
|
|
|
TESSLINE() : is_hole(false), loop(NULL), next(NULL) {}
|
|
|
|
TESSLINE(const TESSLINE& src) : loop(NULL), next(NULL) {
|
|
|
|
CopyFrom(src);
|
|
|
|
}
|
|
|
|
~TESSLINE() {
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
TESSLINE& operator=(const TESSLINE& src) {
|
|
|
|
CopyFrom(src);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// Consume the circular list of EDGEPTs to make a TESSLINE.
|
|
|
|
static TESSLINE* BuildFromOutlineList(EDGEPT* outline);
|
|
|
|
// Copies the data and the outline, but leaves next untouched.
|
|
|
|
void CopyFrom(const TESSLINE& src);
|
|
|
|
// Deletes owned data.
|
|
|
|
void Clear();
|
2010-11-30 08:56:39 +08:00
|
|
|
// Normalize in-place using the DENORM.
|
|
|
|
void Normalize(const DENORM& denorm);
|
2010-11-24 02:34:14 +08:00
|
|
|
// Rotates by the given rotation in place.
|
|
|
|
void Rotate(const FCOORD rotation);
|
|
|
|
// Moves by the given vec in place.
|
|
|
|
void Move(const ICOORD vec);
|
|
|
|
// Scales by the given factor in place.
|
|
|
|
void Scale(float factor);
|
|
|
|
// Sets up the start and vec members of the loop from the pos members.
|
|
|
|
void SetupFromPos();
|
|
|
|
// Recomputes the bounding box from the points in the loop.
|
|
|
|
void ComputeBoundingBox();
|
|
|
|
// Computes the min and max cross product of the outline points with the
|
|
|
|
// given vec and returns the results in min_xp and max_xp. Geometrically
|
|
|
|
// this is the left and right edge of the outline perpendicular to the
|
|
|
|
// given direction, but to get the distance units correct, you would
|
|
|
|
// have to divide by the modulus of vec.
|
|
|
|
void MinMaxCrossProduct(const TPOINT vec, int* min_xp, int* max_xp) const;
|
|
|
|
|
|
|
|
TBOX bounding_box() const;
|
|
|
|
// Returns true if the point is contained within the outline box.
|
|
|
|
bool Contains(const TPOINT& pt) {
|
|
|
|
return topleft.x <= pt.x && pt.x <= botright.x &&
|
|
|
|
botright.y <= pt.y && pt.y <= topleft.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void plot(ScrollView* window, ScrollView::Color color,
|
|
|
|
ScrollView::Color child_color);
|
|
|
|
|
|
|
|
int BBArea() const {
|
|
|
|
return (botright.x - topleft.x) * (topleft.y - botright.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
TPOINT topleft; // Top left of loop.
|
|
|
|
TPOINT botright; // Bottom right of loop.
|
|
|
|
TPOINT start; // Start of loop.
|
|
|
|
bool is_hole; // True if this is a hole/child outline.
|
|
|
|
EDGEPT *loop; // Edgeloop.
|
|
|
|
TESSLINE *next; // Next outline in blob.
|
|
|
|
}; // Outline structure.
|
|
|
|
|
|
|
|
struct TBLOB {
|
|
|
|
TBLOB() : outlines(NULL), next(NULL) {}
|
|
|
|
TBLOB(const TBLOB& src) : outlines(NULL), next(NULL) {
|
|
|
|
CopyFrom(src);
|
|
|
|
}
|
|
|
|
~TBLOB() {
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
TBLOB& operator=(const TBLOB& src) {
|
|
|
|
CopyFrom(src);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// Factory to build a TBLOB from a C_BLOB with polygonal
|
|
|
|
// approximation along the way.
|
|
|
|
static TBLOB* PolygonalCopy(C_BLOB* src);
|
2012-02-02 11:06:39 +08:00
|
|
|
// Normalizes the blob for classification only if needed.
|
|
|
|
// (Normally this means a non-zero classify rotation.)
|
|
|
|
// If no Normalization is needed, then NULL is returned, and the denorm is
|
|
|
|
// unchanged. Otherwise a new TBLOB is returned and the denorm points to
|
|
|
|
// a new DENORM. In this case, both the TBLOB and DENORM must be deleted.
|
|
|
|
TBLOB* ClassifyNormalizeIfNeeded(const DENORM** denorm) const;
|
2010-11-24 02:34:14 +08:00
|
|
|
// Copies the data and the outlines, but leaves next untouched.
|
|
|
|
void CopyFrom(const TBLOB& src);
|
|
|
|
// Deletes owned data.
|
|
|
|
void Clear();
|
2010-11-30 08:56:39 +08:00
|
|
|
// Normalize in-place using the DENORM.
|
|
|
|
void Normalize(const DENORM& denorm);
|
2010-11-24 02:34:14 +08:00
|
|
|
// Rotates by the given rotation in place.
|
|
|
|
void Rotate(const FCOORD rotation);
|
|
|
|
// Moves by the given vec in place.
|
|
|
|
void Move(const ICOORD vec);
|
|
|
|
// Scales by the given factor in place.
|
|
|
|
void Scale(float factor);
|
|
|
|
// Recomputes the bounding boxes of the outlines.
|
|
|
|
void ComputeBoundingBoxes();
|
|
|
|
|
|
|
|
// Returns the number of outlines.
|
|
|
|
int NumOutlines() const;
|
|
|
|
|
|
|
|
TBOX bounding_box() const;
|
|
|
|
|
|
|
|
void plot(ScrollView* window, ScrollView::Color color,
|
|
|
|
ScrollView::Color child_color);
|
|
|
|
|
|
|
|
int BBArea() const {
|
|
|
|
int total_area = 0;
|
|
|
|
for (TESSLINE* outline = outlines; outline != NULL; outline = outline->next)
|
|
|
|
total_area += outline->BBArea();
|
|
|
|
return total_area;
|
|
|
|
}
|
|
|
|
|
|
|
|
TESSLINE *outlines; // List of outlines in blob.
|
|
|
|
TBLOB *next; // Next blob in block.
|
|
|
|
}; // Blob structure.
|
|
|
|
|
|
|
|
int count_blobs(TBLOB *blobs);
|
|
|
|
|
|
|
|
struct TWERD {
|
|
|
|
TWERD() : blobs(NULL), latin_script(false), next(NULL) {}
|
|
|
|
TWERD(const TWERD& src) : blobs(NULL), next(NULL) {
|
|
|
|
CopyFrom(src);
|
|
|
|
}
|
|
|
|
~TWERD() {
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
TWERD& operator=(const TWERD& src) {
|
|
|
|
CopyFrom(src);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// Factory to build a TWERD from a (C_BLOB) WERD, with polygonal
|
|
|
|
// approximation along the way.
|
|
|
|
static TWERD* PolygonalCopy(WERD* src);
|
2010-11-30 08:56:39 +08:00
|
|
|
// Setup for Baseline normalization, recording the normalization in the
|
|
|
|
// DENORM, but doesn't do any normalization.
|
|
|
|
void SetupBLNormalize(const BLOCK* block, const ROW* row,
|
|
|
|
float x_height, bool numeric_mode,
|
|
|
|
DENORM* denorm) const;
|
|
|
|
// Normalize in-place using the DENORM.
|
|
|
|
void Normalize(const DENORM& denorm);
|
2010-11-24 02:34:14 +08:00
|
|
|
// Copies the data and the blobs, but leaves next untouched.
|
|
|
|
void CopyFrom(const TWERD& src);
|
|
|
|
// Deletes owned data.
|
|
|
|
void Clear();
|
|
|
|
// Recomputes the bounding boxes of the blobs.
|
|
|
|
void ComputeBoundingBoxes();
|
|
|
|
|
|
|
|
// Returns the number of blobs in the word.
|
|
|
|
int NumBlobs() const {
|
|
|
|
return count_blobs(blobs);
|
|
|
|
}
|
|
|
|
TBOX bounding_box() const;
|
|
|
|
|
|
|
|
// Merges the blobs from start to end, not including end, and deletes
|
|
|
|
// the blobs between start and end.
|
|
|
|
void MergeBlobs(int start, int end);
|
|
|
|
|
|
|
|
void plot(ScrollView* window);
|
|
|
|
|
|
|
|
TBLOB* blobs; // blobs in word.
|
|
|
|
bool latin_script; // This word is in a latin-based script.
|
|
|
|
TWERD* next; // next word.
|
|
|
|
};
|
|
|
|
|
2007-03-08 04:03:40 +08:00
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
M a c r o s
|
|
|
|
----------------------------------------------------------------------*/
|
|
|
|
/**********************************************************************
|
|
|
|
* free_widths
|
|
|
|
*
|
|
|
|
* Free the memory taken up by a width array.
|
|
|
|
**********************************************************************/
|
|
|
|
#define free_widths(w) \
|
|
|
|
if (w) memfree (w)
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
F u n c t i o n s
|
|
|
|
----------------------------------------------------------------------*/
|
2010-11-24 02:34:14 +08:00
|
|
|
// TODO(rays) This will become a member of TBLOB when TBLOB's definition
|
|
|
|
// moves to blobs.h
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2012-02-02 11:06:39 +08:00
|
|
|
// Returns the center of blob's bounding box in origin.
|
|
|
|
void blob_origin(TBLOB *blob, TPOINT *origin);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
/*blob to compute on */
|
2012-02-02 11:06:39 +08:00
|
|
|
WIDTH_RECORD *blobs_widths(TBLOB *blobs);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
bool divisible_blob(TBLOB *blob, bool italic_blob, TPOINT* location);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void divide_blobs(TBLOB *blob, TBLOB *other_blob, bool italic_blob,
|
|
|
|
const TPOINT& location);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
#endif
|