Use C++-11 code instead of TessCallback for WidthCallback

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2019-07-03 22:51:10 +02:00
parent 1c1eb76c36
commit 0ea8ada308
12 changed files with 49 additions and 59 deletions

View File

@ -1,10 +1,9 @@
///////////////////////////////////////////////////////////////////////
// File: ccnontextdetect.cpp
// Description: Connected-Component-based photo (non-text) detection.
// Copyright 2011 Google Inc. All Rights Reserved.
// Author: rays@google.com (Ray Smith)
// Created: Sat Jun 11 10:12:01 PST 2011
// Author: rays@google.com (Ray Smith)
//
// Copyright 2011 Google Inc. All Rights Reserved.
// 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

View File

@ -645,7 +645,7 @@ ColPartition* ColPartition::SingletonPartner(bool upper) {
}
// Merge with the other partition and delete it.
void ColPartition::Absorb(ColPartition* other, WidthCallback* cb) {
void ColPartition::Absorb(ColPartition* other, WidthCallback cb) {
// The result has to either own all of the blobs or none of them.
// Verify the flag is consistent.
ASSERT_HOST(owns_blobs() == other->owns_blobs());
@ -1077,10 +1077,10 @@ void ColPartition::ColumnRange(int resolution, ColPartitionSet* columns,
}
// Sets the internal flags good_width_ and good_column_.
void ColPartition::SetColumnGoodness(WidthCallback* cb) {
void ColPartition::SetColumnGoodness(WidthCallback cb) {
int y = MidY();
int width = RightAtY(y) - LeftAtY(y);
good_width_ = cb->Run(width);
good_width_ = cb(width);
good_column_ = blob_type_ == BRT_TEXT && left_key_tab_ && right_key_tab_;
}

View File

@ -3,7 +3,6 @@
// Description: Class to hold partitions of the page that correspond
// roughly to text lines.
// Author: Ray Smith
// Created: Thu Aug 14 10:50:01 PDT 2008
//
// (C) Copyright 2008, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
@ -573,7 +572,7 @@ class ColPartition : public ELIST2_LINK {
ColPartition* SingletonPartner(bool upper);
// Merge with the other partition and delete it.
void Absorb(ColPartition* other, WidthCallback* cb);
void Absorb(ColPartition* other, WidthCallback cb);
// Returns true if the overlap between this and the merged pair of
// merge candidates is sufficiently trivial to be allowed.
@ -618,7 +617,7 @@ class ColPartition : public ELIST2_LINK {
int* first_col, int* last_col);
// Sets the internal flags good_width_ and good_column_.
void SetColumnGoodness(WidthCallback* cb);
void SetColumnGoodness(WidthCallback cb);
// Determines whether the blobs in this partition mostly represent
// a leader (fixed pitch sequence) and sets the member blobs accordingly.

View File

@ -99,9 +99,8 @@ void ColPartitionGrid::HandleClick(int x, int y) {
// true, then the partitions are merged.
// Both callbacks are deleted before returning.
void ColPartitionGrid::Merges(
TessResultCallback2<bool, ColPartition*, TBOX*>* box_cb,
TessResultCallback2<bool, const ColPartition*,
const ColPartition*>* confirm_cb) {
std::function<bool(ColPartition*, TBOX*)> box_cb,
std::function<bool(const ColPartition*, const ColPartition*)> confirm_cb) {
// Iterate the ColPartitions in the grid.
ColPartitionGridSearch gsearch(this);
gsearch.StartFullSearch();
@ -110,8 +109,6 @@ void ColPartitionGrid::Merges(
if (MergePart(box_cb, confirm_cb, part))
gsearch.RepositionIterator();
}
delete box_cb;
delete confirm_cb;
}
// For the given partition, calls the box_cb permanent callback
@ -120,9 +117,8 @@ void ColPartitionGrid::Merges(
// true, then the partitions are merged.
// Returns true if the partition is consumed by one or more merges.
bool ColPartitionGrid::MergePart(
TessResultCallback2<bool, ColPartition*, TBOX*>* box_cb,
TessResultCallback2<bool, const ColPartition*,
const ColPartition*>* confirm_cb,
std::function<bool(ColPartition*, TBOX*)> box_cb,
std::function<bool(const ColPartition*, const ColPartition*)> confirm_cb,
ColPartition* part) {
if (part->IsUnMergeableType())
return false;
@ -138,7 +134,7 @@ bool ColPartitionGrid::MergePart(
box.print();
}
// Set up a rectangle search bounded by the part.
if (!box_cb->Run(part, &box))
if (!box_cb(part, &box))
continue;
// Create a list of merge candidates.
ColPartition_CLIST merge_candidates;
@ -404,7 +400,7 @@ void ColPartitionGrid::FindOverlappingPartitions(const TBOX& box,
// in overlap, or tightly spaced text would end up in bits.
ColPartition* ColPartitionGrid::BestMergeCandidate(
const ColPartition* part, ColPartition_CLIST* candidates, bool debug,
TessResultCallback2<bool, const ColPartition*, const ColPartition*>* confirm_cb,
std::function<bool(const ColPartition*, const ColPartition*)> confirm_cb,
int* overlap_increase) {
if (overlap_increase != nullptr)
*overlap_increase = 0;
@ -448,7 +444,7 @@ ColPartition* ColPartitionGrid::BestMergeCandidate(
int best_area = 0;
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
ColPartition* candidate = it.data();
if (confirm_cb != nullptr && !confirm_cb->Run(part, candidate)) {
if (confirm_cb != nullptr && !confirm_cb(part, candidate)) {
if (debug) {
tprintf("Candidate not confirmed:");
candidate->bounding_box().print();
@ -818,7 +814,7 @@ bool ColPartitionGrid::MakeColPartSets(PartSetVector* part_sets) {
// represents the total horizontal extent of the significant content on the
// page. Used for the single column setting in place of automatic detection.
// Returns nullptr if the page is empty of significant content.
ColPartitionSet* ColPartitionGrid::MakeSingleColumnSet(WidthCallback* cb) {
ColPartitionSet* ColPartitionGrid::MakeSingleColumnSet(WidthCallback cb) {
ColPartition* single_column_part = nullptr;
// Iterate the ColPartitions in the grid to get parts onto lists for the
// y bottom of each.

View File

@ -2,7 +2,6 @@
// File: colpartitiongrid.h
// Description: Class collecting code that acts on a BBGrid of ColPartitions.
// Author: Ray Smith
// Created: Mon Oct 05 08:42:01 PDT 2009
//
// (C) Copyright 2009, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
@ -49,18 +48,18 @@ class ColPartitionGrid : public BBGrid<ColPartition,
// calls the confirm_cb to check any more rules. If the confirm_cb returns
// true, then the partitions are merged.
// Both callbacks are deleted before returning.
void Merges(TessResultCallback2<bool, ColPartition*, TBOX*>* box_cb,
TessResultCallback2<bool, const ColPartition*,
const ColPartition*>* confirm_cb);
void Merges(std::function<bool(ColPartition*, TBOX*)> box_cb,
std::function<bool(const ColPartition*,
const ColPartition*)> confirm_cb);
// For the given partition, calls the box_cb permanent callback
// to compute the search box, searches the box, and if a candidate is found,
// calls the confirm_cb to check any more rules. If the confirm_cb returns
// true, then the partitions are merged.
// Returns true if the partition is consumed by one or more merges.
bool MergePart(TessResultCallback2<bool, ColPartition*, TBOX*>* box_cb,
TessResultCallback2<bool, const ColPartition*,
const ColPartition*>* confirm_cb,
bool MergePart(std::function<bool(ColPartition*, TBOX*)> box_cb,
std::function<bool(const ColPartition*,
const ColPartition*)> confirm_cb,
ColPartition* part);
// Computes and returns the total overlap of all partitions in the grid.
@ -82,8 +81,8 @@ class ColPartitionGrid : public BBGrid<ColPartition,
// See colpartitiongrid.cpp for a diagram.
ColPartition* BestMergeCandidate(
const ColPartition* part, ColPartition_CLIST* candidates, bool debug,
TessResultCallback2<bool, const ColPartition*,
const ColPartition*>* confirm_cb,
std::function<bool(const ColPartition*,
const ColPartition*)> confirm_cb,
int* overlap_increase);
// Split partitions where it reduces overlap between their bounding boxes.
@ -133,7 +132,7 @@ class ColPartitionGrid : public BBGrid<ColPartition,
// represents the total horizontal extent of the significant content on the
// page. Used for the single column setting in place of automatic detection.
// Returns nullptr if the page is empty of significant content.
ColPartitionSet* MakeSingleColumnSet(WidthCallback* cb);
ColPartitionSet* MakeSingleColumnSet(WidthCallback cb);
// Mark the BLOBNBOXes in each partition as being owned by that partition.
void ClaimBoxes();

View File

@ -3,7 +3,6 @@
// Description: Class to hold a list of ColPartitions of the page that
// correspond roughly to columns.
// Author: Ray Smith
// Created: Thu Aug 14 10:54:01 PDT 2008
//
// (C) Copyright 2008, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
@ -87,7 +86,7 @@ void ColPartitionSet::RelinquishParts() {
}
// Attempt to improve this by adding partitions or expanding partitions.
void ColPartitionSet::ImproveColumnCandidate(WidthCallback* cb,
void ColPartitionSet::ImproveColumnCandidate(WidthCallback cb,
PartSetVector* src_sets) {
int set_size = src_sets->size();
// Iterate over the provided column sets, as each one may have something
@ -124,13 +123,13 @@ void ColPartitionSet::ImproveColumnCandidate(WidthCallback* cb,
continue;
}
// Check the edges of col_part to see if they can improve part.
bool part_width_ok = cb->Run(part->KeyWidth(part_left, part_right));
bool part_width_ok = cb(part->KeyWidth(part_left, part_right));
if (col_left < part_left && col_left > prev_right) {
// The left edge of the column is better and it doesn't overlap,
// so we can potentially expand it.
int col_box_left = col_part->BoxLeftKey();
bool tab_width_ok = cb->Run(part->KeyWidth(col_left, part_right));
bool box_width_ok = cb->Run(part->KeyWidth(col_box_left, part_right));
bool tab_width_ok = cb(part->KeyWidth(col_left, part_right));
bool box_width_ok = cb(part->KeyWidth(col_box_left, part_right));
if (tab_width_ok || (!part_width_ok)) {
// The tab is leaving the good column metric at least as good as
// it was before, so use the tab.
@ -150,8 +149,8 @@ void ColPartitionSet::ImproveColumnCandidate(WidthCallback* cb,
part_it.data_relative(1)->left_key() > col_right)) {
// The right edge is better, so we can possibly expand it.
int col_box_right = col_part->BoxRightKey();
bool tab_width_ok = cb->Run(part->KeyWidth(part_left, col_right));
bool box_width_ok = cb->Run(part->KeyWidth(part_left, col_box_right));
bool tab_width_ok = cb(part->KeyWidth(part_left, col_right));
bool box_width_ok = cb(part->KeyWidth(part_left, col_box_right));
if (tab_width_ok || (!part_width_ok)) {
// The tab is leaving the good column metric at least as good as
// it was before, so use the tab.
@ -173,7 +172,7 @@ void ColPartitionSet::ImproveColumnCandidate(WidthCallback* cb,
// If this set is good enough to represent a new partitioning into columns,
// add it to the vector of sets, otherwise delete it.
void ColPartitionSet::AddToColumnSetsIfUnique(PartSetVector* column_sets,
WidthCallback* cb) {
WidthCallback cb) {
bool debug = TabFind::WithinTestRegion(2, bounding_box_.left(),
bounding_box_.bottom());
if (debug) {
@ -221,7 +220,7 @@ void ColPartitionSet::AddToColumnSetsIfUnique(PartSetVector* column_sets,
// Return true if the partitions in other are all compatible with the columns
// in this.
bool ColPartitionSet::CompatibleColumns(bool debug, ColPartitionSet* other,
WidthCallback* cb) {
WidthCallback cb) {
if (debug) {
tprintf("CompatibleColumns testing compatibility\n");
Print();
@ -254,7 +253,7 @@ bool ColPartitionSet::CompatibleColumns(bool debug, ColPartitionSet* other,
}
return false; // A partition edge lies outside of all columns
}
if (right_col != left_col && cb->Run(right - left)) {
if (right_col != left_col && cb(right - left)) {
if (debug) {
tprintf("CompatibleColumns false due to good width in multiple cols\n");
part->Print();

View File

@ -3,7 +3,6 @@
// Description: Class to hold a list of ColPartitions of the page that
// correspond roughly to columns.
// Author: Ray Smith
// Created: Thu Aug 14 10:50:01 PDT 2008
//
// (C) Copyright 2008, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
@ -72,15 +71,15 @@ class ColPartitionSet : public ELIST_LINK {
void RelinquishParts();
// Attempt to improve this by adding partitions or expanding partitions.
void ImproveColumnCandidate(WidthCallback* cb, PartSetVector* src_sets);
void ImproveColumnCandidate(WidthCallback cb, PartSetVector* src_sets);
// If this set is good enough to represent a new partitioning into columns,
// add it to the vector of sets, otherwise delete it.
void AddToColumnSetsIfUnique(PartSetVector* column_sets, WidthCallback* cb);
void AddToColumnSetsIfUnique(PartSetVector* column_sets, WidthCallback cb);
// Return true if the partitions in other are all compatible with the columns
// in this.
bool CompatibleColumns(bool debug, ColPartitionSet* other, WidthCallback* cb);
bool CompatibleColumns(bool debug, ColPartitionSet* other, WidthCallback cb);
// Returns the total width of all blobs in the part_set that do not lie
// within an approved column. Used as a cost measure for using this

View File

@ -1865,9 +1865,10 @@ void StrokeWidth::CompletePartition(PageSegMode pageseg_mode,
// Merge partitions where the merge appears harmless.
// As this
void StrokeWidth::EasyMerges(ColPartitionGrid* part_grid) {
using namespace std::placeholders; // for _1, _2
part_grid->Merges(
NewPermanentTessCallback(this, &StrokeWidth::OrientationSearchBox),
NewPermanentTessCallback(this, &StrokeWidth::ConfirmEasyMerge));
std::bind(&StrokeWidth::OrientationSearchBox, this, _1, _2),
std::bind(&StrokeWidth::ConfirmEasyMerge, this, _1, _2));
}
// Compute a search box based on the orientation of the partition.

View File

@ -72,11 +72,11 @@ TabFind::TabFind(int gridsize, const ICOORD& bleft, const ICOORD& tright,
width_cb_ = nullptr;
v_it_.add_list_after(vlines);
SetVerticalSkewAndParallelize(vertical_x, vertical_y);
width_cb_ = NewPermanentTessCallback(this, &TabFind::CommonWidth);
using namespace std::placeholders; // for _1
width_cb_ = std::bind(&TabFind::CommonWidth, this, _1);
}
TabFind::~TabFind() {
delete width_cb_;
}
///////////////// PUBLIC functions (mostly used by TabVector). //////////////

View File

@ -2,7 +2,6 @@
// File: tabfind.h
// Description: Subclass of BBGrid to find tabstops.
// Author: Ray Smith
// Created: Fri Mar 21 15:03:01 PST 2008
//
// (C) Copyright 2008, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
@ -20,8 +19,8 @@
#ifndef TESSERACT_TEXTORD_TABFIND_H_
#define TESSERACT_TEXTORD_TABFIND_H_
#include <functional> // for std::function
#include "alignedblob.h"
#include "tesscallback.h"
#include "tabvector.h"
#include "linefind.h"
@ -33,7 +32,7 @@ struct Pix;
namespace tesseract {
using WidthCallback = TessResultCallback1<bool, int>;
using WidthCallback = std::function<bool(int)>;
struct AlignedBlobParams;
class ColPartitionGrid;
@ -155,7 +154,7 @@ class TabFind : public AlignedBlob {
/**
* Return a callback for testing CommonWidth.
*/
WidthCallback* WidthCB() {
WidthCallback WidthCB() {
return width_cb_;
}
@ -374,7 +373,7 @@ class TabFind : public AlignedBlob {
// List of commonly occurring width ranges with x=min and y=max.
ICOORDELT_LIST column_widths_; ///< List of commonly occurring width ranges.
/** Callback to test an int for being a common width. */
WidthCallback* width_cb_;
WidthCallback width_cb_;
// Sets of bounding boxes that are candidate tab stops.
GenericVector<BLOBNBOX*> left_tab_boxes_;
GenericVector<BLOBNBOX*> right_tab_boxes_;

View File

@ -258,7 +258,7 @@ void TableFinder::InsertCleanPartitions(ColPartitionGrid* grid,
// High level function to perform table detection
void TableFinder::LocateTables(ColPartitionGrid* grid,
ColPartitionSet** all_columns,
WidthCallback* width_cb,
WidthCallback width_cb,
const FCOORD& reskew) {
// initialize spacing, neighbors, and columns
InitializePartitions(all_columns);
@ -1996,7 +1996,7 @@ void TableFinder::DisplayColPartitionConnections(
// assigned to any table to their original types.
void TableFinder::MakeTableBlocks(ColPartitionGrid* grid,
ColPartitionSet** all_columns,
WidthCallback* width_cb) {
WidthCallback width_cb) {
// Since we have table blocks already, remove table tags from all
// colpartitions
GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>

View File

@ -2,7 +2,6 @@
// File: tablefind.h
// Description: Helper classes to find tables from ColPartitions.
// Author: Faisal Shafait (faisal.shafait@dfki.de)
// Created: Tue Jan 06 11:13:01 PST 2009
//
// (C) Copyright 2009, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
@ -156,7 +155,7 @@ class TableFinder {
// if that feature is enabled.
void LocateTables(ColPartitionGrid* grid,
ColPartitionSet** columns,
WidthCallback* width_cb,
WidthCallback width_cb,
const FCOORD& reskew);
protected:
@ -394,7 +393,7 @@ class TableFinder {
// assigned to any table to their original types.
void MakeTableBlocks(ColPartitionGrid* grid,
ColPartitionSet** columns,
WidthCallback* width_cb);
WidthCallback width_cb);
/////////////////////////////////////////////////
// Useful objects used during table find process.