2007-03-08 04:03:40 +08:00
|
|
|
/**********************************************************************
|
|
|
|
* File: werd.cpp (Formerly word.c)
|
|
|
|
* Description: Code for the WERD class.
|
2010-11-24 02:34:14 +08:00
|
|
|
* Author: Ray Smith
|
|
|
|
* Created: Tue Oct 08 14:32:12 BST 1991
|
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.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
2010-07-22 02:11:00 +08:00
|
|
|
#include "mfcpch.h"
|
2010-11-24 02:34:14 +08:00
|
|
|
#include "blckerr.h"
|
|
|
|
#include "helpers.h"
|
|
|
|
#include "linlsq.h"
|
|
|
|
#include "werd.h"
|
2010-09-30 23:53:40 +08:00
|
|
|
|
2010-05-28 20:03:45 +08:00
|
|
|
// Include automatically generated configuration file if running autoconf.
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config_auto.h"
|
|
|
|
#endif
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
#define FIRST_COLOUR ScrollView::RED //< first rainbow colour
|
|
|
|
#define LAST_COLOUR ScrollView::AQUAMARINE //< last rainbow colour
|
|
|
|
#define CHILD_COLOUR ScrollView::BROWN //< colour of children
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
const ERRCODE CANT_SCALE_EDGESTEPS =
|
2010-11-24 02:34:14 +08:00
|
|
|
"Attempted to scale an edgestep format word";
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-30 08:56:39 +08:00
|
|
|
ELIST2IZE(WERD)
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::WERD
|
|
|
|
*
|
|
|
|
* Constructor to build a WERD from a list of C_BLOBs.
|
2010-11-24 02:34:14 +08:00
|
|
|
* blob_list The C_BLOBs (in word order) are not copied;
|
|
|
|
* we take its elements and put them in our lists.
|
|
|
|
* blank_count blanks in front of the word
|
|
|
|
* text correct text, outlives this WERD
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2010-11-24 02:34:14 +08:00
|
|
|
WERD::WERD(C_BLOB_LIST *blob_list, uinT8 blank_count, const char *text)
|
|
|
|
: blanks(blank_count),
|
|
|
|
flags(0),
|
|
|
|
script_id_(0),
|
|
|
|
correct(text) {
|
|
|
|
C_BLOB_IT start_it = blob_list;
|
|
|
|
C_BLOB_IT end_it = blob_list;
|
2007-03-08 04:03:40 +08:00
|
|
|
C_BLOB_IT rej_cblob_it = &rej_cblobs;
|
2010-11-24 02:34:14 +08:00
|
|
|
C_OUTLINE_IT c_outline_it;
|
2008-04-22 08:41:37 +08:00
|
|
|
inT16 inverted_vote = 0;
|
|
|
|
inT16 non_inverted_vote = 0;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
// Move blob_list's elements into cblobs.
|
|
|
|
while (!end_it.at_last())
|
|
|
|
end_it.forward();
|
|
|
|
cblobs.assign_to_sublist(&start_it, &end_it);
|
|
|
|
|
2007-03-08 04:03:40 +08:00
|
|
|
/*
|
|
|
|
Set white on black flag for the WERD, moving any duff blobs onto the
|
|
|
|
rej_cblobs list.
|
|
|
|
First, walk the cblobs checking the inverse flag for each outline of each
|
|
|
|
cblob. If a cblob has inconsistent flag settings for its different
|
|
|
|
outlines, move the blob to the reject list. Otherwise, increment the
|
|
|
|
appropriate w-on-b or b-on-w vote for the word.
|
|
|
|
|
|
|
|
Now set the inversion flag for the WERD by maximum vote.
|
|
|
|
|
|
|
|
Walk the blobs again, moving any blob whose inversion flag does not agree
|
|
|
|
with the concencus onto the reject list.
|
|
|
|
*/
|
2010-11-24 02:34:14 +08:00
|
|
|
start_it.set_to_list(&cblobs);
|
|
|
|
if (start_it.empty())
|
2007-03-08 04:03:40 +08:00
|
|
|
return;
|
2010-11-24 02:34:14 +08:00
|
|
|
for (start_it.mark_cycle_pt(); !start_it.cycled_list(); start_it.forward()) {
|
|
|
|
BOOL8 reject_blob = FALSE;
|
|
|
|
BOOL8 blob_inverted;
|
|
|
|
|
|
|
|
c_outline_it.set_to_list(start_it.data()->out_list());
|
|
|
|
blob_inverted = c_outline_it.data()->flag(COUT_INVERSE);
|
|
|
|
for (c_outline_it.mark_cycle_pt();
|
|
|
|
!c_outline_it.cycled_list() && !reject_blob;
|
|
|
|
c_outline_it.forward()) {
|
|
|
|
reject_blob = c_outline_it.data()->flag(COUT_INVERSE) != blob_inverted;
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
if (reject_blob) {
|
|
|
|
rej_cblob_it.add_after_then_move(start_it.extract());
|
|
|
|
} else {
|
2007-03-08 04:03:40 +08:00
|
|
|
if (blob_inverted)
|
|
|
|
inverted_vote++;
|
|
|
|
else
|
|
|
|
non_inverted_vote++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
flags.set_bit(W_INVERSE, (inverted_vote > non_inverted_vote));
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
start_it.set_to_list(&cblobs);
|
|
|
|
if (start_it.empty())
|
2007-03-08 04:03:40 +08:00
|
|
|
return;
|
2010-11-24 02:34:14 +08:00
|
|
|
for (start_it.mark_cycle_pt(); !start_it.cycled_list(); start_it.forward()) {
|
|
|
|
c_outline_it.set_to_list(start_it.data()->out_list());
|
|
|
|
if (c_outline_it.data()->flag(COUT_INVERSE) != flags.bit(W_INVERSE))
|
|
|
|
rej_cblob_it.add_after_then_move(start_it.extract());
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::WERD
|
|
|
|
*
|
|
|
|
* Constructor to build a WERD from a list of BLOBs.
|
|
|
|
* The BLOBs are not copied so the source list is emptied.
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
WERD::WERD(PBLOB_LIST *blob_list, //< In word order
|
|
|
|
uinT8 blank_count, //< Blanks in front
|
|
|
|
const char *text) //< Ccorrect text
|
|
|
|
: flags(0),
|
|
|
|
script_id_(0),
|
|
|
|
correct(text) {
|
2007-03-08 04:03:40 +08:00
|
|
|
PBLOB_IT start_it = blob_list; //iterator
|
|
|
|
PBLOB_IT end_it = blob_list; //another
|
|
|
|
|
|
|
|
while (!end_it.at_last ())
|
|
|
|
end_it.forward (); //move to last
|
|
|
|
((PBLOB_LIST *) (&cblobs))->assign_to_sublist (&start_it, &end_it);
|
|
|
|
//move to our list
|
|
|
|
//it's a polygon
|
|
|
|
flags.set_bit (W_POLYGON, TRUE);
|
|
|
|
blanks = blank_count;
|
|
|
|
// fprintf(stderr,"Wrong constructor!!!!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::WERD
|
|
|
|
*
|
|
|
|
* Constructor to build a WERD from a list of BLOBs.
|
|
|
|
* The BLOBs are not copied so the source list is emptied.
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
WERD::WERD(PBLOB_LIST * blob_list, //< In word order
|
|
|
|
WERD * clone) //< Source of flags
|
|
|
|
: flags(clone->flags),
|
|
|
|
script_id_(clone->script_id_),
|
|
|
|
correct(clone->correct) {
|
2007-03-08 04:03:40 +08:00
|
|
|
PBLOB_IT start_it = blob_list; //iterator
|
|
|
|
PBLOB_IT end_it = blob_list; //another
|
|
|
|
|
|
|
|
while (!end_it.at_last ())
|
|
|
|
end_it.forward (); //move to last
|
|
|
|
((PBLOB_LIST *) (&cblobs))->assign_to_sublist (&start_it, &end_it);
|
|
|
|
//move to our list
|
|
|
|
blanks = clone->blanks;
|
|
|
|
// fprintf(stderr,"Wrong constructor!!!!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::WERD
|
|
|
|
*
|
|
|
|
* Constructor to build a WERD from a list of C_BLOBs.
|
|
|
|
* The C_BLOBs are not copied so the source list is emptied.
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
WERD::WERD(C_BLOB_LIST * blob_list, //< In word order
|
|
|
|
WERD * clone) //< Source of flags
|
|
|
|
: flags(clone->flags),
|
|
|
|
script_id_(clone->script_id_),
|
|
|
|
correct(clone->correct) {
|
|
|
|
C_BLOB_IT start_it = blob_list; // iterator
|
|
|
|
C_BLOB_IT end_it = blob_list; // another
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
while (!end_it.at_last ())
|
|
|
|
end_it.forward (); //move to last
|
|
|
|
((C_BLOB_LIST *) (&cblobs))->assign_to_sublist (&start_it, &end_it);
|
|
|
|
//move to our list
|
|
|
|
blanks = clone->blanks;
|
|
|
|
// fprintf(stderr,"Wrong constructor!!!!\n");
|
|
|
|
}
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
// Construct a WERD from a single_blob and clone the flags from this.
|
|
|
|
// W_BOL and W_EOL flags are set according to the given values.
|
|
|
|
WERD* WERD::ConstructFromSingleBlob(bool bol, bool eol, C_BLOB* blob) {
|
|
|
|
C_BLOB_LIST temp_blobs;
|
|
|
|
C_BLOB_IT temp_it(&temp_blobs);
|
|
|
|
temp_it.add_after_then_move(blob);
|
|
|
|
WERD* blob_word = new WERD(&temp_blobs, this);
|
|
|
|
blob_word->set_flag(W_BOL, bol);
|
|
|
|
blob_word->set_flag(W_EOL, eol);
|
|
|
|
return blob_word;
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::bounding_box
|
|
|
|
*
|
|
|
|
* Return the bounding box of the WERD.
|
|
|
|
* This is quite a mess to compute!
|
|
|
|
* ORIGINALLY, REJECT CBLOBS WERE EXCLUDED, however, this led to bugs when the
|
|
|
|
* words on the row were re-sorted. The original words were built with reject
|
|
|
|
* blobs included. The FUZZY SPACE flags were set accordingly. If ALL the
|
|
|
|
* blobs in a word are rejected the BB for the word is NULL, causing the sort
|
|
|
|
* to screw up, leading to the erroneous possibility of the first word in a
|
|
|
|
* row being marked as FUZZY space.
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
TBOX WERD::bounding_box() {
|
|
|
|
TBOX box; // box being built
|
|
|
|
C_BLOB_IT rej_cblob_it = &rej_cblobs; // rejected blobs
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
for (rej_cblob_it.mark_cycle_pt(); !rej_cblob_it.cycled_list();
|
|
|
|
rej_cblob_it.forward()) {
|
|
|
|
box += rej_cblob_it.data()->bounding_box();
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
if (flags.bit(W_POLYGON)) { // polygons
|
|
|
|
PBLOB_IT it = (PBLOB_LIST *)(&cblobs);
|
|
|
|
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
|
|
|
|
box += it.data()->bounding_box();
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
|
|
|
C_BLOB_IT it = &cblobs; // blobs of WERD
|
|
|
|
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
|
|
|
|
box += it.data()->bounding_box();
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::move
|
|
|
|
*
|
|
|
|
* Reposition WERD by vector
|
|
|
|
* NOTE!! REJECT CBLOBS ARE NOT MOVED
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void WERD::move(const ICOORD vec) {
|
|
|
|
PBLOB_IT blob_it((PBLOB_LIST *)&cblobs);
|
2007-03-08 04:03:40 +08:00
|
|
|
C_BLOB_IT cblob_it(&cblobs); // cblob iterator
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
if (flags.bit(W_POLYGON)) {
|
|
|
|
for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward())
|
|
|
|
blob_it.data()->move(vec);
|
|
|
|
} else {
|
|
|
|
for (cblob_it.mark_cycle_pt(); !cblob_it.cycled_list(); cblob_it.forward())
|
|
|
|
cblob_it.data()->move(vec);
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::scale
|
|
|
|
*
|
|
|
|
* Scale WERD by multiplier
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void WERD::scale(const float f) {
|
|
|
|
PBLOB_IT blob_it((PBLOB_LIST *)&cblobs);
|
|
|
|
|
|
|
|
if (flags.bit(W_POLYGON)) {
|
|
|
|
for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward())
|
|
|
|
blob_it.data()->scale(f);
|
|
|
|
} else {
|
|
|
|
CANT_SCALE_EDGESTEPS.error("WERD::scale", ABORT, NULL);
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::join_on
|
|
|
|
*
|
|
|
|
* Join other word onto this one. Delete the old word.
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void WERD::join_on(WERD* other) {
|
|
|
|
PBLOB_IT blob_it((PBLOB_LIST *)&cblobs);
|
|
|
|
PBLOB_IT src_it((PBLOB_LIST *)&other->cblobs);
|
2008-04-22 08:41:37 +08:00
|
|
|
C_BLOB_IT rej_cblob_it(&rej_cblobs);
|
2010-11-24 02:34:14 +08:00
|
|
|
C_BLOB_IT src_rej_it(&other->rej_cblobs);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
while (!src_it.empty()) {
|
|
|
|
blob_it.add_to_end(src_it.extract());
|
|
|
|
src_it.forward();
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
while (!src_rej_it.empty()) {
|
|
|
|
rej_cblob_it.add_to_end(src_rej_it.extract());
|
|
|
|
src_rej_it.forward();
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::copy_on
|
|
|
|
*
|
|
|
|
* Copy blobs from other word onto this one.
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void WERD::copy_on(WERD* other) {
|
|
|
|
bool reversed = other->bounding_box().left() < bounding_box().left();
|
|
|
|
if (flags.bit(W_POLYGON)) {
|
|
|
|
PBLOB_IT blob_it((PBLOB_LIST *) & cblobs);
|
2007-03-08 04:03:40 +08:00
|
|
|
// blob iterator
|
|
|
|
PBLOB_LIST blobs;
|
|
|
|
|
2008-12-31 02:15:44 +08:00
|
|
|
blobs.deep_copy(reinterpret_cast<PBLOB_LIST*>(&other->cblobs),
|
|
|
|
&PBLOB::deep_copy);
|
2010-11-24 02:34:14 +08:00
|
|
|
if (reversed) {
|
|
|
|
blob_it.add_list_before(&blobs);
|
|
|
|
} else {
|
|
|
|
blob_it.move_to_last();
|
|
|
|
blob_it.add_list_after(&blobs);
|
|
|
|
}
|
2008-12-31 02:15:44 +08:00
|
|
|
} else {
|
2008-04-22 08:41:37 +08:00
|
|
|
C_BLOB_IT c_blob_it(&cblobs);
|
2007-03-08 04:03:40 +08:00
|
|
|
C_BLOB_LIST c_blobs;
|
|
|
|
|
2008-12-31 02:15:44 +08:00
|
|
|
c_blobs.deep_copy(&other->cblobs, &C_BLOB::deep_copy);
|
2010-11-24 02:34:14 +08:00
|
|
|
if (reversed) {
|
|
|
|
c_blob_it.add_list_before(&c_blobs);
|
|
|
|
} else {
|
|
|
|
c_blob_it.move_to_last();
|
|
|
|
c_blob_it.add_list_after(&c_blobs);
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
if (!other->rej_cblobs.empty()) {
|
2008-04-22 08:41:37 +08:00
|
|
|
C_BLOB_IT rej_c_blob_it(&rej_cblobs);
|
2007-03-08 04:03:40 +08:00
|
|
|
C_BLOB_LIST new_rej_c_blobs;
|
|
|
|
|
2008-12-31 02:15:44 +08:00
|
|
|
new_rej_c_blobs.deep_copy(&other->rej_cblobs, &C_BLOB::deep_copy);
|
2010-11-24 02:34:14 +08:00
|
|
|
if (reversed) {
|
|
|
|
rej_c_blob_it.add_list_before(&new_rej_c_blobs);
|
|
|
|
} else {
|
|
|
|
rej_c_blob_it.move_to_last();
|
|
|
|
rej_c_blob_it.add_list_after(&new_rej_c_blobs);
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::print
|
|
|
|
*
|
|
|
|
* Display members
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void WERD::print() {
|
|
|
|
tprintf("Blanks= %d\n", blanks);
|
|
|
|
bounding_box().print();
|
|
|
|
tprintf("Flags = %d = 0%o\n", flags.val, flags.val);
|
|
|
|
tprintf(" W_SEGMENTED = %s\n", flags.bit(W_SEGMENTED) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_ITALIC = %s\n", flags.bit(W_ITALIC) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_BOL = %s\n", flags.bit(W_BOL) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_EOL = %s\n", flags.bit(W_EOL) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_NORMALIZED = %s\n",
|
|
|
|
flags.bit(W_NORMALIZED) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_POLYGON = %s\n", flags.bit(W_POLYGON) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_SCRIPT_HAS_XHEIGHT = %s\n",
|
|
|
|
flags.bit(W_SCRIPT_HAS_XHEIGHT) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_SCRIPT_IS_LATIN = %s\n",
|
|
|
|
flags.bit(W_SCRIPT_IS_LATIN) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_DONT_CHOP = %s\n", flags.bit(W_DONT_CHOP) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_REP_CHAR = %s\n", flags.bit(W_REP_CHAR) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_FUZZY_SP = %s\n", flags.bit(W_FUZZY_SP) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf(" W_FUZZY_NON = %s\n", flags.bit(W_FUZZY_NON) ? "TRUE" : "FALSE ");
|
|
|
|
tprintf("Correct= %s\n", correct.string());
|
|
|
|
tprintf("Rejected cblob count = %d\n", rej_cblobs.length());
|
|
|
|
tprintf("Script = %d\n", script_id_);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::plot
|
|
|
|
*
|
|
|
|
* Draw the WERD in the given colour.
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
#ifndef GRAPHICS_DISABLED
|
2010-11-24 02:34:14 +08:00
|
|
|
void WERD::plot(ScrollView *window, ScrollView::Color colour) {
|
|
|
|
if (flags.bit(W_POLYGON)) { // polygon blobs
|
|
|
|
PBLOB_IT it = (PBLOB_LIST *)(&cblobs);
|
|
|
|
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
|
|
|
|
it.data()->plot(window, colour, colour);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
} else { // chain code blobs
|
|
|
|
C_BLOB_IT it = &cblobs;
|
|
|
|
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
|
|
|
|
it.data()->plot(window, colour, colour);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
plot_rej_blobs(window);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
#ifndef GRAPHICS_DISABLED
|
|
|
|
|
|
|
|
// Get the next color in the (looping) rainbow.
|
|
|
|
ScrollView::Color WERD::NextColor(ScrollView::Color colour) {
|
|
|
|
ScrollView::Color next = static_cast<ScrollView::Color>(colour + 1);
|
|
|
|
if (next >= LAST_COLOUR || next < FIRST_COLOUR)
|
|
|
|
next = FIRST_COLOUR;
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::plot
|
|
|
|
*
|
2010-11-24 02:34:14 +08:00
|
|
|
* Draw the WERD in rainbow colours in window.
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
void WERD::plot(ScrollView* window) {
|
|
|
|
ScrollView::Color colour = FIRST_COLOUR;
|
|
|
|
if (flags.bit(W_POLYGON)) {
|
|
|
|
PBLOB_IT it = (PBLOB_LIST *)(&cblobs);
|
|
|
|
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
|
|
|
|
it.data()->plot(window, colour, CHILD_COLOUR);
|
|
|
|
colour = NextColor(colour);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
} else {
|
|
|
|
C_BLOB_IT it = &cblobs;
|
|
|
|
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
|
|
|
|
it.data()->plot(window, colour, CHILD_COLOUR);
|
|
|
|
colour = NextColor(colour);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
2010-11-24 02:34:14 +08:00
|
|
|
plot_rej_blobs(window);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::plot_rej_blobs
|
|
|
|
*
|
2010-11-24 02:34:14 +08:00
|
|
|
* Draw the WERD rejected blobs in window - ALWAYS GREY
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
#ifndef GRAPHICS_DISABLED
|
2010-11-24 02:34:14 +08:00
|
|
|
void WERD::plot_rej_blobs(ScrollView *window) {
|
|
|
|
if (flags.bit(W_POLYGON)) {
|
|
|
|
PBLOB_IT it = (PBLOB_LIST *)(&rej_cblobs);
|
|
|
|
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
|
|
|
|
it.data()->plot(window, ScrollView::GREY, ScrollView::GREY);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2008-12-31 02:15:44 +08:00
|
|
|
} else {
|
2010-11-24 02:34:14 +08:00
|
|
|
C_BLOB_IT it = &rej_cblobs;
|
|
|
|
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
|
|
|
|
it.data()->plot(window, ScrollView::GREY, ScrollView::GREY);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::shallow_copy()
|
|
|
|
*
|
|
|
|
* Make a shallow copy of a word
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
WERD *WERD::shallow_copy() {
|
2007-03-08 04:03:40 +08:00
|
|
|
WERD *new_word = new WERD;
|
|
|
|
|
|
|
|
new_word->blanks = blanks;
|
|
|
|
new_word->flags = flags;
|
|
|
|
new_word->dummy = dummy;
|
|
|
|
new_word->correct = correct;
|
|
|
|
return new_word;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* WERD::operator=
|
|
|
|
*
|
|
|
|
* Assign a word, DEEP copying the blob list
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
WERD & WERD::operator= (const WERD & source) {
|
|
|
|
this->ELIST2_LINK::operator= (source);
|
2007-03-08 04:03:40 +08:00
|
|
|
blanks = source.blanks;
|
|
|
|
flags = source.flags;
|
2010-11-24 02:34:14 +08:00
|
|
|
script_id_ = source.script_id_;
|
2007-03-08 04:03:40 +08:00
|
|
|
dummy = source.dummy;
|
|
|
|
correct = source.correct;
|
2010-11-24 02:34:14 +08:00
|
|
|
if (flags.bit(W_POLYGON)) {
|
2008-12-31 02:15:44 +08:00
|
|
|
if (!cblobs.empty())
|
|
|
|
reinterpret_cast<PBLOB_LIST*>(&cblobs)->clear();
|
|
|
|
reinterpret_cast<PBLOB_LIST*>(&cblobs)->deep_copy(
|
|
|
|
reinterpret_cast<const PBLOB_LIST*>(&source.cblobs), &PBLOB::deep_copy);
|
|
|
|
|
|
|
|
if (!rej_cblobs.empty())
|
|
|
|
reinterpret_cast<PBLOB_LIST*>(&rej_cblobs)->clear();
|
|
|
|
reinterpret_cast<PBLOB_LIST*>(&rej_cblobs)->deep_copy(
|
|
|
|
reinterpret_cast<const PBLOB_LIST*>(&source.rej_cblobs),
|
|
|
|
&PBLOB::deep_copy);
|
|
|
|
} else {
|
2010-11-24 02:34:14 +08:00
|
|
|
if (!cblobs.empty())
|
|
|
|
cblobs.clear();
|
2008-12-31 02:15:44 +08:00
|
|
|
cblobs.deep_copy(&source.cblobs, &C_BLOB::deep_copy);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
if (!rej_cblobs.empty())
|
|
|
|
rej_cblobs.clear();
|
2008-12-31 02:15:44 +08:00
|
|
|
rej_cblobs.deep_copy(&source.rej_cblobs, &C_BLOB::deep_copy);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 21:23:23 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* word_comparator()
|
|
|
|
*
|
|
|
|
* word comparator used to sort a word list so that words are in increasing
|
|
|
|
* order of left edge.
|
2010-07-27 21:23:23 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
int word_comparator(const void *word1p, const void *word2p) {
|
|
|
|
WERD *word1 = *(WERD **)word1p;
|
|
|
|
WERD *word2 = *(WERD **)word2p;
|
|
|
|
return word1->bounding_box().left() - word2->bounding_box().left();
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
/**
|
|
|
|
* WERD::ConstructWerdWithNewBlobs()
|
|
|
|
*
|
|
|
|
* This method returns a new werd constructed using the blobs in the input
|
|
|
|
* all_blobs list, which correspond to the blobs in this werd object. The
|
|
|
|
* blobs used to construct the new word are consumed and removed from the
|
|
|
|
* input all_blobs list.
|
|
|
|
* Returns NULL if the word couldn't be constructed.
|
|
|
|
* Returns original blobs for which no matches were found in the output list
|
|
|
|
* orphan_blobs (appends).
|
|
|
|
*/
|
|
|
|
|
|
|
|
WERD* WERD::ConstructWerdWithNewBlobs(C_BLOB_LIST* all_blobs,
|
|
|
|
C_BLOB_LIST* orphan_blobs) {
|
|
|
|
C_BLOB_LIST current_blob_list;
|
|
|
|
C_BLOB_IT werd_blobs_it(¤t_blob_list);
|
|
|
|
// Add the word's c_blobs.
|
|
|
|
werd_blobs_it.add_list_after(cblob_list());
|
|
|
|
|
|
|
|
// New blob list. These contain the blobs which will form the new word.
|
|
|
|
C_BLOB_LIST new_werd_blobs;
|
|
|
|
C_BLOB_IT new_blobs_it(&new_werd_blobs);
|
|
|
|
|
|
|
|
// not_found_blobs contains the list of current word's blobs for which a
|
|
|
|
// corresponding blob wasn't found in the input all_blobs list.
|
|
|
|
C_BLOB_LIST not_found_blobs;
|
|
|
|
C_BLOB_IT not_found_it(¬_found_blobs);
|
|
|
|
not_found_it.move_to_last();
|
|
|
|
|
|
|
|
werd_blobs_it.move_to_first();
|
|
|
|
for (werd_blobs_it.mark_cycle_pt(); !werd_blobs_it.cycled_list();
|
|
|
|
werd_blobs_it.forward()) {
|
|
|
|
C_BLOB* werd_blob = werd_blobs_it.extract();
|
|
|
|
TBOX werd_blob_box = werd_blob->bounding_box();
|
|
|
|
bool found = false;
|
|
|
|
// Now find the corresponding blob for this blob in the all_blobs
|
|
|
|
// list. For now, follow the inefficient method of pairwise
|
|
|
|
// comparisons. Ideally, one can pre-bucket the blobs by row.
|
|
|
|
C_BLOB_IT all_blobs_it(all_blobs);
|
|
|
|
for (all_blobs_it.mark_cycle_pt(); !all_blobs_it.cycled_list();
|
|
|
|
all_blobs_it.forward()) {
|
|
|
|
C_BLOB* a_blob = all_blobs_it.data();
|
|
|
|
// Compute the overlap of the two blobs. If major, a_blob should
|
|
|
|
// be added to the new blobs list.
|
|
|
|
TBOX a_blob_box = a_blob->bounding_box();
|
|
|
|
if (a_blob_box.null_box()) {
|
|
|
|
tprintf("Bounding box couldn't be ascertained\n");
|
|
|
|
}
|
|
|
|
if (werd_blob_box.contains(a_blob_box) ||
|
|
|
|
werd_blob_box.major_overlap(a_blob_box)) {
|
|
|
|
// Old blobs are from minimal splits, therefore are expected to be
|
|
|
|
// bigger. The new small blobs should cover a significant portion.
|
|
|
|
// This is it.
|
|
|
|
all_blobs_it.extract();
|
|
|
|
new_blobs_it.add_after_then_move(a_blob);
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
not_found_it.add_after_then_move(werd_blob);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Iterate over all not found blobs. Some of them may be due to
|
|
|
|
// under-segmentation (which is OK, since the corresponding blob is already
|
|
|
|
// in the list in that case.
|
|
|
|
not_found_it.move_to_first();
|
|
|
|
for (not_found_it.mark_cycle_pt(); !not_found_it.cycled_list();
|
|
|
|
not_found_it.forward()) {
|
|
|
|
C_BLOB* not_found = not_found_it.data();
|
|
|
|
TBOX not_found_box = not_found->bounding_box();
|
|
|
|
bool found = false;
|
|
|
|
C_BLOB_IT existing_blobs_it(new_blobs_it);
|
|
|
|
for (existing_blobs_it.mark_cycle_pt(); !existing_blobs_it.cycled_list();
|
|
|
|
existing_blobs_it.forward()) {
|
|
|
|
C_BLOB* a_blob = existing_blobs_it.data();
|
|
|
|
TBOX a_blob_box = a_blob->bounding_box();
|
|
|
|
if ((not_found_box.major_overlap(a_blob_box) ||
|
|
|
|
a_blob_box.major_overlap(not_found_box)) &&
|
|
|
|
not_found_box.y_overlap(a_blob_box) > 0.8) {
|
|
|
|
// Already taken care of.
|
|
|
|
found = true;
|
|
|
|
not_found_it.extract();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (orphan_blobs) {
|
|
|
|
C_BLOB_IT orphan_blobs_it(orphan_blobs);
|
|
|
|
orphan_blobs_it.move_to_last();
|
|
|
|
orphan_blobs_it.add_list_after(¬_found_blobs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// New blobs are ready. Create a new werd object with these.
|
|
|
|
WERD* new_werd = NULL;
|
|
|
|
if (!new_werd_blobs.empty()) {
|
|
|
|
new_werd = new WERD(&new_werd_blobs, this);
|
|
|
|
}
|
|
|
|
return new_werd;
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|