diff --git a/src/textord/ccnontextdetect.cpp b/src/textord/ccnontextdetect.cpp index 734c75fd..5c28dbc1 100644 --- a/src/textord/ccnontextdetect.cpp +++ b/src/textord/ccnontextdetect.cpp @@ -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 diff --git a/src/textord/colpartition.cpp b/src/textord/colpartition.cpp index ec567507..943243b2 100644 --- a/src/textord/colpartition.cpp +++ b/src/textord/colpartition.cpp @@ -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_; } diff --git a/src/textord/colpartition.h b/src/textord/colpartition.h index 702b6ea9..06c9e22c 100644 --- a/src/textord/colpartition.h +++ b/src/textord/colpartition.h @@ -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. diff --git a/src/textord/colpartitiongrid.cpp b/src/textord/colpartitiongrid.cpp index e2ff4ec2..70305b7a 100644 --- a/src/textord/colpartitiongrid.cpp +++ b/src/textord/colpartitiongrid.cpp @@ -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* box_cb, - TessResultCallback2* confirm_cb) { + std::function box_cb, + std::function 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* box_cb, - TessResultCallback2* confirm_cb, + std::function box_cb, + std::function 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* confirm_cb, + std::function 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. diff --git a/src/textord/colpartitiongrid.h b/src/textord/colpartitiongrid.h index df32f05c..32bb46e3 100644 --- a/src/textord/colpartitiongrid.h +++ b/src/textord/colpartitiongrid.h @@ -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* box_cb, - TessResultCallback2* confirm_cb); + void Merges(std::function box_cb, + std::function 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* box_cb, - TessResultCallback2* confirm_cb, + bool MergePart(std::function box_cb, + std::function confirm_cb, ColPartition* part); // Computes and returns the total overlap of all partitions in the grid. @@ -82,8 +81,8 @@ class ColPartitionGrid : public BBGrid* confirm_cb, + std::function confirm_cb, int* overlap_increase); // Split partitions where it reduces overlap between their bounding boxes. @@ -133,7 +132,7 @@ class ColPartitionGrid : public BBGridsize(); // 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(); diff --git a/src/textord/colpartitionset.h b/src/textord/colpartitionset.h index f201a0bd..57b61b34 100644 --- a/src/textord/colpartitionset.h +++ b/src/textord/colpartitionset.h @@ -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 diff --git a/src/textord/strokewidth.cpp b/src/textord/strokewidth.cpp index a0e054fa..5355fcb2 100644 --- a/src/textord/strokewidth.cpp +++ b/src/textord/strokewidth.cpp @@ -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. diff --git a/src/textord/tabfind.cpp b/src/textord/tabfind.cpp index f84aedea..b198ab11 100644 --- a/src/textord/tabfind.cpp +++ b/src/textord/tabfind.cpp @@ -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). ////////////// diff --git a/src/textord/tabfind.h b/src/textord/tabfind.h index 7323ed4b..aaccb763 100644 --- a/src/textord/tabfind.h +++ b/src/textord/tabfind.h @@ -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 // 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; +using WidthCallback = std::function; 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 left_tab_boxes_; GenericVector right_tab_boxes_; diff --git a/src/textord/tablefind.cpp b/src/textord/tablefind.cpp index 4f2529f4..f6253cbb 100644 --- a/src/textord/tablefind.cpp +++ b/src/textord/tablefind.cpp @@ -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 diff --git a/src/textord/tablefind.h b/src/textord/tablefind.h index 20e746a2..552d917f 100644 --- a/src/textord/tablefind.h +++ b/src/textord/tablefind.h @@ -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.